SkJpegCodec.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright 2015 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef SkJpegCodec_DEFINED
  8. #define SkJpegCodec_DEFINED
  9. #include "include/codec/SkCodec.h"
  10. #include "include/core/SkImageInfo.h"
  11. #include "include/core/SkStream.h"
  12. #include "include/private/SkTemplates.h"
  13. #include "src/codec/SkSwizzler.h"
  14. class JpegDecoderMgr;
  15. /*
  16. *
  17. * This class implements the decoding for jpeg images
  18. *
  19. */
  20. class SkJpegCodec : public SkCodec {
  21. public:
  22. static bool IsJpeg(const void*, size_t);
  23. /*
  24. * Assumes IsJpeg was called and returned true
  25. * Takes ownership of the stream
  26. */
  27. static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*);
  28. protected:
  29. /*
  30. * Recommend a set of destination dimensions given a requested scale
  31. */
  32. SkISize onGetScaledDimensions(float desiredScale) const override;
  33. /*
  34. * Initiates the jpeg decode
  35. */
  36. Result onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes, const Options&,
  37. int*) override;
  38. bool onQueryYUV8(SkYUVASizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const override;
  39. Result onGetYUV8Planes(const SkYUVASizeInfo& sizeInfo,
  40. void* planes[SkYUVASizeInfo::kMaxCount]) override;
  41. SkEncodedImageFormat onGetEncodedFormat() const override {
  42. return SkEncodedImageFormat::kJPEG;
  43. }
  44. bool onRewind() override;
  45. bool onDimensionsSupported(const SkISize&) override;
  46. bool conversionSupported(const SkImageInfo&, bool, bool) override;
  47. private:
  48. /*
  49. * Allows SkRawCodec to communicate the color profile from the exif data.
  50. */
  51. static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*,
  52. std::unique_ptr<SkEncodedInfo::ICCProfile> defaultColorProfile);
  53. /*
  54. * Read enough of the stream to initialize the SkJpegCodec.
  55. * Returns a bool representing success or failure.
  56. *
  57. * @param codecOut
  58. * If this returns true, and codecOut was not nullptr,
  59. * codecOut will be set to a new SkJpegCodec.
  60. *
  61. * @param decoderMgrOut
  62. * If this returns true, and codecOut was nullptr,
  63. * decoderMgrOut must be non-nullptr and decoderMgrOut will be set to a new
  64. * JpegDecoderMgr pointer.
  65. *
  66. * @param stream
  67. * Deleted on failure.
  68. * codecOut will take ownership of it in the case where we created a codec.
  69. * Ownership is unchanged when we set decoderMgrOut.
  70. *
  71. * @param defaultColorProfile
  72. * If the jpeg does not have an embedded color profile, the image data should
  73. * be tagged with this color profile.
  74. */
  75. static Result ReadHeader(SkStream* stream, SkCodec** codecOut,
  76. JpegDecoderMgr** decoderMgrOut,
  77. std::unique_ptr<SkEncodedInfo::ICCProfile> defaultColorProfile);
  78. /*
  79. * Creates an instance of the decoder
  80. * Called only by NewFromStream
  81. *
  82. * @param info contains properties of the encoded data
  83. * @param stream the encoded image data
  84. * @param decoderMgr holds decompress struct, src manager, and error manager
  85. * takes ownership
  86. */
  87. SkJpegCodec(SkEncodedInfo&& info, std::unique_ptr<SkStream> stream,
  88. JpegDecoderMgr* decoderMgr, SkEncodedOrigin origin);
  89. void initializeSwizzler(const SkImageInfo& dstInfo, const Options& options,
  90. bool needsCMYKToRGB);
  91. void allocateStorage(const SkImageInfo& dstInfo);
  92. int readRows(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, int count, const Options&);
  93. /*
  94. * Scanline decoding.
  95. */
  96. SkSampler* getSampler(bool createIfNecessary) override;
  97. Result onStartScanlineDecode(const SkImageInfo& dstInfo,
  98. const Options& options) override;
  99. int onGetScanlines(void* dst, int count, size_t rowBytes) override;
  100. bool onSkipScanlines(int count) override;
  101. std::unique_ptr<JpegDecoderMgr> fDecoderMgr;
  102. // We will save the state of the decompress struct after reading the header.
  103. // This allows us to safely call onGetScaledDimensions() at any time.
  104. const int fReadyState;
  105. SkAutoTMalloc<uint8_t> fStorage;
  106. uint8_t* fSwizzleSrcRow;
  107. uint32_t* fColorXformSrcRow;
  108. // libjpeg-turbo provides some subsetting. In the case that libjpeg-turbo
  109. // cannot take the exact the subset that we need, we will use the swizzler
  110. // to further subset the output from libjpeg-turbo.
  111. SkIRect fSwizzlerSubset;
  112. std::unique_ptr<SkSwizzler> fSwizzler;
  113. friend class SkRawCodec;
  114. typedef SkCodec INHERITED;
  115. };
  116. #endif