DecoderFactory.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. *
  3. * This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. #ifndef mozilla_image_DecoderFactory_h
  7. #define mozilla_image_DecoderFactory_h
  8. #include "DecoderFlags.h"
  9. #include "mozilla/Attributes.h"
  10. #include "mozilla/Maybe.h"
  11. #include "mozilla/NotNull.h"
  12. #include "mozilla/gfx/2D.h"
  13. #include "nsCOMPtr.h"
  14. #include "SurfaceFlags.h"
  15. namespace mozilla {
  16. namespace image {
  17. class Decoder;
  18. class IDecodingTask;
  19. class nsICODecoder;
  20. class RasterImage;
  21. class SourceBuffer;
  22. /**
  23. * The type of decoder; this is usually determined from a MIME type using
  24. * DecoderFactory::GetDecoderType().
  25. */
  26. enum class DecoderType
  27. {
  28. PNG,
  29. GIF,
  30. JPEG,
  31. BMP,
  32. BMP_CLIPBOARD,
  33. ICO,
  34. ICON,
  35. WEBP,
  36. UNKNOWN
  37. };
  38. class DecoderFactory
  39. {
  40. public:
  41. /// @return the type of decoder which is appropriate for @aMimeType.
  42. static DecoderType GetDecoderType(const char* aMimeType);
  43. /**
  44. * Creates and initializes a decoder for non-animated images of type @aType.
  45. * (If the image *is* animated, only the first frame will be decoded.) The
  46. * decoder will send notifications to @aImage.
  47. *
  48. * @param aType Which type of decoder to create - JPEG, PNG, etc.
  49. * @param aImage The image will own the decoder and which should receive
  50. * notifications as decoding progresses.
  51. * @param aSourceBuffer The SourceBuffer which the decoder will read its data
  52. * from.
  53. * @param aIntrinsicSize The intrinsic size of the image, normally obtained
  54. * during the metadata decode.
  55. * @param aOutputSize The output size for the decoder. If this is smaller than
  56. * the intrinsic size, the decoder will downscale the
  57. * image.
  58. * @param aDecoderFlags Flags specifying the behavior of this decoder.
  59. * @param aSurfaceFlags Flags specifying the type of output this decoder
  60. * should produce.
  61. * @param aSampleSize The sample size requested using #-moz-samplesize (or 0
  62. * if none).
  63. */
  64. static already_AddRefed<IDecodingTask>
  65. CreateDecoder(DecoderType aType,
  66. NotNull<RasterImage*> aImage,
  67. NotNull<SourceBuffer*> aSourceBuffer,
  68. const gfx::IntSize& aIntrinsicSize,
  69. const gfx::IntSize& aOutputSize,
  70. DecoderFlags aDecoderFlags,
  71. SurfaceFlags aSurfaceFlags,
  72. int aSampleSize);
  73. /**
  74. * Creates and initializes a decoder for animated images of type @aType.
  75. * The decoder will send notifications to @aImage.
  76. *
  77. * @param aType Which type of decoder to create - JPEG, PNG, etc.
  78. * @param aImage The image will own the decoder and which should receive
  79. * notifications as decoding progresses.
  80. * @param aSourceBuffer The SourceBuffer which the decoder will read its data
  81. * from.
  82. * @param aIntrinsicSize The intrinsic size of the image, normally obtained
  83. * during the metadata decode.
  84. * @param aDecoderFlags Flags specifying the behavior of this decoder.
  85. * @param aSurfaceFlags Flags specifying the type of output this decoder
  86. * should produce.
  87. */
  88. static already_AddRefed<IDecodingTask>
  89. CreateAnimationDecoder(DecoderType aType,
  90. NotNull<RasterImage*> aImage,
  91. NotNull<SourceBuffer*> aSourceBuffer,
  92. const gfx::IntSize& aIntrinsicSize,
  93. DecoderFlags aDecoderFlags,
  94. SurfaceFlags aSurfaceFlags);
  95. /**
  96. * Creates and initializes a metadata decoder of type @aType. This decoder
  97. * will only decode the image's header, extracting metadata like the size of
  98. * the image. No actual image data will be decoded and no surfaces will be
  99. * allocated. The decoder will send notifications to @aImage.
  100. *
  101. * @param aType Which type of decoder to create - JPEG, PNG, etc.
  102. * @param aImage The image will own the decoder and which should receive
  103. * notifications as decoding progresses.
  104. * @param aSourceBuffer The SourceBuffer which the decoder will read its data
  105. * from.
  106. * @param aSampleSize The sample size requested using #-moz-samplesize (or 0
  107. * if none).
  108. */
  109. static already_AddRefed<IDecodingTask>
  110. CreateMetadataDecoder(DecoderType aType,
  111. NotNull<RasterImage*> aImage,
  112. NotNull<SourceBuffer*> aSourceBuffer,
  113. int aSampleSize);
  114. /**
  115. * Creates and initializes a decoder for an ICO resource, which may be either
  116. * a BMP or PNG image.
  117. *
  118. * @param aType Which type of decoder to create. This must be either BMP or
  119. * PNG.
  120. * @param aSourceBuffer The SourceBuffer which the decoder will read its data
  121. * from.
  122. * @param aICODecoder The ICO decoder which is controlling this resource
  123. * decoder. @aICODecoder's settings will be copied to the
  124. * resource decoder, so the two decoders will have the
  125. * same decoder flags, surface flags, target size, and
  126. * other parameters.
  127. * @param aDataOffset If @aType is BMP, specifies the offset at which data
  128. * begins in the BMP resource. Must be Some() if and only
  129. * if @aType is BMP.
  130. */
  131. static already_AddRefed<Decoder>
  132. CreateDecoderForICOResource(DecoderType aType,
  133. NotNull<SourceBuffer*> aSourceBuffer,
  134. NotNull<nsICODecoder*> aICODecoder,
  135. const Maybe<uint32_t>& aDataOffset = Nothing());
  136. /**
  137. * Creates and initializes an anonymous decoder (one which isn't associated
  138. * with an Image object). Only the first frame of the image will be decoded.
  139. *
  140. * @param aType Which type of decoder to create - JPEG, PNG, etc.
  141. * @param aSourceBuffer The SourceBuffer which the decoder will read its data
  142. * from.
  143. * @param aOutputSize If Some(), the output size for the decoder. If this is
  144. * smaller than the intrinsic size, the decoder will
  145. * downscale the image. If Nothing(), the output size will
  146. * be the intrinsic size.
  147. * @param aSurfaceFlags Flags specifying the type of output this decoder
  148. * should produce.
  149. */
  150. static already_AddRefed<Decoder>
  151. CreateAnonymousDecoder(DecoderType aType,
  152. NotNull<SourceBuffer*> aSourceBuffer,
  153. const Maybe<gfx::IntSize>& aOutputSize,
  154. SurfaceFlags aSurfaceFlags);
  155. /**
  156. * Creates and initializes an anonymous metadata decoder (one which isn't
  157. * associated with an Image object). This decoder will only decode the image's
  158. * header, extracting metadata like the size of the image. No actual image
  159. * data will be decoded and no surfaces will be allocated.
  160. *
  161. * @param aType Which type of decoder to create - JPEG, PNG, etc.
  162. * @param aSourceBuffer The SourceBuffer which the decoder will read its data
  163. * from.
  164. */
  165. static already_AddRefed<Decoder>
  166. CreateAnonymousMetadataDecoder(DecoderType aType,
  167. NotNull<SourceBuffer*> aSourceBuffer);
  168. private:
  169. virtual ~DecoderFactory() = 0;
  170. /**
  171. * An internal method which allocates a new decoder of the requested @aType.
  172. */
  173. static already_AddRefed<Decoder> GetDecoder(DecoderType aType,
  174. RasterImage* aImage,
  175. bool aIsRedecode);
  176. };
  177. } // namespace image
  178. } // namespace mozilla
  179. #endif // mozilla_image_DecoderFactory_h