00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef SkImageDecoder_DEFINED
00018 #define SkImageDecoder_DEFINED
00019
00020 #include "SkBitmap.h"
00021 #include "SkRefCnt.h"
00022
00023 class SkStream;
00024
00029 class SkImageDecoder {
00030 public:
00031 virtual ~SkImageDecoder();
00032
00033 enum Format {
00034 kUnknown_Format,
00035 kBMP_Format,
00036 kGIF_Format,
00037 kICO_Format,
00038 kJPEG_Format,
00039 kPNG_Format,
00040 kWBMP_Format,
00041
00042 kLastKnownFormat = kWBMP_Format
00043 };
00044
00047 virtual Format getFormat() const;
00048
00052 bool getDitherImage() const { return fDitherImage; }
00053
00057 void setDitherImage(bool dither) { fDitherImage = dither; }
00058
00064 class Peeker : public SkRefCnt {
00065 public:
00069 virtual bool peek(const char tag[], const void* data, size_t length) = 0;
00070 };
00071
00072 Peeker* getPeeker() const { return fPeeker; }
00073 Peeker* setPeeker(Peeker*);
00074
00080 class Chooser : public SkRefCnt {
00081 public:
00082 virtual void begin(int count) {}
00083 virtual void inspect(int index, SkBitmap::Config config, int width, int height) {}
00086 virtual int choose() = 0;
00087 };
00088
00089 Chooser* getChooser() const { return fChooser; }
00090 Chooser* setChooser(Chooser*);
00091
00092 SkBitmap::Allocator* getAllocator() const { return fAllocator; }
00093 SkBitmap::Allocator* setAllocator(SkBitmap::Allocator*);
00094
00095
00096
00097
00098
00099
00100
00101 int getSampleSize() const { return fSampleSize; }
00102 void setSampleSize(int size);
00103
00106 void resetSampleSize() { this->setSampleSize(1); }
00107
00119 void cancelDecode() {
00120
00121
00122 fShouldCancelDecode = true;
00123 }
00124
00129 enum Mode {
00130 kDecodeBounds_Mode,
00131 kDecodePixels_Mode
00132 };
00133
00147 bool decode(SkStream*, SkBitmap* bitmap, SkBitmap::Config pref, Mode);
00148
00152 static SkImageDecoder* Factory(SkStream*);
00153
00167 static bool DecodeFile(const char file[], SkBitmap* bitmap,
00168 SkBitmap::Config prefConfig, Mode,
00169 Format* format = NULL);
00170 static bool DecodeFile(const char file[], SkBitmap* bitmap) {
00171 return DecodeFile(file, bitmap, SkBitmap::kNo_Config,
00172 kDecodePixels_Mode, NULL);
00173 }
00187 static bool DecodeMemory(const void* buffer, size_t size, SkBitmap* bitmap,
00188 SkBitmap::Config prefConfig, Mode,
00189 Format* format = NULL);
00190 static bool DecodeMemory(const void* buffer, size_t size, SkBitmap* bitmap){
00191 return DecodeMemory(buffer, size, bitmap, SkBitmap::kNo_Config,
00192 kDecodePixels_Mode, NULL);
00193 }
00207 static bool DecodeStream(SkStream* stream, SkBitmap* bitmap,
00208 SkBitmap::Config prefConfig, Mode,
00209 Format* format = NULL);
00210 static bool DecodeStream(SkStream* stream, SkBitmap* bitmap) {
00211 return DecodeStream(stream, bitmap, SkBitmap::kNo_Config,
00212 kDecodePixels_Mode, NULL);
00213 }
00214
00220 static SkBitmap::Config GetDeviceConfig();
00227 static void SetDeviceConfig(SkBitmap::Config);
00228
00230 SkDEBUGCODE(static void UnitTest();)
00233 protected:
00234
00235 virtual bool onDecode(SkStream*, SkBitmap* bitmap, SkBitmap::Config pref,
00236 Mode) = 0;
00237
00247 public:
00248 bool shouldCancelDecode() const { return fShouldCancelDecode; }
00249
00250 protected:
00251 SkImageDecoder();
00252
00253
00254
00255 bool chooseFromOneChoice(SkBitmap::Config config, int width, int height) const;
00256
00257
00258
00259
00260
00261 bool allocPixelRef(SkBitmap*, SkColorTable*) const;
00262
00263 private:
00264 Peeker* fPeeker;
00265 Chooser* fChooser;
00266 SkBitmap::Allocator* fAllocator;
00267 int fSampleSize;
00268 bool fDitherImage;
00269 mutable bool fShouldCancelDecode;
00270
00271
00272 SkImageDecoder(const SkImageDecoder&);
00273 SkImageDecoder& operator=(const SkImageDecoder&);
00274 };
00275
00282 class SkImageDecoderFactory : public SkRefCnt {
00283 public:
00284 virtual SkImageDecoder* newDecoder(SkStream*) = 0;
00285 };
00286
00287 class SkDefaultImageDecoderFactory : SkImageDecoderFactory {
00288 public:
00289
00290 virtual SkImageDecoder* newDecoder(SkStream* stream) {
00291 return SkImageDecoder::Factory(stream);
00292 }
00293 };
00294
00295
00296 #endif