michael@0: michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: #ifndef SkImageEncoder_DEFINED michael@0: #define SkImageEncoder_DEFINED michael@0: michael@0: #include "SkTypes.h" michael@0: #include "SkTRegistry.h" michael@0: michael@0: class SkBitmap; michael@0: class SkData; michael@0: class SkWStream; michael@0: michael@0: class SkImageEncoder { michael@0: public: michael@0: enum Type { michael@0: kUnknown_Type, michael@0: kBMP_Type, michael@0: kGIF_Type, michael@0: kICO_Type, michael@0: kJPEG_Type, michael@0: kPNG_Type, michael@0: kWBMP_Type, michael@0: kWEBP_Type, michael@0: }; michael@0: static SkImageEncoder* Create(Type); michael@0: michael@0: virtual ~SkImageEncoder(); michael@0: michael@0: /* Quality ranges from 0..100 */ michael@0: enum { michael@0: kDefaultQuality = 80 michael@0: }; michael@0: michael@0: /** michael@0: * Encode bitmap 'bm', returning the results in an SkData, at quality level michael@0: * 'quality' (which can be in range 0-100). If the bitmap cannot be michael@0: * encoded, return null. On success, the caller is responsible for michael@0: * calling unref() on the data when they are finished. michael@0: */ michael@0: SkData* encodeData(const SkBitmap&, int quality); michael@0: michael@0: /** michael@0: * Encode bitmap 'bm' in the desired format, writing results to michael@0: * file 'file', at quality level 'quality' (which can be in range michael@0: * 0-100). Returns false on failure. michael@0: */ michael@0: bool encodeFile(const char file[], const SkBitmap& bm, int quality); michael@0: michael@0: /** michael@0: * Encode bitmap 'bm' in the desired format, writing results to michael@0: * stream 'stream', at quality level 'quality' (which can be in michael@0: * range 0-100). Returns false on failure. michael@0: */ michael@0: bool encodeStream(SkWStream* stream, const SkBitmap& bm, int quality); michael@0: michael@0: static SkData* EncodeData(const SkBitmap&, Type, int quality); michael@0: static bool EncodeFile(const char file[], const SkBitmap&, Type, michael@0: int quality); michael@0: static bool EncodeStream(SkWStream*, const SkBitmap&, Type, michael@0: int quality); michael@0: michael@0: protected: michael@0: /** michael@0: * Encode bitmap 'bm' in the desired format, writing results to michael@0: * stream 'stream', at quality level 'quality' (which can be in michael@0: * range 0-100). michael@0: * michael@0: * This must be overridden by each SkImageEncoder implementation. michael@0: */ michael@0: virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) = 0; michael@0: }; michael@0: michael@0: // This macro declares a global (i.e., non-class owned) creation entry point michael@0: // for each encoder (e.g., CreateJPEGImageEncoder) michael@0: #define DECLARE_ENCODER_CREATOR(codec) \ michael@0: SkImageEncoder *Create ## codec (); michael@0: michael@0: // This macro defines the global creation entry point for each encoder. Each michael@0: // encoder implementation that registers with the encoder factory must call it. michael@0: #define DEFINE_ENCODER_CREATOR(codec) \ michael@0: SkImageEncoder *Create ## codec () { \ michael@0: return SkNEW( Sk ## codec ); \ michael@0: } michael@0: michael@0: // All the encoders known by Skia. Note that, depending on the compiler settings, michael@0: // not all of these will be available michael@0: /** An ARGBImageEncoder will always write out michael@0: * bitmap.width() * bitmap.height() * 4 michael@0: * bytes. michael@0: */ michael@0: DECLARE_ENCODER_CREATOR(ARGBImageEncoder); michael@0: DECLARE_ENCODER_CREATOR(JPEGImageEncoder); michael@0: DECLARE_ENCODER_CREATOR(PNGImageEncoder); michael@0: DECLARE_ENCODER_CREATOR(WEBPImageEncoder); michael@0: michael@0: // Typedef to make registering encoder callback easier michael@0: // This has to be defined outside SkImageEncoder. :( michael@0: typedef SkTRegistry SkImageEncoder_EncodeReg; michael@0: #endif