blob: 225d56cebbabb98fc6136f68fa9bee9262ac2468 [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"
Matt Saretta9da22d2016-12-08 08:32:44 -050010#include "SkCodec.h"
Florin Malitaab244f02017-05-03 19:16:58 +000011#include "SkImage.h"
msarettbe36d592016-06-13 11:25:03 -070012
13namespace skiagm {
14
15class BitmapImageGM : public GM {
16public:
17 BitmapImageGM() {}
18
19protected:
20
21 SkString onShortName() override {
Matt Saretta9da22d2016-12-08 08:32:44 -050022 return SkString("bitmap-image-srgb-legacy");
msarettbe36d592016-06-13 11:25:03 -070023 }
24
25 SkISize onISize() override {
26 return SkISize::Make(2*kSize, 2*kSize);
27 }
28
29 void onDraw(SkCanvas* canvas) override {
30 // Create image.
Hal Canaryc465d132017-12-08 10:21:31 -050031 const char* path = "images/mandrill_512_q075.jpg";
Matt Saretta9da22d2016-12-08 08:32:44 -050032 sk_sp<SkImage> image = GetResourceAsImage(path);
msarettbe36d592016-06-13 11:25:03 -070033 if (!image) {
34 SkDebugf("Failure: Is the resource path set properly?");
35 return;
36 }
37
38 // Create matching bitmap.
Mike Reedede7bac2017-07-23 15:30:02 -040039 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(GetResourceAsStream(path)));
msarettbe36d592016-06-13 11:25:03 -070040 SkBitmap bitmap;
Matt Saretta9da22d2016-12-08 08:32:44 -050041 bitmap.allocPixels(codec->getInfo());
42 codec->getPixels(codec->getInfo(), bitmap.getPixels(), bitmap.rowBytes());
msarettbe36d592016-06-13 11:25:03 -070043
44 // The GM will be displayed in a 2x2 grid.
Matt Saretta9da22d2016-12-08 08:32:44 -050045 // The top two squares show an sRGB image, then bitmap, drawn to a legacy canvas.
msarettbe36d592016-06-13 11:25:03 -070046 SkImageInfo linearInfo = SkImageInfo::MakeN32(2*kSize, kSize, kOpaque_SkAlphaType);
Matt Saretta9da22d2016-12-08 08:32:44 -050047 SkBitmap legacyBMCanvas;
48 legacyBMCanvas.allocPixels(linearInfo);
49 SkCanvas legacyCanvas(legacyBMCanvas);
50 legacyCanvas.drawImage(image, 0.0f, 0.0f, nullptr);
51 legacyCanvas.translate(SkScalar(kSize), 0.0f);
52 legacyCanvas.drawBitmap(bitmap, 0.0f, 0.0f, nullptr);
53 canvas->drawBitmap(legacyBMCanvas, 0.0f, 0.0f, nullptr);
msarettbe36d592016-06-13 11:25:03 -070054 canvas->translate(0.0f, SkScalar(kSize));
55
56 // The bottom two squares show an sRGB image, then bitmap, drawn to a srgb canvas.
57 SkImageInfo srgbInfo = SkImageInfo::MakeS32(2*kSize, kSize, kOpaque_SkAlphaType);
58 SkBitmap srgbBMCanvas;
59 srgbBMCanvas.allocPixels(srgbInfo);
60 SkCanvas srgbCanvas(srgbBMCanvas);
61 srgbCanvas.drawImage(image, 0.0f, 0.0f, nullptr);
62 srgbCanvas.translate(SkScalar(kSize), 0.0f);
63 srgbCanvas.drawBitmap(bitmap, 0.0f, 0.0f, nullptr);
64 canvas->drawBitmap(srgbBMCanvas, 0.0f, 0.0f, nullptr);
65 }
66
67private:
68 static constexpr int kSize = 512;
69
70 typedef GM INHERITED;
71};
72
73//////////////////////////////////////////////////////////////////////////////
74
75static GM* MyFactory(void*) { return new BitmapImageGM; }
76static GMRegistry reg(MyFactory);
77
78}