blob: 0a5ec56fe8e2207e387f23afb42eef6a21d1d556 [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 *
scroggoa1193e42015-01-21 12:09:53 -080088 * This object, if non-NULL, takes ownership of stream and deletes stream
89 * upon deletion. If NULL is returned, stream is deleted immediately.
halcanary@google.com3d50ea12014-01-02 13:15:13 +000090 *
91 * @param Options (see above)
92 *
93 * @return NULL on failure, a new SkImageGenerator on success.
halcanary@google.com29d96932013-12-09 13:45:02 +000094 */
commit-bot@chromium.org1f2d3572014-04-04 20:13:05 +000095 SkImageGenerator* Create(SkStreamRewindable* stream,
96 const Options& opt);
halcanary@google.comad04eb42013-11-21 15:32:08 +000097
98 /**
halcanary@google.com3d50ea12014-01-02 13:15:13 +000099 * @param data Contains the encoded image data that will be used by
100 * the SkDecodingImageGenerator. Will be ref()ed by the
101 * SkImageGenerator constructor and and unref()ed on deletion.
halcanary@google.comad04eb42013-11-21 15:32:08 +0000102 */
commit-bot@chromium.org1f2d3572014-04-04 20:13:05 +0000103 SkImageGenerator* Create(SkData* data, const Options& opt);
halcanary@google.comad04eb42013-11-21 15:32:08 +0000104};
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000105
106// // Example of most basic use case:
107//
108// bool install_data(SkData* data, SkBitmap* dst) {
109// return SkInstallDiscardablePixelRef(
110// SkDecodingImageGenerator::Create(
111// data, SkDecodingImageGenerator::Options()), dst, NULL);
112// }
113// bool install_stream(SkStreamRewindable* stream, SkBitmap* dst) {
114// return SkInstallDiscardablePixelRef(
115// SkDecodingImageGenerator::Create(
116// stream, SkDecodingImageGenerator::Options()), dst, NULL);
117// }
118
halcanary@google.comad04eb42013-11-21 15:32:08 +0000119#endif // SkDecodingImageGenerator_DEFINED