scroggo@google.com | 8dc8bc5 | 2013-08-07 19:16:05 +0000 | [diff] [blame] | 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.h" |
Matt Sarett | 68b8e3d | 2017-04-28 11:15:22 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 10 | |
| 11 | #include "Resources.h" |
scroggo@google.com | 8dc8bc5 | 2013-08-07 19:16:05 +0000 | [diff] [blame] | 12 | #include "SkCanvas.h" |
scroggo@google.com | 8dc8bc5 | 2013-08-07 19:16:05 +0000 | [diff] [blame] | 13 | #include "SkOSFile.h" |
| 14 | |
| 15 | namespace skiagm { |
| 16 | |
| 17 | /** |
| 18 | * Test copying an image from 8888 to 4444. |
| 19 | */ |
| 20 | class CopyTo4444GM : public GM { |
| 21 | public: |
| 22 | CopyTo4444GM() {} |
| 23 | |
| 24 | protected: |
| 25 | virtual SkString onShortName() { |
| 26 | return SkString("copyTo4444"); |
| 27 | } |
| 28 | |
| 29 | virtual SkISize onISize() { |
Matt Sarett | d16084f | 2017-05-26 11:40:25 -0400 | [diff] [blame] | 30 | return SkISize::Make(360, 180); |
scroggo@google.com | 8dc8bc5 | 2013-08-07 19:16:05 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame^] | 33 | DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) { |
scroggo@google.com | 8dc8bc5 | 2013-08-07 19:16:05 +0000 | [diff] [blame] | 34 | SkBitmap bm, bm4444; |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 35 | if (!GetResourceAsBitmap("images/dog.jpg", &bm)) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame^] | 36 | *errorMsg = "Could not decode the file. Did you forget to set the resourcePath?"; |
| 37 | return DrawResult::kFail; |
scroggo@google.com | 8dc8bc5 | 2013-08-07 19:16:05 +0000 | [diff] [blame] | 38 | } |
| 39 | canvas->drawBitmap(bm, 0, 0); |
Matt Sarett | d16084f | 2017-05-26 11:40:25 -0400 | [diff] [blame] | 40 | |
| 41 | // This should dither or we will see artifacts in the background of the image. |
Matt Sarett | 68b8e3d | 2017-04-28 11:15:22 -0400 | [diff] [blame] | 42 | SkAssertResult(sk_tool_utils::copy_to(&bm4444, kARGB_4444_SkColorType, bm)); |
scroggo@google.com | 7bb36ab | 2013-08-07 19:39:56 +0000 | [diff] [blame] | 43 | canvas->drawBitmap(bm4444, SkIntToScalar(bm.width()), 0); |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame^] | 44 | return DrawResult::kOk; |
scroggo@google.com | 8dc8bc5 | 2013-08-07 19:16:05 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | private: |
| 48 | typedef GM INHERITED; |
| 49 | }; |
| 50 | |
| 51 | ////////////////////////////////////////////////////////////////////////////// |
| 52 | |
Hal Canary | e964c18 | 2019-01-23 10:22:01 -0500 | [diff] [blame] | 53 | DEF_GM( return new CopyTo4444GM; ) |
scroggo@google.com | 8dc8bc5 | 2013-08-07 19:16:05 +0000 | [diff] [blame] | 54 | |
| 55 | } |
Cary Clark | bd8b25a | 2018-02-21 08:14:26 -0500 | [diff] [blame] | 56 | |
| 57 | DEF_SIMPLE_GM(format4444, canvas, 64, 64) { |
| 58 | canvas->scale(16, 16); |
| 59 | SkBitmap bitmap; |
| 60 | SkImageInfo imageInfo = SkImageInfo::Make(1, 1, kARGB_4444_SkColorType, kPremul_SkAlphaType); |
| 61 | bitmap.allocPixels(imageInfo); |
| 62 | SkCanvas offscreen(bitmap); |
| 63 | offscreen.clear(SK_ColorRED); |
| 64 | canvas->drawBitmap(bitmap, 0, 0); |
| 65 | offscreen.clear(SK_ColorBLUE); |
| 66 | canvas->drawBitmap(bitmap, 1, 1); |
| 67 | auto pack4444 = [](unsigned a, unsigned r, unsigned g, unsigned b) -> uint16_t { |
| 68 | return (a << 0) | (b << 4) | (g << 8) | (r << 12); |
| 69 | }; |
| 70 | uint16_t red4444 = pack4444(0xF, 0xF, 0x0, 0x0); |
| 71 | uint16_t blue4444 = pack4444(0xF, 0x0, 0x0, 0x0F); |
| 72 | SkPixmap redPixmap(imageInfo, &red4444, 2); |
| 73 | if (bitmap.writePixels(redPixmap, 0, 0)) { |
| 74 | canvas->drawBitmap(bitmap, 2, 2); |
| 75 | } |
| 76 | SkPixmap bluePixmap(imageInfo, &blue4444, 2); |
| 77 | if (bitmap.writePixels(bluePixmap, 0, 0)) { |
| 78 | canvas->drawBitmap(bitmap, 3, 3); |
| 79 | } |
| 80 | } |