| 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 |
|
| 8 |
#ifndef SkJpegCodec_DEFINED |
| 9 |
#define SkJpegCodec_DEFINED |
| 10 |
|
| 11 |
#include "include/codec/SkCodec.h" |
| 12 |
#include "include/codec/SkEncodedImageFormat.h" |
| 13 |
#include "include/codec/SkEncodedOrigin.h" |
| 14 |
#include "include/core/SkRect.h" |
| 15 |
#include "include/core/SkSize.h" |
| 16 |
#include "include/core/SkTypes.h" |
| 17 |
#include "include/core/SkYUVAPixmaps.h" |
| 18 |
#include "include/private/SkEncodedInfo.h" |
| 19 |
#include "include/private/base/SkTemplates.h" |
| 20 |
|
| 21 |
#include <cstddef> |
| 22 |
#include <cstdint> |
| 23 |
#include <memory> |
| 24 |
|
| 25 |
class JpegDecoderMgr; |
| 26 |
class SkSampler; |
| 27 |
class SkStream; |
| 28 |
class SkSwizzler; |
| 29 |
struct SkGainmapInfo; |
| 30 |
struct SkImageInfo; |
| 31 |
|
| 32 |
/* |
| 33 |
* |
| 34 |
* This class implements the decoding for jpeg images |
| 35 |
* |
| 36 |
*/ |
| 37 |
class SkJpegCodec : public SkCodec { |
| 38 |
public: |
| 39 |
~SkJpegCodec() override; |
| 40 |
|
| 41 |
static bool IsJpeg(const void*, size_t); |
| 42 |
|
| 43 |
/* |
| 44 |
* Assumes IsJpeg was called and returned true |
| 45 |
* Takes ownership of the stream |
| 46 |
*/ |
| 47 |
static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*); |
| 48 |
|
| 49 |
protected: |
| 50 |
|
| 51 |
/* |
| 52 |
* Recommend a set of destination dimensions given a requested scale |
| 53 |
*/ |
| 54 |
SkISize onGetScaledDimensions(float desiredScale) const override; |
| 55 |
|
| 56 |
/* |
| 57 |
* Initiates the jpeg decode |
| 58 |
*/ |
| 59 |
Result onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes, const Options&, |
| 60 |
int*) override; |
| 61 |
|
| 62 |
bool onQueryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes&, |
| 63 |
SkYUVAPixmapInfo*) const override; |
| 64 |
|
| 65 |
Result onGetYUVAPlanes(const SkYUVAPixmaps& yuvaPixmaps) override; |
| 66 |
|
| 67 |
SkEncodedImageFormat onGetEncodedFormat() const override { |
| 68 |
return SkEncodedImageFormat::kJPEG; |
| 69 |
} |
| 70 |
|
| 71 |
bool onRewind() override; |
| 72 |
|
| 73 |
bool onDimensionsSupported(const SkISize&) override; |
| 74 |
|
| 75 |
bool conversionSupported(const SkImageInfo&, bool, bool) override; |
| 76 |
|
| 77 |
bool onGetGainmapCodec(SkGainmapInfo* info, std::unique_ptr<SkCodec>* gainmapCodec) override; |
| 78 |
bool onGetGainmapInfo(SkGainmapInfo* info, |
| 79 |
std::unique_ptr<SkStream>* gainmapImageStream) override; |
| 80 |
|
| 81 |
private: |
| 82 |
/* |
| 83 |
* Allows SkRawCodec to communicate the color profile from the exif data. |
| 84 |
*/ |
| 85 |
static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*, |
| 86 |
std::unique_ptr<SkCodecs::ColorProfile> defaultColorProfile); |
| 87 |
|
| 88 |
/* |
| 89 |
* Creates an instance of the decoder |
| 90 |
* Called only by NewFromStream |
| 91 |
* |
| 92 |
* @param info contains properties of the encoded data |
| 93 |
* @param stream the encoded image data |
| 94 |
* @param decoderMgr holds decompress struct, src manager, and error manager |
| 95 |
* takes ownership |
| 96 |
* @param origin indicates the image orientation as specified in Exif metadata. |
| 97 |
* @param xmpMetadata holds the XMP metadata included in the image, if any. |
| 98 |
*/ |
| 99 |
SkJpegCodec(SkEncodedInfo&& info, |
| 100 |
std::unique_ptr<SkStream> stream, |
| 101 |
JpegDecoderMgr* decoderMgr, |
| 102 |
SkEncodedOrigin origin); |
| 103 |
|
| 104 |
void initializeSwizzler(const SkImageInfo& dstInfo, const Options& options, |
| 105 |
bool needsCMYKToRGB); |
| 106 |
[[nodiscard]] bool allocateStorage(const SkImageInfo& dstInfo); |
| 107 |
Result readRows(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, int count, |
| 108 |
const Options&, int* rowsDecoded); |
| 109 |
|
| 110 |
/* |
| 111 |
* Scanline decoding. |
| 112 |
*/ |
| 113 |
SkSampler* getSampler(bool createIfNecessary) override; |
| 114 |
Result onStartScanlineDecode(const SkImageInfo& dstInfo, |
| 115 |
const Options& options) override; |
| 116 |
int onGetScanlines(void* dst, int count, size_t rowBytes) override; |
| 117 |
bool onSkipScanlines(int count) override; |
| 118 |
|
| 119 |
std::unique_ptr<JpegDecoderMgr> fDecoderMgr; |
| 120 |
|
| 121 |
// We will save the state of the decompress struct after reading the header. |
| 122 |
// This allows us to safely call onGetScaledDimensions() at any time. |
| 123 |
const int fReadyState; |
| 124 |
|
| 125 |
skia_private::AutoTMalloc<uint8_t> fStorage; |
| 126 |
uint8_t* fSwizzleSrcRow = nullptr; |
| 127 |
uint32_t* fColorXformSrcRow = nullptr; |
| 128 |
|
| 129 |
// libjpeg-turbo provides some subsetting. In the case that libjpeg-turbo |
| 130 |
// cannot take the exact the subset that we need, we will use the swizzler |
| 131 |
// to further subset the output from libjpeg-turbo. |
| 132 |
SkIRect fSwizzlerSubset = SkIRect::MakeEmpty(); |
| 133 |
|
| 134 |
std::unique_ptr<SkSwizzler> fSwizzler; |
| 135 |
|
| 136 |
friend class SkRawCodec; |
| 137 |
}; |
| 138 |
|
| 139 |
#endif |