aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/images/SkDecodingImageGenerator.h
blob: fef7c6c7c789a0f1e4d5a6af9834a649c33c04cb (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * Copyright 2013 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SkDecodingImageGenerator_DEFINED
#define SkDecodingImageGenerator_DEFINED

#include "SkBitmap.h"
#include "SkImageGenerator.h"

class SkData;
class SkStreamRewindable;

/**
 *  An implementation of SkImageGenerator that calls into
 *  SkImageDecoder.
 */
class SkDecodingImageGenerator : public SkImageGenerator {
public:
    virtual ~SkDecodingImageGenerator();
    virtual SkData* refEncodedData() SK_OVERRIDE;
    // This implementaion of getInfo() always returns true.
    virtual bool getInfo(SkImageInfo* info) SK_OVERRIDE;
    virtual bool getPixels(const SkImageInfo& info,
                           void* pixels,
                           size_t rowBytes) SK_OVERRIDE;
    /**
     *  These options will be passed on to the image decoder.  The
     *  defaults are sensible.
     *
     *  @param fSampleSize If set to > 1, tells the decoder to return a
     *         smaller than original bitmap, sampling 1 pixel for
     *         every size pixels. e.g. if sample size is set to 3,
     *         then the returned bitmap will be 1/3 as wide and high,
     *         and will contain 1/9 as many pixels as the original.
     *         Note: this is a hint, and the codec may choose to
     *         ignore this, or only approximate the sample size.
     *
     *  @param fDitherImage Set to true if the the decoder should try to
     *         dither the resulting image when decoding to a smaller
     *         color-space.  The default is true.
     *
     *  @param fRequestedColorType If not given, then use whichever
     *         config the decoder wants.  Else try to use this color
     *         type.  If the decoder won't support this color type,
     *         SkDecodingImageGenerator::Create will return
     *         NULL. kIndex_8_SkColorType is not supported.
     */
    struct Options {
        Options()
            : fSampleSize(1)
            , fDitherImage(true)
            , fUseRequestedColorType(false)
            , fRequestedColorType() { }
        Options(int sampleSize, bool dither)
            : fSampleSize(sampleSize)
            , fDitherImage(dither)
            , fUseRequestedColorType(false)
            , fRequestedColorType() { }
        Options(int sampleSize, bool dither, SkColorType colorType)
            : fSampleSize(sampleSize)
            , fDitherImage(dither)
            , fUseRequestedColorType(true)
            , fRequestedColorType(colorType) { }
        const int         fSampleSize;
        const bool        fDitherImage;
        const bool        fUseRequestedColorType;
        const SkColorType fRequestedColorType;
    };

    /**
     *  These two functions return a SkImageGenerator that calls into
     *  SkImageDecoder.  They return NULL on failure.
     *
     *  The SkData version of this function is preferred.  If the stream
     *  has an underlying SkData (such as a SkMemoryStream) pass that in.
     *
     *  This object will unref the stream when done or on failure.  Since
     *  streams have internal state (position), the caller should not pass
     *  a shared stream in.  Pass either a new duplicated stream in or
     *  transfer ownership of the stream.  This factory asserts
     *  stream->unique().
     *
     *  For example:
     *    SkStreamRewindable* stream;
     *    ...
     *    SkImageGenerator* gen
     *        = SkDecodingImageGenerator::Create(
     *            stream->duplicate(), SkDecodingImageGenerator::Options());
     *    ...
     *    SkDELETE(gen);
     *
     *  @param Options (see above)
     *
     *  @return NULL on failure, a new SkImageGenerator on success.
     */
    static SkImageGenerator* Create(SkStreamRewindable* stream,
                                    const Options& opt);

    /**
     *  @param data Contains the encoded image data that will be used by
     *         the SkDecodingImageGenerator.  Will be ref()ed by the
     *         SkImageGenerator constructor and and unref()ed on deletion.
     */
    static SkImageGenerator* Create(SkData* data, const Options& opt);

private:
    SkData*                fData;
    SkStreamRewindable*    fStream;
    const SkImageInfo      fInfo;
    const int              fSampleSize;
    const bool             fDitherImage;

    SkDecodingImageGenerator(SkData* data,
                             SkStreamRewindable* stream,
                             const SkImageInfo& info,
                             int sampleSize,
                             bool ditherImage);
    static SkImageGenerator* Create(SkData*, SkStreamRewindable*,
                                    const Options&);
    typedef SkImageGenerator INHERITED;
};

//  // Example of most basic use case:
//
//  bool install_data(SkData* data, SkBitmap* dst) {
//     return SkInstallDiscardablePixelRef(
//         SkDecodingImageGenerator::Create(
//             data, SkDecodingImageGenerator::Options()), dst, NULL);
//  }
//  bool install_stream(SkStreamRewindable* stream, SkBitmap* dst) {
//     return SkInstallDiscardablePixelRef(
//         SkDecodingImageGenerator::Create(
//             stream, SkDecodingImageGenerator::Options()), dst, NULL);
//  }

#endif  // SkDecodingImageGenerator_DEFINED