1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/include/core/SkImageEncoder.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,104 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2011 Google Inc. 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 +#ifndef SkImageEncoder_DEFINED 1.12 +#define SkImageEncoder_DEFINED 1.13 + 1.14 +#include "SkTypes.h" 1.15 +#include "SkTRegistry.h" 1.16 + 1.17 +class SkBitmap; 1.18 +class SkData; 1.19 +class SkWStream; 1.20 + 1.21 +class SkImageEncoder { 1.22 +public: 1.23 + enum Type { 1.24 + kUnknown_Type, 1.25 + kBMP_Type, 1.26 + kGIF_Type, 1.27 + kICO_Type, 1.28 + kJPEG_Type, 1.29 + kPNG_Type, 1.30 + kWBMP_Type, 1.31 + kWEBP_Type, 1.32 + }; 1.33 + static SkImageEncoder* Create(Type); 1.34 + 1.35 + virtual ~SkImageEncoder(); 1.36 + 1.37 + /* Quality ranges from 0..100 */ 1.38 + enum { 1.39 + kDefaultQuality = 80 1.40 + }; 1.41 + 1.42 + /** 1.43 + * Encode bitmap 'bm', returning the results in an SkData, at quality level 1.44 + * 'quality' (which can be in range 0-100). If the bitmap cannot be 1.45 + * encoded, return null. On success, the caller is responsible for 1.46 + * calling unref() on the data when they are finished. 1.47 + */ 1.48 + SkData* encodeData(const SkBitmap&, int quality); 1.49 + 1.50 + /** 1.51 + * Encode bitmap 'bm' in the desired format, writing results to 1.52 + * file 'file', at quality level 'quality' (which can be in range 1.53 + * 0-100). Returns false on failure. 1.54 + */ 1.55 + bool encodeFile(const char file[], const SkBitmap& bm, int quality); 1.56 + 1.57 + /** 1.58 + * Encode bitmap 'bm' in the desired format, writing results to 1.59 + * stream 'stream', at quality level 'quality' (which can be in 1.60 + * range 0-100). Returns false on failure. 1.61 + */ 1.62 + bool encodeStream(SkWStream* stream, const SkBitmap& bm, int quality); 1.63 + 1.64 + static SkData* EncodeData(const SkBitmap&, Type, int quality); 1.65 + static bool EncodeFile(const char file[], const SkBitmap&, Type, 1.66 + int quality); 1.67 + static bool EncodeStream(SkWStream*, const SkBitmap&, Type, 1.68 + int quality); 1.69 + 1.70 +protected: 1.71 + /** 1.72 + * Encode bitmap 'bm' in the desired format, writing results to 1.73 + * stream 'stream', at quality level 'quality' (which can be in 1.74 + * range 0-100). 1.75 + * 1.76 + * This must be overridden by each SkImageEncoder implementation. 1.77 + */ 1.78 + virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) = 0; 1.79 +}; 1.80 + 1.81 +// This macro declares a global (i.e., non-class owned) creation entry point 1.82 +// for each encoder (e.g., CreateJPEGImageEncoder) 1.83 +#define DECLARE_ENCODER_CREATOR(codec) \ 1.84 + SkImageEncoder *Create ## codec (); 1.85 + 1.86 +// This macro defines the global creation entry point for each encoder. Each 1.87 +// encoder implementation that registers with the encoder factory must call it. 1.88 +#define DEFINE_ENCODER_CREATOR(codec) \ 1.89 + SkImageEncoder *Create ## codec () { \ 1.90 + return SkNEW( Sk ## codec ); \ 1.91 + } 1.92 + 1.93 +// All the encoders known by Skia. Note that, depending on the compiler settings, 1.94 +// not all of these will be available 1.95 +/** An ARGBImageEncoder will always write out 1.96 + * bitmap.width() * bitmap.height() * 4 1.97 + * bytes. 1.98 + */ 1.99 +DECLARE_ENCODER_CREATOR(ARGBImageEncoder); 1.100 +DECLARE_ENCODER_CREATOR(JPEGImageEncoder); 1.101 +DECLARE_ENCODER_CREATOR(PNGImageEncoder); 1.102 +DECLARE_ENCODER_CREATOR(WEBPImageEncoder); 1.103 + 1.104 +// Typedef to make registering encoder callback easier 1.105 +// This has to be defined outside SkImageEncoder. :( 1.106 +typedef SkTRegistry<SkImageEncoder*(*)(SkImageEncoder::Type)> SkImageEncoder_EncodeReg; 1.107 +#endif