Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: src/pdf/SkPDFImage.cpp

Issue 840653002: SkPDFImage no longer caches a unpremul version of N32 bitmaps. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: simpler CL Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 c = get_argb8888_neighbor_avg_color(bitmap, x, y);
reed1 2015/01/08 18:29:48 // we average the neighbors so that ... (?)
162 dst[2] = SkGetPackedB32(src[x]); 164 *dst++ = SkGetPackedR32(c);
163 dst += 3; 165 *dst++ = SkGetPackedG32(c);
166 *dst++ = SkGetPackedB32(c);
167 } else {
168 SkUnPreMultiply::Scale s = scaleTable[alpha];
169 *dst++ = SkUnPreMultiply::ApplyScale(s, SkGetPackedR32(c));
170 *dst++ = SkUnPreMultiply::ApplyScale(s, SkGetPackedG32(c));
171 *dst++ = SkUnPreMultiply::ApplyScale(s, SkGetPackedB32(c));
172 }
164 } 173 }
165 } 174 }
166 } 175 }
176 SkASSERT(dst == streamSize + (uint8_t*)stream->getMemoryBase());
167 return stream; 177 return stream;
168 } 178 }
169 179
170 static SkStream* extract_a8_alpha(const SkBitmap& bitmap, 180 static SkStream* extract_a8_alpha(const SkBitmap& bitmap,
171 const SkIRect& srcRect, 181 const SkIRect& srcRect,
172 bool* isOpaque, 182 bool* isOpaque,
173 bool* isTransparent) { 183 bool* isTransparent) {
174 const int alphaRowBytes = srcRect.width(); 184 const int alphaRowBytes = srcRect.width();
175 SkStream* stream = SkNEW_ARGS(SkMemoryStream, 185 SkStream* stream = SkNEW_ARGS(SkMemoryStream,
176 (alphaRowBytes * srcRect.height())); 186 (alphaRowBytes * srcRect.height()));
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
457 extract_image_data(bitmap, srcRect, true, &isTransparent)); 467 extract_image_data(bitmap, srcRect, true, &isTransparent));
458 } 468 }
459 if (isTransparent) { 469 if (isTransparent) {
460 return NULL; 470 return NULL;
461 } 471 }
462 472
463 SkPDFImage* image; 473 SkPDFImage* image;
464 SkColorType colorType = bitmap.colorType(); 474 SkColorType colorType = bitmap.colorType();
465 if (alphaData.get() != NULL && (kN32_SkColorType == colorType || 475 if (alphaData.get() != NULL && (kN32_SkColorType == colorType ||
466 kARGB_4444_SkColorType == colorType)) { 476 kARGB_4444_SkColorType == colorType)) {
467 SkBitmap unpremulBitmap = unpremultiply_bitmap(bitmap, srcRect); 477 if (kN32_SkColorType == colorType) {
468 image = SkNEW_ARGS(SkPDFImage, (NULL, unpremulBitmap, false, 478 image = SkNEW_ARGS(SkPDFImage, (NULL, bitmap, false,
469 SkIRect::MakeWH(srcRect.width(), srcRect.height()), 479 SkIRect::MakeWH(srcRect.width(),
470 encoder)); 480 srcRect.height()),
481 encoder));
482 } else {
483 SkBitmap unpremulBitmap = unpremultiply_bitmap(bitmap, srcRect);
484 image = SkNEW_ARGS(SkPDFImage, (NULL, unpremulBitmap, false,
485 SkIRect::MakeWH(srcRect.width(),
486 srcRect.height()),
487 encoder));
488 }
471 } else { 489 } else {
472 image = SkNEW_ARGS(SkPDFImage, (NULL, bitmap, false, srcRect, encoder)); 490 image = SkNEW_ARGS(SkPDFImage, (NULL, bitmap, false, srcRect, encoder));
473 } 491 }
474 if (alphaData.get() != NULL) { 492 if (alphaData.get() != NULL) {
475 SkAutoTUnref<SkPDFImage> mask( 493 SkAutoTUnref<SkPDFImage> mask(
476 SkNEW_ARGS(SkPDFImage, (alphaData.get(), bitmap, 494 SkNEW_ARGS(SkPDFImage, (alphaData.get(), bitmap,
477 true, srcRect, NULL))); 495 true, srcRect, NULL)));
478 image->addSMask(mask); 496 image->addSMask(mask);
479 } 497 }
480 498
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
581 fEncoder(pdfImage.fEncoder), 599 fEncoder(pdfImage.fEncoder),
582 fStreamValid(pdfImage.fStreamValid) { 600 fStreamValid(pdfImage.fStreamValid) {
583 // Nothing to do here - the image params are already copied in SkPDFStream's 601 // Nothing to do here - the image params are already copied in SkPDFStream's
584 // constructor, and the bitmap will be regenerated and encoded in 602 // constructor, and the bitmap will be regenerated and encoded in
585 // populate. 603 // populate.
586 } 604 }
587 605
588 bool SkPDFImage::populate(SkPDFCatalog* catalog) { 606 bool SkPDFImage::populate(SkPDFCatalog* catalog) {
589 if (getState() == kUnused_State) { 607 if (getState() == kUnused_State) {
590 // Initializing image data for the first time. 608 // Initializing image data for the first time.
591 SkDynamicMemoryWStream dctCompressedWStream;
592 if (!skip_compression(catalog) && fEncoder && 609 if (!skip_compression(catalog) && fEncoder &&
593 get_uncompressed_size(fBitmap, fSrcRect) > 1) { 610 get_uncompressed_size(fBitmap, fSrcRect) > 1) {
594 SkBitmap subset; 611 SkBitmap subset;
595 // Extract subset 612 // Extract subset
596 if (!fBitmap.extractSubset(&subset, fSrcRect)) { 613 if (!fBitmap.extractSubset(&subset, fSrcRect)) {
597 return false; 614 return false;
598 } 615 }
599 size_t pixelRefOffset = 0; 616 size_t pixelRefOffset = 0;
600 SkAutoTUnref<SkData> data(fEncoder(&pixelRefOffset, subset)); 617 SkAutoTUnref<SkData> data(fEncoder(&pixelRefOffset, subset));
601 if (data.get() && data->size() < get_uncompressed_size(fBitmap, 618 if (data.get() && data->size() < get_uncompressed_size(fBitmap,
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
716 if (SkIRect::MakeWH(bitmap.width(), bitmap.height()) == subset) { 733 if (SkIRect::MakeWH(bitmap.width(), bitmap.height()) == subset) {
717 SkAutoTUnref<SkData> encodedData(ref_encoded_data(bitmap)); 734 SkAutoTUnref<SkData> encodedData(ref_encoded_data(bitmap));
718 if (is_jfif_jpeg(encodedData)) { 735 if (is_jfif_jpeg(encodedData)) {
719 return SkNEW_ARGS(PDFJPEGImage, 736 return SkNEW_ARGS(PDFJPEGImage,
720 (encodedData, bitmap.width(), bitmap.height())); 737 (encodedData, bitmap.width(), bitmap.height()));
721 } 738 }
722 } 739 }
723 #endif 740 #endif
724 return SkPDFImage::CreateImage(bitmap, subset, encoder); 741 return SkPDFImage::CreateImage(bitmap, subset, encoder);
725 } 742 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698