1 2 /* 3 * Copyright 2011 Google Inc. 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 #ifndef SkImageEncoder_DEFINED 9 #define SkImageEncoder_DEFINED 10 11 #include "SkTypes.h" 12 #include "SkTRegistry.h" 13 14 class SkBitmap; 15 class SkData; 16 class SkWStream; 17 18 class SkImageEncoder { 19 public: 20 enum Type { 21 kUnknown_Type, 22 kBMP_Type, 23 kGIF_Type, 24 kICO_Type, 25 kJPEG_Type, 26 kPNG_Type, 27 kWBMP_Type, 28 kWEBP_Type, 29 }; 30 static SkImageEncoder* Create(Type); 31 32 virtual ~SkImageEncoder(); 33 34 /* Quality ranges from 0..100 */ 35 enum { 36 kDefaultQuality = 80 37 }; 38 39 /** 40 * Encode bitmap 'bm', returning the results in an SkData, at quality level 41 * 'quality' (which can be in range 0-100). If the bitmap cannot be 42 * encoded, return null. On success, the caller is responsible for 43 * calling unref() on the data when they are finished. 44 */ 45 SkData* encodeData(const SkBitmap&, int quality); 46 47 /** 48 * Encode bitmap 'bm' in the desired format, writing results to 49 * file 'file', at quality level 'quality' (which can be in range 50 * 0-100). Returns false on failure. 51 */ 52 bool encodeFile(const char file[], const SkBitmap& bm, int quality); 53 54 /** 55 * Encode bitmap 'bm' in the desired format, writing results to 56 * stream 'stream', at quality level 'quality' (which can be in 57 * range 0-100). Returns false on failure. 58 */ 59 bool encodeStream(SkWStream* stream, const SkBitmap& bm, int quality); 60 61 static SkData* EncodeData(const SkBitmap&, Type, int quality); 62 static bool EncodeFile(const char file[], const SkBitmap&, Type, 63 int quality); 64 static bool EncodeStream(SkWStream*, const SkBitmap&, Type, 65 int quality); 66 67 protected: 68 /** 69 * Encode bitmap 'bm' in the desired format, writing results to 70 * stream 'stream', at quality level 'quality' (which can be in 71 * range 0-100). 72 * 73 * This must be overridden by each SkImageEncoder implementation. 74 */ 75 virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) = 0; 76 }; 77 78 // This macro declares a global (i.e., non-class owned) creation entry point 79 // for each encoder (e.g., CreateJPEGImageEncoder) 80 #define DECLARE_ENCODER_CREATOR(codec) \ 81 SkImageEncoder *Create ## codec (); 82 83 // This macro defines the global creation entry point for each encoder. Each 84 // encoder implementation that registers with the encoder factory must call it. 85 #define DEFINE_ENCODER_CREATOR(codec) \ 86 SkImageEncoder *Create ## codec () { \ 87 return SkNEW( Sk ## codec ); \ 88 } 89 90 // All the encoders known by Skia. Note that, depending on the compiler settings, 91 // not all of these will be available 92 /** An ARGBImageEncoder will always write out 93 * bitmap.width() * bitmap.height() * 4 94 * bytes. 95 */ 96 DECLARE_ENCODER_CREATOR(ARGBImageEncoder); 97 DECLARE_ENCODER_CREATOR(JPEGImageEncoder); 98 DECLARE_ENCODER_CREATOR(PNGImageEncoder); 99 DECLARE_ENCODER_CREATOR(WEBPImageEncoder); 100 101 // Typedef to make registering encoder callback easier 102 // This has to be defined outside SkImageEncoder. :( 103 typedef SkTRegistry<SkImageEncoder*(*)(SkImageEncoder::Type)> SkImageEncoder_EncodeReg; 104 #endif 105