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 15 matching lines...) Expand all Loading... |
26 // For more information on WebP image format, and libwebp library, see: | 26 // For more information on WebP image format, and libwebp library, see: |
27 // http://code.google.com/speed/webp/ | 27 // http://code.google.com/speed/webp/ |
28 // http://www.webmproject.org/code/#libwebp_webp_image_decoder_library | 28 // http://www.webmproject.org/code/#libwebp_webp_image_decoder_library |
29 // http://review.webmproject.org/gitweb?p=libwebp.git | 29 // http://review.webmproject.org/gitweb?p=libwebp.git |
30 | 30 |
31 #include <stdio.h> | 31 #include <stdio.h> |
32 extern "C" { | 32 extern "C" { |
33 // If moving libwebp out of skia source tree, path for webp headers must be | 33 // If moving libwebp out of skia source tree, path for webp headers must be |
34 // updated accordingly. Here, we enforce using local copy in webp sub-directory. | 34 // updated accordingly. Here, we enforce using local copy in webp sub-directory. |
35 #include "webp/decode.h" | 35 #include "webp/decode.h" |
| 36 #include "webp/demux.h" |
36 #include "webp/encode.h" | 37 #include "webp/encode.h" |
37 } | 38 } |
38 | 39 |
39 // this enables timing code to report milliseconds for a decode | 40 // this enables timing code to report milliseconds for a decode |
40 //#define TIME_DECODE | 41 //#define TIME_DECODE |
41 | 42 |
42 ////////////////////////////////////////////////////////////////////////// | 43 ////////////////////////////////////////////////////////////////////////// |
43 ////////////////////////////////////////////////////////////////////////// | 44 ////////////////////////////////////////////////////////////////////////// |
44 | 45 |
45 // Define VP8 I/O on top of Skia stream | 46 // Define VP8 I/O on top of Skia stream |
(...skipping 14 matching lines...) Expand all Loading... |
60 const size_t bytesRead = stream->read(dst, bytesToRead); | 61 const size_t bytesRead = stream->read(dst, bytesToRead); |
61 if (0 == bytesRead) { | 62 if (0 == bytesRead) { |
62 SkASSERT(stream->isAtEnd()); | 63 SkASSERT(stream->isAtEnd()); |
63 break; | 64 break; |
64 } | 65 } |
65 bytesToRead -= bytesRead; | 66 bytesToRead -= bytesRead; |
66 totalBytesRead += bytesRead; | 67 totalBytesRead += bytesRead; |
67 SkASSERT(bytesToRead + totalBytesRead == WEBP_VP8_HEADER_SIZE); | 68 SkASSERT(bytesToRead + totalBytesRead == WEBP_VP8_HEADER_SIZE); |
68 } while (!stream->isAtEnd() && bytesToRead > 0); | 69 } while (!stream->isAtEnd() && bytesToRead > 0); |
69 | 70 |
70 WebPBitstreamFeatures features; | 71 WebPData demuxData = { buffer, totalBytesRead }; |
71 VP8StatusCode status = WebPGetFeatures(buffer, totalBytesRead, &features); | 72 WebPDemuxState demuxState; |
72 if (VP8_STATUS_OK != status) { | 73 WebPDemuxer* demuxer = WebPDemuxPartial(&demuxData, &demuxState); |
73 return false; // Invalid WebP file. | 74 if (!demuxer) { |
| 75 return false; |
74 } | 76 } |
75 *width = features.width; | 77 |
76 *height = features.height; | 78 SkAutoTCallVProc<WebPDemuxer, WebPDemuxDelete> autoDeleter(demuxer); |
77 *alpha = features.has_alpha; | 79 switch (demuxState) { |
| 80 case WEBP_DEMUX_PARSE_ERROR: |
| 81 case WEBP_DEMUX_PARSING_HEADER: |
| 82 return false; |
| 83 default: |
| 84 // Header parsed. |
| 85 break; |
| 86 } |
| 87 |
| 88 *width = WebPDemuxGetI(demuxer, WEBP_FF_CANVAS_WIDTH); |
| 89 *height = WebPDemuxGetI(demuxer, WEBP_FF_CANVAS_HEIGHT); |
| 90 uint32_t format_flags = WebPDemuxGetI(demuxer, WEBP_FF_FORMAT_FLAGS); |
| 91 *alpha = SkToBool(format_flags & ALPHA_FLAG) ? 1 : 0; |
78 | 92 |
79 // sanity check for image size that's about to be decoded. | 93 // sanity check for image size that's about to be decoded. |
80 { | 94 { |
81 int64_t size = sk_64_mul(*width, *height); | 95 int64_t size = sk_64_mul(*width, *height); |
82 if (!sk_64_isS32(size)) { | 96 if (!sk_64_isS32(size)) { |
83 return false; | 97 return false; |
84 } | 98 } |
85 // now check that if we are 4-bytes per pixel, we also don't overflow | 99 // now check that if we are 4-bytes per pixel, we also don't overflow |
86 if (sk_64_asS32(size) > (0x7FFFFFFF >> 2)) { | 100 if (sk_64_asS32(size) > (0x7FFFFFFF >> 2)) { |
87 return false; | 101 return false; |
(...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
668 return SkImageDecoder::kUnknown_Format; | 682 return SkImageDecoder::kUnknown_Format; |
669 } | 683 } |
670 | 684 |
671 static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) { | 685 static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) { |
672 return (SkImageEncoder::kWEBP_Type == t) ? SkNEW(SkWEBPImageEncoder) : NUL
L; | 686 return (SkImageEncoder::kWEBP_Type == t) ? SkNEW(SkWEBPImageEncoder) : NUL
L; |
673 } | 687 } |
674 | 688 |
675 static SkImageDecoder_DecodeReg gDReg(sk_libwebp_dfactory); | 689 static SkImageDecoder_DecodeReg gDReg(sk_libwebp_dfactory); |
676 static SkImageDecoder_FormatReg gFormatReg(get_format_webp); | 690 static SkImageDecoder_FormatReg gFormatReg(get_format_webp); |
677 static SkImageEncoder_EncodeReg gEReg(sk_libwebp_efactory); | 691 static SkImageEncoder_EncodeReg gEReg(sk_libwebp_efactory); |
OLD | NEW |