blob: 12a49d59c47e6c6b89c248a7bd88341d35633da3 [file] [log] [blame]
halcanary@google.comad04eb42013-11-21 15:32:08 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkDecodingImageGenerator_DEFINED
9#define SkDecodingImageGenerator_DEFINED
10
halcanary@google.com3d50ea12014-01-02 13:15:13 +000011#include "SkBitmap.h"
halcanary@google.comad04eb42013-11-21 15:32:08 +000012#include "SkImageGenerator.h"
13
halcanary@google.com3d50ea12014-01-02 13:15:13 +000014class SkData;
halcanary@google.com29d96932013-12-09 13:45:02 +000015class SkStreamRewindable;
halcanary@google.comad04eb42013-11-21 15:32:08 +000016
17/**
halcanary@google.com3d50ea12014-01-02 13:15:13 +000018 * An implementation of SkImageGenerator that calls into
19 * SkImageDecoder.
halcanary@google.comad04eb42013-11-21 15:32:08 +000020 */
21class SkDecodingImageGenerator : public SkImageGenerator {
22public:
halcanary@google.com3d50ea12014-01-02 13:15:13 +000023 virtual ~SkDecodingImageGenerator();
24 virtual SkData* refEncodedData() SK_OVERRIDE;
25 // This implementaion of getInfo() always returns true.
26 virtual bool getInfo(SkImageInfo* info) SK_OVERRIDE;
27 virtual bool getPixels(const SkImageInfo& info,
28 void* pixels,
29 size_t rowBytes) SK_OVERRIDE;
30 /**
31 * These options will be passed on to the image decoder. The
32 * defaults are sensible.
halcanary@google.comd665dc42013-12-18 17:40:25 +000033 *
halcanary@google.com3d50ea12014-01-02 13:15:13 +000034 * @param fSampleSize If set to > 1, tells the decoder to return a
35 * smaller than original bitmap, sampling 1 pixel for
36 * every size pixels. e.g. if sample size is set to 3,
37 * then the returned bitmap will be 1/3 as wide and high,
38 * and will contain 1/9 as many pixels as the original.
39 * Note: this is a hint, and the codec may choose to
40 * ignore this, or only approximate the sample size.
41 *
42 * @param fDitherImage Set to true if the the decoder should try to
43 * dither the resulting image when decoding to a smaller
44 * color-space. The default is true.
45 *
46 * @param fRequestedColorType If not given, then use whichever
47 * config the decoder wants. Else try to use this color
48 * type. If the decoder won't support this color type,
49 * SkDecodingImageGenerator::Create will return
50 * NULL. kIndex_8_SkColorType is not supported.
51 */
52 struct Options {
53 Options()
54 : fSampleSize(1)
55 , fDitherImage(true)
56 , fUseRequestedColorType(false)
57 , fRequestedColorType() { }
58 Options(int sampleSize, bool dither)
59 : fSampleSize(sampleSize)
60 , fDitherImage(dither)
61 , fUseRequestedColorType(false)
62 , fRequestedColorType() { }
63 Options(int sampleSize, bool dither, SkColorType colorType)
64 : fSampleSize(sampleSize)
65 , fDitherImage(dither)
66 , fUseRequestedColorType(true)
67 , fRequestedColorType(colorType) { }
68 const int fSampleSize;
69 const bool fDitherImage;
70 const bool fUseRequestedColorType;
71 const SkColorType fRequestedColorType;
72 };
73
74 /**
75 * These two functions return a SkImageGenerator that calls into
76 * SkImageDecoder. They return NULL on failure.
77 *
78 * The SkData version of this function is preferred. If the stream
79 * has an underlying SkData (such as a SkMemoryStream) pass that in.
80 *
81 * This object will unref the stream when done or on failure. Since
82 * streams have internal state (position), the caller should not pass
83 * a shared stream in. Pass either a new duplicated stream in or
84 * transfer ownership of the stream. This factory asserts
halcanary@google.com29d96932013-12-09 13:45:02 +000085 * stream->unique().
86 *
87 * For example:
88 * SkStreamRewindable* stream;
89 * ...
90 * SkImageGenerator* gen
halcanary@google.com3d50ea12014-01-02 13:15:13 +000091 * = SkDecodingImageGenerator::Create(
92 * stream->duplicate(), SkDecodingImageGenerator::Options());
halcanary@google.com29d96932013-12-09 13:45:02 +000093 * ...
94 * SkDELETE(gen);
halcanary@google.com3d50ea12014-01-02 13:15:13 +000095 *
96 * @param Options (see above)
97 *
98 * @return NULL on failure, a new SkImageGenerator on success.
halcanary@google.com29d96932013-12-09 13:45:02 +000099 */
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000100 static SkImageGenerator* Create(SkStreamRewindable* stream,
101 const Options& opt);
halcanary@google.comad04eb42013-11-21 15:32:08 +0000102
103 /**
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000104 * @param data Contains the encoded image data that will be used by
105 * the SkDecodingImageGenerator. Will be ref()ed by the
106 * SkImageGenerator constructor and and unref()ed on deletion.
halcanary@google.comad04eb42013-11-21 15:32:08 +0000107 */
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000108 static SkImageGenerator* Create(SkData* data, const Options& opt);
halcanary@google.comad04eb42013-11-21 15:32:08 +0000109
110private:
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000111 SkData* fData;
112 SkStreamRewindable* fStream;
113 const SkImageInfo fInfo;
114 const int fSampleSize;
115 const bool fDitherImage;
116 const SkBitmap::Config fRequestedConfig;
117 SkDecodingImageGenerator(SkData* data,
118 SkStreamRewindable* stream,
119 const SkImageInfo& info,
120 int sampleSize,
121 bool ditherImage,
122 SkBitmap::Config requestedConfig);
123 static SkImageGenerator* Create(SkData*, SkStreamRewindable*,
124 const Options&);
125 typedef SkImageGenerator INHERITED;
halcanary@google.comad04eb42013-11-21 15:32:08 +0000126};
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000127
128// // Example of most basic use case:
129//
130// bool install_data(SkData* data, SkBitmap* dst) {
131// return SkInstallDiscardablePixelRef(
132// SkDecodingImageGenerator::Create(
133// data, SkDecodingImageGenerator::Options()), dst, NULL);
134// }
135// bool install_stream(SkStreamRewindable* stream, SkBitmap* dst) {
136// return SkInstallDiscardablePixelRef(
137// SkDecodingImageGenerator::Create(
138// stream, SkDecodingImageGenerator::Options()), dst, NULL);
139// }
140
halcanary@google.comad04eb42013-11-21 15:32:08 +0000141#endif // SkDecodingImageGenerator_DEFINED