blob: 8c37d5da44902a4561a6864b5e126c33b86c6cb0 [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"
msarettbe36d592016-06-13 11:25:03 -070011
12namespace skiagm {
13
14class BitmapImageGM : public GM {
15public:
16 BitmapImageGM() {}
17
18protected:
19
20 SkString onShortName() override {
Matt Saretta9da22d2016-12-08 08:32:44 -050021 return SkString("bitmap-image-srgb-legacy");
msarettbe36d592016-06-13 11:25:03 -070022 }
23
24 SkISize onISize() override {
25 return SkISize::Make(2*kSize, 2*kSize);
26 }
27
28 void onDraw(SkCanvas* canvas) override {
29 // Create image.
Matt Saretta9da22d2016-12-08 08:32:44 -050030 const char* path = "mandrill_512_q075.jpg";
31 sk_sp<SkImage> image = GetResourceAsImage(path);
msarettbe36d592016-06-13 11:25:03 -070032 if (!image) {
33 SkDebugf("Failure: Is the resource path set properly?");
34 return;
35 }
36
37 // Create matching bitmap.
Matt Saretta9da22d2016-12-08 08:32:44 -050038 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(GetResourceAsStream(path)));
msarettbe36d592016-06-13 11:25:03 -070039 SkBitmap bitmap;
Matt Saretta9da22d2016-12-08 08:32:44 -050040 bitmap.allocPixels(codec->getInfo());
41 codec->getPixels(codec->getInfo(), bitmap.getPixels(), bitmap.rowBytes());
msarettbe36d592016-06-13 11:25:03 -070042
43 // The GM will be displayed in a 2x2 grid.
Matt Saretta9da22d2016-12-08 08:32:44 -050044 // The top two squares show an sRGB image, then bitmap, drawn to a legacy canvas.
msarettbe36d592016-06-13 11:25:03 -070045 SkImageInfo linearInfo = SkImageInfo::MakeN32(2*kSize, kSize, kOpaque_SkAlphaType);
Matt Saretta9da22d2016-12-08 08:32:44 -050046 SkBitmap legacyBMCanvas;
47 legacyBMCanvas.allocPixels(linearInfo);
48 SkCanvas legacyCanvas(legacyBMCanvas);
49 legacyCanvas.drawImage(image, 0.0f, 0.0f, nullptr);
50 legacyCanvas.translate(SkScalar(kSize), 0.0f);
51 legacyCanvas.drawBitmap(bitmap, 0.0f, 0.0f, nullptr);
52 canvas->drawBitmap(legacyBMCanvas, 0.0f, 0.0f, nullptr);
msarettbe36d592016-06-13 11:25:03 -070053 canvas->translate(0.0f, SkScalar(kSize));
54
55 // The bottom two squares show an sRGB image, then bitmap, drawn to a srgb canvas.
56 SkImageInfo srgbInfo = SkImageInfo::MakeS32(2*kSize, kSize, kOpaque_SkAlphaType);
57 SkBitmap srgbBMCanvas;
58 srgbBMCanvas.allocPixels(srgbInfo);
59 SkCanvas srgbCanvas(srgbBMCanvas);
60 srgbCanvas.drawImage(image, 0.0f, 0.0f, nullptr);
61 srgbCanvas.translate(SkScalar(kSize), 0.0f);
62 srgbCanvas.drawBitmap(bitmap, 0.0f, 0.0f, nullptr);
63 canvas->drawBitmap(srgbBMCanvas, 0.0f, 0.0f, nullptr);
64 }
65
66private:
67 static constexpr int kSize = 512;
68
69 typedef GM INHERITED;
70};
71
72//////////////////////////////////////////////////////////////////////////////
73
74static GM* MyFactory(void*) { return new BitmapImageGM; }
75static GMRegistry reg(MyFactory);
76
77}