Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 #ifndef SkImageDecoder_DEFINED | 10 #ifndef SkImageDecoder_DEFINED |
| 11 #define SkImageDecoder_DEFINED | 11 #define SkImageDecoder_DEFINED |
| 12 | 12 |
| 13 #include "SkBitmap.h" | 13 #include "SkBitmap.h" |
| 14 #include "SkBitmapFactory.h" | 14 #include "SkBitmapFactory.h" |
| 15 #include "SkImage.h" | 15 #include "SkImage.h" |
| 16 #include "SkRect.h" | |
| 16 #include "SkRefCnt.h" | 17 #include "SkRefCnt.h" |
| 17 | 18 |
| 18 class SkStream; | 19 class SkStream; |
| 19 | 20 |
| 20 /** \class SkImageDecoder | 21 /** \class SkImageDecoder |
| 21 | 22 |
| 22 Base class for decoding compressed images into a SkBitmap | 23 Base class for decoding compressed images into a SkBitmap |
| 23 */ | 24 */ |
| 24 class SkImageDecoder { | 25 class SkImageDecoder { |
| 25 public: | 26 public: |
| 26 virtual ~SkImageDecoder(); | 27 virtual ~SkImageDecoder(); |
| 27 | 28 |
| 29 // Should be consistent with kFormatName | |
| 28 enum Format { | 30 enum Format { |
| 29 kUnknown_Format, | 31 kUnknown_Format, |
| 30 kBMP_Format, | 32 kBMP_Format, |
| 31 kGIF_Format, | 33 kGIF_Format, |
| 32 kICO_Format, | 34 kICO_Format, |
| 33 kJPEG_Format, | 35 kJPEG_Format, |
| 34 kPNG_Format, | 36 kPNG_Format, |
| 35 kWBMP_Format, | 37 kWBMP_Format, |
| 38 kWEBP_Format, | |
| 36 | 39 |
| 37 kLastKnownFormat = kWBMP_Format | 40 kLastKnownFormat = kWEBP_Format |
| 38 }; | 41 }; |
| 39 | 42 |
| 43 /** Contains the image format name. | |
| 44 * This should be consistent with Format. | |
| 45 * | |
| 46 * The format name gives a more meaningful error message than enum. | |
| 47 */ | |
|
robertphillips
2013/03/11 18:25:55
Can we use SK_ARRAY_COUNT here or a kNumFormats co
djsollen
2013/03/11 19:43:24
Done.
| |
| 48 static const char *kFormatName[8]; | |
| 49 | |
| 40 /** Return the compressed data's format (see Format enum) | 50 /** Return the compressed data's format (see Format enum) |
| 41 */ | 51 */ |
| 42 virtual Format getFormat() const; | 52 virtual Format getFormat() const; |
| 43 | 53 |
| 54 /** Return the compressed data's format name. | |
| 55 */ | |
|
robertphillips
2013/03/11 18:25:55
this->
| |
| 56 const char* getFormatName() const { return kFormatName[getFormat()]; } | |
| 57 | |
| 44 /** Returns true if the decoder should try to dither the resulting image. | 58 /** Returns true if the decoder should try to dither the resulting image. |
| 45 The default setting is true. | 59 The default setting is true. |
| 46 */ | 60 */ |
| 47 bool getDitherImage() const { return fDitherImage; } | 61 bool getDitherImage() const { return fDitherImage; } |
| 48 | 62 |
| 49 /** Set to true if the the decoder should try to dither the resulting image. | 63 /** Set to true if the the decoder should try to dither the resulting image. |
| 50 The default setting is true. | 64 The default setting is true. |
| 51 */ | 65 */ |
| 52 void setDitherImage(bool dither) { fDitherImage = dither; } | 66 void setDitherImage(bool dither) { fDitherImage = dither; } |
| 53 | 67 |
| 68 /** Returns true if the decoder should try to decode the | |
| 69 resulting image to a higher quality even at the expense of | |
| 70 the decoding speed. | |
| 71 */ | |
| 72 bool getPreferQualityOverSpeed() const { return fPreferQualityOverSpeed; } | |
| 73 | |
| 74 /** Set to true if the the decoder should try to decode the | |
| 75 resulting image to a higher quality even at the expense of | |
| 76 the decoding speed. | |
| 77 */ | |
| 78 void setPreferQualityOverSpeed(bool qualityOverSpeed) { | |
| 79 fPreferQualityOverSpeed = qualityOverSpeed; | |
| 80 } | |
| 81 | |
| 54 /** \class Peeker | 82 /** \class Peeker |
| 55 | 83 |
| 56 Base class for optional callbacks to retrieve meta/chunk data out of | 84 Base class for optional callbacks to retrieve meta/chunk data out of |
| 57 an image as it is being decoded. | 85 an image as it is being decoded. |
| 58 */ | 86 */ |
| 59 class Peeker : public SkRefCnt { | 87 class Peeker : public SkRefCnt { |
| 60 public: | 88 public: |
| 61 SK_DECLARE_INST_COUNT(Peeker) | 89 SK_DECLARE_INST_COUNT(Peeker) |
| 62 | 90 |
| 63 /** Return true to continue decoding, or false to indicate an error, whi ch | 91 /** Return true to continue decoding, or false to indicate an error, whi ch |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 168 which will allocated a pixelRef. To access the pixel memory, the codec | 196 which will allocated a pixelRef. To access the pixel memory, the codec |
| 169 needs to call lockPixels/unlockPixels on the | 197 needs to call lockPixels/unlockPixels on the |
| 170 bitmap. It can then set the pixels with the decompressed image. | 198 bitmap. It can then set the pixels with the decompressed image. |
| 171 * If the image cannot be decompressed, return false. After the | 199 * If the image cannot be decompressed, return false. After the |
| 172 * decoding, the function converts the decoded config in bitmap | 200 * decoding, the function converts the decoded config in bitmap |
| 173 * to pref if possible. Whether a conversion is feasible is | 201 * to pref if possible. Whether a conversion is feasible is |
| 174 * tested by Bitmap::canCopyTo(pref). | 202 * tested by Bitmap::canCopyTo(pref). |
| 175 | 203 |
| 176 note: document use of Allocator, Peeker and Chooser | 204 note: document use of Allocator, Peeker and Chooser |
| 177 */ | 205 */ |
| 178 bool decode(SkStream*, SkBitmap* bitmap, SkBitmap::Config pref, Mode); | 206 bool decode(SkStream*, SkBitmap* bitmap, SkBitmap::Config pref, Mode, bool r euseBitmap = false); |
| 179 bool decode(SkStream* stream, SkBitmap* bitmap, Mode mode) { | 207 bool decode(SkStream* stream, SkBitmap* bitmap, Mode mode, bool reuseBitmap = false) { |
| 180 return this->decode(stream, bitmap, SkBitmap::kNo_Config, mode); | 208 return this->decode(stream, bitmap, SkBitmap::kNo_Config, mode, reuseBit map); |
| 181 } | 209 } |
| 182 | 210 |
| 211 /** | |
| 212 * Given a stream, build an index for doing tile-based decode. | |
| 213 * The built index will be saved in the decoder, and the image size will | |
| 214 * be returned in width and height. | |
| 215 * | |
| 216 * Return true for success or false on failure. | |
| 217 */ | |
| 218 virtual bool buildTileIndex(SkStream*, int *width, int *height); | |
| 219 | |
| 220 /** | |
| 221 * Decode a rectangle region in the image specified by rect. | |
| 222 * The method can only be called after buildTileIndex(). | |
| 223 * | |
| 224 * Return true for success. | |
| 225 * Return false if the index is never built or failing in decoding. | |
| 226 */ | |
| 227 virtual bool decodeRegion(SkBitmap* bitmap, const SkIRect& rect, | |
| 228 SkBitmap::Config pref); | |
| 229 | |
| 183 /** Given a stream, this will try to find an appropriate decoder object. | 230 /** Given a stream, this will try to find an appropriate decoder object. |
| 184 If none is found, the method returns NULL. | 231 If none is found, the method returns NULL. |
| 185 */ | 232 */ |
| 186 static SkImageDecoder* Factory(SkStream*); | 233 static SkImageDecoder* Factory(SkStream*); |
| 187 | 234 |
| 188 /** Decode the image stored in the specified file, and store the result | 235 /** Decode the image stored in the specified file, and store the result |
| 189 in bitmap. Return true for success or false on failure. | 236 in bitmap. Return true for success or false on failure. |
| 190 | 237 |
| 191 If pref is kNo_Config, then the decoder is free to choose the most natur al | 238 If pref is kNo_Config, then the decoder is free to choose the most natur al |
| 192 config given the image data. If pref something other than kNo_Config, | 239 config given the image data. If pref something other than kNo_Config, |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 289 static void SetDeviceConfig(SkBitmap::Config); | 336 static void SetDeviceConfig(SkBitmap::Config); |
| 290 | 337 |
| 291 /** @cond UNIT_TEST */ | 338 /** @cond UNIT_TEST */ |
| 292 SkDEBUGCODE(static void UnitTest();) | 339 SkDEBUGCODE(static void UnitTest();) |
| 293 /** @endcond */ | 340 /** @endcond */ |
| 294 | 341 |
| 295 protected: | 342 protected: |
| 296 // must be overridden in subclasses. This guy is called by decode(...) | 343 // must be overridden in subclasses. This guy is called by decode(...) |
| 297 virtual bool onDecode(SkStream*, SkBitmap* bitmap, Mode) = 0; | 344 virtual bool onDecode(SkStream*, SkBitmap* bitmap, Mode) = 0; |
| 298 | 345 |
| 346 // If the decoder wants to support tiled based decoding, | |
| 347 // this method must be overridden. This guy is called by buildTileIndex(...) | |
| 348 virtual bool onBuildTileIndex(SkStream*, int *width, int *height) { | |
| 349 return false; | |
| 350 } | |
| 351 | |
| 352 // If the decoder wants to support tiled based decoding, | |
| 353 // this method must be overridden. This guy is called by decodeRegion(...) | |
| 354 virtual bool onDecodeRegion(SkBitmap* bitmap, const SkIRect& rect) { | |
| 355 return false; | |
| 356 } | |
| 357 | |
| 358 /* | |
| 359 * Crop a rectangle from the src Bitmap to the dest Bitmap. src and dst are | |
| 360 * both sampled by sampleSize from an original Bitmap. | |
| 361 * | |
| 362 * @param dst the destination Bitmap. | |
| 363 * @param src the source Bitmap that is sampled by sampleSize from the origi nal | |
| 364 * Bitmap. | |
| 365 * @param sampleSize the sample size that src is sampled from the original B itmap. | |
|
robertphillips
2013/03/11 18:25:55
Btimap -> bitmap
djsollen
2013/03/11 19:43:24
Done.
| |
| 366 * @param (srcX, srcY) the upper-left point of the src Btimap in terms of | |
| 367 * the coordinate in the original Bitmap. | |
| 368 * @param (width, height) the width and height of the unsampled dst. | |
| 369 * @param (dstX, dstY) the upper-left point of the dest Bitmap in terms of | |
| 370 * the coordinate in the original Bitmap. | |
| 371 */ | |
| 372 void cropBitmap(SkBitmap *dst, SkBitmap *src, int sampleSize, | |
| 373 int dstX, int dstY, int width, int height, | |
| 374 int srcX, int srcY); | |
| 375 | |
| 376 | |
| 377 | |
| 299 /** Can be queried from within onDecode, to see if the user (possibly in | 378 /** Can be queried from within onDecode, to see if the user (possibly in |
| 300 a different thread) has requested the decode to cancel. If this returns | 379 a different thread) has requested the decode to cancel. If this returns |
| 301 true, your onDecode() should stop and return false. | 380 true, your onDecode() should stop and return false. |
| 302 Each subclass needs to decide how often it can query this, to balance | 381 Each subclass needs to decide how often it can query this, to balance |
| 303 responsiveness with performance. | 382 responsiveness with performance. |
| 304 | 383 |
| 305 Calling this outside of onDecode() may return undefined values. | 384 Calling this outside of onDecode() may return undefined values. |
| 306 */ | 385 */ |
| 307 | 386 |
| 308 public: | 387 public: |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 339 private: | 418 private: |
| 340 Peeker* fPeeker; | 419 Peeker* fPeeker; |
| 341 Chooser* fChooser; | 420 Chooser* fChooser; |
| 342 SkBitmap::Allocator* fAllocator; | 421 SkBitmap::Allocator* fAllocator; |
| 343 int fSampleSize; | 422 int fSampleSize; |
| 344 SkBitmap::Config fDefaultPref; // use if fUsePrefTable is false | 423 SkBitmap::Config fDefaultPref; // use if fUsePrefTable is false |
| 345 SkBitmap::Config fPrefTable[6]; // use if fUsePrefTable is true | 424 SkBitmap::Config fPrefTable[6]; // use if fUsePrefTable is true |
| 346 bool fDitherImage; | 425 bool fDitherImage; |
| 347 bool fUsePrefTable; | 426 bool fUsePrefTable; |
| 348 mutable bool fShouldCancelDecode; | 427 mutable bool fShouldCancelDecode; |
| 428 bool fPreferQualityOverSpeed; | |
| 349 | 429 |
| 350 // illegal | 430 // illegal |
| 351 SkImageDecoder(const SkImageDecoder&); | 431 SkImageDecoder(const SkImageDecoder&); |
| 352 SkImageDecoder& operator=(const SkImageDecoder&); | 432 SkImageDecoder& operator=(const SkImageDecoder&); |
| 353 }; | 433 }; |
| 354 | 434 |
| 355 /** Calling newDecoder with a stream returns a new matching imagedecoder | 435 /** Calling newDecoder with a stream returns a new matching imagedecoder |
| 356 instance, or NULL if none can be found. The caller must manage its ownership | 436 instance, or NULL if none can be found. The caller must manage its ownership |
| 357 of the stream as usual, calling unref() when it is done, as the returned | 437 of the stream as usual, calling unref() when it is done, as the returned |
| 358 decoder may have called ref() (and if so, the decoder is responsible for | 438 decoder may have called ref() (and if so, the decoder is responsible for |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 389 } | 469 } |
| 390 | 470 |
| 391 // All the decoders known by Skia. Note that, depending on the compiler settings , | 471 // All the decoders known by Skia. Note that, depending on the compiler settings , |
| 392 // not all of these will be available | 472 // not all of these will be available |
| 393 DECLARE_DECODER_CREATOR(BMPImageDecoder); | 473 DECLARE_DECODER_CREATOR(BMPImageDecoder); |
| 394 DECLARE_DECODER_CREATOR(GIFImageDecoder); | 474 DECLARE_DECODER_CREATOR(GIFImageDecoder); |
| 395 DECLARE_DECODER_CREATOR(ICOImageDecoder); | 475 DECLARE_DECODER_CREATOR(ICOImageDecoder); |
| 396 DECLARE_DECODER_CREATOR(JPEGImageDecoder); | 476 DECLARE_DECODER_CREATOR(JPEGImageDecoder); |
| 397 DECLARE_DECODER_CREATOR(PNGImageDecoder); | 477 DECLARE_DECODER_CREATOR(PNGImageDecoder); |
| 398 DECLARE_DECODER_CREATOR(WBMPImageDecoder); | 478 DECLARE_DECODER_CREATOR(WBMPImageDecoder); |
| 479 DECLARE_DECODER_CREATOR(WEBPImageDecoder); | |
| 399 | 480 |
| 400 #endif | 481 #endif |
| OLD | NEW |