Bug 1686838 [wpt PR 27194] - Update wpt metadata, a=testonly
[gecko.git] / image / imgTools.cpp
blobda05636e5fee908253697771e82471ee5fec306a
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 #include "imgTools.h"
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"
39 using namespace mozilla::gfx;
41 namespace mozilla {
42 namespace image {
44 namespace {
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!");
53 if (count > 0) {
54 imgLoader::GetMimeTypeFromContent(fromRawSegment, count, *mimeType);
57 *writeCount = 0;
58 return NS_ERROR_FAILURE;
61 class ImageDecoderListener final : public nsIStreamListener,
62 public IProgressObserver,
63 public imgIContainer {
64 public:
65 NS_DECL_ISUPPORTS
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());
76 NS_IMETHOD
77 OnDataAvailable(nsIRequest* aRequest, nsIInputStream* aInputStream,
78 uint64_t aOffset, uint32_t aCount) override {
79 if (!mImage) {
80 nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
82 nsCString mimeType;
83 channel->GetContentType(mimeType);
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);
93 RefPtr<ProgressTracker> tracker = new ProgressTracker();
94 if (mObserver) {
95 tracker->AddObserver(this);
98 mImage = ImageFactory::CreateImage(channel, tracker, mimeType, mURI,
99 /* aIsMultiPart */ false, 0);
101 if (mImage->HasError()) {
102 return NS_ERROR_FAILURE;
106 return mImage->OnImageDataAvailable(aRequest, nullptr, aInputStream,
107 aOffset, aCount);
110 NS_IMETHOD
111 OnStartRequest(nsIRequest* aRequest) override { return NS_OK; }
113 NS_IMETHOD
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;
121 mImage->OnImageDataComplete(aRequest, nullptr, aStatus, true);
122 nsCOMPtr<imgIContainer> container = this;
123 mCallback->OnImageReady(container, aStatus);
124 return NS_OK;
127 virtual void Notify(int32_t aType,
128 const nsIntRect* aRect = nullptr) override {
129 if (mObserver) {
130 mObserver->Notify(nullptr, aType, aRect);
134 virtual void OnLoadComplete(bool aLastPart) override {}
136 // Other notifications are ignored.
137 virtual void SetHasImage() override {}
138 virtual bool NotificationsDeferred() const override { return false; }
139 virtual void MarkPendingNotify() override {}
140 virtual void ClearPendingNotify() override {}
142 // imgIContainer
143 NS_FORWARD_IMGICONTAINER(mImage->)
145 nsresult GetNativeSizes(nsTArray<nsIntSize>& aNativeSizes) const override {
146 return mImage->GetNativeSizes(aNativeSizes);
149 size_t GetNativeSizesLength() const override {
150 return mImage->GetNativeSizesLength();
153 private:
154 virtual ~ImageDecoderListener() = default;
156 nsCOMPtr<nsIURI> mURI;
157 RefPtr<image::Image> mImage;
158 nsCOMPtr<imgIContainerCallback> mCallback;
159 nsCOMPtr<imgINotificationObserver> mObserver;
162 NS_IMPL_ISUPPORTS(ImageDecoderListener, nsIStreamListener, imgIContainer)
164 class ImageDecoderHelper final : public Runnable,
165 public nsIInputStreamCallback {
166 public:
167 NS_DECL_ISUPPORTS_INHERITED
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());
184 NS_IMETHOD
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);
193 RefPtr<ProgressTracker> tracker = mImage->GetProgressTracker();
194 tracker->SyncNotifyProgress(FLAG_LOAD_COMPLETE);
196 nsCOMPtr<imgIContainer> container;
197 if (NS_SUCCEEDED(mStatus)) {
198 container = mImage;
201 mCallback->OnImageReady(container, mStatus);
202 return NS_OK;
205 uint64_t length;
206 nsresult rv = mInputStream->Available(&length);
207 if (rv == NS_BASE_STREAM_CLOSED) {
208 return OperationCompleted(NS_OK);
211 if (NS_WARN_IF(NS_FAILED(rv))) {
212 return OperationCompleted(rv);
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);
224 return NS_OK;
227 // We really have nothing else to read.
228 if (length == 0) {
229 return OperationCompleted(NS_OK);
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);
240 rv = mEventTarget->Dispatch(this, NS_DISPATCH_NORMAL);
241 if (NS_WARN_IF(NS_FAILED(rv))) {
242 return OperationCompleted(rv);
245 return NS_OK;
248 NS_IMETHOD
249 OnInputStreamReady(nsIAsyncInputStream* aAsyncInputStream) override {
250 MOZ_ASSERT(!NS_IsMainThread());
251 return Run();
254 nsresult OperationCompleted(nsresult aStatus) {
255 MOZ_ASSERT(!NS_IsMainThread());
257 mStatus = aStatus;
258 mCallbackEventTarget->Dispatch(this, NS_DISPATCH_NORMAL);
259 return NS_OK;
262 private:
263 ~ImageDecoderHelper() {
264 SurfaceCache::ReleaseImageOnMainThread(mImage.forget());
265 NS_ReleaseOnMainThread("ImageDecoderHelper::mCallback", mCallback.forget());
268 RefPtr<image::Image> mImage;
270 nsCOMPtr<nsIInputStream> mInputStream;
271 nsCOMPtr<nsIEventTarget> mEventTarget;
272 nsCOMPtr<imgIContainerCallback> mCallback;
273 nsCOMPtr<nsIEventTarget> mCallbackEventTarget;
275 nsresult mStatus;
278 NS_IMPL_ISUPPORTS_INHERITED(ImageDecoderHelper, Runnable,
279 nsIInputStreamCallback)
281 } // namespace
283 /* ========== imgITools implementation ========== */
285 NS_IMPL_ISUPPORTS(imgTools, imgITools)
287 imgTools::imgTools() { /* member initializers and constructor code */
290 imgTools::~imgTools() { /* destructor code */
293 NS_IMETHODIMP
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;
302 JS::Rooted<JSObject*> obj(aCx,
303 JS::UnwrapArrayBuffer(&aArrayBuffer.toObject()));
304 if (!obj) {
305 return NS_ERROR_FAILURE;
308 uint8_t* bufferData = nullptr;
309 uint32_t bufferLength = 0;
310 bool isSharedMemory = false;
312 JS::GetArrayBufferLengthAndData(obj, &bufferLength, &isSharedMemory,
313 &bufferData);
314 return DecodeImageFromBuffer((char*)bufferData, bufferLength, aMimeType,
315 aContainer);
318 NS_IMETHODIMP
319 imgTools::DecodeImageFromBuffer(const char* aBuffer, uint32_t aSize,
320 const nsACString& aMimeType,
321 imgIContainer** aContainer) {
322 MOZ_ASSERT(NS_IsMainThread());
324 NS_ENSURE_ARG_POINTER(aBuffer);
326 // Create a new image container to hold the decoded data.
327 nsAutoCString mimeType(aMimeType);
328 RefPtr<image::Image> image =
329 ImageFactory::CreateAnonymousImage(mimeType, aSize);
330 RefPtr<ProgressTracker> tracker = image->GetProgressTracker();
332 if (image->HasError()) {
333 return NS_ERROR_FAILURE;
336 // Let's create a temporary inputStream.
337 nsCOMPtr<nsIInputStream> stream;
338 nsresult rv = NS_NewByteInputStream(
339 getter_AddRefs(stream), Span(aBuffer, aSize), NS_ASSIGNMENT_DEPEND);
340 NS_ENSURE_SUCCESS(rv, rv);
341 MOZ_ASSERT(stream);
342 MOZ_ASSERT(NS_InputStreamIsBuffered(stream));
344 rv = image->OnImageDataAvailable(nullptr, nullptr, stream, 0, aSize);
345 NS_ENSURE_SUCCESS(rv, rv);
347 // Let the Image know we've sent all the data.
348 rv = image->OnImageDataComplete(nullptr, nullptr, NS_OK, true);
349 tracker->SyncNotifyProgress(FLAG_LOAD_COMPLETE);
350 NS_ENSURE_SUCCESS(rv, rv);
352 // All done.
353 image.forget(aContainer);
354 return NS_OK;
357 NS_IMETHODIMP
358 imgTools::DecodeImageFromChannelAsync(nsIURI* aURI, nsIChannel* aChannel,
359 imgIContainerCallback* aCallback,
360 imgINotificationObserver* aObserver) {
361 MOZ_ASSERT(NS_IsMainThread());
363 NS_ENSURE_ARG_POINTER(aURI);
364 NS_ENSURE_ARG_POINTER(aChannel);
365 NS_ENSURE_ARG_POINTER(aCallback);
367 RefPtr<ImageDecoderListener> listener =
368 new ImageDecoderListener(aURI, aCallback, aObserver);
370 return aChannel->AsyncOpen(listener);
373 NS_IMETHODIMP
374 imgTools::DecodeImageAsync(nsIInputStream* aInStr, const nsACString& aMimeType,
375 imgIContainerCallback* aCallback,
376 nsIEventTarget* aEventTarget) {
377 MOZ_ASSERT(NS_IsMainThread());
379 NS_ENSURE_ARG_POINTER(aInStr);
380 NS_ENSURE_ARG_POINTER(aCallback);
381 NS_ENSURE_ARG_POINTER(aEventTarget);
383 nsresult rv;
385 // Let's continuing the reading on a separate thread.
386 DecodePool* decodePool = DecodePool::Singleton();
387 MOZ_ASSERT(decodePool);
389 RefPtr<nsIEventTarget> target = decodePool->GetIOEventTarget();
390 NS_ENSURE_TRUE(target, NS_ERROR_FAILURE);
392 // Prepare the input stream.
393 nsCOMPtr<nsIInputStream> stream = aInStr;
394 if (!NS_InputStreamIsBuffered(aInStr)) {
395 nsCOMPtr<nsIInputStream> bufStream;
396 rv = NS_NewBufferedInputStream(getter_AddRefs(bufStream), stream.forget(),
397 1024);
398 NS_ENSURE_SUCCESS(rv, rv);
399 stream = std::move(bufStream);
402 // Create a new image container to hold the decoded data.
403 nsAutoCString mimeType(aMimeType);
404 RefPtr<image::Image> image = ImageFactory::CreateAnonymousImage(mimeType, 0);
406 // Already an error?
407 if (image->HasError()) {
408 return NS_ERROR_FAILURE;
411 RefPtr<ImageDecoderHelper> helper = new ImageDecoderHelper(
412 image.forget(), stream.forget(), target, aCallback, aEventTarget);
413 rv = target->Dispatch(helper.forget(), NS_DISPATCH_NORMAL);
414 NS_ENSURE_SUCCESS(rv, rv);
416 return NS_OK;
420 * This takes a DataSourceSurface rather than a SourceSurface because some
421 * of the callers have a DataSourceSurface and we don't want to call
422 * GetDataSurface on such surfaces since that may incur a conversion to
423 * SurfaceType::DATA which we don't need.
425 static nsresult EncodeImageData(DataSourceSurface* aDataSurface,
426 DataSourceSurface::ScopedMap& aMap,
427 const nsACString& aMimeType,
428 const nsAString& aOutputOptions,
429 nsIInputStream** aStream) {
430 MOZ_ASSERT(aDataSurface->GetFormat() == SurfaceFormat::B8G8R8A8 ||
431 aDataSurface->GetFormat() == SurfaceFormat::B8G8R8X8,
432 "We're assuming B8G8R8A8/X8");
434 // Get an image encoder for the media type
435 nsAutoCString encoderCID("@mozilla.org/image/encoder;2?type="_ns + aMimeType);
437 nsCOMPtr<imgIEncoder> encoder = do_CreateInstance(encoderCID.get());
438 if (!encoder) {
439 return NS_IMAGELIB_ERROR_NO_ENCODER;
442 IntSize size = aDataSurface->GetSize();
443 uint32_t dataLength = aMap.GetStride() * size.height;
445 // Encode the bitmap
446 nsresult rv = encoder->InitFromData(
447 aMap.GetData(), dataLength, size.width, size.height, aMap.GetStride(),
448 imgIEncoder::INPUT_FORMAT_HOSTARGB, aOutputOptions);
449 NS_ENSURE_SUCCESS(rv, rv);
451 encoder.forget(aStream);
452 return NS_OK;
455 static nsresult EncodeImageData(DataSourceSurface* aDataSurface,
456 const nsACString& aMimeType,
457 const nsAString& aOutputOptions,
458 nsIInputStream** aStream) {
459 DataSourceSurface::ScopedMap map(aDataSurface, DataSourceSurface::READ);
460 if (!map.IsMapped()) {
461 return NS_ERROR_FAILURE;
464 return EncodeImageData(aDataSurface, map, aMimeType, aOutputOptions, aStream);
467 NS_IMETHODIMP
468 imgTools::EncodeImage(imgIContainer* aContainer, const nsACString& aMimeType,
469 const nsAString& aOutputOptions,
470 nsIInputStream** aStream) {
471 // Use frame 0 from the image container.
472 RefPtr<SourceSurface> frame = aContainer->GetFrame(
473 imgIContainer::FRAME_FIRST,
474 imgIContainer::FLAG_SYNC_DECODE | imgIContainer::FLAG_ASYNC_NOTIFY);
475 NS_ENSURE_TRUE(frame, NS_ERROR_FAILURE);
477 RefPtr<DataSourceSurface> dataSurface;
479 if (frame->GetFormat() == SurfaceFormat::B8G8R8A8 ||
480 frame->GetFormat() == SurfaceFormat::B8G8R8X8) {
481 dataSurface = frame->GetDataSurface();
482 } else {
483 // Convert format to SurfaceFormat::B8G8R8A8
484 dataSurface = gfxUtils::CopySurfaceToDataSourceSurfaceWithFormat(
485 frame, SurfaceFormat::B8G8R8A8);
488 NS_ENSURE_TRUE(dataSurface, NS_ERROR_FAILURE);
490 return EncodeImageData(dataSurface, aMimeType, aOutputOptions, aStream);
493 NS_IMETHODIMP
494 imgTools::EncodeScaledImage(imgIContainer* aContainer,
495 const nsACString& aMimeType, int32_t aScaledWidth,
496 int32_t aScaledHeight,
497 const nsAString& aOutputOptions,
498 nsIInputStream** aStream) {
499 NS_ENSURE_ARG(aScaledWidth >= 0 && aScaledHeight >= 0);
501 // If no scaled size is specified, we'll just encode the image at its
502 // original size (no scaling).
503 if (aScaledWidth == 0 && aScaledHeight == 0) {
504 return EncodeImage(aContainer, aMimeType, aOutputOptions, aStream);
507 // Retrieve the image's size.
508 int32_t imageWidth = 0;
509 int32_t imageHeight = 0;
510 aContainer->GetWidth(&imageWidth);
511 aContainer->GetHeight(&imageHeight);
513 // If the given width or height is zero we'll replace it with the image's
514 // original dimensions.
515 IntSize scaledSize(aScaledWidth == 0 ? imageWidth : aScaledWidth,
516 aScaledHeight == 0 ? imageHeight : aScaledHeight);
518 // Use frame 0 from the image container.
519 RefPtr<SourceSurface> frame = aContainer->GetFrameAtSize(
520 scaledSize, imgIContainer::FRAME_FIRST,
521 imgIContainer::FLAG_HIGH_QUALITY_SCALING |
522 imgIContainer::FLAG_SYNC_DECODE | imgIContainer::FLAG_ASYNC_NOTIFY);
523 NS_ENSURE_TRUE(frame, NS_ERROR_FAILURE);
525 // If the given surface is the right size/format, we can encode it directly.
526 if (scaledSize == frame->GetSize() &&
527 (frame->GetFormat() == SurfaceFormat::B8G8R8A8 ||
528 frame->GetFormat() == SurfaceFormat::B8G8R8X8)) {
529 RefPtr<DataSourceSurface> dataSurface = frame->GetDataSurface();
530 if (dataSurface) {
531 return EncodeImageData(dataSurface, aMimeType, aOutputOptions, aStream);
535 // Otherwise we need to scale it using a draw target.
536 RefPtr<DataSourceSurface> dataSurface =
537 Factory::CreateDataSourceSurface(scaledSize, SurfaceFormat::B8G8R8A8);
538 if (NS_WARN_IF(!dataSurface)) {
539 return NS_ERROR_FAILURE;
542 DataSourceSurface::ScopedMap map(dataSurface, DataSourceSurface::READ_WRITE);
543 if (!map.IsMapped()) {
544 return NS_ERROR_FAILURE;
547 RefPtr<DrawTarget> dt = Factory::CreateDrawTargetForData(
548 BackendType::SKIA, map.GetData(), dataSurface->GetSize(), map.GetStride(),
549 SurfaceFormat::B8G8R8A8);
550 if (!dt) {
551 gfxWarning() << "imgTools::EncodeImage failed in CreateDrawTargetForData";
552 return NS_ERROR_OUT_OF_MEMORY;
555 IntSize frameSize = frame->GetSize();
556 dt->DrawSurface(frame, Rect(0, 0, scaledSize.width, scaledSize.height),
557 Rect(0, 0, frameSize.width, frameSize.height),
558 DrawSurfaceOptions(),
559 DrawOptions(1.0f, CompositionOp::OP_SOURCE));
561 return EncodeImageData(dataSurface, map, aMimeType, aOutputOptions, aStream);
564 NS_IMETHODIMP
565 imgTools::EncodeCroppedImage(imgIContainer* aContainer,
566 const nsACString& aMimeType, int32_t aOffsetX,
567 int32_t aOffsetY, int32_t aWidth, int32_t aHeight,
568 const nsAString& aOutputOptions,
569 nsIInputStream** aStream) {
570 NS_ENSURE_ARG(aOffsetX >= 0 && aOffsetY >= 0 && aWidth >= 0 && aHeight >= 0);
572 // Offsets must be zero when no width and height are given or else we're out
573 // of bounds.
574 NS_ENSURE_ARG(aWidth + aHeight > 0 || aOffsetX + aOffsetY == 0);
576 // If no size is specified then we'll preserve the image's original dimensions
577 // and don't need to crop.
578 if (aWidth == 0 && aHeight == 0) {
579 return EncodeImage(aContainer, aMimeType, aOutputOptions, aStream);
582 // Use frame 0 from the image container.
583 RefPtr<SourceSurface> frame = aContainer->GetFrame(
584 imgIContainer::FRAME_FIRST,
585 imgIContainer::FLAG_SYNC_DECODE | imgIContainer::FLAG_ASYNC_NOTIFY);
586 NS_ENSURE_TRUE(frame, NS_ERROR_FAILURE);
588 int32_t frameWidth = frame->GetSize().width;
589 int32_t frameHeight = frame->GetSize().height;
591 // If the given width or height is zero we'll replace it with the image's
592 // original dimensions.
593 if (aWidth == 0) {
594 aWidth = frameWidth;
595 } else if (aHeight == 0) {
596 aHeight = frameHeight;
599 // Check that the given crop rectangle is within image bounds.
600 NS_ENSURE_ARG(frameWidth >= aOffsetX + aWidth &&
601 frameHeight >= aOffsetY + aHeight);
603 RefPtr<DataSourceSurface> dataSurface = Factory::CreateDataSourceSurface(
604 IntSize(aWidth, aHeight), SurfaceFormat::B8G8R8A8,
605 /* aZero = */ true);
606 if (NS_WARN_IF(!dataSurface)) {
607 return NS_ERROR_FAILURE;
610 DataSourceSurface::ScopedMap map(dataSurface, DataSourceSurface::READ_WRITE);
611 if (!map.IsMapped()) {
612 return NS_ERROR_FAILURE;
615 RefPtr<DrawTarget> dt = Factory::CreateDrawTargetForData(
616 BackendType::SKIA, map.GetData(), dataSurface->GetSize(), map.GetStride(),
617 SurfaceFormat::B8G8R8A8);
618 if (!dt) {
619 gfxWarning()
620 << "imgTools::EncodeCroppedImage failed in CreateDrawTargetForData";
621 return NS_ERROR_OUT_OF_MEMORY;
623 dt->CopySurface(frame, IntRect(aOffsetX, aOffsetY, aWidth, aHeight),
624 IntPoint(0, 0));
626 return EncodeImageData(dataSurface, map, aMimeType, aOutputOptions, aStream);
629 NS_IMETHODIMP
630 imgTools::CreateScriptedObserver(imgIScriptedNotificationObserver* aInner,
631 imgINotificationObserver** aObserver) {
632 NS_ADDREF(*aObserver = new ScriptedNotificationObserver(aInner));
633 return NS_OK;
636 NS_IMETHODIMP
637 imgTools::GetImgLoaderForDocument(dom::Document* aDoc, imgILoader** aLoader) {
638 NS_IF_ADDREF(*aLoader = nsContentUtils::GetImgLoaderForDocument(aDoc));
639 return NS_OK;
642 NS_IMETHODIMP
643 imgTools::GetImgCacheForDocument(dom::Document* aDoc, imgICache** aCache) {
644 nsCOMPtr<imgILoader> loader;
645 nsresult rv = GetImgLoaderForDocument(aDoc, getter_AddRefs(loader));
646 NS_ENSURE_SUCCESS(rv, rv);
647 return CallQueryInterface(loader, aCache);
650 } // namespace image
651 } // namespace mozilla