Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(222)

Side by Side Diff: src/images/SkDecodingImageGenerator.cpp

Issue 171723007: add new copyTo version to SkBitmap, which takes SkColorType (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/images/SkDecodingImageGenerator.h ('k') | src/ports/SkImageDecoder_CG.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkData.h" 8 #include "SkData.h"
9 #include "SkDecodingImageGenerator.h" 9 #include "SkDecodingImageGenerator.h"
10 #include "SkImageDecoder.h" 10 #include "SkImageDecoder.h"
11 #include "SkImageInfo.h" 11 #include "SkImageInfo.h"
12 #include "SkImageGenerator.h" 12 #include "SkImageGenerator.h"
13 #include "SkImagePriv.h" 13 #include "SkImagePriv.h"
14 #include "SkStream.h" 14 #include "SkStream.h"
15 #include "SkUtils.h" 15 #include "SkUtils.h"
16 16
17 static bool equal_modulo_alpha(const SkImageInfo& a, const SkImageInfo& b) {
18 return a.width() == b.width() && a.height() == b.height() &&
19 a.colorType() == b.colorType();
20 }
21
17 namespace { 22 namespace {
18 /** 23 /**
19 * Special allocator used by getPixels(). Uses preallocated memory 24 * Special allocator used by getPixels(). Uses preallocated memory
20 * provided. 25 * provided if possible, else fall-back on the default allocator
21 */ 26 */
22 class TargetAllocator : public SkBitmap::Allocator { 27 class TargetAllocator : public SkBitmap::Allocator {
23 public: 28 public:
24 TargetAllocator(void* target, 29 TargetAllocator(const SkImageInfo& info,
25 size_t rowBytes, 30 void* target,
26 int width, 31 size_t rowBytes)
27 int height, 32 : fInfo(info)
28 SkBitmap::Config config) 33 , fTarget(target)
29 : fTarget(target)
30 , fRowBytes(rowBytes) 34 , fRowBytes(rowBytes)
31 , fWidth(width) 35 {}
32 , fHeight(height)
33 , fConfig(config) { }
34 36
35 bool isReady() { return (fTarget != NULL); } 37 bool isReady() { return (fTarget != NULL); }
36 38
37 virtual bool allocPixelRef(SkBitmap* bm, SkColorTable* ct) { 39 virtual bool allocPixelRef(SkBitmap* bm, SkColorTable* ct) {
38 if ((NULL == fTarget) 40 if (NULL == fTarget || !equal_modulo_alpha(fInfo, bm->info())) {
39 || (fConfig != bm->config())
40 || (fWidth != bm->width())
41 || (fHeight != bm->height())
42 || (ct != NULL)) {
43 // Call default allocator. 41 // Call default allocator.
44 return bm->allocPixels(NULL, ct); 42 return bm->allocPixels(NULL, ct);
45 } 43 }
46 // make sure fRowBytes is correct. 44
47 bm->setConfig(fConfig, fWidth, fHeight, fRowBytes, bm->alphaType());
48 // TODO(halcanary): verify that all callers of this function 45 // TODO(halcanary): verify that all callers of this function
49 // will respect new RowBytes. Will be moot once rowbytes belongs 46 // will respect new RowBytes. Will be moot once rowbytes belongs
50 // to PixelRef. 47 // to PixelRef.
51 bm->setPixels(fTarget, NULL); 48 bm->installPixels(fInfo, fTarget, fRowBytes, NULL, NULL);
49
52 fTarget = NULL; // never alloc same pixels twice! 50 fTarget = NULL; // never alloc same pixels twice!
53 return true; 51 return true;
54 } 52 }
55 53
56 private: 54 private:
55 const SkImageInfo fInfo;
57 void* fTarget; // Block of memory to be supplied as pixel memory 56 void* fTarget; // Block of memory to be supplied as pixel memory
58 // in allocPixelRef. Must be large enough to hold 57 // in allocPixelRef. Must be large enough to hold
59 // a bitmap described by fWidth, fHeight, and 58 // a bitmap described by fInfo and fRowBytes
60 // fRowBytes. 59 const size_t fRowBytes; // rowbytes for the destination bitmap
61 size_t fRowBytes; // rowbytes for the destination bitmap 60
62 int fWidth; // Along with fHeight and fConfig, the information
63 int fHeight; // about the bitmap whose pixels this allocator is
64 // expected to allocate. If they do not match the
65 // bitmap passed to allocPixelRef, it is assumed
66 // that the bitmap will be copied to a bitmap with
67 // the correct info using this allocator, so the
68 // default allocator will be used instead of
69 // fTarget.
70 SkBitmap::Config fConfig;
71 typedef SkBitmap::Allocator INHERITED; 61 typedef SkBitmap::Allocator INHERITED;
72 }; 62 };
73 63
74 // TODO(halcanary): Give this macro a better name and move it into SkTypes.h 64 // TODO(halcanary): Give this macro a better name and move it into SkTypes.h
75 #ifdef SK_DEBUG 65 #ifdef SK_DEBUG
76 #define SkCheckResult(expr, value) SkASSERT((value) == (expr)) 66 #define SkCheckResult(expr, value) SkASSERT((value) == (expr))
77 #else 67 #else
78 #define SkCheckResult(expr, value) (void)(expr) 68 #define SkCheckResult(expr, value) (void)(expr)
79 #endif 69 #endif
80 70
81 #ifdef SK_DEBUG 71 #ifdef SK_DEBUG
82 inline bool check_alpha(SkAlphaType reported, SkAlphaType actual) { 72 inline bool check_alpha(SkAlphaType reported, SkAlphaType actual) {
83 return ((reported == actual) 73 return ((reported == actual)
84 || ((reported == kPremul_SkAlphaType) 74 || ((reported == kPremul_SkAlphaType)
85 && (actual == kOpaque_SkAlphaType))); 75 && (actual == kOpaque_SkAlphaType)));
86 } 76 }
87 #endif // SK_DEBUG 77 #endif // SK_DEBUG
88 78
89 } // namespace 79 } // namespace
90 //////////////////////////////////////////////////////////////////////////////// 80 ////////////////////////////////////////////////////////////////////////////////
91 81
92 SkDecodingImageGenerator::SkDecodingImageGenerator( 82 SkDecodingImageGenerator::SkDecodingImageGenerator(
93 SkData* data, 83 SkData* data,
94 SkStreamRewindable* stream, 84 SkStreamRewindable* stream,
95 const SkImageInfo& info, 85 const SkImageInfo& info,
96 int sampleSize, 86 int sampleSize,
97 bool ditherImage, 87 bool ditherImage)
98 SkBitmap::Config requestedConfig)
99 : fData(data) 88 : fData(data)
100 , fStream(stream) 89 , fStream(stream)
101 , fInfo(info) 90 , fInfo(info)
102 , fSampleSize(sampleSize) 91 , fSampleSize(sampleSize)
103 , fDitherImage(ditherImage) 92 , fDitherImage(ditherImage)
104 , fRequestedConfig(requestedConfig) { 93 {
105 SkASSERT(stream != NULL); 94 SkASSERT(stream != NULL);
106 SkSafeRef(fData); // may be NULL. 95 SkSafeRef(fData); // may be NULL.
107 } 96 }
108 97
109 SkDecodingImageGenerator::~SkDecodingImageGenerator() { 98 SkDecodingImageGenerator::~SkDecodingImageGenerator() {
110 SkSafeUnref(fData); 99 SkSafeUnref(fData);
111 fStream->unref(); 100 fStream->unref();
112 } 101 }
113 102
114 bool SkDecodingImageGenerator::getInfo(SkImageInfo* info) { 103 bool SkDecodingImageGenerator::getInfo(SkImageInfo* info) {
(...skipping 29 matching lines...) Expand all
144 size_t rowBytes) { 133 size_t rowBytes) {
145 if (NULL == pixels) { 134 if (NULL == pixels) {
146 return false; 135 return false;
147 } 136 }
148 if (fInfo != info) { 137 if (fInfo != info) {
149 // The caller has specified a different info. This is an 138 // The caller has specified a different info. This is an
150 // error for this kind of SkImageGenerator. Use the Options 139 // error for this kind of SkImageGenerator. Use the Options
151 // to change the settings. 140 // to change the settings.
152 return false; 141 return false;
153 } 142 }
154 int bpp = SkBitmap::ComputeBytesPerPixel(fRequestedConfig); 143 if (info.minRowBytes() > rowBytes) {
155 if (static_cast<size_t>(bpp * info.fWidth) > rowBytes) {
156 // The caller has specified a bad rowBytes. 144 // The caller has specified a bad rowBytes.
157 return false; 145 return false;
158 } 146 }
159 147
160 SkAssertResult(fStream->rewind()); 148 SkAssertResult(fStream->rewind());
161 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); 149 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream));
162 if (NULL == decoder.get()) { 150 if (NULL == decoder.get()) {
163 return false; 151 return false;
164 } 152 }
165 decoder->setDitherImage(fDitherImage); 153 decoder->setDitherImage(fDitherImage);
166 decoder->setSampleSize(fSampleSize); 154 decoder->setSampleSize(fSampleSize);
167 155
168 SkBitmap bitmap; 156 SkBitmap bitmap;
169 TargetAllocator allocator(pixels, rowBytes, info.fWidth, 157 TargetAllocator allocator(fInfo, pixels, rowBytes);
170 info.fHeight, fRequestedConfig);
171 decoder->setAllocator(&allocator); 158 decoder->setAllocator(&allocator);
172 bool success = decoder->decode(fStream, &bitmap, fRequestedConfig, 159 // TODO: need to be able to pass colortype directly to decoder
160 SkBitmap::Config legacyConfig = SkColorTypeToBitmapConfig(info.colorType());
161 bool success = decoder->decode(fStream, &bitmap, legacyConfig,
173 SkImageDecoder::kDecodePixels_Mode); 162 SkImageDecoder::kDecodePixels_Mode);
174 decoder->setAllocator(NULL); 163 decoder->setAllocator(NULL);
175 if (!success) { 164 if (!success) {
176 return false; 165 return false;
177 } 166 }
178 if (allocator.isReady()) { // Did not use pixels! 167 if (allocator.isReady()) { // Did not use pixels!
179 SkBitmap bm; 168 SkBitmap bm;
180 SkASSERT(bitmap.canCopyTo(fRequestedConfig)); 169 SkASSERT(bitmap.canCopyTo(info.colorType()));
181 if (!bitmap.copyTo(&bm, fRequestedConfig, &allocator) 170 bool copySuccess = bitmap.copyTo(&bm, info.colorType(), &allocator);
182 || allocator.isReady()) { 171 if (!copySuccess || allocator.isReady()) {
183 SkDEBUGFAIL("bitmap.copyTo(requestedConfig) failed."); 172 SkDEBUGFAIL("bitmap.copyTo(requestedConfig) failed.");
184 // Earlier we checked canCopyto(); we expect consistency. 173 // Earlier we checked canCopyto(); we expect consistency.
185 return false; 174 return false;
186 } 175 }
187 SkASSERT(check_alpha(fInfo.fAlphaType, bm.alphaType())); 176 SkASSERT(check_alpha(info.alphaType(), bm.alphaType()));
188 } else { 177 } else {
189 SkASSERT(check_alpha(fInfo.fAlphaType, bitmap.alphaType())); 178 SkASSERT(check_alpha(info.alphaType(), bitmap.alphaType()));
190 } 179 }
191 return true; 180 return true;
192 } 181 }
193 182
194 SkImageGenerator* SkDecodingImageGenerator::Create( 183 SkImageGenerator* SkDecodingImageGenerator::Create(
195 SkData* data, 184 SkData* data,
196 const SkDecodingImageGenerator::Options& opts) { 185 const SkDecodingImageGenerator::Options& opts) {
197 SkASSERT(data != NULL); 186 SkASSERT(data != NULL);
198 if (NULL == data) { 187 if (NULL == data) {
199 return NULL; 188 return NULL;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 SkBitmap bitmap; 227 SkBitmap bitmap;
239 decoder->setSampleSize(opts.fSampleSize); 228 decoder->setSampleSize(opts.fSampleSize);
240 if (!decoder->decode(stream, &bitmap, 229 if (!decoder->decode(stream, &bitmap,
241 SkImageDecoder::kDecodeBounds_Mode)) { 230 SkImageDecoder::kDecodeBounds_Mode)) {
242 return NULL; 231 return NULL;
243 } 232 }
244 if (bitmap.config() == SkBitmap::kNo_Config) { 233 if (bitmap.config() == SkBitmap::kNo_Config) {
245 return NULL; 234 return NULL;
246 } 235 }
247 236
248 SkImageInfo info; 237 SkImageInfo info = bitmap.info();
249 SkBitmap::Config config;
250 238
251 if (!opts.fUseRequestedColorType) { 239 if (!opts.fUseRequestedColorType) {
252 // Use default config. 240 // Use default
253 if (SkBitmap::kIndex8_Config == bitmap.config()) { 241 if (kIndex_8_SkColorType == bitmap.colorType()) {
254 // We don't support kIndex8 because we don't support 242 // We don't support kIndex8 because we don't support
255 // colortables in this workflow. 243 // colortables in this workflow.
256 config = SkBitmap::kARGB_8888_Config;
257 info.fWidth = bitmap.width();
258 info.fHeight = bitmap.height();
259 info.fColorType = kPMColor_SkColorType; 244 info.fColorType = kPMColor_SkColorType;
260 info.fAlphaType = bitmap.alphaType();
261 } else {
262 config = bitmap.config(); // Save for later!
263 if (!bitmap.asImageInfo(&info)) {
264 SkDEBUGFAIL("Getting SkImageInfo from bitmap failed.");
265 return NULL;
266 }
267 } 245 }
268 } else { 246 } else {
269 config = SkColorTypeToBitmapConfig(opts.fRequestedColorType); 247 if (!bitmap.canCopyTo(opts.fRequestedColorType)) {
270 if (!bitmap.canCopyTo(config)) { 248 SkASSERT(bitmap.colorType() != opts.fRequestedColorType);
271 SkASSERT(bitmap.config() != config);
272 return NULL; // Can not translate to needed config. 249 return NULL; // Can not translate to needed config.
273 } 250 }
274 info.fWidth = bitmap.width();
275 info.fHeight = bitmap.height();
276 info.fColorType = opts.fRequestedColorType; 251 info.fColorType = opts.fRequestedColorType;
277 info.fAlphaType = bitmap.alphaType();
278 } 252 }
279 return SkNEW_ARGS(SkDecodingImageGenerator, 253 return SkNEW_ARGS(SkDecodingImageGenerator,
280 (data, autoStream.detach(), info, 254 (data, autoStream.detach(), info,
281 opts.fSampleSize, opts.fDitherImage, config)); 255 opts.fSampleSize, opts.fDitherImage));
282 } 256 }
OLDNEW
« no previous file with comments | « src/images/SkDecodingImageGenerator.h ('k') | src/ports/SkImageDecoder_CG.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698