blob: 6cb39be8d84ada83173a549aab4e8ce8fb29dfd4 [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 */
commit-bot@chromium.org1f2d3572014-04-04 20:13:05 +000021namespace SkDecodingImageGenerator {
halcanary@google.com3d50ea12014-01-02 13:15:13 +000022 /**
23 * These options will be passed on to the image decoder. The
24 * defaults are sensible.
halcanary@google.comd665dc42013-12-18 17:40:25 +000025 *
halcanary@google.com3d50ea12014-01-02 13:15:13 +000026 * @param fSampleSize If set to > 1, tells the decoder to return a
27 * smaller than original bitmap, sampling 1 pixel for
28 * every size pixels. e.g. if sample size is set to 3,
29 * then the returned bitmap will be 1/3 as wide and high,
30 * and will contain 1/9 as many pixels as the original.
31 * Note: this is a hint, and the codec may choose to
32 * ignore this, or only approximate the sample size.
33 *
34 * @param fDitherImage Set to true if the the decoder should try to
35 * dither the resulting image when decoding to a smaller
36 * color-space. The default is true.
37 *
38 * @param fRequestedColorType If not given, then use whichever
39 * config the decoder wants. Else try to use this color
40 * type. If the decoder won't support this color type,
41 * SkDecodingImageGenerator::Create will return
42 * NULL. kIndex_8_SkColorType is not supported.
commit-bot@chromium.org1ae492f2014-04-07 21:37:36 +000043 *
44 * @param fRequireUnpremul If true, the decoder will attempt to
45 * decode without premultiplying the alpha. If it cannot,
46 * the pixels will be set to NULL.
halcanary@google.com3d50ea12014-01-02 13:15:13 +000047 */
48 struct Options {
49 Options()
50 : fSampleSize(1)
51 , fDitherImage(true)
52 , fUseRequestedColorType(false)
commit-bot@chromium.org1ae492f2014-04-07 21:37:36 +000053 , fRequestedColorType()
54 , fRequireUnpremul(false) { }
halcanary@google.com3d50ea12014-01-02 13:15:13 +000055 Options(int sampleSize, bool dither)
56 : fSampleSize(sampleSize)
57 , fDitherImage(dither)
58 , fUseRequestedColorType(false)
commit-bot@chromium.org1ae492f2014-04-07 21:37:36 +000059 , fRequestedColorType()
60 , fRequireUnpremul(false) { }
halcanary@google.com3d50ea12014-01-02 13:15:13 +000061 Options(int sampleSize, bool dither, SkColorType colorType)
62 : fSampleSize(sampleSize)
63 , fDitherImage(dither)
64 , fUseRequestedColorType(true)
commit-bot@chromium.org1ae492f2014-04-07 21:37:36 +000065 , fRequestedColorType(colorType)
66 , fRequireUnpremul(false) { }
67 Options(int sampleSize, bool dither, SkColorType colorType,
68 bool requireUnpremul)
69 : fSampleSize(sampleSize)
70 , fDitherImage(dither)
71 , fUseRequestedColorType(true)
72 , fRequestedColorType(colorType)
73 , fRequireUnpremul(requireUnpremul) { }
halcanary@google.com3d50ea12014-01-02 13:15:13 +000074 const int fSampleSize;
75 const bool fDitherImage;
76 const bool fUseRequestedColorType;
77 const SkColorType fRequestedColorType;
commit-bot@chromium.org1ae492f2014-04-07 21:37:36 +000078 const bool fRequireUnpremul;
halcanary@google.com3d50ea12014-01-02 13:15:13 +000079 };
80
81 /**
82 * These two functions return a SkImageGenerator that calls into
83 * SkImageDecoder. They return NULL on failure.
84 *
85 * The SkData version of this function is preferred. If the stream
86 * has an underlying SkData (such as a SkMemoryStream) pass that in.
87 *
88 * This object will unref the stream when done or on failure. Since
89 * streams have internal state (position), the caller should not pass
90 * a shared stream in. Pass either a new duplicated stream in or
91 * transfer ownership of the stream. This factory asserts
halcanary@google.com29d96932013-12-09 13:45:02 +000092 * stream->unique().
93 *
94 * For example:
95 * SkStreamRewindable* stream;
96 * ...
97 * SkImageGenerator* gen
halcanary@google.com3d50ea12014-01-02 13:15:13 +000098 * = SkDecodingImageGenerator::Create(
99 * stream->duplicate(), SkDecodingImageGenerator::Options());
halcanary@google.com29d96932013-12-09 13:45:02 +0000100 * ...
101 * SkDELETE(gen);
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000102 *
103 * @param Options (see above)
104 *
105 * @return NULL on failure, a new SkImageGenerator on success.
halcanary@google.com29d96932013-12-09 13:45:02 +0000106 */
commit-bot@chromium.org1f2d3572014-04-04 20:13:05 +0000107 SkImageGenerator* Create(SkStreamRewindable* stream,
108 const Options& opt);
halcanary@google.comad04eb42013-11-21 15:32:08 +0000109
110 /**
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000111 * @param data Contains the encoded image data that will be used by
112 * the SkDecodingImageGenerator. Will be ref()ed by the
113 * SkImageGenerator constructor and and unref()ed on deletion.
halcanary@google.comad04eb42013-11-21 15:32:08 +0000114 */
commit-bot@chromium.org1f2d3572014-04-04 20:13:05 +0000115 SkImageGenerator* Create(SkData* data, const Options& opt);
halcanary@google.comad04eb42013-11-21 15:32:08 +0000116};
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000117
118// // Example of most basic use case:
119//
120// bool install_data(SkData* data, SkBitmap* dst) {
121// return SkInstallDiscardablePixelRef(
122// SkDecodingImageGenerator::Create(
123// data, SkDecodingImageGenerator::Options()), dst, NULL);
124// }
125// bool install_stream(SkStreamRewindable* stream, SkBitmap* dst) {
126// return SkInstallDiscardablePixelRef(
127// SkDecodingImageGenerator::Create(
128// stream, SkDecodingImageGenerator::Options()), dst, NULL);
129// }
130
halcanary@google.comad04eb42013-11-21 15:32:08 +0000131#endif // SkDecodingImageGenerator_DEFINED