scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [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 SkCodec_DEFINED |
| 9 | #define SkCodec_DEFINED |
| 10 | |
bungeman | f3c15b7 | 2015-08-19 11:56:48 -0700 | [diff] [blame] | 11 | #include "../private/SkTemplates.h" |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 12 | #include "SkColor.h" |
scroggo | 1dd3ea9 | 2015-03-20 11:55:55 -0700 | [diff] [blame] | 13 | #include "SkEncodedFormat.h" |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 14 | #include "SkEncodedInfo.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 15 | #include "SkImageInfo.h" |
| 16 | #include "SkSize.h" |
scroggo | fffeede | 2015-03-18 10:50:37 -0700 | [diff] [blame] | 17 | #include "SkStream.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 18 | #include "SkTypes.h" |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 19 | #include "SkYUVSizeInfo.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 20 | |
msarett | ad8bcfe | 2016-03-07 07:09:03 -0800 | [diff] [blame] | 21 | class SkColorSpace; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 22 | class SkData; |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 23 | class SkPngChunkReader; |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 24 | class SkSampler; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 25 | |
msarett | 9876ac5 | 2016-06-01 14:47:18 -0700 | [diff] [blame^] | 26 | namespace DM { |
| 27 | class ColorCodecSrc; |
| 28 | } |
| 29 | |
| 30 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 31 | /** |
| 32 | * Abstraction layer directly on top of an image codec. |
| 33 | */ |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 34 | class SkCodec : SkNoncopyable { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 35 | public: |
| 36 | /** |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 37 | * Minimum number of bytes that must be buffered in SkStream input. |
| 38 | * |
| 39 | * An SkStream passed to NewFromStream must be able to use this many |
| 40 | * bytes to determine the image type. Then the same SkStream must be |
| 41 | * passed to the correct decoder to read from the beginning. |
| 42 | * |
| 43 | * This can be accomplished by implementing peek() to support peeking |
| 44 | * this many bytes, or by implementing rewind() to be able to rewind() |
| 45 | * after reading this many bytes. |
| 46 | */ |
| 47 | static size_t MinBufferedBytesNeeded(); |
| 48 | |
| 49 | /** |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 50 | * If this stream represents an encoded image that we know how to decode, |
| 51 | * return an SkCodec that can decode it. Otherwise return NULL. |
| 52 | * |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 53 | * As stated above, this call must be able to peek or read |
| 54 | * MinBufferedBytesNeeded to determine the correct format, and then start |
| 55 | * reading from the beginning. First it will attempt to peek, and it |
| 56 | * assumes that if less than MinBufferedBytesNeeded bytes (but more than |
| 57 | * zero) are returned, this is because the stream is shorter than this, |
| 58 | * so falling back to reading would not provide more data. If peek() |
| 59 | * returns zero bytes, this call will instead attempt to read(). This |
| 60 | * will require that the stream can be rewind()ed. |
| 61 | * |
| 62 | * If SkPngChunkReader is not NULL, take a ref and pass it to libpng if |
| 63 | * the image is a png. |
| 64 | * |
msarett | 7d5105c | 2015-12-02 07:02:41 -0800 | [diff] [blame] | 65 | * If the SkPngChunkReader is not NULL then: |
| 66 | * If the image is not a PNG, the SkPngChunkReader will be ignored. |
| 67 | * If the image is a PNG, the SkPngChunkReader will be reffed. |
| 68 | * If the PNG has unknown chunks, the SkPngChunkReader will be used |
| 69 | * to handle these chunks. SkPngChunkReader will be called to read |
| 70 | * any unknown chunk at any point during the creation of the codec |
| 71 | * or the decode. Note that if SkPngChunkReader fails to read a |
| 72 | * chunk, this could result in a failure to create the codec or a |
| 73 | * failure to decode the image. |
| 74 | * If the PNG does not contain unknown chunks, the SkPngChunkReader |
| 75 | * will not be used or modified. |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 76 | * |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 77 | * If NULL is returned, the stream is deleted immediately. Otherwise, the |
| 78 | * SkCodec takes ownership of it, and will delete it when done with it. |
| 79 | */ |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 80 | static SkCodec* NewFromStream(SkStream*, SkPngChunkReader* = NULL); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * If this data represents an encoded image that we know how to decode, |
| 84 | * return an SkCodec that can decode it. Otherwise return NULL. |
| 85 | * |
msarett | 7d5105c | 2015-12-02 07:02:41 -0800 | [diff] [blame] | 86 | * If the SkPngChunkReader is not NULL then: |
| 87 | * If the image is not a PNG, the SkPngChunkReader will be ignored. |
| 88 | * If the image is a PNG, the SkPngChunkReader will be reffed. |
| 89 | * If the PNG has unknown chunks, the SkPngChunkReader will be used |
| 90 | * to handle these chunks. SkPngChunkReader will be called to read |
| 91 | * any unknown chunk at any point during the creation of the codec |
| 92 | * or the decode. Note that if SkPngChunkReader fails to read a |
| 93 | * chunk, this could result in a failure to create the codec or a |
| 94 | * failure to decode the image. |
| 95 | * If the PNG does not contain unknown chunks, the SkPngChunkReader |
| 96 | * will not be used or modified. |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 97 | * |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 98 | * Will take a ref if it returns a codec, else will not affect the data. |
| 99 | */ |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 100 | static SkCodec* NewFromData(SkData*, SkPngChunkReader* = NULL); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 101 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 102 | virtual ~SkCodec(); |
| 103 | |
| 104 | /** |
| 105 | * Return the ImageInfo associated with this codec. |
| 106 | */ |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 107 | const SkImageInfo& getInfo() const { return fSrcInfo; } |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 108 | |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 109 | const SkEncodedInfo& getEncodedInfo() const { return fEncodedInfo; } |
| 110 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 111 | /** |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 112 | * Returns the color space associated with the codec. |
| 113 | * Does not affect ownership. |
| 114 | * Might be NULL. |
| 115 | */ |
msarett | ad8bcfe | 2016-03-07 07:09:03 -0800 | [diff] [blame] | 116 | SkColorSpace* getColorSpace() const { return fColorSpace.get(); } |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 117 | |
msarett | 0e6274f | 2016-03-21 08:04:40 -0700 | [diff] [blame] | 118 | enum Origin { |
| 119 | kTopLeft_Origin = 1, // Default |
| 120 | kTopRight_Origin = 2, // Reflected across y-axis |
| 121 | kBottomRight_Origin = 3, // Rotated 180 |
| 122 | kBottomLeft_Origin = 4, // Reflected across x-axis |
| 123 | kLeftTop_Origin = 5, // Reflected across x-axis, Rotated 90 CCW |
| 124 | kRightTop_Origin = 6, // Rotated 90 CW |
| 125 | kRightBottom_Origin = 7, // Reflected across x-axis, Rotated 90 CW |
| 126 | kLeftBottom_Origin = 8, // Rotated 90 CCW |
| 127 | kDefault_Origin = kTopLeft_Origin, |
| 128 | kLast_Origin = kLeftBottom_Origin, |
| 129 | }; |
| 130 | |
| 131 | /** |
| 132 | * Returns the image orientation stored in the EXIF data. |
| 133 | * If there is no EXIF data, or if we cannot read the EXIF data, returns kTopLeft. |
| 134 | */ |
| 135 | Origin getOrigin() const { return fOrigin; } |
| 136 | |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 137 | /** |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 138 | * Return a size that approximately supports the desired scale factor. |
| 139 | * The codec may not be able to scale efficiently to the exact scale |
| 140 | * factor requested, so return a size that approximates that scale. |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 141 | * The returned value is the codec's suggestion for the closest valid |
| 142 | * scale that it can natively support |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 143 | */ |
scroggo | fffeede | 2015-03-18 10:50:37 -0700 | [diff] [blame] | 144 | SkISize getScaledDimensions(float desiredScale) const { |
msarett | b32758a | 2015-08-18 13:22:46 -0700 | [diff] [blame] | 145 | // Negative and zero scales are errors. |
| 146 | SkASSERT(desiredScale > 0.0f); |
| 147 | if (desiredScale <= 0.0f) { |
| 148 | return SkISize::Make(0, 0); |
| 149 | } |
| 150 | |
| 151 | // Upscaling is not supported. Return the original size if the client |
| 152 | // requests an upscale. |
| 153 | if (desiredScale >= 1.0f) { |
| 154 | return this->getInfo().dimensions(); |
| 155 | } |
scroggo | fffeede | 2015-03-18 10:50:37 -0700 | [diff] [blame] | 156 | return this->onGetScaledDimensions(desiredScale); |
| 157 | } |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 158 | |
scroggo | 1dd3ea9 | 2015-03-20 11:55:55 -0700 | [diff] [blame] | 159 | /** |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 160 | * Return (via desiredSubset) a subset which can decoded from this codec, |
| 161 | * or false if this codec cannot decode subsets or anything similar to |
| 162 | * desiredSubset. |
| 163 | * |
| 164 | * @param desiredSubset In/out parameter. As input, a desired subset of |
| 165 | * the original bounds (as specified by getInfo). If true is returned, |
| 166 | * desiredSubset may have been modified to a subset which is |
| 167 | * supported. Although a particular change may have been made to |
| 168 | * desiredSubset to create something supported, it is possible other |
| 169 | * changes could result in a valid subset. |
| 170 | * If false is returned, desiredSubset's value is undefined. |
| 171 | * @return true if this codec supports decoding desiredSubset (as |
| 172 | * returned, potentially modified) |
| 173 | */ |
| 174 | bool getValidSubset(SkIRect* desiredSubset) const { |
| 175 | return this->onGetValidSubset(desiredSubset); |
| 176 | } |
| 177 | |
| 178 | /** |
scroggo | 1dd3ea9 | 2015-03-20 11:55:55 -0700 | [diff] [blame] | 179 | * Format of the encoded data. |
| 180 | */ |
| 181 | SkEncodedFormat getEncodedFormat() const { return this->onGetEncodedFormat(); } |
| 182 | |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 183 | /** |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 184 | * Used to describe the result of a call to getPixels(). |
| 185 | * |
| 186 | * Result is the union of possible results from subclasses. |
| 187 | */ |
| 188 | enum Result { |
| 189 | /** |
| 190 | * General return value for success. |
| 191 | */ |
| 192 | kSuccess, |
| 193 | /** |
| 194 | * The input is incomplete. A partial image was generated. |
| 195 | */ |
| 196 | kIncompleteInput, |
| 197 | /** |
| 198 | * The generator cannot convert to match the request, ignoring |
| 199 | * dimensions. |
| 200 | */ |
| 201 | kInvalidConversion, |
| 202 | /** |
| 203 | * The generator cannot scale to requested size. |
| 204 | */ |
| 205 | kInvalidScale, |
| 206 | /** |
| 207 | * Parameters (besides info) are invalid. e.g. NULL pixels, rowBytes |
| 208 | * too small, etc. |
| 209 | */ |
| 210 | kInvalidParameters, |
| 211 | /** |
| 212 | * The input did not contain a valid image. |
| 213 | */ |
| 214 | kInvalidInput, |
| 215 | /** |
| 216 | * Fulfilling this request requires rewinding the input, which is not |
| 217 | * supported for this input. |
| 218 | */ |
| 219 | kCouldNotRewind, |
| 220 | /** |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 221 | * This method is not implemented by this codec. |
| 222 | * FIXME: Perhaps this should be kUnsupported? |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 223 | */ |
| 224 | kUnimplemented, |
| 225 | }; |
| 226 | |
| 227 | /** |
| 228 | * Whether or not the memory passed to getPixels is zero initialized. |
| 229 | */ |
| 230 | enum ZeroInitialized { |
| 231 | /** |
| 232 | * The memory passed to getPixels is zero initialized. The SkCodec |
| 233 | * may take advantage of this by skipping writing zeroes. |
| 234 | */ |
| 235 | kYes_ZeroInitialized, |
| 236 | /** |
| 237 | * The memory passed to getPixels has not been initialized to zero, |
| 238 | * so the SkCodec must write all zeroes to memory. |
| 239 | * |
| 240 | * This is the default. It will be used if no Options struct is used. |
| 241 | */ |
| 242 | kNo_ZeroInitialized, |
| 243 | }; |
| 244 | |
| 245 | /** |
| 246 | * Additional options to pass to getPixels. |
| 247 | */ |
| 248 | struct Options { |
| 249 | Options() |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 250 | : fZeroInitialized(kNo_ZeroInitialized) |
| 251 | , fSubset(NULL) |
| 252 | {} |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 253 | |
| 254 | ZeroInitialized fZeroInitialized; |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 255 | /** |
| 256 | * If not NULL, represents a subset of the original image to decode. |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 257 | * Must be within the bounds returned by getInfo(). |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 258 | * If the EncodedFormat is kWEBP_SkEncodedFormat (the only one which |
| 259 | * currently supports subsets), the top and left values must be even. |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 260 | * |
scroggo | 26694c3 | 2016-06-01 12:08:23 -0700 | [diff] [blame] | 261 | * In getPixels, we will attempt to decode the exact rectangular |
| 262 | * subset specified by fSubset. |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 263 | * |
| 264 | * In a scanline decode, it does not make sense to specify a subset |
| 265 | * top or subset height, since the client already controls which rows |
| 266 | * to get and which rows to skip. During scanline decodes, we will |
| 267 | * require that the subset top be zero and the subset height be equal |
| 268 | * to the full height. We will, however, use the values of |
| 269 | * subset left and subset width to decode partial scanlines on calls |
| 270 | * to getScanlines(). |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 271 | */ |
| 272 | SkIRect* fSubset; |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 273 | }; |
| 274 | |
| 275 | /** |
| 276 | * Decode into the given pixels, a block of memory of size at |
| 277 | * least (info.fHeight - 1) * rowBytes + (info.fWidth * |
| 278 | * bytesPerPixel) |
| 279 | * |
| 280 | * Repeated calls to this function should give the same results, |
| 281 | * allowing the PixelRef to be immutable. |
| 282 | * |
| 283 | * @param info A description of the format (config, size) |
| 284 | * expected by the caller. This can simply be identical |
| 285 | * to the info returned by getInfo(). |
| 286 | * |
| 287 | * This contract also allows the caller to specify |
| 288 | * different output-configs, which the implementation can |
| 289 | * decide to support or not. |
| 290 | * |
| 291 | * A size that does not match getInfo() implies a request |
| 292 | * to scale. If the generator cannot perform this scale, |
| 293 | * it will return kInvalidScale. |
| 294 | * |
| 295 | * If info is kIndex8_SkColorType, then the caller must provide storage for up to 256 |
| 296 | * SkPMColor values in ctable. On success the generator must copy N colors into that storage, |
| 297 | * (where N is the logical number of table entries) and set ctableCount to N. |
| 298 | * |
| 299 | * If info is not kIndex8_SkColorType, then the last two parameters may be NULL. If ctableCount |
| 300 | * is not null, it will be set to 0. |
| 301 | * |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 302 | * If a scanline decode is in progress, scanline mode will end, requiring the client to call |
| 303 | * startScanlineDecode() in order to return to decoding scanlines. |
| 304 | * |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 305 | * @return Result kSuccess, or another value explaining the type of failure. |
| 306 | */ |
| 307 | Result getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, const Options*, |
| 308 | SkPMColor ctable[], int* ctableCount); |
| 309 | |
| 310 | /** |
| 311 | * Simplified version of getPixels() that asserts that info is NOT kIndex8_SkColorType and |
| 312 | * uses the default Options. |
| 313 | */ |
| 314 | Result getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes); |
| 315 | |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 316 | /** |
| 317 | * If decoding to YUV is supported, this returns true. Otherwise, this |
| 318 | * returns false and does not modify any of the parameters. |
| 319 | * |
| 320 | * @param sizeInfo Output parameter indicating the sizes and required |
| 321 | * allocation widths of the Y, U, and V planes. |
| 322 | * @param colorSpace Output parameter. If non-NULL this is set to kJPEG, |
| 323 | * otherwise this is ignored. |
| 324 | */ |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 325 | bool queryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const { |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 326 | if (nullptr == sizeInfo) { |
| 327 | return false; |
| 328 | } |
| 329 | |
| 330 | return this->onQueryYUV8(sizeInfo, colorSpace); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Returns kSuccess, or another value explaining the type of failure. |
| 335 | * This always attempts to perform a full decode. If the client only |
| 336 | * wants size, it should call queryYUV8(). |
| 337 | * |
| 338 | * @param sizeInfo Needs to exactly match the values returned by the |
| 339 | * query, except the WidthBytes may be larger than the |
| 340 | * recommendation (but not smaller). |
| 341 | * @param planes Memory for each of the Y, U, and V planes. |
| 342 | */ |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 343 | Result getYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) { |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 344 | if (nullptr == planes || nullptr == planes[0] || nullptr == planes[1] || |
| 345 | nullptr == planes[2]) { |
| 346 | return kInvalidInput; |
| 347 | } |
| 348 | |
| 349 | if (!this->rewindIfNeeded()) { |
| 350 | return kCouldNotRewind; |
| 351 | } |
| 352 | |
| 353 | return this->onGetYUV8Planes(sizeInfo, planes); |
| 354 | } |
| 355 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 356 | /** |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 357 | * The remaining functions revolve around decoding scanlines. |
| 358 | */ |
| 359 | |
| 360 | /** |
| 361 | * Prepare for a scanline decode with the specified options. |
| 362 | * |
| 363 | * After this call, this class will be ready to decode the first scanline. |
| 364 | * |
| 365 | * This must be called in order to call getScanlines or skipScanlines. |
| 366 | * |
| 367 | * This may require rewinding the stream. |
| 368 | * |
| 369 | * Not all SkCodecs support this. |
| 370 | * |
| 371 | * @param dstInfo Info of the destination. If the dimensions do not match |
| 372 | * those of getInfo, this implies a scale. |
| 373 | * @param options Contains decoding options, including if memory is zero |
| 374 | * initialized. |
| 375 | * @param ctable A pointer to a color table. When dstInfo.colorType() is |
| 376 | * kIndex8, this should be non-NULL and have enough storage for 256 |
| 377 | * colors. The color table will be populated after decoding the palette. |
| 378 | * @param ctableCount A pointer to the size of the color table. When |
| 379 | * dstInfo.colorType() is kIndex8, this should be non-NULL. It will |
| 380 | * be modified to the true size of the color table (<= 256) after |
| 381 | * decoding the palette. |
| 382 | * @return Enum representing success or reason for failure. |
| 383 | */ |
| 384 | Result startScanlineDecode(const SkImageInfo& dstInfo, const SkCodec::Options* options, |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 385 | SkPMColor ctable[], int* ctableCount); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 386 | |
| 387 | /** |
| 388 | * Simplified version of startScanlineDecode() that asserts that info is NOT |
| 389 | * kIndex8_SkColorType and uses the default Options. |
| 390 | */ |
| 391 | Result startScanlineDecode(const SkImageInfo& dstInfo); |
| 392 | |
| 393 | /** |
| 394 | * Write the next countLines scanlines into dst. |
| 395 | * |
| 396 | * Not valid to call before calling startScanlineDecode(). |
| 397 | * |
| 398 | * @param dst Must be non-null, and large enough to hold countLines |
| 399 | * scanlines of size rowBytes. |
| 400 | * @param countLines Number of lines to write. |
| 401 | * @param rowBytes Number of bytes per row. Must be large enough to hold |
| 402 | * a scanline based on the SkImageInfo used to create this object. |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 403 | * @return the number of lines successfully decoded. If this value is |
| 404 | * less than countLines, this will fill the remaining lines with a |
| 405 | * default value. |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 406 | */ |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 407 | int getScanlines(void* dst, int countLines, size_t rowBytes); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 408 | |
| 409 | /** |
| 410 | * Skip count scanlines. |
| 411 | * |
| 412 | * Not valid to call before calling startScanlineDecode(). |
| 413 | * |
| 414 | * The default version just calls onGetScanlines and discards the dst. |
| 415 | * NOTE: If skipped lines are the only lines with alpha, this default |
| 416 | * will make reallyHasAlpha return true, when it could have returned |
| 417 | * false. |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 418 | * |
| 419 | * @return true if the scanlines were successfully skipped |
| 420 | * false on failure, possible reasons for failure include: |
| 421 | * An incomplete input image stream. |
| 422 | * Calling this function before calling startScanlineDecode(). |
| 423 | * If countLines is less than zero or so large that it moves |
| 424 | * the current scanline past the end of the image. |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 425 | */ |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 426 | bool skipScanlines(int countLines); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 427 | |
| 428 | /** |
| 429 | * The order in which rows are output from the scanline decoder is not the |
| 430 | * same for all variations of all image types. This explains the possible |
| 431 | * output row orderings. |
| 432 | */ |
| 433 | enum SkScanlineOrder { |
| 434 | /* |
| 435 | * By far the most common, this indicates that the image can be decoded |
| 436 | * reliably using the scanline decoder, and that rows will be output in |
| 437 | * the logical order. |
| 438 | */ |
| 439 | kTopDown_SkScanlineOrder, |
| 440 | |
| 441 | /* |
| 442 | * This indicates that the scanline decoder reliably outputs rows, but |
| 443 | * they will be returned in reverse order. If the scanline format is |
| 444 | * kBottomUp, the nextScanline() API can be used to determine the actual |
| 445 | * y-coordinate of the next output row, but the client is not forced |
| 446 | * to take advantage of this, given that it's not too tough to keep |
| 447 | * track independently. |
| 448 | * |
| 449 | * For full image decodes, it is safe to get all of the scanlines at |
| 450 | * once, since the decoder will handle inverting the rows as it |
| 451 | * decodes. |
| 452 | * |
| 453 | * For subset decodes and sampling, it is simplest to get and skip |
| 454 | * scanlines one at a time, using the nextScanline() API. It is |
| 455 | * possible to ask for larger chunks at a time, but this should be used |
| 456 | * with caution. As with full image decodes, the decoder will handle |
| 457 | * inverting the requested rows, but rows will still be delivered |
| 458 | * starting from the bottom of the image. |
| 459 | * |
| 460 | * Upside down bmps are an example. |
| 461 | */ |
| 462 | kBottomUp_SkScanlineOrder, |
| 463 | |
| 464 | /* |
| 465 | * This indicates that the scanline decoder reliably outputs rows, but |
| 466 | * they will not be in logical order. If the scanline format is |
| 467 | * kOutOfOrder, the nextScanline() API should be used to determine the |
| 468 | * actual y-coordinate of the next output row. |
| 469 | * |
| 470 | * For this scanline ordering, it is advisable to get and skip |
| 471 | * scanlines one at a time. |
| 472 | * |
| 473 | * Interlaced gifs are an example. |
| 474 | */ |
| 475 | kOutOfOrder_SkScanlineOrder, |
scroggo | 26694c3 | 2016-06-01 12:08:23 -0700 | [diff] [blame] | 476 | |
| 477 | /* |
| 478 | * Indicates that the entire image must be decoded in order to output |
| 479 | * any amount of scanlines. In this case, it is a REALLY BAD IDEA to |
| 480 | * request scanlines 1-by-1 or in small chunks. The client should |
| 481 | * determine which scanlines are needed and ask for all of them in |
| 482 | * a single call to getScanlines(). |
| 483 | * |
| 484 | * Interlaced pngs are an example. |
| 485 | */ |
| 486 | kNone_SkScanlineOrder, |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 487 | }; |
| 488 | |
| 489 | /** |
| 490 | * An enum representing the order in which scanlines will be returned by |
| 491 | * the scanline decoder. |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 492 | * |
| 493 | * This is undefined before startScanlineDecode() is called. |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 494 | */ |
| 495 | SkScanlineOrder getScanlineOrder() const { return this->onGetScanlineOrder(); } |
| 496 | |
| 497 | /** |
| 498 | * Returns the y-coordinate of the next row to be returned by the scanline |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 499 | * decoder. |
| 500 | * |
| 501 | * This will equal fCurrScanline, except in the case of strangely |
| 502 | * encoded image types (bottom-up bmps, interlaced gifs). |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 503 | * |
| 504 | * Results are undefined when not in scanline decoding mode. |
| 505 | */ |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 506 | int nextScanline() const { return this->outputScanline(fCurrScanline); } |
| 507 | |
| 508 | /** |
msarett | cb0d5c9 | 2015-12-03 12:23:43 -0800 | [diff] [blame] | 509 | * Returns the output y-coordinate of the row that corresponds to an input |
| 510 | * y-coordinate. The input y-coordinate represents where the scanline |
| 511 | * is located in the encoded data. |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 512 | * |
| 513 | * This will equal inputScanline, except in the case of strangely |
| 514 | * encoded image types (bottom-up bmps, interlaced gifs). |
| 515 | */ |
| 516 | int outputScanline(int inputScanline) const; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 517 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 518 | protected: |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 519 | /** |
| 520 | * Takes ownership of SkStream* |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 521 | */ |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 522 | SkCodec(int width, |
| 523 | int height, |
| 524 | const SkEncodedInfo&, |
msarett | 0e6274f | 2016-03-21 08:04:40 -0700 | [diff] [blame] | 525 | SkStream*, |
| 526 | sk_sp<SkColorSpace> = nullptr, |
| 527 | Origin = kTopLeft_Origin); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 528 | |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 529 | virtual SkISize onGetScaledDimensions(float /*desiredScale*/) const { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 530 | // By default, scaling is not supported. |
halcanary | b880d7f | 2015-03-26 06:29:03 -0700 | [diff] [blame] | 531 | return this->getInfo().dimensions(); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 532 | } |
| 533 | |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 534 | // FIXME: What to do about subsets?? |
| 535 | /** |
| 536 | * Subclasses should override if they support dimensions other than the |
| 537 | * srcInfo's. |
| 538 | */ |
| 539 | virtual bool onDimensionsSupported(const SkISize&) { |
| 540 | return false; |
| 541 | } |
| 542 | |
scroggo | 1dd3ea9 | 2015-03-20 11:55:55 -0700 | [diff] [blame] | 543 | virtual SkEncodedFormat onGetEncodedFormat() const = 0; |
| 544 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 545 | /** |
| 546 | * @param rowsDecoded When the encoded image stream is incomplete, this function |
| 547 | * will return kIncompleteInput and rowsDecoded will be set to |
| 548 | * the number of scanlines that were successfully decoded. |
| 549 | * This will allow getPixels() to fill the uninitialized memory. |
| 550 | */ |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 551 | virtual Result onGetPixels(const SkImageInfo& info, |
| 552 | void* pixels, size_t rowBytes, const Options&, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 553 | SkPMColor ctable[], int* ctableCount, |
| 554 | int* rowsDecoded) = 0; |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 555 | |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 556 | virtual bool onQueryYUV8(SkYUVSizeInfo*, SkYUVColorSpace*) const { |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 557 | return false; |
| 558 | } |
| 559 | |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 560 | virtual Result onGetYUV8Planes(const SkYUVSizeInfo&, void*[3] /*planes*/) { |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 561 | return kUnimplemented; |
| 562 | } |
| 563 | |
| 564 | virtual bool onGetValidSubset(SkIRect* /*desiredSubset*/) const { |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 565 | // By default, subsets are not supported. |
| 566 | return false; |
| 567 | } |
| 568 | |
msarett | 90c4d5f | 2015-12-10 13:09:24 -0800 | [diff] [blame] | 569 | /** |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 570 | * If the stream was previously read, attempt to rewind. |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 571 | * |
| 572 | * If the stream needed to be rewound, call onRewind. |
| 573 | * @returns true if the codec is at the right position and can be used. |
| 574 | * false if there was a failure to rewind. |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 575 | * |
scroggo | 3a7701c | 2015-09-30 09:15:14 -0700 | [diff] [blame] | 576 | * This is called by getPixels() and start(). Subclasses may call if they |
| 577 | * need to rewind at another time. |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 578 | */ |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 579 | bool SK_WARN_UNUSED_RESULT rewindIfNeeded(); |
| 580 | |
| 581 | /** |
| 582 | * Called by rewindIfNeeded, if the stream needed to be rewound. |
| 583 | * |
| 584 | * Subclasses should do any set up needed after a rewind. |
| 585 | */ |
| 586 | virtual bool onRewind() { |
| 587 | return true; |
| 588 | } |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 589 | |
msarett | c0e80c1 | 2015-07-01 06:50:35 -0700 | [diff] [blame] | 590 | /** |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 591 | * On an incomplete input, getPixels() and getScanlines() will fill any uninitialized |
| 592 | * scanlines. This allows the subclass to indicate what value to fill with. |
| 593 | * |
| 594 | * @param colorType Destination color type. |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 595 | * @return The value with which to fill uninitialized pixels. |
| 596 | * |
| 597 | * Note that we can interpret the return value as an SkPMColor, a 16-bit 565 color, |
| 598 | * an 8-bit gray color, or an 8-bit index into a color table, depending on the color |
| 599 | * type. |
| 600 | */ |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 601 | uint32_t getFillValue(SkColorType colorType) const { |
| 602 | return this->onGetFillValue(colorType); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /** |
| 606 | * Some subclasses will override this function, but this is a useful default for the color |
| 607 | * types that we support. Note that for color types that do not use the full 32-bits, |
| 608 | * we will simply take the low bits of the fill value. |
| 609 | * |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 610 | * kN32_SkColorType: Transparent or Black, depending on the src alpha type |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 611 | * kRGB_565_SkColorType: Black |
| 612 | * kGray_8_SkColorType: Black |
| 613 | * kIndex_8_SkColorType: First color in color table |
| 614 | */ |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 615 | virtual uint32_t onGetFillValue(SkColorType /*colorType*/) const { |
| 616 | return kOpaque_SkAlphaType == fSrcInfo.alphaType() ? SK_ColorBLACK : SK_ColorTRANSPARENT; |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | /** |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 620 | * Get method for the input stream |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 621 | */ |
| 622 | SkStream* stream() { |
| 623 | return fStream.get(); |
| 624 | } |
| 625 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 626 | /** |
| 627 | * The remaining functions revolve around decoding scanlines. |
| 628 | */ |
| 629 | |
| 630 | /** |
| 631 | * Most images types will be kTopDown and will not need to override this function. |
| 632 | */ |
| 633 | virtual SkScanlineOrder onGetScanlineOrder() const { return kTopDown_SkScanlineOrder; } |
| 634 | |
scroggo | 26694c3 | 2016-06-01 12:08:23 -0700 | [diff] [blame] | 635 | /** |
| 636 | * Update the current scanline. Used by interlaced png. |
| 637 | */ |
| 638 | void updateCurrScanline(int newY) { fCurrScanline = newY; } |
| 639 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 640 | const SkImageInfo& dstInfo() const { return fDstInfo; } |
| 641 | |
| 642 | const SkCodec::Options& options() const { return fOptions; } |
| 643 | |
msarett | cb0d5c9 | 2015-12-03 12:23:43 -0800 | [diff] [blame] | 644 | /** |
| 645 | * Returns the number of scanlines that have been decoded so far. |
| 646 | * This is unaffected by the SkScanlineOrder. |
| 647 | * |
| 648 | * Returns -1 if we have not started a scanline decode. |
| 649 | */ |
| 650 | int currScanline() const { return fCurrScanline; } |
| 651 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 652 | virtual int onOutputScanline(int inputScanline) const; |
| 653 | |
msarett | 9876ac5 | 2016-06-01 14:47:18 -0700 | [diff] [blame^] | 654 | /** |
| 655 | * Used for testing with qcms. |
| 656 | * FIXME: Remove this when we are done comparing with qcms. |
| 657 | */ |
| 658 | virtual sk_sp<SkData> getICCData() const { return nullptr; } |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 659 | private: |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 660 | const SkEncodedInfo fEncodedInfo; |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 661 | const SkImageInfo fSrcInfo; |
| 662 | SkAutoTDelete<SkStream> fStream; |
| 663 | bool fNeedsRewind; |
msarett | ad8bcfe | 2016-03-07 07:09:03 -0800 | [diff] [blame] | 664 | sk_sp<SkColorSpace> fColorSpace; |
msarett | 0e6274f | 2016-03-21 08:04:40 -0700 | [diff] [blame] | 665 | const Origin fOrigin; |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 666 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 667 | // These fields are only meaningful during scanline decodes. |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 668 | SkImageInfo fDstInfo; |
| 669 | SkCodec::Options fOptions; |
| 670 | int fCurrScanline; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 671 | |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 672 | /** |
| 673 | * Return whether these dimensions are supported as a scale. |
| 674 | * |
| 675 | * The codec may choose to cache the information about scale and subset. |
| 676 | * Either way, the same information will be passed to onGetPixels/onStart |
| 677 | * on success. |
| 678 | * |
| 679 | * This must return true for a size returned from getScaledDimensions. |
| 680 | */ |
| 681 | bool dimensionsSupported(const SkISize& dim) { |
| 682 | return dim == fSrcInfo.dimensions() || this->onDimensionsSupported(dim); |
| 683 | } |
| 684 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 685 | // Methods for scanline decoding. |
msarett | 33bee09 | 2015-11-11 12:43:07 -0800 | [diff] [blame] | 686 | virtual SkCodec::Result onStartScanlineDecode(const SkImageInfo& /*dstInfo*/, |
| 687 | const SkCodec::Options& /*options*/, SkPMColor* /*ctable*/, int* /*ctableCount*/) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 688 | return kUnimplemented; |
| 689 | } |
| 690 | |
msarett | 9b9497e | 2016-02-11 13:29:36 -0800 | [diff] [blame] | 691 | virtual bool onSkipScanlines(int /*countLines*/) { return false; } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 692 | |
msarett | 33bee09 | 2015-11-11 12:43:07 -0800 | [diff] [blame] | 693 | virtual int onGetScanlines(void* /*dst*/, int /*countLines*/, size_t /*rowBytes*/) { return 0; } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 694 | |
| 695 | /** |
| 696 | * On an incomplete decode, getPixels() and getScanlines() will call this function |
| 697 | * to fill any uinitialized memory. |
| 698 | * |
| 699 | * @param dstInfo Contains the destination color type |
| 700 | * Contains the destination alpha type |
| 701 | * Contains the destination width |
| 702 | * The height stored in this info is unused |
| 703 | * @param dst Pointer to the start of destination pixel memory |
| 704 | * @param rowBytes Stride length in destination pixel memory |
| 705 | * @param zeroInit Indicates if memory is zero initialized |
| 706 | * @param linesRequested Number of lines that the client requested |
| 707 | * @param linesDecoded Number of lines that were successfully decoded |
| 708 | */ |
| 709 | void fillIncompleteImage(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, |
| 710 | ZeroInitialized zeroInit, int linesRequested, int linesDecoded); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 711 | |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 712 | /** |
| 713 | * Return an object which will allow forcing scanline decodes to sample in X. |
| 714 | * |
| 715 | * May create a sampler, if one is not currently being used. Otherwise, does |
| 716 | * not affect ownership. |
| 717 | * |
| 718 | * Only valid during scanline decoding. |
| 719 | */ |
msarett | 33bee09 | 2015-11-11 12:43:07 -0800 | [diff] [blame] | 720 | virtual SkSampler* getSampler(bool /*createIfNecessary*/) { return nullptr; } |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 721 | |
msarett | 9876ac5 | 2016-06-01 14:47:18 -0700 | [diff] [blame^] | 722 | // For testing with qcms |
| 723 | // FIXME: Remove this when we are done comparing with qcms. |
| 724 | friend class DM::ColorCodecSrc; |
| 725 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 726 | friend class SkSampledCodec; |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 727 | friend class SkIcoCodec; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 728 | }; |
| 729 | #endif // SkCodec_DEFINED |