xref: /aosp_15_r20/external/skia/docs/examples/Image_readPixels.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(Image_readPixels, 256, 256, false, 3) {
draw(SkCanvas * canvas)5 void draw(SkCanvas* canvas) {
6     canvas->scale(.5f, .5f);
7     const int width = 32;
8     const int height = 32;
9     std::vector<int32_t> dstPixels;
10     dstPixels.resize(height * width * 4);
11     SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
12     for (int y = 0; y < 512; y += height ) {
13         for (int x = 0; x < 512; x += width ) {
14             if (image->readPixels(nullptr, info, &dstPixels.front(), width * 4, x, y)) {
15                 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
16                 SkBitmap bitmap;
17                 bitmap.installPixels(dstPixmap);
18                 canvas->drawImage(bitmap.asImage(), 0, 0);
19             }
20             canvas->translate(48, 0);
21         }
22         canvas->translate(-16 * 48, 48);
23     }
24 }
25 }  // END FIDDLE
26