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

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

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

Powered by Google App Engine
This is Rietveld 408576698