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

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

Issue 2174493002: Add color space xform support to SkJpegCodec (includes F16!) (Closed) Base URL: https://skia.googlesource.com/skia.git@drop
Patch Set: Fix return value bug in readRows() Created 4 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 "SkCodec.h" 8 #include "SkCodec.h"
9 #include "SkMSAN.h" 9 #include "SkMSAN.h"
10 #include "SkJpegCodec.h" 10 #include "SkJpegCodec.h"
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 } 184 }
185 185
186 bool SkJpegCodec::ReadHeader(SkStream* stream, SkCodec** codecOut, 186 bool SkJpegCodec::ReadHeader(SkStream* stream, SkCodec** codecOut,
187 JpegDecoderMgr** decoderMgrOut) { 187 JpegDecoderMgr** decoderMgrOut) {
188 188
189 // Create a JpegDecoderMgr to own all of the decompress information 189 // Create a JpegDecoderMgr to own all of the decompress information
190 SkAutoTDelete<JpegDecoderMgr> decoderMgr(new JpegDecoderMgr(stream)); 190 SkAutoTDelete<JpegDecoderMgr> decoderMgr(new JpegDecoderMgr(stream));
191 191
192 // libjpeg errors will be caught and reported here 192 // libjpeg errors will be caught and reported here
193 if (setjmp(decoderMgr->getJmpBuf())) { 193 if (setjmp(decoderMgr->getJmpBuf())) {
194 return decoderMgr->returnFalse("setjmp"); 194 return decoderMgr->returnFalse("ReadHeader");
195 } 195 }
196 196
197 // Initialize the decompress info and the source manager 197 // Initialize the decompress info and the source manager
198 decoderMgr->init(); 198 decoderMgr->init();
199 199
200 // Instruct jpeg library to save the markers that we care about. Since 200 // Instruct jpeg library to save the markers that we care about. Since
201 // the orientation and color profile will not change, we can skip this 201 // the orientation and color profile will not change, we can skip this
202 // step on rewinds. 202 // step on rewinds.
203 if (codecOut) { 203 if (codecOut) {
204 jpeg_save_markers(decoderMgr->dinfo(), kExifMarker, 0xFFFF); 204 jpeg_save_markers(decoderMgr->dinfo(), kExifMarker, 0xFFFF);
205 jpeg_save_markers(decoderMgr->dinfo(), kICCMarker, 0xFFFF); 205 jpeg_save_markers(decoderMgr->dinfo(), kICCMarker, 0xFFFF);
206 } 206 }
207 207
208 // Read the jpeg header 208 // Read the jpeg header
209 if (JPEG_HEADER_OK != jpeg_read_header(decoderMgr->dinfo(), true)) { 209 if (JPEG_HEADER_OK != jpeg_read_header(decoderMgr->dinfo(), true)) {
210 return decoderMgr->returnFalse("read_header"); 210 return decoderMgr->returnFalse("ReadHeader");
211 } 211 }
212 212
213 if (codecOut) { 213 if (codecOut) {
214 // Get the encoded color type 214 // Get the encoded color type
215 SkEncodedInfo::Color color; 215 SkEncodedInfo::Color color;
216 if (!decoderMgr->getEncodedColor(&color)) { 216 if (!decoderMgr->getEncodedColor(&color)) {
217 return false; 217 return false;
218 } 218 }
219 219
220 // Create image info object and the codec 220 // Create image info object and the codec
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 } 256 }
257 return nullptr; 257 return nullptr;
258 } 258 }
259 259
260 SkJpegCodec::SkJpegCodec(int width, int height, const SkEncodedInfo& info, SkStr eam* stream, 260 SkJpegCodec::SkJpegCodec(int width, int height, const SkEncodedInfo& info, SkStr eam* stream,
261 JpegDecoderMgr* decoderMgr, sk_sp<SkColorSpace> colorSpace, Origin origi n, 261 JpegDecoderMgr* decoderMgr, sk_sp<SkColorSpace> colorSpace, Origin origi n,
262 sk_sp<SkData> iccData) 262 sk_sp<SkData> iccData)
263 : INHERITED(width, height, info, stream, std::move(colorSpace), origin) 263 : INHERITED(width, height, info, stream, std::move(colorSpace), origin)
264 , fDecoderMgr(decoderMgr) 264 , fDecoderMgr(decoderMgr)
265 , fReadyState(decoderMgr->dinfo()->global_state) 265 , fReadyState(decoderMgr->dinfo()->global_state)
266 , fSrcRow(nullptr) 266 , fSwizzleSrcRow(nullptr)
267 , fColorXformSrcRow(nullptr)
267 , fSwizzlerSubset(SkIRect::MakeEmpty()) 268 , fSwizzlerSubset(SkIRect::MakeEmpty())
268 , fICCData(std::move(iccData)) 269 , fICCData(std::move(iccData))
269 {} 270 {}
270 271
271 /* 272 /*
272 * Return the row bytes of a particular image type and width 273 * Return the row bytes of a particular image type and width
273 */ 274 */
274 static size_t get_row_bytes(const j_decompress_ptr dinfo) { 275 static size_t get_row_bytes(const j_decompress_ptr dinfo) {
275 #ifdef TURBO_HAS_565 276 #ifdef TURBO_HAS_565
276 const size_t colorBytes = (dinfo->out_color_space == JCS_RGB565) ? 2 : 277 const size_t colorBytes = (dinfo->out_color_space == JCS_RGB565) ? 2 :
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 dinfo.global_state = fReadyState; 330 dinfo.global_state = fReadyState;
330 calc_output_dimensions(&dinfo, num, denom); 331 calc_output_dimensions(&dinfo, num, denom);
331 332
332 // Return the calculated output dimensions for the given scale 333 // Return the calculated output dimensions for the given scale
333 return SkISize::Make(dinfo.output_width, dinfo.output_height); 334 return SkISize::Make(dinfo.output_width, dinfo.output_height);
334 } 335 }
335 336
336 bool SkJpegCodec::onRewind() { 337 bool SkJpegCodec::onRewind() {
337 JpegDecoderMgr* decoderMgr = nullptr; 338 JpegDecoderMgr* decoderMgr = nullptr;
338 if (!ReadHeader(this->stream(), nullptr, &decoderMgr)) { 339 if (!ReadHeader(this->stream(), nullptr, &decoderMgr)) {
339 return fDecoderMgr->returnFalse("could not rewind"); 340 return fDecoderMgr->returnFalse("onRewind");
340 } 341 }
341 SkASSERT(nullptr != decoderMgr); 342 SkASSERT(nullptr != decoderMgr);
342 fDecoderMgr.reset(decoderMgr); 343 fDecoderMgr.reset(decoderMgr);
343 344
344 fSwizzler.reset(nullptr); 345 fSwizzler.reset(nullptr);
345 fSrcRow = nullptr; 346 fSwizzleSrcRow = nullptr;
347 fColorXformSrcRow = nullptr;
346 fStorage.reset(); 348 fStorage.reset();
349 fColorXform.reset(nullptr);
347 350
348 return true; 351 return true;
349 } 352 }
350 353
351 /* 354 /*
352 * Checks if the conversion between the input image and the requested output 355 * Checks if the conversion between the input image and the requested output
353 * image has been implemented 356 * image has been implemented
354 * Sets the output color space 357 * Sets the output color space
355 */ 358 */
356 bool SkJpegCodec::setOutputColorSpace(const SkImageInfo& dst) { 359 bool SkJpegCodec::setOutputColorSpace(const SkImageInfo& dstInfo, bool needsColo rXform) {
357 if (kUnknown_SkAlphaType == dst.alphaType()) { 360 if (kUnknown_SkAlphaType == dstInfo.alphaType()) {
358 return false; 361 return false;
359 } 362 }
360 363
361 if (kOpaque_SkAlphaType != dst.alphaType()) { 364 if (kOpaque_SkAlphaType != dstInfo.alphaType()) {
362 SkCodecPrintf("Warning: an opaque image should be decoded as opaque " 365 SkCodecPrintf("Warning: an opaque image should be decoded as opaque "
363 "- it is being decoded as non-opaque, which will draw slow er\n"); 366 "- it is being decoded as non-opaque, which will draw slow er\n");
364 } 367 }
365 368
366 // Check if we will decode to CMYK because a conversion to RGBA is not suppo rted 369 // Check if we will decode to CMYK. libjpeg-turbo does not convert CMYK to RGBA, so
367 J_COLOR_SPACE colorSpace = fDecoderMgr->dinfo()->jpeg_color_space; 370 // we must do it ourselves.
368 bool isCMYK = JCS_CMYK == colorSpace || JCS_YCCK == colorSpace; 371 J_COLOR_SPACE encodedColorType = fDecoderMgr->dinfo()->jpeg_color_space;
372 bool isCMYK = (JCS_CMYK == encodedColorType || JCS_YCCK == encodedColorType) ;
369 373
370 // Check for valid color types and set the output color space 374 // Check for valid color types and set the output color space
371 switch (dst.colorType()) { 375 switch (dstInfo.colorType()) {
372 case kRGBA_8888_SkColorType: 376 case kRGBA_8888_SkColorType:
373 if (isCMYK) { 377 if (isCMYK) {
374 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; 378 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK;
375 } else { 379 } else {
376 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA; 380 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA;
377 } 381 }
378 return true; 382 return true;
379 case kBGRA_8888_SkColorType: 383 case kBGRA_8888_SkColorType:
380 if (isCMYK) { 384 if (isCMYK) {
381 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; 385 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK;
386 } else if (needsColorXform) {
387 // Our color transformation code requires RGBA order inputs, but it'll swizzle
388 // to BGRA for us.
389 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA;
382 } else { 390 } else {
383 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_BGRA; 391 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_BGRA;
384 } 392 }
385 return true; 393 return true;
386 case kRGB_565_SkColorType: 394 case kRGB_565_SkColorType:
395 if (needsColorXform) {
396 return false;
397 }
398
387 if (isCMYK) { 399 if (isCMYK) {
388 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK; 400 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK;
389 } else { 401 } else {
390 #ifdef TURBO_HAS_565 402 #ifdef TURBO_HAS_565
391 fDecoderMgr->dinfo()->dither_mode = JDITHER_NONE; 403 fDecoderMgr->dinfo()->dither_mode = JDITHER_NONE;
392 fDecoderMgr->dinfo()->out_color_space = JCS_RGB565; 404 fDecoderMgr->dinfo()->out_color_space = JCS_RGB565;
393 #else 405 #else
394 fDecoderMgr->dinfo()->out_color_space = JCS_RGB; 406 fDecoderMgr->dinfo()->out_color_space = JCS_RGB;
395 #endif 407 #endif
396 } 408 }
397 return true; 409 return true;
398 case kGray_8_SkColorType: 410 case kGray_8_SkColorType:
411 if (needsColorXform || JCS_GRAYSCALE != encodedColorType) {
412 return false;
413 }
414
415 fDecoderMgr->dinfo()->out_color_space = JCS_GRAYSCALE;
416 return true;
417 case kRGBA_F16_SkColorType:
418 SkASSERT(needsColorXform);
419
399 if (isCMYK) { 420 if (isCMYK) {
400 return false; 421 fDecoderMgr->dinfo()->out_color_space = JCS_CMYK;
401 } else { 422 } else {
402 // We will enable decodes to gray even if the image is color bec ause this is 423 fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA;
403 // much faster than decoding to color and then converting
404 fDecoderMgr->dinfo()->out_color_space = JCS_GRAYSCALE;
405 } 424 }
406 return true; 425 return true;
407 default: 426 default:
408 return false; 427 return false;
409 } 428 }
410 } 429 }
411 430
412 /* 431 /*
413 * Checks if we can natively scale to the requested dimensions and natively scal es the 432 * Checks if we can natively scale to the requested dimensions and natively scal es the
414 * dimensions if possible 433 * dimensions if possible
415 */ 434 */
416 bool SkJpegCodec::onDimensionsSupported(const SkISize& size) { 435 bool SkJpegCodec::onDimensionsSupported(const SkISize& size) {
417 if (setjmp(fDecoderMgr->getJmpBuf())) { 436 if (setjmp(fDecoderMgr->getJmpBuf())) {
418 return fDecoderMgr->returnFalse("onDimensionsSupported/setjmp"); 437 return fDecoderMgr->returnFalse("onDimensionsSupported");
419 } 438 }
420 439
421 const unsigned int dstWidth = size.width(); 440 const unsigned int dstWidth = size.width();
422 const unsigned int dstHeight = size.height(); 441 const unsigned int dstHeight = size.height();
423 442
424 // Set up a fake decompress struct in order to use libjpeg to calculate outp ut dimensions 443 // Set up a fake decompress struct in order to use libjpeg to calculate outp ut dimensions
425 // FIXME: Why is this necessary? 444 // FIXME: Why is this necessary?
426 jpeg_decompress_struct dinfo; 445 jpeg_decompress_struct dinfo;
427 sk_bzero(&dinfo, sizeof(dinfo)); 446 sk_bzero(&dinfo, sizeof(dinfo));
428 dinfo.image_width = this->getInfo().width(); 447 dinfo.image_width = this->getInfo().width();
(...skipping 14 matching lines...) Expand all
443 // Try the next scale 462 // Try the next scale
444 num -= 1; 463 num -= 1;
445 calc_output_dimensions(&dinfo, num, denom); 464 calc_output_dimensions(&dinfo, num, denom);
446 } 465 }
447 466
448 fDecoderMgr->dinfo()->scale_num = num; 467 fDecoderMgr->dinfo()->scale_num = num;
449 fDecoderMgr->dinfo()->scale_denom = denom; 468 fDecoderMgr->dinfo()->scale_denom = denom;
450 return true; 469 return true;
451 } 470 }
452 471
472 static bool needs_color_xform(const SkImageInfo& dstInfo, const SkImageInfo& src Info) {
473 // FIXME (msarett):
474 // Do a better check for color space equality.
475 return (kRGBA_F16_SkColorType == dstInfo.colorType()) ||
476 (dstInfo.colorSpace() && (dstInfo.colorSpace() != srcInfo.colorSpace( )));
477 }
478
479 int SkJpegCodec::readRows(const SkImageInfo& dstInfo, void* dst, size_t rowBytes , int count) {
480 // Set the jump location for libjpeg-turbo errors
481 if (setjmp(fDecoderMgr->getJmpBuf())) {
482 return 0;
483 }
484
485 // When fSwizzleSrcRow is non-null, it means that we need to swizzle. In th is case,
486 // we will always decode into fSwizzlerSrcRow before swizzling into the next buffer.
487 // We can never swizzle "in place" because the swizzler may perform sampling and/or
488 // subsetting.
489 // When fColorXformSrcRow is non-null, it means that we need to color xform and that
490 // we cannot color xform "in place" (many times we can, but not when the dst is F16).
491 // In this case, we will color xform from fColorXformSrc into the dst.
492 JSAMPLE* decodeDst = (JSAMPLE*) dst;
493 uint32_t* swizzleDst = (uint32_t*) dst;
494 size_t decodeDstRowBytes = rowBytes;
495 size_t swizzleDstRowBytes = rowBytes;
496 if (fSwizzleSrcRow && fColorXformSrcRow) {
497 decodeDst = (JSAMPLE*) fSwizzleSrcRow;
498 swizzleDst = fColorXformSrcRow;
499 decodeDstRowBytes = 0;
500 swizzleDstRowBytes = 0;
501 } else if (fColorXformSrcRow) {
502 decodeDst = (JSAMPLE*) fColorXformSrcRow;
503 swizzleDst = fColorXformSrcRow;
504 decodeDstRowBytes = 0;
505 swizzleDstRowBytes = 0;
506 } else if (fSwizzleSrcRow) {
507 decodeDst = (JSAMPLE*) fSwizzleSrcRow;
508 decodeDstRowBytes = 0;
509 }
510
511 for (int y = 0; y < count; y++) {
512 uint32_t lines = jpeg_read_scanlines(fDecoderMgr->dinfo(), &decodeDst, 1 );
513 sk_msan_mark_initialized(decodeDst, decodeDst + rowBytes, "skbug.com/455 0");
514 if (0 == lines) {
515 return y;
516 }
517
518 if (fSwizzler) {
519 fSwizzler->swizzle(swizzleDst, decodeDst);
520 }
521
522 if (fColorXform) {
523 int width = dstInfo.width();
524 switch (dstInfo.colorType()) {
525 case kRGBA_8888_SkColorType:
526 fColorXform->applyToRGBA((uint32_t*) dst, swizzleDst, width) ;
527 break;
528 case kBGRA_8888_SkColorType:
529 fColorXform->applyToBGRA((uint32_t*) dst, swizzleDst, width) ;
530 break;
531 case kRGBA_F16_SkColorType:
532 fColorXform->applyToF16((uint64_t*) dst, swizzleDst, width);
533 break;
534 default:
535 SkASSERT(false);
536 break;
537 }
538
539 dst = SkTAddOffset<void>(dst, rowBytes);
540 }
541
542 decodeDst = SkTAddOffset<JSAMPLE>(decodeDst, decodeDstRowBytes);
543 swizzleDst = SkTAddOffset<uint32_t>(swizzleDst, swizzleDstRowBytes);
544 }
545
546 return count;
547 }
548
453 /* 549 /*
454 * Performs the jpeg decode 550 * Performs the jpeg decode
455 */ 551 */
456 SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo, 552 SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo,
457 void* dst, size_t dstRowBytes, 553 void* dst, size_t dstRowBytes,
458 const Options& options, SkPMColor*, int *, 554 const Options& options, SkPMColor*, int *,
459 int* rowsDecoded) { 555 int* rowsDecoded) {
460 if (options.fSubset) { 556 if (options.fSubset) {
461 // Subsets are not supported. 557 // Subsets are not supported.
462 return kUnimplemented; 558 return kUnimplemented;
463 } 559 }
464 560
465 // Get a pointer to the decompress info since we will use it quite frequentl y 561 // Get a pointer to the decompress info since we will use it quite frequentl y
466 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo(); 562 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
467 563
468 // Set the jump location for libjpeg errors 564 // Set the jump location for libjpeg errors
469 if (setjmp(fDecoderMgr->getJmpBuf())) { 565 if (setjmp(fDecoderMgr->getJmpBuf())) {
470 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); 566 return fDecoderMgr->returnFailure("setjmp", kInvalidInput);
471 } 567 }
472 568
473 // Check if we can decode to the requested destination and set the output co lor space 569 // Check if we can decode to the requested destination and set the output co lor space
474 if (!this->setOutputColorSpace(dstInfo)) { 570 bool needsColorXform = needs_color_xform(dstInfo, this->getInfo());
475 return fDecoderMgr->returnFailure("conversion_possible", kInvalidConvers ion); 571 if (!this->setOutputColorSpace(dstInfo, needsColorXform)) {
572 return fDecoderMgr->returnFailure("setOutputColorSpace", kInvalidConvers ion);
476 } 573 }
477 574
478 // Now, given valid output dimensions, we can start the decompress 575 if (!this->initializeColorXform(dstInfo, needsColorXform)) {
576 return fDecoderMgr->returnFailure("initializeColorXform", kInvalidParame ters);
577 }
578
479 if (!jpeg_start_decompress(dinfo)) { 579 if (!jpeg_start_decompress(dinfo)) {
480 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); 580 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput);
481 } 581 }
482 582
483 // The recommended output buffer height should always be 1 in high quality m odes. 583 // The recommended output buffer height should always be 1 in high quality m odes.
484 // If it's not, we want to know because it means our strategy is not optimal . 584 // If it's not, we want to know because it means our strategy is not optimal .
485 SkASSERT(1 == dinfo->rec_outbuf_height); 585 SkASSERT(1 == dinfo->rec_outbuf_height);
486 586
487 J_COLOR_SPACE colorSpace = dinfo->out_color_space; 587 J_COLOR_SPACE colorSpace = dinfo->out_color_space;
488 if (JCS_CMYK == colorSpace || JCS_RGB == colorSpace) { 588 if (JCS_CMYK == colorSpace || JCS_RGB == colorSpace) {
489 this->initializeSwizzler(dstInfo, options); 589 this->initializeSwizzler(dstInfo, options);
490 } 590 }
491 591
492 // Perform the decode a single row at a time 592 this->allocateStorage(dstInfo);
493 uint32_t dstHeight = dstInfo.height();
494 593
495 JSAMPLE* dstRow; 594 int rows = this->readRows(dstInfo, dst, dstRowBytes, dstInfo.height());
496 if (fSwizzler) { 595 if (rows < dstInfo.height()) {
497 // write data to storage row, then sample using swizzler 596 *rowsDecoded = rows;
498 dstRow = fSrcRow; 597 return fDecoderMgr->returnFailure("Incomplete image data", kIncompleteIn put);
499 } else {
500 // write data directly to dst
501 dstRow = (JSAMPLE*) dst;
502 }
503
504 for (uint32_t y = 0; y < dstHeight; y++) {
505 // Read rows of the image
506 uint32_t lines = jpeg_read_scanlines(dinfo, &dstRow, 1);
507 sk_msan_mark_initialized(dstRow, dstRow + dstRowBytes, "skbug.com/4550") ;
508
509 // If we cannot read enough rows, assume the input is incomplete
510 if (lines != 1) {
511 *rowsDecoded = y;
512 return fDecoderMgr->returnFailure("Incomplete image data", kIncomple teInput);
513 }
514
515 if (fSwizzler) {
516 // use swizzler to sample row
517 fSwizzler->swizzle(dst, dstRow);
518 dst = SkTAddOffset<JSAMPLE>(dst, dstRowBytes);
519 } else {
520 dstRow = SkTAddOffset<JSAMPLE>(dstRow, dstRowBytes);
521 }
522 } 598 }
523 599
524 return kSuccess; 600 return kSuccess;
525 } 601 }
526 602
603 void SkJpegCodec::allocateStorage(const SkImageInfo& dstInfo) {
604 size_t swizzleBytes = 0;
605 if (fSwizzler) {
606 swizzleBytes = get_row_bytes(fDecoderMgr->dinfo());
607 }
608
609 size_t xformBytes = 0;
610 if (kRGBA_F16_SkColorType == dstInfo.colorType()) {
611 SkASSERT(fColorXform);
612 xformBytes = dstInfo.width() * sizeof(SkColorSpaceXform::RGBA32);
613 }
614
615 size_t totalBytes = swizzleBytes + xformBytes;
616 if (totalBytes > 0) {
617 fStorage.reset(totalBytes);
618 fSwizzleSrcRow = (swizzleBytes > 0) ? fStorage.get() : nullptr;
619 fColorXformSrcRow = (xformBytes > 0) ?
620 SkTAddOffset<uint32_t>(fStorage.get(), swizzleBytes) : nullptr;
621 }
622 }
623
527 void SkJpegCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& options) { 624 void SkJpegCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& options) {
528 // libjpeg-turbo may have already performed color conversion. We must indic ate the 625 // libjpeg-turbo may have already performed color conversion. We must indic ate the
529 // appropriate format to the swizzler. 626 // appropriate format to the swizzler.
530 SkEncodedInfo swizzlerInfo = this->getEncodedInfo(); 627 SkEncodedInfo swizzlerInfo = this->getEncodedInfo();
531 bool preSwizzled = true; 628 bool preSwizzled = true;
532 switch (fDecoderMgr->dinfo()->out_color_space) { 629 switch (fDecoderMgr->dinfo()->out_color_space) {
533 case JCS_RGB: 630 case JCS_RGB:
534 preSwizzled = false; 631 preSwizzled = false;
535 swizzlerInfo = SkEncodedInfo::Make(SkEncodedInfo::kRGB_Color, 632 swizzlerInfo = SkEncodedInfo::Make(SkEncodedInfo::kRGB_Color,
536 swizzlerInfo.alpha(), 633 swizzlerInfo.alpha(),
537 swizzlerInfo.bitsPerComponent()); 634 swizzlerInfo.bitsPerComponent());
538 break; 635 break;
539 case JCS_CMYK: 636 case JCS_CMYK:
540 preSwizzled = false; 637 preSwizzled = false;
541 swizzlerInfo = SkEncodedInfo::Make( 638 swizzlerInfo = SkEncodedInfo::Make(SkEncodedInfo::kInvertedCMYK_Colo r,
542 SkEncodedInfo::kInvertedCMYK_Color, swizzlerInfo.alpha(), 639 swizzlerInfo.alpha(),
543 swizzlerInfo.bitsPerComponent()); 640 swizzlerInfo.bitsPerComponent());
544 break; 641 break;
545 default: 642 default:
546 break; 643 break;
547 } 644 }
548 645
549 Options swizzlerOptions = options; 646 Options swizzlerOptions = options;
550 if (options.fSubset) { 647 if (options.fSubset) {
551 // Use fSwizzlerSubset if this is a subset decode. This is necessary in the case 648 // Use fSwizzlerSubset if this is a subset decode. This is necessary in the case
552 // where libjpeg-turbo provides a subset and then we need to subset it f urther. 649 // where libjpeg-turbo provides a subset and then we need to subset it f urther.
553 // Also, verify that fSwizzlerSubset is initialized and valid. 650 // Also, verify that fSwizzlerSubset is initialized and valid.
554 SkASSERT(!fSwizzlerSubset.isEmpty() && fSwizzlerSubset.x() <= options.fS ubset->x() && 651 SkASSERT(!fSwizzlerSubset.isEmpty() && fSwizzlerSubset.x() <= options.fS ubset->x() &&
555 fSwizzlerSubset.width() == options.fSubset->width()); 652 fSwizzlerSubset.width() == options.fSubset->width());
556 swizzlerOptions.fSubset = &fSwizzlerSubset; 653 swizzlerOptions.fSubset = &fSwizzlerSubset;
557 } 654 }
558 fSwizzler.reset(SkSwizzler::CreateSwizzler(swizzlerInfo, nullptr, dstInfo, s wizzlerOptions, 655 fSwizzler.reset(SkSwizzler::CreateSwizzler(swizzlerInfo, nullptr, dstInfo, s wizzlerOptions,
559 nullptr, preSwizzled)); 656 nullptr, preSwizzled));
560 SkASSERT(fSwizzler); 657 SkASSERT(fSwizzler);
561 fStorage.reset(get_row_bytes(fDecoderMgr->dinfo())); 658 }
562 fSrcRow = fStorage.get(); 659
660 bool SkJpegCodec::initializeColorXform(const SkImageInfo& dstInfo, bool needsCol orXform) {
661 if (needsColorXform) {
662 fColorXform = SkColorSpaceXform::New(sk_ref_sp(this->getInfo().colorSpac e()),
663 sk_ref_sp(dstInfo.colorSpace()));
664 if (!fColorXform && kRGBA_F16_SkColorType == dstInfo.colorType()) {
665 return false;
666 }
667 }
668
669 return true;
563 } 670 }
564 671
565 SkSampler* SkJpegCodec::getSampler(bool createIfNecessary) { 672 SkSampler* SkJpegCodec::getSampler(bool createIfNecessary) {
566 if (!createIfNecessary || fSwizzler) { 673 if (!createIfNecessary || fSwizzler) {
567 SkASSERT(!fSwizzler || (fSrcRow && fStorage.get() == fSrcRow)); 674 SkASSERT(!fSwizzler || (fSwizzleSrcRow && fStorage.get() == fSwizzleSrcR ow));
568 return fSwizzler; 675 return fSwizzler;
569 } 676 }
570 677
571 this->initializeSwizzler(this->dstInfo(), this->options()); 678 this->initializeSwizzler(this->dstInfo(), this->options());
679 this->allocateStorage(this->dstInfo());
572 return fSwizzler; 680 return fSwizzler;
573 } 681 }
574 682
575 SkCodec::Result SkJpegCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, 683 SkCodec::Result SkJpegCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
576 const Options& options, SkPMColor ctable[], int* ctableCount) { 684 const Options& options, SkPMColor ctable[], int* ctableCount) {
577 // Set the jump location for libjpeg errors 685 // Set the jump location for libjpeg errors
578 if (setjmp(fDecoderMgr->getJmpBuf())) { 686 if (setjmp(fDecoderMgr->getJmpBuf())) {
579 SkCodecPrintf("setjmp: Error from libjpeg\n"); 687 SkCodecPrintf("setjmp: Error from libjpeg\n");
580 return kInvalidInput; 688 return kInvalidInput;
581 } 689 }
582 690
583 // Check if we can decode to the requested destination and set the output co lor space 691 // Check if we can decode to the requested destination and set the output co lor space
584 if (!this->setOutputColorSpace(dstInfo)) { 692 bool needsColorXform = needs_color_xform(dstInfo, this->getInfo());
693 if (!this->setOutputColorSpace(dstInfo, needsColorXform)) {
585 return kInvalidConversion; 694 return kInvalidConversion;
586 } 695 }
587 696
588 // Now, given valid output dimensions, we can start the decompress 697 if (!this->initializeColorXform(dstInfo, needsColorXform)) {
698 return fDecoderMgr->returnFailure("initializeColorXform", kInvalidParame ters);
699 }
700
589 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) { 701 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) {
590 SkCodecPrintf("start decompress failed\n"); 702 SkCodecPrintf("start decompress failed\n");
591 return kInvalidInput; 703 return kInvalidInput;
592 } 704 }
593 705
594 #ifdef TURBO_HAS_CROP 706 #ifdef TURBO_HAS_CROP
595 if (options.fSubset) { 707 if (options.fSubset) {
596 uint32_t startX = options.fSubset->x(); 708 uint32_t startX = options.fSubset->x();
597 uint32_t width = options.fSubset->width(); 709 uint32_t width = options.fSubset->width();
598 710
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
639 } 751 }
640 752
641 // We will need a swizzler if we are performing a subset decode or 753 // We will need a swizzler if we are performing a subset decode or
642 // converting from CMYK. 754 // converting from CMYK.
643 J_COLOR_SPACE colorSpace = fDecoderMgr->dinfo()->out_color_space; 755 J_COLOR_SPACE colorSpace = fDecoderMgr->dinfo()->out_color_space;
644 if (options.fSubset || JCS_CMYK == colorSpace || JCS_RGB == colorSpace) { 756 if (options.fSubset || JCS_CMYK == colorSpace || JCS_RGB == colorSpace) {
645 this->initializeSwizzler(dstInfo, options); 757 this->initializeSwizzler(dstInfo, options);
646 } 758 }
647 #endif 759 #endif
648 760
761 this->allocateStorage(dstInfo);
762
649 return kSuccess; 763 return kSuccess;
650 } 764 }
651 765
652 int SkJpegCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) { 766 int SkJpegCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
653 // Set the jump location for libjpeg errors 767 int rows = this->readRows(this->dstInfo(), dst, dstRowBytes, count);
654 if (setjmp(fDecoderMgr->getJmpBuf())) { 768 if (rows < count) {
655 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); 769 // This allows us to skip calling jpeg_finish_decompress().
656 } 770 fDecoderMgr->dinfo()->output_scanline = this->dstInfo().height();
657 // Read rows one at a time
658 JSAMPLE* dstRow;
659 size_t srcRowBytes = get_row_bytes(fDecoderMgr->dinfo());
660 if (fSwizzler) {
661 // write data to storage row, then sample using swizzler
662 dstRow = fSrcRow;
663 } else {
664 // write data directly to dst
665 SkASSERT(count == 1 || dstRowBytes >= srcRowBytes);
666 dstRow = (JSAMPLE*) dst;
667 } 771 }
668 772
669 for (int y = 0; y < count; y++) { 773 return rows;
670 // Read row of the image
671 uint32_t rowsDecoded = jpeg_read_scanlines(fDecoderMgr->dinfo(), &dstRow , 1);
672 sk_msan_mark_initialized(dstRow, dstRow + srcRowBytes, "skbug.com/4550") ;
673 if (rowsDecoded != 1) {
674 fDecoderMgr->dinfo()->output_scanline = this->dstInfo().height();
675 return y;
676 }
677
678 if (fSwizzler) {
679 // use swizzler to sample row
680 fSwizzler->swizzle(dst, dstRow);
681 dst = SkTAddOffset<JSAMPLE>(dst, dstRowBytes);
682 } else {
683 dstRow = SkTAddOffset<JSAMPLE>(dstRow, dstRowBytes);
684 }
685 }
686 return count;
687 } 774 }
688 775
689 bool SkJpegCodec::onSkipScanlines(int count) { 776 bool SkJpegCodec::onSkipScanlines(int count) {
690 // Set the jump location for libjpeg errors 777 // Set the jump location for libjpeg errors
691 if (setjmp(fDecoderMgr->getJmpBuf())) { 778 if (setjmp(fDecoderMgr->getJmpBuf())) {
692 return fDecoderMgr->returnFalse("setjmp"); 779 return fDecoderMgr->returnFalse("onSkipScanlines");
693 } 780 }
694 781
695 #ifdef TURBO_HAS_SKIP 782 #ifdef TURBO_HAS_SKIP
696 return (uint32_t) count == jpeg_skip_scanlines(fDecoderMgr->dinfo(), count); 783 return (uint32_t) count == jpeg_skip_scanlines(fDecoderMgr->dinfo(), count);
697 #else 784 #else
698 if (!fSrcRow) { 785 if (!fSwizzleSrcRow) {
699 fStorage.reset(get_row_bytes(fDecoderMgr->dinfo())); 786 fStorage.reset(get_row_bytes(fDecoderMgr->dinfo()));
700 fSrcRow = fStorage.get(); 787 fSwizzleSrcRow = fStorage.get();
701 } 788 }
702 789
703 for (int y = 0; y < count; y++) { 790 for (int y = 0; y < count; y++) {
704 if (1 != jpeg_read_scanlines(fDecoderMgr->dinfo(), &fSrcRow, 1)) { 791 if (1 != jpeg_read_scanlines(fDecoderMgr->dinfo(), &fSwizzleSrcRow, 1)) {
705 return false; 792 return false;
706 } 793 }
707 } 794 }
708 return true; 795 return true;
709 #endif 796 #endif
710 } 797 }
711 798
712 static bool is_yuv_supported(jpeg_decompress_struct* dinfo) { 799 static bool is_yuv_supported(jpeg_decompress_struct* dinfo) {
713 // Scaling is not supported in raw data mode. 800 // Scaling is not supported in raw data mode.
714 SkASSERT(dinfo->scale_num == dinfo->scale_denom); 801 SkASSERT(dinfo->scale_num == dinfo->scale_denom);
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
899 986
900 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock); 987 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock);
901 if (linesRead < remainingRows) { 988 if (linesRead < remainingRows) {
902 // FIXME: Handle incomplete YUV decodes without signalling an error. 989 // FIXME: Handle incomplete YUV decodes without signalling an error.
903 return kInvalidInput; 990 return kInvalidInput;
904 } 991 }
905 } 992 }
906 993
907 return kSuccess; 994 return kSuccess;
908 } 995 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698