• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkColorPriv.h"
14 #include "include/core/SkImage.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkScalar.h"
18 #include "include/core/SkSize.h"
19 #include "include/core/SkString.h"
20 #include "include/core/SkTypes.h"
21 
22 /**
23  * This GM checks that bitmap pixels are unpremultiplied before being exported
24  * to other formats. If unpremultiplication is implemented properly, this
25  * GM should come out completely white. If not, this GM looks like a row of two
26  * greyscale gradients above a row of grey lines.
27  * This tests both the ARGB4444 and ARGB8888 bitmap configurations.
28  */
29 
30 constexpr int SLIDE_SIZE = 256;
31 
init_bitmap(SkColorType ct,SkBitmap * bitmap)32 static void init_bitmap(SkColorType ct, SkBitmap* bitmap) {
33     bitmap->allocPixels(SkImageInfo::Make(SLIDE_SIZE, SLIDE_SIZE, ct,
34                                           kPremul_SkAlphaType));
35     bitmap->eraseColor(SK_ColorWHITE);
36 }
37 
make_argb8888_gradient()38 static sk_sp<SkImage> make_argb8888_gradient() {
39     SkBitmap bitmap;
40     init_bitmap(kN32_SkColorType, &bitmap);
41     for (int y = 0; y < SLIDE_SIZE; y++) {
42         uint32_t* dst = bitmap.getAddr32(0, y);
43         for (int x = 0; x < SLIDE_SIZE; x++) {
44             dst[x] = SkPackARGB32(y, y, y, y);
45         }
46     }
47     return bitmap.asImage();
48 }
49 
make_argb4444_gradient()50 static sk_sp<SkImage> make_argb4444_gradient() {
51     SkBitmap bitmap;
52     init_bitmap(kARGB_4444_SkColorType, &bitmap);
53     // Using draw rather than readPixels to suppress dither
54     SkPaint paint;
55     paint.setBlendMode(SkBlendMode::kSrc);
56     SkCanvas{ bitmap }.drawImage(make_argb8888_gradient(), 0, 0, SkSamplingOptions(), &paint);
57     return bitmap.asImage();
58 }
59 
make_argb8888_stripes()60 static sk_sp<SkImage> make_argb8888_stripes() {
61     SkBitmap bitmap;
62     init_bitmap(kN32_SkColorType, &bitmap);
63     uint8_t rowColor = 0;
64     for (int y = 0; y < SLIDE_SIZE; y++) {
65         uint32_t* dst = bitmap.getAddr32(0, y);
66         for (int x = 0; x < SLIDE_SIZE; x++) {
67             dst[x] = SkPackARGB32(rowColor, rowColor,
68                                   rowColor, rowColor);
69         }
70         if (rowColor == 0) {
71             rowColor = 255;
72         } else {
73             rowColor = 0;
74         }
75     }
76     return bitmap.asImage();
77 }
78 
make_argb4444_stripes()79 static sk_sp<SkImage> make_argb4444_stripes() {
80     SkBitmap bitmap;
81     init_bitmap(kARGB_4444_SkColorType, &bitmap);
82     // Using draw rather than readPixels to suppress dither
83     SkPaint paint;
84     paint.setBlendMode(SkBlendMode::kSrc);
85     SkCanvas{ bitmap }.drawImage(make_argb8888_stripes(), 0, 0, SkSamplingOptions(), &paint);
86     return bitmap.asImage();
87 }
88 
89 namespace skiagm {
90 
91 class BitmapPremulGM : public GM {
92 public:
BitmapPremulGM()93     BitmapPremulGM() {
94         this->setBGColor(SK_ColorWHITE);
95     }
96 
97 protected:
onShortName()98     SkString onShortName() override {
99         return SkString("bitmap_premul");
100     }
101 
onISize()102     SkISize onISize() override {
103         return SkISize::Make(SLIDE_SIZE * 2, SLIDE_SIZE * 2);
104     }
105 
onDraw(SkCanvas * canvas)106     void onDraw(SkCanvas* canvas) override {
107         SkScalar slideSize = SkIntToScalar(SLIDE_SIZE);
108         canvas->drawImage(make_argb8888_gradient(), 0, 0);
109         canvas->drawImage(make_argb4444_gradient(), slideSize, 0);
110         canvas->drawImage(make_argb8888_stripes(), 0, slideSize);
111         canvas->drawImage(make_argb4444_stripes(), slideSize, slideSize);
112     }
113 
114 private:
115     using INHERITED = GM;
116 };
117 
118 DEF_GM( return new BitmapPremulGM; )
119 }  // namespace skiagm
120