2013-04-05 14:14:34 -07: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-07-31 18:10:31 -07:00
|
|
|
#include "ImageOps.h"
|
|
|
|
|
2013-04-05 14:14:34 -07:00
|
|
|
#include "ClippedImage.h"
|
2015-07-31 18:10:31 -07:00
|
|
|
#include "DecodePool.h"
|
|
|
|
#include "Decoder.h"
|
|
|
|
#include "DecoderFactory.h"
|
2014-07-21 17:59:22 -07:00
|
|
|
#include "DynamicImage.h"
|
2013-04-05 14:14:34 -07:00
|
|
|
#include "FrozenImage.h"
|
|
|
|
#include "Image.h"
|
2015-07-31 18:10:31 -07:00
|
|
|
#include "imgIContainer.h"
|
2015-07-31 18:10:34 -07:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2015-07-31 18:10:31 -07:00
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
#include "OrientedImage.h"
|
|
|
|
#include "SourceBuffer.h"
|
2013-04-05 14:14:34 -07:00
|
|
|
|
2015-07-31 18:10:31 -07:00
|
|
|
using namespace mozilla::gfx;
|
2013-04-05 14:14:34 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace image {
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<Image>
|
|
|
|
ImageOps::Freeze(Image* aImage)
|
|
|
|
{
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<Image> frozenImage = new FrozenImage(aImage);
|
2013-04-05 14:14:34 -07: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>
|
|
|
|
ImageOps::Clip(Image* aImage, nsIntRect aClip)
|
|
|
|
{
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<Image> clippedImage = new ClippedImage(aImage, aClip);
|
2013-04-05 14:14:34 -07:00
|
|
|
return clippedImage.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<imgIContainer>
|
|
|
|
ImageOps::Clip(imgIContainer* aImage, nsIntRect aClip)
|
|
|
|
{
|
|
|
|
nsCOMPtr<imgIContainer> clippedImage =
|
|
|
|
new ClippedImage(static_cast<Image*>(aImage), aClip);
|
|
|
|
return clippedImage.forget();
|
|
|
|
}
|
|
|
|
|
2013-08-25 00:19:43 -07:00
|
|
|
/* static */ already_AddRefed<Image>
|
|
|
|
ImageOps::Orient(Image* aImage, Orientation aOrientation)
|
|
|
|
{
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<Image> orientedImage = new OrientedImage(aImage, aOrientation);
|
2013-08-25 00:19:43 -07: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-21 17:59:22 -07:00
|
|
|
/* static */ already_AddRefed<imgIContainer>
|
|
|
|
ImageOps::CreateFromDrawable(gfxDrawable* aDrawable)
|
|
|
|
{
|
|
|
|
nsCOMPtr<imgIContainer> drawableImage = new DynamicImage(aDrawable);
|
|
|
|
return drawableImage.forget();
|
|
|
|
}
|
|
|
|
|
2015-07-31 18:10:31 -07:00
|
|
|
/* static */ already_AddRefed<gfx::SourceSurface>
|
|
|
|
ImageOps::DecodeToSurface(nsIInputStream* aInputStream,
|
|
|
|
const nsACString& aMimeType,
|
|
|
|
uint32_t aFlags)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aInputStream);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// Prepare the input stream.
|
|
|
|
nsCOMPtr<nsIInputStream> inputStream = aInputStream;
|
|
|
|
if (!NS_InputStreamIsBuffered(aInputStream)) {
|
|
|
|
nsCOMPtr<nsIInputStream> bufStream;
|
|
|
|
rv = NS_NewBufferedInputStream(getter_AddRefs(bufStream),
|
|
|
|
aInputStream, 1024);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
inputStream = bufStream;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<SourceBuffer> sourceBuffer = new SourceBuffer();
|
2015-07-31 18:10:31 -07:00
|
|
|
sourceBuffer->ExpectLength(length);
|
|
|
|
rv = sourceBuffer->AppendFromInputStream(inputStream, length);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
sourceBuffer->Complete(NS_OK);
|
|
|
|
|
|
|
|
// Create a decoder.
|
|
|
|
DecoderType decoderType =
|
|
|
|
DecoderFactory::GetDecoderType(PromiseFlatCString(aMimeType).get());
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<Decoder> decoder =
|
2015-08-14 17:56:44 -07:00
|
|
|
DecoderFactory::CreateAnonymousDecoder(decoderType,
|
|
|
|
sourceBuffer,
|
|
|
|
ToSurfaceFlags(aFlags));
|
2015-07-31 18:10:31 -07:00
|
|
|
if (!decoder) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the decoder synchronously.
|
|
|
|
decoder->Decode();
|
|
|
|
if (!decoder->GetDecodeDone() || decoder->HasError()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pull out the surface.
|
|
|
|
RawAccessFrameRef frame = decoder->GetCurrentFrameRef();
|
|
|
|
if (!frame) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<SourceSurface> surface = frame->GetSurface();
|
2015-07-31 18:10:31 -07:00
|
|
|
if (!surface) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return surface.forget();
|
|
|
|
}
|
|
|
|
|
2013-04-05 14:14:34 -07:00
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|