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 "SkDeflateWStream.h" | 9 #include "SkDeflateWStream.h" |
10 #include "SkPDFBitmap.h" | 10 #include "SkPDFBitmap.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 | 23 |
24 static void pdf_stream_end(SkWStream* stream) { | 24 static void pdf_stream_end(SkWStream* stream) { |
25 static const char streamEnd[] = "\nendstream"; | 25 static const char streamEnd[] = "\nendstream"; |
26 stream->write(streamEnd, strlen(streamEnd)); | 26 stream->write(streamEnd, strlen(streamEnd)); |
27 } | 27 } |
28 | 28 |
29 static size_t pixel_count(const SkBitmap& bm) { | 29 static size_t pixel_count(const SkBitmap& bm) { |
30 return SkToSizeT(bm.width()) * SkToSizeT(bm.height()); | 30 return SkToSizeT(bm.width()) * SkToSizeT(bm.height()); |
31 } | 31 } |
32 | 32 |
33 static bool skip_compression(SkPDFDocument::Flags flag) { | |
34 #ifndef SK_NO_FLATE | |
35 return SkToBool(flag & SkPDFDocument::kFavorSpeedOverSize_Flags); | |
36 #else | |
37 return true; | |
38 #endif // SK_NO_FLATE | |
39 } | |
40 | |
41 // write a single byte to a stream n times. | 33 // write a single byte to a stream n times. |
42 static void fill_stream(SkWStream* out, char value, size_t n) { | 34 static void fill_stream(SkWStream* out, char value, size_t n) { |
43 char buffer[4096]; | 35 char buffer[4096]; |
44 memset(buffer, value, sizeof(buffer)); | 36 memset(buffer, value, sizeof(buffer)); |
45 while (n) { | 37 while (n) { |
46 size_t k = SkTMin(n, sizeof(buffer)); | 38 size_t k = SkTMin(n, sizeof(buffer)); |
47 out->write(buffer, k); | 39 out->write(buffer, k); |
48 n -= k; | 40 n -= k; |
49 } | 41 } |
50 } | 42 } |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 void addResources(SkTSet<SkPDFObject*>*, SkPDFCatalog*) const SK_OVERRIDE {} | 149 void addResources(SkTSet<SkPDFObject*>*, SkPDFCatalog*) const SK_OVERRIDE {} |
158 | 150 |
159 private: | 151 private: |
160 const SkBitmap fBitmap; | 152 const SkBitmap fBitmap; |
161 void emitDict(SkWStream*, SkPDFCatalog*, size_t, bool) const; | 153 void emitDict(SkWStream*, SkPDFCatalog*, size_t, bool) const; |
162 }; | 154 }; |
163 | 155 |
164 void PDFAlphaBitmap::emitObject(SkWStream* stream, SkPDFCatalog* catalog) { | 156 void PDFAlphaBitmap::emitObject(SkWStream* stream, SkPDFCatalog* catalog) { |
165 SkAutoLockPixels autoLockPixels(fBitmap); | 157 SkAutoLockPixels autoLockPixels(fBitmap); |
166 | 158 |
167 if (skip_compression(catalog->getDocumentFlags())) { | |
168 this->emitDict(stream, catalog, pixel_count(fBitmap), | |
169 /*deflate=*/false); | |
170 pdf_stream_begin(stream); | |
171 pmcolor_alpha_to_a8(fBitmap, stream); | |
172 pdf_stream_end(stream); | |
173 return; | |
174 } | |
175 #ifndef SK_NO_FLATE | 159 #ifndef SK_NO_FLATE |
176 // Write to a temporary buffer to get the compressed length. | 160 // Write to a temporary buffer to get the compressed length. |
177 SkDynamicMemoryWStream buffer; | 161 SkDynamicMemoryWStream buffer; |
178 SkDeflateWStream deflateWStream(&buffer); | 162 SkDeflateWStream deflateWStream(&buffer); |
179 pmcolor_alpha_to_a8(fBitmap, &deflateWStream); | 163 pmcolor_alpha_to_a8(fBitmap, &deflateWStream); |
180 deflateWStream.finalize(); // call before detachAsStream(). | 164 deflateWStream.finalize(); // call before detachAsStream(). |
181 SkAutoTDelete<SkStreamAsset> asset(buffer.detachAsStream()); | 165 SkAutoTDelete<SkStreamAsset> asset(buffer.detachAsStream()); |
182 | 166 |
183 this->emitDict(stream, catalog, asset->getLength(), /*deflate=*/true); | 167 this->emitDict(stream, catalog, asset->getLength(), /*deflate=*/true); |
184 pdf_stream_begin(stream); | 168 pdf_stream_begin(stream); |
185 stream->writeStream(asset.get(), asset->getLength()); | 169 stream->writeStream(asset.get(), asset->getLength()); |
186 pdf_stream_end(stream); | 170 pdf_stream_end(stream); |
| 171 #else |
| 172 this->emitDict(stream, catalog, pixel_count(fBitmap), /*deflate=*/false); |
| 173 pdf_stream_begin(stream); |
| 174 pmcolor_alpha_to_a8(fBitmap, stream); |
| 175 pdf_stream_end(stream); |
187 #endif // SK_NO_FLATE | 176 #endif // SK_NO_FLATE |
188 } | 177 } |
189 | 178 |
190 void PDFAlphaBitmap::emitDict(SkWStream* stream, | 179 void PDFAlphaBitmap::emitDict(SkWStream* stream, |
191 SkPDFCatalog* catalog, | 180 SkPDFCatalog* catalog, |
192 size_t length, | 181 size_t length, |
193 bool deflate) const { | 182 bool deflate) const { |
194 SkPDFDict pdfDict("XObject"); | 183 SkPDFDict pdfDict("XObject"); |
195 pdfDict.insertName("Subtype", "Image"); | 184 pdfDict.insertName("Subtype", "Image"); |
196 pdfDict.insertInt("Width", fBitmap.width()); | 185 pdfDict.insertInt("Width", fBitmap.width()); |
(...skipping 13 matching lines...) Expand all Loading... |
210 void SkPDFBitmap::addResources(SkTSet<SkPDFObject*>* resourceSet, | 199 void SkPDFBitmap::addResources(SkTSet<SkPDFObject*>* resourceSet, |
211 SkPDFCatalog* catalog) const { | 200 SkPDFCatalog* catalog) const { |
212 if (fSMask.get()) { | 201 if (fSMask.get()) { |
213 resourceSet->add(fSMask.get()); | 202 resourceSet->add(fSMask.get()); |
214 } | 203 } |
215 } | 204 } |
216 | 205 |
217 void SkPDFBitmap::emitObject(SkWStream* stream, SkPDFCatalog* catalog) { | 206 void SkPDFBitmap::emitObject(SkWStream* stream, SkPDFCatalog* catalog) { |
218 SkAutoLockPixels autoLockPixels(fBitmap); | 207 SkAutoLockPixels autoLockPixels(fBitmap); |
219 | 208 |
220 if (skip_compression(catalog->getDocumentFlags())) { | |
221 this->emitDict(stream, catalog, 3 * pixel_count(fBitmap), | |
222 /*deflate=*/false); | |
223 pdf_stream_begin(stream); | |
224 pmcolor_to_rgb24(fBitmap, stream); | |
225 pdf_stream_end(stream); | |
226 return; | |
227 } | |
228 #ifndef SK_NO_FLATE | 209 #ifndef SK_NO_FLATE |
229 // Write to a temporary buffer to get the compressed length. | 210 // Write to a temporary buffer to get the compressed length. |
230 SkDynamicMemoryWStream buffer; | 211 SkDynamicMemoryWStream buffer; |
231 SkDeflateWStream deflateWStream(&buffer); | 212 SkDeflateWStream deflateWStream(&buffer); |
232 pmcolor_to_rgb24(fBitmap, &deflateWStream); | 213 pmcolor_to_rgb24(fBitmap, &deflateWStream); |
233 deflateWStream.finalize(); // call before detachAsStream(). | 214 deflateWStream.finalize(); // call before detachAsStream(). |
234 SkAutoTDelete<SkStreamAsset> asset(buffer.detachAsStream()); | 215 SkAutoTDelete<SkStreamAsset> asset(buffer.detachAsStream()); |
235 | 216 |
236 this->emitDict(stream, catalog, asset->getLength(), /*deflate=*/true); | 217 this->emitDict(stream, catalog, asset->getLength(), /*deflate=*/true); |
237 pdf_stream_begin(stream); | 218 pdf_stream_begin(stream); |
238 stream->writeStream(asset.get(), asset->getLength()); | 219 stream->writeStream(asset.get(), asset->getLength()); |
239 pdf_stream_end(stream); | 220 pdf_stream_end(stream); |
| 221 #else |
| 222 this->emitDict(stream, catalog, 3 * pixel_count(fBitmap), /*deflate=*/false)
; |
| 223 pdf_stream_begin(stream); |
| 224 pmcolor_to_rgb24(fBitmap, stream); |
| 225 pdf_stream_end(stream); |
| 226 return; |
240 #endif // SK_NO_FLATE | 227 #endif // SK_NO_FLATE |
241 } | 228 } |
242 | 229 |
243 void SkPDFBitmap::emitDict(SkWStream* stream, | 230 void SkPDFBitmap::emitDict(SkWStream* stream, |
244 SkPDFCatalog* catalog, | 231 SkPDFCatalog* catalog, |
245 size_t length, | 232 size_t length, |
246 bool deflate) const { | 233 bool deflate) const { |
247 SkPDFDict pdfDict("XObject"); | 234 SkPDFDict pdfDict("XObject"); |
248 pdfDict.insertName("Subtype", "Image"); | 235 pdfDict.insertName("Subtype", "Image"); |
249 pdfDict.insertInt("Width", fBitmap.width()); | 236 pdfDict.insertInt("Width", fBitmap.width()); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 return NULL; | 311 return NULL; |
325 } | 312 } |
326 // PDFAlphaBitmaps do not get directly canonicalized (they | 313 // PDFAlphaBitmaps do not get directly canonicalized (they |
327 // are refed by the SkPDFBitmap). | 314 // are refed by the SkPDFBitmap). |
328 smask = SkNEW_ARGS(PDFAlphaBitmap, (bm)); | 315 smask = SkNEW_ARGS(PDFAlphaBitmap, (bm)); |
329 } | 316 } |
330 pdfBitmap = SkNEW_ARGS(SkPDFBitmap, (bm, smask)); | 317 pdfBitmap = SkNEW_ARGS(SkPDFBitmap, (bm, smask)); |
331 canon.addBitmap(pdfBitmap); | 318 canon.addBitmap(pdfBitmap); |
332 return pdfBitmap; | 319 return pdfBitmap; |
333 } | 320 } |
OLD | NEW |