OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved. | 2 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved. |
3 * | 3 * |
4 * This library is free software; you can redistribute it and/or | 4 * This library is free software; you can redistribute it and/or |
5 * modify it under the terms of the GNU Library General Public | 5 * modify it under the terms of the GNU Library General Public |
6 * License as published by the Free Software Foundation; either | 6 * License as published by the Free Software Foundation; either |
7 * version 2 of the License, or (at your option) any later version. | 7 * version 2 of the License, or (at your option) any later version. |
8 * | 8 * |
9 * This library is distributed in the hope that it will be useful, | 9 * This library is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
56 } | 56 } |
57 | 57 |
58 inline bool MatchesCURSignature(const char* contents) { | 58 inline bool MatchesCURSignature(const char* contents) { |
59 return !memcmp(contents, "\x00\x00\x02\x00", 4); | 59 return !memcmp(contents, "\x00\x00\x02\x00", 4); |
60 } | 60 } |
61 | 61 |
62 inline bool MatchesBMPSignature(const char* contents) { | 62 inline bool MatchesBMPSignature(const char* contents) { |
63 return !memcmp(contents, "BM", 2); | 63 return !memcmp(contents, "BM", 2); |
64 } | 64 } |
65 | 65 |
66 // This needs to be updated if we ever add a Matches*Signature() which requires | 66 // This needs to be updated if we ever add a Matches*Signature() which requires |
Noel Gordon
2017/05/27 03:51:09
Remove this comment. It is a redundant statement
naga
2017/05/29 05:39:56
No need to remove this comment. We need to update
Noel Gordon
2017/05/29 05:43:51
Are you planning on adding a new image decoding si
| |
67 // more characters. | 67 // more characters. |
68 static constexpr size_t kLongestSignatureLength = sizeof("RIFF????WEBPVP") - 1; | 68 static constexpr size_t kLongestSignatureLength = sizeof("RIFF????WEBPVP") - 1; |
69 | 69 |
70 std::unique_ptr<ImageDecoder> ImageDecoder::Create( | 70 std::unique_ptr<ImageDecoder> ImageDecoder::Create( |
71 RefPtr<SegmentReader> data, | 71 RefPtr<SegmentReader> data, |
72 bool data_complete, | 72 bool data_complete, |
73 AlphaOption alpha_option, | 73 AlphaOption alpha_option, |
74 const ColorBehavior& color_behavior) { | 74 const ColorBehavior& color_behavior) { |
75 // We need at least kLongestSignatureLength bytes to run the signature | 75 // We need at least kLongestSignatureLength bytes to run the signature |
Noel Gordon
2017/05/27 03:51:09
// At least kLongestSignatureLength bytes are need
naga
2017/05/29 05:39:56
I will update the patch based on the above comment
| |
76 // matcher. | 76 // matcher. |
77 if (data->size() < kLongestSignatureLength) | 77 if (data->size() < kLongestSignatureLength) |
78 return nullptr; | 78 return nullptr; |
79 | 79 |
80 const size_t max_decoded_bytes = | 80 const size_t max_decoded_bytes = |
81 Platform::Current() ? Platform::Current()->MaxDecodedImageBytes() | 81 Platform::Current() ? Platform::Current()->MaxDecodedImageBytes() |
82 : kNoDecodedImageByteLimit; | 82 : kNoDecodedImageByteLimit; |
83 | 83 |
84 // Access the first kLongestSignatureLength chars to sniff the signature. | 84 // Access the first kLongestSignatureLength chars to sniff the signature. |
85 // (note: FastSharedBufferReader only makes a copy if the bytes are segmented) | 85 // (note: FastSharedBufferReader only makes a copy if the bytes are segmented) |
86 char buffer[kLongestSignatureLength]; | 86 char buffer[kLongestSignatureLength]; |
87 const FastSharedBufferReader fast_reader(data); | 87 const FastSharedBufferReader fast_reader(data); |
88 const ImageDecoder::SniffResult sniff_result = DetermineImageType( | 88 const char* contents = |
89 fast_reader.GetConsecutiveData(0, kLongestSignatureLength, buffer), | 89 fast_reader.GetConsecutiveData(0, kLongestSignatureLength, buffer); |
90 kLongestSignatureLength); | |
91 | 90 |
92 std::unique_ptr<ImageDecoder> decoder; | 91 std::unique_ptr<ImageDecoder> decoder; |
93 switch (sniff_result) { | 92 if (MatchesJPEGSignature(contents)) { |
94 case SniffResult::JPEG: | 93 decoder.reset( |
95 decoder.reset(new JPEGImageDecoder(alpha_option, color_behavior, | 94 new JPEGImageDecoder(alpha_option, color_behavior, max_decoded_bytes)); |
96 max_decoded_bytes)); | 95 } else if (MatchesPNGSignature(contents)) { |
97 break; | 96 decoder.reset( |
98 case SniffResult::PNG: | 97 new PNGImageDecoder(alpha_option, color_behavior, max_decoded_bytes)); |
99 decoder.reset( | 98 } else if (MatchesGIFSignature(contents)) { |
100 new PNGImageDecoder(alpha_option, color_behavior, max_decoded_bytes)); | 99 decoder.reset( |
101 break; | 100 new GIFImageDecoder(alpha_option, color_behavior, max_decoded_bytes)); |
102 case SniffResult::GIF: | 101 } else if (MatchesWebPSignature(contents)) { |
103 decoder.reset( | 102 decoder.reset( |
104 new GIFImageDecoder(alpha_option, color_behavior, max_decoded_bytes)); | 103 new WEBPImageDecoder(alpha_option, color_behavior, max_decoded_bytes)); |
105 break; | 104 } else if (MatchesICOSignature(contents) || MatchesCURSignature(contents)) { |
106 case SniffResult::WEBP: | 105 decoder.reset( |
107 decoder.reset(new WEBPImageDecoder(alpha_option, color_behavior, | 106 new ICOImageDecoder(alpha_option, color_behavior, max_decoded_bytes)); |
108 max_decoded_bytes)); | 107 } else if (MatchesBMPSignature(contents)) { |
109 break; | 108 decoder.reset( |
110 case SniffResult::ICO: | 109 new BMPImageDecoder(alpha_option, color_behavior, max_decoded_bytes)); |
111 decoder.reset( | |
112 new ICOImageDecoder(alpha_option, color_behavior, max_decoded_bytes)); | |
113 break; | |
114 case SniffResult::BMP: | |
115 decoder.reset( | |
116 new BMPImageDecoder(alpha_option, color_behavior, max_decoded_bytes)); | |
117 break; | |
118 case SniffResult::kInvalid: | |
119 break; | |
120 } | 110 } |
121 | 111 |
122 if (decoder) | 112 if (decoder) |
123 decoder->SetData(std::move(data), data_complete); | 113 decoder->SetData(std::move(data), data_complete); |
124 | 114 |
125 return decoder; | 115 return decoder; |
126 } | 116 } |
127 | 117 |
128 bool ImageDecoder::HasSufficientDataToSniffImageType(const SharedBuffer& data) { | 118 bool ImageDecoder::HasSufficientDataToSniffImageType(const SharedBuffer& data) { |
129 return data.size() >= kLongestSignatureLength; | 119 return data.size() >= kLongestSignatureLength; |
130 } | 120 } |
131 | 121 |
132 ImageDecoder::SniffResult ImageDecoder::DetermineImageType(const char* contents, | |
133 size_t length) { | |
134 DCHECK_GE(length, kLongestSignatureLength); | |
135 | |
136 if (MatchesJPEGSignature(contents)) | |
137 return SniffResult::JPEG; | |
138 if (MatchesPNGSignature(contents)) | |
139 return SniffResult::PNG; | |
140 if (MatchesGIFSignature(contents)) | |
141 return SniffResult::GIF; | |
142 if (MatchesWebPSignature(contents)) | |
143 return SniffResult::WEBP; | |
144 if (MatchesICOSignature(contents) || MatchesCURSignature(contents)) | |
145 return SniffResult::ICO; | |
146 if (MatchesBMPSignature(contents)) | |
147 return SniffResult::BMP; | |
148 return SniffResult::kInvalid; | |
149 } | |
150 | |
151 size_t ImageDecoder::FrameCount() { | 122 size_t ImageDecoder::FrameCount() { |
152 const size_t old_size = frame_buffer_cache_.size(); | 123 const size_t old_size = frame_buffer_cache_.size(); |
153 const size_t new_size = DecodeFrameCount(); | 124 const size_t new_size = DecodeFrameCount(); |
154 if (old_size != new_size) { | 125 if (old_size != new_size) { |
155 frame_buffer_cache_.resize(new_size); | 126 frame_buffer_cache_.resize(new_size); |
156 for (size_t i = old_size; i < new_size; ++i) { | 127 for (size_t i = old_size; i < new_size; ++i) { |
157 frame_buffer_cache_[i].SetPremultiplyAlpha(premultiply_alpha_); | 128 frame_buffer_cache_[i].SetPremultiplyAlpha(premultiply_alpha_); |
158 InitializeNewFrame(i); | 129 InitializeNewFrame(i); |
159 } | 130 } |
160 } | 131 } |
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
577 } | 548 } |
578 | 549 |
579 // For color spaces without an identifiable gamut, just fall through to | 550 // For color spaces without an identifiable gamut, just fall through to |
580 // sRGB. | 551 // sRGB. |
581 } | 552 } |
582 | 553 |
583 return SkColorSpace::MakeSRGB(); | 554 return SkColorSpace::MakeSRGB(); |
584 } | 555 } |
585 | 556 |
586 } // namespace blink | 557 } // namespace blink |
OLD | NEW |