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