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

Side by Side Diff: third_party/WebKit/Source/platform/image-decoders/ImageDecoder.cpp

Issue 2892403003: Simplify image decoder selection. (Closed)
Patch Set: y Created 3 years, 6 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
« no previous file with comments | « third_party/WebKit/Source/platform/image-decoders/ImageDecoder.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
67 // more characters.
68 static constexpr size_t kLongestSignatureLength = sizeof("RIFF????WEBPVP") - 1; 66 static constexpr size_t kLongestSignatureLength = sizeof("RIFF????WEBPVP") - 1;
69 67
70 std::unique_ptr<ImageDecoder> ImageDecoder::Create( 68 std::unique_ptr<ImageDecoder> ImageDecoder::Create(
71 RefPtr<SegmentReader> data, 69 RefPtr<SegmentReader> data,
72 bool data_complete, 70 bool data_complete,
73 AlphaOption alpha_option, 71 AlphaOption alpha_option,
74 const ColorBehavior& color_behavior) { 72 const ColorBehavior& color_behavior) {
75 // We need at least kLongestSignatureLength bytes to run the signature 73 // At least kLongestSignatureLength bytes are needed to sniff the signature.
76 // matcher.
77 if (data->size() < kLongestSignatureLength) 74 if (data->size() < kLongestSignatureLength)
78 return nullptr; 75 return nullptr;
79 76
80 const size_t max_decoded_bytes = 77 const size_t max_decoded_bytes =
81 Platform::Current() ? Platform::Current()->MaxDecodedImageBytes() 78 Platform::Current() ? Platform::Current()->MaxDecodedImageBytes()
82 : kNoDecodedImageByteLimit; 79 : kNoDecodedImageByteLimit;
83 80
84 // Access the first kLongestSignatureLength chars to sniff the signature. 81 // Access the first kLongestSignatureLength chars to sniff the signature.
85 // (note: FastSharedBufferReader only makes a copy if the bytes are segmented) 82 // (note: FastSharedBufferReader only makes a copy if the bytes are segmented)
86 char buffer[kLongestSignatureLength]; 83 char buffer[kLongestSignatureLength];
87 const FastSharedBufferReader fast_reader(data); 84 const FastSharedBufferReader fast_reader(data);
88 const ImageDecoder::SniffResult sniff_result = DetermineImageType( 85 const char* contents =
89 fast_reader.GetConsecutiveData(0, kLongestSignatureLength, buffer), 86 fast_reader.GetConsecutiveData(0, kLongestSignatureLength, buffer);
90 kLongestSignatureLength);
91 87
92 std::unique_ptr<ImageDecoder> decoder; 88 std::unique_ptr<ImageDecoder> decoder;
93 switch (sniff_result) { 89 if (MatchesJPEGSignature(contents)) {
94 case SniffResult::JPEG: 90 decoder.reset(
95 decoder.reset(new JPEGImageDecoder(alpha_option, color_behavior, 91 new JPEGImageDecoder(alpha_option, color_behavior, max_decoded_bytes));
96 max_decoded_bytes)); 92 } else if (MatchesPNGSignature(contents)) {
97 break; 93 decoder.reset(
98 case SniffResult::PNG: 94 new PNGImageDecoder(alpha_option, color_behavior, max_decoded_bytes));
99 decoder.reset( 95 } else if (MatchesGIFSignature(contents)) {
100 new PNGImageDecoder(alpha_option, color_behavior, max_decoded_bytes)); 96 decoder.reset(
101 break; 97 new GIFImageDecoder(alpha_option, color_behavior, max_decoded_bytes));
102 case SniffResult::GIF: 98 } else if (MatchesWebPSignature(contents)) {
103 decoder.reset( 99 decoder.reset(
104 new GIFImageDecoder(alpha_option, color_behavior, max_decoded_bytes)); 100 new WEBPImageDecoder(alpha_option, color_behavior, max_decoded_bytes));
105 break; 101 } else if (MatchesICOSignature(contents) || MatchesCURSignature(contents)) {
106 case SniffResult::WEBP: 102 decoder.reset(
107 decoder.reset(new WEBPImageDecoder(alpha_option, color_behavior, 103 new ICOImageDecoder(alpha_option, color_behavior, max_decoded_bytes));
108 max_decoded_bytes)); 104 } else if (MatchesBMPSignature(contents)) {
109 break; 105 decoder.reset(
110 case SniffResult::ICO: 106 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 } 107 }
121 108
122 if (decoder) 109 if (decoder)
123 decoder->SetData(std::move(data), data_complete); 110 decoder->SetData(std::move(data), data_complete);
124 111
125 return decoder; 112 return decoder;
126 } 113 }
127 114
128 bool ImageDecoder::HasSufficientDataToSniffImageType(const SharedBuffer& data) { 115 bool ImageDecoder::HasSufficientDataToSniffImageType(const SharedBuffer& data) {
129 return data.size() >= kLongestSignatureLength; 116 return data.size() >= kLongestSignatureLength;
130 } 117 }
131 118
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() { 119 size_t ImageDecoder::FrameCount() {
152 const size_t old_size = frame_buffer_cache_.size(); 120 const size_t old_size = frame_buffer_cache_.size();
153 const size_t new_size = DecodeFrameCount(); 121 const size_t new_size = DecodeFrameCount();
154 if (old_size != new_size) { 122 if (old_size != new_size) {
155 frame_buffer_cache_.resize(new_size); 123 frame_buffer_cache_.resize(new_size);
156 for (size_t i = old_size; i < new_size; ++i) { 124 for (size_t i = old_size; i < new_size; ++i) {
157 frame_buffer_cache_[i].SetPremultiplyAlpha(premultiply_alpha_); 125 frame_buffer_cache_[i].SetPremultiplyAlpha(premultiply_alpha_);
158 InitializeNewFrame(i); 126 InitializeNewFrame(i);
159 } 127 }
160 } 128 }
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after
577 } 545 }
578 546
579 // For color spaces without an identifiable gamut, just fall through to 547 // For color spaces without an identifiable gamut, just fall through to
580 // sRGB. 548 // sRGB.
581 } 549 }
582 550
583 return SkColorSpace::MakeSRGB(); 551 return SkColorSpace::MakeSRGB();
584 } 552 }
585 553
586 } // namespace blink 554 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/image-decoders/ImageDecoder.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698