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

Unified Diff: ios/web/public/image_fetcher/webp_decoder.mm

Issue 2689213010: Add a static method to WebPDecoder to decode WebP (Closed)
Patch Set: Address comment Created 3 years, 10 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ios/web/public/image_fetcher/webp_decoder.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ios/web/public/image_fetcher/webp_decoder.mm
diff --git a/ios/web/public/image_fetcher/webp_decoder.mm b/ios/web/public/image_fetcher/webp_decoder.mm
index 952882266753e0e88d7d9265f051dd051d444f44..d5e7d923add86c93ae77a8ed9a6417e2673f0639 100644
--- a/ios/web/public/image_fetcher/webp_decoder.mm
+++ b/ios/web/public/image_fetcher/webp_decoder.mm
@@ -17,6 +17,40 @@
namespace {
+class WebpDecoderDelegate : public webp_transcode::WebpDecoder::Delegate {
+ public:
+ WebpDecoderDelegate() = default;
+
+ NSData* data() const { return decoded_image_; }
+
+ // WebpDecoder::Delegate methods
+ void OnFinishedDecoding(bool success) override {
+ if (!success)
+ decoded_image_ = nil;
+ }
+
+ void SetImageFeatures(
+ size_t total_size,
+ webp_transcode::WebpDecoder::DecodedImageFormat format) override {
+ decoded_image_ = [[NSMutableData alloc] initWithCapacity:total_size];
+ }
+
+ void OnDataDecoded(NSData* data) override {
+ DCHECK(decoded_image_);
+ [decoded_image_ appendData:data];
+ }
+
+ private:
+ ~WebpDecoderDelegate() override {}
+ NSMutableData* decoded_image_;
+
+ DISALLOW_COPY_AND_ASSIGN(WebpDecoderDelegate);
+};
+
+// Content-type header for WebP images.
+const char kWEBPFirstMagicPattern[] = "RIFF";
+const char kWEBPSecondMagicPattern[] = "WEBP";
+
const uint8_t kNumIfdEntries = 15;
const unsigned int kExtraDataSize = 16;
// 10b for signature/header + n * 12b entries + 4b for IFD terminator:
@@ -91,6 +125,26 @@ void WriteTiffHeader(uint8_t* dst,
namespace webp_transcode {
// static
+NSData* WebpDecoder::DecodeWebpImage(NSData* webp_image) {
+ scoped_refptr<WebpDecoderDelegate> delegate(new WebpDecoderDelegate);
+
+ scoped_refptr<webp_transcode::WebpDecoder> decoder(
+ new webp_transcode::WebpDecoder(delegate.get()));
+
+ decoder->OnDataReceived(webp_image);
+ DLOG_IF(ERROR, !delegate->data()) << "WebP image decoding failed.";
+ return delegate->data();
+}
+
+// static
+bool WebpDecoder::IsWebpImage(const std::string& image_data) {
+ if (image_data.length() < 12)
+ return false;
+ return image_data.compare(0, 4, kWEBPFirstMagicPattern) == 0 &&
+ image_data.compare(8, 4, kWEBPSecondMagicPattern) == 0;
+}
+
+// static
size_t WebpDecoder::GetHeaderSize() {
return kHeaderSize;
}
« no previous file with comments | « ios/web/public/image_fetcher/webp_decoder.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698