blob: a89064b62f84cc48fa5cf616c21af1b6c76ee362 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com42263962009-05-01 04:00:01 +00008#include "Test.h"
9#include "SkBitmap.h"
reed@android.com311c82d2009-05-05 23:13:23 +000010#include "SkRect.h"
reed@android.com42263962009-05-01 04:00:01 +000011
12static const char* boolStr(bool value) {
13 return value ? "true" : "false";
14}
15
16// these are in the same order as the SkBitmap::Config enum
17static const char* gConfigName[] = {
18 "None", "A1", "A8", "Index8", "565", "4444", "8888", "RLE_Index8"
19};
20
reed@android.comcafc9f92009-08-22 03:44:57 +000021static void report_opaqueness(skiatest::Reporter* reporter, const SkBitmap& src,
22 const SkBitmap& dst) {
23 SkString str;
24 str.printf("src %s opaque:%d, dst %s opaque:%d",
25 gConfigName[src.config()], src.isOpaque(),
26 gConfigName[dst.config()], dst.isOpaque());
27 reporter->reportFailed(str);
28}
29
30static bool canHaveAlpha(SkBitmap::Config config) {
31 return config != SkBitmap::kRGB_565_Config;
32}
33
34// copyTo() should preserve isOpaque when it makes sense
35static void test_isOpaque(skiatest::Reporter* reporter, const SkBitmap& src,
36 SkBitmap::Config dstConfig) {
37 SkBitmap bitmap(src);
38 SkBitmap dst;
39
40 // we need the lock so that we get a valid colorTable (when available)
41 SkAutoLockPixels alp(bitmap);
42 SkColorTable* ctable = bitmap.getColorTable();
43 unsigned ctableFlags = ctable ? ctable->getFlags() : 0;
44
45 if (canHaveAlpha(bitmap.config()) && canHaveAlpha(dstConfig)) {
46 bitmap.setIsOpaque(false);
47 if (ctable) {
48 ctable->setFlags(ctableFlags & ~SkColorTable::kColorsAreOpaque_Flag);
49 }
50 REPORTER_ASSERT(reporter, bitmap.copyTo(&dst, dstConfig));
51 REPORTER_ASSERT(reporter, dst.config() == dstConfig);
52 if (bitmap.isOpaque() != dst.isOpaque()) {
53 report_opaqueness(reporter, bitmap, dst);
54 }
55 }
56
57 bitmap.setIsOpaque(true);
58 if (ctable) {
59 ctable->setFlags(ctableFlags | SkColorTable::kColorsAreOpaque_Flag);
60 }
61 REPORTER_ASSERT(reporter, bitmap.copyTo(&dst, dstConfig));
62 REPORTER_ASSERT(reporter, dst.config() == dstConfig);
63 if (bitmap.isOpaque() != dst.isOpaque()) {
64 report_opaqueness(reporter, bitmap, dst);
65 }
66
67 if (ctable) {
68 ctable->setFlags(ctableFlags);
69 }
70}
71
reed@google.com9ce6e752011-01-10 14:04:07 +000072static void init_src(const SkBitmap& bitmap, const SkColorTable* ct) {
weita@google.comf9ab99a2009-05-03 18:23:30 +000073 SkAutoLockPixels lock(bitmap);
reed@android.com42263962009-05-01 04:00:01 +000074 if (bitmap.getPixels()) {
reed@google.com9ce6e752011-01-10 14:04:07 +000075 if (ct) {
76 sk_bzero(bitmap.getPixels(), bitmap.getSize());
77 } else {
78 bitmap.eraseColor(SK_ColorWHITE);
79 }
reed@android.com42263962009-05-01 04:00:01 +000080 }
81}
82
83SkColorTable* init_ctable() {
84 static const SkColor colors[] = {
85 SK_ColorBLACK, SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE
86 };
87 return new SkColorTable(colors, SK_ARRAY_COUNT(colors));
88}
89
90struct Pair {
91 SkBitmap::Config fConfig;
92 const char* fValid;
93};
94
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000095// Utility functions for copyPixelsTo()/copyPixelsFrom() tests.
96// getPixel()
97// setPixel()
98// getSkConfigName()
99// struct Coordinates
100// reportCopyVerification()
101// writeCoordPixels()
102
103// Utility function to read the value of a given pixel in bm. All
104// values converted to uint32_t for simplification of comparisons.
105uint32_t getPixel(int x, int y, const SkBitmap& bm) {
106 uint32_t val = 0;
107 uint16_t val16;
108 uint8_t val8, shift;
109 SkAutoLockPixels lock(bm);
110 const void* rawAddr = bm.getAddr(x,y);
111
112 switch (bm.getConfig()) {
113 case SkBitmap::kARGB_8888_Config:
114 memcpy(&val, rawAddr, sizeof(uint32_t));
115 break;
116 case SkBitmap::kARGB_4444_Config:
117 case SkBitmap::kRGB_565_Config:
118 memcpy(&val16, rawAddr, sizeof(uint16_t));
119 val = val16;
120 break;
121 case SkBitmap::kA8_Config:
122 case SkBitmap::kIndex8_Config:
123 memcpy(&val8, rawAddr, sizeof(uint8_t));
124 val = val8;
125 break;
126 case SkBitmap::kA1_Config:
127 memcpy(&val8, rawAddr, sizeof(uint8_t));
128 shift = x % 8;
129 val = (val8 >> shift) & 0x1 ;
130 break;
131 default:
132 break;
133 }
134 return val;
135}
136
137// Utility function to set value of any pixel in bm.
138// bm.getConfig() specifies what format 'val' must be
139// converted to, but at present uint32_t can handle all formats.
140void setPixel(int x, int y, uint32_t val, SkBitmap& bm) {
141 uint16_t val16;
142 uint8_t val8, shift;
143 SkAutoLockPixels lock(bm);
144 void* rawAddr = bm.getAddr(x,y);
145
146 switch (bm.getConfig()) {
147 case SkBitmap::kARGB_8888_Config:
148 memcpy(rawAddr, &val, sizeof(uint32_t));
149 break;
150 case SkBitmap::kARGB_4444_Config:
151 case SkBitmap::kRGB_565_Config:
152 val16 = val & 0xFFFF;
153 memcpy(rawAddr, &val16, sizeof(uint16_t));
154 break;
155 case SkBitmap::kA8_Config:
156 case SkBitmap::kIndex8_Config:
157 val8 = val & 0xFF;
158 memcpy(rawAddr, &val8, sizeof(uint8_t));
159 break;
160 case SkBitmap::kA1_Config:
161 shift = x % 8; // We assume we're in the right byte.
162 memcpy(&val8, rawAddr, sizeof(uint8_t));
163 if (val & 0x1) // Turn bit on.
164 val8 |= (0x1 << shift);
165 else // Turn bit off.
166 val8 &= ~(0x1 << shift);
167 memcpy(rawAddr, &val8, sizeof(uint8_t));
168 break;
169 default:
170 // Ignore.
171 break;
172 }
173}
174
175// Utility to return string containing name of each format, to
176// simplify diagnostic output.
177const char* getSkConfigName(const SkBitmap& bm) {
178 switch (bm.getConfig()) {
179 case SkBitmap::kNo_Config: return "SkBitmap::kNo_Config";
180 case SkBitmap::kA1_Config: return "SkBitmap::kA1_Config";
181 case SkBitmap::kA8_Config: return "SkBitmap::kA8_Config";
182 case SkBitmap::kIndex8_Config: return "SkBitmap::kIndex8_Config";
183 case SkBitmap::kRGB_565_Config: return "SkBitmap::kRGB_565_Config";
184 case SkBitmap::kARGB_4444_Config: return "SkBitmap::kARGB_4444_Config";
185 case SkBitmap::kARGB_8888_Config: return "SkBitmap::kARGB_8888_Config";
186 case SkBitmap::kRLE_Index8_Config:
187 return "SkBitmap::kRLE_Index8_Config,";
188 default: return "Unknown SkBitmap configuration.";
189 }
190}
191
192// Helper struct to contain pixel locations, while avoiding need for STL.
193struct Coordinates {
194
195 const int length;
196 SkIPoint* const data;
197
198 explicit Coordinates(int _length): length(_length)
199 , data(new SkIPoint[length]) { }
200
201 ~Coordinates(){
202 delete [] data;
203 }
204
205 SkIPoint* operator[](int i) const {
206 // Use with care, no bounds checking.
207 return data + i;
208 }
209};
210
211// A function to verify that two bitmaps contain the same pixel values
212// at all coordinates indicated by coords. Simplifies verification of
213// copied bitmaps.
214void reportCopyVerification(const SkBitmap& bm1, const SkBitmap& bm2,
215 Coordinates& coords,
216 const char* msg,
217 skiatest::Reporter* reporter){
218 bool success = true;
219
220 // Confirm all pixels in the list match.
221 for (int i = 0; i < coords.length; ++i)
222 success = success &&
223 (getPixel(coords[i]->fX, coords[i]->fY, bm1) ==
224 getPixel(coords[i]->fX, coords[i]->fY, bm2));
225
226 if (!success) {
227 SkString str;
228 str.printf("%s [config = %s]",
229 msg, getSkConfigName(bm1));
230 reporter->reportFailed(str);
231 }
232}
233
234// Writes unique pixel values at locations specified by coords.
235void writeCoordPixels(SkBitmap& bm, const Coordinates& coords) {
236 for (int i = 0; i < coords.length; ++i)
237 setPixel(coords[i]->fX, coords[i]->fY, i, bm);
238}
239
reed@android.com42263962009-05-01 04:00:01 +0000240static void TestBitmapCopy(skiatest::Reporter* reporter) {
241 static const Pair gPairs[] = {
242 { SkBitmap::kNo_Config, "00000000" },
weita@google.comf9ab99a2009-05-03 18:23:30 +0000243 { SkBitmap::kA1_Config, "01000000" },
reed@android.com42263962009-05-01 04:00:01 +0000244 { SkBitmap::kA8_Config, "00101110" },
245 { SkBitmap::kIndex8_Config, "00111110" },
246 { SkBitmap::kRGB_565_Config, "00101110" },
247 { SkBitmap::kARGB_4444_Config, "00101110" },
248 { SkBitmap::kARGB_8888_Config, "00101110" },
reed@android.comfbaa88d2009-05-06 17:44:34 +0000249// TODO: create valid RLE bitmap to test with
250 // { SkBitmap::kRLE_Index8_Config, "00101111" }
reed@android.com42263962009-05-01 04:00:01 +0000251 };
weita@google.comf9ab99a2009-05-03 18:23:30 +0000252
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000253 static const bool isExtracted[] = {
254 false, true
255 };
256
reed@android.com42263962009-05-01 04:00:01 +0000257 const int W = 20;
258 const int H = 33;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000259
reed@android.com42263962009-05-01 04:00:01 +0000260 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
261 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
262 SkBitmap src, dst;
263 SkColorTable* ct = NULL;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000264
reed@android.com42263962009-05-01 04:00:01 +0000265 src.setConfig(gPairs[i].fConfig, W, H);
reed@android.comfbaa88d2009-05-06 17:44:34 +0000266 if (SkBitmap::kIndex8_Config == src.config() ||
267 SkBitmap::kRLE_Index8_Config == src.config()) {
reed@android.com42263962009-05-01 04:00:01 +0000268 ct = init_ctable();
269 }
270 src.allocPixels(ct);
reed@android.comd0a529d2010-01-08 14:01:41 +0000271 SkSafeUnref(ct);
reed@android.com42263962009-05-01 04:00:01 +0000272
reed@google.com9ce6e752011-01-10 14:04:07 +0000273 init_src(src, ct);
reed@android.com42263962009-05-01 04:00:01 +0000274 bool success = src.copyTo(&dst, gPairs[j].fConfig);
275 bool expected = gPairs[i].fValid[j] != '0';
276 if (success != expected) {
277 SkString str;
278 str.printf("SkBitmap::copyTo from %s to %s. expected %s returned %s",
279 gConfigName[i], gConfigName[j], boolStr(expected),
280 boolStr(success));
281 reporter->reportFailed(str);
282 }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000283
reed@android.comfbaa88d2009-05-06 17:44:34 +0000284 bool canSucceed = src.canCopyTo(gPairs[j].fConfig);
285 if (success != canSucceed) {
286 SkString str;
287 str.printf("SkBitmap::copyTo from %s to %s. returned %s canCopyTo %s",
288 gConfigName[i], gConfigName[j], boolStr(success),
289 boolStr(canSucceed));
290 reporter->reportFailed(str);
291 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000292
reed@android.com42263962009-05-01 04:00:01 +0000293 if (success) {
294 REPORTER_ASSERT(reporter, src.width() == dst.width());
295 REPORTER_ASSERT(reporter, src.height() == dst.height());
weita@google.comf9ab99a2009-05-03 18:23:30 +0000296 REPORTER_ASSERT(reporter, dst.config() == gPairs[j].fConfig);
reed@android.comcafc9f92009-08-22 03:44:57 +0000297 test_isOpaque(reporter, src, dst.config());
reed@android.com42263962009-05-01 04:00:01 +0000298 if (src.config() == dst.config()) {
weita@google.comf9ab99a2009-05-03 18:23:30 +0000299 SkAutoLockPixels srcLock(src);
reed@android.com42263962009-05-01 04:00:01 +0000300 SkAutoLockPixels dstLock(dst);
301 REPORTER_ASSERT(reporter, src.readyToDraw());
302 REPORTER_ASSERT(reporter, dst.readyToDraw());
303 const char* srcP = (const char*)src.getAddr(0, 0);
304 const char* dstP = (const char*)dst.getAddr(0, 0);
305 REPORTER_ASSERT(reporter, srcP != dstP);
306 REPORTER_ASSERT(reporter, !memcmp(srcP, dstP,
307 src.getSize()));
308 }
reed@android.com311c82d2009-05-05 23:13:23 +0000309 // test extractSubset
310 {
skyostil@google.comce7adb52012-01-13 14:56:51 +0000311 SkBitmap bitmap(src);
reed@android.com311c82d2009-05-05 23:13:23 +0000312 SkBitmap subset;
313 SkIRect r;
314 r.set(1, 1, 2, 2);
skyostil@google.comce7adb52012-01-13 14:56:51 +0000315 bitmap.setIsOpaque(true);
skyostil@google.com0eb75762012-01-16 10:45:53 +0000316 bitmap.setIsVolatile(true);
skyostil@google.comce7adb52012-01-13 14:56:51 +0000317 if (bitmap.extractSubset(&subset, r)) {
reed@android.com311c82d2009-05-05 23:13:23 +0000318 REPORTER_ASSERT(reporter, subset.width() == 1);
319 REPORTER_ASSERT(reporter, subset.height() == 1);
skyostil@google.comce7adb52012-01-13 14:56:51 +0000320 REPORTER_ASSERT(reporter,
321 subset.isOpaque() == bitmap.isOpaque());
skyostil@google.com0eb75762012-01-16 10:45:53 +0000322 REPORTER_ASSERT(reporter,
323 subset.isVolatile() == true);
reed@android.com311c82d2009-05-05 23:13:23 +0000324
325 SkBitmap copy;
326 REPORTER_ASSERT(reporter,
327 subset.copyTo(&copy, subset.config()));
328 REPORTER_ASSERT(reporter, copy.width() == 1);
329 REPORTER_ASSERT(reporter, copy.height() == 1);
330 REPORTER_ASSERT(reporter, copy.rowBytes() <= 4);
reed@google.com1fcd51e2011-01-05 15:50:27 +0000331
reed@android.com311c82d2009-05-05 23:13:23 +0000332 SkAutoLockPixels alp0(subset);
333 SkAutoLockPixels alp1(copy);
334 // they should both have, or both not-have, a colortable
335 bool hasCT = subset.getColorTable() != NULL;
336 REPORTER_ASSERT(reporter,
337 (copy.getColorTable() != NULL) == hasCT);
338 }
skyostil@google.comce7adb52012-01-13 14:56:51 +0000339 bitmap.setIsOpaque(false);
skyostil@google.com0eb75762012-01-16 10:45:53 +0000340 bitmap.setIsVolatile(false);
skyostil@google.comce7adb52012-01-13 14:56:51 +0000341 if (bitmap.extractSubset(&subset, r)) {
342 REPORTER_ASSERT(reporter,
343 subset.isOpaque() == bitmap.isOpaque());
skyostil@google.com0eb75762012-01-16 10:45:53 +0000344 REPORTER_ASSERT(reporter,
345 subset.isVolatile() == false);
skyostil@google.comce7adb52012-01-13 14:56:51 +0000346 }
reed@android.com311c82d2009-05-05 23:13:23 +0000347 }
reed@android.com42263962009-05-01 04:00:01 +0000348 } else {
349 // dst should be unchanged from its initial state
350 REPORTER_ASSERT(reporter, dst.config() == SkBitmap::kNo_Config);
351 REPORTER_ASSERT(reporter, dst.width() == 0);
352 REPORTER_ASSERT(reporter, dst.height() == 0);
353 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000354 } // for (size_t j = ...
355
356 // Tests for getSafeSize(), getSafeSize64(), copyPixelsTo(),
357 // copyPixelsFrom().
358 //
359 for (size_t copyCase = 0; copyCase < SK_ARRAY_COUNT(isExtracted);
360 ++copyCase) {
361 // Test copying to/from external buffer.
362 // Note: the tests below have hard-coded values ---
363 // Please take care if modifying.
364 if (gPairs[i].fConfig != SkBitmap::kRLE_Index8_Config) {
365
366 // Tests for getSafeSize64().
367 // Test with a very large configuration without pixel buffer
368 // attached.
369 SkBitmap tstSafeSize;
370 tstSafeSize.setConfig(gPairs[i].fConfig, 100000000U,
371 100000000U);
372 Sk64 safeSize = tstSafeSize.getSafeSize64();
373 if (safeSize.isNeg()) {
374 SkString str;
375 str.printf("getSafeSize64() negative: %s",
376 getSkConfigName(tstSafeSize));
377 reporter->reportFailed(str);
378 }
379 bool sizeFail = false;
380 // Compare against hand-computed values.
381 switch (gPairs[i].fConfig) {
382 case SkBitmap::kNo_Config:
383 break;
384
385 case SkBitmap::kA1_Config:
386 if (safeSize.fHi != 0x470DE ||
387 safeSize.fLo != 0x4DF82000)
388 sizeFail = true;
389 break;
390
391 case SkBitmap::kA8_Config:
392 case SkBitmap::kIndex8_Config:
393 if (safeSize.fHi != 0x2386F2 ||
394 safeSize.fLo != 0x6FC10000)
395 sizeFail = true;
396 break;
397
398 case SkBitmap::kRGB_565_Config:
399 case SkBitmap::kARGB_4444_Config:
400 if (safeSize.fHi != 0x470DE4 ||
401 safeSize.fLo != 0xDF820000)
402 sizeFail = true;
403 break;
404
405 case SkBitmap::kARGB_8888_Config:
406 if (safeSize.fHi != 0x8E1BC9 ||
407 safeSize.fLo != 0xBF040000)
408 sizeFail = true;
409 break;
410
411 case SkBitmap::kRLE_Index8_Config:
412 break;
413
414 default:
415 break;
416 }
417 if (sizeFail) {
418 SkString str;
419 str.printf("getSafeSize64() wrong size: %s",
420 getSkConfigName(tstSafeSize));
421 reporter->reportFailed(str);
422 }
423
424 size_t subW, subH;
425 // Set sizes to be height = 2 to force the last row of the
426 // source to be used, thus verifying correct operation if
427 // the bitmap is an extracted subset.
428 if (gPairs[i].fConfig == SkBitmap::kA1_Config) {
429 // If one-bit per pixel, use 9 pixels to force more than
430 // one byte per row.
431 subW = 9;
432 subH = 2;
433 } else {
434 // All other configurations are at least one byte per pixel,
435 // and different configs will test copying different numbers
436 // of bytes.
437 subW = subH = 2;
438 }
439
440 // Create bitmap to act as source for copies and subsets.
441 SkBitmap src, subset;
442 SkColorTable* ct = NULL;
443 if (isExtracted[copyCase]) { // A larger image to extract from.
444 src.setConfig(gPairs[i].fConfig, 2 * subW + 1, subH);
445 } else // Tests expect a 2x2 bitmap, so make smaller.
446 src.setConfig(gPairs[i].fConfig, subW, subH);
447 if (SkBitmap::kIndex8_Config == src.config() ||
448 SkBitmap::kRLE_Index8_Config == src.config()) {
449 ct = init_ctable();
450 }
451
452 src.allocPixels(ct);
453 SkSafeUnref(ct);
454
455 // Either copy src or extract into 'subset', which is used
456 // for subsequent calls to copyPixelsTo/From.
457 bool srcReady = false;
458 if (isExtracted[copyCase]) {
459 // The extractedSubset() test case allows us to test copy-
460 // ing when src and dst mave possibly different strides.
461 SkIRect r;
462 if (gPairs[i].fConfig == SkBitmap::kA1_Config)
463 // This config seems to need byte-alignment of
464 // extracted subset bits.
465 r.set(0, 0, subW, subH);
466 else
467 r.set(1, 0, 1 + subW, subH); // 2x2 extracted bitmap
468
469 srcReady = src.extractSubset(&subset, r);
470 } else {
471 srcReady = src.copyTo(&subset, src.getConfig());
472 }
473
474 // Not all configurations will generate a valid 'subset'.
475 if (srcReady) {
476
477 // Allocate our target buffer 'buf' for all copies.
478 // To simplify verifying correctness of copies attach
479 // buf to a SkBitmap, but copies are done using the
480 // raw buffer pointer.
481 const uint32_t bufSize = subH *
482 SkBitmap::ComputeRowBytes(src.getConfig(), subW) * 2;
483 uint8_t* buf = new uint8_t[bufSize];
484
485 SkBitmap bufBm; // Attach buf to this bitmap.
486 bool successExpected;
487
488 // Set up values for each pixel being copied.
489 Coordinates coords(subW * subH);
490 for (size_t x = 0; x < subW; ++x)
491 for (size_t y = 0; y < subH; ++y)
492 {
reed@google.com1fcd51e2011-01-05 15:50:27 +0000493 int index = y * subW + x;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000494 SkASSERT(index < coords.length);
495 coords[index]->fX = x;
496 coords[index]->fY = y;
497 }
498
499 writeCoordPixels(subset, coords);
500
501 // Test #1 ////////////////////////////////////////////
502
503 // Before/after comparisons easier if we attach buf
504 // to an appropriately configured SkBitmap.
505 memset(buf, 0xFF, bufSize);
506 // Config with stride greater than src but that fits in buf.
507 bufBm.setConfig(gPairs[i].fConfig, subW, subH,
508 SkBitmap::ComputeRowBytes(subset.getConfig(), subW)
509 * 2);
510 bufBm.setPixels(buf);
511 successExpected = false;
512 // Then attempt to copy with a stride that is too large
513 // to fit in the buffer.
514 REPORTER_ASSERT(reporter,
515 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes() * 3)
516 == successExpected);
517
518 if (successExpected)
519 reportCopyVerification(subset, bufBm, coords,
520 "copyPixelsTo(buf, bufSize, 1.5*maxRowBytes)",
521 reporter);
522
523 // Test #2 ////////////////////////////////////////////
524 // This test should always succeed, but in the case
525 // of extracted bitmaps only because we handle the
526 // issue of getSafeSize(). Without getSafeSize()
527 // buffer overrun/read would occur.
528 memset(buf, 0xFF, bufSize);
529 bufBm.setConfig(gPairs[i].fConfig, subW, subH,
530 subset.rowBytes());
531 bufBm.setPixels(buf);
532 successExpected = subset.getSafeSize() <= bufSize;
533 REPORTER_ASSERT(reporter,
534 subset.copyPixelsTo(buf, bufSize) ==
535 successExpected);
536 if (successExpected)
537 reportCopyVerification(subset, bufBm, coords,
538 "copyPixelsTo(buf, bufSize)", reporter);
539
540 // Test #3 ////////////////////////////////////////////
541 // Copy with different stride between src and dst.
542 memset(buf, 0xFF, bufSize);
543 bufBm.setConfig(gPairs[i].fConfig, subW, subH,
544 subset.rowBytes()+1);
545 bufBm.setPixels(buf);
546 successExpected = true; // Should always work.
547 REPORTER_ASSERT(reporter,
548 subset.copyPixelsTo(buf, bufSize,
549 subset.rowBytes()+1) == successExpected);
550 if (successExpected)
551 reportCopyVerification(subset, bufBm, coords,
552 "copyPixelsTo(buf, bufSize, rowBytes+1)", reporter);
553
554 // Test #4 ////////////////////////////////////////////
555 // Test copy with stride too small.
556 memset(buf, 0xFF, bufSize);
557 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
558 bufBm.setPixels(buf);
559 successExpected = false;
560 // Request copy with stride too small.
561 REPORTER_ASSERT(reporter,
562 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes()-1)
563 == successExpected);
564 if (successExpected)
565 reportCopyVerification(subset, bufBm, coords,
566 "copyPixelsTo(buf, bufSize, rowBytes()-1)", reporter);
567
reed@google.comab77aaf2011-11-01 16:03:35 +0000568#if 0 // copyPixelsFrom is gone
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000569 // Test #5 ////////////////////////////////////////////
570 // Tests the case where the source stride is too small
571 // for the source configuration.
572 memset(buf, 0xFF, bufSize);
573 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
574 bufBm.setPixels(buf);
575 writeCoordPixels(bufBm, coords);
576 REPORTER_ASSERT(reporter,
577 subset.copyPixelsFrom(buf, bufSize, 1) == false);
578
epoger@google.com69731aa2011-04-26 19:31:33 +0000579 // Test #6 ///////////////////////////////////////////
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000580 // Tests basic copy from an external buffer to the bitmap.
581 // If the bitmap is "extracted", this also tests the case
582 // where the source stride is different from the dest.
583 // stride.
584 // We've made the buffer large enough to always succeed.
585 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
586 bufBm.setPixels(buf);
587 writeCoordPixels(bufBm, coords);
588 REPORTER_ASSERT(reporter,
589 subset.copyPixelsFrom(buf, bufSize, bufBm.rowBytes()) ==
590 true);
591 reportCopyVerification(bufBm, subset, coords,
592 "copyPixelsFrom(buf, bufSize)",
593 reporter);
594
595 // Test #7 ////////////////////////////////////////////
596 // Tests the case where the source buffer is too small
597 // for the transfer.
598 REPORTER_ASSERT(reporter,
599 subset.copyPixelsFrom(buf, 1, subset.rowBytes()) ==
600 false);
601
602 delete [] buf;
reed@google.comab77aaf2011-11-01 16:03:35 +0000603#endif
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000604 }
605 }
606 } // for (size_t copyCase ...
reed@android.com42263962009-05-01 04:00:01 +0000607 }
608}
609
610#include "TestClassDef.h"
611DEFINE_TESTCLASS("BitmapCopy", TestBitmapCopyClass, TestBitmapCopy)