blob: ad6a74dda86a561154a19502edd7f5727a3acdbd [file] [log] [blame]
msarettbe36d592016-06-13 11:25:03 -07001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "gm.h"
9#include "Resources.h"
10
11namespace skiagm {
12
13class BitmapImageGM : public GM {
14public:
15 BitmapImageGM() {}
16
17protected:
18
19 SkString onShortName() override {
20 return SkString("bitmap-image-srgb-linear");
21 }
22
23 SkISize onISize() override {
24 return SkISize::Make(2*kSize, 2*kSize);
25 }
26
27 void onDraw(SkCanvas* canvas) override {
28 // Create image.
29 sk_sp<SkImage> image = GetResourceAsImage("mandrill_512_q075.jpg");
30 if (!image) {
31 SkDebugf("Failure: Is the resource path set properly?");
32 return;
33 }
34
35 // Create matching bitmap.
36 SkBitmap bitmap;
37 SkAssertResult(image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode));
38
39 // The GM will be displayed in a 2x2 grid.
40 // The top two squares show an sRGB image, then bitmap, drawn to a linear canvas.
41 SkImageInfo linearInfo = SkImageInfo::MakeN32(2*kSize, kSize, kOpaque_SkAlphaType);
42 SkBitmap linearBMCanvas;
43 linearBMCanvas.allocPixels(linearInfo);
44 SkCanvas linearCanvas(linearBMCanvas);
45 linearCanvas.drawImage(image, 0.0f, 0.0f, nullptr);
46 linearCanvas.translate(SkScalar(kSize), 0.0f);
47 linearCanvas.drawBitmap(bitmap, 0.0f, 0.0f, nullptr);
48 canvas->drawBitmap(linearBMCanvas, 0.0f, 0.0f, nullptr);
49 canvas->translate(0.0f, SkScalar(kSize));
50
51 // The bottom two squares show an sRGB image, then bitmap, drawn to a srgb canvas.
52 SkImageInfo srgbInfo = SkImageInfo::MakeS32(2*kSize, kSize, kOpaque_SkAlphaType);
53 SkBitmap srgbBMCanvas;
54 srgbBMCanvas.allocPixels(srgbInfo);
55 SkCanvas srgbCanvas(srgbBMCanvas);
56 srgbCanvas.drawImage(image, 0.0f, 0.0f, nullptr);
57 srgbCanvas.translate(SkScalar(kSize), 0.0f);
58 srgbCanvas.drawBitmap(bitmap, 0.0f, 0.0f, nullptr);
59 canvas->drawBitmap(srgbBMCanvas, 0.0f, 0.0f, nullptr);
60 }
61
62private:
63 static constexpr int kSize = 512;
64
65 typedef GM INHERITED;
66};
67
68//////////////////////////////////////////////////////////////////////////////
69
70static GM* MyFactory(void*) { return new BitmapImageGM; }
71static GMRegistry reg(MyFactory);
72
73}