blob: 3fbfefa5f7f6425bb5c2fa8cf04bd65c1d1077d1 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@android.comb08eb2b2009-01-06 20:16:26 +00008#ifndef SkImageEncoder_DEFINED
9#define SkImageEncoder_DEFINED
10
11#include "SkTypes.h"
12
13class SkBitmap;
14class SkWStream;
15
16class SkImageEncoder {
17public:
18 enum Type {
19 kJPEG_Type,
20 kPNG_Type
21 };
22 static SkImageEncoder* Create(Type);
23
24 virtual ~SkImageEncoder();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000025
reed@android.comb08eb2b2009-01-06 20:16:26 +000026 /* Quality ranges from 0..100 */
27 enum {
28 kDefaultQuality = 80
29 };
30
epoger@google.com4ce738b2012-11-16 18:44:18 +000031 /**
32 * Encode bitmap 'bm' in the desired format, writing results to
33 * file 'file', at quality level 'quality' (which can be in range
34 * 0-100).
35 *
36 * Calls the particular implementation's onEncode() method to
37 * actually do the encoding.
38 */
39 bool encodeFile(const char file[], const SkBitmap& bm, int quality);
40
41 /**
42 * Encode bitmap 'bm' in the desired format, writing results to
43 * stream 'stream', at quality level 'quality' (which can be in
44 * range 0-100).
45 *
46 * Calls the particular implementation's onEncode() method to
47 * actually do the encoding.
48 */
49 bool encodeStream(SkWStream* stream, const SkBitmap& bm, int quality);
reed@android.comb08eb2b2009-01-06 20:16:26 +000050
51 static bool EncodeFile(const char file[], const SkBitmap&, Type,
52 int quality);
53 static bool EncodeStream(SkWStream*, const SkBitmap&, Type,
54 int quality);
55
56protected:
epoger@google.com4ce738b2012-11-16 18:44:18 +000057 /**
58 * Encode bitmap 'bm' in the desired format, writing results to
59 * stream 'stream', at quality level 'quality' (which can be in
60 * range 0-100).
61 *
62 * This must be overridden by each SkImageEncoder implementation.
63 */
64 virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) = 0;
reed@android.comb08eb2b2009-01-06 20:16:26 +000065};
66
robertphillips@google.comec51cb82012-03-23 18:13:47 +000067// This macro declares a global (i.e., non-class owned) creation entry point
68// for each encoder (e.g., CreateJPEGImageEncoder)
69#define DECLARE_ENCODER_CREATOR(codec) \
70 SkImageEncoder *Create ## codec ();
71
72// This macro defines the global creation entry point for each encoder. Each
73// encoder implementation that registers with the encoder factory must call it.
74#define DEFINE_ENCODER_CREATOR(codec) \
75 SkImageEncoder *Create ## codec () { \
76 return SkNEW( Sk ## codec ); \
77 }
78
79// All the encoders known by Skia. Note that, depending on the compiler settings,
80// not all of these will be available
81DECLARE_ENCODER_CREATOR(JPEGImageEncoder);
82DECLARE_ENCODER_CREATOR(PNGImageEncoder);
83
reed@android.comb08eb2b2009-01-06 20:16:26 +000084#endif