Skia
2DGraphicsLibrary
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SkImageEncoder.h
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 SkPixelSerializer;
16 class SkPixmap;
17 class SkData;
18 class SkWStream;
19 
21 public:
22  // TODO (scroggo): Merge with SkEncodedFormat.
23  enum Type {
24  kUnknown_Type,
25  kBMP_Type,
26  kGIF_Type,
27  kICO_Type,
28  kJPEG_Type,
29  kPNG_Type,
30  kWBMP_Type,
31  kWEBP_Type,
32  kKTX_Type,
33  };
34  static SkImageEncoder* Create(Type);
35 
36  virtual ~SkImageEncoder();
37 
38  /* Quality ranges from 0..100 */
39  enum {
40  kDefaultQuality = 80
41  };
42 
49  SkData* encodeData(const SkBitmap&, int quality);
50 
56  bool encodeFile(const char file[], const SkBitmap& bm, int quality);
57 
63  bool encodeStream(SkWStream* stream, const SkBitmap& bm, int quality);
64 
65  static SkData* EncodeData(const SkImageInfo&, const void* pixels, size_t rowBytes,
66  Type, int quality);
67  static SkData* EncodeData(const SkBitmap&, Type, int quality);
68 
69  static SkData* EncodeData(const SkPixmap&, Type, int quality);
70 
71  static bool EncodeFile(const char file[], const SkBitmap&, Type,
72  int quality);
73  static bool EncodeStream(SkWStream*, const SkBitmap&, Type,
74  int quality);
75 
79 
80 protected:
88  virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) = 0;
89 };
90 
91 // This macro declares a global (i.e., non-class owned) creation entry point
92 // for each encoder (e.g., CreateJPEGImageEncoder)
93 #define DECLARE_ENCODER_CREATOR(codec) \
94  SK_API SkImageEncoder *Create ## codec ();
95 
96 // This macro defines the global creation entry point for each encoder. Each
97 // encoder implementation that registers with the encoder factory must call it.
98 #define DEFINE_ENCODER_CREATOR(codec) \
99  SkImageEncoder* Create##codec() { return new Sk##codec; }
100 
101 // All the encoders known by Skia. Note that, depending on the compiler settings,
102 // not all of these will be available
103 DECLARE_ENCODER_CREATOR(JPEGImageEncoder);
104 DECLARE_ENCODER_CREATOR(PNGImageEncoder);
105 DECLARE_ENCODER_CREATOR(KTXImageEncoder);
106 DECLARE_ENCODER_CREATOR(WEBPImageEncoder);
107 
108 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
109 SkImageEncoder* CreateImageEncoder_CG(SkImageEncoder::Type type);
110 #endif
111 
112 #if defined(SK_BUILD_FOR_WIN)
113 SkImageEncoder* CreateImageEncoder_WIC(SkImageEncoder::Type type);
114 #endif
115 
116 // Typedef to make registering encoder callback easier
117 // This has to be defined outside SkImageEncoder. :(
119 #endif
Describe an image's dimensions and pixel type.
Definition: SkImageInfo.h:181
bool encodeStream(SkWStream *stream, const SkBitmap &bm, int quality)
Encode bitmap 'bm' in the desired format, writing results to stream 'stream', at quality level 'quali...
Pairs SkImageInfo with actual pixels and rowbytes.
Definition: SkPixmap.h:23
SkData holds an immutable data buffer.
Definition: SkData.h:22
The SkBitmap class specifies a raster bitmap.
Definition: SkBitmap.h:41
Interface for serializing pixels, e.g.
Definition: SkPixelSerializer.h:19
virtual bool onEncode(SkWStream *stream, const SkBitmap &bm, int quality)=0
Encode bitmap 'bm' in the desired format, writing results to stream 'stream', at quality level 'quali...
static SkPixelSerializer * CreatePixelSerializer()
Uses SkImageEncoder to serialize images that are not already encoded as SkImageEncoder::kPNG_Type ima...
Definition: SkStream.h:182
SkData * encodeData(const SkBitmap &, int quality)
Encode bitmap 'bm', returning the results in an SkData, at quality level 'quality' (which can be in r...
Definition: SkImageEncoder.h:20
Template class that registers itself (in the constructor) into a linked-list and provides a function-...
Definition: SkTRegistry.h:19
bool encodeFile(const char file[], const SkBitmap &bm, int quality)
Encode bitmap 'bm' in the desired format, writing results to file 'file', at quality level 'quality' ...