• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // HASH=b2cbbbbcffb618865d8aae3bc04b2a62
5 REG_FIDDLE(Bitmap_readPixels, 256, 128, false, 0) {
draw(SkCanvas * canvas)6 void draw(SkCanvas* canvas) {
7     const int width = 256;
8     const int height = 64;
9     SkImageInfo srcInfo = SkImageInfo::MakeN32Premul(width, height);
10     SkColor  gradColors[] = { 0xFFAA3300, 0x7F881122 };
11     SkPoint  gradPoints[] = { { 0, 0 }, { 256, 0 } };
12     SkPaint paint;
13     paint.setShader(SkGradientShader::MakeLinear(gradPoints, gradColors, nullptr,
14                     SK_ARRAY_COUNT(gradColors), SkTileMode::kClamp));
15     SkBitmap bitmap;
16     bitmap.allocPixels(srcInfo);
17     SkCanvas srcCanvas(bitmap);
18     srcCanvas.drawRect(SkRect::MakeWH(width, height), paint);
19     canvas->drawImage(bitmap.asImage(), 0, 0);
20     SkImageInfo dstInfo = srcInfo.makeColorType(kARGB_4444_SkColorType);
21     std::vector<int16_t> dstPixels;
22     dstPixels.resize(height * width);
23     bitmap.readPixels(dstInfo, &dstPixels.front(), width * 2, 0, 0);
24     SkPixmap dstPixmap(dstInfo, &dstPixels.front(), width * 2);
25     bitmap.installPixels(dstPixmap);
26     canvas->drawImage(bitmap.asImage(), 0, 64);
27 }
28 }  // END FIDDLE
29