1 /*
2 * Copyright 2011 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 "include/core/SkAlphaType.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkColorPriv.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkColorType.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkPoint.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkSize.h"
19 #include "include/core/SkTypes.h"
20 #include "src/core/SkMemset.h"
21 #include "tests/Test.h"
22 #include "tools/ToolUtils.h"
23
24 #include <array>
25 #include <cstddef>
26
init_src(const SkBitmap & bitmap)27 static void init_src(const SkBitmap& bitmap) {
28 if (bitmap.getPixels()) {
29 bitmap.eraseColor(SK_ColorWHITE);
30 }
31 }
32
33 struct Pair {
34 SkColorType fColorType;
35 const char* fValid;
36 };
37
38 // Utility functions for copyPixelsTo()/copyPixelsFrom() tests.
39 // getPixel()
40 // setPixel()
41 // getSkConfigName()
42 // struct Coordinates
43 // reportCopyVerification()
44 // writeCoordPixels()
45
46 // Helper struct to contain pixel locations, while avoiding need for STL.
47 struct Coordinates {
48
49 const int length;
50 SkIPoint* const data;
51
CoordinatesCoordinates52 explicit Coordinates(int _length): length(_length)
53 , data(new SkIPoint[length]) { }
54
~CoordinatesCoordinates55 ~Coordinates(){
56 delete [] data;
57 }
58
operator []Coordinates59 SkIPoint* operator[](int i) const {
60 // Use with care, no bounds checking.
61 return data + i;
62 }
63 };
64
65 static const Pair gPairs[] = {
66 { kUnknown_SkColorType, "0000000" },
67 { kAlpha_8_SkColorType, "0100000" },
68 { kRGB_565_SkColorType, "0101011" },
69 { kARGB_4444_SkColorType, "0101111" },
70 { kN32_SkColorType, "0101111" },
71 { kRGBA_F16_SkColorType, "0101011" },
72 };
73
setup_src_bitmaps(SkBitmap * srcOpaque,SkBitmap * srcPremul,SkColorType ct)74 static void setup_src_bitmaps(SkBitmap* srcOpaque, SkBitmap* srcPremul,
75 SkColorType ct) {
76 const int W = 20;
77 const int H = 33;
78 sk_sp<SkColorSpace> colorSpace = nullptr;
79 if (kRGBA_F16_SkColorType == ct) {
80 colorSpace = SkColorSpace::MakeSRGB();
81 }
82
83 srcOpaque->allocPixels(SkImageInfo::Make(W, H, ct, kOpaque_SkAlphaType, colorSpace));
84 srcPremul->allocPixels(SkImageInfo::Make(W, H, ct, kPremul_SkAlphaType, colorSpace));
85 init_src(*srcOpaque);
86 init_src(*srcPremul);
87 }
88
DEF_TEST(BitmapCopy_extractSubset,reporter)89 DEF_TEST(BitmapCopy_extractSubset, reporter) {
90 const int W = 20;
91 for (size_t i = 0; i < std::size(gPairs); i++) {
92 SkBitmap srcOpaque, srcPremul;
93 setup_src_bitmaps(&srcOpaque, &srcPremul, gPairs[i].fColorType);
94
95 SkBitmap bitmap(srcOpaque);
96 SkBitmap subset;
97 SkIRect r;
98 // Extract a subset which has the same width as the original. This
99 // catches a bug where we cloned the genID incorrectly.
100 r.setLTRB(0, 1, W, 3);
101 // Relies on old behavior of extractSubset failing if colortype is unknown
102 if (kUnknown_SkColorType != bitmap.colorType() && bitmap.extractSubset(&subset, r)) {
103 REPORTER_ASSERT(reporter, subset.width() == W);
104 REPORTER_ASSERT(reporter, subset.height() == 2);
105 REPORTER_ASSERT(reporter, subset.alphaType() == bitmap.alphaType());
106
107 // Test copying an extracted subset.
108 for (size_t j = 0; j < std::size(gPairs); j++) {
109 SkBitmap copy;
110 bool success = ToolUtils::copy_to(©, gPairs[j].fColorType, subset);
111 if (!success) {
112 // Skip checking that success matches fValid, which is redundant
113 // with the code below.
114 REPORTER_ASSERT(reporter, gPairs[i].fColorType != gPairs[j].fColorType);
115 continue;
116 }
117
118 // When performing a copy of an extracted subset, the gen id should
119 // change.
120 REPORTER_ASSERT(reporter, copy.getGenerationID() != subset.getGenerationID());
121
122 REPORTER_ASSERT(reporter, copy.width() == W);
123 REPORTER_ASSERT(reporter, copy.height() == 2);
124 }
125 }
126
127 bitmap = srcPremul;
128 if (bitmap.extractSubset(&subset, r)) {
129 REPORTER_ASSERT(reporter, subset.alphaType() == bitmap.alphaType());
130 }
131 }
132 }
133
134 /**
135 * Construct 4x4 pixels where we can look at a color and determine where it should be in the grid.
136 * alpha = 0xFF, blue = 0x80, red = x, green = y
137 */
fill_4x4_pixels(SkPMColor colors[16])138 static void fill_4x4_pixels(SkPMColor colors[16]) {
139 for (int y = 0; y < 4; ++y) {
140 for (int x = 0; x < 4; ++x) {
141 colors[y*4+x] = SkPackARGB32(0xFF, x, y, 0x80);
142 }
143 }
144 }
145
check_4x4_pixel(SkPMColor color,unsigned x,unsigned y)146 static bool check_4x4_pixel(SkPMColor color, unsigned x, unsigned y) {
147 SkASSERT(x < 4 && y < 4);
148 return 0xFF == SkGetPackedA32(color) &&
149 x == SkGetPackedR32(color) &&
150 y == SkGetPackedG32(color) &&
151 0x80 == SkGetPackedB32(color);
152 }
153
154 /**
155 * Fill with all zeros, which will never match any value from fill_4x4_pixels
156 */
clear_4x4_pixels(SkPMColor colors[16])157 static void clear_4x4_pixels(SkPMColor colors[16]) {
158 SkOpts::memset32(colors, 0, 16);
159 }
160
161 // Much of readPixels is exercised by copyTo testing, since readPixels is the backend for that
162 // method. Here we explicitly test subset copies.
163 //
DEF_TEST(BitmapReadPixels,reporter)164 DEF_TEST(BitmapReadPixels, reporter) {
165 const int W = 4;
166 const int H = 4;
167 const size_t rowBytes = W * sizeof(SkPMColor);
168 const SkImageInfo srcInfo = SkImageInfo::MakeN32Premul(W, H);
169 SkPMColor srcPixels[16];
170 fill_4x4_pixels(srcPixels);
171 SkBitmap srcBM;
172 srcBM.installPixels(srcInfo, srcPixels, rowBytes);
173
174 SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(W, H);
175 SkPMColor dstPixels[16];
176
177 const struct {
178 bool fExpectedSuccess;
179 SkIPoint fRequestedSrcLoc;
180 SkISize fRequestedDstSize;
181 // If fExpectedSuccess, check these, otherwise ignore
182 SkIPoint fExpectedDstLoc;
183 SkIRect fExpectedSrcR;
184 } gRec[] = {
185 { true, { 0, 0 }, { 4, 4 }, { 0, 0 }, { 0, 0, 4, 4 } },
186 { true, { 1, 1 }, { 2, 2 }, { 0, 0 }, { 1, 1, 3, 3 } },
187 { true, { 2, 2 }, { 4, 4 }, { 0, 0 }, { 2, 2, 4, 4 } },
188 { true, {-1,-1 }, { 2, 2 }, { 1, 1 }, { 0, 0, 1, 1 } },
189 { false, {-1,-1 }, { 1, 1 }, { 0, 0 }, { 0, 0, 0, 0 } },
190 };
191
192 for (size_t i = 0; i < std::size(gRec); ++i) {
193 clear_4x4_pixels(dstPixels);
194
195 dstInfo = dstInfo.makeDimensions(gRec[i].fRequestedDstSize);
196 bool success = srcBM.readPixels(dstInfo, dstPixels, rowBytes,
197 gRec[i].fRequestedSrcLoc.x(), gRec[i].fRequestedSrcLoc.y());
198
199 REPORTER_ASSERT(reporter, gRec[i].fExpectedSuccess == success);
200 if (success) {
201 const SkIRect srcR = gRec[i].fExpectedSrcR;
202 const int dstX = gRec[i].fExpectedDstLoc.x();
203 const int dstY = gRec[i].fExpectedDstLoc.y();
204 // Walk the dst pixels, and check if we got what we expected
205 for (int y = 0; y < H; ++y) {
206 for (int x = 0; x < W; ++x) {
207 SkPMColor dstC = dstPixels[y*4+x];
208 // get into src coordinates
209 int sx = x - dstX + srcR.x();
210 int sy = y - dstY + srcR.y();
211 if (srcR.contains(sx, sy)) {
212 REPORTER_ASSERT(reporter, check_4x4_pixel(dstC, sx, sy));
213 } else {
214 REPORTER_ASSERT(reporter, 0 == dstC);
215 }
216 }
217 }
218 }
219 }
220 }
221