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 SkBitmap bitmap; 52 bitmap.allocPixels(codec->getInfo()); 53 codec->getPixels(codec->getInfo(), bitmap.getPixels(), bitmap.rowBytes()); 54 55 // The GM will be displayed in a 2x2 grid. 56 // The top two squares show an sRGB image, then bitmap, drawn to a legacy canvas. 57 SkImageInfo linearInfo = SkImageInfo::MakeN32(2*kSize, kSize, kOpaque_SkAlphaType); 58 SkBitmap legacyBMCanvas; 59 legacyBMCanvas.allocPixels(linearInfo); 60 SkCanvas legacyCanvas(legacyBMCanvas); 61 legacyCanvas.drawImage(image, 0.0f, 0.0f, nullptr); 62 legacyCanvas.translate(SkScalar(kSize), 0.0f); 63 legacyCanvas.drawBitmap(bitmap, 0.0f, 0.0f, nullptr); 64 canvas->drawBitmap(legacyBMCanvas, 0.0f, 0.0f, nullptr); 65 canvas->translate(0.0f, SkScalar(kSize)); 66 67 // The bottom two squares show an sRGB image, then bitmap, drawn to a srgb canvas. 68 SkImageInfo srgbInfo = SkImageInfo::MakeS32(2*kSize, kSize, kOpaque_SkAlphaType); 69 SkBitmap srgbBMCanvas; 70 srgbBMCanvas.allocPixels(srgbInfo); 71 SkCanvas srgbCanvas(srgbBMCanvas); 72 srgbCanvas.drawImage(image, 0.0f, 0.0f, nullptr); 73 srgbCanvas.translate(SkScalar(kSize), 0.0f); 74 srgbCanvas.drawBitmap(bitmap, 0.0f, 0.0f, nullptr); 75 canvas->drawBitmap(srgbBMCanvas, 0.0f, 0.0f, nullptr); 76 return DrawResult::kOk; 77 } 78 79 private: 80 static constexpr int kSize = 512; 81 82 typedef GM INHERITED; 83 }; 84 85 ////////////////////////////////////////////////////////////////////////////// 86 87 DEF_GM( return new BitmapImageGM; ) 88 89 } 90