2008-01-13 07:15:20 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2008-01-13 07:15:20 +03:00
|
|
|
|
|
|
|
#include "imgTools.h"
|
2013-03-17 11:55:15 +04:00
|
|
|
|
2017-11-27 19:05:57 +03:00
|
|
|
#include "DecodePool.h"
|
2014-04-15 22:02:23 +04:00
|
|
|
#include "gfxUtils.h"
|
2014-02-25 04:51:45 +04:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2017-03-27 14:49:21 +03:00
|
|
|
#include "mozilla/gfx/Logging.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2008-01-13 07:15:20 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2012-11-16 22:24:58 +04:00
|
|
|
#include "imgLoader.h"
|
2012-06-26 08:20:12 +04:00
|
|
|
#include "imgICache.h"
|
2008-01-13 07:15:20 +03:00
|
|
|
#include "imgIContainer.h"
|
|
|
|
#include "imgIEncoder.h"
|
2018-06-06 03:42:56 +03:00
|
|
|
#include "nsNetUtil.h" // for NS_NewBufferedInputStream
|
2009-07-22 02:57:25 +04:00
|
|
|
#include "nsStreamUtils.h"
|
2017-11-27 19:05:57 +03:00
|
|
|
#include "nsStringStream.h"
|
2012-06-26 08:20:12 +04:00
|
|
|
#include "nsContentUtils.h"
|
2017-11-27 19:05:57 +03:00
|
|
|
#include "nsProxyRelease.h"
|
2012-12-18 05:35:07 +04:00
|
|
|
#include "ImageFactory.h"
|
2013-02-14 01:53:42 +04:00
|
|
|
#include "Image.h"
|
2012-10-12 20:11:22 +04:00
|
|
|
#include "ScriptedNotificationObserver.h"
|
|
|
|
#include "imgIScriptedNotificationObserver.h"
|
2013-10-16 05:00:31 +04:00
|
|
|
#include "gfxPlatform.h"
|
2019-03-05 02:19:16 +03:00
|
|
|
#include "js/ArrayBuffer.h"
|
|
|
|
#include "js/RootingAPI.h" // JS::{Handle,Rooted}
|
|
|
|
#include "js/Value.h" // JS::Value
|
2008-01-13 07:15:20 +03:00
|
|
|
|
2013-10-16 05:00:31 +04:00
|
|
|
using namespace mozilla::gfx;
|
2010-08-14 08:09:49 +04:00
|
|
|
|
2015-09-04 00:00:00 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace image {
|
2017-11-27 19:05:57 +03:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ImageDecoderHelper final : public Runnable,
|
|
|
|
public nsIInputStreamCallback {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
|
|
|
ImageDecoderHelper(already_AddRefed<image::Image> aImage,
|
|
|
|
already_AddRefed<nsIInputStream> aInputStream,
|
|
|
|
nsIEventTarget* aEventTarget,
|
|
|
|
imgIContainerCallback* aCallback,
|
|
|
|
nsIEventTarget* aCallbackEventTarget)
|
|
|
|
: Runnable("ImageDecoderHelper"),
|
2018-05-30 22:15:35 +03:00
|
|
|
mImage(std::move(aImage)),
|
|
|
|
mInputStream(std::move(aInputStream)),
|
2017-11-27 19:05:57 +03:00
|
|
|
mEventTarget(aEventTarget),
|
|
|
|
mCallback(aCallback),
|
|
|
|
mCallbackEventTarget(aCallbackEventTarget),
|
|
|
|
mStatus(NS_OK) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override {
|
|
|
|
// This runnable is dispatched on the Image thread when reading data, but
|
|
|
|
// at the end, it goes back to the main-thread in order to complete the
|
|
|
|
// operation.
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
// Let the Image know we've sent all the data.
|
|
|
|
mImage->OnImageDataComplete(nullptr, nullptr, mStatus, true);
|
|
|
|
|
|
|
|
RefPtr<ProgressTracker> tracker = mImage->GetProgressTracker();
|
|
|
|
tracker->SyncNotifyProgress(FLAG_LOAD_COMPLETE);
|
|
|
|
|
|
|
|
nsCOMPtr<imgIContainer> container;
|
|
|
|
if (NS_SUCCEEDED(mStatus)) {
|
2018-10-02 00:38:01 +03:00
|
|
|
container = mImage;
|
2017-11-27 19:05:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mCallback->OnImageReady(container, mStatus);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t length;
|
|
|
|
nsresult rv = mInputStream->Available(&length);
|
|
|
|
if (rv == NS_BASE_STREAM_CLOSED) {
|
|
|
|
return OperationCompleted(NS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return OperationCompleted(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing else to read, but maybe we just need to wait.
|
|
|
|
if (length == 0) {
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> asyncInputStream =
|
|
|
|
do_QueryInterface(mInputStream);
|
|
|
|
if (asyncInputStream) {
|
|
|
|
rv = asyncInputStream->AsyncWait(this, 0, 0, mEventTarget);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return OperationCompleted(rv);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We really have nothing else to read.
|
|
|
|
if (length == 0) {
|
|
|
|
return OperationCompleted(NS_OK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the source data to the Image.
|
|
|
|
rv = mImage->OnImageDataAvailable(nullptr, nullptr, mInputStream, 0,
|
|
|
|
uint32_t(length));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return OperationCompleted(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = mEventTarget->Dispatch(this, NS_DISPATCH_NORMAL);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return OperationCompleted(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
OnInputStreamReady(nsIAsyncInputStream* aAsyncInputStream) override {
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
return Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult OperationCompleted(nsresult aStatus) {
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
|
|
|
mStatus = aStatus;
|
|
|
|
mCallbackEventTarget->Dispatch(this, NS_DISPATCH_NORMAL);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~ImageDecoderHelper() {
|
|
|
|
NS_ReleaseOnMainThreadSystemGroup("ImageDecoderHelper::mImage",
|
|
|
|
mImage.forget());
|
|
|
|
NS_ReleaseOnMainThreadSystemGroup("ImageDecoderHelper::mCallback",
|
|
|
|
mCallback.forget());
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<image::Image> mImage;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> mInputStream;
|
|
|
|
nsCOMPtr<nsIEventTarget> mEventTarget;
|
|
|
|
nsCOMPtr<imgIContainerCallback> mCallback;
|
|
|
|
nsCOMPtr<nsIEventTarget> mCallbackEventTarget;
|
|
|
|
|
|
|
|
nsresult mStatus;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(ImageDecoderHelper, Runnable,
|
|
|
|
nsIInputStreamCallback)
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2008-01-13 07:15:20 +03:00
|
|
|
/* ========== imgITools implementation ========== */
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(imgTools, imgITools)
|
2008-01-13 07:15:20 +03:00
|
|
|
|
|
|
|
imgTools::imgTools() { /* member initializers and constructor code */
|
|
|
|
}
|
|
|
|
|
|
|
|
imgTools::~imgTools() { /* destructor code */
|
|
|
|
}
|
|
|
|
|
2017-11-30 16:48:47 +03:00
|
|
|
NS_IMETHODIMP
|
2019-03-05 02:19:16 +03:00
|
|
|
imgTools::DecodeImageFromArrayBuffer(JS::Handle<JS::Value> aArrayBuffer,
|
2017-11-30 16:48:47 +03:00
|
|
|
const nsACString& aMimeType,
|
|
|
|
JSContext* aCx,
|
|
|
|
imgIContainer** aContainer) {
|
|
|
|
if (!aArrayBuffer.isObject()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> obj(aCx,
|
2019-03-05 02:19:16 +03:00
|
|
|
JS::UnwrapArrayBuffer(&aArrayBuffer.toObject()));
|
2017-11-30 16:48:47 +03:00
|
|
|
if (!obj) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* bufferData = nullptr;
|
|
|
|
uint32_t bufferLength = 0;
|
|
|
|
bool isSharedMemory = false;
|
|
|
|
|
2019-03-05 02:19:16 +03:00
|
|
|
JS::GetArrayBufferLengthAndData(obj, &bufferLength, &isSharedMemory,
|
2017-11-30 16:48:47 +03:00
|
|
|
&bufferData);
|
2017-11-30 16:48:47 +03:00
|
|
|
return DecodeImageFromBuffer((char*)bufferData, bufferLength, aMimeType,
|
|
|
|
aContainer);
|
2017-11-30 16:48:47 +03:00
|
|
|
}
|
|
|
|
|
2015-04-02 23:41:00 +03:00
|
|
|
NS_IMETHODIMP
|
2017-11-30 16:48:47 +03:00
|
|
|
imgTools::DecodeImageFromBuffer(const char* aBuffer, uint32_t aSize,
|
|
|
|
const nsACString& aMimeType,
|
|
|
|
imgIContainer** aContainer) {
|
2015-08-01 04:10:31 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2017-11-27 19:05:57 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aBuffer);
|
2008-01-13 07:15:20 +03:00
|
|
|
|
2017-08-01 13:59:12 +03:00
|
|
|
// Create a new image container to hold the decoded data.
|
|
|
|
nsAutoCString mimeType(aMimeType);
|
|
|
|
RefPtr<image::Image> image =
|
2017-11-27 19:05:57 +03:00
|
|
|
ImageFactory::CreateAnonymousImage(mimeType, aSize);
|
2017-08-01 13:59:12 +03:00
|
|
|
RefPtr<ProgressTracker> tracker = image->GetProgressTracker();
|
|
|
|
|
|
|
|
if (image->HasError()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2017-11-27 19:05:57 +03:00
|
|
|
// Let's create a temporary inputStream.
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2019-02-25 22:11:20 +03:00
|
|
|
nsresult rv = NS_NewByteInputStream(
|
|
|
|
getter_AddRefs(stream), MakeSpan(aBuffer, aSize), NS_ASSIGNMENT_DEPEND);
|
2017-11-27 19:05:57 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
MOZ_ASSERT(stream);
|
|
|
|
MOZ_ASSERT(NS_InputStreamIsBuffered(stream));
|
|
|
|
|
|
|
|
rv = image->OnImageDataAvailable(nullptr, nullptr, stream, 0, aSize);
|
2008-01-13 07:15:20 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-11-15 07:10:48 +03:00
|
|
|
|
2012-12-18 05:35:07 +04:00
|
|
|
// Let the Image know we've sent all the data.
|
2013-02-14 06:41:10 +04:00
|
|
|
rv = image->OnImageDataComplete(nullptr, nullptr, NS_OK, true);
|
2014-11-18 01:29:56 +03:00
|
|
|
tracker->SyncNotifyProgress(FLAG_LOAD_COMPLETE);
|
2009-09-13 02:44:18 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-12-18 05:35:07 +04:00
|
|
|
// All done.
|
2017-11-27 19:05:57 +03:00
|
|
|
image.forget(aContainer);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
imgTools::DecodeImageAsync(nsIInputStream* aInStr, const nsACString& aMimeType,
|
|
|
|
imgIContainerCallback* aCallback,
|
|
|
|
nsIEventTarget* aEventTarget) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
NS_ENSURE_ARG_POINTER(aInStr);
|
|
|
|
NS_ENSURE_ARG_POINTER(aCallback);
|
|
|
|
NS_ENSURE_ARG_POINTER(aEventTarget);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// Let's continuing the reading on a separate thread.
|
|
|
|
DecodePool* decodePool = DecodePool::Singleton();
|
|
|
|
MOZ_ASSERT(decodePool);
|
|
|
|
|
|
|
|
RefPtr<nsIEventTarget> target = decodePool->GetIOEventTarget();
|
|
|
|
NS_ENSURE_TRUE(target, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
// Prepare the input stream.
|
|
|
|
nsCOMPtr<nsIInputStream> stream = aInStr;
|
|
|
|
if (!NS_InputStreamIsBuffered(aInStr)) {
|
|
|
|
nsCOMPtr<nsIInputStream> bufStream;
|
|
|
|
rv = NS_NewBufferedInputStream(getter_AddRefs(bufStream), stream.forget(),
|
|
|
|
1024);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
stream = bufStream.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new image container to hold the decoded data.
|
|
|
|
nsAutoCString mimeType(aMimeType);
|
|
|
|
RefPtr<image::Image> image = ImageFactory::CreateAnonymousImage(mimeType, 0);
|
|
|
|
|
|
|
|
// Already an error?
|
|
|
|
if (image->HasError()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<ImageDecoderHelper> helper = new ImageDecoderHelper(
|
|
|
|
image.forget(), stream.forget(), target, aCallback, aEventTarget);
|
|
|
|
rv = target->Dispatch(helper.forget(), NS_DISPATCH_NORMAL);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-01-13 07:15:20 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-25 04:51:45 +04:00
|
|
|
/**
|
|
|
|
* This takes a DataSourceSurface rather than a SourceSurface because some
|
|
|
|
* of the callers have a DataSourceSurface and we don't want to call
|
2019-09-12 12:00:52 +03:00
|
|
|
* GetDataSurface on such surfaces since that may incur a conversion to
|
2014-02-25 04:51:45 +04:00
|
|
|
* SurfaceType::DATA which we don't need.
|
|
|
|
*/
|
2015-04-02 23:41:00 +03:00
|
|
|
static nsresult EncodeImageData(DataSourceSurface* aDataSurface,
|
2018-05-25 16:02:20 +03:00
|
|
|
DataSourceSurface::ScopedMap& aMap,
|
2015-04-02 23:41:00 +03:00
|
|
|
const nsACString& aMimeType,
|
|
|
|
const nsAString& aOutputOptions,
|
|
|
|
nsIInputStream** aStream) {
|
2018-05-25 16:02:20 +03:00
|
|
|
MOZ_ASSERT(aDataSurface->GetFormat() == SurfaceFormat::B8G8R8A8 ||
|
|
|
|
aDataSurface->GetFormat() == SurfaceFormat::B8G8R8X8,
|
|
|
|
"We're assuming B8G8R8A8/X8");
|
2014-02-25 04:51:45 +04:00
|
|
|
|
|
|
|
// Get an image encoder for the media type
|
|
|
|
nsAutoCString encoderCID(
|
|
|
|
NS_LITERAL_CSTRING("@mozilla.org/image/encoder;2?type=") + aMimeType);
|
|
|
|
|
|
|
|
nsCOMPtr<imgIEncoder> encoder = do_CreateInstance(encoderCID.get());
|
2015-04-02 23:41:00 +03:00
|
|
|
if (!encoder) {
|
2014-02-25 04:51:45 +04:00
|
|
|
return NS_IMAGELIB_ERROR_NO_ENCODER;
|
2015-04-02 23:41:00 +03:00
|
|
|
}
|
2014-02-25 04:51:45 +04:00
|
|
|
|
|
|
|
IntSize size = aDataSurface->GetSize();
|
2018-05-25 16:02:20 +03:00
|
|
|
uint32_t dataLength = aMap.GetStride() * size.height;
|
2014-02-25 04:51:45 +04:00
|
|
|
|
|
|
|
// Encode the bitmap
|
2018-05-25 16:02:20 +03:00
|
|
|
nsresult rv = encoder->InitFromData(
|
|
|
|
aMap.GetData(), dataLength, size.width, size.height, aMap.GetStride(),
|
2014-02-25 04:51:45 +04:00
|
|
|
imgIEncoder::INPUT_FORMAT_HOSTARGB, aOutputOptions);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-03-12 20:20:29 +03:00
|
|
|
encoder.forget(aStream);
|
|
|
|
return NS_OK;
|
2014-02-25 04:51:45 +04:00
|
|
|
}
|
2008-01-13 07:15:20 +03:00
|
|
|
|
2018-05-25 16:02:20 +03:00
|
|
|
static nsresult EncodeImageData(DataSourceSurface* aDataSurface,
|
|
|
|
const nsACString& aMimeType,
|
|
|
|
const nsAString& aOutputOptions,
|
|
|
|
nsIInputStream** aStream) {
|
|
|
|
DataSourceSurface::ScopedMap map(aDataSurface, DataSourceSurface::READ);
|
|
|
|
if (!map.IsMapped()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EncodeImageData(aDataSurface, map, aMimeType, aOutputOptions, aStream);
|
|
|
|
}
|
|
|
|
|
2015-04-02 23:41:00 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
imgTools::EncodeImage(imgIContainer* aContainer, const nsACString& aMimeType,
|
|
|
|
const nsAString& aOutputOptions,
|
|
|
|
nsIInputStream** aStream) {
|
2012-07-03 10:22:10 +04:00
|
|
|
// Use frame 0 from the image container.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SourceSurface> frame = aContainer->GetFrame(
|
2019-09-03 18:36:23 +03:00
|
|
|
imgIContainer::FRAME_FIRST,
|
|
|
|
imgIContainer::FLAG_SYNC_DECODE | imgIContainer::FLAG_ASYNC_NOTIFY);
|
2014-02-25 04:51:45 +04:00
|
|
|
NS_ENSURE_TRUE(frame, NS_ERROR_FAILURE);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DataSourceSurface> dataSurface;
|
2014-04-15 22:02:23 +04:00
|
|
|
|
2018-05-25 16:02:20 +03:00
|
|
|
if (frame->GetFormat() == SurfaceFormat::B8G8R8A8 ||
|
|
|
|
frame->GetFormat() == SurfaceFormat::B8G8R8X8) {
|
2014-04-15 22:02:23 +04:00
|
|
|
dataSurface = frame->GetDataSurface();
|
|
|
|
} else {
|
|
|
|
// Convert format to SurfaceFormat::B8G8R8A8
|
|
|
|
dataSurface = gfxUtils::CopySurfaceToDataSourceSurfaceWithFormat(
|
|
|
|
frame, SurfaceFormat::B8G8R8A8);
|
|
|
|
}
|
|
|
|
|
2014-02-25 04:51:45 +04:00
|
|
|
NS_ENSURE_TRUE(dataSurface, NS_ERROR_FAILURE);
|
2012-07-03 10:22:10 +04:00
|
|
|
|
2014-02-25 04:51:45 +04:00
|
|
|
return EncodeImageData(dataSurface, aMimeType, aOutputOptions, aStream);
|
2008-01-13 07:15:20 +03:00
|
|
|
}
|
|
|
|
|
2015-04-02 23:41:00 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
imgTools::EncodeScaledImage(imgIContainer* aContainer,
|
|
|
|
const nsACString& aMimeType, int32_t aScaledWidth,
|
|
|
|
int32_t aScaledHeight,
|
|
|
|
const nsAString& aOutputOptions,
|
|
|
|
nsIInputStream** aStream) {
|
2012-07-03 10:22:10 +04:00
|
|
|
NS_ENSURE_ARG(aScaledWidth >= 0 && aScaledHeight >= 0);
|
2008-01-13 07:15:20 +03:00
|
|
|
|
|
|
|
// If no scaled size is specified, we'll just encode the image at its
|
|
|
|
// original size (no scaling).
|
|
|
|
if (aScaledWidth == 0 && aScaledHeight == 0) {
|
2012-07-03 10:22:10 +04:00
|
|
|
return EncodeImage(aContainer, aMimeType, aOutputOptions, aStream);
|
2008-01-13 07:15:20 +03:00
|
|
|
}
|
|
|
|
|
2015-09-19 23:34:12 +03:00
|
|
|
// Retrieve the image's size.
|
|
|
|
int32_t imageWidth = 0;
|
|
|
|
int32_t imageHeight = 0;
|
|
|
|
aContainer->GetWidth(&imageWidth);
|
|
|
|
aContainer->GetHeight(&imageHeight);
|
2015-09-18 23:01:25 +03:00
|
|
|
|
|
|
|
// If the given width or height is zero we'll replace it with the image's
|
|
|
|
// original dimensions.
|
2015-09-19 23:34:12 +03:00
|
|
|
IntSize scaledSize(aScaledWidth == 0 ? imageWidth : aScaledWidth,
|
|
|
|
aScaledHeight == 0 ? imageHeight : aScaledHeight);
|
|
|
|
|
|
|
|
// Use frame 0 from the image container.
|
2019-09-03 18:36:23 +03:00
|
|
|
RefPtr<SourceSurface> frame = aContainer->GetFrameAtSize(
|
|
|
|
scaledSize, imgIContainer::FRAME_FIRST,
|
|
|
|
imgIContainer::FLAG_HIGH_QUALITY_SCALING |
|
|
|
|
imgIContainer::FLAG_SYNC_DECODE | imgIContainer::FLAG_ASYNC_NOTIFY);
|
2015-09-19 23:34:12 +03:00
|
|
|
NS_ENSURE_TRUE(frame, NS_ERROR_FAILURE);
|
2015-09-18 23:01:25 +03:00
|
|
|
|
2018-05-25 16:02:20 +03:00
|
|
|
// If the given surface is the right size/format, we can encode it directly.
|
|
|
|
if (scaledSize == frame->GetSize() &&
|
|
|
|
(frame->GetFormat() == SurfaceFormat::B8G8R8A8 ||
|
|
|
|
frame->GetFormat() == SurfaceFormat::B8G8R8X8)) {
|
|
|
|
RefPtr<DataSourceSurface> dataSurface = frame->GetDataSurface();
|
|
|
|
if (dataSurface) {
|
|
|
|
return EncodeImageData(dataSurface, aMimeType, aOutputOptions, aStream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise we need to scale it using a draw target.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DataSourceSurface> dataSurface =
|
2015-09-19 23:34:12 +03:00
|
|
|
Factory::CreateDataSourceSurface(scaledSize, SurfaceFormat::B8G8R8A8);
|
2014-08-27 19:57:43 +04:00
|
|
|
if (NS_WARN_IF(!dataSurface)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2018-05-25 16:02:20 +03:00
|
|
|
DataSourceSurface::ScopedMap map(dataSurface, DataSourceSurface::READ_WRITE);
|
|
|
|
if (!map.IsMapped()) {
|
2014-02-25 04:51:45 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-03-07 17:21:38 +04:00
|
|
|
}
|
2012-07-03 10:22:10 +04:00
|
|
|
|
2018-05-25 16:02:20 +03:00
|
|
|
RefPtr<DrawTarget> dt = Factory::CreateDrawTargetForData(
|
2014-02-25 04:51:45 +04:00
|
|
|
BackendType::SKIA, map.GetData(), dataSurface->GetSize(), map.GetStride(),
|
|
|
|
SurfaceFormat::B8G8R8A8);
|
2015-03-09 22:48:20 +03:00
|
|
|
if (!dt) {
|
|
|
|
gfxWarning() << "imgTools::EncodeImage failed in CreateDrawTargetForData";
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2015-09-19 23:34:12 +03:00
|
|
|
IntSize frameSize = frame->GetSize();
|
|
|
|
dt->DrawSurface(frame, Rect(0, 0, scaledSize.width, scaledSize.height),
|
|
|
|
Rect(0, 0, frameSize.width, frameSize.height),
|
2014-02-25 04:51:45 +04:00
|
|
|
DrawSurfaceOptions(),
|
|
|
|
DrawOptions(1.0f, CompositionOp::OP_SOURCE));
|
|
|
|
|
2018-05-25 16:02:20 +03:00
|
|
|
return EncodeImageData(dataSurface, map, aMimeType, aOutputOptions, aStream);
|
2012-07-03 10:22:10 +04:00
|
|
|
}
|
|
|
|
|
2015-04-02 23:41:00 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
imgTools::EncodeCroppedImage(imgIContainer* aContainer,
|
|
|
|
const nsACString& aMimeType, int32_t aOffsetX,
|
|
|
|
int32_t aOffsetY, int32_t aWidth, int32_t aHeight,
|
|
|
|
const nsAString& aOutputOptions,
|
|
|
|
nsIInputStream** aStream) {
|
2012-07-03 10:22:10 +04:00
|
|
|
NS_ENSURE_ARG(aOffsetX >= 0 && aOffsetY >= 0 && aWidth >= 0 && aHeight >= 0);
|
|
|
|
|
|
|
|
// Offsets must be zero when no width and height are given or else we're out
|
|
|
|
// of bounds.
|
|
|
|
NS_ENSURE_ARG(aWidth + aHeight > 0 || aOffsetX + aOffsetY == 0);
|
|
|
|
|
|
|
|
// If no size is specified then we'll preserve the image's original dimensions
|
|
|
|
// and don't need to crop.
|
|
|
|
if (aWidth == 0 && aHeight == 0) {
|
|
|
|
return EncodeImage(aContainer, aMimeType, aOutputOptions, aStream);
|
|
|
|
}
|
2008-01-13 07:15:20 +03:00
|
|
|
|
|
|
|
// Use frame 0 from the image container.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SourceSurface> frame = aContainer->GetFrame(
|
2019-09-03 18:36:23 +03:00
|
|
|
imgIContainer::FRAME_FIRST,
|
|
|
|
imgIContainer::FLAG_SYNC_DECODE | imgIContainer::FLAG_ASYNC_NOTIFY);
|
2014-02-25 04:51:45 +04:00
|
|
|
NS_ENSURE_TRUE(frame, NS_ERROR_FAILURE);
|
2008-01-13 07:15:20 +03:00
|
|
|
|
2014-02-25 04:51:45 +04:00
|
|
|
int32_t frameWidth = frame->GetSize().width;
|
|
|
|
int32_t frameHeight = frame->GetSize().height;
|
2008-01-13 07:15:20 +03:00
|
|
|
|
2012-07-03 10:22:10 +04:00
|
|
|
// If the given width or height is zero we'll replace it with the image's
|
|
|
|
// original dimensions.
|
|
|
|
if (aWidth == 0) {
|
|
|
|
aWidth = frameWidth;
|
|
|
|
} else if (aHeight == 0) {
|
|
|
|
aHeight = frameHeight;
|
|
|
|
}
|
2008-01-13 07:15:20 +03:00
|
|
|
|
2012-07-03 10:22:10 +04:00
|
|
|
// Check that the given crop rectangle is within image bounds.
|
|
|
|
NS_ENSURE_ARG(frameWidth >= aOffsetX + aWidth &&
|
|
|
|
frameHeight >= aOffsetY + aHeight);
|
2008-01-13 07:15:20 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DataSourceSurface> dataSurface = Factory::CreateDataSourceSurface(
|
2014-02-25 04:51:45 +04:00
|
|
|
IntSize(aWidth, aHeight), SurfaceFormat::B8G8R8A8,
|
2014-09-11 01:54:16 +04:00
|
|
|
/* aZero = */ true);
|
2014-08-27 19:57:43 +04:00
|
|
|
if (NS_WARN_IF(!dataSurface)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2018-05-25 16:02:20 +03:00
|
|
|
DataSourceSurface::ScopedMap map(dataSurface, DataSourceSurface::READ_WRITE);
|
|
|
|
if (!map.IsMapped()) {
|
2012-07-03 10:22:10 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-03-07 17:21:38 +04:00
|
|
|
}
|
2012-07-03 10:22:10 +04:00
|
|
|
|
2018-05-25 16:02:20 +03:00
|
|
|
RefPtr<DrawTarget> dt = Factory::CreateDrawTargetForData(
|
2014-02-25 04:51:45 +04:00
|
|
|
BackendType::SKIA, map.GetData(), dataSurface->GetSize(), map.GetStride(),
|
|
|
|
SurfaceFormat::B8G8R8A8);
|
2015-03-09 22:48:20 +03:00
|
|
|
if (!dt) {
|
2015-04-02 23:41:00 +03:00
|
|
|
gfxWarning()
|
|
|
|
<< "imgTools::EncodeCroppedImage failed in CreateDrawTargetForData";
|
2015-03-09 22:48:20 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2014-02-25 04:51:45 +04:00
|
|
|
dt->CopySurface(frame, IntRect(aOffsetX, aOffsetY, aWidth, aHeight),
|
|
|
|
IntPoint(0, 0));
|
|
|
|
|
2018-05-25 16:02:20 +03:00
|
|
|
return EncodeImageData(dataSurface, map, aMimeType, aOutputOptions, aStream);
|
2012-07-03 10:22:10 +04:00
|
|
|
}
|
2012-06-26 08:20:12 +04:00
|
|
|
|
2015-04-02 23:41:00 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
imgTools::CreateScriptedObserver(imgIScriptedNotificationObserver* aInner,
|
|
|
|
imgINotificationObserver** aObserver) {
|
2012-10-12 20:11:22 +04:00
|
|
|
NS_ADDREF(*aObserver = new ScriptedNotificationObserver(aInner));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-26 08:20:12 +04:00
|
|
|
NS_IMETHODIMP
|
2019-01-02 16:05:23 +03:00
|
|
|
imgTools::GetImgLoaderForDocument(dom::Document* aDoc, imgILoader** aLoader) {
|
2018-05-11 20:46:15 +03:00
|
|
|
NS_IF_ADDREF(*aLoader = nsContentUtils::GetImgLoaderForDocument(aDoc));
|
2012-06-26 08:20:12 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-01-02 16:05:23 +03:00
|
|
|
imgTools::GetImgCacheForDocument(dom::Document* aDoc, imgICache** aCache) {
|
2012-06-26 08:20:12 +04:00
|
|
|
nsCOMPtr<imgILoader> loader;
|
|
|
|
nsresult rv = GetImgLoaderForDocument(aDoc, getter_AddRefs(loader));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return CallQueryInterface(loader, aCache);
|
|
|
|
}
|
2015-09-04 00:00:00 +03:00
|
|
|
|
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|