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

Side by Side Diff: src/codec/SkBmpRLECodec.cpp

Issue 1287423002: Scanline decoding for bmp (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Scaling for all types of bmps Created 5 years, 4 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
OLDNEW
1 /* 1 /*
2 * Copyright 2015 Google Inc. 2 * Copyright 2015 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 "SkBmpRLECodec.h" 8 #include "SkBmpRLECodec.h"
9 #include "SkCodecPriv.h" 9 #include "SkCodecPriv.h"
10 #include "SkColorPriv.h" 10 #include "SkColorPriv.h"
11 #include "SkScaledCodec.h"
11 #include "SkScanlineDecoder.h" 12 #include "SkScanlineDecoder.h"
12 #include "SkStream.h" 13 #include "SkStream.h"
13 14
14 /* 15 /*
15 * Creates an instance of the decoder 16 * Creates an instance of the decoder
16 * Called only by NewFromStream 17 * Called only by NewFromStream
17 */ 18 */
18 SkBmpRLECodec::SkBmpRLECodec(const SkImageInfo& info, SkStream* stream, 19 SkBmpRLECodec::SkBmpRLECodec(const SkImageInfo& info, SkStream* stream,
19 uint16_t bitsPerPixel, uint32_t numColors, 20 uint16_t bitsPerPixel, uint32_t numColors,
20 uint32_t bytesPerColor, uint32_t offset, 21 uint32_t bytesPerColor, uint32_t offset,
(...skipping 25 matching lines...) Expand all
46 } 47 }
47 if (dstInfo.dimensions() != this->getInfo().dimensions()) { 48 if (dstInfo.dimensions() != this->getInfo().dimensions()) {
48 SkCodecPrintf("Error: scaling not supported.\n"); 49 SkCodecPrintf("Error: scaling not supported.\n");
49 return kInvalidScale; 50 return kInvalidScale;
50 } 51 }
51 if (!conversion_possible(dstInfo, this->getInfo())) { 52 if (!conversion_possible(dstInfo, this->getInfo())) {
52 SkCodecPrintf("Error: cannot convert input type to output type.\n"); 53 SkCodecPrintf("Error: cannot convert input type to output type.\n");
53 return kInvalidConversion; 54 return kInvalidConversion;
54 } 55 }
55 56
56 // Create the color table if necessary and prepare the stream for decode 57 Result result = this->prepareToDecode(dstInfo, opts, inputColorPtr, inputCol orCount);
57 // Note that if it is non-NULL, inputColorCount will be modified 58 if (kSuccess != result) {
58 if (!this->createColorTable(inputColorCount)) { 59 return result;
59 SkCodecPrintf("Error: could not create color table.\n");
60 return kInvalidInput;
61 }
62
63 // Copy the color table to the client if necessary
64 copy_color_table(dstInfo, fColorTable, inputColorPtr, inputColorCount);
65
66 // Initialize a swizzler if necessary
67 if (!this->initializeStreamBuffer()) {
68 SkCodecPrintf("Error: cannot initialize swizzler.\n");
69 return kInvalidConversion;
70 } 60 }
71 61
72 // Perform the decode 62 // Perform the decode
73 return decode(dstInfo, dst, dstRowBytes, opts); 63 return decode(dstInfo, dst, dstRowBytes, opts);
74 } 64 }
75 65
76 /* 66 /*
77 * Process the color table for the bmp input 67 * Process the color table for the bmp input
78 */ 68 */
79 bool SkBmpRLECodec::createColorTable(int* numColors) { 69 bool SkBmpRLECodec::createColorTable(int* numColors) {
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 fRLEBytes = remainingBytes + additionalBytes; 177 fRLEBytes = remainingBytes + additionalBytes;
188 return fRLEBytes; 178 return fRLEBytes;
189 } 179 }
190 180
191 /* 181 /*
192 * Set an RLE pixel using the color table 182 * Set an RLE pixel using the color table
193 */ 183 */
194 void SkBmpRLECodec::setPixel(void* dst, size_t dstRowBytes, 184 void SkBmpRLECodec::setPixel(void* dst, size_t dstRowBytes,
195 const SkImageInfo& dstInfo, uint32_t x, uint32_t y, 185 const SkImageInfo& dstInfo, uint32_t x, uint32_t y,
196 uint8_t index) { 186 uint8_t index) {
197 // Set the row 187 int sampleX;
198 int height = dstInfo.height(); 188 SkScaledCodec::ComputeSampleSize(dstInfo, this->getInfo(), &sampleX, NULL);
199 int row; 189 if (is_coord_necessary(x, sampleX >> 1, sampleX, dstInfo.width())) {
200 if (SkBmpCodec::kBottomUp_RowOrder == this->rowOrder()) { 190 // Set the row
201 row = height - y - 1; 191 uint32_t row = this->getDstRow(y, dstInfo.height());
202 } else {
203 row = y;
204 }
205 192
206 // Set the pixel based on destination color type 193 // Set the pixel based on destination color type
207 switch (dstInfo.colorType()) { 194 switch (dstInfo.colorType()) {
208 case kN32_SkColorType: { 195 case kN32_SkColorType: {
209 SkPMColor* dstRow = SkTAddOffset<SkPMColor>((SkPMColor*) dst, 196 SkPMColor* dstRow = SkTAddOffset<SkPMColor>((SkPMColor*) dst,
210 row * (int) dstRowBytes); 197 row * (int) dstRowBytes);
211 dstRow[x] = fColorTable->operator[](index); 198 dstRow[x / sampleX] = fColorTable->operator[](index);
212 break; 199 break;
200 }
201 case kRGB_565_SkColorType: {
202 uint16_t* dstRow = SkTAddOffset<uint16_t>(dst, row * (int) dstRo wBytes);
203 dstRow[x / sampleX] = SkPixel32ToPixel16(fColorTable->operator[] (index));
204 break;
205 }
206 default:
207 // This case should not be reached. We should catch an invalid
208 // color type when we check that the conversion is possible.
209 SkASSERT(false);
210 break;
213 } 211 }
214 case kRGB_565_SkColorType: {
215 uint16_t* dstRow = SkTAddOffset<uint16_t>(dst, row * (int) dstRowByt es);
216 dstRow[x] = SkPixel32ToPixel16(fColorTable->operator[](index));
217 break;
218 }
219 default:
220 // This case should not be reached. We should catch an invalid
221 // color type when we check that the conversion is possible.
222 SkASSERT(false);
223 break;
224 } 212 }
225 } 213 }
226 214
227 /* 215 /*
228 * Set an RLE pixel from R, G, B values 216 * Set an RLE pixel from R, G, B values
229 */ 217 */
230 void SkBmpRLECodec::setRGBPixel(void* dst, size_t dstRowBytes, 218 void SkBmpRLECodec::setRGBPixel(void* dst, size_t dstRowBytes,
231 const SkImageInfo& dstInfo, uint32_t x, 219 const SkImageInfo& dstInfo, uint32_t x,
232 uint32_t y, uint8_t red, uint8_t green, 220 uint32_t y, uint8_t red, uint8_t green,
233 uint8_t blue) { 221 uint8_t blue) {
234 // Set the row 222 int sampleX;
235 int height = dstInfo.height(); 223 SkScaledCodec::ComputeSampleSize(dstInfo, this->getInfo(), &sampleX, NULL);
236 int row; 224 if (is_coord_necessary(x, sampleX >> 1, sampleX, dstInfo.width())) {
237 if (SkBmpCodec::kBottomUp_RowOrder == this->rowOrder()) { 225 // Set the row
238 row = height - y - 1; 226 uint32_t row = this->getDstRow(y, dstInfo.height());
239 } else {
240 row = y;
241 }
242 227
243 // Set the pixel based on destination color type 228 // Set the pixel based on destination color type
244 switch (dstInfo.colorType()) { 229 switch (dstInfo.colorType()) {
245 case kN32_SkColorType: { 230 case kN32_SkColorType: {
246 SkPMColor* dstRow = SkTAddOffset<SkPMColor>((SkPMColor*) dst, 231 SkPMColor* dstRow = SkTAddOffset<SkPMColor>((SkPMColor*) dst,
247 row * (int) dstRowBytes); 232 row * (int) dstRowBytes);
248 dstRow[x] = SkPackARGB32NoCheck(0xFF, red, green, blue); 233 dstRow[x] = SkPackARGB32NoCheck(0xFF, red, green, blue);
249 break; 234 break;
235 }
236 case kRGB_565_SkColorType: {
237 uint16_t* dstRow = SkTAddOffset<uint16_t>(dst, row * (int) dstRo wBytes);
238 dstRow[x] = SkPack888ToRGB16(red, green, blue);
239 break;
240 }
241 default:
242 // This case should not be reached. We should catch an invalid
243 // color type when we check that the conversion is possible.
244 SkASSERT(false);
245 break;
250 } 246 }
251 case kRGB_565_SkColorType: {
252 uint16_t* dstRow = SkTAddOffset<uint16_t>(dst, row * (int) dstRowByt es);
253 dstRow[x] = SkPack888ToRGB16(red, green, blue);
254 break;
255 }
256 default:
257 // This case should not be reached. We should catch an invalid
258 // color type when we check that the conversion is possible.
259 SkASSERT(false);
260 break;
261 } 247 }
262 } 248 }
263 249
264 /* 250 /*
265 * Performs the bitmap decoding for RLE input format 251 * Performs the bitmap decoding for RLE input format
266 * RLE decoding is performed all at once, rather than a one row at a time 252 * RLE decoding is performed all at once, rather than a one row at a time
267 */ 253 */
268 SkCodec::Result SkBmpRLECodec::decode(const SkImageInfo& dstInfo, 254 SkCodec::Result SkBmpRLECodec::decode(const SkImageInfo& dstInfo,
269 void* dst, size_t dstRowBytes, 255 void* dst, size_t dstRowBytes,
270 const Options& opts) { 256 const Options& opts) {
271 // Set RLE flags 257 // Set RLE flags
272 static const uint8_t RLE_ESCAPE = 0; 258 static const uint8_t RLE_ESCAPE = 0;
273 static const uint8_t RLE_EOL = 0; 259 static const uint8_t RLE_EOL = 0;
274 static const uint8_t RLE_EOF = 1; 260 static const uint8_t RLE_EOF = 1;
275 static const uint8_t RLE_DELTA = 2; 261 static const uint8_t RLE_DELTA = 2;
276 262
277 // Set constant values 263 // Set constant values
278 const int width = dstInfo.width(); 264 const int width = this->getInfo().width();
279 const int height = dstInfo.height(); 265 const int height = dstInfo.height();
280 266
281 // Destination parameters 267 // Destination parameters
282 int x = 0; 268 int x = 0;
283 int y = 0; 269 int y = 0;
284 270
285 // Set the background as transparent. Then, if the RLE code skips pixels, 271 // Set the background as transparent. Then, if the RLE code skips pixels,
286 // the skipped pixels will be transparent. 272 // the skipped pixels will be transparent.
287 // Because of the need for transparent pixels, kN32 is the only color 273 // Because of the need for transparent pixels, kN32 is the only color
288 // type that makes sense for the destination format. 274 // type that makes sense for the destination format.
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 // Set the indicated number of pixels 435 // Set the indicated number of pixels
450 for (int which = 0; x < endX; x++) { 436 for (int which = 0; x < endX; x++) {
451 setPixel(dst, dstRowBytes, dstInfo, x, y, 437 setPixel(dst, dstRowBytes, dstInfo, x, y,
452 indices[which]); 438 indices[which]);
453 which = !which; 439 which = !which;
454 } 440 }
455 } 441 }
456 } 442 }
457 } 443 }
458 } 444 }
445
446 SkCodec::Result SkBmpRLECodec::prepareToDecode(const SkImageInfo& dstInfo,
447 const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputCo lorCount) {
448 // Create the color table if necessary and prepare the stream for decode
449 // Note that if it is non-NULL, inputColorCount will be modified
450 if (!this->createColorTable(inputColorCount)) {
451 SkCodecPrintf("Error: could not create color table.\n");
452 return SkCodec::kInvalidInput;
453 }
454
455 // Copy the color table to the client if necessary
456 copy_color_table(dstInfo, this->fColorTable, inputColorPtr, inputColorCount) ;
457
458 // Initialize a buffer for encoded RLE data
459 if (!this->initializeStreamBuffer()) {
460 SkCodecPrintf("Error: cannot initialize swizzler.\n");
461 return SkCodec::kInvalidConversion;
462 }
463
464 return SkCodec::kSuccess;
465 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698