1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/images/SkDecodingImageGenerator.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,140 @@ 1.4 +/* 1.5 + * Copyright 2013 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#ifndef SkDecodingImageGenerator_DEFINED 1.12 +#define SkDecodingImageGenerator_DEFINED 1.13 + 1.14 +#include "SkBitmap.h" 1.15 +#include "SkImageGenerator.h" 1.16 + 1.17 +class SkData; 1.18 +class SkStreamRewindable; 1.19 + 1.20 +/** 1.21 + * An implementation of SkImageGenerator that calls into 1.22 + * SkImageDecoder. 1.23 + */ 1.24 +class SkDecodingImageGenerator : public SkImageGenerator { 1.25 +public: 1.26 + virtual ~SkDecodingImageGenerator(); 1.27 + virtual SkData* refEncodedData() SK_OVERRIDE; 1.28 + // This implementaion of getInfo() always returns true. 1.29 + virtual bool getInfo(SkImageInfo* info) SK_OVERRIDE; 1.30 + virtual bool getPixels(const SkImageInfo& info, 1.31 + void* pixels, 1.32 + size_t rowBytes) SK_OVERRIDE; 1.33 + /** 1.34 + * These options will be passed on to the image decoder. The 1.35 + * defaults are sensible. 1.36 + * 1.37 + * @param fSampleSize If set to > 1, tells the decoder to return a 1.38 + * smaller than original bitmap, sampling 1 pixel for 1.39 + * every size pixels. e.g. if sample size is set to 3, 1.40 + * then the returned bitmap will be 1/3 as wide and high, 1.41 + * and will contain 1/9 as many pixels as the original. 1.42 + * Note: this is a hint, and the codec may choose to 1.43 + * ignore this, or only approximate the sample size. 1.44 + * 1.45 + * @param fDitherImage Set to true if the the decoder should try to 1.46 + * dither the resulting image when decoding to a smaller 1.47 + * color-space. The default is true. 1.48 + * 1.49 + * @param fRequestedColorType If not given, then use whichever 1.50 + * config the decoder wants. Else try to use this color 1.51 + * type. If the decoder won't support this color type, 1.52 + * SkDecodingImageGenerator::Create will return 1.53 + * NULL. kIndex_8_SkColorType is not supported. 1.54 + */ 1.55 + struct Options { 1.56 + Options() 1.57 + : fSampleSize(1) 1.58 + , fDitherImage(true) 1.59 + , fUseRequestedColorType(false) 1.60 + , fRequestedColorType() { } 1.61 + Options(int sampleSize, bool dither) 1.62 + : fSampleSize(sampleSize) 1.63 + , fDitherImage(dither) 1.64 + , fUseRequestedColorType(false) 1.65 + , fRequestedColorType() { } 1.66 + Options(int sampleSize, bool dither, SkColorType colorType) 1.67 + : fSampleSize(sampleSize) 1.68 + , fDitherImage(dither) 1.69 + , fUseRequestedColorType(true) 1.70 + , fRequestedColorType(colorType) { } 1.71 + const int fSampleSize; 1.72 + const bool fDitherImage; 1.73 + const bool fUseRequestedColorType; 1.74 + const SkColorType fRequestedColorType; 1.75 + }; 1.76 + 1.77 + /** 1.78 + * These two functions return a SkImageGenerator that calls into 1.79 + * SkImageDecoder. They return NULL on failure. 1.80 + * 1.81 + * The SkData version of this function is preferred. If the stream 1.82 + * has an underlying SkData (such as a SkMemoryStream) pass that in. 1.83 + * 1.84 + * This object will unref the stream when done or on failure. Since 1.85 + * streams have internal state (position), the caller should not pass 1.86 + * a shared stream in. Pass either a new duplicated stream in or 1.87 + * transfer ownership of the stream. This factory asserts 1.88 + * stream->unique(). 1.89 + * 1.90 + * For example: 1.91 + * SkStreamRewindable* stream; 1.92 + * ... 1.93 + * SkImageGenerator* gen 1.94 + * = SkDecodingImageGenerator::Create( 1.95 + * stream->duplicate(), SkDecodingImageGenerator::Options()); 1.96 + * ... 1.97 + * SkDELETE(gen); 1.98 + * 1.99 + * @param Options (see above) 1.100 + * 1.101 + * @return NULL on failure, a new SkImageGenerator on success. 1.102 + */ 1.103 + static SkImageGenerator* Create(SkStreamRewindable* stream, 1.104 + const Options& opt); 1.105 + 1.106 + /** 1.107 + * @param data Contains the encoded image data that will be used by 1.108 + * the SkDecodingImageGenerator. Will be ref()ed by the 1.109 + * SkImageGenerator constructor and and unref()ed on deletion. 1.110 + */ 1.111 + static SkImageGenerator* Create(SkData* data, const Options& opt); 1.112 + 1.113 +private: 1.114 + SkData* fData; 1.115 + SkStreamRewindable* fStream; 1.116 + const SkImageInfo fInfo; 1.117 + const int fSampleSize; 1.118 + const bool fDitherImage; 1.119 + 1.120 + SkDecodingImageGenerator(SkData* data, 1.121 + SkStreamRewindable* stream, 1.122 + const SkImageInfo& info, 1.123 + int sampleSize, 1.124 + bool ditherImage); 1.125 + static SkImageGenerator* Create(SkData*, SkStreamRewindable*, 1.126 + const Options&); 1.127 + typedef SkImageGenerator INHERITED; 1.128 +}; 1.129 + 1.130 +// // Example of most basic use case: 1.131 +// 1.132 +// bool install_data(SkData* data, SkBitmap* dst) { 1.133 +// return SkInstallDiscardablePixelRef( 1.134 +// SkDecodingImageGenerator::Create( 1.135 +// data, SkDecodingImageGenerator::Options()), dst, NULL); 1.136 +// } 1.137 +// bool install_stream(SkStreamRewindable* stream, SkBitmap* dst) { 1.138 +// return SkInstallDiscardablePixelRef( 1.139 +// SkDecodingImageGenerator::Create( 1.140 +// stream, SkDecodingImageGenerator::Options()), dst, NULL); 1.141 +// } 1.142 + 1.143 +#endif // SkDecodingImageGenerator_DEFINED