2 * Copyright 2015 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
7 #ifndef SkCodecImageGenerator_DEFINED
8 #define SkCodecImageGenerator_DEFINED
10 #include "include/codec/SkCodec.h"
11 #include "include/core/SkData.h"
12 #include "include/core/SkImageGenerator.h"
16 class SkCodecImageGenerator : public SkImageGenerator {
19 * If this data represents an encoded image that we know how to decode,
20 * return an SkCodecImageGenerator. Otherwise return nullptr.
22 static std::unique_ptr<SkImageGenerator> MakeFromEncodedCodec(
23 sk_sp<SkData>, std::optional<SkAlphaType> = std::nullopt);
25 static std::unique_ptr<SkImageGenerator> MakeFromCodec(std::unique_ptr<SkCodec>);
28 * Return a size that approximately supports the desired scale factor. The codec may not be able
29 * to scale efficiently to the exact scale factor requested, so return a size that approximates
30 * that scale. The returned value is the codec's suggestion for the closest valid scale that it
31 * can natively support.
33 * This is similar to SkCodec::getScaledDimensions, but adjusts the returned dimensions based
34 * on the image's EXIF orientation.
36 SkISize getScaledDimensions(float desiredScale) const;
39 * Decode into the given pixels, a block of memory of size at
40 * least (info.fHeight - 1) * rowBytes + (info.fWidth *
43 * Repeated calls to this function should give the same results,
44 * allowing the PixelRef to be immutable.
46 * @param info A description of the format
47 * expected by the caller. This can simply be identical
48 * to the info returned by getInfo().
50 * This contract also allows the caller to specify
51 * different output-configs, which the implementation can
52 * decide to support or not.
54 * A size that does not match getInfo() implies a request
55 * to scale. If the generator cannot perform this scale,
56 * it will return false.
58 * @return true on success.
60 bool getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, const SkCodec::Options* options = nullptr);
63 * Return the number of frames in the image.
65 * May require reading through the stream.
67 int getFrameCount() { return fCodec->getFrameCount(); }
70 * Return info about a single frame.
72 * Only supported by multi-frame images. Does not read through the stream,
73 * so it should be called after getFrameCount() to parse any frames that
74 * have not already been parsed.
76 bool getFrameInfo(int index, SkCodec::FrameInfo* info) const {
77 return fCodec->getFrameInfo(index, info);
81 * Return the number of times to repeat, if this image is animated. This number does not
82 * include the first play through of each frame. For example, a repetition count of 4 means
83 * that each frame is played 5 times and then the animation stops.
85 * It can return kRepetitionCountInfinite, a negative number, meaning that the animation
86 * should loop forever.
88 * May require reading the stream to find the repetition count.
90 * As such, future decoding calls may require a rewind.
92 * For still (non-animated) image codecs, this will return 0.
94 int getRepetitionCount() { return fCodec->getRepetitionCount(); }
97 sk_sp<SkData> onRefEncodedData() override;
99 bool onGetPixels(const SkImageInfo& info,
102 const Options& opts) override;
104 bool onQueryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes&,
105 SkYUVAPixmapInfo*) const override;
107 bool onGetYUVAPlanes(const SkYUVAPixmaps& yuvaPixmaps) override;
111 * Takes ownership of codec
113 SkCodecImageGenerator(std::unique_ptr<SkCodec>, sk_sp<SkData>, std::optional<SkAlphaType>);
115 std::unique_ptr<SkCodec> fCodec;
118 using INHERITED = SkImageGenerator;
120 #endif // SkCodecImageGenerator_DEFINED