/src/mozilla-central/image/ImageOps.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
7 | | #include "ImageOps.h" |
8 | | |
9 | | #include "ClippedImage.h" |
10 | | #include "Decoder.h" |
11 | | #include "DecoderFactory.h" |
12 | | #include "DynamicImage.h" |
13 | | #include "FrozenImage.h" |
14 | | #include "IDecodingTask.h" |
15 | | #include "Image.h" |
16 | | #include "ImageMetadata.h" |
17 | | #include "imgIContainer.h" |
18 | | #include "mozilla/gfx/2D.h" |
19 | | #include "nsNetUtil.h" // for NS_NewBufferedInputStream |
20 | | #include "nsStreamUtils.h" |
21 | | #include "OrientedImage.h" |
22 | | #include "SourceBuffer.h" |
23 | | |
24 | | using namespace mozilla::gfx; |
25 | | |
26 | | namespace mozilla { |
27 | | namespace image { |
28 | | |
29 | | /* static */ already_AddRefed<Image> |
30 | | ImageOps::Freeze(Image* aImage) |
31 | 0 | { |
32 | 0 | RefPtr<Image> frozenImage = new FrozenImage(aImage); |
33 | 0 | return frozenImage.forget(); |
34 | 0 | } |
35 | | |
36 | | /* static */ already_AddRefed<imgIContainer> |
37 | | ImageOps::Freeze(imgIContainer* aImage) |
38 | 0 | { |
39 | 0 | nsCOMPtr<imgIContainer> frozenImage = |
40 | 0 | new FrozenImage(static_cast<Image*>(aImage)); |
41 | 0 | return frozenImage.forget(); |
42 | 0 | } |
43 | | |
44 | | /* static */ already_AddRefed<Image> |
45 | | ImageOps::Clip(Image* aImage, nsIntRect aClip, |
46 | | const Maybe<nsSize>& aSVGViewportSize) |
47 | 0 | { |
48 | 0 | RefPtr<Image> clippedImage = new ClippedImage(aImage, aClip, aSVGViewportSize); |
49 | 0 | return clippedImage.forget(); |
50 | 0 | } |
51 | | |
52 | | /* static */ already_AddRefed<imgIContainer> |
53 | | ImageOps::Clip(imgIContainer* aImage, nsIntRect aClip, |
54 | | const Maybe<nsSize>& aSVGViewportSize) |
55 | 0 | { |
56 | 0 | nsCOMPtr<imgIContainer> clippedImage = |
57 | 0 | new ClippedImage(static_cast<Image*>(aImage), aClip, aSVGViewportSize); |
58 | 0 | return clippedImage.forget(); |
59 | 0 | } |
60 | | |
61 | | /* static */ already_AddRefed<Image> |
62 | | ImageOps::Orient(Image* aImage, Orientation aOrientation) |
63 | 0 | { |
64 | 0 | RefPtr<Image> orientedImage = new OrientedImage(aImage, aOrientation); |
65 | 0 | return orientedImage.forget(); |
66 | 0 | } |
67 | | |
68 | | /* static */ already_AddRefed<imgIContainer> |
69 | | ImageOps::Orient(imgIContainer* aImage, Orientation aOrientation) |
70 | 0 | { |
71 | 0 | nsCOMPtr<imgIContainer> orientedImage = |
72 | 0 | new OrientedImage(static_cast<Image*>(aImage), aOrientation); |
73 | 0 | return orientedImage.forget(); |
74 | 0 | } |
75 | | |
76 | | /* static */ already_AddRefed<imgIContainer> |
77 | | ImageOps::CreateFromDrawable(gfxDrawable* aDrawable) |
78 | 0 | { |
79 | 0 | nsCOMPtr<imgIContainer> drawableImage = new DynamicImage(aDrawable); |
80 | 0 | return drawableImage.forget(); |
81 | 0 | } |
82 | | |
83 | | class ImageOps::ImageBufferImpl final : public ImageOps::ImageBuffer { |
84 | | public: |
85 | | explicit ImageBufferImpl(already_AddRefed<SourceBuffer> aSourceBuffer) |
86 | | : mSourceBuffer(aSourceBuffer) |
87 | 0 | { } |
88 | | |
89 | | protected: |
90 | 0 | ~ImageBufferImpl() override { } |
91 | | |
92 | | already_AddRefed<SourceBuffer> GetSourceBuffer() const override |
93 | 0 | { |
94 | 0 | RefPtr<SourceBuffer> sourceBuffer = mSourceBuffer; |
95 | 0 | return sourceBuffer.forget(); |
96 | 0 | } |
97 | | |
98 | | private: |
99 | | RefPtr<SourceBuffer> mSourceBuffer; |
100 | | }; |
101 | | |
102 | | /* static */ already_AddRefed<ImageOps::ImageBuffer> |
103 | | ImageOps::CreateImageBuffer(already_AddRefed<nsIInputStream> aInputStream) |
104 | 0 | { |
105 | 0 | nsCOMPtr<nsIInputStream> inputStream = std::move(aInputStream); |
106 | 0 | MOZ_ASSERT(inputStream); |
107 | 0 |
|
108 | 0 | nsresult rv; |
109 | 0 |
|
110 | 0 | // Prepare the input stream. |
111 | 0 | if (!NS_InputStreamIsBuffered(inputStream)) { |
112 | 0 | nsCOMPtr<nsIInputStream> bufStream; |
113 | 0 | rv = NS_NewBufferedInputStream(getter_AddRefs(bufStream), |
114 | 0 | inputStream.forget(), 1024); |
115 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
116 | 0 | return nullptr; |
117 | 0 | } |
118 | 0 | } |
119 | 0 | |
120 | 0 | // Figure out how much data we've been passed. |
121 | 0 | uint64_t length; |
122 | 0 | rv = inputStream->Available(&length); |
123 | 0 | if (NS_FAILED(rv) || length > UINT32_MAX) { |
124 | 0 | return nullptr; |
125 | 0 | } |
126 | 0 | |
127 | 0 | // Write the data into a SourceBuffer. |
128 | 0 | RefPtr<SourceBuffer> sourceBuffer = new SourceBuffer(); |
129 | 0 | sourceBuffer->ExpectLength(length); |
130 | 0 | rv = sourceBuffer->AppendFromInputStream(inputStream, length); |
131 | 0 | if (NS_FAILED(rv)) { |
132 | 0 | return nullptr; |
133 | 0 | } |
134 | 0 | // Make sure our sourceBuffer is marked as complete. |
135 | 0 | if (sourceBuffer->IsComplete()) { |
136 | 0 | NS_WARNING("The SourceBuffer was unexpectedly marked as complete. This may " |
137 | 0 | "indicate either an OOM condition, or that imagelib was not " |
138 | 0 | "initialized properly."); |
139 | 0 | return nullptr; |
140 | 0 | } |
141 | 0 | sourceBuffer->Complete(NS_OK); |
142 | 0 |
|
143 | 0 | RefPtr<ImageBuffer> imageBuffer = new ImageBufferImpl(sourceBuffer.forget()); |
144 | 0 | return imageBuffer.forget(); |
145 | 0 | } |
146 | | |
147 | | /* static */ nsresult |
148 | | ImageOps::DecodeMetadata(already_AddRefed<nsIInputStream> aInputStream, |
149 | | const nsACString& aMimeType, |
150 | | ImageMetadata& aMetadata) |
151 | 0 | { |
152 | 0 | nsCOMPtr<nsIInputStream> inputStream = std::move(aInputStream); |
153 | 0 | RefPtr<ImageBuffer> buffer = CreateImageBuffer(inputStream.forget()); |
154 | 0 | return DecodeMetadata(buffer, aMimeType, aMetadata); |
155 | 0 | } |
156 | | |
157 | | /* static */ nsresult |
158 | | ImageOps::DecodeMetadata(ImageBuffer* aBuffer, |
159 | | const nsACString& aMimeType, |
160 | | ImageMetadata& aMetadata) |
161 | 0 | { |
162 | 0 | if (!aBuffer) { |
163 | 0 | return NS_ERROR_FAILURE; |
164 | 0 | } |
165 | 0 | |
166 | 0 | RefPtr<SourceBuffer> sourceBuffer = aBuffer->GetSourceBuffer(); |
167 | 0 | if (NS_WARN_IF(!sourceBuffer)) { |
168 | 0 | return NS_ERROR_FAILURE; |
169 | 0 | } |
170 | 0 | |
171 | 0 | // Create a decoder. |
172 | 0 | DecoderType decoderType = |
173 | 0 | DecoderFactory::GetDecoderType(PromiseFlatCString(aMimeType).get()); |
174 | 0 | RefPtr<Decoder> decoder = |
175 | 0 | DecoderFactory::CreateAnonymousMetadataDecoder(decoderType, |
176 | 0 | WrapNotNull(sourceBuffer)); |
177 | 0 | if (!decoder) { |
178 | 0 | return NS_ERROR_FAILURE; |
179 | 0 | } |
180 | 0 | |
181 | 0 | // Run the decoder synchronously. |
182 | 0 | RefPtr<IDecodingTask> task = new AnonymousDecodingTask(WrapNotNull(decoder)); |
183 | 0 | task->Run(); |
184 | 0 | if (!decoder->GetDecodeDone() || decoder->HasError()) { |
185 | 0 | return NS_ERROR_FAILURE; |
186 | 0 | } |
187 | 0 | |
188 | 0 | aMetadata = decoder->GetImageMetadata(); |
189 | 0 | if (aMetadata.GetNativeSizes().IsEmpty() && aMetadata.HasSize()) { |
190 | 0 | aMetadata.AddNativeSize(aMetadata.GetSize()); |
191 | 0 | } |
192 | 0 |
|
193 | 0 | return NS_OK; |
194 | 0 | } |
195 | | |
196 | | /* static */ already_AddRefed<gfx::SourceSurface> |
197 | | ImageOps::DecodeToSurface(already_AddRefed<nsIInputStream> aInputStream, |
198 | | const nsACString& aMimeType, |
199 | | uint32_t aFlags, |
200 | | const Maybe<IntSize>& aSize /* = Nothing() */) |
201 | 0 | { |
202 | 0 | nsCOMPtr<nsIInputStream> inputStream = std::move(aInputStream); |
203 | 0 | RefPtr<ImageBuffer> buffer = CreateImageBuffer(inputStream.forget()); |
204 | 0 | return DecodeToSurface(buffer, aMimeType, aFlags, aSize); |
205 | 0 | } |
206 | | |
207 | | /* static */ already_AddRefed<gfx::SourceSurface> |
208 | | ImageOps::DecodeToSurface(ImageBuffer* aBuffer, |
209 | | const nsACString& aMimeType, |
210 | | uint32_t aFlags, |
211 | | const Maybe<IntSize>& aSize /* = Nothing() */) |
212 | 0 | { |
213 | 0 | if (!aBuffer) { |
214 | 0 | return nullptr; |
215 | 0 | } |
216 | 0 | |
217 | 0 | RefPtr<SourceBuffer> sourceBuffer = aBuffer->GetSourceBuffer(); |
218 | 0 | if (NS_WARN_IF(!sourceBuffer)) { |
219 | 0 | return nullptr; |
220 | 0 | } |
221 | 0 | |
222 | 0 | // Create a decoder. |
223 | 0 | DecoderType decoderType = |
224 | 0 | DecoderFactory::GetDecoderType(PromiseFlatCString(aMimeType).get()); |
225 | 0 | RefPtr<Decoder> decoder = |
226 | 0 | DecoderFactory::CreateAnonymousDecoder(decoderType, |
227 | 0 | WrapNotNull(sourceBuffer), |
228 | 0 | aSize, |
229 | 0 | DecoderFlags::FIRST_FRAME_ONLY, |
230 | 0 | ToSurfaceFlags(aFlags)); |
231 | 0 | if (!decoder) { |
232 | 0 | return nullptr; |
233 | 0 | } |
234 | 0 | |
235 | 0 | // Run the decoder synchronously. |
236 | 0 | RefPtr<IDecodingTask> task = new AnonymousDecodingTask(WrapNotNull(decoder)); |
237 | 0 | task->Run(); |
238 | 0 | if (!decoder->GetDecodeDone() || decoder->HasError()) { |
239 | 0 | return nullptr; |
240 | 0 | } |
241 | 0 | |
242 | 0 | // Pull out the surface. |
243 | 0 | RawAccessFrameRef frame = decoder->GetCurrentFrameRef(); |
244 | 0 | if (!frame) { |
245 | 0 | return nullptr; |
246 | 0 | } |
247 | 0 | |
248 | 0 | RefPtr<SourceSurface> surface = frame->GetSourceSurface(); |
249 | 0 | if (!surface) { |
250 | 0 | return nullptr; |
251 | 0 | } |
252 | 0 | |
253 | 0 | return surface.forget(); |
254 | 0 | } |
255 | | |
256 | | } // namespace image |
257 | | } // namespace mozilla |