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

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

Issue 1549473003: Add getYUV8Planes() API to SkCodec (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years 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 "SkJpegCodec.h" 9 #include "SkJpegCodec.h"
10 #include "SkJpegDecoderMgr.h" 10 #include "SkJpegDecoderMgr.h"
(...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 #endif 445 #endif
446 446
447 bool SkJpegCodec::onSkipScanlines(int count) { 447 bool SkJpegCodec::onSkipScanlines(int count) {
448 // Set the jump location for libjpeg errors 448 // Set the jump location for libjpeg errors
449 if (setjmp(fDecoderMgr->getJmpBuf())) { 449 if (setjmp(fDecoderMgr->getJmpBuf())) {
450 return fDecoderMgr->returnFalse("setjmp"); 450 return fDecoderMgr->returnFalse("setjmp");
451 } 451 }
452 452
453 return (uint32_t) count == jpeg_skip_scanlines(fDecoderMgr->dinfo(), count); 453 return (uint32_t) count == jpeg_skip_scanlines(fDecoderMgr->dinfo(), count);
454 } 454 }
455
456 static bool is_yuv_supported(jpeg_decompress_struct* dinfo) {
457 // Scaling is not supported in raw data mode.
458 SkASSERT(dinfo->scale_num == dinfo->scale_denom);
459
460 // I can't imagine that this would ever change, but we do depend on it.
461 SkASSERT(8 == DCTSIZE);
462
463 if (JCS_YCbCr != dinfo->jpeg_color_space) {
464 return false;
465 }
466
467 SkASSERT(3 == dinfo->num_components);
468 SkASSERT(dinfo->comp_info);
469
470 // FIXME: Support YUV for progressive images. The fix might be as easy as
471 // removing this check.
msarett 2015/12/22 21:01:36 Chromium supports progressive images (in a differe
472 if (dinfo->comps_in_scan < dinfo->num_components || dinfo->progressive_mode) {
473 return false;
474 }
475
476 // TODO: It is possible to perform a YUV decode for any combination of
477 // horizontal and vertical sampling that is supported by
478 // libjpeg/libjpeg-turbo. However, we will start by supporting
479 // only the common cases.
msarett 2015/12/22 21:01:36 Chromium and SkImageDecoder only support these com
480 bool unitUV = (1 == dinfo->comp_info[1].h_samp_factor) &&
481 (1 == dinfo->comp_info[1].v_samp_factor) &&
482 (1 == dinfo->comp_info[2].h_samp_factor) &&
483 (1 == dinfo->comp_info[2].v_samp_factor);
484
485 int hSampY = dinfo->comp_info[0].h_samp_factor;
486 int vSampY = dinfo->comp_info[0].v_samp_factor;
487 // TODO: Chromium reports the YUV subsampling factors to the client.
488 // Ex: 444, 422, 420, etc.
489 // Could it be useful to do the same here?
490 bool commonY = (1 == hSampY && 1 == vSampY) ||
491 (2 == hSampY && 1 == vSampY) ||
492 (2 == hSampY && 2 == vSampY) ||
493 (1 == hSampY && 2 == vSampY) ||
494 (4 == hSampY && 1 == vSampY) ||
495 (4 == hSampY && 2 == vSampY);
496
497 return unitUV && commonY;
498 }
499
500 bool SkJpegCodec::onGetYUV8Sizes(SkISize sizes[3]) const {
501 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
502 if (!is_yuv_supported(dinfo)) {
503 return false;
504 }
505
506 sizes[0].set(dinfo->comp_info[0].width_in_blocks * 8, dinfo->comp_info[0].do wnsampled_height);
507 sizes[1].set(dinfo->comp_info[1].width_in_blocks * 8, dinfo->comp_info[1].do wnsampled_height);
508 sizes[2].set(dinfo->comp_info[2].width_in_blocks * 8, dinfo->comp_info[2].do wnsampled_height);
509 return true;
510 }
511
512 SkCodec::Result SkJpegCodec::onGetYUV8Planes(SkISize sizes[3], void* pixels[3], size_t rowBytes[3],
513 SkYUVColorSpace* colorSpace) {
514 SkISize recommendedSizes[3];
515 // This will check is_yuv_supported(), so we don't need to here.
516 bool supportsYUV = this->onGetYUV8Sizes(recommendedSizes);
517 if (!supportsYUV || memcmp((const void*) recommendedSizes, (const void*) siz es, sizeof(sizes)))
518 {
519 fDecoderMgr->returnFailure("onGetYUV8Sizes", kInvalidInput);
520 }
521
522 // Set the jump location for libjpeg errors
523 if (setjmp(fDecoderMgr->getJmpBuf())) {
524 return fDecoderMgr->returnFailure("setjmp", kInvalidInput);
525 }
526
527 // Get a pointer to the decompress info since we will use it quite frequentl y
528 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
529
530 dinfo->raw_data_out = TRUE;
531 if (!jpeg_start_decompress(dinfo)) {
532 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput);
533 }
534
535 // A previous implementation claims that the return value of is_yuv_supporte d()
536 // may change after calling jpeg_start_decompress(). It looks to me like th is
537 // was caused by a bug in the old code, but we'll be safe and check here.
538 SkASSERT(is_yuv_supported(dinfo));
539
540 // TODO: As mentioned in is_yuv_supported(), we are requiring that U and V b e
541 // the same size and Y be equal to the image size (possibly with paddi ng
542 // on the width). This is by far the most common case, but technicall y we
543 // could support other combinations of sampling factors.
544 SkASSERT(sizes[1] == sizes[2]);
545 SkASSERT((uint32_t) sizes[0].height() == dinfo->output_height);
546
547 // Build a JSAMPIMAGE to handle output from libjpeg-turbo. A JSAMPIMAGE has
548 // a 2-D array of pixels for each of the components (Y, U, V) in the image.
549 // Cheat Sheet:
550 // JSAMPIMAGE == JSAMPLEARRAY* == JSAMPROW** == JSAMPLE***
551 JSAMPARRAY yuv[3];
552 // Set aside enough space for pointers to rows of Y, U, and V.
553 JSAMPROW rowptrs[16 + 8 + 8];
554 // TODO: Note that if we support more sampling factors, the amount of space set
555 // aside here and/or which pointers we use for Y, U, and V may change.
556 // This would also add complexity to the code below.
557 yuv[0] = &rowptrs[0]; // Y rows (8 or 16)
558 yuv[1] = &rowptrs[16]; // U rows (8)
559 yuv[2] = &rowptrs[24]; // V rows (8)
560
561 // Initialize rowptrs.
562 int numYRowsPerBlock = 8 * dinfo->comp_info[0].v_samp_factor;
563 for (int i = 0; i < numYRowsPerBlock; i++) {
564 rowptrs[i] = (JSAMPROW) SkTAddOffset<void>(pixels[0], i * rowBytes[0]);
565 }
566 for (int i = 0; i < 8; i++) {
567 rowptrs[i + 16] =(JSAMPROW) SkTAddOffset<void>(pixels[1], i * rowBytes[1 ]);
568 rowptrs[i + 24] = (JSAMPROW) SkTAddOffset<void>(pixels[2], i * rowBytes[ 2]);
569 }
570
571 // After each loop iteration, we will increment pointers to Y, U, and V.
572 int blockIncrementY = numYRowsPerBlock * rowBytes[0];
573 int blockIncrementU = 8 * rowBytes[1];
574 int blockIncrementV = 8 * rowBytes[2];
575
576 uint32_t numRowsPerBlock = numYRowsPerBlock;
577 // We intentionally round down here. As a final step, we may need to decode one
578 // more partial block.
579 int numIters = dinfo->output_height / numRowsPerBlock;
580 int iters = 0;
581 while (iters < numIters) {
582 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock);
583 if (linesRead < numRowsPerBlock) {
584 // FIXME: We will treat this as an error for now.
585 // Can we fill memory and still display an image?
586 return kIncompleteInput;
587 }
588
589 // Update rowptrs.
590 for (int i = 0; i < numYRowsPerBlock; i++) {
591 rowptrs[i] += blockIncrementY;
592 }
593 for (int i = 0; i < 8; i++) {
594 rowptrs[i + 16] += blockIncrementU;
595 rowptrs[i + 24] += blockIncrementV;
596 }
597 iters++;
598 }
599
600 uint32_t remainingRows = dinfo->output_height - dinfo->output_scanline;
601 SkASSERT(remainingRows == dinfo->output_height % numRowsPerBlock);
602 SkASSERT(dinfo->output_scanline == numIters * numRowsPerBlock);
603 if (remainingRows > 0) {
604 // libjpeg-turbo needs memory to be padded by the block sizes. We will fulfill
605 // this requirement using a dummy row buffer.
606 SkAutoTMalloc<JSAMPLE> dummyRow(rowBytes[0]);
607 for (int i = remainingRows; i < numYRowsPerBlock; i++) {
608 rowptrs[i] = dummyRow.get();
609 }
610 int remainingUVRows = dinfo->comp_info[1].downsampled_height - 8 * numIt ers;
611 for (int i = remainingUVRows; i < 8; i++) {
612 rowptrs[i + 16] = dummyRow.get();
613 rowptrs[i + 24] = dummyRow.get();
614 }
615
616 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock);
617 if (linesRead < remainingRows) {
618 // FIXME: We will treat this as an error for now.
619 // Can we fill memory and still display an image?
620 return kIncompleteInput;
621 }
622 }
623
624 // FIXME: This is ugly. We should find a solution where the input memory is
625 // padded appropriately, but we are able to report the true width fro m
626 // the start.
627 sizes[0].fWidth = dinfo->comp_info[0].downsampled_width;
628 sizes[1].fWidth = dinfo->comp_info[1].downsampled_width;
629 sizes[2].fWidth = dinfo->comp_info[2].downsampled_width;
630
631 if (nullptr != colorSpace) {
632 *colorSpace = kJPEG_SkYUVColorSpace;
msarett 2015/12/22 21:01:36 Can someone comment on if/why this is useful? Wou
633 }
634
635 return kSuccess;
636 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698