OLD | NEW |
| (Empty) |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef IOS_WEB_PUBLIC_IMAGE_FETCHER_WEBP_DECODER_H_ | |
6 #define IOS_WEB_PUBLIC_IMAGE_FETCHER_WEBP_DECODER_H_ | |
7 | |
8 #import <Foundation/Foundation.h> | |
9 #include <stddef.h> | |
10 | |
11 #include <memory> | |
12 | |
13 #import "base/mac/scoped_nsobject.h" | |
14 #include "base/memory/ref_counted.h" | |
15 #include "third_party/libwebp/webp/decode.h" | |
16 | |
17 @class NSData; | |
18 | |
19 namespace webp_transcode { | |
20 | |
21 // Decodes a WebP image into either JPEG, PNG or uncompressed TIFF. | |
22 class WebpDecoder : public base::RefCountedThreadSafe<WebpDecoder> { | |
23 public: | |
24 // Format of the decoded image. | |
25 // This enum is used for UMA reporting, keep it in sync with the histogram | |
26 // definition. | |
27 enum DecodedImageFormat { JPEG = 1, PNG, TIFF, DECODED_FORMAT_COUNT }; | |
28 | |
29 class Delegate : public base::RefCountedThreadSafe<WebpDecoder::Delegate> { | |
30 public: | |
31 virtual void OnFinishedDecoding(bool success) = 0; | |
32 virtual void SetImageFeatures(size_t total_size, // In bytes. | |
33 DecodedImageFormat format) = 0; | |
34 virtual void OnDataDecoded(NSData* data) = 0; | |
35 | |
36 protected: | |
37 friend class base::RefCountedThreadSafe<WebpDecoder::Delegate>; | |
38 virtual ~Delegate() {} | |
39 }; | |
40 | |
41 explicit WebpDecoder(WebpDecoder::Delegate* delegate); | |
42 | |
43 // Returns an NSData object containing the decoded image data of the given | |
44 // webp_image. Returns nil in case of failure. | |
45 static NSData* DecodeWebpImage(NSData* webp_image); | |
46 | |
47 // Returns true if the given image_data is a WebP image. | |
48 // | |
49 // Every WebP file contains a 12 byte file header in the beginning of the | |
50 // file. | |
51 // A WebP file header starts with the four ASCII characters "RIFF". The next | |
52 // four bytes contain the image size and the last four header bytes contain | |
53 // the four ASCII characters "WEBP". | |
54 // | |
55 // WebP file header: | |
56 // 1 1 | |
57 // Byte Nr. 0 1 2 3 4 5 6 7 8 9 0 1 | |
58 // Byte value [ R I F F ? ? ? ? W E B P ] | |
59 // | |
60 // For more information see: | |
61 // https://developers.google.com/speed/webp/docs/riff_container#webp_file_head
er | |
62 static bool IsWebpImage(const std::string& image_data); | |
63 | |
64 // For tests. | |
65 static size_t GetHeaderSize(); | |
66 | |
67 // Main entry point. | |
68 void OnDataReceived(NSData* data); | |
69 | |
70 // Stops the decoding. | |
71 void Stop(); | |
72 | |
73 private: | |
74 struct WebPIDecoderDeleter { | |
75 inline void operator()(WebPIDecoder* ptr) const { WebPIDelete(ptr); } | |
76 }; | |
77 | |
78 enum State { READING_FEATURES, READING_DATA, DONE }; | |
79 | |
80 friend class base::RefCountedThreadSafe<WebpDecoder>; | |
81 virtual ~WebpDecoder(); | |
82 | |
83 // Implements WebP image decoding state machine steps. | |
84 void DoReadFeatures(NSData* data); | |
85 void DoReadData(NSData* data); | |
86 bool DoSendData(); | |
87 | |
88 scoped_refptr<WebpDecoder::Delegate> delegate_; | |
89 WebPDecoderConfig config_; | |
90 WebpDecoder::State state_; | |
91 std::unique_ptr<WebPIDecoder, WebPIDecoderDeleter> incremental_decoder_; | |
92 base::scoped_nsobject<NSData> output_buffer_; | |
93 base::scoped_nsobject<NSMutableData> features_; | |
94 int has_alpha_; | |
95 }; | |
96 | |
97 } // namespace webp_transcode | |
98 | |
99 #endif // IOS_WEB_PUBLIC_IMAGE_FETCHER_WEBP_DECODER_H_ | |
OLD | NEW |