Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 182 dinfo.global_state = DSTATE_READY; | 182 dinfo.global_state = DSTATE_READY; |
| 183 dinfo.num_components = 0; | 183 dinfo.num_components = 0; |
| 184 dinfo.scale_num = num; | 184 dinfo.scale_num = num; |
| 185 dinfo.scale_denom = denom; | 185 dinfo.scale_denom = denom; |
| 186 turbo_jpeg_calc_output_dimensions(&dinfo); | 186 turbo_jpeg_calc_output_dimensions(&dinfo); |
| 187 | 187 |
| 188 // Return the calculated output dimensions for the given scale | 188 // Return the calculated output dimensions for the given scale |
| 189 return SkISize::Make(dinfo.output_width, dinfo.output_height); | 189 return SkISize::Make(dinfo.output_width, dinfo.output_height); |
| 190 } | 190 } |
| 191 | 191 |
| 192 SkCodec::Result SkJpegCodec::initializeSwizzler(const SkImageInfo& requestedInfo , | |
| 193 void* dst, size_t rowBytes, | |
| 194 const Options& options, | |
| 195 SkPMColor ctable[], | |
| 196 int* ctableCount) { | |
| 197 | |
| 198 const SkColorType srcColorType = requestedInfo.colorType(); | |
| 199 SkSwizzler::SrcConfig srcConfig; | |
| 200 switch (srcColorType) { | |
| 201 case kGray_8_SkColorType: | |
| 202 srcConfig = SkSwizzler::kGray; | |
| 203 break; | |
| 204 case kRGBA_8888_SkColorType: | |
| 205 srcConfig = SkSwizzler::kRGBX; | |
| 206 break; | |
| 207 case kBGRA_8888_SkColorType: | |
| 208 srcConfig = SkSwizzler::kBGRX; | |
| 209 break; | |
| 210 case kRGB_565_SkColorType: | |
| 211 srcConfig = SkSwizzler::kRGB_565; | |
| 212 break; | |
| 213 default: | |
| 214 //would have exited before now if the colorType was supported by jpe g | |
| 215 SkASSERT(false); | |
| 216 } | |
| 217 | |
| 218 fSwizzler.reset(SkSwizzler::CreateSwizzler(srcConfig, NULL, requestedInfo, d st, rowBytes, | |
| 219 options.fZeroInitialized, this->g etInfo().width())); | |
| 220 if (!fSwizzler) { | |
| 221 // FIXME: CreateSwizzler could fail for another reason. | |
| 222 return kUnimplemented; | |
| 223 } | |
| 224 return kSuccess; | |
| 225 } | |
| 226 | |
| 192 /* | 227 /* |
| 193 * Handles rewinding the input stream if it is necessary | 228 * Handles rewinding the input stream if it is necessary |
| 194 */ | 229 */ |
| 195 bool SkJpegCodec::handleRewind() { | 230 bool SkJpegCodec::handleRewind() { |
| 196 switch(this->rewindIfNeeded()) { | 231 switch(this->rewindIfNeeded()) { |
| 197 case kCouldNotRewind_RewindState: | 232 case kCouldNotRewind_RewindState: |
| 198 return fDecoderMgr->returnFalse("could not rewind"); | 233 return fDecoderMgr->returnFalse("could not rewind"); |
| 199 case kRewound_RewindState: { | 234 case kRewound_RewindState: { |
| 200 JpegDecoderMgr* decoderMgr = NULL; | 235 JpegDecoderMgr* decoderMgr = NULL; |
| 201 if (!ReadHeader(this->stream(), NULL, &decoderMgr)) { | 236 if (!ReadHeader(this->stream(), NULL, &decoderMgr)) { |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 fDecoderMgr->dinfo()->scale_denom = 8; | 315 fDecoderMgr->dinfo()->scale_denom = 8; |
| 281 fDecoderMgr->dinfo()->scale_num = 8; | 316 fDecoderMgr->dinfo()->scale_num = 8; |
| 282 turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); | 317 turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); |
| 283 while (fDecoderMgr->dinfo()->output_width != dstWidth || | 318 while (fDecoderMgr->dinfo()->output_width != dstWidth || |
| 284 fDecoderMgr->dinfo()->output_height != dstHeight) { | 319 fDecoderMgr->dinfo()->output_height != dstHeight) { |
| 285 | 320 |
| 286 // Return a failure if we have tried all of the possible scales | 321 // Return a failure if we have tried all of the possible scales |
| 287 if (1 == fDecoderMgr->dinfo()->scale_num || | 322 if (1 == fDecoderMgr->dinfo()->scale_num || |
| 288 dstWidth > fDecoderMgr->dinfo()->output_width || | 323 dstWidth > fDecoderMgr->dinfo()->output_width || |
| 289 dstHeight > fDecoderMgr->dinfo()->output_height) { | 324 dstHeight > fDecoderMgr->dinfo()->output_height) { |
| 325 // reset scale settings on failure | |
| 326 this->fDecoderMgr->dinfo()->scale_num = 8; | |
| 327 turbo_jpeg_calc_output_dimensions(this->fDecoderMgr->dinfo()); | |
| 290 return fDecoderMgr->returnFalse("could not scale to requested dimens ions"); | 328 return fDecoderMgr->returnFalse("could not scale to requested dimens ions"); |
| 291 } | 329 } |
| 292 | 330 |
| 293 // Try the next scale | 331 // Try the next scale |
| 294 fDecoderMgr->dinfo()->scale_num -= 1; | 332 fDecoderMgr->dinfo()->scale_num -= 1; |
| 295 turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); | 333 turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); |
| 296 } | 334 } |
| 297 return true; | 335 return true; |
| 298 } | 336 } |
| 299 | 337 |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 378 } | 416 } |
| 379 | 417 |
| 380 /* | 418 /* |
| 381 * Enable scanline decoding for jpegs | 419 * Enable scanline decoding for jpegs |
| 382 */ | 420 */ |
| 383 class SkJpegScanlineDecoder : public SkScanlineDecoder { | 421 class SkJpegScanlineDecoder : public SkScanlineDecoder { |
| 384 public: | 422 public: |
| 385 SkJpegScanlineDecoder(const SkImageInfo& dstInfo, SkJpegCodec* codec) | 423 SkJpegScanlineDecoder(const SkImageInfo& dstInfo, SkJpegCodec* codec) |
| 386 : INHERITED(dstInfo) | 424 : INHERITED(dstInfo) |
| 387 , fCodec(codec) | 425 , fCodec(codec) |
| 388 {} | 426 { |
| 427 if(fCodec->fSwizzler) { | |
| 428 fStorage.reset(fCodec->getInfo().width() * dstInfo.minRowBytes()); | |
| 429 fSrcRow = static_cast<uint8_t*>(fStorage.get()); | |
| 430 } | |
| 431 } | |
| 389 | 432 |
| 390 virtual ~SkJpegScanlineDecoder() { | 433 virtual ~SkJpegScanlineDecoder() { |
| 391 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { | 434 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { |
| 392 SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n"); | 435 SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n"); |
| 393 return; | 436 return; |
| 394 } | 437 } |
| 395 | 438 |
| 396 // We may not have decoded the entire image. Prevent libjpeg-turbo from failing on a | 439 // We may not have decoded the entire image. Prevent libjpeg-turbo from failing on a |
| 397 // partial decode. | 440 // partial decode. |
| 398 fCodec->fDecoderMgr->dinfo()->output_scanline = fCodec->getInfo().height (); | 441 fCodec->fDecoderMgr->dinfo()->output_scanline = fCodec->getInfo().height (); |
| 399 turbo_jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo()); | 442 turbo_jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo()); |
| 400 } | 443 } |
| 401 | 444 |
| 402 SkCodec::Result onGetScanlines(void* dst, int count, size_t rowBytes) overri de { | 445 SkCodec::Result onGetScanlines(void* dst, int count, size_t rowBytes) overri de { |
| 403 // Set the jump location for libjpeg errors | 446 // Set the jump location for libjpeg errors |
| 404 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { | 447 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { |
| 405 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvali dInput); | 448 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvali dInput); |
| 406 } | 449 } |
| 450 // Read rows one at a time | |
| 451 JSAMPLE* dstRow; | |
| 452 if (fCodec->fSwizzler) { | |
| 453 // write data to storage row, then sample using swizzler | |
| 454 dstRow = fSrcRow; | |
| 455 } else { | |
| 456 // write data directly to dst | |
| 457 dstRow = (JSAMPLE*) dst; | |
| 458 } | |
| 407 | 459 |
| 408 // Read rows one at a time | |
| 409 JSAMPLE* dstRow = (JSAMPLE*) dst; | |
| 410 for (int y = 0; y < count; y++) { | 460 for (int y = 0; y < count; y++) { |
| 411 // Read row of the image | 461 // Read row of the image |
| 412 uint32_t rowsDecoded = | 462 uint32_t rowsDecoded = |
| 413 turbo_jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &dst Row, 1); | 463 turbo_jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &dst Row, 1); |
| 414 if (rowsDecoded != 1) { | 464 if (rowsDecoded != 1) { |
| 415 SkSwizzler::Fill( | 465 SkSwizzler::Fill( |
| 416 dstRow, this->dstInfo(), rowBytes, count - y, SK_ColorBL ACK, NULL); | 466 dstRow, this->dstInfo(), rowBytes, count - y, SK_ColorBL ACK, NULL); |
| 417 fCodec->fDecoderMgr->dinfo()->output_scanline = this->dstInfo(). height(); | 467 fCodec->fDecoderMgr->dinfo()->output_scanline = this->dstInfo(). height(); |
| 418 return SkCodec::kIncompleteInput; | 468 return SkCodec::kIncompleteInput; |
| 419 } | 469 } |
| 420 | 470 |
| 421 // Convert to RGBA if necessary | 471 // Convert to RGBA if necessary |
| 422 if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) { | 472 if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) { |
| 423 convert_CMYK_to_RGBA(dstRow, this->dstInfo().width()); | 473 convert_CMYK_to_RGBA(dstRow, this->dstInfo().width()); |
| 424 } | 474 } |
| 425 | 475 |
| 426 // Move to the next row | 476 if(fCodec->fSwizzler) { |
| 427 dstRow = SkTAddOffset<JSAMPLE>(dstRow, rowBytes); | 477 // use swizzler to sample row |
| 478 fCodec->fSwizzler->setDstRow(dst); | |
| 479 fCodec->fSwizzler->next(dstRow); | |
| 480 dst = SkTAddOffset<JSAMPLE>(dst, rowBytes); | |
| 481 } else { | |
| 482 dstRow = SkTAddOffset<JSAMPLE>(dstRow, rowBytes); | |
| 483 } | |
| 428 } | 484 } |
| 429 | |
| 430 return SkCodec::kSuccess; | 485 return SkCodec::kSuccess; |
| 431 } | 486 } |
| 432 | 487 |
| 433 #ifndef TURBO_HAS_SKIP | 488 #ifndef TURBO_HAS_SKIP |
| 434 #define turbo_jpeg_skip_scanlines(dinfo, count) \ | 489 #define turbo_jpeg_skip_scanlines(dinfo, count) \ |
| 435 SkAutoMalloc storage(dinfo->output_width * dinfo->out_color_components); \ | 490 SkAutoMalloc storage(dinfo->output_width * dinfo->out_color_components); \ |
| 436 uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); \ | 491 uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); \ |
| 437 for (int y = 0; y < count; y++) { \ | 492 for (int y = 0; y < count; y++) { \ |
| 438 turbo_jpeg_read_scanlines(dinfo, &storagePtr, 1); \ | 493 turbo_jpeg_read_scanlines(dinfo, &storagePtr, 1); \ |
| 439 } | 494 } |
| 440 #endif | 495 #endif |
| 441 | 496 |
| 442 SkCodec::Result onSkipScanlines(int count) override { | 497 SkCodec::Result onSkipScanlines(int count) override { |
| 443 // Set the jump location for libjpeg errors | 498 // Set the jump location for libjpeg errors |
| 444 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { | 499 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { |
| 445 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvali dInput); | 500 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvali dInput); |
| 446 } | 501 } |
| 447 | 502 |
| 448 turbo_jpeg_skip_scanlines(fCodec->fDecoderMgr->dinfo(), count); | 503 turbo_jpeg_skip_scanlines(fCodec->fDecoderMgr->dinfo(), count); |
| 449 | 504 |
| 450 return SkCodec::kSuccess; | 505 return SkCodec::kSuccess; |
| 451 } | 506 } |
| 452 | 507 |
| 453 private: | 508 private: |
| 454 SkAutoTDelete<SkJpegCodec> fCodec; | 509 SkAutoTDelete<SkJpegCodec> fCodec; |
| 510 SkAutoMalloc fStorage; | |
|
scroggo
2015/07/31 13:35:33
This one is only needed for sampling, too, right?
emmaleer
2015/07/31 18:41:56
Yep! I've added a comment.
| |
| 511 uint8_t* fSrcRow; // Only used if sampling is needed | |
| 455 | 512 |
| 456 typedef SkScanlineDecoder INHERITED; | 513 typedef SkScanlineDecoder INHERITED; |
| 457 }; | 514 }; |
| 458 | 515 |
| 459 SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo, | 516 SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo, |
| 460 const Options& options, SkPMColor ctable[], int* ctableCount) { | 517 const Options& options, SkPMColor ctable[], int* ctableCount) { |
| 461 | 518 |
| 462 // Rewind the stream if needed | 519 // Rewind the stream if needed |
| 463 if (!this->handleRewind()) { | 520 if (!this->handleRewind()) { |
| 464 SkCodecPrintf("Could not rewind\n"); | 521 SkCodecPrintf("Could not rewind\n"); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 481 } | 538 } |
| 482 | 539 |
| 483 // Check if we can decode to the requested destination and set the output co lor space | 540 // Check if we can decode to the requested destination and set the output co lor space |
| 484 if (!codec->setOutputColorSpace(dstInfo)) { | 541 if (!codec->setOutputColorSpace(dstInfo)) { |
| 485 SkCodecPrintf("Cannot convert to output type\n"); | 542 SkCodecPrintf("Cannot convert to output type\n"); |
| 486 return NULL; | 543 return NULL; |
| 487 } | 544 } |
| 488 | 545 |
| 489 // Perform the necessary scaling | 546 // Perform the necessary scaling |
| 490 if (!codec->scaleToDimensions(dstInfo.width(), dstInfo.height())) { | 547 if (!codec->scaleToDimensions(dstInfo.width(), dstInfo.height())) { |
| 491 SkCodecPrintf("Cannot scale to output dimensions\n"); | 548 // native scaling to dstInfo dimensions not supported |
| 492 return NULL; | 549 |
| 550 if (dstInfo.height() != this->getInfo().height()) { | |
|
scroggo
2015/07/31 13:35:33
This should call DimensionsSupportedForSampling
emmaleer
2015/07/31 18:41:56
Acknowledged.
| |
| 551 // no non-native height sampling supported | |
| 552 return NULL; | |
| 553 } | |
| 554 // only support down sampling, dstWidth cannot be larger that srcWidth | |
| 555 if(dstInfo.width() > this->getInfo().width()) { | |
| 556 return NULL; | |
| 557 } | |
| 558 // create swizzler for sampling | |
| 559 if (codec->initializeSwizzler(dstInfo, NULL, dstInfo.minRowBytes(), opti ons, ctable, | |
| 560 ctableCount) != kSuccess) { | |
| 561 SkCodecPrintf("failed to initialize the swizzler.\n"); | |
| 562 return NULL; | |
| 563 } | |
| 493 } | 564 } |
| 494 | 565 |
| 495 // Now, given valid output dimensions, we can start the decompress | 566 // Now, given valid output dimensions, we can start the decompress |
| 496 if (!turbo_jpeg_start_decompress(codec->fDecoderMgr->dinfo())) { | 567 if (!turbo_jpeg_start_decompress(codec->fDecoderMgr->dinfo())) { |
| 497 SkCodecPrintf("start decompress failed\n"); | 568 SkCodecPrintf("start decompress failed\n"); |
| 498 return NULL; | 569 return NULL; |
| 499 } | 570 } |
| 500 | 571 |
| 501 // Return the new scanline decoder | 572 // Return the new scanline decoder |
| 502 return SkNEW_ARGS(SkJpegScanlineDecoder, (dstInfo, codec.detach())); | 573 return SkNEW_ARGS(SkJpegScanlineDecoder, (dstInfo, codec.detach())); |
| 503 } | 574 } |
| OLD | NEW |