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