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#include "SkTRegistry.h"
13
14class SkBitmap;
15class SkData;
16class SkWStream;
17
18class SkImageEncoder {
19public:
20    enum Type {
21        kUnknown_Type,
22        kBMP_Type,
23        kGIF_Type,
24        kICO_Type,
25        kJPEG_Type,
26        kPNG_Type,
27        kWBMP_Type,
28        kWEBP_Type,
29        kKTX_Type,
30    };
31    static SkImageEncoder* Create(Type);
32
33    virtual ~SkImageEncoder();
34
35    /*  Quality ranges from 0..100 */
36    enum {
37        kDefaultQuality = 80
38    };
39
40    /**
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.
45     */
46    SkData* encodeData(const SkBitmap&, int quality);
47
48    /**
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.
52     */
53    bool encodeFile(const char file[], const SkBitmap& bm, int quality);
54
55    /**
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.
59     */
60    bool encodeStream(SkWStream* stream, const SkBitmap& bm, int quality);
61
62    static SkData* EncodeData(const SkBitmap&, Type, int quality);
63    static bool EncodeFile(const char file[], const SkBitmap&, Type,
64                           int quality);
65    static bool EncodeStream(SkWStream*, const SkBitmap&, Type,
66                           int quality);
67
68protected:
69    /**
70     * Encode bitmap 'bm' in the desired format, writing results to
71     * stream 'stream', at quality level 'quality' (which can be in
72     * range 0-100).
73     *
74     * This must be overridden by each SkImageEncoder implementation.
75     */
76    virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) = 0;
77};
78
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 ();
83
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 );            \
89    }
90
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
95 *  bytes.
96 */
97DECLARE_ENCODER_CREATOR(ARGBImageEncoder);
98DECLARE_ENCODER_CREATOR(JPEGImageEncoder);
99DECLARE_ENCODER_CREATOR(PNGImageEncoder);
100DECLARE_ENCODER_CREATOR(KTXImageEncoder);
101DECLARE_ENCODER_CREATOR(WEBPImageEncoder);
102
103// Typedef to make registering encoder callback easier
104// This has to be defined outside SkImageEncoder. :(
105typedef SkTRegistry<SkImageEncoder*(*)(SkImageEncoder::Type)> SkImageEncoder_EncodeReg;
106#endif
107