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.
5 #ifndef COMPONENTS_WEBP_TRANSCODE_WEBP_DECODER_H_
6 #define COMPONENTS_WEBP_TRANSCODE_WEBP_DECODER_H_
8 #import <Foundation/Foundation.h>
10 #include "base/mac/scoped_nsobject.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "third_party/libwebp/webp/decode.h"
17 namespace webp_transcode
{
19 // Decodes a WebP image into either JPEG, PNG or uncompressed TIFF.
20 class WebpDecoder
: public base::RefCountedThreadSafe
<WebpDecoder
> {
22 // Format of the decoded image.
23 // This enum is used for UMA reporting, keep it in sync with the histogram
25 enum DecodedImageFormat
{ JPEG
= 1, PNG
, TIFF
, DECODED_FORMAT_COUNT
};
27 class Delegate
: public base::RefCountedThreadSafe
<WebpDecoder::Delegate
> {
29 virtual void OnFinishedDecoding(bool success
) = 0;
30 virtual void SetImageFeatures(size_t total_size
, // In bytes.
31 DecodedImageFormat format
) = 0;
32 virtual void OnDataDecoded(NSData
* data
) = 0;
35 friend class base::RefCountedThreadSafe
<WebpDecoder::Delegate
>;
36 virtual ~Delegate() {}
39 explicit WebpDecoder(WebpDecoder::Delegate
* delegate
);
42 static size_t GetHeaderSize();
45 void OnDataReceived(const base::scoped_nsobject
<NSData
>& data
);
47 // Stops the decoding.
51 struct WebPIDecoderDeleter
{
52 inline void operator()(WebPIDecoder
* ptr
) const { WebPIDelete(ptr
); }
55 enum State
{ READING_FEATURES
, READING_DATA
, DONE
};
57 friend class base::RefCountedThreadSafe
<WebpDecoder
>;
58 virtual ~WebpDecoder();
60 // Implements WebP image decoding state machine steps.
61 void DoReadFeatures(NSData
* data
);
62 void DoReadData(NSData
* data
);
65 scoped_refptr
<WebpDecoder::Delegate
> delegate_
;
66 WebPDecoderConfig config_
;
67 WebpDecoder::State state_
;
68 scoped_ptr
<WebPIDecoder
, WebPIDecoderDeleter
> incremental_decoder_
;
69 base::scoped_nsobject
<NSData
> output_buffer_
;
70 base::scoped_nsobject
<NSMutableData
> features_
;
74 } // namespace webp_transcode
76 #endif // COMPONENTS_WEBP_TRANSCODE_WEBP_DECODER_H_