blob: aaa7349eed031a27927f7ed943a9c197909eca71 [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
Hal Canarybd865e22019-07-18 11:51:19 -040021namespace {
scroggo@google.com8dc8bc52013-08-07 19:16:05 +000022/**
23 * Test copying an image from 8888 to 4444.
24 */
Hal Canarybd865e22019-07-18 11:51:19 -040025class CopyTo4444GM : public skiagm::GM {
26 SkString onShortName() override { return SkString("copyTo4444"); }
scroggo@google.com8dc8bc52013-08-07 19:16:05 +000027
Hal Canarybd865e22019-07-18 11:51:19 -040028 SkISize onISize() override { return {360, 180}; }
scroggo@google.com8dc8bc52013-08-07 19:16:05 +000029
Hal Canarybd865e22019-07-18 11:51:19 -040030 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
scroggo@google.com8dc8bc52013-08-07 19:16:05 +000031 SkBitmap bm, bm4444;
Hal Canaryc465d132017-12-08 10:21:31 -050032 if (!GetResourceAsBitmap("images/dog.jpg", &bm)) {
Chris Dalton50e24d72019-02-07 16:20:09 -070033 *errorMsg = "Could not decode the file. Did you forget to set the resourcePath?";
34 return DrawResult::kFail;
scroggo@google.com8dc8bc52013-08-07 19:16:05 +000035 }
Mike Reedfa582c82021-01-23 22:07:05 -050036 canvas->drawImage(bm.asImage(), 0, 0);
Matt Sarettd16084f2017-05-26 11:40:25 -040037
38 // This should dither or we will see artifacts in the background of the image.
Mike Kleinea3f0142019-03-20 11:12:10 -050039 SkAssertResult(ToolUtils::copy_to(&bm4444, kARGB_4444_SkColorType, bm));
Mike Reedfa582c82021-01-23 22:07:05 -050040 canvas->drawImage(bm4444.asImage(), SkIntToScalar(bm.width()), 0);
Chris Dalton50e24d72019-02-07 16:20:09 -070041 return DrawResult::kOk;
scroggo@google.com8dc8bc52013-08-07 19:16:05 +000042 }
scroggo@google.com8dc8bc52013-08-07 19:16:05 +000043};
Hal Canarybd865e22019-07-18 11:51:19 -040044} // namespace
scroggo@google.com8dc8bc52013-08-07 19:16:05 +000045
Hal Canarye964c182019-01-23 10:22:01 -050046DEF_GM( return new CopyTo4444GM; )
scroggo@google.com8dc8bc52013-08-07 19:16:05 +000047
Hal Canarybd865e22019-07-18 11:51:19 -040048//////////////////////////////////////////////////////////////////////////////
Cary Clarkbd8b25a2018-02-21 08:14:26 -050049
50DEF_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 Reed607a3822021-01-24 19:49:21 -050057 canvas->drawImage(bitmap.asImage(), 0, 0);
Cary Clarkbd8b25a2018-02-21 08:14:26 -050058 offscreen.clear(SK_ColorBLUE);
Mike Reed607a3822021-01-24 19:49:21 -050059 canvas->drawImage(bitmap.asImage(), 1, 1);
Cary Clarkbd8b25a2018-02-21 08:14:26 -050060 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 Reed607a3822021-01-24 19:49:21 -050067 canvas->drawImage(bitmap.asImage(), 2, 2);
Cary Clarkbd8b25a2018-02-21 08:14:26 -050068 }
69 SkPixmap bluePixmap(imageInfo, &blue4444, 2);
70 if (bitmap.writePixels(bluePixmap, 0, 0)) {
Mike Reed607a3822021-01-24 19:49:21 -050071 canvas->drawImage(bitmap.asImage(), 3, 3);
Cary Clarkbd8b25a2018-02-21 08:14:26 -050072 }
73}