1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 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/. */
7 #ifndef mozilla_image_DecoderFactory_h
8 #define mozilla_image_DecoderFactory_h
10 #include "DecoderFlags.h"
11 #include "mozilla/Attributes.h"
12 #include "mozilla/Maybe.h"
13 #include "mozilla/NotNull.h"
14 #include "mozilla/gfx/2D.h"
16 #include "Orientation.h"
17 #include "SurfaceFlags.h"
19 namespace mozilla::image
{
26 class SourceBufferIterator
;
29 * The type of decoder; this is usually determined from a MIME type using
30 * DecoderFactory::GetDecoderType().
32 enum class DecoderType
{
46 class DecoderFactory
{
48 /// @return the type of decoder which is appropriate for @aMimeType.
49 static DecoderType
GetDecoderType(const char* aMimeType
);
51 /// @return the default flags to use when creating a decoder of @aType.
52 static DecoderFlags
GetDefaultDecoderFlagsForType(DecoderType aType
);
55 * Creates and initializes a decoder for non-animated images of type @aType.
56 * (If the image *is* animated, only the first frame will be decoded.) The
57 * decoder will send notifications to @aImage.
59 * @param aType Which type of decoder to create - JPEG, PNG, etc.
60 * @param aImage The image will own the decoder and which should receive
61 * notifications as decoding progresses.
62 * @param aSourceBuffer The SourceBuffer which the decoder will read its data
64 * @param aIntrinsicSize The intrinsic size of the image, normally obtained
65 * during the metadata decode.
66 * @param aOutputSize The output size for the decoder. If this is smaller than
67 * the intrinsic size, the decoder will downscale the
69 * @param aDecoderFlags Flags specifying the behavior of this decoder.
70 * @param aSurfaceFlags Flags specifying the type of output this decoder
72 * @param aOutTask Task representing the decoder.
73 * @return NS_OK if the decoder has been created/initialized successfully;
74 * NS_ERROR_ALREADY_INITIALIZED if there is already an active decoder
76 * Else some other unrecoverable error occurred.
78 static nsresult
CreateDecoder(DecoderType aType
, NotNull
<RasterImage
*> aImage
,
79 NotNull
<SourceBuffer
*> aSourceBuffer
,
80 const gfx::IntSize
& aIntrinsicSize
,
81 const gfx::IntSize
& aOutputSize
,
82 DecoderFlags aDecoderFlags
,
83 SurfaceFlags aSurfaceFlags
,
84 IDecodingTask
** aOutTask
);
87 * Creates and initializes a decoder for animated images of type @aType.
88 * The decoder will send notifications to @aImage.
90 * @param aType Which type of decoder to create - JPEG, PNG, etc.
91 * @param aImage The image will own the decoder and which should receive
92 * notifications as decoding progresses.
93 * @param aSourceBuffer The SourceBuffer which the decoder will read its data
95 * @param aIntrinsicSize The intrinsic size of the image, normally obtained
96 * during the metadata decode.
97 * @param aDecoderFlags Flags specifying the behavior of this decoder.
98 * @param aSurfaceFlags Flags specifying the type of output this decoder
100 * @param aCurrentFrame The current frame the decoder should auto advance to.
101 * @param aOutTask Task representing the decoder.
102 * @return NS_OK if the decoder has been created/initialized successfully;
103 * NS_ERROR_ALREADY_INITIALIZED if there is already an active decoder
105 * Else some other unrecoverable error occurred.
107 static nsresult
CreateAnimationDecoder(
108 DecoderType aType
, NotNull
<RasterImage
*> aImage
,
109 NotNull
<SourceBuffer
*> aSourceBuffer
, const gfx::IntSize
& aIntrinsicSize
,
110 DecoderFlags aDecoderFlags
, SurfaceFlags aSurfaceFlags
,
111 size_t aCurrentFrame
, IDecodingTask
** aOutTask
);
114 * Creates and initializes a decoder for animated images, cloned from the
117 * @param aDecoder Decoder to clone.
119 static already_AddRefed
<Decoder
> CloneAnimationDecoder(Decoder
* aDecoder
);
122 * Creates and initializes a metadata decoder of type @aType. This decoder
123 * will only decode the image's header, extracting metadata like the size of
124 * the image. No actual image data will be decoded and no surfaces will be
125 * allocated. The decoder will send notifications to @aImage.
127 * @param aType Which type of decoder to create - JPEG, PNG, etc.
128 * @param aImage The image will own the decoder and which should receive
129 * notifications as decoding progresses.
130 * @param aSourceBuffer The SourceBuffer which the decoder will read its data
133 static already_AddRefed
<IDecodingTask
> CreateMetadataDecoder(
134 DecoderType aType
, NotNull
<RasterImage
*> aImage
, DecoderFlags aFlags
,
135 NotNull
<SourceBuffer
*> aSourceBuffer
);
138 * Creates and initializes a decoder for an ICO resource, which may be either
139 * a BMP or PNG image.
141 * @param aType Which type of decoder to create. This must be either BMP or
143 * @param aIterator The SourceBufferIterator which the decoder will read its
145 * @param aICODecoder The ICO decoder which is controlling this resource
146 * decoder. @aICODecoder's settings will be copied to the
147 * resource decoder, so the two decoders will have the
148 * same decoder flags, surface flags, target size, and
150 * @param aIsMetadataDecode Indicates whether or not this decoder is for
151 * metadata or not. Independent of the state of the
153 * @param aExpectedSize The expected size of the resource from the ICO header.
154 * @param aDataOffset If @aType is BMP, specifies the offset at which data
155 * begins in the BMP resource. Must be Some() if and only
158 static already_AddRefed
<Decoder
> CreateDecoderForICOResource(
159 DecoderType aType
, SourceBufferIterator
&& aIterator
,
160 NotNull
<nsICODecoder
*> aICODecoder
, bool aIsMetadataDecode
,
161 const Maybe
<OrientedIntSize
>& aExpectedSize
,
162 const Maybe
<uint32_t>& aDataOffset
= Nothing());
165 * Creates and initializes an anonymous decoder (one which isn't associated
166 * with an Image object). Only the first frame of the image will be decoded.
168 * @param aType Which type of decoder to create - JPEG, PNG, etc.
169 * @param aSourceBuffer The SourceBuffer which the decoder will read its data
171 * @param aOutputSize If Some(), the output size for the decoder. If this is
172 * smaller than the intrinsic size, the decoder will
173 * downscale the image. If Nothing(), the output size will
174 * be the intrinsic size.
175 * @param aDecoderFlags Flags specifying the behavior of this decoder.
176 * @param aSurfaceFlags Flags specifying the type of output this decoder
179 static already_AddRefed
<Decoder
> CreateAnonymousDecoder(
180 DecoderType aType
, NotNull
<SourceBuffer
*> aSourceBuffer
,
181 const Maybe
<gfx::IntSize
>& aOutputSize
, DecoderFlags aDecoderFlags
,
182 SurfaceFlags aSurfaceFlags
);
185 * Creates and initializes an anonymous metadata decoder (one which isn't
186 * associated with an Image object). This decoder will only decode the image's
187 * header, extracting metadata like the size of the image. No actual image
188 * data will be decoded and no surfaces will be allocated.
190 * @param aType Which type of decoder to create - JPEG, PNG, etc.
191 * @param aSourceBuffer The SourceBuffer which the decoder will read its data
194 static already_AddRefed
<Decoder
> CreateAnonymousMetadataDecoder(
195 DecoderType aType
, NotNull
<SourceBuffer
*> aSourceBuffer
);
198 virtual ~DecoderFactory() = 0;
201 * An internal method which allocates a new decoder of the requested @aType.
203 static already_AddRefed
<Decoder
> GetDecoder(DecoderType aType
,
208 } // namespace mozilla::image
210 #endif // mozilla_image_DecoderFactory_h