1 /* 2 * Copyright 2013 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/core/SkBitmap.h" 10 #include "include/core/SkCanvas.h" 11 #include "include/core/SkColor.h" 12 #include "include/core/SkImageInfo.h" 13 #include "include/core/SkPixmap.h" 14 #include "include/core/SkScalar.h" 15 #include "include/core/SkSize.h" 16 #include "include/core/SkString.h" 17 #include "include/core/SkTypes.h" 18 #include "tools/DecodeUtils.h" 19 #include "tools/Resources.h" 20 #include "tools/ToolUtils.h" 21 22 namespace { 23 /** 24 * Test copying an image from 8888 to 4444. 25 */ 26 class CopyTo4444GM : public skiagm::GM { getName() const27 SkString getName() const override { return SkString("copyTo4444"); } 28 getISize()29 SkISize getISize() override { return {360, 180}; } 30 onDraw(SkCanvas * canvas,SkString * errorMsg)31 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { 32 SkBitmap bm, bm4444; 33 if (!ToolUtils::GetResourceAsBitmap("images/dog.jpg", &bm)) { 34 *errorMsg = "Could not decode the file. Did you forget to set the resourcePath?"; 35 return DrawResult::kFail; 36 } 37 canvas->drawImage(bm.asImage(), 0, 0); 38 39 // This should dither or we will see artifacts in the background of the image. 40 SkAssertResult(ToolUtils::copy_to(&bm4444, kARGB_4444_SkColorType, bm)); 41 canvas->drawImage(bm4444.asImage(), SkIntToScalar(bm.width()), 0); 42 return DrawResult::kOk; 43 } 44 }; 45 } // namespace 46 47 DEF_GM( return new CopyTo4444GM; ) 48 49 ////////////////////////////////////////////////////////////////////////////// 50 51 DEF_SIMPLE_GM(format4444, canvas, 64, 64) { 52 canvas->scale(16, 16); 53 SkBitmap bitmap; 54 SkImageInfo imageInfo = SkImageInfo::Make(1, 1, kARGB_4444_SkColorType, kPremul_SkAlphaType); 55 bitmap.allocPixels(imageInfo); 56 SkCanvas offscreen(bitmap); 57 offscreen.clear(SK_ColorRED); 58 canvas->drawImage(bitmap.asImage(), 0, 0); 59 offscreen.clear(SK_ColorBLUE); 60 canvas->drawImage(bitmap.asImage(), 1, 1); __anon8392701e0202(unsigned a, unsigned r, unsigned g, unsigned b) 61 auto pack4444 = [](unsigned a, unsigned r, unsigned g, unsigned b) -> uint16_t { 62 return (a << 0) | (b << 4) | (g << 8) | (r << 12); 63 }; 64 uint16_t red4444 = pack4444(0xF, 0xF, 0x0, 0x0); 65 uint16_t blue4444 = pack4444(0xF, 0x0, 0x0, 0x0F); 66 SkPixmap redPixmap(imageInfo, &red4444, 2); 67 if (bitmap.writePixels(redPixmap, 0, 0)) { 68 canvas->drawImage(bitmap.asImage(), 2, 2); 69 } 70 SkPixmap bluePixmap(imageInfo, &blue4444, 2); 71 if (bitmap.writePixels(bluePixmap, 0, 0)) { 72 canvas->drawImage(bitmap.asImage(), 3, 3); 73 } 74 } 75