Flutter Engine
The Flutter Engine
copy_to_4444.cpp
Go to the documentation of this file.
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
8#include "gm/gm.h"
15#include "include/core/SkSize.h"
18#include "tools/DecodeUtils.h"
19#include "tools/Resources.h"
20#include "tools/ToolUtils.h"
21
22namespace {
23/**
24 * Test copying an image from 8888 to 4444.
25 */
26class CopyTo4444GM : public skiagm::GM {
27 SkString getName() const override { return SkString("copyTo4444"); }
28
29 SkISize getISize() override { return {360, 180}; }
30
31 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
32 SkBitmap bm, bm4444;
33 if (!ToolUtils::GetResourceAsBitmap("images/dog.jpg", &bm)) {
34 *errorMsg = "Could not decode the file. Did you forget to set the resourcePath?";
35 return DrawResult::kFail;
36 }
37 canvas->drawImage(bm.asImage(), 0, 0);
38
39 // This should dither or we will see artifacts in the background of the image.
41 canvas->drawImage(bm4444.asImage(), SkIntToScalar(bm.width()), 0);
42 return DrawResult::kOk;
43 }
44};
45} // namespace
46
47DEF_GM( return new CopyTo4444GM; )
48
49//////////////////////////////////////////////////////////////////////////////
50
51DEF_SIMPLE_GM(format4444, canvas, 64, 64) {
52 canvas->scale(16, 16);
55 bitmap.allocPixels(imageInfo);
56 SkCanvas offscreen(bitmap);
57 offscreen.clear(SK_ColorRED);
58 canvas->drawImage(bitmap.asImage(), 0, 0);
59 offscreen.clear(SK_ColorBLUE);
60 canvas->drawImage(bitmap.asImage(), 1, 1);
61 auto pack4444 = [](unsigned a, unsigned r, unsigned g, unsigned b) -> uint16_t {
62 return (a << 0) | (b << 4) | (g << 8) | (r << 12);
63 };
64 uint16_t red4444 = pack4444(0xF, 0xF, 0x0, 0x0);
65 uint16_t blue4444 = pack4444(0xF, 0x0, 0x0, 0x0F);
66 SkPixmap redPixmap(imageInfo, &red4444, 2);
67 if (bitmap.writePixels(redPixmap, 0, 0)) {
68 canvas->drawImage(bitmap.asImage(), 2, 2);
69 }
70 SkPixmap bluePixmap(imageInfo, &blue4444, 2);
71 if (bitmap.writePixels(bluePixmap, 0, 0)) {
72 canvas->drawImage(bitmap.asImage(), 3, 3);
73 }
74}
SkAssertResult(font.textToGlyphs("Hello", 5, SkTextEncoding::kUTF8, glyphs, std::size(glyphs))==count)
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
@ kARGB_4444_SkColorType
pixel with 4 bits for alpha, red, green, blue; in 16-bit word
Definition: SkColorType.h:23
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
#define SkIntToScalar(x)
Definition: SkScalar.h:57
sk_sp< SkImage > asImage() const
Definition: SkBitmap.cpp:645
int width() const
Definition: SkBitmap.h:149
void clear(SkColor color)
Definition: SkCanvas.h:1199
void scale(SkScalar sx, SkScalar sy)
Definition: SkCanvas.cpp:1289
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
Definition: gm.h:110
virtual SkISize getISize()=0
virtual SkString getName() const =0
virtual DrawResult onDraw(SkCanvas *, SkString *errorMsg)
Definition: gm.cpp:139
DEF_SIMPLE_GM(format4444, canvas, 64, 64)
static bool b
struct MyStruct a[10]
#define DEF_GM(CODE)
Definition: gm.h:40
bool copy_to(SkBitmap *dst, SkColorType dstColorType, const SkBitmap &src)
Definition: ToolUtils.cpp:394
bool GetResourceAsBitmap(const char *resource, SkBitmap *dst)
Definition: DecodeUtils.h:21
Definition: bitmap.py:1
DrawResult
Definition: gm.h:104
Definition: SkSize.h:16
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)