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/gm.h" 9 #include "include/codec/SkCodec.h" 10 #include "include/core/SkBitmap.h" 11 #include "include/core/SkCanvas.h" 12 #include "include/core/SkImage.h" 13 #include "include/core/SkImageInfo.h" 14 #include "include/core/SkRefCnt.h" 15 #include "include/core/SkScalar.h" 16 #include "include/core/SkSize.h" 17 #include "include/core/SkStream.h" 18 #include "include/core/SkString.h" 19 #include "tools/DecodeUtils.h" 20 #include "tools/Resources.h" 21 22 #include <memory> 23 24 namespace skiagm { 25 26 class BitmapImageGM : public GM { 27 public: BitmapImageGM()28 BitmapImageGM() {} 29 30 protected: getName() const31 SkString getName() const override { return SkString("bitmap-image-srgb-legacy"); } 32 getISize()33 SkISize getISize() override { return SkISize::Make(2 * kSize, 2 * kSize); } 34 onDraw(SkCanvas * canvas,SkString * errorMsg)35 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { 36 // Create image. 37 const char* path = "images/mandrill_512_q075.jpg"; 38 sk_sp<SkImage> image = ToolUtils::GetResourceAsImage(path); 39 if (!image) { 40 *errorMsg = "Couldn't load images/mandrill_512_q075.jpg. " 41 "Did you forget to set the resource path?"; 42 return DrawResult::kFail; 43 } 44 45 // Create matching bitmap. 46 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(GetResourceAsStream(path))); 47 auto [codecImage, _] = codec->getImage(); 48 49 // The GM will be displayed in a 2x2 grid. 50 // The top two squares show an sRGB image, then bitmap, drawn to a legacy canvas. 51 SkImageInfo linearInfo = SkImageInfo::MakeN32(2*kSize, kSize, kOpaque_SkAlphaType); 52 SkBitmap legacyBMCanvas; 53 legacyBMCanvas.allocPixels(linearInfo); 54 SkCanvas legacyCanvas(legacyBMCanvas); 55 legacyCanvas.drawImage(image, 0.0f, 0.0f); 56 legacyCanvas.translate(SkScalar(kSize), 0.0f); 57 legacyCanvas.drawImage(codecImage, 0.0f, 0.0f); 58 canvas->drawImage(legacyBMCanvas.asImage(), 0.0f, 0.0f); 59 canvas->translate(0.0f, SkScalar(kSize)); 60 61 // The bottom two squares show an sRGB image, then bitmap, drawn to a srgb canvas. 62 SkImageInfo srgbInfo = SkImageInfo::MakeS32(2*kSize, kSize, kOpaque_SkAlphaType); 63 SkBitmap srgbBMCanvas; 64 srgbBMCanvas.allocPixels(srgbInfo); 65 SkCanvas srgbCanvas(srgbBMCanvas); 66 srgbCanvas.drawImage(image, 0.0f, 0.0f); 67 srgbCanvas.translate(SkScalar(kSize), 0.0f); 68 srgbCanvas.drawImage(codecImage, 0.0f, 0.0f); 69 canvas->drawImage(srgbBMCanvas.asImage(), 0.0f, 0.0f); 70 return DrawResult::kOk; 71 } 72 73 private: 74 inline static constexpr int kSize = 512; 75 76 using INHERITED = GM; 77 }; 78 79 ////////////////////////////////////////////////////////////////////////////// 80 81 DEF_GM( return new BitmapImageGM; ) 82 83 } // namespace skiagm 84