1cb93a386Sopenharmony_ci// Copyright 2019 Google LLC.
2cb93a386Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3cb93a386Sopenharmony_ci#include "tools/fiddle/examples.h"
4cb93a386Sopenharmony_ci// HASH=df4e355c4845350daede833b4fd21ec1
5cb93a386Sopenharmony_ciREG_FIDDLE(Pixmap_readPixels, 256, 128, false, 0) {
6cb93a386Sopenharmony_civoid draw(SkCanvas* canvas) {
7cb93a386Sopenharmony_ci    std::vector<int32_t> pixels;
8cb93a386Sopenharmony_ci    const int width = 256;
9cb93a386Sopenharmony_ci    const int height = 64;
10cb93a386Sopenharmony_ci    pixels.resize(height * width * 4);
11cb93a386Sopenharmony_ci    SkImageInfo srcInfo = SkImageInfo::MakeN32Premul(width, height);
12cb93a386Sopenharmony_ci    SkPixmap srcPixmap(srcInfo, (const void*) &pixels.front(), width * 4);
13cb93a386Sopenharmony_ci    SkColor  gradColors[] = { 0xFFAA3300, 0x7F881122 };
14cb93a386Sopenharmony_ci    SkPoint  gradPoints[] = { { 0, 0 }, { 256, 0 } };
15cb93a386Sopenharmony_ci    SkPaint paint;
16cb93a386Sopenharmony_ci    paint.setShader(SkGradientShader::MakeLinear(gradPoints, gradColors, nullptr,
17cb93a386Sopenharmony_ci                    SK_ARRAY_COUNT(gradColors), SkTileMode::kClamp));
18cb93a386Sopenharmony_ci    SkBitmap bitmap;
19cb93a386Sopenharmony_ci    bitmap.installPixels(srcPixmap);
20cb93a386Sopenharmony_ci    SkCanvas srcCanvas(bitmap);
21cb93a386Sopenharmony_ci    srcCanvas.drawRect(SkRect::MakeWH(width, height), paint);
22cb93a386Sopenharmony_ci    canvas->drawImage(bitmap.asImage(), 0, 0);
23cb93a386Sopenharmony_ci    std::vector<int32_t> dstPixels;
24cb93a386Sopenharmony_ci    dstPixels.resize(height * width * 2);
25cb93a386Sopenharmony_ci    SkImageInfo dstInfo = srcInfo.makeColorType(kARGB_4444_SkColorType);
26cb93a386Sopenharmony_ci    srcPixmap.readPixels(dstInfo, &dstPixels.front(), width * 2);
27cb93a386Sopenharmony_ci    SkPixmap dstPixmap(dstInfo, &dstPixels.front(), width * 2);
28cb93a386Sopenharmony_ci    bitmap.installPixels(dstPixmap);
29cb93a386Sopenharmony_ci    canvas->drawImage(bitmap.asImage(), 0, 128);
30cb93a386Sopenharmony_ci}
31cb93a386Sopenharmony_ci}  // END FIDDLE
32