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