2013-04-05 21:14:34 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
|
|
|
* 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/. */
|
|
|
|
|
2015-08-01 01:10:31 +00:00
|
|
|
#include "ImageOps.h"
|
|
|
|
|
2013-04-05 21:14:34 +00:00
|
|
|
#include "ClippedImage.h"
|
2015-08-01 01:10:31 +00:00
|
|
|
#include "Decoder.h"
|
|
|
|
#include "DecoderFactory.h"
|
2014-07-22 00:59:22 +00:00
|
|
|
#include "DynamicImage.h"
|
2013-04-05 21:14:34 +00:00
|
|
|
#include "FrozenImage.h"
|
2016-06-29 20:43:19 +00:00
|
|
|
#include "IDecodingTask.h"
|
2013-04-05 21:14:34 +00:00
|
|
|
#include "Image.h"
|
2017-03-22 13:05:36 +00:00
|
|
|
#include "ImageMetadata.h"
|
2015-08-01 01:10:31 +00:00
|
|
|
#include "imgIContainer.h"
|
2015-08-01 01:10:34 +00:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2018-06-06 00:42:56 +00:00
|
|
|
#include "nsNetUtil.h" // for NS_NewBufferedInputStream
|
2015-08-01 01:10:31 +00:00
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
#include "OrientedImage.h"
|
|
|
|
#include "SourceBuffer.h"
|
2013-04-05 21:14:34 +00:00
|
|
|
|
2015-08-01 01:10:31 +00:00
|
|
|
using namespace mozilla::gfx;
|
2013-04-05 21:14:34 +00:00
|
|
|
|
2021-04-11 17:39:12 +00:00
|
|
|
namespace mozilla::image {
|
2013-04-05 21:14:34 +00:00
|
|
|
|
2019-02-25 22:07:58 +00:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<Image> ImageOps::Freeze(Image* aImage) {
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Image> frozenImage = new FrozenImage(aImage);
|
2013-04-05 21:14:34 +00:00
|
|
|
return frozenImage.forget();
|
|
|
|
}
|
|
|
|
|
2019-02-25 22:07:58 +00:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<imgIContainer> ImageOps::Freeze(imgIContainer* aImage) {
|
2013-04-05 21:14:34 +00:00
|
|
|
nsCOMPtr<imgIContainer> frozenImage =
|
|
|
|
new FrozenImage(static_cast<Image*>(aImage));
|
|
|
|
return frozenImage.forget();
|
|
|
|
}
|
|
|
|
|
2019-02-25 22:07:58 +00:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<Image> ImageOps::Clip(Image* aImage, nsIntRect aClip,
|
|
|
|
const Maybe<nsSize>& aSVGViewportSize) {
|
2016-03-08 07:54:13 +00:00
|
|
|
RefPtr<Image> clippedImage =
|
|
|
|
new ClippedImage(aImage, aClip, aSVGViewportSize);
|
2013-04-05 21:14:34 +00:00
|
|
|
return clippedImage.forget();
|
|
|
|
}
|
|
|
|
|
2019-02-25 22:07:58 +00:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<imgIContainer> ImageOps::Clip(
|
2016-03-08 07:54:13 +00:00
|
|
|
imgIContainer* aImage, nsIntRect aClip,
|
|
|
|
const Maybe<nsSize>& aSVGViewportSize) {
|
2013-04-05 21:14:34 +00:00
|
|
|
nsCOMPtr<imgIContainer> clippedImage =
|
2016-03-08 07:54:13 +00:00
|
|
|
new ClippedImage(static_cast<Image*>(aImage), aClip, aSVGViewportSize);
|
2013-04-05 21:14:34 +00:00
|
|
|
return clippedImage.forget();
|
|
|
|
}
|
|
|
|
|
2019-02-25 22:07:58 +00:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<Image> ImageOps::Orient(Image* aImage,
|
|
|
|
Orientation aOrientation) {
|
2021-04-11 17:39:12 +00:00
|
|
|
if (aOrientation.IsIdentity()) {
|
|
|
|
return do_AddRef(aImage);
|
|
|
|
}
|
|
|
|
RefPtr<Image> image = new OrientedImage(aImage, aOrientation);
|
|
|
|
return image.forget();
|
2013-08-25 07:19:43 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 22:07:58 +00:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<imgIContainer> ImageOps::Orient(imgIContainer* aImage,
|
|
|
|
Orientation aOrientation) {
|
2021-04-11 17:39:12 +00:00
|
|
|
return Orient(static_cast<Image*>(aImage), aOrientation);
|
2013-08-25 07:19:43 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 02:57:32 +00:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<imgIContainer> ImageOps::Unorient(imgIContainer* aImage) {
|
2021-04-11 17:39:12 +00:00
|
|
|
return Orient(aImage, aImage->GetOrientation().Reversed());
|
2020-04-17 02:57:32 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 22:07:58 +00:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<imgIContainer> ImageOps::CreateFromDrawable(
|
2014-07-22 00:59:22 +00:00
|
|
|
gfxDrawable* aDrawable) {
|
|
|
|
nsCOMPtr<imgIContainer> drawableImage = new DynamicImage(aDrawable);
|
|
|
|
return drawableImage.forget();
|
|
|
|
}
|
|
|
|
|
2017-03-22 13:05:36 +00:00
|
|
|
class ImageOps::ImageBufferImpl final : public ImageOps::ImageBuffer {
|
|
|
|
public:
|
|
|
|
explicit ImageBufferImpl(already_AddRefed<SourceBuffer> aSourceBuffer)
|
|
|
|
: mSourceBuffer(aSourceBuffer) {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
~ImageBufferImpl() override {}
|
|
|
|
|
|
|
|
already_AddRefed<SourceBuffer> GetSourceBuffer() const override {
|
|
|
|
RefPtr<SourceBuffer> sourceBuffer = mSourceBuffer;
|
|
|
|
return sourceBuffer.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<SourceBuffer> mSourceBuffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<ImageOps::ImageBuffer>
|
2017-10-19 09:39:30 +00:00
|
|
|
ImageOps::CreateImageBuffer(already_AddRefed<nsIInputStream> aInputStream) {
|
2018-05-30 19:15:35 +00:00
|
|
|
nsCOMPtr<nsIInputStream> inputStream = std::move(aInputStream);
|
2017-10-19 09:39:30 +00:00
|
|
|
MOZ_ASSERT(inputStream);
|
2015-08-01 01:10:31 +00:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// Prepare the input stream.
|
2017-10-19 09:39:30 +00:00
|
|
|
if (!NS_InputStreamIsBuffered(inputStream)) {
|
2015-08-01 01:10:31 +00:00
|
|
|
nsCOMPtr<nsIInputStream> bufStream;
|
|
|
|
rv = NS_NewBufferedInputStream(getter_AddRefs(bufStream),
|
2017-10-19 09:39:30 +00:00
|
|
|
inputStream.forget(), 1024);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return nullptr;
|
2015-08-01 01:10:31 +00:00
|
|
|
}
|
2019-04-26 13:05:36 +00:00
|
|
|
inputStream = std::move(bufStream);
|
2015-08-01 01:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out how much data we've been passed.
|
|
|
|
uint64_t length;
|
|
|
|
rv = inputStream->Available(&length);
|
|
|
|
if (NS_FAILED(rv) || length > UINT32_MAX) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the data into a SourceBuffer.
|
2017-03-22 13:05:36 +00:00
|
|
|
RefPtr<SourceBuffer> sourceBuffer = new SourceBuffer();
|
2015-08-01 01:10:31 +00:00
|
|
|
sourceBuffer->ExpectLength(length);
|
|
|
|
rv = sourceBuffer->AppendFromInputStream(inputStream, length);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-02-08 16:33:39 +00:00
|
|
|
// Make sure our sourceBuffer is marked as complete.
|
2017-02-24 15:38:24 +00:00
|
|
|
if (sourceBuffer->IsComplete()) {
|
|
|
|
NS_WARNING(
|
|
|
|
"The SourceBuffer was unexpectedly marked as complete. This may "
|
|
|
|
"indicate either an OOM condition, or that imagelib was not "
|
|
|
|
"initialized properly.");
|
|
|
|
return nullptr;
|
2017-02-08 16:33:39 +00:00
|
|
|
}
|
2017-02-24 15:38:24 +00:00
|
|
|
sourceBuffer->Complete(NS_OK);
|
2015-08-01 01:10:31 +00:00
|
|
|
|
2017-03-22 13:05:36 +00:00
|
|
|
RefPtr<ImageBuffer> imageBuffer = new ImageBufferImpl(sourceBuffer.forget());
|
|
|
|
return imageBuffer.forget();
|
|
|
|
}
|
|
|
|
|
2019-02-25 22:07:58 +00:00
|
|
|
/* static */
|
|
|
|
nsresult ImageOps::DecodeMetadata(already_AddRefed<nsIInputStream> aInputStream,
|
|
|
|
const nsACString& aMimeType,
|
|
|
|
ImageMetadata& aMetadata) {
|
2018-05-30 19:15:35 +00:00
|
|
|
nsCOMPtr<nsIInputStream> inputStream = std::move(aInputStream);
|
2017-10-19 09:39:30 +00:00
|
|
|
RefPtr<ImageBuffer> buffer = CreateImageBuffer(inputStream.forget());
|
2017-03-22 13:05:36 +00:00
|
|
|
return DecodeMetadata(buffer, aMimeType, aMetadata);
|
|
|
|
}
|
|
|
|
|
2019-02-25 22:07:58 +00:00
|
|
|
/* static */
|
|
|
|
nsresult ImageOps::DecodeMetadata(ImageBuffer* aBuffer,
|
|
|
|
const nsACString& aMimeType,
|
|
|
|
ImageMetadata& aMetadata) {
|
2017-03-22 13:05:36 +00:00
|
|
|
if (!aBuffer) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<SourceBuffer> sourceBuffer = aBuffer->GetSourceBuffer();
|
|
|
|
if (NS_WARN_IF(!sourceBuffer)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a decoder.
|
|
|
|
DecoderType decoderType =
|
|
|
|
DecoderFactory::GetDecoderType(PromiseFlatCString(aMimeType).get());
|
|
|
|
RefPtr<Decoder> decoder = DecoderFactory::CreateAnonymousMetadataDecoder(
|
|
|
|
decoderType, WrapNotNull(sourceBuffer));
|
|
|
|
if (!decoder) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the decoder synchronously.
|
2018-11-01 20:36:16 +00:00
|
|
|
RefPtr<IDecodingTask> task =
|
|
|
|
new AnonymousDecodingTask(WrapNotNull(decoder), /* aResumable */ false);
|
2017-03-22 13:05:36 +00:00
|
|
|
task->Run();
|
|
|
|
if (!decoder->GetDecodeDone() || decoder->HasError()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
aMetadata = decoder->GetImageMetadata();
|
|
|
|
if (aMetadata.GetNativeSizes().IsEmpty() && aMetadata.HasSize()) {
|
|
|
|
aMetadata.AddNativeSize(aMetadata.GetSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<gfx::SourceSurface> ImageOps::DecodeToSurface(
|
2017-10-19 09:39:30 +00:00
|
|
|
already_AddRefed<nsIInputStream> aInputStream, const nsACString& aMimeType,
|
2017-03-22 13:05:36 +00:00
|
|
|
uint32_t aFlags, const Maybe<IntSize>& aSize /* = Nothing() */) {
|
2018-05-30 19:15:35 +00:00
|
|
|
nsCOMPtr<nsIInputStream> inputStream = std::move(aInputStream);
|
2017-10-19 09:39:30 +00:00
|
|
|
RefPtr<ImageBuffer> buffer = CreateImageBuffer(inputStream.forget());
|
2017-03-22 13:05:36 +00:00
|
|
|
return DecodeToSurface(buffer, aMimeType, aFlags, aSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<gfx::SourceSurface> ImageOps::DecodeToSurface(
|
|
|
|
ImageBuffer* aBuffer, const nsACString& aMimeType, uint32_t aFlags,
|
|
|
|
const Maybe<IntSize>& aSize /* = Nothing() */) {
|
|
|
|
if (!aBuffer) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<SourceBuffer> sourceBuffer = aBuffer->GetSourceBuffer();
|
|
|
|
if (NS_WARN_IF(!sourceBuffer)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-08-01 01:10:31 +00:00
|
|
|
// Create a decoder.
|
|
|
|
DecoderType decoderType =
|
|
|
|
DecoderFactory::GetDecoderType(PromiseFlatCString(aMimeType).get());
|
2017-03-22 13:05:36 +00:00
|
|
|
RefPtr<Decoder> decoder = DecoderFactory::CreateAnonymousDecoder(
|
|
|
|
decoderType, WrapNotNull(sourceBuffer), aSize,
|
2018-09-17 19:06:29 +00:00
|
|
|
DecoderFlags::FIRST_FRAME_ONLY, ToSurfaceFlags(aFlags));
|
2015-08-01 01:10:31 +00:00
|
|
|
if (!decoder) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the decoder synchronously.
|
2018-11-01 20:36:16 +00:00
|
|
|
RefPtr<IDecodingTask> task =
|
|
|
|
new AnonymousDecodingTask(WrapNotNull(decoder), /* aResumable */ false);
|
2016-06-29 20:43:19 +00:00
|
|
|
task->Run();
|
2015-08-01 01:10:31 +00:00
|
|
|
if (!decoder->GetDecodeDone() || decoder->HasError()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pull out the surface.
|
|
|
|
RawAccessFrameRef frame = decoder->GetCurrentFrameRef();
|
|
|
|
if (!frame) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-08-18 19:43:16 +00:00
|
|
|
RefPtr<SourceSurface> surface = frame->GetSourceSurface();
|
2015-08-01 01:10:31 +00:00
|
|
|
if (!surface) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return surface.forget();
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:39:12 +00:00
|
|
|
} // namespace mozilla::image
|