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

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
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 namespace { 17 namespace {
18 /** 18 /**
19 * Special allocator used by getPixels(). Uses preallocated memory 19 * Special allocator used by getPixels(). Uses preallocated memory
20 * provided. 20 * provided.
21 */ 21 */
22 class TargetAllocator : public SkBitmap::Allocator { 22 class TargetAllocator : public SkBitmap::Allocator {
23 public: 23 public:
24 TargetAllocator(void* target, 24 TargetAllocator(const SkImageInfo& info,
25 size_t rowBytes, 25 void* target,
26 int width, 26 size_t rowBytes)
27 int height, 27 : fInfo(info)
28 SkBitmap::Config config) 28 , fTarget(target)
29 : fTarget(target)
30 , fRowBytes(rowBytes) 29 , fRowBytes(rowBytes)
31 , fWidth(width) 30 {}
32 , fHeight(height)
33 , fConfig(config) { }
34 31
35 bool isReady() { return (fTarget != NULL); } 32 bool isReady() { return (fTarget != NULL); }
36 33
37 virtual bool allocPixelRef(SkBitmap* bm, SkColorTable* ct) { 34 virtual bool allocPixelRef(SkBitmap* bm, SkColorTable* ct) {
38 if ((NULL == fTarget) 35 if (NULL == fTarget || fInfo != bm->info()) {
39 || (fConfig != bm->config())
40 || (fWidth != bm->width())
41 || (fHeight != bm->height())
42 || (ct != NULL)) {
43 // Call default allocator. 36 // Call default allocator.
44 return bm->allocPixels(NULL, ct); 37 return bm->allocPixels(NULL, ct);
45 } 38 }
46 // make sure fRowBytes is correct. 39
47 bm->setConfig(fConfig, fWidth, fHeight, fRowBytes, bm->alphaType());
48 // TODO(halcanary): verify that all callers of this function 40 // TODO(halcanary): verify that all callers of this function
49 // will respect new RowBytes. Will be moot once rowbytes belongs 41 // will respect new RowBytes. Will be moot once rowbytes belongs
50 // to PixelRef. 42 // to PixelRef.
51 bm->setPixels(fTarget, NULL); 43 bm->installPixels(fInfo, fTarget, fRowBytes, NULL, NULL);
44
52 fTarget = NULL; // never alloc same pixels twice! 45 fTarget = NULL; // never alloc same pixels twice!
53 return true; 46 return true;
54 } 47 }
55 48
56 private: 49 private:
50 const SkImageInfo fInfo;
57 void* fTarget; // Block of memory to be supplied as pixel memory 51 void* fTarget; // Block of memory to be supplied as pixel memory
58 // in allocPixelRef. Must be large enough to hold 52 // in allocPixelRef. Must be large enough to hold
59 // a bitmap described by fWidth, fHeight, and 53 // a bitmap described fInfo and fRowBytes
scroggo 2014/02/20 22:14:43 described by*
reed1 2014/02/21 19:17:53 Done.
60 // fRowBytes. 54 const size_t fRowBytes; // rowbytes for the destination bitmap
61 size_t fRowBytes; // rowbytes for the destination bitmap 55
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; 56 typedef SkBitmap::Allocator INHERITED;
72 }; 57 };
73 58
74 // TODO(halcanary): Give this macro a better name and move it into SkTypes.h 59 // TODO(halcanary): Give this macro a better name and move it into SkTypes.h
75 #ifdef SK_DEBUG 60 #ifdef SK_DEBUG
76 #define SkCheckResult(expr, value) SkASSERT((value) == (expr)) 61 #define SkCheckResult(expr, value) SkASSERT((value) == (expr))
77 #else 62 #else
78 #define SkCheckResult(expr, value) (void)(expr) 63 #define SkCheckResult(expr, value) (void)(expr)
79 #endif 64 #endif
80 65
81 #ifdef SK_DEBUG 66 #ifdef SK_DEBUG
82 inline bool check_alpha(SkAlphaType reported, SkAlphaType actual) { 67 inline bool check_alpha(SkAlphaType reported, SkAlphaType actual) {
83 return ((reported == actual) 68 return ((reported == actual)
84 || ((reported == kPremul_SkAlphaType) 69 || ((reported == kPremul_SkAlphaType)
85 && (actual == kOpaque_SkAlphaType))); 70 && (actual == kOpaque_SkAlphaType)));
86 } 71 }
87 #endif // SK_DEBUG 72 #endif // SK_DEBUG
88 73
89 } // namespace 74 } // namespace
90 //////////////////////////////////////////////////////////////////////////////// 75 ////////////////////////////////////////////////////////////////////////////////
91 76
92 SkDecodingImageGenerator::SkDecodingImageGenerator( 77 SkDecodingImageGenerator::SkDecodingImageGenerator(
93 SkData* data, 78 SkData* data,
94 SkStreamRewindable* stream, 79 SkStreamRewindable* stream,
95 const SkImageInfo& info, 80 const SkImageInfo& info,
96 int sampleSize, 81 int sampleSize,
97 bool ditherImage, 82 bool ditherImage)
98 SkBitmap::Config requestedConfig)
99 : fData(data) 83 : fData(data)
100 , fStream(stream) 84 , fStream(stream)
101 , fInfo(info) 85 , fInfo(info)
102 , fSampleSize(sampleSize) 86 , fSampleSize(sampleSize)
103 , fDitherImage(ditherImage) 87 , fDitherImage(ditherImage)
104 , fRequestedConfig(requestedConfig) { 88 {
105 SkASSERT(stream != NULL); 89 SkASSERT(stream != NULL);
106 SkSafeRef(fData); // may be NULL. 90 SkSafeRef(fData); // may be NULL.
107 } 91 }
108 92
109 SkDecodingImageGenerator::~SkDecodingImageGenerator() { 93 SkDecodingImageGenerator::~SkDecodingImageGenerator() {
110 SkSafeUnref(fData); 94 SkSafeUnref(fData);
111 fStream->unref(); 95 fStream->unref();
112 } 96 }
113 97
114 bool SkDecodingImageGenerator::getInfo(SkImageInfo* info) { 98 bool SkDecodingImageGenerator::getInfo(SkImageInfo* info) {
(...skipping 29 matching lines...) Expand all
144 size_t rowBytes) { 128 size_t rowBytes) {
145 if (NULL == pixels) { 129 if (NULL == pixels) {
146 return false; 130 return false;
147 } 131 }
148 if (fInfo != info) { 132 if (fInfo != info) {
149 // The caller has specified a different info. This is an 133 // The caller has specified a different info. This is an
150 // error for this kind of SkImageGenerator. Use the Options 134 // error for this kind of SkImageGenerator. Use the Options
151 // to change the settings. 135 // to change the settings.
152 return false; 136 return false;
153 } 137 }
154 int bpp = SkBitmap::ComputeBytesPerPixel(fRequestedConfig); 138 if (info.minRowBytes() > rowBytes) {
155 if (static_cast<size_t>(bpp * info.fWidth) > rowBytes) {
156 // The caller has specified a bad rowBytes. 139 // The caller has specified a bad rowBytes.
157 return false; 140 return false;
158 } 141 }
159 142
160 SkAssertResult(fStream->rewind()); 143 SkAssertResult(fStream->rewind());
161 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); 144 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream));
162 if (NULL == decoder.get()) { 145 if (NULL == decoder.get()) {
163 return false; 146 return false;
164 } 147 }
165 decoder->setDitherImage(fDitherImage); 148 decoder->setDitherImage(fDitherImage);
166 decoder->setSampleSize(fSampleSize); 149 decoder->setSampleSize(fSampleSize);
167 150
168 SkBitmap bitmap; 151 SkBitmap bitmap;
169 TargetAllocator allocator(pixels, rowBytes, info.fWidth, 152 TargetAllocator allocator(fInfo, pixels, rowBytes);
170 info.fHeight, fRequestedConfig);
171 decoder->setAllocator(&allocator); 153 decoder->setAllocator(&allocator);
172 bool success = decoder->decode(fStream, &bitmap, fRequestedConfig, 154 // TODO: need to be able to pass colortype directly to decoder
155 SkBitmap::Config legacyConfig = SkColorTypeToBitmapConfig(info.colorType());
156 bool success = decoder->decode(fStream, &bitmap, legacyConfig,
173 SkImageDecoder::kDecodePixels_Mode); 157 SkImageDecoder::kDecodePixels_Mode);
174 decoder->setAllocator(NULL); 158 decoder->setAllocator(NULL);
175 if (!success) { 159 if (!success) {
176 return false; 160 return false;
177 } 161 }
178 if (allocator.isReady()) { // Did not use pixels! 162 if (allocator.isReady()) { // Did not use pixels!
179 SkBitmap bm; 163 SkBitmap bm;
180 SkASSERT(bitmap.canCopyTo(fRequestedConfig)); 164 SkASSERT(bitmap.canCopyTo(info.colorType()));
181 if (!bitmap.copyTo(&bm, fRequestedConfig, &allocator) 165 if (!bitmap.copyTo(&bm, info.colorType(), &allocator)
182 || allocator.isReady()) { 166 || allocator.isReady()) {
183 SkDEBUGFAIL("bitmap.copyTo(requestedConfig) failed."); 167 SkDEBUGFAIL("bitmap.copyTo(requestedConfig) failed.");
184 // Earlier we checked canCopyto(); we expect consistency. 168 // Earlier we checked canCopyto(); we expect consistency.
185 return false; 169 return false;
186 } 170 }
187 SkASSERT(check_alpha(fInfo.fAlphaType, bm.alphaType())); 171 SkASSERT(check_alpha(info.alphaType(), bm.alphaType()));
188 } else { 172 } else {
189 SkASSERT(check_alpha(fInfo.fAlphaType, bitmap.alphaType())); 173 SkASSERT(check_alpha(info.alphaType(), bitmap.alphaType()));
190 } 174 }
191 return true; 175 return true;
192 } 176 }
193 177
194 SkImageGenerator* SkDecodingImageGenerator::Create( 178 SkImageGenerator* SkDecodingImageGenerator::Create(
195 SkData* data, 179 SkData* data,
196 const SkDecodingImageGenerator::Options& opts) { 180 const SkDecodingImageGenerator::Options& opts) {
197 SkASSERT(data != NULL); 181 SkASSERT(data != NULL);
198 if (NULL == data) { 182 if (NULL == data) {
199 return NULL; 183 return NULL;
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 info.fColorType = kPMColor_SkColorType; 243 info.fColorType = kPMColor_SkColorType;
260 info.fAlphaType = bitmap.alphaType(); 244 info.fAlphaType = bitmap.alphaType();
261 } else { 245 } else {
262 config = bitmap.config(); // Save for later! 246 config = bitmap.config(); // Save for later!
263 if (!bitmap.asImageInfo(&info)) { 247 if (!bitmap.asImageInfo(&info)) {
264 SkDEBUGFAIL("Getting SkImageInfo from bitmap failed."); 248 SkDEBUGFAIL("Getting SkImageInfo from bitmap failed.");
265 return NULL; 249 return NULL;
266 } 250 }
267 } 251 }
268 } else { 252 } else {
269 config = SkColorTypeToBitmapConfig(opts.fRequestedColorType); 253 if (!bitmap.canCopyTo(opts.fRequestedColorType)) {
270 if (!bitmap.canCopyTo(config)) { 254 SkASSERT(bitmap.colorType() != opts.fRequestedColorType);
271 SkASSERT(bitmap.config() != config);
272 return NULL; // Can not translate to needed config. 255 return NULL; // Can not translate to needed config.
273 } 256 }
274 info.fWidth = bitmap.width(); 257 info = bitmap.info();
275 info.fHeight = bitmap.height();
276 info.fColorType = opts.fRequestedColorType; 258 info.fColorType = opts.fRequestedColorType;
277 info.fAlphaType = bitmap.alphaType();
278 } 259 }
279 return SkNEW_ARGS(SkDecodingImageGenerator, 260 return SkNEW_ARGS(SkDecodingImageGenerator,
280 (data, autoStream.detach(), info, 261 (data, autoStream.detach(), info,
281 opts.fSampleSize, opts.fDitherImage, config)); 262 opts.fSampleSize, opts.fDitherImage));
282 } 263 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698