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