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