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

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

Issue 1260673002: SkScaledCodec class (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Works for jpeg images 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 "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
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 int sampleX = this->getInfo().width() / requestedInfo.width();
scroggo 2015/07/30 17:53:01 I think this should be shared with png, etc.
218
219 fSwizzler.reset(SkSwizzler::CreateSwizzler(srcConfig, NULL, requestedInfo, d st, rowBytes,
220 options.fZeroInitialized, sampleX ));
221 if (!fSwizzler) {
222 // FIXME: CreateSwizzler could fail for another reason.
223 return kUnimplemented;
224 }
225 return kSuccess;
226 }
227
192 /* 228 /*
193 * Handles rewinding the input stream if it is necessary 229 * Handles rewinding the input stream if it is necessary
194 */ 230 */
195 bool SkJpegCodec::handleRewind() { 231 bool SkJpegCodec::handleRewind() {
196 switch(this->rewindIfNeeded()) { 232 switch(this->rewindIfNeeded()) {
197 case kCouldNotRewind_RewindState: 233 case kCouldNotRewind_RewindState:
198 return fDecoderMgr->returnFalse("could not rewind"); 234 return fDecoderMgr->returnFalse("could not rewind");
199 case kRewound_RewindState: { 235 case kRewound_RewindState: {
200 JpegDecoderMgr* decoderMgr = NULL; 236 JpegDecoderMgr* decoderMgr = NULL;
201 if (!ReadHeader(this->stream(), NULL, &decoderMgr)) { 237 if (!ReadHeader(this->stream(), NULL, &decoderMgr)) {
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 fDecoderMgr->dinfo()->scale_denom = 8; 316 fDecoderMgr->dinfo()->scale_denom = 8;
281 fDecoderMgr->dinfo()->scale_num = 8; 317 fDecoderMgr->dinfo()->scale_num = 8;
282 turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); 318 turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo());
283 while (fDecoderMgr->dinfo()->output_width != dstWidth || 319 while (fDecoderMgr->dinfo()->output_width != dstWidth ||
284 fDecoderMgr->dinfo()->output_height != dstHeight) { 320 fDecoderMgr->dinfo()->output_height != dstHeight) {
285 321
286 // Return a failure if we have tried all of the possible scales 322 // Return a failure if we have tried all of the possible scales
287 if (1 == fDecoderMgr->dinfo()->scale_num || 323 if (1 == fDecoderMgr->dinfo()->scale_num ||
288 dstWidth > fDecoderMgr->dinfo()->output_width || 324 dstWidth > fDecoderMgr->dinfo()->output_width ||
289 dstHeight > fDecoderMgr->dinfo()->output_height) { 325 dstHeight > fDecoderMgr->dinfo()->output_height) {
326 this->fDecoderMgr->dinfo()->scale_num = 8;
scroggo 2015/07/30 17:53:01 Could you add a comment explaining this? I think t
emmaleer 2015/07/30 22:27:55 Yes, the idea is if scaleToDimensions fails, then
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
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 fStorage.reset(fCodec->getInfo().width() * dstInfo.minRowBytes());
428 fSrcRow = static_cast<uint8_t*>(fStorage.get());
429 }
389 430
390 virtual ~SkJpegScanlineDecoder() { 431 virtual ~SkJpegScanlineDecoder() {
391 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { 432 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
392 SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n"); 433 SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n");
393 return; 434 return;
394 } 435 }
395 436
396 // We may not have decoded the entire image. Prevent libjpeg-turbo from failing on a 437 // We may not have decoded the entire image. Prevent libjpeg-turbo from failing on a
397 // partial decode. 438 // partial decode.
398 fCodec->fDecoderMgr->dinfo()->output_scanline = fCodec->getInfo().height (); 439 fCodec->fDecoderMgr->dinfo()->output_scanline = fCodec->getInfo().height ();
399 turbo_jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo()); 440 turbo_jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo());
400 } 441 }
401 442
402 SkCodec::Result onGetScanlines(void* dst, int count, size_t rowBytes) overri de { 443 SkCodec::Result onGetScanlines(void* dst, int count, size_t rowBytes) overri de {
403 // Set the jump location for libjpeg errors 444 // Set the jump location for libjpeg errors
404 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { 445 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
405 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvali dInput); 446 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvali dInput);
406 } 447 }
448 // Read rows one at a time
449 JSAMPLE* dstRow;
450 if (fCodec->fSwizzler) {
451 // write data to storage row, then sample using swizzler
452 dstRow = fSrcRow;
453 } else {
454 // write data directly to dst
455 dstRow = (JSAMPLE*) dst;
456 }
407 457
408 // Read rows one at a time
409 JSAMPLE* dstRow = (JSAMPLE*) dst;
410 for (int y = 0; y < count; y++) { 458 for (int y = 0; y < count; y++) {
411 // Read row of the image 459 // Read row of the image
412 uint32_t rowsDecoded = 460 uint32_t rowsDecoded =
413 turbo_jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &dst Row, 1); 461 turbo_jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &dst Row, 1);
414 if (rowsDecoded != 1) { 462 if (rowsDecoded != 1) {
415 SkSwizzler::Fill( 463 SkSwizzler::Fill(
416 dstRow, this->dstInfo(), rowBytes, count - y, SK_ColorBL ACK, NULL); 464 dstRow, this->dstInfo(), rowBytes, count - y, SK_ColorBL ACK, NULL);
417 fCodec->fDecoderMgr->dinfo()->output_scanline = this->dstInfo(). height(); 465 fCodec->fDecoderMgr->dinfo()->output_scanline = this->dstInfo(). height();
418 return SkCodec::kIncompleteInput; 466 return SkCodec::kIncompleteInput;
419 } 467 }
420 468
421 // Convert to RGBA if necessary 469 // Convert to RGBA if necessary
422 if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) { 470 if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) {
423 convert_CMYK_to_RGBA(dstRow, this->dstInfo().width()); 471 convert_CMYK_to_RGBA(dstRow, this->dstInfo().width());
424 } 472 }
425 473
426 // Move to the next row 474 if(fCodec->fSwizzler) {
427 dstRow = SkTAddOffset<JSAMPLE>(dstRow, rowBytes); 475 // use swizzler to sample row
476 fCodec->fSwizzler->setDstRow(dst);
477 fCodec->fSwizzler->next(dstRow);
478 dst += rowBytes;
479 } else {
480 dstRow = SkTAddOffset<JSAMPLE>(dstRow, rowBytes);
481 }
428 } 482 }
429
430 return SkCodec::kSuccess; 483 return SkCodec::kSuccess;
431 } 484 }
432 485
433 #ifndef TURBO_HAS_SKIP 486 #ifndef TURBO_HAS_SKIP
434 #define turbo_jpeg_skip_scanlines(dinfo, count) \ 487 #define turbo_jpeg_skip_scanlines(dinfo, count) \
435 SkAutoMalloc storage(dinfo->output_width * dinfo->out_color_components); \ 488 SkAutoMalloc storage(dinfo->output_width * dinfo->out_color_components); \
436 uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); \ 489 uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); \
437 for (int y = 0; y < count; y++) { \ 490 for (int y = 0; y < count; y++) { \
438 turbo_jpeg_read_scanlines(dinfo, &storagePtr, 1); \ 491 turbo_jpeg_read_scanlines(dinfo, &storagePtr, 1); \
439 } 492 }
440 #endif 493 #endif
441 494
442 SkCodec::Result onSkipScanlines(int count) override { 495 SkCodec::Result onSkipScanlines(int count) override {
443 // Set the jump location for libjpeg errors 496 // Set the jump location for libjpeg errors
444 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { 497 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
445 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvali dInput); 498 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvali dInput);
446 } 499 }
447 500
448 turbo_jpeg_skip_scanlines(fCodec->fDecoderMgr->dinfo(), count); 501 turbo_jpeg_skip_scanlines(fCodec->fDecoderMgr->dinfo(), count);
449 502
450 return SkCodec::kSuccess; 503 return SkCodec::kSuccess;
451 } 504 }
452 505
453 private: 506 private:
454 SkAutoTDelete<SkJpegCodec> fCodec; 507 SkAutoTDelete<SkJpegCodec> fCodec;
508 SkAutoMalloc fStorage;
msarett 2015/07/30 13:55:38 nit: off by a space
emmaleer 2015/07/30 17:50:39 Acknowledged.
509 uint8_t* fSrcRow;
455 510
456 typedef SkScanlineDecoder INHERITED; 511 typedef SkScanlineDecoder INHERITED;
457 }; 512 };
458 513
459 SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo, 514 SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo,
460 const Options& options, SkPMColor ctable[], int* ctableCount) { 515 const Options& options, SkPMColor ctable[], int* ctableCount) {
461 516
462 // Rewind the stream if needed 517 // Rewind the stream if needed
463 if (!this->handleRewind()) { 518 if (!this->handleRewind()) {
464 SkCodecPrintf("Could not rewind\n"); 519 SkCodecPrintf("Could not rewind\n");
(...skipping 16 matching lines...) Expand all
481 } 536 }
482 537
483 // Check if we can decode to the requested destination and set the output co lor space 538 // Check if we can decode to the requested destination and set the output co lor space
484 if (!codec->setOutputColorSpace(dstInfo)) { 539 if (!codec->setOutputColorSpace(dstInfo)) {
485 SkCodecPrintf("Cannot convert to output type\n"); 540 SkCodecPrintf("Cannot convert to output type\n");
486 return NULL; 541 return NULL;
487 } 542 }
488 543
489 // Perform the necessary scaling 544 // Perform the necessary scaling
490 if (!codec->scaleToDimensions(dstInfo.width(), dstInfo.height())) { 545 if (!codec->scaleToDimensions(dstInfo.width(), dstInfo.height())) {
491 SkCodecPrintf("Cannot scale to output dimensions\n"); 546 // native scaling to dstInfo dimensions not supported
492 return NULL; 547
548 if (dstInfo.height() != this->getInfo().height()) {
549 // no non-native height sampling supported
550 return NULL;
551 }
552 // create swizzler for sampling
553 if (codec->initializeSwizzler(dstInfo, NULL, dstInfo.minRowBytes(), opti ons, ctable,
554 ctableCount) != kSuccess) {
555 SkCodecPrintf("failed to initialize the swizzler.\n");
556 return NULL;
557 }
493 } 558 }
494 559
495 // Now, given valid output dimensions, we can start the decompress 560 // Now, given valid output dimensions, we can start the decompress
496 if (!turbo_jpeg_start_decompress(codec->fDecoderMgr->dinfo())) { 561 if (!turbo_jpeg_start_decompress(codec->fDecoderMgr->dinfo())) {
497 SkCodecPrintf("start decompress failed\n"); 562 SkCodecPrintf("start decompress failed\n");
498 return NULL; 563 return NULL;
499 } 564 }
500 565
501 // Return the new scanline decoder 566 // Return the new scanline decoder
502 return SkNEW_ARGS(SkJpegScanlineDecoder, (dstInfo, codec.detach())); 567 return SkNEW_ARGS(SkJpegScanlineDecoder, (dstInfo, codec.detach()));
503 } 568 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698