msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 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 SkAndroidCodec_DEFINED |
| 9 | #define SkAndroidCodec_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame^] | 11 | #include "include/codec/SkCodec.h" |
| 12 | #include "include/core/SkEncodedImageFormat.h" |
| 13 | #include "include/core/SkStream.h" |
| 14 | #include "include/core/SkTypes.h" |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 15 | |
| 16 | /** |
| 17 | * Abstract interface defining image codec functionality that is necessary for |
| 18 | * Android. |
| 19 | */ |
Derek Sollenberger | 2fbf1bc | 2017-09-20 15:51:08 -0400 | [diff] [blame] | 20 | class SK_API SkAndroidCodec : SkNoncopyable { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 21 | public: |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 22 | enum class ExifOrientationBehavior { |
| 23 | /** |
| 24 | * Ignore any exif orientation markers in the data. |
| 25 | * |
| 26 | * getInfo's width and height will match the header of the image, and |
| 27 | * no processing will be done to match the marker. |
| 28 | */ |
| 29 | kIgnore, |
| 30 | |
| 31 | /** |
| 32 | * Respect the exif orientation marker. |
| 33 | * |
| 34 | * getInfo's width and height will represent what they should be after |
| 35 | * applying the orientation. For example, if the marker specifies a |
| 36 | * rotation by 90 degrees, they will be swapped relative to the header. |
| 37 | * getAndroidPixels will apply the orientation as well. |
| 38 | */ |
| 39 | kRespect, |
| 40 | }; |
| 41 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 42 | /** |
Leon Scroggins III | 7397d7a | 2018-01-04 13:26:30 -0500 | [diff] [blame] | 43 | * Pass ownership of an SkCodec to a newly-created SkAndroidCodec. |
| 44 | */ |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 45 | static std::unique_ptr<SkAndroidCodec> MakeFromCodec(std::unique_ptr<SkCodec>, |
| 46 | ExifOrientationBehavior = ExifOrientationBehavior::kIgnore); |
Leon Scroggins III | 7397d7a | 2018-01-04 13:26:30 -0500 | [diff] [blame] | 47 | |
| 48 | /** |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 49 | * If this stream represents an encoded image that we know how to decode, |
| 50 | * return an SkAndroidCodec that can decode it. Otherwise return NULL. |
| 51 | * |
msarett | 7d5105c | 2015-12-02 07:02:41 -0800 | [diff] [blame] | 52 | * The SkPngChunkReader handles unknown chunks in PNGs. |
| 53 | * See SkCodec.h for more details. |
| 54 | * |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 55 | * If NULL is returned, the stream is deleted immediately. Otherwise, the |
| 56 | * SkCodec takes ownership of it, and will delete it when done with it. |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 57 | * |
| 58 | * ExifOrientationBehavior is set to kIgnore. |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 59 | */ |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 60 | static std::unique_ptr<SkAndroidCodec> MakeFromStream(std::unique_ptr<SkStream>, |
| 61 | SkPngChunkReader* = nullptr); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * If this data represents an encoded image that we know how to decode, |
| 65 | * return an SkAndroidCodec that can decode it. Otherwise return NULL. |
| 66 | * |
msarett | 7d5105c | 2015-12-02 07:02:41 -0800 | [diff] [blame] | 67 | * The SkPngChunkReader handles unknown chunks in PNGs. |
| 68 | * See SkCodec.h for more details. |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 69 | * |
| 70 | * ExifOrientationBehavior is set to kIgnore. |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 71 | */ |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 72 | static std::unique_ptr<SkAndroidCodec> MakeFromData(sk_sp<SkData>, SkPngChunkReader* = nullptr); |
| 73 | |
Leon Scroggins III | 0741818 | 2017-08-15 12:24:02 -0400 | [diff] [blame] | 74 | virtual ~SkAndroidCodec(); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 75 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 76 | const SkImageInfo& getInfo() const { return fInfo; } |
| 77 | |
| 78 | /** |
| 79 | * Format of the encoded data. |
| 80 | */ |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 81 | SkEncodedImageFormat getEncodedFormat() const { return fCodec->getEncodedFormat(); } |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 82 | |
| 83 | /** |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 84 | * @param requestedColorType Color type requested by the client |
| 85 | * |
Matt Sarett | 8dcc84f | 2016-12-14 10:23:41 -0500 | [diff] [blame] | 86 | * |requestedColorType| may be overriden. We will default to kF16 |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 87 | * for high precision images. |
Matt Sarett | 8dcc84f | 2016-12-14 10:23:41 -0500 | [diff] [blame] | 88 | * |
| 89 | * In the general case, if it is possible to decode to |
| 90 | * |requestedColorType|, this returns |requestedColorType|. |
| 91 | * Otherwise, this returns a color type that is an appropriate |
| 92 | * match for the the encoded data. |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 93 | */ |
| 94 | SkColorType computeOutputColorType(SkColorType requestedColorType); |
| 95 | |
| 96 | /** |
| 97 | * @param requestedUnpremul Indicates if the client requested |
| 98 | * unpremultiplied output |
| 99 | * |
| 100 | * Returns the appropriate alpha type to decode to. If the image |
| 101 | * has alpha, the value of requestedUnpremul will be honored. |
| 102 | */ |
| 103 | SkAlphaType computeOutputAlphaType(bool requestedUnpremul); |
| 104 | |
| 105 | /** |
Matt Sarett | 68feef4 | 2017-04-11 09:51:32 -0400 | [diff] [blame] | 106 | * @param outputColorType Color type that the client will decode to. |
| 107 | * @param prefColorSpace Preferred color space to decode to. |
| 108 | * This may not return |prefColorSpace| for a couple reasons. |
| 109 | * (1) Android Principles: 565 must be sRGB, F16 must be |
| 110 | * linear sRGB, transfer function must be parametric. |
| 111 | * (2) Codec Limitations: F16 requires a linear color space. |
Matt Sarett | 966bb34 | 2016-12-12 16:30:13 -0500 | [diff] [blame] | 112 | * |
| 113 | * Returns the appropriate color space to decode to. |
Matt Sarett | 966bb34 | 2016-12-12 16:30:13 -0500 | [diff] [blame] | 114 | */ |
Matt Sarett | 68feef4 | 2017-04-11 09:51:32 -0400 | [diff] [blame] | 115 | sk_sp<SkColorSpace> computeOutputColorSpace(SkColorType outputColorType, |
| 116 | sk_sp<SkColorSpace> prefColorSpace = nullptr); |
Matt Sarett | 966bb34 | 2016-12-12 16:30:13 -0500 | [diff] [blame] | 117 | |
| 118 | /** |
Leon Scroggins III | 07a722c | 2018-01-16 15:01:17 -0500 | [diff] [blame] | 119 | * Compute the appropriate sample size to get to |size|. |
| 120 | * |
| 121 | * @param size As an input parameter, the desired output size of |
| 122 | * the decode. As an output parameter, the smallest sampled size |
| 123 | * larger than the input. |
| 124 | * @return the sample size to set AndroidOptions::fSampleSize to decode |
| 125 | * to the output |size|. |
| 126 | */ |
| 127 | int computeSampleSize(SkISize* size) const; |
| 128 | |
| 129 | /** |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 130 | * Returns the dimensions of the scaled output image, for an input |
| 131 | * sampleSize. |
| 132 | * |
| 133 | * When the sample size divides evenly into the original dimensions, the |
| 134 | * scaled output dimensions will simply be equal to the original |
| 135 | * dimensions divided by the sample size. |
| 136 | * |
| 137 | * When the sample size does not divide even into the original |
| 138 | * dimensions, the codec may round up or down, depending on what is most |
| 139 | * efficient to decode. |
| 140 | * |
| 141 | * Finally, the codec will always recommend a non-zero output, so the output |
| 142 | * dimension will always be one if the sampleSize is greater than the |
| 143 | * original dimension. |
| 144 | */ |
| 145 | SkISize getSampledDimensions(int sampleSize) const; |
| 146 | |
| 147 | /** |
| 148 | * Return (via desiredSubset) a subset which can decoded from this codec, |
| 149 | * or false if the input subset is invalid. |
| 150 | * |
| 151 | * @param desiredSubset in/out parameter |
| 152 | * As input, a desired subset of the original bounds |
| 153 | * (as specified by getInfo). |
| 154 | * As output, if true is returned, desiredSubset may |
| 155 | * have been modified to a subset which is |
| 156 | * supported. Although a particular change may have |
| 157 | * been made to desiredSubset to create something |
| 158 | * supported, it is possible other changes could |
| 159 | * result in a valid subset. If false is returned, |
| 160 | * desiredSubset's value is undefined. |
| 161 | * @return true If the input desiredSubset is valid. |
| 162 | * desiredSubset may be modified to a subset |
| 163 | * supported by the codec. |
| 164 | * false If desiredSubset is invalid (NULL or not fully |
| 165 | * contained within the image). |
| 166 | */ |
| 167 | bool getSupportedSubset(SkIRect* desiredSubset) const; |
| 168 | // TODO: Rename SkCodec::getValidSubset() to getSupportedSubset() |
| 169 | |
| 170 | /** |
| 171 | * Returns the dimensions of the scaled, partial output image, for an |
| 172 | * input sampleSize and subset. |
| 173 | * |
| 174 | * @param sampleSize Factor to scale down by. |
| 175 | * @param subset Must be a valid subset of the original image |
| 176 | * dimensions and a subset supported by SkAndroidCodec. |
| 177 | * getSubset() can be used to obtain a subset supported |
| 178 | * by SkAndroidCodec. |
| 179 | * @return Size of the scaled partial image. Or zero size |
| 180 | * if either of the inputs is invalid. |
| 181 | */ |
| 182 | SkISize getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const; |
| 183 | |
| 184 | /** |
| 185 | * Additional options to pass to getAndroidPixels(). |
| 186 | */ |
| 187 | // FIXME: It's a bit redundant to name these AndroidOptions when this class is already |
| 188 | // called SkAndroidCodec. On the other hand, it's may be a bit confusing to call |
| 189 | // these Options when SkCodec has a slightly different set of Options. Maybe these |
| 190 | // should be DecodeOptions or SamplingOptions? |
| 191 | struct AndroidOptions { |
| 192 | AndroidOptions() |
| 193 | : fZeroInitialized(SkCodec::kNo_ZeroInitialized) |
| 194 | , fSubset(nullptr) |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 195 | , fSampleSize(1) |
| 196 | {} |
| 197 | |
| 198 | /** |
| 199 | * Indicates is destination pixel memory is zero initialized. |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 200 | * |
| 201 | * The default is SkCodec::kNo_ZeroInitialized. |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 202 | */ |
| 203 | SkCodec::ZeroInitialized fZeroInitialized; |
| 204 | |
| 205 | /** |
| 206 | * If not NULL, represents a subset of the original image to decode. |
| 207 | * |
| 208 | * Must be within the bounds returned by getInfo(). |
| 209 | * |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 210 | * If the EncodedFormat is SkEncodedImageFormat::kWEBP, the top and left |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 211 | * values must be even. |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 212 | * |
| 213 | * The default is NULL, meaning a decode of the entire image. |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 214 | */ |
| 215 | SkIRect* fSubset; |
| 216 | |
| 217 | /** |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 218 | * The client may provide an integer downscale factor for the decode. |
| 219 | * The codec may implement this downscaling by sampling or another |
| 220 | * method if it is more efficient. |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 221 | * |
| 222 | * The default is 1, representing no downscaling. |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 223 | */ |
| 224 | int fSampleSize; |
| 225 | }; |
| 226 | |
| 227 | /** |
| 228 | * Decode into the given pixels, a block of memory of size at |
| 229 | * least (info.fHeight - 1) * rowBytes + (info.fWidth * |
| 230 | * bytesPerPixel) |
| 231 | * |
| 232 | * Repeated calls to this function should give the same results, |
| 233 | * allowing the PixelRef to be immutable. |
| 234 | * |
| 235 | * @param info A description of the format (config, size) |
| 236 | * expected by the caller. This can simply be identical |
| 237 | * to the info returned by getInfo(). |
| 238 | * |
| 239 | * This contract also allows the caller to specify |
| 240 | * different output-configs, which the implementation can |
| 241 | * decide to support or not. |
| 242 | * |
| 243 | * A size that does not match getInfo() implies a request |
| 244 | * to scale or subset. If the codec cannot perform this |
| 245 | * scaling or subsetting, it will return an error code. |
| 246 | * |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 247 | * The AndroidOptions object is also used to specify any requested scaling or subsetting |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 248 | * using options->fSampleSize and options->fSubset. If NULL, the defaults (as specified above |
| 249 | * for AndroidOptions) are used. |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 250 | * |
| 251 | * @return Result kSuccess, or another value explaining the type of failure. |
| 252 | */ |
| 253 | // FIXME: It's a bit redundant to name this getAndroidPixels() when this class is already |
| 254 | // called SkAndroidCodec. On the other hand, it's may be a bit confusing to call |
| 255 | // this getPixels() when it is a slightly different API than SkCodec's getPixels(). |
| 256 | // Maybe this should be decode() or decodeSubset()? |
| 257 | SkCodec::Result getAndroidPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
scroggo | e95a068 | 2015-11-04 04:31:12 -0800 | [diff] [blame] | 258 | const AndroidOptions* options); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 259 | |
| 260 | /** |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 261 | * Simplified version of getAndroidPixels() where we supply the default AndroidOptions as |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 262 | * specified above for AndroidOptions. It will not perform any scaling or subsetting. |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 263 | */ |
| 264 | SkCodec::Result getAndroidPixels(const SkImageInfo& info, void* pixels, size_t rowBytes); |
| 265 | |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 266 | SkCodec::Result getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
| 267 | return this->getAndroidPixels(info, pixels, rowBytes); |
| 268 | } |
| 269 | |
Leon Scroggins III | 42ee284 | 2018-01-14 14:46:51 -0500 | [diff] [blame] | 270 | SkCodec* codec() const { return fCodec.get(); } |
| 271 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 272 | protected: |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 273 | SkAndroidCodec(SkCodec*, ExifOrientationBehavior = ExifOrientationBehavior::kIgnore); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 274 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 275 | virtual SkISize onGetSampledDimensions(int sampleSize) const = 0; |
| 276 | |
| 277 | virtual bool onGetSupportedSubset(SkIRect* desiredSubset) const = 0; |
| 278 | |
| 279 | virtual SkCodec::Result onGetAndroidPixels(const SkImageInfo& info, void* pixels, |
scroggo | e95a068 | 2015-11-04 04:31:12 -0800 | [diff] [blame] | 280 | size_t rowBytes, const AndroidOptions& options) = 0; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 281 | |
| 282 | private: |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 283 | const SkImageInfo fInfo; |
| 284 | const ExifOrientationBehavior fOrientationBehavior; |
| 285 | std::unique_ptr<SkCodec> fCodec; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 286 | }; |
| 287 | #endif // SkAndroidCodec_DEFINED |