Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(173)

Side by Side Diff: include/core/SkImageDecoder.h

Issue 23477009: Change SkImageDecoders to take an SkStreamRewindable. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Only switch to SkStreamRewindable when necessary. Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | include/images/SkImageRef.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "SkTRegistry.h" 18 #include "SkTRegistry.h"
19 #include "SkTypes.h" 19 #include "SkTypes.h"
20 20
21 class SkStream; 21 class SkStream;
22 class SkStreamRewindable;
22 23
23 /** \class SkImageDecoder 24 /** \class SkImageDecoder
24 25
25 Base class for decoding compressed images into a SkBitmap 26 Base class for decoding compressed images into a SkBitmap
26 */ 27 */
27 class SkImageDecoder : public SkNoncopyable { 28 class SkImageDecoder : public SkNoncopyable {
28 public: 29 public:
29 virtual ~SkImageDecoder(); 30 virtual ~SkImageDecoder();
30 31
31 enum Format { 32 enum Format {
32 kUnknown_Format, 33 kUnknown_Format,
33 kBMP_Format, 34 kBMP_Format,
34 kGIF_Format, 35 kGIF_Format,
35 kICO_Format, 36 kICO_Format,
36 kJPEG_Format, 37 kJPEG_Format,
37 kPNG_Format, 38 kPNG_Format,
38 kWBMP_Format, 39 kWBMP_Format,
39 kWEBP_Format, 40 kWEBP_Format,
40 41
41 kLastKnownFormat = kWEBP_Format, 42 kLastKnownFormat = kWEBP_Format,
42 }; 43 };
43 44
44 /** Return the format of image this decoder can decode. If this decoder can decode multiple 45 /** Return the format of image this decoder can decode. If this decoder can decode multiple
45 formats, kUnknown_Format will be returned. 46 formats, kUnknown_Format will be returned.
46 */ 47 */
47 virtual Format getFormat() const; 48 virtual Format getFormat() const;
48 49
49 /** Return the format of the SkStream or kUnknown_Format if it cannot be det ermined. Rewinds the 50 /** Return the format of the SkStreamRewindable or kUnknown_Format if it can not be determined.
50 stream before returning. 51 Rewinds the stream before returning.
51 */ 52 */
52 static Format GetStreamFormat(SkStream*); 53 static Format GetStreamFormat(SkStreamRewindable*);
53 54
54 /** Return a readable string of the Format provided. 55 /** Return a readable string of the Format provided.
55 */ 56 */
56 static const char* GetFormatName(Format); 57 static const char* GetFormatName(Format);
57 58
58 /** Return a readable string of the value returned by getFormat(). 59 /** Return a readable string of the value returned by getFormat().
59 */ 60 */
60 const char* getFormatName() const; 61 const char* getFormatName() const;
61 62
62 /** Returns true if the decoder should try to dither the resulting image. 63 /** Returns true if the decoder should try to dither the resulting image.
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 return this->decode(stream, bitmap, SkBitmap::kNo_Config, mode); 284 return this->decode(stream, bitmap, SkBitmap::kNo_Config, mode);
284 } 285 }
285 286
286 /** 287 /**
287 * Given a stream, build an index for doing tile-based decode. 288 * Given a stream, build an index for doing tile-based decode.
288 * The built index will be saved in the decoder, and the image size will 289 * The built index will be saved in the decoder, and the image size will
289 * be returned in width and height. 290 * be returned in width and height.
290 * 291 *
291 * Return true for success or false on failure. 292 * Return true for success or false on failure.
292 */ 293 */
293 bool buildTileIndex(SkStream*, int *width, int *height); 294 bool buildTileIndex(SkStreamRewindable*, int *width, int *height);
294 295
295 /** 296 /**
296 * Decode a rectangle subset in the image. 297 * Decode a rectangle subset in the image.
297 * The method can only be called after buildTileIndex(). 298 * The method can only be called after buildTileIndex().
298 * 299 *
299 * Return true for success. 300 * Return true for success.
300 * Return false if the index is never built or failing in decoding. 301 * Return false if the index is never built or failing in decoding.
301 */ 302 */
302 bool decodeSubset(SkBitmap* bm, const SkIRect& subset, SkBitmap::Config pref ); 303 bool decodeSubset(SkBitmap* bm, const SkIRect& subset, SkBitmap::Config pref );
303 304
304 /** 305 /**
305 * @Deprecated 306 * @Deprecated
306 * Use decodeSubset instead. 307 * Use decodeSubset instead.
307 */ 308 */
308 bool decodeRegion(SkBitmap* bitmap, const SkIRect& rect, SkBitmap::Config pr ef) { 309 bool decodeRegion(SkBitmap* bitmap, const SkIRect& rect, SkBitmap::Config pr ef) {
309 return this->decodeSubset(bitmap, rect, pref); 310 return this->decodeSubset(bitmap, rect, pref);
310 } 311 }
311 312
312 /** Given a stream, this will try to find an appropriate decoder object. 313 /** Given a stream, this will try to find an appropriate decoder object.
313 If none is found, the method returns NULL. 314 If none is found, the method returns NULL.
314 */ 315 */
315 static SkImageDecoder* Factory(SkStream*); 316 static SkImageDecoder* Factory(SkStreamRewindable*);
316 317
317 /** Decode the image stored in the specified file, and store the result 318 /** Decode the image stored in the specified file, and store the result
318 in bitmap. Return true for success or false on failure. 319 in bitmap. Return true for success or false on failure.
319 320
320 @param prefConfig If the PrefConfigTable is not set, prefer this config. 321 @param prefConfig If the PrefConfigTable is not set, prefer this config.
321 See NOTE ABOUT PREFERRED CONFIGS. 322 See NOTE ABOUT PREFERRED CONFIGS.
322 323
323 @param format On success, if format is non-null, it is set to the format 324 @param format On success, if format is non-null, it is set to the format
324 of the decoded file. On failure it is ignored. 325 of the decoded file. On failure it is ignored.
325 */ 326 */
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 * target.fAddr = malloc/other allocation 368 * target.fAddr = malloc/other allocation
368 * target.fRowBytes = ... 369 * target.fRowBytes = ...
369 * // Now decode the actual pixels into target. &info is optional, 370 * // Now decode the actual pixels into target. &info is optional,
370 * // and could be NULL 371 * // and could be NULL
371 * success = DecodeMemoryToTarget(src, size, &info, &target); 372 * success = DecodeMemoryToTarget(src, size, &info, &target);
372 * </code> 373 * </code>
373 */ 374 */
374 static bool DecodeMemoryToTarget(const void* buffer, size_t size, SkImage::I nfo* info, 375 static bool DecodeMemoryToTarget(const void* buffer, size_t size, SkImage::I nfo* info,
375 const SkBitmapFactory::Target* target); 376 const SkBitmapFactory::Target* target);
376 377
377 /** Decode the image stored in the specified SkStream, and store the result 378 /** Decode the image stored in the specified SkStreamRewindable, and store t he result
378 in bitmap. Return true for success or false on failure. 379 in bitmap. Return true for success or false on failure.
379 380
380 @param prefConfig If the PrefConfigTable is not set, prefer this config. 381 @param prefConfig If the PrefConfigTable is not set, prefer this config.
381 See NOTE ABOUT PREFERRED CONFIGS. 382 See NOTE ABOUT PREFERRED CONFIGS.
382 383
383 @param format On success, if format is non-null, it is set to the format 384 @param format On success, if format is non-null, it is set to the format
384 of the decoded stream. On failure it is ignored. 385 of the decoded stream. On failure it is ignored.
385 */ 386 */
386 static bool DecodeStream(SkStream* stream, SkBitmap* bitmap, 387 static bool DecodeStream(SkStreamRewindable* stream, SkBitmap* bitmap,
387 SkBitmap::Config prefConfig, Mode, 388 SkBitmap::Config prefConfig, Mode,
388 Format* format = NULL); 389 Format* format = NULL);
389 static bool DecodeStream(SkStream* stream, SkBitmap* bitmap) { 390 static bool DecodeStream(SkStreamRewindable* stream, SkBitmap* bitmap) {
390 return DecodeStream(stream, bitmap, SkBitmap::kNo_Config, 391 return DecodeStream(stream, bitmap, SkBitmap::kNo_Config,
391 kDecodePixels_Mode, NULL); 392 kDecodePixels_Mode, NULL);
392 } 393 }
393 394
394 /** Return the default config for the running device. 395 /** Return the default config for the running device.
395 Currently this used as a suggestion to image decoders that need to guess 396 Currently this used as a suggestion to image decoders that need to guess
396 what config they should decode into. 397 what config they should decode into.
397 Default is kNo_Config, but this can be changed with SetDeviceConfig() 398 Default is kNo_Config, but this can be changed with SetDeviceConfig()
398 */ 399 */
399 static SkBitmap::Config GetDeviceConfig(); 400 static SkBitmap::Config GetDeviceConfig();
400 /** Set the default config for the running device. 401 /** Set the default config for the running device.
401 Currently this used as a suggestion to image decoders that need to guess 402 Currently this used as a suggestion to image decoders that need to guess
402 what config they should decode into. 403 what config they should decode into.
403 Default is kNo_Config. 404 Default is kNo_Config.
404 This can be queried with GetDeviceConfig() 405 This can be queried with GetDeviceConfig()
405 */ 406 */
406 static void SetDeviceConfig(SkBitmap::Config); 407 static void SetDeviceConfig(SkBitmap::Config);
407 408
408 protected: 409 protected:
409 // must be overridden in subclasses. This guy is called by decode(...) 410 // must be overridden in subclasses. This guy is called by decode(...)
410 virtual bool onDecode(SkStream*, SkBitmap* bitmap, Mode) = 0; 411 virtual bool onDecode(SkStream*, SkBitmap* bitmap, Mode) = 0;
411 412
412 // If the decoder wants to support tiled based decoding, 413 // If the decoder wants to support tiled based decoding,
413 // this method must be overridden. This guy is called by buildTileIndex(...) 414 // this method must be overridden. This guy is called by buildTileIndex(...)
414 virtual bool onBuildTileIndex(SkStream*, int *width, int *height) { 415 virtual bool onBuildTileIndex(SkStreamRewindable*, int *width, int *height) {
415 return false; 416 return false;
416 } 417 }
417 418
418 // If the decoder wants to support tiled based decoding, 419 // If the decoder wants to support tiled based decoding,
419 // this method must be overridden. This guy is called by decodeRegion(...) 420 // this method must be overridden. This guy is called by decodeRegion(...)
420 virtual bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& rect) { 421 virtual bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& rect) {
421 return false; 422 return false;
422 } 423 }
423 424
424 /* 425 /*
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 /** Calling newDecoder with a stream returns a new matching imagedecoder 516 /** Calling newDecoder with a stream returns a new matching imagedecoder
516 instance, or NULL if none can be found. The caller must manage its ownership 517 instance, or NULL if none can be found. The caller must manage its ownership
517 of the stream as usual, calling unref() when it is done, as the returned 518 of the stream as usual, calling unref() when it is done, as the returned
518 decoder may have called ref() (and if so, the decoder is responsible for 519 decoder may have called ref() (and if so, the decoder is responsible for
519 balancing its ownership when it is destroyed). 520 balancing its ownership when it is destroyed).
520 */ 521 */
521 class SkImageDecoderFactory : public SkRefCnt { 522 class SkImageDecoderFactory : public SkRefCnt {
522 public: 523 public:
523 SK_DECLARE_INST_COUNT(SkImageDecoderFactory) 524 SK_DECLARE_INST_COUNT(SkImageDecoderFactory)
524 525
525 virtual SkImageDecoder* newDecoder(SkStream*) = 0; 526 virtual SkImageDecoder* newDecoder(SkStreamRewindable*) = 0;
526 527
527 private: 528 private:
528 typedef SkRefCnt INHERITED; 529 typedef SkRefCnt INHERITED;
529 }; 530 };
530 531
531 class SkDefaultImageDecoderFactory : SkImageDecoderFactory { 532 class SkDefaultImageDecoderFactory : SkImageDecoderFactory {
532 public: 533 public:
533 // calls SkImageDecoder::Factory(stream) 534 // calls SkImageDecoder::Factory(stream)
534 virtual SkImageDecoder* newDecoder(SkStream* stream) { 535 virtual SkImageDecoder* newDecoder(SkStreamRewindable* stream) {
535 return SkImageDecoder::Factory(stream); 536 return SkImageDecoder::Factory(stream);
536 } 537 }
537 }; 538 };
538 539
539 // This macro declares a global (i.e., non-class owned) creation entry point 540 // This macro declares a global (i.e., non-class owned) creation entry point
540 // for each decoder (e.g., CreateJPEGImageDecoder) 541 // for each decoder (e.g., CreateJPEGImageDecoder)
541 #define DECLARE_DECODER_CREATOR(codec) \ 542 #define DECLARE_DECODER_CREATOR(codec) \
542 SkImageDecoder *Create ## codec (); 543 SkImageDecoder *Create ## codec ();
543 544
544 // This macro defines the global creation entry point for each decoder. Each 545 // This macro defines the global creation entry point for each decoder. Each
545 // decoder implementation that registers with the decoder factory must call it. 546 // decoder implementation that registers with the decoder factory must call it.
546 #define DEFINE_DECODER_CREATOR(codec) \ 547 #define DEFINE_DECODER_CREATOR(codec) \
547 SkImageDecoder *Create ## codec () { \ 548 SkImageDecoder *Create ## codec () { \
548 return SkNEW( Sk ## codec ); \ 549 return SkNEW( Sk ## codec ); \
549 } 550 }
550 551
551 // All the decoders known by Skia. Note that, depending on the compiler settings , 552 // All the decoders known by Skia. Note that, depending on the compiler settings ,
552 // not all of these will be available 553 // not all of these will be available
553 DECLARE_DECODER_CREATOR(BMPImageDecoder); 554 DECLARE_DECODER_CREATOR(BMPImageDecoder);
554 DECLARE_DECODER_CREATOR(GIFImageDecoder); 555 DECLARE_DECODER_CREATOR(GIFImageDecoder);
555 DECLARE_DECODER_CREATOR(ICOImageDecoder); 556 DECLARE_DECODER_CREATOR(ICOImageDecoder);
556 DECLARE_DECODER_CREATOR(JPEGImageDecoder); 557 DECLARE_DECODER_CREATOR(JPEGImageDecoder);
557 DECLARE_DECODER_CREATOR(PNGImageDecoder); 558 DECLARE_DECODER_CREATOR(PNGImageDecoder);
558 DECLARE_DECODER_CREATOR(WBMPImageDecoder); 559 DECLARE_DECODER_CREATOR(WBMPImageDecoder);
559 DECLARE_DECODER_CREATOR(WEBPImageDecoder); 560 DECLARE_DECODER_CREATOR(WEBPImageDecoder);
560 561
561 562
562 // Typedefs to make registering decoder and formatter callbacks easier. 563 // Typedefs to make registering decoder and formatter callbacks easier.
563 // These have to be defined outside SkImageDecoder. :( 564 // These have to be defined outside SkImageDecoder. :(
564 typedef SkTRegistry<SkImageDecoder*(*)(SkStream*)> SkImageDecoder_DecodeR eg; 565 typedef SkTRegistry<SkImageDecoder*(*)(SkStreamRewindable*)> SkImageDecod er_DecodeReg;
565 typedef SkTRegistry<SkImageDecoder::Format(*)(SkStream*)> SkImageDecoder_FormatR eg; 566 typedef SkTRegistry<SkImageDecoder::Format(*)(SkStreamRewindable*)> SkImageDecod er_FormatReg;
566 567
567 #endif 568 #endif
OLDNEW
« no previous file with comments | « no previous file | include/images/SkImageRef.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698