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 #include "base/mac/scoped_nsobject.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "third_party/libwebp/webp/decode.h"
15 namespace webp_transcode
{
17 // Decodes a WebP image into either JPEG, PNG or uncompressed TIFF.
18 class WebpDecoder
: public base::RefCountedThreadSafe
<WebpDecoder
> {
20 // Format of the decoded image.
21 // This enum is used for UMA reporting, keep it in sync with the histogram
23 enum DecodedImageFormat
{ JPEG
= 1, PNG
, TIFF
, DECODED_FORMAT_COUNT
};
25 class Delegate
: public base::RefCountedThreadSafe
<WebpDecoder::Delegate
> {
27 virtual void OnFinishedDecoding(bool success
) = 0;
28 virtual void SetImageFeatures(size_t total_size
, // In bytes.
29 DecodedImageFormat format
) = 0;
30 virtual void OnDataDecoded(NSData
* data
) = 0;
33 friend class base::RefCountedThreadSafe
<WebpDecoder::Delegate
>;
34 virtual ~Delegate() {}
37 explicit WebpDecoder(WebpDecoder::Delegate
* delegate
);
40 static size_t GetHeaderSize();
43 void OnDataReceived(const base::scoped_nsobject
<NSData
>& data
);
45 // Stops the decoding.
49 struct WebPIDecoderDeleter
{
50 inline void operator()(WebPIDecoder
* ptr
) const { WebPIDelete(ptr
); }
53 enum State
{ READING_FEATURES
, READING_DATA
, DONE
};
55 friend class base::RefCountedThreadSafe
<WebpDecoder
>;
56 virtual ~WebpDecoder();
58 // Implements WebP image decoding state machine steps.
59 void DoReadFeatures(NSData
* data
);
60 void DoReadData(NSData
* data
);
63 scoped_refptr
<WebpDecoder::Delegate
> delegate_
;
64 WebPDecoderConfig config_
;
65 WebpDecoder::State state_
;
66 scoped_ptr
<WebPIDecoder
, WebPIDecoderDeleter
> incremental_decoder_
;
67 base::scoped_nsobject
<NSData
> output_buffer_
;
68 base::scoped_nsobject
<NSMutableData
> features_
;
72 } // namespace webp_transcode
74 #endif // COMPONENTS_WEBP_TRANSCODE_WEBP_DECODER_H_