xref: /aosp_15_r20/external/skia/docs/examples/ImageInfo_computeOffset.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(ImageInfo_computeOffset, 256, 128, false, 0) {
draw(SkCanvas * canvas)5 void draw(SkCanvas* canvas) {
6     uint8_t pixels[][12] = { { 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00},
7                              { 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00},
8                              { 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00},
9                              { 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0xFF},
10                              { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
11                              { 0x00, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00},
12                              { 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00},
13                              { 0x00, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00} };
14     SkImageInfo imageInfo = SkImageInfo::MakeA8(8, 8);
15     SkBitmap bitmap;
16     bitmap.installPixels(imageInfo, (void*) pixels, sizeof(pixels[0]));
17     SkPaint paint;
18     paint.setColor(SK_ColorRED);
19     canvas->drawImageRect(bitmap.asImage(), SkRect::MakeWH(8, 8), SkRect::MakeWH(32, 32),
20                           SkSamplingOptions(), &paint, SkCanvas::kStrict_SrcRectConstraint);
21     size_t offset = imageInfo.computeOffset(2, 3, sizeof(pixels[0]));
22     pixels[0][offset] = 0x7F;
23     offset = imageInfo.computeOffset(5, 3, sizeof(pixels[0]));
24     pixels[0][offset] = 0x7F;
25     bitmap.installPixels(imageInfo, (void*) pixels, sizeof(pixels[0]));
26     canvas->drawImageRect(bitmap.asImage(), SkRect::MakeWH(8, 8), SkRect::MakeWH(128, 128),
27                           SkSamplingOptions(), &paint, SkCanvas::kStrict_SrcRectConstraint);
28 }
29 }  // END FIDDLE
30