2008-01-12 20:15:20 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-21 04:12:37 -07: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-12 20:15:20 -08:00
|
|
|
|
|
|
|
#include "imgTools.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsString.h"
|
2012-07-27 07:03:27 -07:00
|
|
|
#include "nsError.h"
|
2012-11-16 10:24:58 -08:00
|
|
|
#include "imgLoader.h"
|
2012-06-25 21:20:12 -07:00
|
|
|
#include "imgICache.h"
|
2008-01-12 20:15:20 -08:00
|
|
|
#include "imgIContainer.h"
|
|
|
|
#include "imgIEncoder.h"
|
|
|
|
#include "gfxContext.h"
|
|
|
|
#include "nsStringStream.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsWeakReference.h"
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
2009-07-21 15:57:25 -07:00
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
#include "nsNetUtil.h"
|
2012-06-25 21:20:12 -07:00
|
|
|
#include "nsContentUtils.h"
|
2012-12-17 17:35:07 -08:00
|
|
|
#include "ImageFactory.h"
|
2012-10-12 09:11:22 -07:00
|
|
|
#include "ScriptedNotificationObserver.h"
|
|
|
|
#include "imgIScriptedNotificationObserver.h"
|
2008-01-12 20:15:20 -08:00
|
|
|
|
2012-01-06 08:02:27 -08:00
|
|
|
using namespace mozilla::image;
|
2010-08-13 21:09:49 -07:00
|
|
|
|
2012-06-25 21:20:12 -07:00
|
|
|
class nsIDOMDocument;
|
|
|
|
class nsIDocument;
|
|
|
|
|
2008-01-12 20:15:20 -08:00
|
|
|
/* ========== imgITools implementation ========== */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(imgTools, imgITools)
|
|
|
|
|
|
|
|
imgTools::imgTools()
|
|
|
|
{
|
|
|
|
/* member initializers and constructor code */
|
|
|
|
}
|
|
|
|
|
|
|
|
imgTools::~imgTools()
|
|
|
|
{
|
|
|
|
/* destructor code */
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP imgTools::DecodeImageData(nsIInputStream* aInStr,
|
|
|
|
const nsACString& aMimeType,
|
|
|
|
imgIContainer **aContainer)
|
2012-12-17 17:35:07 -08:00
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(*aContainer == nullptr,
|
|
|
|
"Cannot provide an existing image container to DecodeImageData");
|
|
|
|
|
|
|
|
return DecodeImage(aInStr, aMimeType, aContainer);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP imgTools::DecodeImage(nsIInputStream* aInStr,
|
|
|
|
const nsACString& aMimeType,
|
|
|
|
imgIContainer **aContainer)
|
2008-01-12 20:15:20 -08:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-12-17 17:35:07 -08:00
|
|
|
nsRefPtr<Image> image;
|
2008-01-12 20:15:20 -08:00
|
|
|
|
2009-09-12 15:44:18 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aInStr);
|
2010-08-13 21:09:49 -07:00
|
|
|
|
2012-12-17 17:35:07 -08:00
|
|
|
// Create a new image container to hold the decoded data.
|
|
|
|
nsAutoCString mimeType(aMimeType);
|
|
|
|
image = ImageFactory::CreateAnonymousImage(mimeType);
|
2008-01-12 20:15:20 -08:00
|
|
|
|
2012-12-17 17:35:07 -08:00
|
|
|
if (image->HasError())
|
|
|
|
return NS_ERROR_FAILURE;
|
2008-01-12 20:15:20 -08:00
|
|
|
|
2012-12-17 17:35:07 -08:00
|
|
|
// Prepare the input stream.
|
2009-07-21 15:57:25 -07:00
|
|
|
nsCOMPtr<nsIInputStream> inStream = aInStr;
|
|
|
|
if (!NS_InputStreamIsBuffered(aInStr)) {
|
|
|
|
nsCOMPtr<nsIInputStream> bufStream;
|
|
|
|
rv = NS_NewBufferedInputStream(getter_AddRefs(bufStream), aInStr, 1024);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
inStream = bufStream;
|
|
|
|
}
|
|
|
|
|
2012-12-17 17:35:07 -08:00
|
|
|
// Figure out how much data we've been passed.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t length;
|
2009-07-21 15:57:25 -07:00
|
|
|
rv = inStream->Available(&length);
|
2008-01-12 20:15:20 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-09-27 23:57:33 -07:00
|
|
|
NS_ENSURE_TRUE(length <= UINT32_MAX, NS_ERROR_FILE_TOO_BIG);
|
2008-01-12 20:15:20 -08:00
|
|
|
|
2012-12-17 17:35:07 -08:00
|
|
|
// Send the source data to the Image.
|
|
|
|
rv = image->OnImageDataAvailable(nullptr, nullptr, inStream, 0, uint32_t(length));
|
2008-01-12 20:15:20 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-12-17 17:35:07 -08:00
|
|
|
// Let the Image know we've sent all the data.
|
2012-12-17 14:05:18 -08:00
|
|
|
rv = image->OnImageDataComplete(nullptr, nullptr, NS_OK);
|
2009-09-12 15:44:18 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-12-17 17:35:07 -08:00
|
|
|
// All done.
|
|
|
|
NS_ADDREF(*aContainer = image.get());
|
2008-01-12 20:15:20 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP imgTools::EncodeImage(imgIContainer *aContainer,
|
2010-08-13 21:09:51 -07:00
|
|
|
const nsACString& aMimeType,
|
2011-12-16 16:43:10 -08:00
|
|
|
const nsAString& aOutputOptions,
|
2010-08-13 21:09:51 -07:00
|
|
|
nsIInputStream **aStream)
|
2008-01-12 20:15:20 -08:00
|
|
|
{
|
2012-07-02 23:22:10 -07:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// Use frame 0 from the image container.
|
|
|
|
nsRefPtr<gfxImageSurface> frame;
|
|
|
|
rv = GetFirstImageFrame(aContainer, getter_AddRefs(frame));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return EncodeImageData(frame, aMimeType, aOutputOptions, aStream);
|
2008-01-12 20:15:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP imgTools::EncodeScaledImage(imgIContainer *aContainer,
|
|
|
|
const nsACString& aMimeType,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aScaledWidth,
|
|
|
|
int32_t aScaledHeight,
|
2011-12-16 16:43:10 -08:00
|
|
|
const nsAString& aOutputOptions,
|
2008-01-12 20:15:20 -08:00
|
|
|
nsIInputStream **aStream)
|
|
|
|
{
|
2012-07-02 23:22:10 -07:00
|
|
|
NS_ENSURE_ARG(aScaledWidth >= 0 && aScaledHeight >= 0);
|
2008-01-12 20:15:20 -08: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-02 23:22:10 -07:00
|
|
|
return EncodeImage(aContainer, aMimeType, aOutputOptions, aStream);
|
2008-01-12 20:15:20 -08:00
|
|
|
}
|
|
|
|
|
2012-07-02 23:22:10 -07:00
|
|
|
// Use frame 0 from the image container.
|
|
|
|
nsRefPtr<gfxImageSurface> frame;
|
|
|
|
nsresult rv = GetFirstImageFrame(aContainer, getter_AddRefs(frame));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2008-01-12 20:15:20 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t frameWidth = frame->Width(), frameHeight = frame->Height();
|
2012-07-02 23:22:10 -07:00
|
|
|
|
|
|
|
// If the given width or height is zero we'll replace it with the image's
|
|
|
|
// original dimensions.
|
|
|
|
if (aScaledWidth == 0) {
|
|
|
|
aScaledWidth = frameWidth;
|
|
|
|
} else if (aScaledHeight == 0) {
|
|
|
|
aScaledHeight = frameHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a temporary image surface
|
|
|
|
nsRefPtr<gfxImageSurface> dest = new gfxImageSurface(gfxIntSize(aScaledWidth, aScaledHeight),
|
|
|
|
gfxASurface::ImageFormatARGB32);
|
|
|
|
gfxContext ctx(dest);
|
|
|
|
|
|
|
|
// Set scaling
|
|
|
|
gfxFloat sw = (double) aScaledWidth / frameWidth;
|
|
|
|
gfxFloat sh = (double) aScaledHeight / frameHeight;
|
|
|
|
ctx.Scale(sw, sh);
|
|
|
|
|
|
|
|
// Paint a scaled image
|
|
|
|
ctx.SetOperator(gfxContext::OPERATOR_SOURCE);
|
|
|
|
ctx.SetSource(frame);
|
|
|
|
ctx.Paint();
|
|
|
|
|
|
|
|
return EncodeImageData(dest, aMimeType, aOutputOptions, aStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP imgTools::EncodeCroppedImage(imgIContainer *aContainer,
|
|
|
|
const nsACString& aMimeType,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aOffsetX,
|
|
|
|
int32_t aOffsetY,
|
|
|
|
int32_t aWidth,
|
|
|
|
int32_t aHeight,
|
2012-07-02 23:22:10 -07:00
|
|
|
const nsAString& aOutputOptions,
|
|
|
|
nsIInputStream **aStream)
|
|
|
|
{
|
|
|
|
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-12 20:15:20 -08:00
|
|
|
|
|
|
|
// Use frame 0 from the image container.
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-20 18:50:15 -07:00
|
|
|
nsRefPtr<gfxImageSurface> frame;
|
2012-07-02 23:22:10 -07:00
|
|
|
nsresult rv = GetFirstImageFrame(aContainer, getter_AddRefs(frame));
|
2008-01-12 20:15:20 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t frameWidth = frame->Width(), frameHeight = frame->Height();
|
2008-01-12 20:15:20 -08:00
|
|
|
|
2012-07-02 23:22:10 -07: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-12 20:15:20 -08:00
|
|
|
|
2012-07-02 23:22:10 -07:00
|
|
|
// Check that the given crop rectangle is within image bounds.
|
|
|
|
NS_ENSURE_ARG(frameWidth >= aOffsetX + aWidth &&
|
|
|
|
frameHeight >= aOffsetY + aHeight);
|
2008-01-12 20:15:20 -08:00
|
|
|
|
2012-07-02 23:22:10 -07:00
|
|
|
// Create a temporary image surface
|
|
|
|
nsRefPtr<gfxImageSurface> dest = new gfxImageSurface(gfxIntSize(aWidth, aHeight),
|
|
|
|
gfxASurface::ImageFormatARGB32);
|
|
|
|
gfxContext ctx(dest);
|
2008-01-12 20:15:20 -08:00
|
|
|
|
2012-07-02 23:22:10 -07:00
|
|
|
// Set translate
|
|
|
|
ctx.Translate(gfxPoint(-aOffsetX, -aOffsetY));
|
2008-01-12 20:15:20 -08:00
|
|
|
|
2012-07-02 23:22:10 -07:00
|
|
|
// Paint a scaled image
|
|
|
|
ctx.SetOperator(gfxContext::OPERATOR_SOURCE);
|
|
|
|
ctx.SetSource(frame);
|
|
|
|
ctx.Paint();
|
|
|
|
|
|
|
|
return EncodeImageData(dest, aMimeType, aOutputOptions, aStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP imgTools::EncodeImageData(gfxImageSurface *aSurface,
|
|
|
|
const nsACString& aMimeType,
|
|
|
|
const nsAString& aOutputOptions,
|
|
|
|
nsIInputStream **aStream)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t *bitmapData;
|
|
|
|
uint32_t bitmapDataLength, strideSize;
|
2012-07-02 23:22:10 -07:00
|
|
|
|
|
|
|
// Get an image encoder for the media type
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString encoderCID(
|
2012-07-02 23:22:10 -07:00
|
|
|
NS_LITERAL_CSTRING("@mozilla.org/image/encoder;2?type=") + aMimeType);
|
|
|
|
|
|
|
|
nsCOMPtr<imgIEncoder> encoder = do_CreateInstance(encoderCID.get());
|
|
|
|
if (!encoder)
|
|
|
|
return NS_IMAGELIB_ERROR_NO_ENCODER;
|
|
|
|
|
|
|
|
bitmapData = aSurface->Data();
|
|
|
|
if (!bitmapData)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
strideSize = aSurface->Stride();
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t width = aSurface->Width(), height = aSurface->Height();
|
2012-07-02 23:22:10 -07:00
|
|
|
bitmapDataLength = height * strideSize;
|
2008-01-12 20:15:20 -08:00
|
|
|
|
|
|
|
// Encode the bitmap
|
2012-07-02 23:22:10 -07:00
|
|
|
nsresult rv = encoder->InitFromData(bitmapData,
|
|
|
|
bitmapDataLength,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
strideSize,
|
|
|
|
imgIEncoder::INPUT_FORMAT_HOSTARGB,
|
|
|
|
aOutputOptions);
|
2008-01-12 20:15:20 -08:00
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return CallQueryInterface(encoder, aStream);
|
|
|
|
}
|
2012-07-02 23:22:10 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP imgTools::GetFirstImageFrame(imgIContainer *aContainer,
|
|
|
|
gfxImageSurface **aSurface)
|
|
|
|
{
|
|
|
|
nsRefPtr<gfxImageSurface> frame;
|
|
|
|
nsresult rv = aContainer->CopyFrame(imgIContainer::FRAME_CURRENT, true,
|
|
|
|
getter_AddRefs(frame));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ENSURE_TRUE(frame, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
NS_ENSURE_TRUE(frame->Width() && frame->Height(), NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
frame.forget(aSurface);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-06-25 21:20:12 -07:00
|
|
|
|
2012-10-12 09:11:22 -07:00
|
|
|
NS_IMETHODIMP imgTools::CreateScriptedObserver(imgIScriptedNotificationObserver* aInner,
|
|
|
|
imgINotificationObserver** aObserver)
|
|
|
|
{
|
|
|
|
NS_ADDREF(*aObserver = new ScriptedNotificationObserver(aInner));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-25 21:20:12 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
imgTools::GetImgLoaderForDocument(nsIDOMDocument* aDoc, imgILoader** aLoader)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryInterface(aDoc);
|
|
|
|
NS_IF_ADDREF(*aLoader = nsContentUtils::GetImgLoaderForDocument(doc));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
imgTools::GetImgCacheForDocument(nsIDOMDocument* aDoc, imgICache** aCache)
|
|
|
|
{
|
|
|
|
nsCOMPtr<imgILoader> loader;
|
|
|
|
nsresult rv = GetImgLoaderForDocument(aDoc, getter_AddRefs(loader));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return CallQueryInterface(loader, aCache);
|
|
|
|
}
|