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