OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "SkColorPriv.h" | 8 #include "SkColorPriv.h" |
9 #include "SkFlate.h" | 9 #include "SkFlate.h" |
10 #include "SkPDFBitmap.h" | 10 #include "SkPDFBitmap.h" |
11 #include "SkPDFCanon.h" | 11 #include "SkPDFCanon.h" |
12 #include "SkPDFCatalog.h" | 12 #include "SkPDFCatalog.h" |
13 #include "SkStream.h" | 13 #include "SkStream.h" |
14 #include "SkUnPreMultiply.h" | 14 #include "SkUnPreMultiply.h" |
15 | 15 |
16 //////////////////////////////////////////////////////////////////////////////// | 16 //////////////////////////////////////////////////////////////////////////////// |
17 | 17 |
18 static void pdf_stream_begin(SkWStream* stream) { | 18 static void pdf_stream_begin(SkWStream* stream) { |
19 static const char streamBegin[] = " stream\n"; | 19 static const char streamBegin[] = " stream\n"; |
20 stream->write(streamBegin, strlen(streamBegin)); | 20 stream->write(streamBegin, strlen(streamBegin)); |
21 } | 21 } |
22 | 22 |
23 static void pdf_stream_end(SkWStream* stream) { | 23 static void pdf_stream_end(SkWStream* stream) { |
24 static const char streamEnd[] = "\nendstream"; | 24 static const char streamEnd[] = "\nendstream"; |
25 stream->write(streamEnd, strlen(streamEnd)); | 25 stream->write(streamEnd, strlen(streamEnd)); |
26 } | 26 } |
27 | 27 |
28 static size_t pixel_count(const SkBitmap& bm) { | 28 //////////////////////////////////////////////////////////////////////////////// |
29 return SkToSizeT(bm.width()) * SkToSizeT(bm.height()); | |
30 } | |
31 | 29 |
32 // write a single byte to a stream n times. | 30 // write a single byte to a stream n times. |
33 static void fill_stream(SkWStream* out, char value, size_t n) { | 31 static void fill_stream(SkWStream* out, char value, size_t n) { |
34 char buffer[4096]; | 32 char buffer[4096]; |
35 memset(buffer, value, sizeof(buffer)); | 33 memset(buffer, value, sizeof(buffer)); |
36 while (n) { | 34 for (size_t i = 0; i < n / sizeof(buffer); ++i) { |
37 size_t k = SkTMin(n, sizeof(buffer)); | 35 out->write(buffer, sizeof(buffer)); |
38 out->write(buffer, k); | 36 } |
39 n -= k; | 37 out->write(buffer, n % sizeof(buffer)); |
40 } | 38 } |
41 } | 39 |
42 | 40 // unpremultiply and extract R, G, B components. |
43 static SkPMColor get_pmcolor_neighbor_avg_color(const SkBitmap& bitmap, | 41 static void pmcolor_to_rgb24(SkPMColor pmColor, uint8_t* rgb) { |
44 int xOrig, | 42 uint32_t s = SkUnPreMultiply::GetScale(SkGetPackedA32(pmColor)); |
45 int yOrig) { | 43 rgb[0] = SkUnPreMultiply::ApplyScale(s, SkGetPackedR32(pmColor)); |
46 SkASSERT(kN32_SkColorType == bitmap.colorType()); | 44 rgb[1] = SkUnPreMultiply::ApplyScale(s, SkGetPackedG32(pmColor)); |
47 SkASSERT(bitmap.getPixels()); | 45 rgb[2] = SkUnPreMultiply::ApplyScale(s, SkGetPackedB32(pmColor)); |
48 uint8_t count = 0; | 46 } |
49 unsigned r = 0; | 47 |
50 unsigned g = 0; | 48 /* It is necessary to average the color component of transparent |
51 unsigned b = 0; | 49 pixels with their surrounding neighbors since the PDF renderer may |
52 for (int y = yOrig - 1; y <= yOrig + 1; ++y) { | 50 separately re-sample the alpha and color channels when the image is |
53 if (y < 0 || y >= bitmap.height()) { | 51 not displayed at its native resolution. Since an alpha of zero |
54 continue; | 52 gives no information about the color component, the pathological |
55 } | 53 case is a white image with sharp transparency bounds - the color |
56 uint32_t* src = bitmap.getAddr32(0, y); | 54 channel goes to black, and the should-be-transparent pixels are |
57 for (int x = xOrig - 1; x <= xOrig + 1; ++x) { | 55 rendered as grey because of the separate soft mask and color |
58 if (x < 0 || x >= bitmap.width()) { | 56 resizing. e.g.: gm/bitmappremul.cpp */ |
59 continue; | 57 static void get_neighbor_avg_color(const SkBitmap& bm, |
60 } | 58 int xOrig, |
61 SkPMColor pmColor = src[x]; | 59 int yOrig, |
62 U8CPU alpha = SkGetPackedA32(pmColor); | 60 uint8_t rgb[3]) { |
63 if (alpha != SK_AlphaTRANSPARENT) { | 61 SkASSERT(kN32_SkColorType == bm.colorType()); |
64 uint32_t s = SkUnPreMultiply::GetScale(alpha); | 62 unsigned a = 0, r = 0, g = 0, b = 0; |
65 r += SkUnPreMultiply::ApplyScale(s, SkGetPackedR32(pmColor)); | 63 // Clamp the range to the edge of the bitmap. |
66 g += SkUnPreMultiply::ApplyScale(s, SkGetPackedG32(pmColor)); | 64 int ymin = SkTMax(0, yOrig - 1); |
67 b += SkUnPreMultiply::ApplyScale(s, SkGetPackedB32(pmColor)); | 65 int ymax = SkTMin(yOrig + 1, bm.height() - 1); |
68 ++count; | 66 int xmin = SkTMax(0, xOrig - 1); |
69 } | 67 int xmax = SkTMin(xOrig + 1, bm.width() - 1); |
70 } | 68 for (int y = ymin; y <= ymax; ++y) { |
71 } | 69 SkPMColor* scanline = bm.getAddr32(0, y); |
72 if (count == 0) { | 70 for (int x = xmin; x <= xmax; ++x) { |
73 return SkPackARGB32NoCheck(SK_AlphaOPAQUE, 0, 0, 0); | 71 SkPMColor pmColor = scanline[x]; |
72 a += SkGetPackedA32(pmColor); | |
73 r += SkGetPackedR32(pmColor); | |
74 g += SkGetPackedG32(pmColor); | |
75 b += SkGetPackedB32(pmColor); | |
76 } | |
77 } | |
78 if (a > 0) { | |
79 rgb[0] = SkToU8(255 * r / a); | |
80 rgb[1] = SkToU8(255 * g / a); | |
81 rgb[2] = SkToU8(255 * b / a); | |
74 } else { | 82 } else { |
75 return SkPackARGB32NoCheck( | 83 rgb[0] = rgb[1] = rgb[2] = 0; |
76 SK_AlphaOPAQUE, r / count, g / count, b / count); | 84 } |
77 } | 85 } |
78 } | 86 |
79 | 87 static size_t pixel_count(const SkBitmap& bm) { |
80 static void pmcolor_to_rgb24(const SkBitmap& bm, SkWStream* out) { | 88 return SkToSizeT(bm.width()) * SkToSizeT(bm.height()); |
81 SkASSERT(kN32_SkColorType == bm.colorType()); | 89 } |
82 if (!bm.getPixels()) { | 90 |
83 fill_stream(out, '\xFF', 3 * pixel_count(bm)); | 91 static const SkBitmap& not4444(const SkBitmap& input, SkBitmap* copy) { |
92 if (input.colorType() != kARGB_4444_SkColorType) { | |
93 return input; | |
94 } | |
95 // ARGB_4444 is rarely used, so we can do a wasteful tmp copy. | |
96 SkAssertResult(input.copyTo(copy, kN32_SkColorType)); | |
97 copy->setImmutable(); | |
98 return *copy; | |
99 } | |
100 | |
101 // Returns number of expected components in PDF pixels. | |
102 static size_t pdf_pixel_width(SkColorType ct) { | |
reed1
2015/03/20 14:15:49
nit: your comment is very clear, and also a bit di
hal.canary
2015/03/20 15:26:16
Done.
| |
103 switch (ct) { | |
104 case kN32_SkColorType: | |
105 case kRGB_565_SkColorType: | |
106 case kARGB_4444_SkColorType: | |
107 return 3; | |
108 case kAlpha_8_SkColorType: | |
109 case kIndex_8_SkColorType: | |
110 case kGray_8_SkColorType: | |
111 return 1; | |
112 case kUnknown_SkColorType: | |
113 default: | |
114 SkASSERT(false); | |
115 return 0; | |
116 } | |
117 } | |
118 | |
119 static void bitmap_to_pdf_pixels(const SkBitmap& bitmap, SkWStream* out) { | |
120 if (!bitmap.getPixels()) { | |
121 size_t size = pixel_count(bitmap) * pdf_pixel_width(bitmap.colorType()); | |
122 fill_stream(out, '\x00', size); | |
84 return; | 123 return; |
85 } | 124 } |
86 size_t scanlineLength = 3 * bm.width(); | 125 SkBitmap copy; |
87 SkAutoTMalloc<uint8_t> scanline(scanlineLength); | 126 const SkBitmap& bm = not4444(bitmap, ©); |
88 for (int y = 0; y < bm.height(); ++y) { | 127 SkAutoLockPixels autoLockPixels(bm); |
89 uint8_t* dst = scanline.get(); | 128 switch (bm.colorType()) { |
90 const SkPMColor* src = bm.getAddr32(0, y); | 129 case kN32_SkColorType: { |
91 for (int x = 0; x < bm.width(); ++x) { | 130 SkASSERT(3 == pdf_pixel_width(bitmap.colorType())); |
92 SkPMColor color = *src++; | 131 SkAutoTMalloc<uint8_t> scanline(3 * bm.width()); |
93 U8CPU alpha = SkGetPackedA32(color); | 132 for (int y = 0; y < bm.height(); ++y) { |
94 if (alpha != SK_AlphaTRANSPARENT) { | 133 const SkPMColor* src = bm.getAddr32(0, y); |
95 uint32_t s = SkUnPreMultiply::GetScale(alpha); | 134 uint8_t* dst = scanline.get(); |
96 *dst++ = SkUnPreMultiply::ApplyScale(s, SkGetPackedR32(color)); | 135 for (int x = 0; x < bm.width(); ++x) { |
97 *dst++ = SkUnPreMultiply::ApplyScale(s, SkGetPackedG32(color)); | 136 SkPMColor color = *src++; |
98 *dst++ = SkUnPreMultiply::ApplyScale(s, SkGetPackedB32(color)); | 137 U8CPU alpha = SkGetPackedA32(color); |
99 } else { | 138 if (alpha != SK_AlphaTRANSPARENT) { |
100 /* It is necessary to average the color component of | 139 pmcolor_to_rgb24(color, dst); |
101 transparent pixels with their surrounding neighbors | 140 } else { |
102 since the PDF renderer may separately re-sample the | 141 get_neighbor_avg_color(bm, x, y, dst); |
103 alpha and color channels when the image is not | 142 } |
104 displayed at its native resolution. Since an alpha | 143 dst += 3; |
105 of zero gives no information about the color | 144 } |
106 component, the pathological case is a white image | 145 out->write(scanline.get(), 3 * bm.width()); |
107 with sharp transparency bounds - the color channel | 146 } |
108 goes to black, and the should-be-transparent pixels | 147 return; |
109 are rendered as grey because of the separate soft | 148 } |
110 mask and color resizing. e.g.: gm/bitmappremul.cpp */ | 149 case kRGB_565_SkColorType: { |
111 color = get_pmcolor_neighbor_avg_color(bm, x, y); | 150 SkASSERT(3 == pdf_pixel_width(bitmap.colorType())); |
112 *dst++ = SkGetPackedR32(color); | 151 SkAutoTMalloc<uint8_t> scanline(3 * bm.width()); |
113 *dst++ = SkGetPackedG32(color); | 152 for (int y = 0; y < bm.height(); ++y) { |
114 *dst++ = SkGetPackedB32(color); | 153 const uint16_t* src = bm.getAddr16(0, y); |
115 } | 154 uint8_t* dst = scanline.get(); |
116 } | 155 for (int x = 0; x < bm.width(); ++x) { |
117 out->write(scanline.get(), scanlineLength); | 156 U16CPU color565 = *src++; |
118 } | 157 *dst++ = SkPacked16ToR32(color565); |
119 } | 158 *dst++ = SkPacked16ToG32(color565); |
120 | 159 *dst++ = SkPacked16ToB32(color565); |
121 static void pmcolor_alpha_to_a8(const SkBitmap& bm, SkWStream* out) { | 160 } |
122 SkASSERT(kN32_SkColorType == bm.colorType()); | 161 out->write(scanline.get(), 3 * bm.width()); |
123 if (!bm.getPixels()) { | 162 } |
124 fill_stream(out, '\xFF', pixel_count(bm)); | 163 return; |
164 } | |
165 case kAlpha_8_SkColorType: | |
166 SkASSERT(1 == pdf_pixel_width(bitmap.colorType())); | |
167 fill_stream(out, '\x00', pixel_count(bm)); | |
168 return; | |
169 case kGray_8_SkColorType: | |
170 case kIndex_8_SkColorType: | |
171 SkASSERT(1 == pdf_pixel_width(bitmap.colorType())); | |
172 // these two formats need no transformation to serialize. | |
173 for (int y = 0; y < bm.height(); ++y) { | |
174 out->write(bm.getAddr8(0, y), bm.width()); | |
175 } | |
176 return; | |
177 case kUnknown_SkColorType: | |
178 case kARGB_4444_SkColorType: | |
179 default: | |
180 SkASSERT(false); | |
181 } | |
182 } | |
183 | |
184 //////////////////////////////////////////////////////////////////////////////// | |
185 | |
186 static void bitmap_alpha_to_a8(const SkBitmap& bitmap, SkWStream* out) { | |
187 if (!bitmap.getPixels()) { | |
188 fill_stream(out, '\xFF', pixel_count(bitmap)); | |
125 return; | 189 return; |
126 } | 190 } |
127 size_t scanlineLength = bm.width(); | 191 SkBitmap copy; |
128 SkAutoTMalloc<uint8_t> scanline(scanlineLength); | 192 const SkBitmap& bm = not4444(bitmap, ©); |
129 for (int y = 0; y < bm.height(); ++y) { | 193 SkAutoLockPixels autoLockPixels(bm); |
130 uint8_t* dst = scanline.get(); | 194 switch (bm.colorType()) { |
131 const SkPMColor* src = bm.getAddr32(0, y); | 195 case kN32_SkColorType: { |
132 for (int x = 0; x < bm.width(); ++x) { | 196 SkAutoTMalloc<uint8_t> scanline(bm.width()); |
133 *dst++ = SkGetPackedA32(*src++); | 197 for (int y = 0; y < bm.height(); ++y) { |
134 } | 198 uint8_t* dst = scanline.get(); |
135 out->write(scanline.get(), scanlineLength); | 199 const SkPMColor* src = bm.getAddr32(0, y); |
136 } | 200 for (int x = 0; x < bm.width(); ++x) { |
137 } | 201 *dst++ = SkGetPackedA32(*src++); |
138 | 202 } |
203 out->write(scanline.get(), bm.width()); | |
204 } | |
205 return; | |
206 } | |
207 case kAlpha_8_SkColorType: | |
208 for (int y = 0; y < bm.height(); ++y) { | |
209 out->write(bm.getAddr8(0, y), bm.width()); | |
210 } | |
211 return; | |
212 case kIndex_8_SkColorType: { | |
213 SkColorTable* ct = bm.getColorTable(); | |
214 SkASSERT(ct); | |
215 SkAutoTMalloc<uint8_t> scanline(bm.width()); | |
216 for (int y = 0; y < bm.height(); ++y) { | |
217 uint8_t* dst = scanline.get(); | |
218 const uint8_t* src = bm.getAddr8(0, y); | |
219 for (int x = 0; x < bm.width(); ++x) { | |
220 *dst++ = SkGetPackedA32((*ct)[*src++]); | |
221 } | |
222 out->write(scanline.get(), bm.width()); | |
223 } | |
224 return; | |
225 } | |
226 case kRGB_565_SkColorType: // no alpha | |
227 case kARGB_4444_SkColorType: // converted to N32 | |
228 case kGray_8_SkColorType: // no alpha | |
229 case kUnknown_SkColorType: // unsupported | |
230 default: | |
231 SkASSERT(false); | |
232 } | |
233 } | |
234 | |
139 //////////////////////////////////////////////////////////////////////////////// | 235 //////////////////////////////////////////////////////////////////////////////// |
140 | 236 |
141 namespace { | 237 namespace { |
142 // This SkPDFObject only outputs the alpha layer of the given bitmap. | 238 // This SkPDFObject only outputs the alpha layer of the given bitmap. |
143 class PDFAlphaBitmap : public SkPDFObject { | 239 class PDFAlphaBitmap : public SkPDFObject { |
144 public: | 240 public: |
145 PDFAlphaBitmap(const SkBitmap& bm) : fBitmap(bm) {} | 241 PDFAlphaBitmap(const SkBitmap& bm) : fBitmap(bm) {} |
146 ~PDFAlphaBitmap() {} | 242 ~PDFAlphaBitmap() {} |
147 void emitObject(SkWStream*, SkPDFCatalog*) SK_OVERRIDE; | 243 void emitObject(SkWStream*, SkPDFCatalog*) SK_OVERRIDE; |
148 void addResources(SkTSet<SkPDFObject*>*, SkPDFCatalog*) const SK_OVERRIDE {} | |
149 | 244 |
150 private: | 245 private: |
151 const SkBitmap fBitmap; | 246 const SkBitmap fBitmap; |
152 void emitDict(SkWStream*, SkPDFCatalog*, size_t, bool) const; | 247 void emitDict(SkWStream*, SkPDFCatalog*, size_t) const; |
153 }; | 248 }; |
154 | 249 |
155 void PDFAlphaBitmap::emitObject(SkWStream* stream, SkPDFCatalog* catalog) { | 250 void PDFAlphaBitmap::emitObject(SkWStream* stream, SkPDFCatalog* catalog) { |
156 SkAutoLockPixels autoLockPixels(fBitmap); | 251 SkAutoLockPixels autoLockPixels(fBitmap); |
252 SkASSERT(fBitmap.colorType() != kIndex_8_SkColorType || | |
253 fBitmap.getColorTable()); | |
157 | 254 |
158 #ifndef SK_NO_FLATE | |
159 // Write to a temporary buffer to get the compressed length. | 255 // Write to a temporary buffer to get the compressed length. |
160 SkDynamicMemoryWStream buffer; | 256 SkDynamicMemoryWStream buffer; |
161 SkDeflateWStream deflateWStream(&buffer); | 257 SkDeflateWStream deflateWStream(&buffer); |
162 pmcolor_alpha_to_a8(fBitmap, &deflateWStream); | 258 bitmap_alpha_to_a8(fBitmap, &deflateWStream); |
163 deflateWStream.finalize(); // call before detachAsStream(). | 259 deflateWStream.finalize(); // call before detachAsStream(). |
164 SkAutoTDelete<SkStreamAsset> asset(buffer.detachAsStream()); | 260 SkAutoTDelete<SkStreamAsset> asset(buffer.detachAsStream()); |
165 | 261 |
166 this->emitDict(stream, catalog, asset->getLength(), /*deflate=*/true); | 262 this->emitDict(stream, catalog, asset->getLength()); |
167 pdf_stream_begin(stream); | 263 pdf_stream_begin(stream); |
168 stream->writeStream(asset.get(), asset->getLength()); | 264 stream->writeStream(asset.get(), asset->getLength()); |
169 pdf_stream_end(stream); | 265 pdf_stream_end(stream); |
170 #else | |
171 this->emitDict(stream, catalog, pixel_count(fBitmap), /*deflate=*/false); | |
172 pdf_stream_begin(stream); | |
173 pmcolor_alpha_to_a8(fBitmap, stream); | |
174 pdf_stream_end(stream); | |
175 #endif // SK_NO_FLATE | |
176 } | 266 } |
177 | 267 |
178 void PDFAlphaBitmap::emitDict(SkWStream* stream, | 268 void PDFAlphaBitmap::emitDict(SkWStream* stream, |
179 SkPDFCatalog* catalog, | 269 SkPDFCatalog* catalog, |
180 size_t length, | 270 size_t length) const { |
181 bool deflate) const { | |
182 SkPDFDict pdfDict("XObject"); | 271 SkPDFDict pdfDict("XObject"); |
183 pdfDict.insertName("Subtype", "Image"); | 272 pdfDict.insertName("Subtype", "Image"); |
184 pdfDict.insertInt("Width", fBitmap.width()); | 273 pdfDict.insertInt("Width", fBitmap.width()); |
185 pdfDict.insertInt("Height", fBitmap.height()); | 274 pdfDict.insertInt("Height", fBitmap.height()); |
186 pdfDict.insertName("ColorSpace", "DeviceGray"); | 275 pdfDict.insertName("ColorSpace", "DeviceGray"); |
187 pdfDict.insertInt("BitsPerComponent", 8); | 276 pdfDict.insertInt("BitsPerComponent", 8); |
188 if (deflate) { | 277 pdfDict.insertName("Filter", "FlateDecode"); |
189 pdfDict.insertName("Filter", "FlateDecode"); | |
190 } | |
191 pdfDict.insertInt("Length", length); | 278 pdfDict.insertInt("Length", length); |
192 pdfDict.emitObject(stream, catalog); | 279 pdfDict.emitObject(stream, catalog); |
193 } | 280 } |
194 } // namespace | 281 } // namespace |
195 | 282 |
196 //////////////////////////////////////////////////////////////////////////////// | 283 //////////////////////////////////////////////////////////////////////////////// |
197 | 284 |
198 void SkPDFBitmap::addResources(SkTSet<SkPDFObject*>* resourceSet, | 285 void SkPDFBitmap::addResources(SkTSet<SkPDFObject*>* resourceSet, |
199 SkPDFCatalog* catalog) const { | 286 SkPDFCatalog* catalog) const { |
200 if (fSMask.get()) { | 287 if (fSMask.get()) { |
201 resourceSet->add(fSMask.get()); | 288 if (resourceSet->add(fSMask.get())) { |
289 fSMask->addResources(resourceSet, catalog); | |
290 } | |
202 } | 291 } |
203 } | 292 } |
204 | 293 |
205 void SkPDFBitmap::emitObject(SkWStream* stream, SkPDFCatalog* catalog) { | 294 void SkPDFBitmap::emitObject(SkWStream* stream, SkPDFCatalog* catalog) { |
206 SkAutoLockPixels autoLockPixels(fBitmap); | 295 SkAutoLockPixels autoLockPixels(fBitmap); |
296 SkASSERT(fBitmap.colorType() != kIndex_8_SkColorType || | |
297 fBitmap.getColorTable()); | |
207 | 298 |
208 #ifndef SK_NO_FLATE | |
209 // Write to a temporary buffer to get the compressed length. | 299 // Write to a temporary buffer to get the compressed length. |
210 SkDynamicMemoryWStream buffer; | 300 SkDynamicMemoryWStream buffer; |
211 SkDeflateWStream deflateWStream(&buffer); | 301 SkDeflateWStream deflateWStream(&buffer); |
212 pmcolor_to_rgb24(fBitmap, &deflateWStream); | 302 bitmap_to_pdf_pixels(fBitmap, &deflateWStream); |
213 deflateWStream.finalize(); // call before detachAsStream(). | 303 deflateWStream.finalize(); // call before detachAsStream(). |
214 SkAutoTDelete<SkStreamAsset> asset(buffer.detachAsStream()); | 304 SkAutoTDelete<SkStreamAsset> asset(buffer.detachAsStream()); |
215 | 305 |
216 this->emitDict(stream, catalog, asset->getLength(), /*deflate=*/true); | 306 this->emitDict(stream, catalog, asset->getLength()); |
217 pdf_stream_begin(stream); | 307 pdf_stream_begin(stream); |
218 stream->writeStream(asset.get(), asset->getLength()); | 308 stream->writeStream(asset.get(), asset->getLength()); |
219 pdf_stream_end(stream); | 309 pdf_stream_end(stream); |
220 #else | 310 } |
221 this->emitDict(stream, catalog, 3 * pixel_count(fBitmap), /*deflate=*/false) ; | 311 |
222 pdf_stream_begin(stream); | 312 static SkPDFArray* make_indexed_color_space(const SkColorTable* table) { |
223 pmcolor_to_rgb24(fBitmap, stream); | 313 SkPDFArray* result = SkNEW(SkPDFArray); |
224 pdf_stream_end(stream); | 314 result->reserve(4); |
225 return; | 315 result->appendName("Indexed"); |
226 #endif // SK_NO_FLATE | 316 result->appendName("DeviceRGB"); |
317 SkASSERT(table); | |
318 result->appendInt(table->count() - 1); | |
reed1
2015/03/20 14:15:49
can a colortable have 0 entries?
hal.canary
2015/03/20 15:26:16
Apparently so. I don't know if that is ever valid
| |
319 | |
320 // Potentially, this could be represented in fewer bytes with a stream. | |
321 // Max size as a string is 1.5k. | |
322 char tableArray[768]; | |
reed1
2015/03/20 14:15:49
tableArray[3 * 256] or some other way to document
hal.canary
2015/03/20 15:26:17
Done.
| |
323 SkASSERT(3u * table->count() <= sizeof(tableArray)); | |
reed1
2015/03/20 14:15:49
I know its bytes, but I think the assert would be
hal.canary
2015/03/20 15:26:17
Done.
| |
324 uint8_t* tablePtr = reinterpret_cast<uint8_t*>(tableArray); | |
325 const SkPMColor* colors = table->readColors(); | |
326 for (int i = 0; i < table->count(); i++) { | |
327 pmcolor_to_rgb24(colors[i], tablePtr); | |
328 tablePtr += 3; | |
329 } | |
330 SkString tableString(tableArray, 3 * table->count()); | |
331 result->append(new SkPDFString(tableString))->unref(); | |
332 return result; | |
227 } | 333 } |
228 | 334 |
229 void SkPDFBitmap::emitDict(SkWStream* stream, | 335 void SkPDFBitmap::emitDict(SkWStream* stream, |
230 SkPDFCatalog* catalog, | 336 SkPDFCatalog* catalog, |
231 size_t length, | 337 size_t length) const { |
232 bool deflate) const { | |
233 SkPDFDict pdfDict("XObject"); | 338 SkPDFDict pdfDict("XObject"); |
234 pdfDict.insertName("Subtype", "Image"); | 339 pdfDict.insertName("Subtype", "Image"); |
235 pdfDict.insertInt("Width", fBitmap.width()); | 340 pdfDict.insertInt("Width", fBitmap.width()); |
236 pdfDict.insertInt("Height", fBitmap.height()); | 341 pdfDict.insertInt("Height", fBitmap.height()); |
237 pdfDict.insertName("ColorSpace", "DeviceRGB"); | 342 if (fBitmap.colorType() == kIndex_8_SkColorType) { |
343 SkASSERT(1 == pdf_pixel_width(fBitmap.colorType())); | |
344 pdfDict.insert("ColorSpace", make_indexed_color_space( | |
345 fBitmap.getColorTable()))->unref(); | |
346 } else if (1 == pdf_pixel_width(fBitmap.colorType())) { | |
347 pdfDict.insertName("ColorSpace", "DeviceGray"); | |
348 } else { | |
349 pdfDict.insertName("ColorSpace", "DeviceRGB"); | |
350 } | |
238 pdfDict.insertInt("BitsPerComponent", 8); | 351 pdfDict.insertInt("BitsPerComponent", 8); |
239 if (fSMask) { | 352 if (fSMask) { |
240 pdfDict.insert("SMask", new SkPDFObjRef(fSMask))->unref(); | 353 pdfDict.insert("SMask", new SkPDFObjRef(fSMask))->unref(); |
241 } | 354 } |
242 if (deflate) { | 355 pdfDict.insertName("Filter", "FlateDecode"); |
243 pdfDict.insertName("Filter", "FlateDecode"); | |
244 } | |
245 pdfDict.insertInt("Length", length); | 356 pdfDict.insertInt("Length", length); |
246 pdfDict.emitObject(stream, catalog); | 357 pdfDict.emitObject(stream, catalog); |
247 } | 358 } |
248 | 359 |
249 SkPDFBitmap::SkPDFBitmap(const SkBitmap& bm, | 360 SkPDFBitmap::SkPDFBitmap(const SkBitmap& bm, |
250 SkPDFObject* smask) | 361 SkPDFObject* smask) |
251 : fBitmap(bm), fSMask(smask) {} | 362 : fBitmap(bm), fSMask(smask) {} |
252 | 363 |
253 SkPDFBitmap::~SkPDFBitmap() {} | 364 SkPDFBitmap::~SkPDFBitmap() {} |
254 | 365 |
255 //////////////////////////////////////////////////////////////////////////////// | 366 //////////////////////////////////////////////////////////////////////////////// |
256 static bool is_transparent(const SkBitmap& bm) { | 367 |
257 SkAutoLockPixels autoLockPixels(bm); | 368 static const SkBitmap& immutable_bitmap(const SkBitmap& bm, SkBitmap* copy) { |
258 if (NULL == bm.getPixels()) { | 369 if (bm.isImmutable()) { |
259 return true; | 370 return bm; |
260 } | 371 } |
261 SkASSERT(kN32_SkColorType == bm.colorType()); | 372 bm.copyTo(copy); |
262 for (int y = 0; y < bm.height(); ++y) { | 373 copy->setImmutable(); |
263 U8CPU alpha = 0; | 374 return *copy; |
264 const SkPMColor* src = bm.getAddr32(0, y); | |
265 for (int x = 0; x < bm.width(); ++x) { | |
266 alpha |= SkGetPackedA32(*src++); | |
267 } | |
268 if (alpha) { | |
269 return false; | |
270 } | |
271 } | |
272 return true; | |
273 } | 375 } |
274 | 376 |
275 SkPDFBitmap* SkPDFBitmap::Create(SkPDFCanon* canon, | 377 SkPDFBitmap* SkPDFBitmap::Create(SkPDFCanon* canon, const SkBitmap& bitmap) { |
276 const SkBitmap& bitmap, | |
277 const SkIRect& subset) { | |
278 SkASSERT(canon); | 378 SkASSERT(canon); |
279 if (kN32_SkColorType != bitmap.colorType()) { | 379 if (!SkColorTypeIsValid(bitmap.colorType()) || |
280 // TODO(halcanary): support other colortypes. | 380 kUnknown_SkColorType == bitmap.colorType()) { |
281 return NULL; | 381 return NULL; |
282 } | 382 } |
283 SkBitmap bm; | 383 SkBitmap copy; |
284 // Should extractSubset be done by the SkPDFDevice? | 384 const SkBitmap& bm = immutable_bitmap(bitmap, ©); |
285 if (!bitmap.extractSubset(&bm, subset)) { | |
286 return NULL; | |
287 } | |
288 if (bm.drawsNothing()) { | 385 if (bm.drawsNothing()) { |
289 return NULL; | 386 return NULL; |
290 } | 387 } |
291 if (!bm.isImmutable()) { | 388 if (SkPDFBitmap* canonBitmap = canon->findBitmap(bm)) { |
292 SkBitmap copy; | 389 return SkRef(canonBitmap); |
293 if (!bm.copyTo(©)) { | |
294 return NULL; | |
295 } | |
296 copy.setImmutable(); | |
297 bm = copy; | |
298 } | |
299 | |
300 SkPDFBitmap* pdfBitmap = canon->findBitmap(bm); | |
301 if (pdfBitmap) { | |
302 return SkRef(pdfBitmap); | |
303 } | 390 } |
304 SkPDFObject* smask = NULL; | 391 SkPDFObject* smask = NULL; |
305 if (!bm.isOpaque() && !SkBitmap::ComputeIsOpaque(bm)) { | 392 if (!bm.isOpaque() && !SkBitmap::ComputeIsOpaque(bm)) { |
306 if (is_transparent(bm)) { | |
307 return NULL; | |
308 } | |
309 // PDFAlphaBitmaps do not get directly canonicalized (they | |
310 // are refed by the SkPDFBitmap). | |
311 smask = SkNEW_ARGS(PDFAlphaBitmap, (bm)); | 393 smask = SkNEW_ARGS(PDFAlphaBitmap, (bm)); |
312 } | 394 } |
313 pdfBitmap = SkNEW_ARGS(SkPDFBitmap, (bm, smask)); | 395 SkPDFBitmap* pdfBitmap = SkNEW_ARGS(SkPDFBitmap, (bm, smask)); |
314 canon->addBitmap(pdfBitmap); | 396 canon->addBitmap(pdfBitmap); |
315 return pdfBitmap; | 397 return pdfBitmap; |
316 } | 398 } |
OLD | NEW |