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

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

Issue 647023006: Qualify the return value of SkImageDecoder::decode (Closed) Base URL: https://skia.googlesource.com/skia.git/+/master
Patch Set: fix a sample Created 6 years, 2 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
« no previous file with comments | « src/images/SkImageDecoder_libgif.cpp ('k') | src/images/SkImageDecoder_libjpeg.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 2006 The Android Open Source Project 2 * Copyright 2006 The Android Open Source Project
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 "SkColorPriv.h" 8 #include "SkColorPriv.h"
9 #include "SkImageDecoder.h" 9 #include "SkImageDecoder.h"
10 #include "SkStream.h" 10 #include "SkStream.h"
11 #include "SkStreamPriv.h" 11 #include "SkStreamPriv.h"
12 #include "SkTypes.h" 12 #include "SkTypes.h"
13 13
14 class SkICOImageDecoder : public SkImageDecoder { 14 class SkICOImageDecoder : public SkImageDecoder {
15 public: 15 public:
16 SkICOImageDecoder(); 16 SkICOImageDecoder();
17 17
18 virtual Format getFormat() const SK_OVERRIDE { 18 virtual Format getFormat() const SK_OVERRIDE {
19 return kICO_Format; 19 return kICO_Format;
20 } 20 }
21 21
22 protected: 22 protected:
23 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE; 23 virtual Result onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE;
24 24
25 private: 25 private:
26 typedef SkImageDecoder INHERITED; 26 typedef SkImageDecoder INHERITED;
27 }; 27 };
28 28
29 //////////////////////////////////////////////////////////////////////////////// ///////// 29 //////////////////////////////////////////////////////////////////////////////// /////////
30 30
31 //read bytes starting from the begin-th index in the buffer 31 //read bytes starting from the begin-th index in the buffer
32 //read in Intel order, and return an integer 32 //read in Intel order, and return an integer
33 33
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 // In the case of a 4 bit image with an odd width, we need to add some 65 // In the case of a 4 bit image with an odd width, we need to add some
66 // so we can go off the end of the drawn bitmap. 66 // so we can go off the end of the drawn bitmap.
67 // Add 4 to ensure that it is still a multiple of 4. 67 // Add 4 to ensure that it is still a multiple of 4.
68 if (4 == bitCount && (w & 0x1)) { 68 if (4 == bitCount && (w & 0x1)) {
69 return (w + 1) << 2; 69 return (w + 1) << 2;
70 } 70 }
71 // Otherwise return 0, which will allow it to be calculated automatically. 71 // Otherwise return 0, which will allow it to be calculated automatically.
72 return 0; 72 return 0;
73 } 73 }
74 74
75 bool SkICOImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) 75 SkImageDecoder::Result SkICOImageDecoder::onDecode(SkStream* stream, SkBitmap* b m, Mode mode)
76 { 76 {
77 SkAutoMalloc autoMal; 77 SkAutoMalloc autoMal;
78 const size_t length = SkCopyStreamToStorage(&autoMal, stream); 78 const size_t length = SkCopyStreamToStorage(&autoMal, stream);
79 if (0 == length) { 79 if (0 == length) {
80 return false; 80 return kFailure;
81 } 81 }
82 82
83 unsigned char* buf = (unsigned char*)autoMal.get(); 83 unsigned char* buf = (unsigned char*)autoMal.get();
84 84
85 //these should always be the same - should i use for error checking? - what about files that have some 85 //these should always be the same - should i use for error checking? - what about files that have some
86 //incorrect values, but still decode properly? 86 //incorrect values, but still decode properly?
87 int reserved = read2Bytes(buf, 0); // 0 87 int reserved = read2Bytes(buf, 0); // 0
88 int type = read2Bytes(buf, 2); // 1 88 int type = read2Bytes(buf, 2); // 1
89 if (reserved != 0 || type != 1) 89 if (reserved != 0 || type != 1) {
90 return false; 90 return kFailure;
91 }
92
91 int count = read2Bytes(buf, 4); 93 int count = read2Bytes(buf, 4);
92 94
93 //need to at least have enough space to hold the initial table of info 95 //need to at least have enough space to hold the initial table of info
94 if (length < (size_t)(6 + count*16)) 96 if (length < (size_t)(6 + count*16)) {
95 return false; 97 return kFailure;
98 }
96 99
97 #ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER 100 #ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER
98 int choice; 101 int choice;
99 Chooser* chooser = this->getChooser(); 102 Chooser* chooser = this->getChooser();
100 //FIXME:if no chooser, consider providing the largest color image 103 //FIXME:if no chooser, consider providing the largest color image
101 //what are the odds that the largest image would be monochrome? 104 //what are the odds that the largest image would be monochrome?
102 if (NULL == chooser) { 105 if (NULL == chooser) {
103 choice = 0; 106 choice = 0;
104 } else { 107 } else {
105 chooser->begin(count); 108 chooser->begin(count);
(...skipping 24 matching lines...) Expand all
130 default: 133 default:
131 SkDEBUGF(("Image with %ibpp not supported\n", bitCount)); 134 SkDEBUGF(("Image with %ibpp not supported\n", bitCount));
132 continue; 135 continue;
133 } 136 }
134 chooser->inspect(i, c, width, height); 137 chooser->inspect(i, c, width, height);
135 } 138 }
136 choice = chooser->choose(); 139 choice = chooser->choose();
137 } 140 }
138 141
139 //you never know what the chooser is going to supply 142 //you never know what the chooser is going to supply
140 if (choice >= count || choice < 0) 143 if (choice >= count || choice < 0) {
141 return false; 144 return kFailure;
145 }
142 #else 146 #else
143 const int choice = 0; // TODO: fold this value into the expressions below 147 const int choice = 0; // TODO: fold this value into the expressions below
144 #endif 148 #endif
145 149
146 //skip ahead to the correct header 150 //skip ahead to the correct header
147 //commented out lines are not used, but if i switch to other read method, ne ed to know how many to skip 151 //commented out lines are not used, but if i switch to other read method, ne ed to know how many to skip
148 //otherwise, they could be used for error checking 152 //otherwise, they could be used for error checking
149 int w = readByte(buf, 6 + choice*16); 153 int w = readByte(buf, 6 + choice*16);
150 int h = readByte(buf, 7 + choice*16); 154 int h = readByte(buf, 7 + choice*16);
151 int colorCount = readByte(buf, 8 + choice*16); 155 int colorCount = readByte(buf, 8 + choice*16);
152 //int reservedToo = readByte(buf, 9 + choice*16); //0 156 //int reservedToo = readByte(buf, 9 + choice*16); //0
153 //int planes = read2Bytes(buf, 10 + choice*16); //1 - but often 0 157 //int planes = read2Bytes(buf, 10 + choice*16); //1 - but often 0
154 //int fakeBitCount = read2Bytes(buf, 12 + choice*16); //should be real - usu ally 0 158 //int fakeBitCount = read2Bytes(buf, 12 + choice*16); //should be real - usu ally 0
155 const size_t size = read4Bytes(buf, 14 + choice*16); //matters? 159 const size_t size = read4Bytes(buf, 14 + choice*16); //matters?
156 const size_t offset = read4Bytes(buf, 18 + choice*16); 160 const size_t offset = read4Bytes(buf, 18 + choice*16);
157 // promote the sum to 64-bits to avoid overflow 161 // promote the sum to 64-bits to avoid overflow
158 if (((uint64_t)offset + size) > length) { 162 if (((uint64_t)offset + size) > length) {
159 return false; 163 return kFailure;
160 } 164 }
161 165
162 // Check to see if this is a PNG image inside the ICO 166 // Check to see if this is a PNG image inside the ICO
163 { 167 {
164 SkMemoryStream subStream(buf + offset, size, false); 168 SkMemoryStream subStream(buf + offset, size, false);
165 SkAutoTDelete<SkImageDecoder> otherDecoder(SkImageDecoder::Factory(&subS tream)); 169 SkAutoTDelete<SkImageDecoder> otherDecoder(SkImageDecoder::Factory(&subS tream));
166 if (otherDecoder.get() != NULL) { 170 if (otherDecoder.get() != NULL) {
167 // Disallow nesting ICO files within one another 171 // Disallow nesting ICO files within one another
172 // FIXME: Can ICO files contain other formats besides PNG?
168 if (otherDecoder->getFormat() == SkImageDecoder::kICO_Format) { 173 if (otherDecoder->getFormat() == SkImageDecoder::kICO_Format) {
169 return false; 174 return kFailure;
170 } 175 }
171 // Set fields on the other decoder to be the same as this one. 176 // Set fields on the other decoder to be the same as this one.
172 this->copyFieldsToOther(otherDecoder.get()); 177 this->copyFieldsToOther(otherDecoder.get());
173 if(otherDecoder->decode(&subStream, bm, this->getDefaultPref(), mode )) { 178 const Result result = otherDecoder->decode(&subStream, bm, this->get DefaultPref(),
174 return true; 179 mode);
180 // FIXME: Should we just return result here? Is it possible that dat a that looked like
181 // a subimage was not, but was actually a valid ICO?
182 if (result != kFailure) {
183 return result;
175 } 184 }
176 } 185 }
177 } 186 }
178 187
179 //int infoSize = read4Bytes(buf, offset); //40 188 //int infoSize = read4Bytes(buf, offset); //40
180 //int width = read4Bytes(buf, offset+4); //should == w 189 //int width = read4Bytes(buf, offset+4); //should == w
181 //int height = read4Bytes(buf, offset+8); //should == 2*h 190 //int height = read4Bytes(buf, offset+8); //should == 2*h
182 //int planesToo = read2Bytes(buf, offset+12); //should == 1 (does it ?) 191 //int planesToo = read2Bytes(buf, offset+12); //should == 1 (does it ?)
183 int bitCount = read2Bytes(buf, offset+14); 192 int bitCount = read2Bytes(buf, offset+14);
184 193
(...skipping 17 matching lines...) Expand all
202 case 24: 211 case 24:
203 placePixel = &editPixelBit24; 212 placePixel = &editPixelBit24;
204 colorCount = 0; 213 colorCount = 0;
205 break; 214 break;
206 case 32: 215 case 32:
207 placePixel = &editPixelBit32; 216 placePixel = &editPixelBit32;
208 colorCount = 0; 217 colorCount = 0;
209 break; 218 break;
210 default: 219 default:
211 SkDEBUGF(("Decoding %ibpp is unimplemented\n", bitCount)); 220 SkDEBUGF(("Decoding %ibpp is unimplemented\n", bitCount));
212 return false; 221 return kFailure;
213 } 222 }
214 223
215 //these should all be zero, but perhaps are not - need to check 224 //these should all be zero, but perhaps are not - need to check
216 //int compression = read4Bytes(buf, offset+16); //0 225 //int compression = read4Bytes(buf, offset+16); //0
217 //int imageSize = read4Bytes(buf, offset+20); //0 - sometimes has a value 226 //int imageSize = read4Bytes(buf, offset+20); //0 - sometimes has a value
218 //int xPixels = read4Bytes(buf, offset+24); //0 227 //int xPixels = read4Bytes(buf, offset+24); //0
219 //int yPixels = read4Bytes(buf, offset+28); //0 228 //int yPixels = read4Bytes(buf, offset+28); //0
220 //int colorsUsed = read4Bytes(buf, offset+32) //0 - might have an ac tual value though 229 //int colorsUsed = read4Bytes(buf, offset+32) //0 - might have an ac tual value though
221 //int colorsImportant = read4Bytes(buf, offset+36); //0 230 //int colorsImportant = read4Bytes(buf, offset+36); //0
222 231
(...skipping 30 matching lines...) Expand all
253 //if we allow different Configs, everything is the same til here 262 //if we allow different Configs, everything is the same til here
254 //change the config, and use different address getter, and place index vs co lor, and add the color table 263 //change the config, and use different address getter, and place index vs co lor, and add the color table
255 //FIXME: what is the tradeoff in size? 264 //FIXME: what is the tradeoff in size?
256 //if the andbitmap (mask) is all zeroes, then we can easily do an index bitm ap 265 //if the andbitmap (mask) is all zeroes, then we can easily do an index bitm ap
257 //however, with small images with large colortables, maybe it's better to st ill do argb_8888 266 //however, with small images with large colortables, maybe it's better to st ill do argb_8888
258 267
259 bm->setInfo(SkImageInfo::MakeN32Premul(w, h), calculateRowBytesFor8888(w, bi tCount)); 268 bm->setInfo(SkImageInfo::MakeN32Premul(w, h), calculateRowBytesFor8888(w, bi tCount));
260 269
261 if (SkImageDecoder::kDecodeBounds_Mode == mode) { 270 if (SkImageDecoder::kDecodeBounds_Mode == mode) {
262 delete[] colors; 271 delete[] colors;
263 return true; 272 return kSuccess;
264 } 273 }
265 274
266 if (!this->allocPixelRef(bm, NULL)) 275 if (!this->allocPixelRef(bm, NULL))
267 { 276 {
268 delete[] colors; 277 delete[] colors;
269 return false; 278 return kFailure;
270 } 279 }
271 280
272 SkAutoLockPixels alp(*bm); 281 SkAutoLockPixels alp(*bm);
273 282
274 for (int y = 0; y < h; y++) 283 for (int y = 0; y < h; y++)
275 { 284 {
276 for (int x = 0; x < w; x++) 285 for (int x = 0; x < w; x++)
277 { 286 {
278 //U32* address = bm->getAddr32(x, y); 287 //U32* address = bm->getAddr32(x, y);
279 288
280 //check the alpha bit first, but pass it along to the function to fi gure out how to deal with it 289 //check the alpha bit first, but pass it along to the function to fi gure out how to deal with it
281 int andPixelNo = andLineWidth*(h-y-1)+x; 290 int andPixelNo = andLineWidth*(h-y-1)+x;
282 //only need to get a new alphaByte when x %8 == 0 291 //only need to get a new alphaByte when x %8 == 0
283 //but that introduces an if and a mod - probably much slower 292 //but that introduces an if and a mod - probably much slower
284 //that's ok, it's just a read of an array, not a stream 293 //that's ok, it's just a read of an array, not a stream
285 int alphaByte = readByte(buf, andOffset + (andPixelNo >> 3)); 294 int alphaByte = readByte(buf, andOffset + (andPixelNo >> 3));
286 int shift = 7 - (andPixelNo & 0x7); 295 int shift = 7 - (andPixelNo & 0x7);
287 int m = 1 << shift; 296 int m = 1 << shift;
288 297
289 int pixelNo = lineWidth*(h-y-1)+x; 298 int pixelNo = lineWidth*(h-y-1)+x;
290 placePixel(pixelNo, buf, xorOffset, x, y, w, bm, alphaByte, m, shift , colors); 299 placePixel(pixelNo, buf, xorOffset, x, y, w, bm, alphaByte, m, shift , colors);
291 300
292 } 301 }
293 } 302 }
294 303
295 delete [] colors; 304 delete [] colors;
296 //ensure we haven't read off the end? 305 //ensure we haven't read off the end?
297 //of course this doesn't help us if the andOffset was a lie... 306 //of course this doesn't help us if the andOffset was a lie...
298 //return andOffset + (andLineWidth >> 3) <= length; 307 //return andOffset + (andLineWidth >> 3) <= length;
299 return true; 308 return kSuccess;
300 } //onDecode 309 } //onDecode
301 310
302 //function to place the pixel, determined by the bitCount 311 //function to place the pixel, determined by the bitCount
303 static void editPixelBit1(const int pixelNo, const unsigned char* buf, 312 static void editPixelBit1(const int pixelNo, const unsigned char* buf,
304 const int xorOffset, int& x, int y, const int w, 313 const int xorOffset, int& x, int y, const int w,
305 SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors) 314 SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors)
306 { 315 {
307 // note that this should be the same as/similar to the AND bitmap 316 // note that this should be the same as/similar to the AND bitmap
308 SkPMColor* address = bm->getAddr32(x,y); 317 SkPMColor* address = bm->getAddr32(x,y);
309 int byte = readByte(buf, xorOffset + (pixelNo >> 3)); 318 int byte = readByte(buf, xorOffset + (pixelNo >> 3));
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
418 static SkImageDecoder_DecodeReg gReg(sk_libico_dfactory); 427 static SkImageDecoder_DecodeReg gReg(sk_libico_dfactory);
419 428
420 static SkImageDecoder::Format get_format_ico(SkStreamRewindable* stream) { 429 static SkImageDecoder::Format get_format_ico(SkStreamRewindable* stream) {
421 if (is_ico(stream)) { 430 if (is_ico(stream)) {
422 return SkImageDecoder::kICO_Format; 431 return SkImageDecoder::kICO_Format;
423 } 432 }
424 return SkImageDecoder::kUnknown_Format; 433 return SkImageDecoder::kUnknown_Format;
425 } 434 }
426 435
427 static SkImageDecoder_FormatReg gFormatReg(get_format_ico); 436 static SkImageDecoder_FormatReg gFormatReg(get_format_ico);
OLDNEW
« no previous file with comments | « src/images/SkImageDecoder_libgif.cpp ('k') | src/images/SkImageDecoder_libjpeg.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698