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 "imgTools.h"
8 
9 #include "DecodePool.h"
10 #include "gfxUtils.h"
11 #include "mozilla/gfx/2D.h"
12 #include "mozilla/gfx/Logging.h"
13 #include "mozilla/RefPtr.h"
14 #include "nsCOMPtr.h"
15 #include "mozilla/dom/Document.h"
16 #include "nsError.h"
17 #include "imgLoader.h"
18 #include "imgICache.h"
19 #include "imgIContainer.h"
20 #include "imgIEncoder.h"
21 #include "nsComponentManagerUtils.h"
22 #include "nsNetUtil.h"  // for NS_NewBufferedInputStream
23 #include "nsStreamUtils.h"
24 #include "nsStringStream.h"
25 #include "nsContentUtils.h"
26 #include "nsProxyRelease.h"
27 #include "nsIStreamListener.h"
28 #include "ImageFactory.h"
29 #include "Image.h"
30 #include "IProgressObserver.h"
31 #include "ScriptedNotificationObserver.h"
32 #include "imgIScriptedNotificationObserver.h"
33 #include "gfxPlatform.h"
34 #include "js/ArrayBuffer.h"
35 #include "js/RootingAPI.h"  // JS::{Handle,Rooted}
36 #include "js/Value.h"       // JS::Value
37 #include "Orientation.h"
38 
39 using namespace mozilla::gfx;
40 
41 namespace mozilla {
42 namespace image {
43 
44 namespace {
45 
sniff_mimetype_callback(nsIInputStream * in,void * data,const char * fromRawSegment,uint32_t toOffset,uint32_t count,uint32_t * writeCount)46 static nsresult sniff_mimetype_callback(nsIInputStream* in, void* data,
47                                         const char* fromRawSegment,
48                                         uint32_t toOffset, uint32_t count,
49                                         uint32_t* writeCount) {
50   nsCString* mimeType = static_cast<nsCString*>(data);
51   MOZ_ASSERT(mimeType, "mimeType is null!");
52 
53   if (count > 0) {
54     imgLoader::GetMimeTypeFromContent(fromRawSegment, count, *mimeType);
55   }
56 
57   *writeCount = 0;
58   return NS_ERROR_FAILURE;
59 }
60 
61 class ImageDecoderListener final : public nsIStreamListener,
62                                    public IProgressObserver,
63                                    public imgIContainer {
64  public:
65   NS_DECL_ISUPPORTS
66 
ImageDecoderListener(nsIURI * aURI,imgIContainerCallback * aCallback,imgINotificationObserver * aObserver)67   ImageDecoderListener(nsIURI* aURI, imgIContainerCallback* aCallback,
68                        imgINotificationObserver* aObserver)
69       : mURI(aURI),
70         mImage(nullptr),
71         mCallback(aCallback),
72         mObserver(aObserver) {
73     MOZ_ASSERT(NS_IsMainThread());
74   }
75 
76   NS_IMETHOD
OnDataAvailable(nsIRequest * aRequest,nsIInputStream * aInputStream,uint64_t aOffset,uint32_t aCount)77   OnDataAvailable(nsIRequest* aRequest, nsIInputStream* aInputStream,
78                   uint64_t aOffset, uint32_t aCount) override {
79     if (!mImage) {
80       nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
81 
82       nsCString mimeType;
83       channel->GetContentType(mimeType);
84 
85       if (aInputStream) {
86         // Look at the first few bytes and see if we can tell what the data is
87         // from that since servers tend to lie. :(
88         uint32_t unused;
89         aInputStream->ReadSegments(sniff_mimetype_callback, &mimeType, aCount,
90                                    &unused);
91       }
92 
93       RefPtr<ProgressTracker> tracker = new ProgressTracker();
94       if (mObserver) {
95         tracker->AddObserver(this);
96       }
97 
98       mImage = ImageFactory::CreateImage(channel, tracker, mimeType, mURI,
99                                          /* aIsMultiPart */ false, 0);
100 
101       if (mImage->HasError()) {
102         return NS_ERROR_FAILURE;
103       }
104     }
105 
106     return mImage->OnImageDataAvailable(aRequest, nullptr, aInputStream,
107                                         aOffset, aCount);
108   }
109 
110   NS_IMETHOD
OnStartRequest(nsIRequest * aRequest)111   OnStartRequest(nsIRequest* aRequest) override { return NS_OK; }
112 
113   NS_IMETHOD
OnStopRequest(nsIRequest * aRequest,nsresult aStatus)114   OnStopRequest(nsIRequest* aRequest, nsresult aStatus) override {
115     // Encouter a fetch error, or no data could be fetched.
116     if (!mImage || NS_FAILED(aStatus)) {
117       mCallback->OnImageReady(nullptr, mImage ? aStatus : NS_ERROR_FAILURE);
118       return NS_OK;
119     }
120 
121     mImage->OnImageDataComplete(aRequest, nullptr, aStatus, true);
122     nsCOMPtr<imgIContainer> container = this;
123     mCallback->OnImageReady(container, aStatus);
124     return NS_OK;
125   }
126 
Notify(int32_t aType,const nsIntRect * aRect=nullptr)127   virtual void Notify(int32_t aType,
128                       const nsIntRect* aRect = nullptr) override {
129     if (mObserver) {
130       mObserver->Notify(nullptr, aType, aRect);
131     }
132   }
133 
OnLoadComplete(bool aLastPart)134   virtual void OnLoadComplete(bool aLastPart) override {}
135 
136   // Other notifications are ignored.
SetHasImage()137   virtual void SetHasImage() override {}
NotificationsDeferred() const138   virtual bool NotificationsDeferred() const override { return false; }
MarkPendingNotify()139   virtual void MarkPendingNotify() override {}
ClearPendingNotify()140   virtual void ClearPendingNotify() override {}
141 
142   // imgIContainer
143   NS_FORWARD_IMGICONTAINER(mImage->)
144 
GetNativeSizes(nsTArray<nsIntSize> & aNativeSizes) const145   nsresult GetNativeSizes(nsTArray<nsIntSize>& aNativeSizes) const override {
146     return mImage->GetNativeSizes(aNativeSizes);
147   }
148 
GetNativeSizesLength() const149   size_t GetNativeSizesLength() const override {
150     return mImage->GetNativeSizesLength();
151   }
152 
153  private:
154   virtual ~ImageDecoderListener() = default;
155 
156   nsCOMPtr<nsIURI> mURI;
157   RefPtr<image::Image> mImage;
158   nsCOMPtr<imgIContainerCallback> mCallback;
159   nsCOMPtr<imgINotificationObserver> mObserver;
160 };
161 
162 NS_IMPL_ISUPPORTS(ImageDecoderListener, nsIStreamListener, imgIContainer)
163 
164 class ImageDecoderHelper final : public Runnable,
165                                  public nsIInputStreamCallback {
166  public:
167   NS_DECL_ISUPPORTS_INHERITED
168 
ImageDecoderHelper(already_AddRefed<image::Image> aImage,already_AddRefed<nsIInputStream> aInputStream,nsIEventTarget * aEventTarget,imgIContainerCallback * aCallback,nsIEventTarget * aCallbackEventTarget)169   ImageDecoderHelper(already_AddRefed<image::Image> aImage,
170                      already_AddRefed<nsIInputStream> aInputStream,
171                      nsIEventTarget* aEventTarget,
172                      imgIContainerCallback* aCallback,
173                      nsIEventTarget* aCallbackEventTarget)
174       : Runnable("ImageDecoderHelper"),
175         mImage(std::move(aImage)),
176         mInputStream(std::move(aInputStream)),
177         mEventTarget(aEventTarget),
178         mCallback(aCallback),
179         mCallbackEventTarget(aCallbackEventTarget),
180         mStatus(NS_OK) {
181     MOZ_ASSERT(NS_IsMainThread());
182   }
183 
184   NS_IMETHOD
Run()185   Run() override {
186     // This runnable is dispatched on the Image thread when reading data, but
187     // at the end, it goes back to the main-thread in order to complete the
188     // operation.
189     if (NS_IsMainThread()) {
190       // Let the Image know we've sent all the data.
191       mImage->OnImageDataComplete(nullptr, nullptr, mStatus, true);
192 
193       RefPtr<ProgressTracker> tracker = mImage->GetProgressTracker();
194       tracker->SyncNotifyProgress(FLAG_LOAD_COMPLETE);
195 
196       nsCOMPtr<imgIContainer> container;
197       if (NS_SUCCEEDED(mStatus)) {
198         container = mImage;
199       }
200 
201       mCallback->OnImageReady(container, mStatus);
202       return NS_OK;
203     }
204 
205     uint64_t length;
206     nsresult rv = mInputStream->Available(&length);
207     if (rv == NS_BASE_STREAM_CLOSED) {
208       return OperationCompleted(NS_OK);
209     }
210 
211     if (NS_WARN_IF(NS_FAILED(rv))) {
212       return OperationCompleted(rv);
213     }
214 
215     // Nothing else to read, but maybe we just need to wait.
216     if (length == 0) {
217       nsCOMPtr<nsIAsyncInputStream> asyncInputStream =
218           do_QueryInterface(mInputStream);
219       if (asyncInputStream) {
220         rv = asyncInputStream->AsyncWait(this, 0, 0, mEventTarget);
221         if (NS_WARN_IF(NS_FAILED(rv))) {
222           return OperationCompleted(rv);
223         }
224         return NS_OK;
225       }
226 
227       // We really have nothing else to read.
228       if (length == 0) {
229         return OperationCompleted(NS_OK);
230       }
231     }
232 
233     // Send the source data to the Image.
234     rv = mImage->OnImageDataAvailable(nullptr, nullptr, mInputStream, 0,
235                                       uint32_t(length));
236     if (NS_WARN_IF(NS_FAILED(rv))) {
237       return OperationCompleted(rv);
238     }
239 
240     rv = mEventTarget->Dispatch(this, NS_DISPATCH_NORMAL);
241     if (NS_WARN_IF(NS_FAILED(rv))) {
242       return OperationCompleted(rv);
243     }
244 
245     return NS_OK;
246   }
247 
248   NS_IMETHOD
OnInputStreamReady(nsIAsyncInputStream * aAsyncInputStream)249   OnInputStreamReady(nsIAsyncInputStream* aAsyncInputStream) override {
250     MOZ_ASSERT(!NS_IsMainThread());
251     return Run();
252   }
253 
OperationCompleted(nsresult aStatus)254   nsresult OperationCompleted(nsresult aStatus) {
255     MOZ_ASSERT(!NS_IsMainThread());
256 
257     mStatus = aStatus;
258     mCallbackEventTarget->Dispatch(this, NS_DISPATCH_NORMAL);
259     return NS_OK;
260   }
261 
262  private:
~ImageDecoderHelper()263   ~ImageDecoderHelper() {
264     SurfaceCache::ReleaseImageOnMainThread(mImage.forget());
265     NS_ReleaseOnMainThread("ImageDecoderHelper::mCallback", mCallback.forget());
266   }
267 
268   RefPtr<image::Image> mImage;
269 
270   nsCOMPtr<nsIInputStream> mInputStream;
271   nsCOMPtr<nsIEventTarget> mEventTarget;
272   nsCOMPtr<imgIContainerCallback> mCallback;
273   nsCOMPtr<nsIEventTarget> mCallbackEventTarget;
274 
275   nsresult mStatus;
276 };
277 
278 NS_IMPL_ISUPPORTS_INHERITED(ImageDecoderHelper, Runnable,
279                             nsIInputStreamCallback)
280 
281 }  // namespace
282 
283 /* ========== imgITools implementation ========== */
284 
NS_IMPL_ISUPPORTS(imgTools,imgITools)285 NS_IMPL_ISUPPORTS(imgTools, imgITools)
286 
287 imgTools::imgTools() { /* member initializers and constructor code */
288 }
289 
~imgTools()290 imgTools::~imgTools() { /* destructor code */
291 }
292 
293 NS_IMETHODIMP
DecodeImageFromArrayBuffer(JS::Handle<JS::Value> aArrayBuffer,const nsACString & aMimeType,JSContext * aCx,imgIContainer ** aContainer)294 imgTools::DecodeImageFromArrayBuffer(JS::Handle<JS::Value> aArrayBuffer,
295                                      const nsACString& aMimeType,
296                                      JSContext* aCx,
297                                      imgIContainer** aContainer) {
298   if (!aArrayBuffer.isObject()) {
299     return NS_ERROR_FAILURE;
300   }
301 
302   JS::Rooted<JSObject*> obj(aCx,
303                             JS::UnwrapArrayBuffer(&aArrayBuffer.toObject()));
304   if (!obj) {
305     return NS_ERROR_FAILURE;
306   }
307 
308   uint8_t* bufferData = nullptr;
309   size_t bufferLength = 0;
310   bool isSharedMemory = false;
311 
312   JS::GetArrayBufferLengthAndData(obj, &bufferLength, &isSharedMemory,
313                                   &bufferData);
314 
315   // Throw for large ArrayBuffers to prevent truncation.
316   if (bufferLength > INT32_MAX) {
317     return NS_ERROR_ILLEGAL_VALUE;
318   }
319 
320   return DecodeImageFromBuffer((char*)bufferData, bufferLength, aMimeType,
321                                aContainer);
322 }
323 
324 NS_IMETHODIMP
DecodeImageFromBuffer(const char * aBuffer,uint32_t aSize,const nsACString & aMimeType,imgIContainer ** aContainer)325 imgTools::DecodeImageFromBuffer(const char* aBuffer, uint32_t aSize,
326                                 const nsACString& aMimeType,
327                                 imgIContainer** aContainer) {
328   MOZ_ASSERT(NS_IsMainThread());
329 
330   NS_ENSURE_ARG_POINTER(aBuffer);
331 
332   // Create a new image container to hold the decoded data.
333   nsAutoCString mimeType(aMimeType);
334   RefPtr<image::Image> image =
335       ImageFactory::CreateAnonymousImage(mimeType, aSize);
336   RefPtr<ProgressTracker> tracker = image->GetProgressTracker();
337 
338   if (image->HasError()) {
339     return NS_ERROR_FAILURE;
340   }
341 
342   // Let's create a temporary inputStream.
343   nsCOMPtr<nsIInputStream> stream;
344   nsresult rv = NS_NewByteInputStream(
345       getter_AddRefs(stream), Span(aBuffer, aSize), NS_ASSIGNMENT_DEPEND);
346   NS_ENSURE_SUCCESS(rv, rv);
347   MOZ_ASSERT(stream);
348   MOZ_ASSERT(NS_InputStreamIsBuffered(stream));
349 
350   rv = image->OnImageDataAvailable(nullptr, nullptr, stream, 0, aSize);
351   NS_ENSURE_SUCCESS(rv, rv);
352 
353   // Let the Image know we've sent all the data.
354   rv = image->OnImageDataComplete(nullptr, nullptr, NS_OK, true);
355   tracker->SyncNotifyProgress(FLAG_LOAD_COMPLETE);
356   NS_ENSURE_SUCCESS(rv, rv);
357 
358   // All done.
359   image.forget(aContainer);
360   return NS_OK;
361 }
362 
363 NS_IMETHODIMP
DecodeImageFromChannelAsync(nsIURI * aURI,nsIChannel * aChannel,imgIContainerCallback * aCallback,imgINotificationObserver * aObserver)364 imgTools::DecodeImageFromChannelAsync(nsIURI* aURI, nsIChannel* aChannel,
365                                       imgIContainerCallback* aCallback,
366                                       imgINotificationObserver* aObserver) {
367   MOZ_ASSERT(NS_IsMainThread());
368 
369   NS_ENSURE_ARG_POINTER(aURI);
370   NS_ENSURE_ARG_POINTER(aChannel);
371   NS_ENSURE_ARG_POINTER(aCallback);
372 
373   RefPtr<ImageDecoderListener> listener =
374       new ImageDecoderListener(aURI, aCallback, aObserver);
375 
376   return aChannel->AsyncOpen(listener);
377 }
378 
379 NS_IMETHODIMP
DecodeImageAsync(nsIInputStream * aInStr,const nsACString & aMimeType,imgIContainerCallback * aCallback,nsIEventTarget * aEventTarget)380 imgTools::DecodeImageAsync(nsIInputStream* aInStr, const nsACString& aMimeType,
381                            imgIContainerCallback* aCallback,
382                            nsIEventTarget* aEventTarget) {
383   MOZ_ASSERT(NS_IsMainThread());
384 
385   NS_ENSURE_ARG_POINTER(aInStr);
386   NS_ENSURE_ARG_POINTER(aCallback);
387   NS_ENSURE_ARG_POINTER(aEventTarget);
388 
389   nsresult rv;
390 
391   // Let's continuing the reading on a separate thread.
392   DecodePool* decodePool = DecodePool::Singleton();
393   MOZ_ASSERT(decodePool);
394 
395   RefPtr<nsIEventTarget> target = decodePool->GetIOEventTarget();
396   NS_ENSURE_TRUE(target, NS_ERROR_FAILURE);
397 
398   // Prepare the input stream.
399   nsCOMPtr<nsIInputStream> stream = aInStr;
400   if (!NS_InputStreamIsBuffered(aInStr)) {
401     nsCOMPtr<nsIInputStream> bufStream;
402     rv = NS_NewBufferedInputStream(getter_AddRefs(bufStream), stream.forget(),
403                                    1024);
404     NS_ENSURE_SUCCESS(rv, rv);
405     stream = std::move(bufStream);
406   }
407 
408   // Create a new image container to hold the decoded data.
409   nsAutoCString mimeType(aMimeType);
410   RefPtr<image::Image> image = ImageFactory::CreateAnonymousImage(mimeType, 0);
411 
412   // Already an error?
413   if (image->HasError()) {
414     return NS_ERROR_FAILURE;
415   }
416 
417   RefPtr<ImageDecoderHelper> helper = new ImageDecoderHelper(
418       image.forget(), stream.forget(), target, aCallback, aEventTarget);
419   rv = target->Dispatch(helper.forget(), NS_DISPATCH_NORMAL);
420   NS_ENSURE_SUCCESS(rv, rv);
421 
422   return NS_OK;
423 }
424 
425 /**
426  * This takes a DataSourceSurface rather than a SourceSurface because some
427  * of the callers have a DataSourceSurface and we don't want to call
428  * GetDataSurface on such surfaces since that may incur a conversion to
429  * SurfaceType::DATA which we don't need.
430  */
EncodeImageData(DataSourceSurface * aDataSurface,DataSourceSurface::ScopedMap & aMap,const nsACString & aMimeType,const nsAString & aOutputOptions,nsIInputStream ** aStream)431 static nsresult EncodeImageData(DataSourceSurface* aDataSurface,
432                                 DataSourceSurface::ScopedMap& aMap,
433                                 const nsACString& aMimeType,
434                                 const nsAString& aOutputOptions,
435                                 nsIInputStream** aStream) {
436   MOZ_ASSERT(aDataSurface->GetFormat() == SurfaceFormat::B8G8R8A8 ||
437                  aDataSurface->GetFormat() == SurfaceFormat::B8G8R8X8,
438              "We're assuming B8G8R8A8/X8");
439 
440   // Get an image encoder for the media type
441   nsAutoCString encoderCID("@mozilla.org/image/encoder;2?type="_ns + aMimeType);
442 
443   nsCOMPtr<imgIEncoder> encoder = do_CreateInstance(encoderCID.get());
444   if (!encoder) {
445     return NS_IMAGELIB_ERROR_NO_ENCODER;
446   }
447 
448   IntSize size = aDataSurface->GetSize();
449   uint32_t dataLength = aMap.GetStride() * size.height;
450 
451   // Encode the bitmap
452   nsresult rv = encoder->InitFromData(
453       aMap.GetData(), dataLength, size.width, size.height, aMap.GetStride(),
454       imgIEncoder::INPUT_FORMAT_HOSTARGB, aOutputOptions);
455   NS_ENSURE_SUCCESS(rv, rv);
456 
457   encoder.forget(aStream);
458   return NS_OK;
459 }
460 
EncodeImageData(DataSourceSurface * aDataSurface,const nsACString & aMimeType,const nsAString & aOutputOptions,nsIInputStream ** aStream)461 static nsresult EncodeImageData(DataSourceSurface* aDataSurface,
462                                 const nsACString& aMimeType,
463                                 const nsAString& aOutputOptions,
464                                 nsIInputStream** aStream) {
465   DataSourceSurface::ScopedMap map(aDataSurface, DataSourceSurface::READ);
466   if (!map.IsMapped()) {
467     return NS_ERROR_FAILURE;
468   }
469 
470   return EncodeImageData(aDataSurface, map, aMimeType, aOutputOptions, aStream);
471 }
472 
473 NS_IMETHODIMP
EncodeImage(imgIContainer * aContainer,const nsACString & aMimeType,const nsAString & aOutputOptions,nsIInputStream ** aStream)474 imgTools::EncodeImage(imgIContainer* aContainer, const nsACString& aMimeType,
475                       const nsAString& aOutputOptions,
476                       nsIInputStream** aStream) {
477   // Use frame 0 from the image container.
478   RefPtr<SourceSurface> frame = aContainer->GetFrame(
479       imgIContainer::FRAME_FIRST,
480       imgIContainer::FLAG_SYNC_DECODE | imgIContainer::FLAG_ASYNC_NOTIFY);
481   NS_ENSURE_TRUE(frame, NS_ERROR_FAILURE);
482 
483   RefPtr<DataSourceSurface> dataSurface;
484 
485   if (frame->GetFormat() == SurfaceFormat::B8G8R8A8 ||
486       frame->GetFormat() == SurfaceFormat::B8G8R8X8) {
487     dataSurface = frame->GetDataSurface();
488   } else {
489     // Convert format to SurfaceFormat::B8G8R8A8
490     dataSurface = gfxUtils::CopySurfaceToDataSourceSurfaceWithFormat(
491         frame, SurfaceFormat::B8G8R8A8);
492   }
493 
494   NS_ENSURE_TRUE(dataSurface, NS_ERROR_FAILURE);
495 
496   return EncodeImageData(dataSurface, aMimeType, aOutputOptions, aStream);
497 }
498 
499 NS_IMETHODIMP
EncodeScaledImage(imgIContainer * aContainer,const nsACString & aMimeType,int32_t aScaledWidth,int32_t aScaledHeight,const nsAString & aOutputOptions,nsIInputStream ** aStream)500 imgTools::EncodeScaledImage(imgIContainer* aContainer,
501                             const nsACString& aMimeType, int32_t aScaledWidth,
502                             int32_t aScaledHeight,
503                             const nsAString& aOutputOptions,
504                             nsIInputStream** aStream) {
505   NS_ENSURE_ARG(aScaledWidth >= 0 && aScaledHeight >= 0);
506 
507   // If no scaled size is specified, we'll just encode the image at its
508   // original size (no scaling).
509   if (aScaledWidth == 0 && aScaledHeight == 0) {
510     return EncodeImage(aContainer, aMimeType, aOutputOptions, aStream);
511   }
512 
513   // Retrieve the image's size.
514   int32_t imageWidth = 0;
515   int32_t imageHeight = 0;
516   aContainer->GetWidth(&imageWidth);
517   aContainer->GetHeight(&imageHeight);
518 
519   // If the given width or height is zero we'll replace it with the image's
520   // original dimensions.
521   IntSize scaledSize(aScaledWidth == 0 ? imageWidth : aScaledWidth,
522                      aScaledHeight == 0 ? imageHeight : aScaledHeight);
523 
524   // Use frame 0 from the image container.
525   RefPtr<SourceSurface> frame = aContainer->GetFrameAtSize(
526       scaledSize, imgIContainer::FRAME_FIRST,
527       imgIContainer::FLAG_HIGH_QUALITY_SCALING |
528           imgIContainer::FLAG_SYNC_DECODE | imgIContainer::FLAG_ASYNC_NOTIFY);
529   NS_ENSURE_TRUE(frame, NS_ERROR_FAILURE);
530 
531   // If the given surface is the right size/format, we can encode it directly.
532   if (scaledSize == frame->GetSize() &&
533       (frame->GetFormat() == SurfaceFormat::B8G8R8A8 ||
534        frame->GetFormat() == SurfaceFormat::B8G8R8X8)) {
535     RefPtr<DataSourceSurface> dataSurface = frame->GetDataSurface();
536     if (dataSurface) {
537       return EncodeImageData(dataSurface, aMimeType, aOutputOptions, aStream);
538     }
539   }
540 
541   // Otherwise we need to scale it using a draw target.
542   RefPtr<DataSourceSurface> dataSurface =
543       Factory::CreateDataSourceSurface(scaledSize, SurfaceFormat::B8G8R8A8);
544   if (NS_WARN_IF(!dataSurface)) {
545     return NS_ERROR_FAILURE;
546   }
547 
548   DataSourceSurface::ScopedMap map(dataSurface, DataSourceSurface::READ_WRITE);
549   if (!map.IsMapped()) {
550     return NS_ERROR_FAILURE;
551   }
552 
553   RefPtr<DrawTarget> dt = Factory::CreateDrawTargetForData(
554       BackendType::SKIA, map.GetData(), dataSurface->GetSize(), map.GetStride(),
555       SurfaceFormat::B8G8R8A8);
556   if (!dt) {
557     gfxWarning() << "imgTools::EncodeImage failed in CreateDrawTargetForData";
558     return NS_ERROR_OUT_OF_MEMORY;
559   }
560 
561   IntSize frameSize = frame->GetSize();
562   dt->DrawSurface(frame, Rect(0, 0, scaledSize.width, scaledSize.height),
563                   Rect(0, 0, frameSize.width, frameSize.height),
564                   DrawSurfaceOptions(),
565                   DrawOptions(1.0f, CompositionOp::OP_SOURCE));
566 
567   return EncodeImageData(dataSurface, map, aMimeType, aOutputOptions, aStream);
568 }
569 
570 NS_IMETHODIMP
EncodeCroppedImage(imgIContainer * aContainer,const nsACString & aMimeType,int32_t aOffsetX,int32_t aOffsetY,int32_t aWidth,int32_t aHeight,const nsAString & aOutputOptions,nsIInputStream ** aStream)571 imgTools::EncodeCroppedImage(imgIContainer* aContainer,
572                              const nsACString& aMimeType, int32_t aOffsetX,
573                              int32_t aOffsetY, int32_t aWidth, int32_t aHeight,
574                              const nsAString& aOutputOptions,
575                              nsIInputStream** aStream) {
576   NS_ENSURE_ARG(aOffsetX >= 0 && aOffsetY >= 0 && aWidth >= 0 && aHeight >= 0);
577 
578   // Offsets must be zero when no width and height are given or else we're out
579   // of bounds.
580   NS_ENSURE_ARG(aWidth + aHeight > 0 || aOffsetX + aOffsetY == 0);
581 
582   // If no size is specified then we'll preserve the image's original dimensions
583   // and don't need to crop.
584   if (aWidth == 0 && aHeight == 0) {
585     return EncodeImage(aContainer, aMimeType, aOutputOptions, aStream);
586   }
587 
588   // Use frame 0 from the image container.
589   RefPtr<SourceSurface> frame = aContainer->GetFrame(
590       imgIContainer::FRAME_FIRST,
591       imgIContainer::FLAG_SYNC_DECODE | imgIContainer::FLAG_ASYNC_NOTIFY);
592   NS_ENSURE_TRUE(frame, NS_ERROR_FAILURE);
593 
594   int32_t frameWidth = frame->GetSize().width;
595   int32_t frameHeight = frame->GetSize().height;
596 
597   // If the given width or height is zero we'll replace it with the image's
598   // original dimensions.
599   if (aWidth == 0) {
600     aWidth = frameWidth;
601   } else if (aHeight == 0) {
602     aHeight = frameHeight;
603   }
604 
605   // Check that the given crop rectangle is within image bounds.
606   NS_ENSURE_ARG(frameWidth >= aOffsetX + aWidth &&
607                 frameHeight >= aOffsetY + aHeight);
608 
609   RefPtr<DataSourceSurface> dataSurface = Factory::CreateDataSourceSurface(
610       IntSize(aWidth, aHeight), SurfaceFormat::B8G8R8A8,
611       /* aZero = */ true);
612   if (NS_WARN_IF(!dataSurface)) {
613     return NS_ERROR_FAILURE;
614   }
615 
616   DataSourceSurface::ScopedMap map(dataSurface, DataSourceSurface::READ_WRITE);
617   if (!map.IsMapped()) {
618     return NS_ERROR_FAILURE;
619   }
620 
621   RefPtr<DrawTarget> dt = Factory::CreateDrawTargetForData(
622       BackendType::SKIA, map.GetData(), dataSurface->GetSize(), map.GetStride(),
623       SurfaceFormat::B8G8R8A8);
624   if (!dt) {
625     gfxWarning()
626         << "imgTools::EncodeCroppedImage failed in CreateDrawTargetForData";
627     return NS_ERROR_OUT_OF_MEMORY;
628   }
629   dt->CopySurface(frame, IntRect(aOffsetX, aOffsetY, aWidth, aHeight),
630                   IntPoint(0, 0));
631 
632   return EncodeImageData(dataSurface, map, aMimeType, aOutputOptions, aStream);
633 }
634 
635 NS_IMETHODIMP
CreateScriptedObserver(imgIScriptedNotificationObserver * aInner,imgINotificationObserver ** aObserver)636 imgTools::CreateScriptedObserver(imgIScriptedNotificationObserver* aInner,
637                                  imgINotificationObserver** aObserver) {
638   NS_ADDREF(*aObserver = new ScriptedNotificationObserver(aInner));
639   return NS_OK;
640 }
641 
642 NS_IMETHODIMP
GetImgLoaderForDocument(dom::Document * aDoc,imgILoader ** aLoader)643 imgTools::GetImgLoaderForDocument(dom::Document* aDoc, imgILoader** aLoader) {
644   NS_IF_ADDREF(*aLoader = nsContentUtils::GetImgLoaderForDocument(aDoc));
645   return NS_OK;
646 }
647 
648 NS_IMETHODIMP
GetImgCacheForDocument(dom::Document * aDoc,imgICache ** aCache)649 imgTools::GetImgCacheForDocument(dom::Document* aDoc, imgICache** aCache) {
650   nsCOMPtr<imgILoader> loader;
651   nsresult rv = GetImgLoaderForDocument(aDoc, getter_AddRefs(loader));
652   NS_ENSURE_SUCCESS(rv, rv);
653   return CallQueryInterface(loader, aCache);
654 }
655 
656 }  // namespace image
657 }  // namespace mozilla
658