blob: e7dcc8998771a238ab71e0b2b6f765628b00d74b [file] [log] [blame]
scroggo@google.com8dc8bc52013-08-07 19:16:05 +00001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#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 Kleinc0bd9f92019-04-23 12:05:21 -050018#include "tools/Resources.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040019#include "tools/ToolUtils.h"
scroggo@google.com8dc8bc52013-08-07 19:16:05 +000020
21namespace skiagm {
22
23/**
24 * Test copying an image from 8888 to 4444.
25 */
26class CopyTo4444GM : public GM {
27public:
28 CopyTo4444GM() {}
29
30protected:
31 virtual SkString onShortName() {
32 return SkString("copyTo4444");
33 }
34
35 virtual SkISize onISize() {
Matt Sarettd16084f2017-05-26 11:40:25 -040036 return SkISize::Make(360, 180);
scroggo@google.com8dc8bc52013-08-07 19:16:05 +000037 }
38
Chris Dalton50e24d72019-02-07 16:20:09 -070039 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) {
scroggo@google.com8dc8bc52013-08-07 19:16:05 +000040 SkBitmap bm, bm4444;
Hal Canaryc465d132017-12-08 10:21:31 -050041 if (!GetResourceAsBitmap("images/dog.jpg", &bm)) {
Chris Dalton50e24d72019-02-07 16:20:09 -070042 *errorMsg = "Could not decode the file. Did you forget to set the resourcePath?";
43 return DrawResult::kFail;
scroggo@google.com8dc8bc52013-08-07 19:16:05 +000044 }
45 canvas->drawBitmap(bm, 0, 0);
Matt Sarettd16084f2017-05-26 11:40:25 -040046
47 // This should dither or we will see artifacts in the background of the image.
Mike Kleinea3f0142019-03-20 11:12:10 -050048 SkAssertResult(ToolUtils::copy_to(&bm4444, kARGB_4444_SkColorType, bm));
scroggo@google.com7bb36ab2013-08-07 19:39:56 +000049 canvas->drawBitmap(bm4444, SkIntToScalar(bm.width()), 0);
Chris Dalton50e24d72019-02-07 16:20:09 -070050 return DrawResult::kOk;
scroggo@google.com8dc8bc52013-08-07 19:16:05 +000051 }
52
53private:
54 typedef GM INHERITED;
55};
56
57//////////////////////////////////////////////////////////////////////////////
58
Hal Canarye964c182019-01-23 10:22:01 -050059DEF_GM( return new CopyTo4444GM; )
scroggo@google.com8dc8bc52013-08-07 19:16:05 +000060
61}
Cary Clarkbd8b25a2018-02-21 08:14:26 -050062
63DEF_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}