| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2010, The Android Open Source Project | 2 * Copyright 2010, The Android Open Source Project |
| 3 * | 3 * |
| 4 * Licensed under the Apache License, Version 2.0 (the "License"); | 4 * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 * you may not use this file except in compliance with the License. | 5 * you may not use this file except in compliance with the License. |
| 6 * You may obtain a copy of the License at | 6 * You may obtain a copy of the License at |
| 7 * | 7 * |
| 8 * http://www.apache.org/licenses/LICENSE-2.0 | 8 * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 * | 9 * |
| 10 * Unless required by applicable law or agreed to in writing, software | 10 * Unless required by applicable law or agreed to in writing, software |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 ////////////////////////////////////////////////////////////////////////// | 130 ////////////////////////////////////////////////////////////////////////// |
| 131 | 131 |
| 132 #ifdef TIME_DECODE | 132 #ifdef TIME_DECODE |
| 133 | 133 |
| 134 #include "SkTime.h" | 134 #include "SkTime.h" |
| 135 | 135 |
| 136 class AutoTimeMillis { | 136 class AutoTimeMillis { |
| 137 public: | 137 public: |
| 138 AutoTimeMillis(const char label[]) : | 138 AutoTimeMillis(const char label[]) : |
| 139 fLabel(label) { | 139 fLabel(label) { |
| 140 if (NULL == fLabel) { | 140 if (nullptr == fLabel) { |
| 141 fLabel = ""; | 141 fLabel = ""; |
| 142 } | 142 } |
| 143 fNow = SkTime::GetMSecs(); | 143 fNow = SkTime::GetMSecs(); |
| 144 } | 144 } |
| 145 ~AutoTimeMillis() { | 145 ~AutoTimeMillis() { |
| 146 SkDebugf("---- Time (ms): %s %d\n", fLabel, SkTime::GetMSecs() - fNow); | 146 SkDebugf("---- Time (ms): %s %d\n", fLabel, SkTime::GetMSecs() - fNow); |
| 147 } | 147 } |
| 148 private: | 148 private: |
| 149 const char* fLabel; | 149 const char* fLabel; |
| 150 SkMSec fNow; | 150 SkMSec fNow; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 } else if (ct == kRGB_565_SkColorType) { | 189 } else if (ct == kRGB_565_SkColorType) { |
| 190 mode = MODE_RGB_565; | 190 mode = MODE_RGB_565; |
| 191 } | 191 } |
| 192 SkASSERT(MODE_LAST != mode); | 192 SkASSERT(MODE_LAST != mode); |
| 193 return mode; | 193 return mode; |
| 194 } | 194 } |
| 195 | 195 |
| 196 // Incremental WebP image decoding. Reads input buffer of 64K size iteratively | 196 // Incremental WebP image decoding. Reads input buffer of 64K size iteratively |
| 197 // and decodes this block to appropriate color-space as per config object. | 197 // and decodes this block to appropriate color-space as per config object. |
| 198 static bool webp_idecode(SkStream* stream, WebPDecoderConfig* config) { | 198 static bool webp_idecode(SkStream* stream, WebPDecoderConfig* config) { |
| 199 WebPIDecoder* idec = WebPIDecode(NULL, 0, config); | 199 WebPIDecoder* idec = WebPIDecode(nullptr, 0, config); |
| 200 if (NULL == idec) { | 200 if (nullptr == idec) { |
| 201 WebPFreeDecBuffer(&config->output); | 201 WebPFreeDecBuffer(&config->output); |
| 202 return false; | 202 return false; |
| 203 } | 203 } |
| 204 | 204 |
| 205 if (!stream->rewind()) { | 205 if (!stream->rewind()) { |
| 206 SkDebugf("Failed to rewind webp stream!"); | 206 SkDebugf("Failed to rewind webp stream!"); |
| 207 return false; | 207 return false; |
| 208 } | 208 } |
| 209 const size_t readBufferSize = stream->hasLength() ? | 209 const size_t readBufferSize = stream->hasLength() ? |
| 210 SkTMin(stream->getLength(), WEBP_IDECODE_BUFFER_SZ) : WEBP_IDECODE_B
UFFER_SZ; | 210 SkTMin(stream->getLength(), WEBP_IDECODE_BUFFER_SZ) : WEBP_IDECODE_B
UFFER_SZ; |
| 211 SkAutoMalloc srcStorage(readBufferSize); | 211 SkAutoMalloc srcStorage(readBufferSize); |
| 212 unsigned char* input = (uint8_t*)srcStorage.get(); | 212 unsigned char* input = (uint8_t*)srcStorage.get(); |
| 213 if (NULL == input) { | 213 if (nullptr == input) { |
| 214 WebPIDelete(idec); | 214 WebPIDelete(idec); |
| 215 WebPFreeDecBuffer(&config->output); | 215 WebPFreeDecBuffer(&config->output); |
| 216 return false; | 216 return false; |
| 217 } | 217 } |
| 218 | 218 |
| 219 bool success = true; | 219 bool success = true; |
| 220 VP8StatusCode status = VP8_STATUS_SUSPENDED; | 220 VP8StatusCode status = VP8_STATUS_SUSPENDED; |
| 221 do { | 221 do { |
| 222 const size_t bytesRead = stream->read(input, readBufferSize); | 222 const size_t bytesRead = stream->read(input, readBufferSize); |
| 223 if (0 == bytesRead) { | 223 if (0 == bytesRead) { |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 if (!directDecode) { | 367 if (!directDecode) { |
| 368 bitmap = &tmpBitmap; | 368 bitmap = &tmpBitmap; |
| 369 } | 369 } |
| 370 | 370 |
| 371 if (bitmap->isNull()) { | 371 if (bitmap->isNull()) { |
| 372 if (!setDecodeConfig(bitmap, width, height)) { | 372 if (!setDecodeConfig(bitmap, width, height)) { |
| 373 return false; | 373 return false; |
| 374 } | 374 } |
| 375 // alloc from native heap if it is a temp bitmap. (prevent GC) | 375 // alloc from native heap if it is a temp bitmap. (prevent GC) |
| 376 bool allocResult = (bitmap == decodedBitmap) | 376 bool allocResult = (bitmap == decodedBitmap) |
| 377 ? allocPixelRef(bitmap, NULL) | 377 ? allocPixelRef(bitmap, nullptr) |
| 378 : bitmap->tryAllocPixels(); | 378 : bitmap->tryAllocPixels(); |
| 379 if (!allocResult) { | 379 if (!allocResult) { |
| 380 return return_false(*decodedBitmap, "allocPixelRef"); | 380 return return_false(*decodedBitmap, "allocPixelRef"); |
| 381 } | 381 } |
| 382 } | 382 } |
| 383 | 383 |
| 384 SkAutoLockPixels alp(*bitmap); | 384 SkAutoLockPixels alp(*bitmap); |
| 385 WebPDecoderConfig config; | 385 WebPDecoderConfig config; |
| 386 if (!webp_get_config_resize_crop(&config, bitmap, rect, | 386 if (!webp_get_config_resize_crop(&config, bitmap, rect, |
| 387 this->shouldPremultiply())) { | 387 this->shouldPremultiply())) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 418 if (!setDecodeConfig(decodedBitmap, sampler.scaledWidth(), | 418 if (!setDecodeConfig(decodedBitmap, sampler.scaledWidth(), |
| 419 sampler.scaledHeight())) { | 419 sampler.scaledHeight())) { |
| 420 return kFailure; | 420 return kFailure; |
| 421 } | 421 } |
| 422 | 422 |
| 423 // If only bounds are requested, done | 423 // If only bounds are requested, done |
| 424 if (SkImageDecoder::kDecodeBounds_Mode == mode) { | 424 if (SkImageDecoder::kDecodeBounds_Mode == mode) { |
| 425 return kSuccess; | 425 return kSuccess; |
| 426 } | 426 } |
| 427 | 427 |
| 428 if (!this->allocPixelRef(decodedBitmap, NULL)) { | 428 if (!this->allocPixelRef(decodedBitmap, nullptr)) { |
| 429 return return_failure(*decodedBitmap, "allocPixelRef"); | 429 return return_failure(*decodedBitmap, "allocPixelRef"); |
| 430 } | 430 } |
| 431 | 431 |
| 432 SkAutoLockPixels alp(*decodedBitmap); | 432 SkAutoLockPixels alp(*decodedBitmap); |
| 433 | 433 |
| 434 WebPDecoderConfig config; | 434 WebPDecoderConfig config; |
| 435 if (!webp_get_config_resize(&config, decodedBitmap, origWidth, origHeight, | 435 if (!webp_get_config_resize(&config, decodedBitmap, origWidth, origHeight, |
| 436 this->shouldPremultiply())) { | 436 this->shouldPremultiply())) { |
| 437 return kFailure; | 437 return kFailure; |
| 438 } | 438 } |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 564 *bpp = 3; | 564 *bpp = 3; |
| 565 return ARGB_4444_To_RGB; | 565 return ARGB_4444_To_RGB; |
| 566 } | 566 } |
| 567 case kRGB_565_SkColorType: | 567 case kRGB_565_SkColorType: |
| 568 *bpp = 3; | 568 *bpp = 3; |
| 569 return RGB_565_To_RGB; | 569 return RGB_565_To_RGB; |
| 570 case kIndex_8_SkColorType: | 570 case kIndex_8_SkColorType: |
| 571 *bpp = 3; | 571 *bpp = 3; |
| 572 return Index8_To_RGB; | 572 return Index8_To_RGB; |
| 573 default: | 573 default: |
| 574 return NULL; | 574 return nullptr; |
| 575 } | 575 } |
| 576 } | 576 } |
| 577 | 577 |
| 578 static int stream_writer(const uint8_t* data, size_t data_size, | 578 static int stream_writer(const uint8_t* data, size_t data_size, |
| 579 const WebPPicture* const picture) { | 579 const WebPPicture* const picture) { |
| 580 SkWStream* const stream = (SkWStream*)picture->custom_ptr; | 580 SkWStream* const stream = (SkWStream*)picture->custom_ptr; |
| 581 return stream->write(data, data_size) ? 1 : 0; | 581 return stream->write(data, data_size) ? 1 : 0; |
| 582 } | 582 } |
| 583 | 583 |
| 584 class SkWEBPImageEncoder : public SkImageEncoder { | 584 class SkWEBPImageEncoder : public SkImageEncoder { |
| 585 protected: | 585 protected: |
| 586 bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) override; | 586 bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) override; |
| 587 | 587 |
| 588 private: | 588 private: |
| 589 typedef SkImageEncoder INHERITED; | 589 typedef SkImageEncoder INHERITED; |
| 590 }; | 590 }; |
| 591 | 591 |
| 592 bool SkWEBPImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bm, | 592 bool SkWEBPImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bm, |
| 593 int quality) { | 593 int quality) { |
| 594 const bool hasAlpha = !bm.isOpaque(); | 594 const bool hasAlpha = !bm.isOpaque(); |
| 595 int bpp = -1; | 595 int bpp = -1; |
| 596 const ScanlineImporter scanline_import = ChooseImporter(bm.colorType(), hasA
lpha, &bpp); | 596 const ScanlineImporter scanline_import = ChooseImporter(bm.colorType(), hasA
lpha, &bpp); |
| 597 if (NULL == scanline_import) { | 597 if (nullptr == scanline_import) { |
| 598 return false; | 598 return false; |
| 599 } | 599 } |
| 600 if (-1 == bpp) { | 600 if (-1 == bpp) { |
| 601 return false; | 601 return false; |
| 602 } | 602 } |
| 603 | 603 |
| 604 SkAutoLockPixels alp(bm); | 604 SkAutoLockPixels alp(bm); |
| 605 if (NULL == bm.getPixels()) { | 605 if (nullptr == bm.getPixels()) { |
| 606 return false; | 606 return false; |
| 607 } | 607 } |
| 608 | 608 |
| 609 WebPConfig webp_config; | 609 WebPConfig webp_config; |
| 610 if (!WebPConfigPreset(&webp_config, WEBP_PRESET_DEFAULT, (float) quality)) { | 610 if (!WebPConfigPreset(&webp_config, WEBP_PRESET_DEFAULT, (float) quality)) { |
| 611 return false; | 611 return false; |
| 612 } | 612 } |
| 613 | 613 |
| 614 WebPPicture pic; | 614 WebPPicture pic; |
| 615 WebPPictureInit(&pic); | 615 WebPPictureInit(&pic); |
| 616 pic.width = bm.width(); | 616 pic.width = bm.width(); |
| 617 pic.height = bm.height(); | 617 pic.height = bm.height(); |
| 618 pic.writer = stream_writer; | 618 pic.writer = stream_writer; |
| 619 pic.custom_ptr = (void*)stream; | 619 pic.custom_ptr = (void*)stream; |
| 620 | 620 |
| 621 const SkPMColor* colors = bm.getColorTable() ? bm.getColorTable()->readColor
s() : NULL; | 621 const SkPMColor* colors = bm.getColorTable() ? bm.getColorTable()->readColor
s() : nullptr; |
| 622 const uint8_t* src = (uint8_t*)bm.getPixels(); | 622 const uint8_t* src = (uint8_t*)bm.getPixels(); |
| 623 const int rgbStride = pic.width * bpp; | 623 const int rgbStride = pic.width * bpp; |
| 624 | 624 |
| 625 // Import (for each scanline) the bit-map image (in appropriate color-space) | 625 // Import (for each scanline) the bit-map image (in appropriate color-space) |
| 626 // to RGB color space. | 626 // to RGB color space. |
| 627 uint8_t* rgb = new uint8_t[rgbStride * pic.height]; | 627 uint8_t* rgb = new uint8_t[rgbStride * pic.height]; |
| 628 for (int y = 0; y < pic.height; ++y) { | 628 for (int y = 0; y < pic.height; ++y) { |
| 629 scanline_import(src + y * bm.rowBytes(), rgb + y * rgbStride, | 629 scanline_import(src + y * bm.rowBytes(), rgb + y * rgbStride, |
| 630 pic.width, colors); | 630 pic.width, colors); |
| 631 } | 631 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 646 | 646 |
| 647 | 647 |
| 648 /////////////////////////////////////////////////////////////////////////////// | 648 /////////////////////////////////////////////////////////////////////////////// |
| 649 DEFINE_DECODER_CREATOR(WEBPImageDecoder); | 649 DEFINE_DECODER_CREATOR(WEBPImageDecoder); |
| 650 DEFINE_ENCODER_CREATOR(WEBPImageEncoder); | 650 DEFINE_ENCODER_CREATOR(WEBPImageEncoder); |
| 651 /////////////////////////////////////////////////////////////////////////////// | 651 /////////////////////////////////////////////////////////////////////////////// |
| 652 | 652 |
| 653 static SkImageDecoder* sk_libwebp_dfactory(SkStreamRewindable* stream) { | 653 static SkImageDecoder* sk_libwebp_dfactory(SkStreamRewindable* stream) { |
| 654 int width, height, hasAlpha; | 654 int width, height, hasAlpha; |
| 655 if (!webp_parse_header(stream, &width, &height, &hasAlpha)) { | 655 if (!webp_parse_header(stream, &width, &height, &hasAlpha)) { |
| 656 return NULL; | 656 return nullptr; |
| 657 } | 657 } |
| 658 | 658 |
| 659 // Magic matches, call decoder | 659 // Magic matches, call decoder |
| 660 return new SkWEBPImageDecoder; | 660 return new SkWEBPImageDecoder; |
| 661 } | 661 } |
| 662 | 662 |
| 663 static SkImageDecoder::Format get_format_webp(SkStreamRewindable* stream) { | 663 static SkImageDecoder::Format get_format_webp(SkStreamRewindable* stream) { |
| 664 int width, height, hasAlpha; | 664 int width, height, hasAlpha; |
| 665 if (webp_parse_header(stream, &width, &height, &hasAlpha)) { | 665 if (webp_parse_header(stream, &width, &height, &hasAlpha)) { |
| 666 return SkImageDecoder::kWEBP_Format; | 666 return SkImageDecoder::kWEBP_Format; |
| 667 } | 667 } |
| 668 return SkImageDecoder::kUnknown_Format; | 668 return SkImageDecoder::kUnknown_Format; |
| 669 } | 669 } |
| 670 | 670 |
| 671 static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) { | 671 static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) { |
| 672 return (SkImageEncoder::kWEBP_Type == t) ? new SkWEBPImageEncoder : NULL; | 672 return (SkImageEncoder::kWEBP_Type == t) ? new SkWEBPImageEncoder : nullptr; |
| 673 } | 673 } |
| 674 | 674 |
| 675 static SkImageDecoder_DecodeReg gDReg(sk_libwebp_dfactory); | 675 static SkImageDecoder_DecodeReg gDReg(sk_libwebp_dfactory); |
| 676 static SkImageDecoder_FormatReg gFormatReg(get_format_webp); | 676 static SkImageDecoder_FormatReg gFormatReg(get_format_webp); |
| 677 static SkImageEncoder_EncodeReg gEReg(sk_libwebp_efactory); | 677 static SkImageEncoder_EncodeReg gEReg(sk_libwebp_efactory); |
| OLD | NEW |