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 | |
Hal Canary | bd865e2 | 2019-07-18 11:51:19 -0400 | [diff] [blame] | 21 | namespace { |
scroggo@google.com | 8dc8bc5 | 2013-08-07 19:16:05 +0000 | [diff] [blame] | 22 | /** |
| 23 | * Test copying an image from 8888 to 4444. |
| 24 | */ |
Hal Canary | bd865e2 | 2019-07-18 11:51:19 -0400 | [diff] [blame] | 25 | class CopyTo4444GM : public skiagm::GM { |
| 26 | SkString onShortName() override { return SkString("copyTo4444"); } |
scroggo@google.com | 8dc8bc5 | 2013-08-07 19:16:05 +0000 | [diff] [blame] | 27 | |
Hal Canary | bd865e2 | 2019-07-18 11:51:19 -0400 | [diff] [blame] | 28 | SkISize onISize() override { return {360, 180}; } |
scroggo@google.com | 8dc8bc5 | 2013-08-07 19:16:05 +0000 | [diff] [blame] | 29 | |
Hal Canary | bd865e2 | 2019-07-18 11:51:19 -0400 | [diff] [blame] | 30 | DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { |
scroggo@google.com | 8dc8bc5 | 2013-08-07 19:16:05 +0000 | [diff] [blame] | 31 | SkBitmap bm, bm4444; |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 32 | if (!GetResourceAsBitmap("images/dog.jpg", &bm)) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 33 | *errorMsg = "Could not decode the file. Did you forget to set the resourcePath?"; |
| 34 | return DrawResult::kFail; |
scroggo@google.com | 8dc8bc5 | 2013-08-07 19:16:05 +0000 | [diff] [blame] | 35 | } |
Mike Reed | fa582c8 | 2021-01-23 22:07:05 -0500 | [diff] [blame] | 36 | canvas->drawImage(bm.asImage(), 0, 0); |
Matt Sarett | d16084f | 2017-05-26 11:40:25 -0400 | [diff] [blame] | 37 | |
| 38 | // 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] | 39 | SkAssertResult(ToolUtils::copy_to(&bm4444, kARGB_4444_SkColorType, bm)); |
Mike Reed | fa582c8 | 2021-01-23 22:07:05 -0500 | [diff] [blame] | 40 | canvas->drawImage(bm4444.asImage(), SkIntToScalar(bm.width()), 0); |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 41 | return DrawResult::kOk; |
scroggo@google.com | 8dc8bc5 | 2013-08-07 19:16:05 +0000 | [diff] [blame] | 42 | } |
scroggo@google.com | 8dc8bc5 | 2013-08-07 19:16:05 +0000 | [diff] [blame] | 43 | }; |
Hal Canary | bd865e2 | 2019-07-18 11:51:19 -0400 | [diff] [blame] | 44 | } // namespace |
scroggo@google.com | 8dc8bc5 | 2013-08-07 19:16:05 +0000 | [diff] [blame] | 45 | |
Hal Canary | e964c18 | 2019-01-23 10:22:01 -0500 | [diff] [blame] | 46 | DEF_GM( return new CopyTo4444GM; ) |
scroggo@google.com | 8dc8bc5 | 2013-08-07 19:16:05 +0000 | [diff] [blame] | 47 | |
Hal Canary | bd865e2 | 2019-07-18 11:51:19 -0400 | [diff] [blame] | 48 | ////////////////////////////////////////////////////////////////////////////// |
Cary Clark | bd8b25a | 2018-02-21 08:14:26 -0500 | [diff] [blame] | 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); |
Mike Reed | 607a382 | 2021-01-24 19:49:21 -0500 | [diff] [blame^] | 57 | canvas->drawImage(bitmap.asImage(), 0, 0); |
Cary Clark | bd8b25a | 2018-02-21 08:14:26 -0500 | [diff] [blame] | 58 | offscreen.clear(SK_ColorBLUE); |
Mike Reed | 607a382 | 2021-01-24 19:49:21 -0500 | [diff] [blame^] | 59 | canvas->drawImage(bitmap.asImage(), 1, 1); |
Cary Clark | bd8b25a | 2018-02-21 08:14:26 -0500 | [diff] [blame] | 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)) { |
Mike Reed | 607a382 | 2021-01-24 19:49:21 -0500 | [diff] [blame^] | 67 | canvas->drawImage(bitmap.asImage(), 2, 2); |
Cary Clark | bd8b25a | 2018-02-21 08:14:26 -0500 | [diff] [blame] | 68 | } |
| 69 | SkPixmap bluePixmap(imageInfo, &blue4444, 2); |
| 70 | if (bitmap.writePixels(bluePixmap, 0, 0)) { |
Mike Reed | 607a382 | 2021-01-24 19:49:21 -0500 | [diff] [blame^] | 71 | canvas->drawImage(bitmap.asImage(), 3, 3); |
Cary Clark | bd8b25a | 2018-02-21 08:14:26 -0500 | [diff] [blame] | 72 | } |
| 73 | } |