| 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 "SkRect.h" |
| 17 #include "SkRefCnt.h" | 17 #include "SkRefCnt.h" |
| 18 #include "SkTypes.h" | 18 #include "SkTypes.h" |
| 19 | 19 |
| 20 class SkStream; | 20 class SkStreamRewindable; |
| 21 | 21 |
| 22 /** \class SkImageDecoder | 22 /** \class SkImageDecoder |
| 23 | 23 |
| 24 Base class for decoding compressed images into a SkBitmap | 24 Base class for decoding compressed images into a SkBitmap |
| 25 */ | 25 */ |
| 26 class SkImageDecoder : public SkNoncopyable { | 26 class SkImageDecoder : public SkNoncopyable { |
| 27 public: | 27 public: |
| 28 virtual ~SkImageDecoder(); | 28 virtual ~SkImageDecoder(); |
| 29 | 29 |
| 30 enum Format { | 30 enum Format { |
| 31 kUnknown_Format, | 31 kUnknown_Format, |
| 32 kBMP_Format, | 32 kBMP_Format, |
| 33 kGIF_Format, | 33 kGIF_Format, |
| 34 kICO_Format, | 34 kICO_Format, |
| 35 kJPEG_Format, | 35 kJPEG_Format, |
| 36 kPNG_Format, | 36 kPNG_Format, |
| 37 kWBMP_Format, | 37 kWBMP_Format, |
| 38 kWEBP_Format, | 38 kWEBP_Format, |
| 39 | 39 |
| 40 kLastKnownFormat = kWEBP_Format, | 40 kLastKnownFormat = kWEBP_Format, |
| 41 }; | 41 }; |
| 42 | 42 |
| 43 /** Return the format of image this decoder can decode. If this decoder can
decode multiple | 43 /** Return the format of image this decoder can decode. If this decoder can
decode multiple |
| 44 formats, kUnknown_Format will be returned. | 44 formats, kUnknown_Format will be returned. |
| 45 */ | 45 */ |
| 46 virtual Format getFormat() const; | 46 virtual Format getFormat() const; |
| 47 | 47 |
| 48 /** Return the format of the SkStream or kUnknown_Format if it cannot be det
ermined. Rewinds the | 48 /** Return the format of the SkStreamRewindable or kUnknown_Format if it can
not be determined. |
| 49 stream before returning. | 49 Rewinds the stream before returning. |
| 50 */ | 50 */ |
| 51 static Format GetStreamFormat(SkStream*); | 51 static Format GetStreamFormat(SkStreamRewindable*); |
| 52 | 52 |
| 53 /** Return a readable string of the Format provided. | 53 /** Return a readable string of the Format provided. |
| 54 */ | 54 */ |
| 55 static const char* GetFormatName(Format); | 55 static const char* GetFormatName(Format); |
| 56 | 56 |
| 57 /** Return a readable string of the value returned by getFormat(). | 57 /** Return a readable string of the value returned by getFormat(). |
| 58 */ | 58 */ |
| 59 const char* getFormatName() const; | 59 const char* getFormatName() const; |
| 60 | 60 |
| 61 /** Returns true if the decoder should try to dither the resulting image. | 61 /** Returns true if the decoder should try to dither the resulting image. |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 used to allocate the pixel memory. A clever allocator can be used | 270 used to allocate the pixel memory. A clever allocator can be used |
| 271 to allocate the memory from a cache, volatile memory, or even from | 271 to allocate the memory from a cache, volatile memory, or even from |
| 272 an existing bitmap's memory. | 272 an existing bitmap's memory. |
| 273 | 273 |
| 274 If a Peeker is installed via setPeeker, it may be used to peek into | 274 If a Peeker is installed via setPeeker, it may be used to peek into |
| 275 meta data during the decode. | 275 meta data during the decode. |
| 276 | 276 |
| 277 If a Chooser is installed via setChooser, it may be used to select | 277 If a Chooser is installed via setChooser, it may be used to select |
| 278 which image to return from a format that contains multiple images. | 278 which image to return from a format that contains multiple images. |
| 279 */ | 279 */ |
| 280 bool decode(SkStream*, SkBitmap* bitmap, SkBitmap::Config pref, Mode); | 280 bool decode(SkStreamRewindable*, SkBitmap* bitmap, SkBitmap::Config pref, Mo
de); |
| 281 bool decode(SkStream* stream, SkBitmap* bitmap, Mode mode) { | 281 bool decode(SkStreamRewindable* stream, SkBitmap* bitmap, Mode mode) { |
| 282 return this->decode(stream, bitmap, SkBitmap::kNo_Config, mode); | 282 return this->decode(stream, bitmap, SkBitmap::kNo_Config, mode); |
| 283 } | 283 } |
| 284 | 284 |
| 285 /** | 285 /** |
| 286 * Given a stream, build an index for doing tile-based decode. | 286 * Given a stream, build an index for doing tile-based decode. |
| 287 * The built index will be saved in the decoder, and the image size will | 287 * The built index will be saved in the decoder, and the image size will |
| 288 * be returned in width and height. | 288 * be returned in width and height. |
| 289 * | 289 * |
| 290 * Return true for success or false on failure. | 290 * Return true for success or false on failure. |
| 291 */ | 291 */ |
| 292 bool buildTileIndex(SkStream*, int *width, int *height); | 292 bool buildTileIndex(SkStreamRewindable*, int *width, int *height); |
| 293 | 293 |
| 294 /** | 294 /** |
| 295 * Decode a rectangle subset in the image. | 295 * Decode a rectangle subset in the image. |
| 296 * The method can only be called after buildTileIndex(). | 296 * The method can only be called after buildTileIndex(). |
| 297 * | 297 * |
| 298 * Return true for success. | 298 * Return true for success. |
| 299 * Return false if the index is never built or failing in decoding. | 299 * Return false if the index is never built or failing in decoding. |
| 300 */ | 300 */ |
| 301 bool decodeSubset(SkBitmap* bm, const SkIRect& subset, SkBitmap::Config pref
); | 301 bool decodeSubset(SkBitmap* bm, const SkIRect& subset, SkBitmap::Config pref
); |
| 302 | 302 |
| 303 /** | 303 /** |
| 304 * @Deprecated | 304 * @Deprecated |
| 305 * Use decodeSubset instead. | 305 * Use decodeSubset instead. |
| 306 */ | 306 */ |
| 307 bool decodeRegion(SkBitmap* bitmap, const SkIRect& rect, SkBitmap::Config pr
ef) { | 307 bool decodeRegion(SkBitmap* bitmap, const SkIRect& rect, SkBitmap::Config pr
ef) { |
| 308 return this->decodeSubset(bitmap, rect, pref); | 308 return this->decodeSubset(bitmap, rect, pref); |
| 309 } | 309 } |
| 310 | 310 |
| 311 /** Given a stream, this will try to find an appropriate decoder object. | 311 /** Given a stream, this will try to find an appropriate decoder object. |
| 312 If none is found, the method returns NULL. | 312 If none is found, the method returns NULL. |
| 313 */ | 313 */ |
| 314 static SkImageDecoder* Factory(SkStream*); | 314 static SkImageDecoder* Factory(SkStreamRewindable*); |
| 315 | 315 |
| 316 /** Decode the image stored in the specified file, and store the result | 316 /** Decode the image stored in the specified file, and store the result |
| 317 in bitmap. Return true for success or false on failure. | 317 in bitmap. Return true for success or false on failure. |
| 318 | 318 |
| 319 @param prefConfig If the PrefConfigTable is not set, prefer this config. | 319 @param prefConfig If the PrefConfigTable is not set, prefer this config. |
| 320 See NOTE ABOUT PREFERRED CONFIGS. | 320 See NOTE ABOUT PREFERRED CONFIGS. |
| 321 | 321 |
| 322 @param format On success, if format is non-null, it is set to the format | 322 @param format On success, if format is non-null, it is set to the format |
| 323 of the decoded file. On failure it is ignored. | 323 of the decoded file. On failure it is ignored. |
| 324 */ | 324 */ |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 * target.fAddr = malloc/other allocation | 366 * target.fAddr = malloc/other allocation |
| 367 * target.fRowBytes = ... | 367 * target.fRowBytes = ... |
| 368 * // Now decode the actual pixels into target. &info is optional, | 368 * // Now decode the actual pixels into target. &info is optional, |
| 369 * // and could be NULL | 369 * // and could be NULL |
| 370 * success = DecodeMemoryToTarget(src, size, &info, &target); | 370 * success = DecodeMemoryToTarget(src, size, &info, &target); |
| 371 * </code> | 371 * </code> |
| 372 */ | 372 */ |
| 373 static bool DecodeMemoryToTarget(const void* buffer, size_t size, SkImage::I
nfo* info, | 373 static bool DecodeMemoryToTarget(const void* buffer, size_t size, SkImage::I
nfo* info, |
| 374 const SkBitmapFactory::Target* target); | 374 const SkBitmapFactory::Target* target); |
| 375 | 375 |
| 376 /** Decode the image stored in the specified SkStream, and store the result | 376 /** Decode the image stored in the specified SkStreamRewindable, and store t
he result |
| 377 in bitmap. Return true for success or false on failure. | 377 in bitmap. Return true for success or false on failure. |
| 378 | 378 |
| 379 @param prefConfig If the PrefConfigTable is not set, prefer this config. | 379 @param prefConfig If the PrefConfigTable is not set, prefer this config. |
| 380 See NOTE ABOUT PREFERRED CONFIGS. | 380 See NOTE ABOUT PREFERRED CONFIGS. |
| 381 | 381 |
| 382 @param format On success, if format is non-null, it is set to the format | 382 @param format On success, if format is non-null, it is set to the format |
| 383 of the decoded stream. On failure it is ignored. | 383 of the decoded stream. On failure it is ignored. |
| 384 */ | 384 */ |
| 385 static bool DecodeStream(SkStream* stream, SkBitmap* bitmap, | 385 static bool DecodeStream(SkStreamRewindable* stream, SkBitmap* bitmap, |
| 386 SkBitmap::Config prefConfig, Mode, | 386 SkBitmap::Config prefConfig, Mode, |
| 387 Format* format = NULL); | 387 Format* format = NULL); |
| 388 static bool DecodeStream(SkStream* stream, SkBitmap* bitmap) { | 388 static bool DecodeStream(SkStreamRewindable* stream, SkBitmap* bitmap) { |
| 389 return DecodeStream(stream, bitmap, SkBitmap::kNo_Config, | 389 return DecodeStream(stream, bitmap, SkBitmap::kNo_Config, |
| 390 kDecodePixels_Mode, NULL); | 390 kDecodePixels_Mode, NULL); |
| 391 } | 391 } |
| 392 | 392 |
| 393 /** Return the default config for the running device. | 393 /** Return the default config for the running device. |
| 394 Currently this used as a suggestion to image decoders that need to guess | 394 Currently this used as a suggestion to image decoders that need to guess |
| 395 what config they should decode into. | 395 what config they should decode into. |
| 396 Default is kNo_Config, but this can be changed with SetDeviceConfig() | 396 Default is kNo_Config, but this can be changed with SetDeviceConfig() |
| 397 */ | 397 */ |
| 398 static SkBitmap::Config GetDeviceConfig(); | 398 static SkBitmap::Config GetDeviceConfig(); |
| 399 /** Set the default config for the running device. | 399 /** Set the default config for the running device. |
| 400 Currently this used as a suggestion to image decoders that need to guess | 400 Currently this used as a suggestion to image decoders that need to guess |
| 401 what config they should decode into. | 401 what config they should decode into. |
| 402 Default is kNo_Config. | 402 Default is kNo_Config. |
| 403 This can be queried with GetDeviceConfig() | 403 This can be queried with GetDeviceConfig() |
| 404 */ | 404 */ |
| 405 static void SetDeviceConfig(SkBitmap::Config); | 405 static void SetDeviceConfig(SkBitmap::Config); |
| 406 | 406 |
| 407 protected: | 407 protected: |
| 408 // must be overridden in subclasses. This guy is called by decode(...) | 408 // must be overridden in subclasses. This guy is called by decode(...) |
| 409 virtual bool onDecode(SkStream*, SkBitmap* bitmap, Mode) = 0; | 409 virtual bool onDecode(SkStreamRewindable*, SkBitmap* bitmap, Mode) = 0; |
| 410 | 410 |
| 411 // If the decoder wants to support tiled based decoding, | 411 // If the decoder wants to support tiled based decoding, |
| 412 // this method must be overridden. This guy is called by buildTileIndex(...) | 412 // this method must be overridden. This guy is called by buildTileIndex(...) |
| 413 virtual bool onBuildTileIndex(SkStream*, int *width, int *height) { | 413 virtual bool onBuildTileIndex(SkStreamRewindable*, int *width, int *height)
{ |
| 414 return false; | 414 return false; |
| 415 } | 415 } |
| 416 | 416 |
| 417 // If the decoder wants to support tiled based decoding, | 417 // If the decoder wants to support tiled based decoding, |
| 418 // this method must be overridden. This guy is called by decodeRegion(...) | 418 // this method must be overridden. This guy is called by decodeRegion(...) |
| 419 virtual bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& rect) { | 419 virtual bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& rect) { |
| 420 return false; | 420 return false; |
| 421 } | 421 } |
| 422 | 422 |
| 423 /* | 423 /* |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 514 /** Calling newDecoder with a stream returns a new matching imagedecoder | 514 /** Calling newDecoder with a stream returns a new matching imagedecoder |
| 515 instance, or NULL if none can be found. The caller must manage its ownership | 515 instance, or NULL if none can be found. The caller must manage its ownership |
| 516 of the stream as usual, calling unref() when it is done, as the returned | 516 of the stream as usual, calling unref() when it is done, as the returned |
| 517 decoder may have called ref() (and if so, the decoder is responsible for | 517 decoder may have called ref() (and if so, the decoder is responsible for |
| 518 balancing its ownership when it is destroyed). | 518 balancing its ownership when it is destroyed). |
| 519 */ | 519 */ |
| 520 class SkImageDecoderFactory : public SkRefCnt { | 520 class SkImageDecoderFactory : public SkRefCnt { |
| 521 public: | 521 public: |
| 522 SK_DECLARE_INST_COUNT(SkImageDecoderFactory) | 522 SK_DECLARE_INST_COUNT(SkImageDecoderFactory) |
| 523 | 523 |
| 524 virtual SkImageDecoder* newDecoder(SkStream*) = 0; | 524 virtual SkImageDecoder* newDecoder(SkStreamRewindable*) = 0; |
| 525 | 525 |
| 526 private: | 526 private: |
| 527 typedef SkRefCnt INHERITED; | 527 typedef SkRefCnt INHERITED; |
| 528 }; | 528 }; |
| 529 | 529 |
| 530 class SkDefaultImageDecoderFactory : SkImageDecoderFactory { | 530 class SkDefaultImageDecoderFactory : SkImageDecoderFactory { |
| 531 public: | 531 public: |
| 532 // calls SkImageDecoder::Factory(stream) | 532 // calls SkImageDecoder::Factory(stream) |
| 533 virtual SkImageDecoder* newDecoder(SkStream* stream) { | 533 virtual SkImageDecoder* newDecoder(SkStreamRewindable* stream) { |
| 534 return SkImageDecoder::Factory(stream); | 534 return SkImageDecoder::Factory(stream); |
| 535 } | 535 } |
| 536 }; | 536 }; |
| 537 | 537 |
| 538 // This macro declares a global (i.e., non-class owned) creation entry point | 538 // This macro declares a global (i.e., non-class owned) creation entry point |
| 539 // for each decoder (e.g., CreateJPEGImageDecoder) | 539 // for each decoder (e.g., CreateJPEGImageDecoder) |
| 540 #define DECLARE_DECODER_CREATOR(codec) \ | 540 #define DECLARE_DECODER_CREATOR(codec) \ |
| 541 SkImageDecoder *Create ## codec (); | 541 SkImageDecoder *Create ## codec (); |
| 542 | 542 |
| 543 // This macro defines the global creation entry point for each decoder. Each | 543 // This macro defines the global creation entry point for each decoder. Each |
| 544 // decoder implementation that registers with the decoder factory must call it. | 544 // decoder implementation that registers with the decoder factory must call it. |
| 545 #define DEFINE_DECODER_CREATOR(codec) \ | 545 #define DEFINE_DECODER_CREATOR(codec) \ |
| 546 SkImageDecoder *Create ## codec () { \ | 546 SkImageDecoder *Create ## codec () { \ |
| 547 return SkNEW( Sk ## codec ); \ | 547 return SkNEW( Sk ## codec ); \ |
| 548 } | 548 } |
| 549 | 549 |
| 550 // All the decoders known by Skia. Note that, depending on the compiler settings
, | 550 // All the decoders known by Skia. Note that, depending on the compiler settings
, |
| 551 // not all of these will be available | 551 // not all of these will be available |
| 552 DECLARE_DECODER_CREATOR(BMPImageDecoder); | 552 DECLARE_DECODER_CREATOR(BMPImageDecoder); |
| 553 DECLARE_DECODER_CREATOR(GIFImageDecoder); | 553 DECLARE_DECODER_CREATOR(GIFImageDecoder); |
| 554 DECLARE_DECODER_CREATOR(ICOImageDecoder); | 554 DECLARE_DECODER_CREATOR(ICOImageDecoder); |
| 555 DECLARE_DECODER_CREATOR(JPEGImageDecoder); | 555 DECLARE_DECODER_CREATOR(JPEGImageDecoder); |
| 556 DECLARE_DECODER_CREATOR(PNGImageDecoder); | 556 DECLARE_DECODER_CREATOR(PNGImageDecoder); |
| 557 DECLARE_DECODER_CREATOR(WBMPImageDecoder); | 557 DECLARE_DECODER_CREATOR(WBMPImageDecoder); |
| 558 DECLARE_DECODER_CREATOR(WEBPImageDecoder); | 558 DECLARE_DECODER_CREATOR(WEBPImageDecoder); |
| 559 | 559 |
| 560 #endif | 560 #endif |
| OLD | NEW |