OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * 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 |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
9 #include "SkRect.h" | 9 #include "SkRect.h" |
10 #include "Test.h" | 10 #include "Test.h" |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 | 178 |
179 // Writes unique pixel values at locations specified by coords. | 179 // Writes unique pixel values at locations specified by coords. |
180 static void writeCoordPixels(SkBitmap& bm, const Coordinates& coords) { | 180 static void writeCoordPixels(SkBitmap& bm, const Coordinates& coords) { |
181 for (int i = 0; i < coords.length; ++i) | 181 for (int i = 0; i < coords.length; ++i) |
182 setPixel(coords[i]->fX, coords[i]->fY, i, bm); | 182 setPixel(coords[i]->fX, coords[i]->fY, i, bm); |
183 } | 183 } |
184 | 184 |
185 static const Pair gPairs[] = { | 185 static const Pair gPairs[] = { |
186 { kUnknown_SkColorType, "000000" }, | 186 { kUnknown_SkColorType, "000000" }, |
187 { kAlpha_8_SkColorType, "010101" }, | 187 { kAlpha_8_SkColorType, "010101" }, |
188 { kIndex_8_SkColorType, "011101" }, | 188 { kIndex_8_SkColorType, "011111" }, |
189 { kRGB_565_SkColorType, "010101" }, | 189 { kRGB_565_SkColorType, "010101" }, |
190 { kARGB_4444_SkColorType, "010111" }, | 190 { kARGB_4444_SkColorType, "010111" }, |
191 { kN32_SkColorType, "010111" }, | 191 { kN32_SkColorType, "010111" }, |
192 }; | 192 }; |
193 | 193 |
194 static const int W = 20; | 194 static const int W = 20; |
195 static const int H = 33; | 195 static const int H = 33; |
196 | 196 |
197 static void setup_src_bitmaps(SkBitmap* srcOpaque, SkBitmap* srcPremul, | 197 static void setup_src_bitmaps(SkBitmap* srcOpaque, SkBitmap* srcPremul, |
198 SkColorType ct) { | 198 SkColorType ct) { |
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
535 // for the transfer. | 535 // for the transfer. |
536 REPORTER_ASSERT(reporter, | 536 REPORTER_ASSERT(reporter, |
537 subset.copyPixelsFrom(buf, 1, subset.rowBytes()) == | 537 subset.copyPixelsFrom(buf, 1, subset.rowBytes()) == |
538 false); | 538 false); |
539 | 539 |
540 #endif | 540 #endif |
541 } | 541 } |
542 } // for (size_t copyCase ... | 542 } // for (size_t copyCase ... |
543 } | 543 } |
544 } | 544 } |
| 545 |
| 546 #include "SkColorPriv.h" |
| 547 #include "SkUtils.h" |
| 548 |
| 549 /** |
| 550 * Construct 4x4 pixels where we can look at a color and determine where it sho
uld be in the grid. |
| 551 * alpha = 0xFF, blue = 0x80, red = x, green = y |
| 552 */ |
| 553 static void fill_4x4_pixels(SkPMColor colors[16]) { |
| 554 for (int y = 0; y < 4; ++y) { |
| 555 for (int x = 0; x < 4; ++x) { |
| 556 colors[y*4+x] = SkPackARGB32(0xFF, x, y, 0x80); |
| 557 } |
| 558 } |
| 559 } |
| 560 |
| 561 static bool check_4x4_pixel(SkPMColor color, unsigned x, unsigned y) { |
| 562 SkASSERT(x < 4 && y < 4); |
| 563 return 0xFF == SkGetPackedA32(color) && |
| 564 x == SkGetPackedR32(color) && |
| 565 y == SkGetPackedG32(color) && |
| 566 0x80 == SkGetPackedB32(color); |
| 567 } |
| 568 |
| 569 /** |
| 570 * Fill with all zeros, which will never match any value from fill_4x4_pixels |
| 571 */ |
| 572 static void clear_4x4_pixels(SkPMColor colors[16]) { |
| 573 sk_memset32(colors, 0, 16); |
| 574 } |
| 575 |
| 576 // Much of readPixels is exercised by copyTo testing, since readPixels is the ba
ckend for that |
| 577 // method. Here we explicitly test subset copies. |
| 578 // |
| 579 DEF_TEST(BitmapReadPixels, reporter) { |
| 580 const int W = 4; |
| 581 const int H = 4; |
| 582 const size_t rowBytes = W * sizeof(SkPMColor); |
| 583 const SkImageInfo srcInfo = SkImageInfo::MakeN32Premul(W, H); |
| 584 SkPMColor srcPixels[16]; |
| 585 fill_4x4_pixels(srcPixels); |
| 586 SkBitmap srcBM; |
| 587 srcBM.installPixels(srcInfo, srcPixels, rowBytes); |
| 588 |
| 589 SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(W, H); |
| 590 SkPMColor dstPixels[16]; |
| 591 |
| 592 const struct { |
| 593 bool fExpectedSuccess; |
| 594 SkIPoint fRequestedSrcLoc; |
| 595 SkISize fRequestedDstSize; |
| 596 // If fExpectedSuccess, check these, otherwise ignore |
| 597 SkIPoint fExpectedDstLoc; |
| 598 SkIRect fExpectedSrcR; |
| 599 } gRec[] = { |
| 600 { true, { 0, 0 }, { 4, 4 }, { 0, 0 }, { 0, 0, 4, 4 } }, |
| 601 { true, { 1, 1 }, { 2, 2 }, { 0, 0 }, { 1, 1, 3, 3 } }, |
| 602 { true, { 2, 2 }, { 4, 4 }, { 0, 0 }, { 2, 2, 4, 4 } }, |
| 603 { true, {-1,-1 }, { 2, 2 }, { 1, 1 }, { 0, 0, 1, 1 } }, |
| 604 { false, {-1,-1 }, { 1, 1 }, { 0, 0 }, { 0, 0, 0, 0 } }, |
| 605 }; |
| 606 |
| 607 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { |
| 608 clear_4x4_pixels(dstPixels); |
| 609 |
| 610 dstInfo.fWidth = gRec[i].fRequestedDstSize.width(); |
| 611 dstInfo.fHeight = gRec[i].fRequestedDstSize.height(); |
| 612 bool success = srcBM.readPixels(dstInfo, dstPixels, rowBytes, |
| 613 gRec[i].fRequestedSrcLoc.x(), gRec[i].fR
equestedSrcLoc.y()); |
| 614 |
| 615 REPORTER_ASSERT(reporter, gRec[i].fExpectedSuccess == success); |
| 616 if (success) { |
| 617 const SkIRect srcR = gRec[i].fExpectedSrcR; |
| 618 const int dstX = gRec[i].fExpectedDstLoc.x(); |
| 619 const int dstY = gRec[i].fExpectedDstLoc.y(); |
| 620 // Walk the dst pixels, and check if we got what we expected |
| 621 for (int y = 0; y < H; ++y) { |
| 622 for (int x = 0; x < W; ++x) { |
| 623 SkPMColor dstC = dstPixels[y*4+x]; |
| 624 // get into src coordinates |
| 625 int sx = x - dstX + srcR.x(); |
| 626 int sy = y - dstY + srcR.y(); |
| 627 if (srcR.contains(sx, sy)) { |
| 628 REPORTER_ASSERT(reporter, check_4x4_pixel(dstC, sx, sy))
; |
| 629 } else { |
| 630 REPORTER_ASSERT(reporter, 0 == dstC); |
| 631 } |
| 632 } |
| 633 } |
| 634 } |
| 635 } |
| 636 } |
| 637 |
OLD | NEW |