xref: /aosp_15_r20/external/skia/docs/examples/Pixmap_readPixels_2.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 // Copyright 2019 Google LLC.
2 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3 #include "tools/fiddle/examples.h"
4 REG_FIDDLE(Pixmap_readPixels_2, 256, 256, false, 3) {
draw(SkCanvas * canvas)5 void draw(SkCanvas* canvas) {
6     SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
7     std::vector<int32_t> srcPixels;
8     const int rowBytes = image->width() * 4;
9     srcPixels.resize(image->height() * rowBytes);
10     SkPixmap pixmap(info, (const void*) &srcPixels.front(), rowBytes);
11     image->readPixels(nullptr, pixmap, 0, 0);
12     for (int offset : { 32, 64, 96 } ) {
13         std::vector<int32_t> dstPixels;
14         dstPixels.resize(image->height() * rowBytes);
15         pixmap.readPixels(info, &dstPixels.front(), rowBytes, offset, 0);
16         SkBitmap bitmap;
17         SkPixmap dstmap(info, &dstPixels.front(), rowBytes);
18         bitmap.installPixels(dstmap);
19         canvas->translate(32, 32);
20         canvas->drawImage(bitmap.asImage(), 0, 0);
21     }
22 }
23 }  // END FIDDLE
24