| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2010 The Android Open Source Project | 2 * Copyright 2010 The Android Open Source Project |
| 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 "SkPDFImage.h" | 8 #include "SkPDFImage.h" |
| 9 | 9 |
| 10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 for (int x = srcRect.fLeft; x < srcRect.fRight; x++) { | 126 for (int x = srcRect.fLeft; x < srcRect.fRight; x++) { |
| 127 dst[0] = SkGetPackedR16(src[x]); | 127 dst[0] = SkGetPackedR16(src[x]); |
| 128 dst[1] = SkGetPackedG16(src[x]); | 128 dst[1] = SkGetPackedG16(src[x]); |
| 129 dst[2] = SkGetPackedB16(src[x]); | 129 dst[2] = SkGetPackedB16(src[x]); |
| 130 dst += 3; | 130 dst += 3; |
| 131 } | 131 } |
| 132 } | 132 } |
| 133 return stream; | 133 return stream; |
| 134 } | 134 } |
| 135 | 135 |
| 136 static uint32_t get_argb8888_neighbor_avg_color(const SkBitmap& bitmap, |
| 137 int xOrig, |
| 138 int yOrig); |
| 139 |
| 136 static SkStream* extract_argb8888_data(const SkBitmap& bitmap, | 140 static SkStream* extract_argb8888_data(const SkBitmap& bitmap, |
| 137 const SkIRect& srcRect, | 141 const SkIRect& srcRect, |
| 138 bool extractAlpha, | 142 bool extractAlpha, |
| 139 bool* isOpaque, | 143 bool* isOpaque, |
| 140 bool* isTransparent) { | 144 bool* isTransparent) { |
| 141 SkStream* stream; | 145 size_t streamSize = extractAlpha ? srcRect.width() * srcRect.height() |
| 142 if (extractAlpha) { | 146 : get_uncompressed_size(bitmap, srcRect); |
| 143 stream = SkNEW_ARGS(SkMemoryStream, | 147 SkStream* stream = SkNEW_ARGS(SkMemoryStream, (streamSize)); |
| 144 (srcRect.width() * srcRect.height())); | |
| 145 } else { | |
| 146 stream = SkNEW_ARGS(SkMemoryStream, | |
| 147 (get_uncompressed_size(bitmap, srcRect))); | |
| 148 } | |
| 149 uint8_t* dst = (uint8_t*)stream->getMemoryBase(); | 148 uint8_t* dst = (uint8_t*)stream->getMemoryBase(); |
| 150 | 149 |
| 150 const SkUnPreMultiply::Scale* scaleTable = SkUnPreMultiply::GetScaleTable(); |
| 151 |
| 151 for (int y = srcRect.fTop; y < srcRect.fBottom; y++) { | 152 for (int y = srcRect.fTop; y < srcRect.fBottom; y++) { |
| 152 uint32_t* src = bitmap.getAddr32(0, y); | 153 uint32_t* src = bitmap.getAddr32(0, y); |
| 153 for (int x = srcRect.fLeft; x < srcRect.fRight; x++) { | 154 for (int x = srcRect.fLeft; x < srcRect.fRight; x++) { |
| 155 SkPMColor c = src[x]; |
| 156 U8CPU alpha = SkGetPackedA32(c); |
| 154 if (extractAlpha) { | 157 if (extractAlpha) { |
| 155 dst[0] = SkGetPackedA32(src[x]); | 158 *isOpaque &= alpha == SK_AlphaOPAQUE; |
| 156 *isOpaque &= dst[0] == SK_AlphaOPAQUE; | 159 *isTransparent &= alpha == SK_AlphaTRANSPARENT; |
| 157 *isTransparent &= dst[0] == SK_AlphaTRANSPARENT; | 160 *dst++ = alpha; |
| 158 dst++; | |
| 159 } else { | 161 } else { |
| 160 dst[0] = SkGetPackedR32(src[x]); | 162 if (SK_AlphaTRANSPARENT == alpha) { |
| 161 dst[1] = SkGetPackedG32(src[x]); | 163 // It is necessary to average the color component of |
| 162 dst[2] = SkGetPackedB32(src[x]); | 164 // transparent pixels with their surrounding neighbors |
| 163 dst += 3; | 165 // since the PDF renderer may separately re-sample the |
| 166 // alpha and color channels when the image is not |
| 167 // displayed at its native resolution. Since an alpha of |
| 168 // zero gives no information about the color component, |
| 169 // the pathological case is a white image with sharp |
| 170 // transparency bounds - the color channel goes to black, |
| 171 // and the should-be-transparent pixels are rendered |
| 172 // as grey because of the separate soft mask and color |
| 173 // resizing. |
| 174 c = get_argb8888_neighbor_avg_color(bitmap, x, y); |
| 175 *dst++ = SkGetPackedR32(c); |
| 176 *dst++ = SkGetPackedG32(c); |
| 177 *dst++ = SkGetPackedB32(c); |
| 178 } else { |
| 179 SkUnPreMultiply::Scale s = scaleTable[alpha]; |
| 180 *dst++ = SkUnPreMultiply::ApplyScale(s, SkGetPackedR32(c)); |
| 181 *dst++ = SkUnPreMultiply::ApplyScale(s, SkGetPackedG32(c)); |
| 182 *dst++ = SkUnPreMultiply::ApplyScale(s, SkGetPackedB32(c)); |
| 183 } |
| 164 } | 184 } |
| 165 } | 185 } |
| 166 } | 186 } |
| 187 SkASSERT(dst == streamSize + (uint8_t*)stream->getMemoryBase()); |
| 167 return stream; | 188 return stream; |
| 168 } | 189 } |
| 169 | 190 |
| 170 static SkStream* extract_a8_alpha(const SkBitmap& bitmap, | 191 static SkStream* extract_a8_alpha(const SkBitmap& bitmap, |
| 171 const SkIRect& srcRect, | 192 const SkIRect& srcRect, |
| 172 bool* isOpaque, | 193 bool* isOpaque, |
| 173 bool* isTransparent) { | 194 bool* isTransparent) { |
| 174 const int alphaRowBytes = srcRect.width(); | 195 const int alphaRowBytes = srcRect.width(); |
| 175 SkStream* stream = SkNEW_ARGS(SkMemoryStream, | 196 SkStream* stream = SkNEW_ARGS(SkMemoryStream, |
| 176 (alphaRowBytes * srcRect.height())); | 197 (alphaRowBytes * srcRect.height())); |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 extract_image_data(bitmap, srcRect, true, &isTransparent)); | 478 extract_image_data(bitmap, srcRect, true, &isTransparent)); |
| 458 } | 479 } |
| 459 if (isTransparent) { | 480 if (isTransparent) { |
| 460 return NULL; | 481 return NULL; |
| 461 } | 482 } |
| 462 | 483 |
| 463 SkPDFImage* image; | 484 SkPDFImage* image; |
| 464 SkColorType colorType = bitmap.colorType(); | 485 SkColorType colorType = bitmap.colorType(); |
| 465 if (alphaData.get() != NULL && (kN32_SkColorType == colorType || | 486 if (alphaData.get() != NULL && (kN32_SkColorType == colorType || |
| 466 kARGB_4444_SkColorType == colorType)) { | 487 kARGB_4444_SkColorType == colorType)) { |
| 467 SkBitmap unpremulBitmap = unpremultiply_bitmap(bitmap, srcRect); | 488 if (kN32_SkColorType == colorType) { |
| 468 image = SkNEW_ARGS(SkPDFImage, (NULL, unpremulBitmap, false, | 489 image = SkNEW_ARGS(SkPDFImage, (NULL, bitmap, false, |
| 469 SkIRect::MakeWH(srcRect.width(), srcRect.height()), | 490 SkIRect::MakeWH(srcRect.width(), |
| 470 encoder)); | 491 srcRect.height()), |
| 492 encoder)); |
| 493 } else { |
| 494 SkBitmap unpremulBitmap = unpremultiply_bitmap(bitmap, srcRect); |
| 495 image = SkNEW_ARGS(SkPDFImage, (NULL, unpremulBitmap, false, |
| 496 SkIRect::MakeWH(srcRect.width(), |
| 497 srcRect.height()), |
| 498 encoder)); |
| 499 } |
| 471 } else { | 500 } else { |
| 472 image = SkNEW_ARGS(SkPDFImage, (NULL, bitmap, false, srcRect, encoder)); | 501 image = SkNEW_ARGS(SkPDFImage, (NULL, bitmap, false, srcRect, encoder)); |
| 473 } | 502 } |
| 474 if (alphaData.get() != NULL) { | 503 if (alphaData.get() != NULL) { |
| 475 SkAutoTUnref<SkPDFImage> mask( | 504 SkAutoTUnref<SkPDFImage> mask( |
| 476 SkNEW_ARGS(SkPDFImage, (alphaData.get(), bitmap, | 505 SkNEW_ARGS(SkPDFImage, (alphaData.get(), bitmap, |
| 477 true, srcRect, NULL))); | 506 true, srcRect, NULL))); |
| 478 image->addSMask(mask); | 507 image->addSMask(mask); |
| 479 } | 508 } |
| 480 | 509 |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 581 fEncoder(pdfImage.fEncoder), | 610 fEncoder(pdfImage.fEncoder), |
| 582 fStreamValid(pdfImage.fStreamValid) { | 611 fStreamValid(pdfImage.fStreamValid) { |
| 583 // Nothing to do here - the image params are already copied in SkPDFStream's | 612 // Nothing to do here - the image params are already copied in SkPDFStream's |
| 584 // constructor, and the bitmap will be regenerated and encoded in | 613 // constructor, and the bitmap will be regenerated and encoded in |
| 585 // populate. | 614 // populate. |
| 586 } | 615 } |
| 587 | 616 |
| 588 bool SkPDFImage::populate(SkPDFCatalog* catalog) { | 617 bool SkPDFImage::populate(SkPDFCatalog* catalog) { |
| 589 if (getState() == kUnused_State) { | 618 if (getState() == kUnused_State) { |
| 590 // Initializing image data for the first time. | 619 // Initializing image data for the first time. |
| 591 SkDynamicMemoryWStream dctCompressedWStream; | |
| 592 if (!skip_compression(catalog) && fEncoder && | 620 if (!skip_compression(catalog) && fEncoder && |
| 593 get_uncompressed_size(fBitmap, fSrcRect) > 1) { | 621 get_uncompressed_size(fBitmap, fSrcRect) > 1) { |
| 594 SkBitmap subset; | 622 SkBitmap subset; |
| 595 // Extract subset | 623 // Extract subset |
| 596 if (!fBitmap.extractSubset(&subset, fSrcRect)) { | 624 if (!fBitmap.extractSubset(&subset, fSrcRect)) { |
| 597 return false; | 625 return false; |
| 598 } | 626 } |
| 599 size_t pixelRefOffset = 0; | 627 size_t pixelRefOffset = 0; |
| 600 SkAutoTUnref<SkData> data(fEncoder(&pixelRefOffset, subset)); | 628 SkAutoTUnref<SkData> data(fEncoder(&pixelRefOffset, subset)); |
| 601 if (data.get() && data->size() < get_uncompressed_size(fBitmap, | 629 if (data.get() && data->size() < get_uncompressed_size(fBitmap, |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 716 if (SkIRect::MakeWH(bitmap.width(), bitmap.height()) == subset) { | 744 if (SkIRect::MakeWH(bitmap.width(), bitmap.height()) == subset) { |
| 717 SkAutoTUnref<SkData> encodedData(ref_encoded_data(bitmap)); | 745 SkAutoTUnref<SkData> encodedData(ref_encoded_data(bitmap)); |
| 718 if (is_jfif_jpeg(encodedData)) { | 746 if (is_jfif_jpeg(encodedData)) { |
| 719 return SkNEW_ARGS(PDFJPEGImage, | 747 return SkNEW_ARGS(PDFJPEGImage, |
| 720 (encodedData, bitmap.width(), bitmap.height())); | 748 (encodedData, bitmap.width(), bitmap.height())); |
| 721 } | 749 } |
| 722 } | 750 } |
| 723 #endif | 751 #endif |
| 724 return SkPDFImage::CreateImage(bitmap, subset, encoder); | 752 return SkPDFImage::CreateImage(bitmap, subset, encoder); |
| 725 } | 753 } |
| OLD | NEW |