blob: ec9b72610d8976464a32983fc16bc4a0856916a7 [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
8#include "gm.h"
Matt Sarett68b8e3d2017-04-28 11:15:22 -04009#include "sk_tool_utils.h"
tfarinabcbc1782014-06-18 14:32:48 -070010
11#include "Resources.h"
scroggo@google.com8dc8bc52013-08-07 19:16:05 +000012#include "SkCanvas.h"
scroggo@google.com8dc8bc52013-08-07 19:16:05 +000013#include "SkOSFile.h"
14
15namespace skiagm {
16
17/**
18 * Test copying an image from 8888 to 4444.
19 */
20class CopyTo4444GM : public GM {
21public:
22 CopyTo4444GM() {}
23
24protected:
25 virtual SkString onShortName() {
26 return SkString("copyTo4444");
27 }
28
29 virtual SkISize onISize() {
Matt Sarettd16084f2017-05-26 11:40:25 -040030 return SkISize::Make(360, 180);
scroggo@google.com8dc8bc52013-08-07 19:16:05 +000031 }
32
33 virtual void onDraw(SkCanvas* canvas) {
34 SkBitmap bm, bm4444;
Hal Canaryc465d132017-12-08 10:21:31 -050035 if (!GetResourceAsBitmap("images/dog.jpg", &bm)) {
scroggo@google.com8dc8bc52013-08-07 19:16:05 +000036 SkDebugf("Could not decode the file. Did you forget to set the "
37 "resourcePath?\n");
38 return;
39 }
40 canvas->drawBitmap(bm, 0, 0);
Matt Sarettd16084f2017-05-26 11:40:25 -040041
42 // This should dither or we will see artifacts in the background of the image.
Matt Sarett68b8e3d2017-04-28 11:15:22 -040043 SkAssertResult(sk_tool_utils::copy_to(&bm4444, kARGB_4444_SkColorType, bm));
scroggo@google.com7bb36ab2013-08-07 19:39:56 +000044 canvas->drawBitmap(bm4444, SkIntToScalar(bm.width()), 0);
scroggo@google.com8dc8bc52013-08-07 19:16:05 +000045 }
46
47private:
48 typedef GM INHERITED;
49};
50
51//////////////////////////////////////////////////////////////////////////////
52
53static GM* MyFactory(void*) { return new CopyTo4444GM; }
54static GMRegistry reg(MyFactory);
55
56}
Cary Clarkbd8b25a2018-02-21 08:14:26 -050057
58DEF_SIMPLE_GM(format4444, canvas, 64, 64) {
59 canvas->scale(16, 16);
60 SkBitmap bitmap;
61 SkImageInfo imageInfo = SkImageInfo::Make(1, 1, kARGB_4444_SkColorType, kPremul_SkAlphaType);
62 bitmap.allocPixels(imageInfo);
63 SkCanvas offscreen(bitmap);
64 offscreen.clear(SK_ColorRED);
65 canvas->drawBitmap(bitmap, 0, 0);
66 offscreen.clear(SK_ColorBLUE);
67 canvas->drawBitmap(bitmap, 1, 1);
68 auto pack4444 = [](unsigned a, unsigned r, unsigned g, unsigned b) -> uint16_t {
69 return (a << 0) | (b << 4) | (g << 8) | (r << 12);
70 };
71 uint16_t red4444 = pack4444(0xF, 0xF, 0x0, 0x0);
72 uint16_t blue4444 = pack4444(0xF, 0x0, 0x0, 0x0F);
73 SkPixmap redPixmap(imageInfo, &red4444, 2);
74 if (bitmap.writePixels(redPixmap, 0, 0)) {
75 canvas->drawBitmap(bitmap, 2, 2);
76 }
77 SkPixmap bluePixmap(imageInfo, &blue4444, 2);
78 if (bitmap.writePixels(bluePixmap, 0, 0)) {
79 canvas->drawBitmap(bitmap, 3, 3);
80 }
81}