webp_decoder.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #ifndef COMPONENTS_IMAGE_FETCHER_IOS_WEBP_DECODER_H_
  5. #define COMPONENTS_IMAGE_FETCHER_IOS_WEBP_DECODER_H_
  6. #import <Foundation/Foundation.h>
  7. #include <stddef.h>
  8. #include <memory>
  9. #include "base/memory/ref_counted.h"
  10. #include "third_party/libwebp/src/src/webp/decode.h"
  11. @class NSData;
  12. namespace webp_transcode {
  13. // Decodes a WebP image into either JPEG, PNG or uncompressed TIFF.
  14. class WebpDecoder : public base::RefCountedThreadSafe<WebpDecoder> {
  15. public:
  16. // Format of the decoded image.
  17. enum DecodedImageFormat { JPEG = 1, PNG, TIFF };
  18. class Delegate : public base::RefCountedThreadSafe<WebpDecoder::Delegate> {
  19. public:
  20. virtual void OnFinishedDecoding(bool success) = 0;
  21. virtual void SetImageFeatures(size_t total_size, // In bytes.
  22. DecodedImageFormat format) = 0;
  23. virtual void OnDataDecoded(NSData* data) = 0;
  24. protected:
  25. friend class base::RefCountedThreadSafe<WebpDecoder::Delegate>;
  26. virtual ~Delegate() {}
  27. };
  28. explicit WebpDecoder(WebpDecoder::Delegate* delegate);
  29. // Returns an NSData object containing the decoded image data of the given
  30. // webp_image. Returns nil in case of failure.
  31. static NSData* DecodeWebpImage(NSData* webp_image);
  32. // Returns true if the given image_data is a WebP image.
  33. //
  34. // Every WebP file contains a 12 byte file header in the beginning of the
  35. // file.
  36. // A WebP file header starts with the four ASCII characters "RIFF". The next
  37. // four bytes contain the image size and the last four header bytes contain
  38. // the four ASCII characters "WEBP".
  39. //
  40. // WebP file header:
  41. // 1 1
  42. // Byte Nr. 0 1 2 3 4 5 6 7 8 9 0 1
  43. // Byte value [ R I F F ? ? ? ? W E B P ]
  44. //
  45. // For more information see:
  46. // https://developers.google.com/speed/webp/docs/riff_container#webp_file_header
  47. static bool IsWebpImage(const std::string& image_data);
  48. // For tests.
  49. static size_t GetHeaderSize();
  50. // Main entry point.
  51. void OnDataReceived(NSData* data);
  52. // Stops the decoding.
  53. void Stop();
  54. private:
  55. struct WebPIDecoderDeleter {
  56. inline void operator()(WebPIDecoder* ptr) const { WebPIDelete(ptr); }
  57. };
  58. enum State { READING_FEATURES, READING_DATA, DONE };
  59. friend class base::RefCountedThreadSafe<WebpDecoder>;
  60. virtual ~WebpDecoder();
  61. // Implements WebP image decoding state machine steps.
  62. void DoReadFeatures(NSData* data);
  63. void DoReadData(NSData* data);
  64. bool DoSendData();
  65. scoped_refptr<WebpDecoder::Delegate> delegate_;
  66. WebPDecoderConfig config_;
  67. WebpDecoder::State state_;
  68. std::unique_ptr<WebPIDecoder, WebPIDecoderDeleter> incremental_decoder_;
  69. __strong NSData* output_buffer_;
  70. __strong NSMutableData* features_;
  71. int has_alpha_;
  72. };
  73. } // namespace webp_transcode
  74. #endif // COMPONENTS_IMAGE_FETCHER_IOS_WEBP_DECODER_H_