msarett | be36d59 | 2016-06-13 11:25:03 -0700 | [diff] [blame] | 1 | /* |
| 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 Sarett | a9da22d | 2016-12-08 08:32:44 -0500 | [diff] [blame] | 10 | #include "SkCodec.h" |
msarett | be36d59 | 2016-06-13 11:25:03 -0700 | [diff] [blame] | 11 | |
| 12 | namespace skiagm { |
| 13 | |
| 14 | class BitmapImageGM : public GM { |
| 15 | public: |
| 16 | BitmapImageGM() {} |
| 17 | |
| 18 | protected: |
| 19 | |
| 20 | SkString onShortName() override { |
Matt Sarett | a9da22d | 2016-12-08 08:32:44 -0500 | [diff] [blame] | 21 | return SkString("bitmap-image-srgb-legacy"); |
msarett | be36d59 | 2016-06-13 11:25:03 -0700 | [diff] [blame] | 22 | } |
| 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 Sarett | a9da22d | 2016-12-08 08:32:44 -0500 | [diff] [blame] | 30 | const char* path = "mandrill_512_q075.jpg"; |
| 31 | sk_sp<SkImage> image = GetResourceAsImage(path); |
msarett | be36d59 | 2016-06-13 11:25:03 -0700 | [diff] [blame] | 32 | if (!image) { |
| 33 | SkDebugf("Failure: Is the resource path set properly?"); |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | // Create matching bitmap. |
Matt Sarett | a9da22d | 2016-12-08 08:32:44 -0500 | [diff] [blame] | 38 | std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(GetResourceAsStream(path))); |
msarett | be36d59 | 2016-06-13 11:25:03 -0700 | [diff] [blame] | 39 | SkBitmap bitmap; |
Matt Sarett | a9da22d | 2016-12-08 08:32:44 -0500 | [diff] [blame] | 40 | bitmap.allocPixels(codec->getInfo()); |
| 41 | codec->getPixels(codec->getInfo(), bitmap.getPixels(), bitmap.rowBytes()); |
msarett | be36d59 | 2016-06-13 11:25:03 -0700 | [diff] [blame] | 42 | |
| 43 | // The GM will be displayed in a 2x2 grid. |
Matt Sarett | a9da22d | 2016-12-08 08:32:44 -0500 | [diff] [blame] | 44 | // The top two squares show an sRGB image, then bitmap, drawn to a legacy canvas. |
msarett | be36d59 | 2016-06-13 11:25:03 -0700 | [diff] [blame] | 45 | SkImageInfo linearInfo = SkImageInfo::MakeN32(2*kSize, kSize, kOpaque_SkAlphaType); |
Matt Sarett | a9da22d | 2016-12-08 08:32:44 -0500 | [diff] [blame] | 46 | 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); |
msarett | be36d59 | 2016-06-13 11:25:03 -0700 | [diff] [blame] | 53 | 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 | |
| 66 | private: |
| 67 | static constexpr int kSize = 512; |
| 68 | |
| 69 | typedef GM INHERITED; |
| 70 | }; |
| 71 | |
| 72 | ////////////////////////////////////////////////////////////////////////////// |
| 73 | |
| 74 | static GM* MyFactory(void*) { return new BitmapImageGM; } |
| 75 | static GMRegistry reg(MyFactory); |
| 76 | |
| 77 | } |