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 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 } |
378 | 378 |
379 //////////////////////////////////////////////////////////////////////////////// | 379 //////////////////////////////////////////////////////////////////////////////// |
380 | 380 |
381 namespace { | 381 namespace { |
382 // This SkPDFObject only outputs the alpha layer of the given bitmap. | 382 // This SkPDFObject only outputs the alpha layer of the given bitmap. |
383 class PDFAlphaBitmap final : public SkPDFObject { | 383 class PDFAlphaBitmap final : public SkPDFObject { |
384 public: | 384 public: |
385 PDFAlphaBitmap(const SkImage* image) : fImage(SkRef(image)) { SkASSERT(image
); } | 385 PDFAlphaBitmap(sk_sp<SkImage> image) : fImage(std::move(image)) { SkASSERT(f
Image); } |
386 void emitObject(SkWStream* stream, | 386 void emitObject(SkWStream* stream, |
387 const SkPDFObjNumMap& objNumMap, | 387 const SkPDFObjNumMap& objNumMap, |
388 const SkPDFSubstituteMap& subs) const override { | 388 const SkPDFSubstituteMap& subs) const override { |
389 SkASSERT(fImage); | 389 SkASSERT(fImage); |
390 emit_image_xobject(stream, fImage.get(), true, nullptr, objNumMap, subs)
; | 390 emit_image_xobject(stream, fImage.get(), true, nullptr, objNumMap, subs)
; |
391 } | 391 } |
392 void drop() override { fImage = nullptr; } | 392 void drop() override { fImage = nullptr; } |
393 | 393 |
394 private: | 394 private: |
395 sk_sp<const SkImage> fImage; | 395 sk_sp<SkImage> fImage; |
396 }; | 396 }; |
397 | 397 |
398 } // namespace | 398 } // namespace |
399 | 399 |
400 //////////////////////////////////////////////////////////////////////////////// | 400 //////////////////////////////////////////////////////////////////////////////// |
401 | 401 |
402 namespace { | 402 namespace { |
403 class PDFDefaultBitmap final : public SkPDFObject { | 403 class PDFDefaultBitmap final : public SkPDFObject { |
404 public: | 404 public: |
405 void emitObject(SkWStream* stream, | 405 void emitObject(SkWStream* stream, |
406 const SkPDFObjNumMap& objNumMap, | 406 const SkPDFObjNumMap& objNumMap, |
407 const SkPDFSubstituteMap& subs) const override { | 407 const SkPDFSubstituteMap& subs) const override { |
408 SkASSERT(fImage); | 408 SkASSERT(fImage); |
409 emit_image_xobject(stream, fImage.get(), false, fSMask, objNumMap, subs)
; | 409 emit_image_xobject(stream, fImage.get(), false, fSMask, objNumMap, subs)
; |
410 } | 410 } |
411 void addResources(SkPDFObjNumMap* catalog, | 411 void addResources(SkPDFObjNumMap* catalog, |
412 const SkPDFSubstituteMap& subs) const override { | 412 const SkPDFSubstituteMap& subs) const override { |
413 SkASSERT(fImage); | 413 SkASSERT(fImage); |
414 if (fSMask.get()) { | 414 if (fSMask.get()) { |
415 SkPDFObject* obj = subs.getSubstitute(fSMask.get()); | 415 SkPDFObject* obj = subs.getSubstitute(fSMask.get()); |
416 SkASSERT(obj); | 416 SkASSERT(obj); |
417 catalog->addObjectRecursively(obj, subs); | 417 catalog->addObjectRecursively(obj, subs); |
418 } | 418 } |
419 } | 419 } |
420 void drop() override { fImage = nullptr; fSMask = nullptr; } | 420 void drop() override { fImage = nullptr; fSMask = nullptr; } |
421 PDFDefaultBitmap(const SkImage* image, SkPDFObject* smask) | 421 PDFDefaultBitmap(sk_sp<SkImage> image, sk_sp<SkPDFObject> smask) |
422 : fImage(SkRef(image)), fSMask(smask) { SkASSERT(fImage); } | 422 : fImage(std::move(image)), fSMask(std::move(smask)) { SkASSERT(fImage);
} |
423 | 423 |
424 private: | 424 private: |
425 sk_sp<const SkImage> fImage; | 425 sk_sp<SkImage> fImage; |
426 sk_sp<SkPDFObject> fSMask; | 426 sk_sp<SkPDFObject> fSMask; |
427 }; | 427 }; |
428 } // namespace | 428 } // namespace |
429 | 429 |
430 //////////////////////////////////////////////////////////////////////////////// | 430 //////////////////////////////////////////////////////////////////////////////// |
431 | 431 |
432 namespace { | 432 namespace { |
433 /** | 433 /** |
434 * This PDFObject assumes that its constructor was handed YUV or | 434 * This PDFObject assumes that its constructor was handed YUV or |
435 * Grayscale JFIF Jpeg-encoded data that can be directly embedded | 435 * Grayscale JFIF Jpeg-encoded data that can be directly embedded |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
467 pdfDict.insertInt("Length", SkToInt(fData->size())); | 467 pdfDict.insertInt("Length", SkToInt(fData->size())); |
468 pdfDict.emitObject(stream, objNumMap, substituteMap); | 468 pdfDict.emitObject(stream, objNumMap, substituteMap); |
469 pdf_stream_begin(stream); | 469 pdf_stream_begin(stream); |
470 stream->write(fData->data(), fData->size()); | 470 stream->write(fData->data(), fData->size()); |
471 pdf_stream_end(stream); | 471 pdf_stream_end(stream); |
472 } | 472 } |
473 } // namespace | 473 } // namespace |
474 | 474 |
475 //////////////////////////////////////////////////////////////////////////////// | 475 //////////////////////////////////////////////////////////////////////////////// |
476 | 476 |
477 SkPDFObject* SkPDFCreateBitmapObject(const SkImage* image, | 477 sk_sp<SkPDFObject> SkPDFCreateBitmapObject(sk_sp<SkImage> image, |
478 SkPixelSerializer* pixelSerializer) { | 478 SkPixelSerializer* pixelSerializer) { |
| 479 SkASSERT(image); |
479 sk_sp<SkData> data(image->refEncoded()); | 480 sk_sp<SkData> data(image->refEncoded()); |
480 SkJFIFInfo info; | 481 SkJFIFInfo info; |
481 if (data && SkIsJFIF(data.get(), &info) && | 482 if (data && SkIsJFIF(data.get(), &info) && |
482 (!pixelSerializer || | 483 (!pixelSerializer || |
483 pixelSerializer->useEncodedData(data->data(), data->size()))) { | 484 pixelSerializer->useEncodedData(data->data(), data->size()))) { |
484 // If there is a SkPixelSerializer, give it a chance to | 485 // If there is a SkPixelSerializer, give it a chance to |
485 // re-encode the JPEG with more compression by returning false | 486 // re-encode the JPEG with more compression by returning false |
486 // from useEncodedData. | 487 // from useEncodedData. |
487 bool yuv = info.fType == SkJFIFInfo::kYCbCr; | 488 bool yuv = info.fType == SkJFIFInfo::kYCbCr; |
488 if (info.fSize == image->dimensions()) { // Sanity check. | 489 if (info.fSize == image->dimensions()) { // Sanity check. |
489 // hold on to data, not image. | 490 // hold on to data, not image. |
490 #ifdef SK_PDF_IMAGE_STATS | 491 #ifdef SK_PDF_IMAGE_STATS |
491 gJpegImageObjects.fetch_add(1); | 492 gJpegImageObjects.fetch_add(1); |
492 #endif | 493 #endif |
493 return new PDFJpegBitmap(info.fSize, data.get(), yuv); | 494 return sk_make_sp<PDFJpegBitmap>(info.fSize, data.get(), yuv); |
494 } | 495 } |
495 } | 496 } |
496 | 497 |
497 if (pixelSerializer) { | 498 if (pixelSerializer) { |
498 SkBitmap bm; | 499 SkBitmap bm; |
499 SkAutoPixmapUnlock apu; | 500 SkAutoPixmapUnlock apu; |
500 if (as_IB(image)->getROPixels(&bm) && bm.requestLock(&apu)) { | 501 if (as_IB(image.get())->getROPixels(&bm) && bm.requestLock(&apu)) { |
501 data.reset(pixelSerializer->encode(apu.pixmap())); | 502 data.reset(pixelSerializer->encode(apu.pixmap())); |
502 if (data && SkIsJFIF(data.get(), &info)) { | 503 if (data && SkIsJFIF(data.get(), &info)) { |
503 bool yuv = info.fType == SkJFIFInfo::kYCbCr; | 504 bool yuv = info.fType == SkJFIFInfo::kYCbCr; |
504 if (info.fSize == image->dimensions()) { // Sanity check. | 505 if (info.fSize == image->dimensions()) { // Sanity check. |
505 return new PDFJpegBitmap(info.fSize, data.get(), yuv); | 506 return sk_make_sp<PDFJpegBitmap>(info.fSize, data.get(), yuv
); |
506 } | 507 } |
507 } | 508 } |
508 } | 509 } |
509 } | 510 } |
510 | 511 |
511 SkPDFObject* smask = | 512 sk_sp<SkPDFObject> smask; |
512 image_compute_is_opaque(image) ? nullptr : new PDFAlphaBitmap(image)
; | 513 if (!image_compute_is_opaque(image.get())) { |
| 514 smask = sk_make_sp<PDFAlphaBitmap>(image); |
| 515 } |
513 #ifdef SK_PDF_IMAGE_STATS | 516 #ifdef SK_PDF_IMAGE_STATS |
514 gRegularImageObjects.fetch_add(1); | 517 gRegularImageObjects.fetch_add(1); |
515 #endif | 518 #endif |
516 return new PDFDefaultBitmap(image, smask); | 519 return sk_make_sp<PDFDefaultBitmap>(std::move(image), std::move(smask)); |
517 } | 520 } |
OLD | NEW |