aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/images/SkImageEncoderPriv.h
blob: 9b632f5a5b33e75d2f7ae1193b9be6acce0eebc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SkImageEncoderPriv_DEFINED
#define SkImageEncoderPriv_DEFINED

#include "SkImageEncoder.h"
#include "SkTRegistry.h"

#ifndef SK_SUPPORT_LEGACY_IMAGE_ENCODER_CLASS

    // TODO(halcanary): replace this class and registry system with something simpler.
    class SkImageEncoder {
    public:
        typedef SkEncodedImageFormat Type;
        static SkImageEncoder* Create(SkEncodedImageFormat);

        virtual ~SkImageEncoder() {}

        bool encodeStream(SkWStream* dst, const SkBitmap& src, int quality) {
            return this->onEncode(dst, src, SkMin32(100, SkMax32(0, quality)));
        }

    protected:
        /**
         * Encode bitmap 'bm' in the desired format, writing results to
         * stream 'stream', at quality level 'quality' (which can be in
         * range 0-100).
         */
        virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) = 0;
    };

#endif  // SK_SUPPORT_LEGACY_IMAGE_ENCODER_CLASS

// This macro declares a global (i.e., non-class owned) creation entry point
// for each encoder (e.g., CreateJPEGImageEncoder)
#define DECLARE_ENCODER_CREATOR(codec)          \
    SK_API SkImageEncoder *Create ## codec ();

// This macro defines the global creation entry point for each encoder. Each
// encoder implementation that registers with the encoder factory must call it.
#define DEFINE_ENCODER_CREATOR(codec) \
    SkImageEncoder* Create##codec() { return new Sk##codec; }

// All the encoders known by Skia. Note that, depending on the compiler settings,
// not all of these will be available
DECLARE_ENCODER_CREATOR(JPEGImageEncoder);
DECLARE_ENCODER_CREATOR(PNGImageEncoder);
DECLARE_ENCODER_CREATOR(KTXImageEncoder);
DECLARE_ENCODER_CREATOR(WEBPImageEncoder);

#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
SkImageEncoder* CreateImageEncoder_CG(SkImageEncoder::Type type);
#endif

#if defined(SK_BUILD_FOR_WIN)
SkImageEncoder* CreateImageEncoder_WIC(SkImageEncoder::Type type);
#endif

// Typedef to make registering encoder callback easier
// This has to be defined outside SkImageEncoder. :(
typedef SkTRegistry<SkImageEncoder*(*)(SkImageEncoder::Type)> SkImageEncoder_EncodeReg;

#endif // SkImageEncoderPriv_DEFINED