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

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

Issue 1260673002: SkScaledCodec class (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Use SkTAbs, update comments 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"
11 #include "SkJpegUtility_codec.h" 11 #include "SkJpegUtility_codec.h"
12 #include "SkCodecPriv.h" 12 #include "SkCodecPriv.h"
13 #include "SkColorPriv.h" 13 #include "SkColorPriv.h"
14 #include "SkScaledCodec.h"
14 #include "SkScanlineDecoder.h" 15 #include "SkScanlineDecoder.h"
15 #include "SkStream.h" 16 #include "SkStream.h"
16 #include "SkTemplates.h" 17 #include "SkTemplates.h"
17 #include "SkTypes.h" 18 #include "SkTypes.h"
18 19
19 // stdio is needed for libjpeg-turbo 20 // stdio is needed for libjpeg-turbo
20 #include <stdio.h> 21 #include <stdio.h>
21 22
22 extern "C" { 23 extern "C" {
23 #include "jpeglibmangler.h" 24 #include "jpeglibmangler.h"
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 dinfo.global_state = DSTATE_READY; 183 dinfo.global_state = DSTATE_READY;
183 dinfo.num_components = 0; 184 dinfo.num_components = 0;
184 dinfo.scale_num = num; 185 dinfo.scale_num = num;
185 dinfo.scale_denom = denom; 186 dinfo.scale_denom = denom;
186 turbo_jpeg_calc_output_dimensions(&dinfo); 187 turbo_jpeg_calc_output_dimensions(&dinfo);
187 188
188 // Return the calculated output dimensions for the given scale 189 // Return the calculated output dimensions for the given scale
189 return SkISize::Make(dinfo.output_width, dinfo.output_height); 190 return SkISize::Make(dinfo.output_width, dinfo.output_height);
190 } 191 }
191 192
193 // The swizzler is only used for sampling in the x direction
194 // when sampling a scanlineDecoder is used. The swizzler is not used in onGetPix els
195 SkCodec::Result SkJpegCodec::initializeSwizzler(const SkImageInfo& requestedInfo ,
196 void* dst, size_t rowBytes,
197 const Options& options,
198 SkPMColor ctable[],
199 int* ctableCount) {
200
201 const SkColorType srcColorType = requestedInfo.colorType();
202 SkSwizzler::SrcConfig srcConfig;
203 switch (srcColorType) {
204 case kGray_8_SkColorType:
205 srcConfig = SkSwizzler::kGray;
206 break;
207 case kRGBA_8888_SkColorType:
208 srcConfig = SkSwizzler::kRGBX;
209 break;
210 case kBGRA_8888_SkColorType:
211 srcConfig = SkSwizzler::kBGRX;
212 break;
213 case kRGB_565_SkColorType:
214 srcConfig = SkSwizzler::kRGB_565;
215 break;
216 default:
217 //would have exited before now if the colorType was supported by jpe g
218 SkASSERT(false);
219 }
220
221 fSwizzler.reset(SkSwizzler::CreateSwizzler(srcConfig, NULL, requestedInfo,
222 options.fZeroInitialized, this->g etInfo().width()));
223 if (!fSwizzler) {
224 // FIXME: CreateSwizzler could fail for another reason.
225 return kUnimplemented;
226 }
227 return kSuccess;
228 }
229
192 /* 230 /*
193 * Handles rewinding the input stream if it is necessary 231 * Handles rewinding the input stream if it is necessary
194 */ 232 */
195 bool SkJpegCodec::handleRewind() { 233 bool SkJpegCodec::handleRewind() {
196 switch(this->rewindIfNeeded()) { 234 switch(this->rewindIfNeeded()) {
197 case kCouldNotRewind_RewindState: 235 case kCouldNotRewind_RewindState:
198 return fDecoderMgr->returnFalse("could not rewind"); 236 return fDecoderMgr->returnFalse("could not rewind");
199 case kRewound_RewindState: { 237 case kRewound_RewindState: {
200 JpegDecoderMgr* decoderMgr = NULL; 238 JpegDecoderMgr* decoderMgr = NULL;
201 if (!ReadHeader(this->stream(), NULL, &decoderMgr)) { 239 if (!ReadHeader(this->stream(), NULL, &decoderMgr)) {
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 // much faster than decoding to color and then converting 303 // much faster than decoding to color and then converting
266 fDecoderMgr->dinfo()->out_color_space = JCS_GRAYSCALE; 304 fDecoderMgr->dinfo()->out_color_space = JCS_GRAYSCALE;
267 } 305 }
268 return true; 306 return true;
269 default: 307 default:
270 return false; 308 return false;
271 } 309 }
272 } 310 }
273 311
274 /* 312 /*
275 * Checks if we can scale to the requested dimensions and scales the dimensions 313 * Checks if we can natively scale to the requested dimensions and natively scal es the
276 * if possible 314 * dimensions if possible
277 */ 315 */
278 bool SkJpegCodec::scaleToDimensions(uint32_t dstWidth, uint32_t dstHeight) { 316 bool SkJpegCodec::nativelyScaleToDimensions(uint32_t dstWidth, uint32_t dstHeigh t) {
279 // libjpeg-turbo can scale to 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, and 1/1 317 // libjpeg-turbo can scale to 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, and 1/1
280 fDecoderMgr->dinfo()->scale_denom = 8; 318 fDecoderMgr->dinfo()->scale_denom = 8;
281 fDecoderMgr->dinfo()->scale_num = 8; 319 fDecoderMgr->dinfo()->scale_num = 8;
282 turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); 320 turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo());
283 while (fDecoderMgr->dinfo()->output_width != dstWidth || 321 while (fDecoderMgr->dinfo()->output_width != dstWidth ||
284 fDecoderMgr->dinfo()->output_height != dstHeight) { 322 fDecoderMgr->dinfo()->output_height != dstHeight) {
285 323
286 // Return a failure if we have tried all of the possible scales 324 // Return a failure if we have tried all of the possible scales
287 if (1 == fDecoderMgr->dinfo()->scale_num || 325 if (1 == fDecoderMgr->dinfo()->scale_num ||
288 dstWidth > fDecoderMgr->dinfo()->output_width || 326 dstWidth > fDecoderMgr->dinfo()->output_width ||
289 dstHeight > fDecoderMgr->dinfo()->output_height) { 327 dstHeight > fDecoderMgr->dinfo()->output_height) {
290 return fDecoderMgr->returnFalse("could not scale to requested dimens ions"); 328 // reset native scale settings on failure because this may be suppor ted by the swizzler
329 this->fDecoderMgr->dinfo()->scale_num = 8;
330 turbo_jpeg_calc_output_dimensions(this->fDecoderMgr->dinfo());
331 return false;
291 } 332 }
292 333
293 // Try the next scale 334 // Try the next scale
294 fDecoderMgr->dinfo()->scale_num -= 1; 335 fDecoderMgr->dinfo()->scale_num -= 1;
295 turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); 336 turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo());
296 } 337 }
297 return true; 338 return true;
298 } 339 }
299 340
300 /* 341 /*
(...skipping 19 matching lines...) Expand all
320 if (setjmp(fDecoderMgr->getJmpBuf())) { 361 if (setjmp(fDecoderMgr->getJmpBuf())) {
321 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); 362 return fDecoderMgr->returnFailure("setjmp", kInvalidInput);
322 } 363 }
323 364
324 // Check if we can decode to the requested destination and set the output co lor space 365 // Check if we can decode to the requested destination and set the output co lor space
325 if (!this->setOutputColorSpace(dstInfo)) { 366 if (!this->setOutputColorSpace(dstInfo)) {
326 return fDecoderMgr->returnFailure("conversion_possible", kInvalidConvers ion); 367 return fDecoderMgr->returnFailure("conversion_possible", kInvalidConvers ion);
327 } 368 }
328 369
329 // Perform the necessary scaling 370 // Perform the necessary scaling
330 if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) { 371 if (!this->nativelyScaleToDimensions(dstInfo.width(), dstInfo.height())) {
331 return fDecoderMgr->returnFailure("cannot scale to requested dims", kInv alidScale); 372 return fDecoderMgr->returnFailure("cannot scale to requested dims", kInv alidScale);
332 } 373 }
333 374
334 // Now, given valid output dimensions, we can start the decompress 375 // Now, given valid output dimensions, we can start the decompress
335 if (!turbo_jpeg_start_decompress(dinfo)) { 376 if (!turbo_jpeg_start_decompress(dinfo)) {
336 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); 377 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput);
337 } 378 }
338 379
339 // The recommended output buffer height should always be 1 in high quality m odes. 380 // The recommended output buffer height should always be 1 in high quality m odes.
340 // If it's not, we want to know because it means our strategy is not optimal . 381 // If it's not, we want to know because it means our strategy is not optimal .
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
387 /* 428 /*
388 * Enable scanline decoding for jpegs 429 * Enable scanline decoding for jpegs
389 */ 430 */
390 class SkJpegScanlineDecoder : public SkScanlineDecoder { 431 class SkJpegScanlineDecoder : public SkScanlineDecoder {
391 public: 432 public:
392 SkJpegScanlineDecoder(const SkImageInfo& dstInfo, SkJpegCodec* codec, 433 SkJpegScanlineDecoder(const SkImageInfo& dstInfo, SkJpegCodec* codec,
393 const SkCodec::Options& opts) 434 const SkCodec::Options& opts)
394 : INHERITED(dstInfo) 435 : INHERITED(dstInfo)
395 , fCodec(codec) 436 , fCodec(codec)
396 , fOpts(opts) 437 , fOpts(opts)
397 {} 438 {
439 if(fCodec->fSwizzler) {
440 fStorage.reset(fCodec->getInfo().width() * dstInfo.minRowBytes());
scroggo 2015/08/03 19:16:20 This seems like more than we need to allocate. (Ma
emmaleer 2015/08/04 18:56:53 Acknowledged.
441 fSrcRow = static_cast<uint8_t*>(fStorage.get());
442 }
scroggo 2015/08/03 19:16:20 else { fSrcRow = NULL; }
emmaleer 2015/08/04 18:56:53 Acknowledged.
443 }
398 444
399 virtual ~SkJpegScanlineDecoder() { 445 virtual ~SkJpegScanlineDecoder() {
400 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { 446 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
401 SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n"); 447 SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n");
402 return; 448 return;
403 } 449 }
404 450
405 // We may not have decoded the entire image. Prevent libjpeg-turbo from failing on a 451 // We may not have decoded the entire image. Prevent libjpeg-turbo from failing on a
406 // partial decode. 452 // partial decode.
407 fCodec->fDecoderMgr->dinfo()->output_scanline = fCodec->getInfo().height (); 453 fCodec->fDecoderMgr->dinfo()->output_scanline = fCodec->getInfo().height ();
408 turbo_jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo()); 454 turbo_jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo());
409 } 455 }
410 456
411 SkCodec::Result onGetScanlines(void* dst, int count, size_t rowBytes) overri de { 457 SkCodec::Result onGetScanlines(void* dst, int count, size_t rowBytes) overri de {
412 // Set the jump location for libjpeg errors 458 // Set the jump location for libjpeg errors
413 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { 459 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
414 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvali dInput); 460 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvali dInput);
415 } 461 }
462 // Read rows one at a time
463 JSAMPLE* dstRow;
464 if (fCodec->fSwizzler) {
465 // write data to storage row, then sample using swizzler
466 dstRow = fSrcRow;
467 } else {
468 // write data directly to dst
469 dstRow = (JSAMPLE*) dst;
470 }
416 471
417 // Read rows one at a time
418 JSAMPLE* dstRow = (JSAMPLE*) dst;
419 for (int y = 0; y < count; y++) { 472 for (int y = 0; y < count; y++) {
420 // Read row of the image 473 // Read row of the image
421 uint32_t rowsDecoded = 474 uint32_t rowsDecoded =
422 turbo_jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &dst Row, 1); 475 turbo_jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &dst Row, 1);
423 if (rowsDecoded != 1) { 476 if (rowsDecoded != 1) {
424 if (SkCodec::kNo_ZeroInitialized == fOpts.fZeroInitialized || 477 if (SkCodec::kNo_ZeroInitialized == fOpts.fZeroInitialized ||
425 kN32_SkColorType == this->dstInfo().colorType()) { 478 kN32_SkColorType == this->dstInfo().colorType()) {
426 SkSwizzler::Fill(dstRow, this->dstInfo(), rowBytes, 479 SkSwizzler::Fill(dstRow, this->dstInfo(), rowBytes,
427 count - y, SK_ColorBLACK, NULL); 480 count - y, SK_ColorBLACK, NULL);
428 } 481 }
429 fCodec->fDecoderMgr->dinfo()->output_scanline = this->dstInfo(). height(); 482 fCodec->fDecoderMgr->dinfo()->output_scanline = this->dstInfo(). height();
430 return SkCodec::kIncompleteInput; 483 return SkCodec::kIncompleteInput;
431 } 484 }
432 485
433 // Convert to RGBA if necessary 486 // Convert to RGBA if necessary
434 if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) { 487 if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) {
435 convert_CMYK_to_RGBA(dstRow, this->dstInfo().width()); 488 convert_CMYK_to_RGBA(dstRow, this->dstInfo().width());
436 } 489 }
437 490
438 // Move to the next row 491 if(fCodec->fSwizzler) {
439 dstRow = SkTAddOffset<JSAMPLE>(dstRow, rowBytes); 492 // use swizzler to sample row
493 fCodec->fSwizzler->swizzle(dst, dstRow);
494 dst = SkTAddOffset<JSAMPLE>(dst, rowBytes);
495 } else {
496 dstRow = SkTAddOffset<JSAMPLE>(dstRow, rowBytes);
497 }
440 } 498 }
441
442 return SkCodec::kSuccess; 499 return SkCodec::kSuccess;
443 } 500 }
444 501
445 #ifndef TURBO_HAS_SKIP 502 #ifndef TURBO_HAS_SKIP
446 #define turbo_jpeg_skip_scanlines(dinfo, count) \ 503 #define turbo_jpeg_skip_scanlines(dinfo, count) \
447 SkAutoMalloc storage(dinfo->output_width * dinfo->out_color_components); \ 504 SkAutoMalloc storage(dinfo->output_width * dinfo->out_color_components); \
448 uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); \ 505 uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); \
449 for (int y = 0; y < count; y++) { \ 506 for (int y = 0; y < count; y++) { \
450 turbo_jpeg_read_scanlines(dinfo, &storagePtr, 1); \ 507 turbo_jpeg_read_scanlines(dinfo, &storagePtr, 1); \
451 } 508 }
452 #endif 509 #endif
453 510
454 SkCodec::Result onSkipScanlines(int count) override { 511 SkCodec::Result onSkipScanlines(int count) override {
455 // Set the jump location for libjpeg errors 512 // Set the jump location for libjpeg errors
456 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { 513 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
457 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvali dInput); 514 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvali dInput);
458 } 515 }
459 516
460 turbo_jpeg_skip_scanlines(fCodec->fDecoderMgr->dinfo(), count); 517 turbo_jpeg_skip_scanlines(fCodec->fDecoderMgr->dinfo(), count);
461 518
462 return SkCodec::kSuccess; 519 return SkCodec::kSuccess;
463 } 520 }
464 521
465 private: 522 private:
466 SkAutoTDelete<SkJpegCodec> fCodec; 523 SkAutoTDelete<SkJpegCodec> fCodec;
524 SkAutoMalloc fStorage; // Only used if sampling is needed
525 uint8_t* fSrcRow; // Only used if sampling is needed
467 const SkCodec::Options& fOpts; 526 const SkCodec::Options& fOpts;
468 527
469 typedef SkScanlineDecoder INHERITED; 528 typedef SkScanlineDecoder INHERITED;
470 }; 529 };
471 530
472 SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo, 531 SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo,
473 const Options& options, SkPMColor ctable[], int* ctableCount) { 532 const Options& options, SkPMColor ctable[], int* ctableCount) {
474 533
475 // Rewind the stream if needed 534 // Rewind the stream if needed
476 if (!this->handleRewind()) { 535 if (!this->handleRewind()) {
(...skipping 16 matching lines...) Expand all
493 return NULL; 552 return NULL;
494 } 553 }
495 554
496 // Check if we can decode to the requested destination and set the output co lor space 555 // Check if we can decode to the requested destination and set the output co lor space
497 if (!codec->setOutputColorSpace(dstInfo)) { 556 if (!codec->setOutputColorSpace(dstInfo)) {
498 SkCodecPrintf("Cannot convert to output type\n"); 557 SkCodecPrintf("Cannot convert to output type\n");
499 return NULL; 558 return NULL;
500 } 559 }
501 560
502 // Perform the necessary scaling 561 // Perform the necessary scaling
503 if (!codec->scaleToDimensions(dstInfo.width(), dstInfo.height())) { 562 if (!codec->nativelyScaleToDimensions(dstInfo.width(), dstInfo.height())) {
504 SkCodecPrintf("Cannot scale to output dimensions\n"); 563 // native scaling to dstInfo dimensions not supported
505 return NULL; 564
565 if (!SkScaledCodec::DimensionsSupportedForSampling(this->getInfo(), dstI nfo)) {
566 return NULL;
567 }
568 // create swizzler for sampling
569 if (codec->initializeSwizzler(dstInfo, NULL, dstInfo.minRowBytes(), opti ons, ctable,
570 ctableCount) != kSuccess) {
571 SkCodecPrintf("failed to initialize the swizzler.\n");
572 return NULL;
573 }
506 } 574 }
507 575
508 // Now, given valid output dimensions, we can start the decompress 576 // Now, given valid output dimensions, we can start the decompress
509 if (!turbo_jpeg_start_decompress(codec->fDecoderMgr->dinfo())) { 577 if (!turbo_jpeg_start_decompress(codec->fDecoderMgr->dinfo())) {
510 SkCodecPrintf("start decompress failed\n"); 578 SkCodecPrintf("start decompress failed\n");
511 return NULL; 579 return NULL;
512 } 580 }
513 581
514 // Return the new scanline decoder 582 // Return the new scanline decoder
515 return SkNEW_ARGS(SkJpegScanlineDecoder, (dstInfo, codec.detach(), options)) ; 583 return SkNEW_ARGS(SkJpegScanlineDecoder, (dstInfo, codec.detach(), options)) ;
516 } 584 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698