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" |
Florin Malita | ab244f0 | 2017-05-03 19:16:58 +0000 | [diff] [blame] | 11 | #include "SkImage.h" |
msarett | be36d59 | 2016-06-13 11:25:03 -0700 | [diff] [blame] | 12 | |
| 13 | namespace skiagm { |
| 14 | |
| 15 | class BitmapImageGM : public GM { |
| 16 | public: |
| 17 | BitmapImageGM() {} |
| 18 | |
| 19 | protected: |
| 20 | |
| 21 | SkString onShortName() override { |
Matt Sarett | a9da22d | 2016-12-08 08:32:44 -0500 | [diff] [blame] | 22 | return SkString("bitmap-image-srgb-legacy"); |
msarett | be36d59 | 2016-06-13 11:25:03 -0700 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | SkISize onISize() override { |
| 26 | return SkISize::Make(2*kSize, 2*kSize); |
| 27 | } |
| 28 | |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 29 | DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { |
msarett | be36d59 | 2016-06-13 11:25:03 -0700 | [diff] [blame] | 30 | // Create image. |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 31 | const char* path = "images/mandrill_512_q075.jpg"; |
Matt Sarett | a9da22d | 2016-12-08 08:32:44 -0500 | [diff] [blame] | 32 | sk_sp<SkImage> image = GetResourceAsImage(path); |
msarett | be36d59 | 2016-06-13 11:25:03 -0700 | [diff] [blame] | 33 | if (!image) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 34 | *errorMsg = "Couldn't load images/mandrill_512_q075.jpg. " |
| 35 | "Did you forget to set the resource path?"; |
| 36 | return DrawResult::kFail; |
msarett | be36d59 | 2016-06-13 11:25:03 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | // Create matching bitmap. |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 40 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(GetResourceAsStream(path))); |
msarett | be36d59 | 2016-06-13 11:25:03 -0700 | [diff] [blame] | 41 | SkBitmap bitmap; |
Matt Sarett | a9da22d | 2016-12-08 08:32:44 -0500 | [diff] [blame] | 42 | bitmap.allocPixels(codec->getInfo()); |
| 43 | codec->getPixels(codec->getInfo(), bitmap.getPixels(), bitmap.rowBytes()); |
msarett | be36d59 | 2016-06-13 11:25:03 -0700 | [diff] [blame] | 44 | |
| 45 | // The GM will be displayed in a 2x2 grid. |
Matt Sarett | a9da22d | 2016-12-08 08:32:44 -0500 | [diff] [blame] | 46 | // 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] | 47 | SkImageInfo linearInfo = SkImageInfo::MakeN32(2*kSize, kSize, kOpaque_SkAlphaType); |
Matt Sarett | a9da22d | 2016-12-08 08:32:44 -0500 | [diff] [blame] | 48 | SkBitmap legacyBMCanvas; |
| 49 | legacyBMCanvas.allocPixels(linearInfo); |
| 50 | SkCanvas legacyCanvas(legacyBMCanvas); |
| 51 | legacyCanvas.drawImage(image, 0.0f, 0.0f, nullptr); |
| 52 | legacyCanvas.translate(SkScalar(kSize), 0.0f); |
| 53 | legacyCanvas.drawBitmap(bitmap, 0.0f, 0.0f, nullptr); |
| 54 | canvas->drawBitmap(legacyBMCanvas, 0.0f, 0.0f, nullptr); |
msarett | be36d59 | 2016-06-13 11:25:03 -0700 | [diff] [blame] | 55 | canvas->translate(0.0f, SkScalar(kSize)); |
| 56 | |
| 57 | // The bottom two squares show an sRGB image, then bitmap, drawn to a srgb canvas. |
| 58 | SkImageInfo srgbInfo = SkImageInfo::MakeS32(2*kSize, kSize, kOpaque_SkAlphaType); |
| 59 | SkBitmap srgbBMCanvas; |
| 60 | srgbBMCanvas.allocPixels(srgbInfo); |
| 61 | SkCanvas srgbCanvas(srgbBMCanvas); |
| 62 | srgbCanvas.drawImage(image, 0.0f, 0.0f, nullptr); |
| 63 | srgbCanvas.translate(SkScalar(kSize), 0.0f); |
| 64 | srgbCanvas.drawBitmap(bitmap, 0.0f, 0.0f, nullptr); |
| 65 | canvas->drawBitmap(srgbBMCanvas, 0.0f, 0.0f, nullptr); |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 66 | return DrawResult::kOk; |
msarett | be36d59 | 2016-06-13 11:25:03 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | private: |
| 70 | static constexpr int kSize = 512; |
| 71 | |
| 72 | typedef GM INHERITED; |
| 73 | }; |
| 74 | |
| 75 | ////////////////////////////////////////////////////////////////////////////// |
| 76 | |
Hal Canary | e964c18 | 2019-01-23 10:22:01 -0500 | [diff] [blame] | 77 | DEF_GM( return new BitmapImageGM; ) |
msarett | be36d59 | 2016-06-13 11:25:03 -0700 | [diff] [blame] | 78 | |
| 79 | } |