2013-04-06 01:14:34 +04: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 04:10:31 +03:00
|
|
|
#include "ImageOps.h"
|
|
|
|
|
2013-04-06 01:14:34 +04:00
|
|
|
#include "ClippedImage.h"
|
2015-08-01 04:10:31 +03:00
|
|
|
#include "Decoder.h"
|
|
|
|
#include "DecoderFactory.h"
|
2014-07-22 04:59:22 +04:00
|
|
|
#include "DynamicImage.h"
|
2013-04-06 01:14:34 +04:00
|
|
|
#include "FrozenImage.h"
|
2016-06-29 23:43:19 +03:00
|
|
|
#include "IDecodingTask.h"
|
2013-04-06 01:14:34 +04:00
|
|
|
#include "Image.h"
|
2017-03-22 16:05:36 +03:00
|
|
|
#include "ImageMetadata.h"
|
2015-08-01 04:10:31 +03:00
|
|
|
#include "imgIContainer.h"
|
2015-08-01 04:10:34 +03:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2018-06-06 03:42:56 +03:00
|
|
|
#include "nsNetUtil.h" // for NS_NewBufferedInputStream
|
2015-08-01 04:10:31 +03:00
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
#include "OrientedImage.h"
|
|
|
|
#include "SourceBuffer.h"
|
2013-04-06 01:14:34 +04:00
|
|
|
|
2015-08-01 04:10:31 +03:00
|
|
|
using namespace mozilla::gfx;
|
2013-04-06 01:14:34 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace image {
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<Image>
|
|
|
|
ImageOps::Freeze(Image* aImage)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Image> frozenImage = new FrozenImage(aImage);
|
2013-04-06 01:14:34 +04:00
|
|
|
return frozenImage.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<imgIContainer>
|
|
|
|
ImageOps::Freeze(imgIContainer* aImage)
|
|
|
|
{
|
|
|
|
nsCOMPtr<imgIContainer> frozenImage =
|
|
|
|
new FrozenImage(static_cast<Image*>(aImage));
|
|
|
|
return frozenImage.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<Image>
|
2016-03-08 10:54:13 +03:00
|
|
|
ImageOps::Clip(Image* aImage, nsIntRect aClip,
|
|
|
|
const Maybe<nsSize>& aSVGViewportSize)
|
2013-04-06 01:14:34 +04:00
|
|
|
{
|
2016-03-08 10:54:13 +03:00
|
|
|
RefPtr<Image> clippedImage = new ClippedImage(aImage, aClip, aSVGViewportSize);
|
2013-04-06 01:14:34 +04:00
|
|
|
return clippedImage.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<imgIContainer>
|
2016-03-08 10:54:13 +03:00
|
|
|
ImageOps::Clip(imgIContainer* aImage, nsIntRect aClip,
|
|
|
|
const Maybe<nsSize>& aSVGViewportSize)
|
2013-04-06 01:14:34 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<imgIContainer> clippedImage =
|
2016-03-08 10:54:13 +03:00
|
|
|
new ClippedImage(static_cast<Image*>(aImage), aClip, aSVGViewportSize);
|
2013-04-06 01:14:34 +04:00
|
|
|
return clippedImage.forget();
|
|
|
|
}
|
|
|
|
|
2013-08-25 11:19:43 +04:00
|
|
|
/* static */ already_AddRefed<Image>
|
|
|
|
ImageOps::Orient(Image* aImage, Orientation aOrientation)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Image> orientedImage = new OrientedImage(aImage, aOrientation);
|
2013-08-25 11:19:43 +04:00
|
|
|
return orientedImage.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<imgIContainer>
|
|
|
|
ImageOps::Orient(imgIContainer* aImage, Orientation aOrientation)
|
|
|
|
{
|
|
|
|
nsCOMPtr<imgIContainer> orientedImage =
|
|
|
|
new OrientedImage(static_cast<Image*>(aImage), aOrientation);
|
|
|
|
return orientedImage.forget();
|
|
|
|
}
|
|
|
|
|
2014-07-22 04:59:22 +04:00
|
|
|
/* static */ already_AddRefed<imgIContainer>
|
|
|
|
ImageOps::CreateFromDrawable(gfxDrawable* aDrawable)
|
|
|
|
{
|
|
|
|
nsCOMPtr<imgIContainer> drawableImage = new DynamicImage(aDrawable);
|
|
|
|
return drawableImage.forget();
|
|
|
|
}
|
|
|
|
|
2017-03-22 16:05:36 +03: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 12:39:30 +03:00
|
|
|
ImageOps::CreateImageBuffer(already_AddRefed<nsIInputStream> aInputStream)
|
2015-08-01 04:10:31 +03:00
|
|
|
{
|
2018-05-30 22:15:35 +03:00
|
|
|
nsCOMPtr<nsIInputStream> inputStream = std::move(aInputStream);
|
2017-10-19 12:39:30 +03:00
|
|
|
MOZ_ASSERT(inputStream);
|
2015-08-01 04:10:31 +03:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// Prepare the input stream.
|
2017-10-19 12:39:30 +03:00
|
|
|
if (!NS_InputStreamIsBuffered(inputStream)) {
|
2015-08-01 04:10:31 +03:00
|
|
|
nsCOMPtr<nsIInputStream> bufStream;
|
|
|
|
rv = NS_NewBufferedInputStream(getter_AddRefs(bufStream),
|
2017-10-19 12:39:30 +03:00
|
|
|
inputStream.forget(), 1024);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return nullptr;
|
2015-08-01 04:10:31 +03: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 16:05:36 +03:00
|
|
|
RefPtr<SourceBuffer> sourceBuffer = new SourceBuffer();
|
2015-08-01 04:10:31 +03:00
|
|
|
sourceBuffer->ExpectLength(length);
|
|
|
|
rv = sourceBuffer->AppendFromInputStream(inputStream, length);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-02-08 19:33:39 +03:00
|
|
|
// Make sure our sourceBuffer is marked as complete.
|
2017-02-24 18:38:24 +03: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 19:33:39 +03:00
|
|
|
}
|
2017-02-24 18:38:24 +03:00
|
|
|
sourceBuffer->Complete(NS_OK);
|
2015-08-01 04:10:31 +03:00
|
|
|
|
2017-03-22 16:05:36 +03:00
|
|
|
RefPtr<ImageBuffer> imageBuffer = new ImageBufferImpl(sourceBuffer.forget());
|
|
|
|
return imageBuffer.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ nsresult
|
2017-10-19 12:39:30 +03:00
|
|
|
ImageOps::DecodeMetadata(already_AddRefed<nsIInputStream> aInputStream,
|
2017-03-22 16:05:36 +03:00
|
|
|
const nsACString& aMimeType,
|
|
|
|
ImageMetadata& aMetadata)
|
|
|
|
{
|
2018-05-30 22:15:35 +03:00
|
|
|
nsCOMPtr<nsIInputStream> inputStream = std::move(aInputStream);
|
2017-10-19 12:39:30 +03:00
|
|
|
RefPtr<ImageBuffer> buffer = CreateImageBuffer(inputStream.forget());
|
2017-03-22 16:05:36 +03:00
|
|
|
return DecodeMetadata(buffer, aMimeType, aMetadata);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ nsresult
|
|
|
|
ImageOps::DecodeMetadata(ImageBuffer* aBuffer,
|
|
|
|
const nsACString& aMimeType,
|
|
|
|
ImageMetadata& aMetadata)
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
RefPtr<IDecodingTask> task = new AnonymousDecodingTask(WrapNotNull(decoder));
|
|
|
|
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>
|
2017-10-19 12:39:30 +03:00
|
|
|
ImageOps::DecodeToSurface(already_AddRefed<nsIInputStream> aInputStream,
|
2017-03-22 16:05:36 +03:00
|
|
|
const nsACString& aMimeType,
|
|
|
|
uint32_t aFlags,
|
|
|
|
const Maybe<IntSize>& aSize /* = Nothing() */)
|
|
|
|
{
|
2018-05-30 22:15:35 +03:00
|
|
|
nsCOMPtr<nsIInputStream> inputStream = std::move(aInputStream);
|
2017-10-19 12:39:30 +03:00
|
|
|
RefPtr<ImageBuffer> buffer = CreateImageBuffer(inputStream.forget());
|
2017-03-22 16:05:36 +03: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 04:10:31 +03:00
|
|
|
// Create a decoder.
|
|
|
|
DecoderType decoderType =
|
|
|
|
DecoderFactory::GetDecoderType(PromiseFlatCString(aMimeType).get());
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Decoder> decoder =
|
2017-03-22 16:05:36 +03:00
|
|
|
DecoderFactory::CreateAnonymousDecoder(decoderType,
|
|
|
|
WrapNotNull(sourceBuffer),
|
2018-09-17 22:06:29 +03:00
|
|
|
aSize,
|
|
|
|
DecoderFlags::FIRST_FRAME_ONLY,
|
|
|
|
ToSurfaceFlags(aFlags));
|
2015-08-01 04:10:31 +03:00
|
|
|
if (!decoder) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the decoder synchronously.
|
2016-06-29 23:43:19 +03:00
|
|
|
RefPtr<IDecodingTask> task = new AnonymousDecodingTask(WrapNotNull(decoder));
|
|
|
|
task->Run();
|
2015-08-01 04:10:31 +03:00
|
|
|
if (!decoder->GetDecodeDone() || decoder->HasError()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pull out the surface.
|
|
|
|
RawAccessFrameRef frame = decoder->GetCurrentFrameRef();
|
|
|
|
if (!frame) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-08-18 22:43:16 +03:00
|
|
|
RefPtr<SourceSurface> surface = frame->GetSourceSurface();
|
2015-08-01 04:10:31 +03:00
|
|
|
if (!surface) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return surface.forget();
|
|
|
|
}
|
|
|
|
|
2013-04-06 01:14:34 +04:00
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|