3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
8 #ifndef SkImageEncoder_DEFINED
9 #define SkImageEncoder_DEFINED
12 #include "SkTRegistry.h"
18 class SkImageEncoder {
31 static SkImageEncoder* Create(Type);
33 virtual ~SkImageEncoder();
35 /* Quality ranges from 0..100 */
41 * Encode bitmap 'bm', returning the results in an SkData, at quality level
42 * 'quality' (which can be in range 0-100). If the bitmap cannot be
43 * encoded, return null. On success, the caller is responsible for
44 * calling unref() on the data when they are finished.
46 SkData* encodeData(const SkBitmap&, int quality);
49 * Encode bitmap 'bm' in the desired format, writing results to
50 * file 'file', at quality level 'quality' (which can be in range
51 * 0-100). Returns false on failure.
53 bool encodeFile(const char file[], const SkBitmap& bm, int quality);
56 * Encode bitmap 'bm' in the desired format, writing results to
57 * stream 'stream', at quality level 'quality' (which can be in
58 * range 0-100). Returns false on failure.
60 bool encodeStream(SkWStream* stream, const SkBitmap& bm, int quality);
62 static SkData* EncodeData(const SkBitmap&, Type, int quality);
63 static bool EncodeFile(const char file[], const SkBitmap&, Type,
65 static bool EncodeStream(SkWStream*, const SkBitmap&, Type,
70 * Encode bitmap 'bm' in the desired format, writing results to
71 * stream 'stream', at quality level 'quality' (which can be in
74 * This must be overridden by each SkImageEncoder implementation.
76 virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) = 0;
79 // This macro declares a global (i.e., non-class owned) creation entry point
80 // for each encoder (e.g., CreateJPEGImageEncoder)
81 #define DECLARE_ENCODER_CREATOR(codec) \
82 SkImageEncoder *Create ## codec ();
84 // This macro defines the global creation entry point for each encoder. Each
85 // encoder implementation that registers with the encoder factory must call it.
86 #define DEFINE_ENCODER_CREATOR(codec) \
87 SkImageEncoder *Create ## codec () { \
88 return SkNEW( Sk ## codec ); \
91 // All the encoders known by Skia. Note that, depending on the compiler settings,
92 // not all of these will be available
93 /** An ARGBImageEncoder will always write out
94 * bitmap.width() * bitmap.height() * 4
97 DECLARE_ENCODER_CREATOR(ARGBImageEncoder);
98 DECLARE_ENCODER_CREATOR(JPEGImageEncoder);
99 DECLARE_ENCODER_CREATOR(PNGImageEncoder);
100 DECLARE_ENCODER_CREATOR(KTXImageEncoder);
101 DECLARE_ENCODER_CREATOR(WEBPImageEncoder);
103 #ifdef SK_BUILD_FOR_IOS
104 DECLARE_ENCODER_CREATOR(PNGImageEncoder_IOS);
107 // Typedef to make registering encoder callback easier
108 // This has to be defined outside SkImageEncoder. :(
109 typedef SkTRegistry<SkImageEncoder*(*)(SkImageEncoder::Type)> SkImageEncoder_EncodeReg;