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 "SkData.h" | 9 #include "SkData.h" |
10 #include "SkDeflate.h" | 10 #include "SkDeflate.h" |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 return; | 290 return; |
291 case kARGB_4444_SkColorType: | 291 case kARGB_4444_SkColorType: |
292 SkDEBUGFAIL("4444 color type should have been converted to N32"); | 292 SkDEBUGFAIL("4444 color type should have been converted to N32"); |
293 return; | 293 return; |
294 case kUnknown_SkColorType: | 294 case kUnknown_SkColorType: |
295 default: | 295 default: |
296 SkDEBUGFAIL("unexpected color type"); | 296 SkDEBUGFAIL("unexpected color type"); |
297 } | 297 } |
298 } | 298 } |
299 | 299 |
300 static sk_sp<SkPDFArray> make_indexed_color_space(const SkColorTable* table) { | 300 static SkPDFArray* make_indexed_color_space(const SkColorTable* table) { |
301 auto result = sk_make_sp<SkPDFArray>(); | 301 SkPDFArray* result = new SkPDFArray; |
302 result->reserve(4); | 302 result->reserve(4); |
303 result->appendName("Indexed"); | 303 result->appendName("Indexed"); |
304 result->appendName("DeviceRGB"); | 304 result->appendName("DeviceRGB"); |
305 SkASSERT(table); | 305 SkASSERT(table); |
306 if (table->count() < 1) { | 306 if (table->count() < 1) { |
307 result->appendInt(0); | 307 result->appendInt(0); |
308 char shortTableArray[3] = {0, 0, 0}; | 308 char shortTableArray[3] = {0, 0, 0}; |
309 SkString tableString(shortTableArray, SK_ARRAY_COUNT(shortTableArray)); | 309 SkString tableString(shortTableArray, SK_ARRAY_COUNT(shortTableArray)); |
310 result->appendString(tableString); | 310 result->appendString(tableString); |
311 return std::move(result); | 311 return result; |
312 } | 312 } |
313 result->appendInt(table->count() - 1); // maximum color index. | 313 result->appendInt(table->count() - 1); // maximum color index. |
314 | 314 |
315 // Potentially, this could be represented in fewer bytes with a stream. | 315 // Potentially, this could be represented in fewer bytes with a stream. |
316 // Max size as a string is 1.5k. | 316 // Max size as a string is 1.5k. |
317 char tableArray[256 * 3]; | 317 char tableArray[256 * 3]; |
318 SkASSERT(3u * table->count() <= SK_ARRAY_COUNT(tableArray)); | 318 SkASSERT(3u * table->count() <= SK_ARRAY_COUNT(tableArray)); |
319 uint8_t* tablePtr = reinterpret_cast<uint8_t*>(tableArray); | 319 uint8_t* tablePtr = reinterpret_cast<uint8_t*>(tableArray); |
320 const SkPMColor* colors = table->readColors(); | 320 const SkPMColor* colors = table->readColors(); |
321 for (int i = 0; i < table->count(); i++) { | 321 for (int i = 0; i < table->count(); i++) { |
322 pmcolor_to_rgb24(colors[i], tablePtr, kN32_SkColorType); | 322 pmcolor_to_rgb24(colors[i], tablePtr, kN32_SkColorType); |
323 tablePtr += 3; | 323 tablePtr += 3; |
324 } | 324 } |
325 SkString tableString(tableArray, 3 * table->count()); | 325 SkString tableString(tableArray, 3 * table->count()); |
326 result->appendString(tableString); | 326 result->appendString(tableString); |
327 return std::move(result); | 327 return result; |
328 } | 328 } |
329 | 329 |
330 static void emit_image_xobject(SkWStream* stream, | 330 static void emit_image_xobject(SkWStream* stream, |
331 const SkImage* image, | 331 const SkImage* image, |
332 bool alpha, | 332 bool alpha, |
333 const sk_sp<SkPDFObject>& smask, | 333 SkPDFObject* smask, |
334 const SkPDFObjNumMap& objNumMap, | 334 const SkPDFObjNumMap& objNumMap, |
335 const SkPDFSubstituteMap& substitutes) { | 335 const SkPDFSubstituteMap& substitutes) { |
336 SkBitmap bitmap; | 336 SkBitmap bitmap; |
337 image_get_ro_pixels(image, &bitmap); // TODO(halcanary): test | 337 image_get_ro_pixels(image, &bitmap); // TODO(halcanary): test |
338 SkAutoLockPixels autoLockPixels(bitmap); // with malformed images. | 338 SkAutoLockPixels autoLockPixels(bitmap); // with malformed images. |
339 | 339 |
340 // Write to a temporary buffer to get the compressed length. | 340 // Write to a temporary buffer to get the compressed length. |
341 SkDynamicMemoryWStream buffer; | 341 SkDynamicMemoryWStream buffer; |
342 SkDeflateWStream deflateWStream(&buffer); | 342 SkDeflateWStream deflateWStream(&buffer); |
343 if (alpha) { | 343 if (alpha) { |
(...skipping 13 matching lines...) Expand all Loading... |
357 } else if (bitmap.colorType() == kIndex_8_SkColorType) { | 357 } else if (bitmap.colorType() == kIndex_8_SkColorType) { |
358 SkASSERT(1 == pdf_color_component_count(bitmap.colorType())); | 358 SkASSERT(1 == pdf_color_component_count(bitmap.colorType())); |
359 pdfDict.insertObject("ColorSpace", | 359 pdfDict.insertObject("ColorSpace", |
360 make_indexed_color_space(bitmap.getColorTable())); | 360 make_indexed_color_space(bitmap.getColorTable())); |
361 } else if (1 == pdf_color_component_count(bitmap.colorType())) { | 361 } else if (1 == pdf_color_component_count(bitmap.colorType())) { |
362 pdfDict.insertName("ColorSpace", "DeviceGray"); | 362 pdfDict.insertName("ColorSpace", "DeviceGray"); |
363 } else { | 363 } else { |
364 pdfDict.insertName("ColorSpace", "DeviceRGB"); | 364 pdfDict.insertName("ColorSpace", "DeviceRGB"); |
365 } | 365 } |
366 if (smask) { | 366 if (smask) { |
367 pdfDict.insertObjRef("SMask", smask); | 367 pdfDict.insertObjRef("SMask", SkRef(smask)); |
368 } | 368 } |
369 pdfDict.insertInt("BitsPerComponent", 8); | 369 pdfDict.insertInt("BitsPerComponent", 8); |
370 pdfDict.insertName("Filter", "FlateDecode"); | 370 pdfDict.insertName("Filter", "FlateDecode"); |
371 pdfDict.insertInt("Length", asset->getLength()); | 371 pdfDict.insertInt("Length", asset->getLength()); |
372 pdfDict.emitObject(stream, objNumMap, substitutes); | 372 pdfDict.emitObject(stream, objNumMap, substitutes); |
373 | 373 |
374 pdf_stream_begin(stream); | 374 pdf_stream_begin(stream); |
375 stream->writeStream(asset.get(), asset->getLength()); | 375 stream->writeStream(asset.get(), asset->getLength()); |
376 pdf_stream_end(stream); | 376 pdf_stream_end(stream); |
377 } | 377 } |
(...skipping 19 matching lines...) Expand all Loading... |
397 } // namespace | 397 } // namespace |
398 | 398 |
399 //////////////////////////////////////////////////////////////////////////////// | 399 //////////////////////////////////////////////////////////////////////////////// |
400 | 400 |
401 namespace { | 401 namespace { |
402 class PDFDefaultBitmap final : public SkPDFObject { | 402 class PDFDefaultBitmap final : public SkPDFObject { |
403 public: | 403 public: |
404 void emitObject(SkWStream* stream, | 404 void emitObject(SkWStream* stream, |
405 const SkPDFObjNumMap& objNumMap, | 405 const SkPDFObjNumMap& objNumMap, |
406 const SkPDFSubstituteMap& subs) const override { | 406 const SkPDFSubstituteMap& subs) const override { |
407 emit_image_xobject(stream, fImage.get(), false, fSMask, objNumMap, subs)
; | 407 emit_image_xobject(stream, fImage.get(), false, fSMask.get(), objNumMap,
subs); |
408 } | 408 } |
409 void addResources(SkPDFObjNumMap* catalog, | 409 void addResources(SkPDFObjNumMap* catalog, |
410 const SkPDFSubstituteMap& subs) const override { | 410 const SkPDFSubstituteMap& subs) const override { |
411 if (fSMask.get()) { | 411 if (fSMask.get()) { |
412 SkPDFObject* obj = subs.getSubstitute(fSMask.get()); | 412 SkPDFObject* obj = subs.getSubstitute(fSMask.get()); |
413 SkASSERT(obj); | 413 SkASSERT(obj); |
414 catalog->addObjectRecursively(obj, subs); | 414 catalog->addObjectRecursively(obj, subs); |
415 } | 415 } |
416 } | 416 } |
417 PDFDefaultBitmap(const SkImage* image, SkPDFObject* smask) | 417 PDFDefaultBitmap(const SkImage* image, SkPDFObject* smask) |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
502 } | 502 } |
503 } | 503 } |
504 | 504 |
505 SkPDFObject* smask = | 505 SkPDFObject* smask = |
506 image_compute_is_opaque(image) ? nullptr : new PDFAlphaBitmap(image)
; | 506 image_compute_is_opaque(image) ? nullptr : new PDFAlphaBitmap(image)
; |
507 #ifdef SK_PDF_IMAGE_STATS | 507 #ifdef SK_PDF_IMAGE_STATS |
508 gRegularImageObjects.fetch_add(1); | 508 gRegularImageObjects.fetch_add(1); |
509 #endif | 509 #endif |
510 return new PDFDefaultBitmap(image, smask); | 510 return new PDFDefaultBitmap(image, smask); |
511 } | 511 } |
OLD | NEW |