• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright 2011 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 #ifndef SkImageEncoder_DEFINED
9 #define SkImageEncoder_DEFINED
10 
11 #include "SkTypes.h"
12 
13 class SkBitmap;
14 class SkWStream;
15 
16 class SkImageEncoder {
17 public:
18     enum Type {
19         kJPEG_Type,
20         kPNG_Type,
21         kWEBP_Type
22     };
23     static SkImageEncoder* Create(Type);
24 
25     virtual ~SkImageEncoder();
26 
27     /*  Quality ranges from 0..100 */
28     enum {
29         kDefaultQuality = 80
30     };
31 
32     /**
33      * Encode bitmap 'bm' in the desired format, writing results to
34      * file 'file', at quality level 'quality' (which can be in range
35      * 0-100).
36      *
37      * Calls the particular implementation's onEncode() method to
38      * actually do the encoding.
39      */
40     bool encodeFile(const char file[], const SkBitmap& bm, int quality);
41 
42     /**
43      * Encode bitmap 'bm' in the desired format, writing results to
44      * stream 'stream', at quality level 'quality' (which can be in
45      * range 0-100).
46      *
47      * Calls the particular implementation's onEncode() method to
48      * actually do the encoding.
49      */
50     bool encodeStream(SkWStream* stream, const SkBitmap& bm, int quality);
51 
52     static bool EncodeFile(const char file[], const SkBitmap&, Type,
53                            int quality);
54     static bool EncodeStream(SkWStream*, const SkBitmap&, Type,
55                            int quality);
56 
57 protected:
58     /**
59      * Encode bitmap 'bm' in the desired format, writing results to
60      * stream 'stream', at quality level 'quality' (which can be in
61      * range 0-100).
62      *
63      * This must be overridden by each SkImageEncoder implementation.
64      */
65     virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) = 0;
66 };
67 
68 // This macro declares a global (i.e., non-class owned) creation entry point
69 // for each encoder (e.g., CreateJPEGImageEncoder)
70 #define DECLARE_ENCODER_CREATOR(codec)          \
71     SkImageEncoder *Create ## codec ();
72 
73 // This macro defines the global creation entry point for each encoder. Each
74 // encoder implementation that registers with the encoder factory must call it.
75 #define DEFINE_ENCODER_CREATOR(codec)           \
76     SkImageEncoder *Create ## codec () {        \
77         return SkNEW( Sk ## codec );            \
78     }
79 
80 // All the encoders known by Skia. Note that, depending on the compiler settings,
81 // not all of these will be available
82 DECLARE_ENCODER_CREATOR(JPEGImageEncoder);
83 DECLARE_ENCODER_CREATOR(PNGImageEncoder);
84 DECLARE_ENCODER_CREATOR(WEBPImageEncoder);
85 
86 #endif
87