OLD | NEW |
---|---|
1 | |
2 /* | 1 /* |
3 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
4 * | 3 * |
5 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 5 * found in the LICENSE file. |
7 */ | 6 */ |
8 #include "Test.h" | 7 #include "Test.h" |
9 #include "TestClassDef.h" | 8 #include "TestClassDef.h" |
10 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
11 #include "SkRect.h" | 10 #include "SkRect.h" |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
198 reporter->reportFailed(str); | 197 reporter->reportFailed(str); |
199 } | 198 } |
200 } | 199 } |
201 | 200 |
202 // Writes unique pixel values at locations specified by coords. | 201 // Writes unique pixel values at locations specified by coords. |
203 static void writeCoordPixels(SkBitmap& bm, const Coordinates& coords) { | 202 static void writeCoordPixels(SkBitmap& bm, const Coordinates& coords) { |
204 for (int i = 0; i < coords.length; ++i) | 203 for (int i = 0; i < coords.length; ++i) |
205 setPixel(coords[i]->fX, coords[i]->fY, i, bm); | 204 setPixel(coords[i]->fX, coords[i]->fY, i, bm); |
206 } | 205 } |
207 | 206 |
207 static const Pair gPairs[] = { | |
208 { SkBitmap::kNo_Config, "0000000" }, | |
209 { SkBitmap::kA8_Config, "0101010" }, | |
210 { SkBitmap::kIndex8_Config, "0111010" }, | |
211 { SkBitmap::kRGB_565_Config, "0101010" }, | |
212 { SkBitmap::kARGB_4444_Config, "0101110" }, | |
213 { SkBitmap::kARGB_8888_Config, "0101110" }, | |
214 }; | |
215 | |
216 static const int W = 20; | |
217 static const int H = 33; | |
218 | |
219 static void setup_src_bitmaps(SkBitmap* srcOpaque, SkBitmap* srcPremul, | |
220 SkBitmap::Config config) { | |
221 { | |
mtklein
2014/01/09 22:05:00
These braces probably aren't useful any more. :)
scroggo
2014/01/10 14:45:58
Removed.
| |
222 SkColorTable* ctOpaque = NULL; | |
223 SkColorTable* ctPremul = NULL; | |
224 | |
225 srcOpaque->setConfig(config, W, H, 0, kOpaque_SkAlphaType); | |
226 srcPremul->setConfig(config, W, H, 0, kPremul_SkAlphaType); | |
227 if (SkBitmap::kIndex8_Config == config) { | |
228 ctOpaque = init_ctable(kOpaque_SkAlphaType); | |
229 ctPremul = init_ctable(kPremul_SkAlphaType); | |
230 } | |
231 srcOpaque->allocPixels(ctOpaque); | |
232 srcPremul->allocPixels(ctPremul); | |
233 SkSafeUnref(ctOpaque); | |
234 SkSafeUnref(ctPremul); | |
235 } | |
236 init_src(*srcOpaque); | |
237 init_src(*srcPremul); | |
238 } | |
239 | |
240 DEF_TEST(BitmapCopy_extractSubset, reporter) { | |
241 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) { | |
242 SkBitmap srcOpaque, srcPremul; | |
243 setup_src_bitmaps(&srcOpaque, &srcPremul, gPairs[i].fConfig); | |
244 | |
245 SkBitmap bitmap(srcOpaque); | |
246 SkBitmap subset; | |
247 SkIRect r; | |
248 // Extract a subset which has the same width as the original. This | |
249 // catches a bug where we cloned the genID incorrectly. | |
250 r.set(0, 1, W, 3); | |
251 bitmap.setIsVolatile(true); | |
252 if (bitmap.extractSubset(&subset, r)) { | |
253 REPORTER_ASSERT(reporter, subset.width() == W); | |
254 REPORTER_ASSERT(reporter, subset.height() == 2); | |
255 REPORTER_ASSERT(reporter, subset.alphaType() == bitmap.alphaType()); | |
256 REPORTER_ASSERT(reporter, subset.isVolatile() == true); | |
257 | |
258 // Test copying an extracted subset. | |
259 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) { | |
260 SkBitmap copy; | |
261 bool success = subset.copyTo(©, gPairs[j].fConfig); | |
262 if (!success) { | |
263 // Skip checking that success matches fValid, which is redun dant | |
264 // with the code below. | |
265 REPORTER_ASSERT(reporter, gPairs[i].fConfig != gPairs[j].fCo nfig); | |
266 continue; | |
267 } | |
268 | |
269 // When performing a copy of an extracted subset, the gen id sho uld | |
270 // change. | |
271 REPORTER_ASSERT(reporter, copy.getGenerationID() != subset.getGe nerationID()); | |
272 | |
273 REPORTER_ASSERT(reporter, copy.width() == W); | |
274 REPORTER_ASSERT(reporter, copy.height() == 2); | |
275 | |
276 if (gPairs[i].fConfig == gPairs[j].fConfig) { | |
277 SkAutoLockPixels alp0(subset); | |
278 SkAutoLockPixels alp1(copy); | |
279 // they should both have, or both not-have, a colortable | |
280 bool hasCT = subset.getColorTable() != NULL; | |
281 REPORTER_ASSERT(reporter, (copy.getColorTable() != NULL) == hasCT); | |
282 } | |
283 } | |
284 } | |
285 | |
286 bitmap = srcPremul; | |
287 bitmap.setIsVolatile(false); | |
288 if (bitmap.extractSubset(&subset, r)) { | |
289 REPORTER_ASSERT(reporter, subset.alphaType() == bitmap.alphaType()); | |
290 REPORTER_ASSERT(reporter, subset.isVolatile() == false); | |
291 } | |
292 } | |
293 } | |
294 | |
208 DEF_TEST(BitmapCopy, reporter) { | 295 DEF_TEST(BitmapCopy, reporter) { |
209 static const Pair gPairs[] = { | |
210 { SkBitmap::kNo_Config, "0000000" }, | |
211 { SkBitmap::kA8_Config, "0101010" }, | |
212 { SkBitmap::kIndex8_Config, "0111010" }, | |
213 { SkBitmap::kRGB_565_Config, "0101010" }, | |
214 { SkBitmap::kARGB_4444_Config, "0101110" }, | |
215 { SkBitmap::kARGB_8888_Config, "0101110" }, | |
216 }; | |
217 | |
218 static const bool isExtracted[] = { | 296 static const bool isExtracted[] = { |
219 false, true | 297 false, true |
220 }; | 298 }; |
221 | 299 |
222 const int W = 20; | 300 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) { |
223 const int H = 33; | 301 SkBitmap srcOpaque, srcPremul; |
302 setup_src_bitmaps(&srcOpaque, &srcPremul, gPairs[i].fConfig); | |
224 | 303 |
225 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) { | |
226 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) { | 304 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) { |
227 SkBitmap srcOpaque, srcPremul, dst; | 305 SkBitmap dst; |
228 | |
229 { | |
230 SkColorTable* ctOpaque = NULL; | |
231 SkColorTable* ctPremul = NULL; | |
232 | |
233 srcOpaque.setConfig(gPairs[i].fConfig, W, H, 0, kOpaque_SkAlphaT ype); | |
234 srcPremul.setConfig(gPairs[i].fConfig, W, H, 0, kPremul_SkAlphaT ype); | |
235 if (SkBitmap::kIndex8_Config == gPairs[i].fConfig) { | |
236 ctOpaque = init_ctable(kOpaque_SkAlphaType); | |
237 ctPremul = init_ctable(kPremul_SkAlphaType); | |
238 } | |
239 srcOpaque.allocPixels(ctOpaque); | |
240 srcPremul.allocPixels(ctPremul); | |
241 SkSafeUnref(ctOpaque); | |
242 SkSafeUnref(ctPremul); | |
243 } | |
244 init_src(srcOpaque); | |
245 init_src(srcPremul); | |
246 | 306 |
247 bool success = srcPremul.copyTo(&dst, gPairs[j].fConfig); | 307 bool success = srcPremul.copyTo(&dst, gPairs[j].fConfig); |
248 bool expected = gPairs[i].fValid[j] != '0'; | 308 bool expected = gPairs[i].fValid[j] != '0'; |
249 if (success != expected) { | 309 if (success != expected) { |
250 SkString str; | 310 SkString str; |
251 str.printf("SkBitmap::copyTo from %s to %s. expected %s returned %s", | 311 str.printf("SkBitmap::copyTo from %s to %s. expected %s returned %s", |
252 gConfigName[i], gConfigName[j], boolStr(expected), | 312 gConfigName[i], gConfigName[j], boolStr(expected), |
253 boolStr(success)); | 313 boolStr(success)); |
254 reporter->reportFailed(str); | 314 reporter->reportFailed(str); |
255 } | 315 } |
(...skipping 19 matching lines...) Expand all Loading... | |
275 REPORTER_ASSERT(reporter, dst.readyToDraw()); | 335 REPORTER_ASSERT(reporter, dst.readyToDraw()); |
276 const char* srcP = (const char*)srcPremul.getAddr(0, 0); | 336 const char* srcP = (const char*)srcPremul.getAddr(0, 0); |
277 const char* dstP = (const char*)dst.getAddr(0, 0); | 337 const char* dstP = (const char*)dst.getAddr(0, 0); |
278 REPORTER_ASSERT(reporter, srcP != dstP); | 338 REPORTER_ASSERT(reporter, srcP != dstP); |
279 REPORTER_ASSERT(reporter, !memcmp(srcP, dstP, | 339 REPORTER_ASSERT(reporter, !memcmp(srcP, dstP, |
280 srcPremul.getSize())); | 340 srcPremul.getSize())); |
281 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() == dst .getGenerationID()); | 341 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() == dst .getGenerationID()); |
282 } else { | 342 } else { |
283 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() != dst .getGenerationID()); | 343 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() != dst .getGenerationID()); |
284 } | 344 } |
285 // test extractSubset | |
286 { | |
287 SkBitmap bitmap(srcOpaque); | |
288 SkBitmap subset; | |
289 SkIRect r; | |
290 r.set(1, 1, 2, 2); | |
291 bitmap.setIsVolatile(true); | |
292 if (bitmap.extractSubset(&subset, r)) { | |
293 REPORTER_ASSERT(reporter, subset.width() == 1); | |
294 REPORTER_ASSERT(reporter, subset.height() == 1); | |
295 REPORTER_ASSERT(reporter, | |
296 subset.alphaType() == bitmap.alphaType() ); | |
297 REPORTER_ASSERT(reporter, | |
298 subset.isVolatile() == true); | |
299 | |
300 SkBitmap copy; | |
301 REPORTER_ASSERT(reporter, | |
302 subset.copyTo(©, subset.config())); | |
303 REPORTER_ASSERT(reporter, copy.width() == 1); | |
304 REPORTER_ASSERT(reporter, copy.height() == 1); | |
305 REPORTER_ASSERT(reporter, copy.rowBytes() <= 4); | |
306 | |
307 SkAutoLockPixels alp0(subset); | |
308 SkAutoLockPixels alp1(copy); | |
309 // they should both have, or both not-have, a colortable | |
310 bool hasCT = subset.getColorTable() != NULL; | |
311 REPORTER_ASSERT(reporter, | |
312 (copy.getColorTable() != NULL) == hasCT); | |
313 } | |
314 bitmap = srcPremul; | |
315 bitmap.setIsVolatile(false); | |
316 if (bitmap.extractSubset(&subset, r)) { | |
317 REPORTER_ASSERT(reporter, | |
318 subset.alphaType() == bitmap.alphaType() ); | |
319 REPORTER_ASSERT(reporter, | |
320 subset.isVolatile() == false); | |
321 } | |
322 } | |
323 } else { | 345 } else { |
324 // dst should be unchanged from its initial state | 346 // dst should be unchanged from its initial state |
325 REPORTER_ASSERT(reporter, dst.config() == SkBitmap::kNo_Config); | 347 REPORTER_ASSERT(reporter, dst.config() == SkBitmap::kNo_Config); |
326 REPORTER_ASSERT(reporter, dst.width() == 0); | 348 REPORTER_ASSERT(reporter, dst.width() == 0); |
327 REPORTER_ASSERT(reporter, dst.height() == 0); | 349 REPORTER_ASSERT(reporter, dst.height() == 0); |
328 } | 350 } |
329 } // for (size_t j = ... | 351 } // for (size_t j = ... |
330 | 352 |
331 // Tests for getSafeSize(), getSafeSize64(), copyPixelsTo(), | 353 // Tests for getSafeSize(), getSafeSize64(), copyPixelsTo(), |
332 // copyPixelsFrom(). | 354 // copyPixelsFrom(). |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
544 // for the transfer. | 566 // for the transfer. |
545 REPORTER_ASSERT(reporter, | 567 REPORTER_ASSERT(reporter, |
546 subset.copyPixelsFrom(buf, 1, subset.rowBytes()) == | 568 subset.copyPixelsFrom(buf, 1, subset.rowBytes()) == |
547 false); | 569 false); |
548 | 570 |
549 #endif | 571 #endif |
550 } | 572 } |
551 } // for (size_t copyCase ... | 573 } // for (size_t copyCase ... |
552 } | 574 } |
553 } | 575 } |
OLD | NEW |