2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is PNG Encoding code
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Google Inc.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2005
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Brett Wilson <brettw@gmail.com>
|
|
|
|
* Stuart Parmenter <pavlov@pavlov.net>
|
2007-04-24 22:56:53 -07:00
|
|
|
* Justin Dolske <dolske@mozilla.com>
|
2010-01-05 13:46:26 -08:00
|
|
|
* Glenn Randers-Pehrson <glennrp@gmail.com>
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2007-04-24 22:56:53 -07:00
|
|
|
#include "nsCRT.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsPNGEncoder.h"
|
|
|
|
#include "prmem.h"
|
2007-04-24 22:56:53 -07:00
|
|
|
#include "prprf.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
|
2010-06-23 10:24:56 -07:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS3(nsPNGEncoder, imgIEncoder, nsIInputStream, nsIAsyncInputStream)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-04-24 22:56:53 -07:00
|
|
|
nsPNGEncoder::nsPNGEncoder() : mPNG(nsnull), mPNGinfo(nsnull),
|
|
|
|
mIsAnimation(PR_FALSE),
|
2010-07-07 02:35:29 -07:00
|
|
|
mFinished(PR_FALSE),
|
2007-04-24 22:56:53 -07:00
|
|
|
mImageBuffer(nsnull), mImageBufferSize(0),
|
2010-06-23 10:24:56 -07:00
|
|
|
mImageBufferUsed(0), mImageBufferReadPoint(0),
|
2010-07-07 02:35:29 -07:00
|
|
|
mCallback(nsnull),
|
2010-06-23 10:24:56 -07:00
|
|
|
mCallbackTarget(nsnull), mNotifyThreshold(0),
|
|
|
|
mMonitor("PNG Encoder Monitor")
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPNGEncoder::~nsPNGEncoder()
|
|
|
|
{
|
|
|
|
if (mImageBuffer) {
|
|
|
|
PR_Free(mImageBuffer);
|
|
|
|
mImageBuffer = nsnull;
|
|
|
|
}
|
2007-10-19 21:29:58 -07:00
|
|
|
// don't leak if EndImageEncode wasn't called
|
|
|
|
if (mPNG)
|
|
|
|
png_destroy_write_struct(&mPNG, &mPNGinfo);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// nsPNGEncoder::InitFromData
|
|
|
|
//
|
|
|
|
// One output option is supported: "transparency=none" means that the
|
|
|
|
// output PNG will not have an alpha channel, even if the input does.
|
|
|
|
//
|
|
|
|
// Based partially on gfx/cairo/cairo/src/cairo-png.c
|
|
|
|
// See also modules/libimg/png/libpng.txt
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPNGEncoder::InitFromData(const PRUint8* aData,
|
2010-01-05 13:46:26 -08:00
|
|
|
PRUint32 aLength, // (unused,
|
|
|
|
// req'd by JS)
|
|
|
|
PRUint32 aWidth,
|
|
|
|
PRUint32 aHeight,
|
|
|
|
PRUint32 aStride,
|
|
|
|
PRUint32 aInputFormat,
|
|
|
|
const nsAString& aOutputOptions)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-04-24 22:56:53 -07:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
rv = StartImageEncode(aWidth, aHeight, aInputFormat, aOutputOptions);
|
|
|
|
if (!NS_SUCCEEDED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2010-01-05 13:46:26 -08:00
|
|
|
rv = AddImageFrame(aData, aLength, aWidth, aHeight, aStride,
|
|
|
|
aInputFormat, aOutputOptions);
|
2007-04-24 22:56:53 -07:00
|
|
|
if (!NS_SUCCEEDED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
rv = EndImageEncode();
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// nsPNGEncoder::StartImageEncode
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// See ::InitFromData for other info.
|
|
|
|
NS_IMETHODIMP nsPNGEncoder::StartImageEncode(PRUint32 aWidth,
|
|
|
|
PRUint32 aHeight,
|
|
|
|
PRUint32 aInputFormat,
|
|
|
|
const nsAString& aOutputOptions)
|
|
|
|
{
|
|
|
|
PRBool useTransparency = PR_TRUE, skipFirstFrame = PR_FALSE;
|
|
|
|
PRUint32 numFrames = 1;
|
|
|
|
PRUint32 numPlays = 0; // For animations, 0 == forever
|
|
|
|
|
|
|
|
// can't initialize more than once
|
|
|
|
if (mImageBuffer != nsnull)
|
|
|
|
return NS_ERROR_ALREADY_INITIALIZED;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// validate input format
|
|
|
|
if (aInputFormat != INPUT_FORMAT_RGB &&
|
|
|
|
aInputFormat != INPUT_FORMAT_RGBA &&
|
|
|
|
aInputFormat != INPUT_FORMAT_HOSTARGB)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
2007-04-24 22:56:53 -07:00
|
|
|
// parse and check any provided output options
|
|
|
|
nsresult rv = ParseOptions(aOutputOptions, &useTransparency, &skipFirstFrame,
|
|
|
|
&numFrames, &numPlays, nsnull, nsnull,
|
|
|
|
nsnull, nsnull, nsnull);
|
2010-01-05 13:46:26 -08:00
|
|
|
if (rv != NS_OK)
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-01-05 13:46:26 -08:00
|
|
|
#ifdef PNG_APNG_SUPPORTED
|
|
|
|
if (numFrames > 1)
|
2007-04-24 22:56:53 -07:00
|
|
|
mIsAnimation = PR_TRUE;
|
2010-01-05 13:46:26 -08:00
|
|
|
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// initialize
|
2007-04-24 22:56:53 -07:00
|
|
|
mPNG = png_create_write_struct(PNG_LIBPNG_VER_STRING,
|
2010-01-05 13:46:26 -08:00
|
|
|
nsnull,
|
2007-04-24 22:56:53 -07:00
|
|
|
ErrorCallback,
|
|
|
|
ErrorCallback);
|
|
|
|
if (! mPNG)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-04-24 22:56:53 -07:00
|
|
|
|
|
|
|
mPNGinfo = png_create_info_struct(mPNG);
|
|
|
|
if (! mPNGinfo) {
|
|
|
|
png_destroy_write_struct(&mPNG, nsnull);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2007-04-24 22:56:53 -07:00
|
|
|
|
|
|
|
// libpng's error handler jumps back here upon an error.
|
|
|
|
// Note: It's important that all png_* callers do this, or errors
|
|
|
|
// will result in a corrupt time-warped stack.
|
|
|
|
if (setjmp(png_jmpbuf(mPNG))) {
|
|
|
|
png_destroy_write_struct(&mPNG, &mPNGinfo);
|
|
|
|
return NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set up to read the data into our image buffer, start out with an 8K
|
|
|
|
// estimated size. Note: we don't have to worry about freeing this data
|
|
|
|
// in this function. It will be freed on object destruction.
|
|
|
|
mImageBufferSize = 8192;
|
|
|
|
mImageBuffer = (PRUint8*)PR_Malloc(mImageBufferSize);
|
|
|
|
if (!mImageBuffer) {
|
2007-04-24 22:56:53 -07:00
|
|
|
png_destroy_write_struct(&mPNG, &mPNGinfo);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
mImageBufferUsed = 0;
|
|
|
|
|
|
|
|
// set our callback for libpng to give us the data
|
2010-01-05 13:46:26 -08:00
|
|
|
png_set_write_fn(mPNG, this, WriteCallback, nsnull);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// include alpha?
|
|
|
|
int colorType;
|
|
|
|
if ((aInputFormat == INPUT_FORMAT_HOSTARGB ||
|
2010-01-05 13:46:26 -08:00
|
|
|
aInputFormat == INPUT_FORMAT_RGBA) &&
|
|
|
|
useTransparency)
|
2007-03-22 10:30:00 -07:00
|
|
|
colorType = PNG_COLOR_TYPE_RGB_ALPHA;
|
|
|
|
else
|
|
|
|
colorType = PNG_COLOR_TYPE_RGB;
|
|
|
|
|
2007-04-24 22:56:53 -07:00
|
|
|
png_set_IHDR(mPNG, mPNGinfo, aWidth, aHeight, 8, colorType,
|
2007-03-22 10:30:00 -07:00
|
|
|
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
|
|
|
|
PNG_FILTER_TYPE_DEFAULT);
|
|
|
|
|
2010-01-05 13:46:26 -08:00
|
|
|
#ifdef PNG_APNG_SUPPORTED
|
2007-04-24 22:56:53 -07:00
|
|
|
if (mIsAnimation) {
|
|
|
|
png_set_first_frame_is_hidden(mPNG, mPNGinfo, skipFirstFrame);
|
|
|
|
png_set_acTL(mPNG, mPNGinfo, numFrames, numPlays);
|
|
|
|
}
|
2010-01-05 13:46:26 -08:00
|
|
|
#endif
|
2007-04-24 22:56:53 -07:00
|
|
|
|
|
|
|
// XXX: support PLTE, gAMA, tRNS, bKGD?
|
|
|
|
|
|
|
|
png_write_info(mPNG, mPNGinfo);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPNGEncoder::AddImageFrame(const PRUint8* aData,
|
2010-01-05 13:46:26 -08:00
|
|
|
PRUint32 aLength, // (unused,
|
|
|
|
// req'd by JS)
|
2007-04-24 22:56:53 -07:00
|
|
|
PRUint32 aWidth,
|
|
|
|
PRUint32 aHeight,
|
|
|
|
PRUint32 aStride,
|
|
|
|
PRUint32 aInputFormat,
|
|
|
|
const nsAString& aFrameOptions)
|
|
|
|
{
|
|
|
|
PRBool useTransparency= PR_TRUE;
|
|
|
|
PRUint32 delay_ms = 500;
|
2010-01-05 13:46:26 -08:00
|
|
|
#ifdef PNG_APNG_SUPPORTED
|
2007-04-24 22:56:53 -07:00
|
|
|
PRUint32 dispose_op = PNG_DISPOSE_OP_NONE;
|
|
|
|
PRUint32 blend_op = PNG_BLEND_OP_SOURCE;
|
2010-01-05 13:46:26 -08:00
|
|
|
#else
|
|
|
|
PRUint32 dispose_op;
|
|
|
|
PRUint32 blend_op;
|
|
|
|
#endif
|
2007-04-24 22:56:53 -07:00
|
|
|
PRUint32 x_offset = 0, y_offset = 0;
|
|
|
|
|
|
|
|
// must be initialized
|
|
|
|
if (mImageBuffer == nsnull)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2007-10-19 21:29:58 -07:00
|
|
|
// EndImageEncode was done, or some error occurred earlier
|
|
|
|
if (!mPNG)
|
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
|
2007-04-24 22:56:53 -07:00
|
|
|
// validate input format
|
|
|
|
if (aInputFormat != INPUT_FORMAT_RGB &&
|
|
|
|
aInputFormat != INPUT_FORMAT_RGBA &&
|
|
|
|
aInputFormat != INPUT_FORMAT_HOSTARGB)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
// libpng's error handler jumps back here upon an error.
|
|
|
|
if (setjmp(png_jmpbuf(mPNG))) {
|
|
|
|
png_destroy_write_struct(&mPNG, &mPNGinfo);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse and check any provided output options
|
|
|
|
nsresult rv = ParseOptions(aFrameOptions, &useTransparency, nsnull,
|
|
|
|
nsnull, nsnull, &dispose_op, &blend_op,
|
|
|
|
&delay_ms, &x_offset, &y_offset);
|
2010-01-05 13:46:26 -08:00
|
|
|
if (rv != NS_OK)
|
|
|
|
return rv;
|
2007-04-24 22:56:53 -07:00
|
|
|
|
2010-01-05 13:46:26 -08:00
|
|
|
#ifdef PNG_APNG_SUPPORTED
|
2007-04-24 22:56:53 -07:00
|
|
|
if (mIsAnimation) {
|
|
|
|
// XXX the row pointers arg (#3) is unused, can it be removed?
|
|
|
|
png_write_frame_head(mPNG, mPNGinfo, nsnull,
|
|
|
|
aWidth, aHeight, x_offset, y_offset,
|
|
|
|
delay_ms, 1000, dispose_op, blend_op);
|
|
|
|
}
|
2010-01-05 13:46:26 -08:00
|
|
|
#endif
|
2007-04-24 22:56:53 -07:00
|
|
|
|
2010-01-05 13:46:26 -08:00
|
|
|
// Stride is the padded width of each row, so it better be longer
|
|
|
|
// (I'm afraid people will not understand what stride means, so
|
|
|
|
// check it well)
|
2007-04-24 22:56:53 -07:00
|
|
|
if ((aInputFormat == INPUT_FORMAT_RGB &&
|
2010-01-05 13:46:26 -08:00
|
|
|
aStride < aWidth * 3) ||
|
|
|
|
((aInputFormat == INPUT_FORMAT_RGBA ||
|
|
|
|
aInputFormat == INPUT_FORMAT_HOSTARGB) &&
|
|
|
|
aStride < aWidth * 4)) {
|
2007-04-24 22:56:53 -07:00
|
|
|
NS_WARNING("Invalid stride for InitFromData/AddImageFrame");
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// write each row: if we add more input formats, we may want to
|
|
|
|
// generalize the conversions
|
|
|
|
if (aInputFormat == INPUT_FORMAT_HOSTARGB) {
|
2010-01-05 13:46:26 -08:00
|
|
|
// PNG requires RGBA with post-multiplied alpha, so we need to
|
|
|
|
// convert
|
2007-03-22 10:30:00 -07:00
|
|
|
PRUint8* row = new PRUint8[aWidth * 4];
|
|
|
|
for (PRUint32 y = 0; y < aHeight; y ++) {
|
|
|
|
ConvertHostARGBRow(&aData[y * aStride], row, aWidth, useTransparency);
|
2007-04-24 22:56:53 -07:00
|
|
|
png_write_row(mPNG, row);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
delete[] row;
|
|
|
|
|
|
|
|
} else if (aInputFormat == INPUT_FORMAT_RGBA && ! useTransparency) {
|
|
|
|
// RBGA, but we need to strip the alpha
|
|
|
|
PRUint8* row = new PRUint8[aWidth * 4];
|
|
|
|
for (PRUint32 y = 0; y < aHeight; y ++) {
|
|
|
|
StripAlpha(&aData[y * aStride], row, aWidth);
|
2007-04-24 22:56:53 -07:00
|
|
|
png_write_row(mPNG, row);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
delete[] row;
|
|
|
|
|
|
|
|
} else if (aInputFormat == INPUT_FORMAT_RGB ||
|
|
|
|
aInputFormat == INPUT_FORMAT_RGBA) {
|
|
|
|
// simple RBG(A), no conversion needed
|
|
|
|
for (PRUint32 y = 0; y < aHeight; y ++) {
|
2007-04-24 22:56:53 -07:00
|
|
|
png_write_row(mPNG, (PRUint8*)&aData[y * aStride]);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
NS_NOTREACHED("Bad format type");
|
2007-04-24 22:56:53 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2010-01-05 13:46:26 -08:00
|
|
|
#ifdef PNG_APNG_SUPPORTED
|
2007-04-24 22:56:53 -07:00
|
|
|
if (mIsAnimation) {
|
|
|
|
png_write_frame_tail(mPNG, mPNGinfo);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-01-05 13:46:26 -08:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-04-24 22:56:53 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPNGEncoder::EndImageEncode()
|
|
|
|
{
|
|
|
|
// must be initialized
|
|
|
|
if (mImageBuffer == nsnull)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2010-01-05 13:46:26 -08:00
|
|
|
// EndImageEncode has already been called, or some error
|
|
|
|
// occurred earlier
|
2007-10-19 21:29:58 -07:00
|
|
|
if (!mPNG)
|
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
|
2007-04-24 22:56:53 -07:00
|
|
|
// libpng's error handler jumps back here upon an error.
|
|
|
|
if (setjmp(png_jmpbuf(mPNG))) {
|
|
|
|
png_destroy_write_struct(&mPNG, &mPNGinfo);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
png_write_end(mPNG, mPNGinfo);
|
|
|
|
png_destroy_write_struct(&mPNG, &mPNGinfo);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-23 10:24:56 -07:00
|
|
|
mFinished = PR_TRUE;
|
|
|
|
NotifyListener();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// if output callback can't get enough memory, it will free our buffer
|
|
|
|
if (!mImageBuffer)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-24 22:56:53 -07:00
|
|
|
nsresult
|
|
|
|
nsPNGEncoder::ParseOptions(const nsAString& aOptions,
|
|
|
|
PRBool* useTransparency,
|
|
|
|
PRBool* skipFirstFrame,
|
|
|
|
PRUint32* numFrames,
|
|
|
|
PRUint32* numPlays,
|
|
|
|
PRUint32* frameDispose,
|
|
|
|
PRUint32* frameBlend,
|
|
|
|
PRUint32* frameDelay,
|
|
|
|
PRUint32* offsetX,
|
|
|
|
PRUint32* offsetY)
|
|
|
|
{
|
2010-01-05 13:46:26 -08:00
|
|
|
#ifdef PNG_APNG_SUPPORTED
|
2008-01-22 15:15:23 -08:00
|
|
|
// Make a copy of aOptions, because strtok() will modify it.
|
|
|
|
nsCAutoString optionsCopy;
|
|
|
|
optionsCopy.Assign(NS_ConvertUTF16toUTF8(aOptions));
|
|
|
|
char* options = optionsCopy.BeginWriting();
|
2007-04-24 22:56:53 -07:00
|
|
|
|
2008-01-22 15:15:23 -08:00
|
|
|
while (char* token = nsCRT::strtok(options, ";", &options)) {
|
2007-04-24 22:56:53 -07:00
|
|
|
// If there's an '=' character, split the token around it.
|
|
|
|
char* equals = token, *value = nsnull;
|
2010-01-05 13:46:26 -08:00
|
|
|
while(*equals != '=' && *equals) {
|
|
|
|
++equals;
|
|
|
|
}
|
|
|
|
if (*equals == '=')
|
|
|
|
value = equals + 1;
|
2007-04-24 22:56:53 -07:00
|
|
|
|
2010-01-05 13:46:26 -08:00
|
|
|
if (value)
|
|
|
|
*equals = '\0'; // temporary null
|
2007-04-24 22:56:53 -07:00
|
|
|
|
|
|
|
// transparency=[yes|no|none]
|
|
|
|
if (nsCRT::strcmp(token, "transparency") == 0 && useTransparency) {
|
2010-01-05 13:46:26 -08:00
|
|
|
if (!value)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2007-04-24 22:56:53 -07:00
|
|
|
|
2010-01-05 13:46:26 -08:00
|
|
|
if (nsCRT::strcmp(value, "none") == 0 ||
|
|
|
|
nsCRT::strcmp(value, "no") == 0) {
|
2007-04-24 22:56:53 -07:00
|
|
|
*useTransparency = PR_FALSE;
|
|
|
|
} else if (nsCRT::strcmp(value, "yes") == 0) {
|
|
|
|
*useTransparency = PR_TRUE;
|
|
|
|
} else {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
// skipfirstframe=[yes|no]
|
2010-01-05 13:46:26 -08:00
|
|
|
} else if (nsCRT::strcmp(token, "skipfirstframe") == 0 &&
|
|
|
|
skipFirstFrame) {
|
|
|
|
if (!value)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2007-04-24 22:56:53 -07:00
|
|
|
|
|
|
|
if (nsCRT::strcmp(value, "no") == 0) {
|
|
|
|
*skipFirstFrame = PR_FALSE;
|
|
|
|
} else if (nsCRT::strcmp(value, "yes") == 0) {
|
|
|
|
*skipFirstFrame = PR_TRUE;
|
|
|
|
} else {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
// frames=#
|
|
|
|
} else if (nsCRT::strcmp(token, "frames") == 0 && numFrames) {
|
2010-01-05 13:46:26 -08:00
|
|
|
if (!value)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2007-04-24 22:56:53 -07:00
|
|
|
|
2010-01-05 13:46:26 -08:00
|
|
|
if (PR_sscanf(value, "%u", numFrames) != 1) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2007-04-24 22:56:53 -07:00
|
|
|
|
|
|
|
// frames=0 is nonsense.
|
2010-01-05 13:46:26 -08:00
|
|
|
if (*numFrames == 0)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2007-04-24 22:56:53 -07:00
|
|
|
|
|
|
|
// plays=#
|
|
|
|
} else if (nsCRT::strcmp(token, "plays") == 0 && numPlays) {
|
2010-01-05 13:46:26 -08:00
|
|
|
if (!value)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2007-04-24 22:56:53 -07:00
|
|
|
|
2010-01-05 13:46:26 -08:00
|
|
|
// plays=0 to loop forever, otherwise play sequence specified
|
|
|
|
// number of times
|
|
|
|
if (PR_sscanf(value, "%u", numPlays) != 1)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2007-04-24 22:56:53 -07:00
|
|
|
|
|
|
|
// dispose=[none|background|previous]
|
|
|
|
} else if (nsCRT::strcmp(token, "dispose") == 0 && frameDispose) {
|
2010-01-05 13:46:26 -08:00
|
|
|
if (!value)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2007-04-24 22:56:53 -07:00
|
|
|
|
|
|
|
if (nsCRT::strcmp(value, "none") == 0) {
|
|
|
|
*frameDispose = PNG_DISPOSE_OP_NONE;
|
|
|
|
} else if (nsCRT::strcmp(value, "background") == 0) {
|
|
|
|
*frameDispose = PNG_DISPOSE_OP_BACKGROUND;
|
|
|
|
} else if (nsCRT::strcmp(value, "previous") == 0) {
|
|
|
|
*frameDispose = PNG_DISPOSE_OP_PREVIOUS;
|
|
|
|
} else {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
// blend=[source|over]
|
|
|
|
} else if (nsCRT::strcmp(token, "blend") == 0 && frameBlend) {
|
2010-01-05 13:46:26 -08:00
|
|
|
if (!value)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2007-04-24 22:56:53 -07:00
|
|
|
|
|
|
|
if (nsCRT::strcmp(value, "source") == 0) {
|
|
|
|
*frameBlend = PNG_BLEND_OP_SOURCE;
|
|
|
|
} else if (nsCRT::strcmp(value, "over") == 0) {
|
|
|
|
*frameBlend = PNG_BLEND_OP_OVER;
|
|
|
|
} else {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
// delay=# (in ms)
|
|
|
|
} else if (nsCRT::strcmp(token, "delay") == 0 && frameDelay) {
|
2010-01-05 13:46:26 -08:00
|
|
|
if (!value)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2007-04-24 22:56:53 -07:00
|
|
|
|
2010-01-05 13:46:26 -08:00
|
|
|
if (PR_sscanf(value, "%u", frameDelay) != 1)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2007-04-24 22:56:53 -07:00
|
|
|
|
|
|
|
// xoffset=#
|
|
|
|
} else if (nsCRT::strcmp(token, "xoffset") == 0 && offsetX) {
|
2010-01-05 13:46:26 -08:00
|
|
|
if (!value)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2007-04-24 22:56:53 -07:00
|
|
|
|
2010-01-05 13:46:26 -08:00
|
|
|
if (PR_sscanf(value, "%u", offsetX) != 1)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2007-04-24 22:56:53 -07:00
|
|
|
|
|
|
|
// yoffset=#
|
|
|
|
} else if (nsCRT::strcmp(token, "yoffset") == 0 && offsetY) {
|
2010-01-05 13:46:26 -08:00
|
|
|
if (!value)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2007-04-24 22:56:53 -07:00
|
|
|
|
2010-01-05 13:46:26 -08:00
|
|
|
if (PR_sscanf(value, "%u", offsetY) != 1)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2007-04-24 22:56:53 -07:00
|
|
|
|
|
|
|
// unknown token name
|
2010-01-05 13:46:26 -08:00
|
|
|
} else
|
2007-04-24 22:56:53 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
2010-01-05 13:46:26 -08:00
|
|
|
if (value)
|
|
|
|
*equals = '='; // restore '=' so strtok doesn't get lost
|
2007-04-24 22:56:53 -07:00
|
|
|
}
|
|
|
|
|
2010-01-05 13:46:26 -08:00
|
|
|
#endif
|
2007-04-24 22:56:53 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* void close (); */
|
|
|
|
NS_IMETHODIMP nsPNGEncoder::Close()
|
|
|
|
{
|
|
|
|
if (mImageBuffer != nsnull) {
|
|
|
|
PR_Free(mImageBuffer);
|
|
|
|
mImageBuffer = nsnull;
|
|
|
|
mImageBufferSize = 0;
|
|
|
|
mImageBufferUsed = 0;
|
|
|
|
mImageBufferReadPoint = 0;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* unsigned long available (); */
|
|
|
|
NS_IMETHODIMP nsPNGEncoder::Available(PRUint32 *_retval)
|
|
|
|
{
|
|
|
|
if (!mImageBuffer)
|
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
|
|
|
|
*_retval = mImageBufferUsed - mImageBufferReadPoint;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-01-05 13:46:26 -08:00
|
|
|
/* [noscript] unsigned long read (in charPtr aBuf,
|
|
|
|
in unsigned long aCount); */
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP nsPNGEncoder::Read(char * aBuf, PRUint32 aCount,
|
|
|
|
PRUint32 *_retval)
|
|
|
|
{
|
|
|
|
return ReadSegments(NS_CopySegmentToBuffer, aBuf, aCount, _retval);
|
|
|
|
}
|
|
|
|
|
2010-01-05 13:46:26 -08:00
|
|
|
/* [noscript] unsigned long readSegments (in nsWriteSegmentFun aWriter,
|
|
|
|
in voidPtr aClosure,
|
|
|
|
in unsigned long aCount); */
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP nsPNGEncoder::ReadSegments(nsWriteSegmentFun aWriter,
|
|
|
|
void *aClosure, PRUint32 aCount,
|
|
|
|
PRUint32 *_retval)
|
|
|
|
{
|
2010-06-23 10:24:56 -07:00
|
|
|
// Avoid another thread reallocing the buffer underneath us
|
|
|
|
mozilla::MonitorAutoEnter autoEnter(mMonitor);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
PRUint32 maxCount = mImageBufferUsed - mImageBufferReadPoint;
|
|
|
|
if (maxCount == 0) {
|
|
|
|
*_retval = 0;
|
2010-06-23 10:24:56 -07:00
|
|
|
return mFinished ? NS_OK : NS_BASE_STREAM_WOULD_BLOCK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aCount > maxCount)
|
|
|
|
aCount = maxCount;
|
2010-01-05 13:46:26 -08:00
|
|
|
nsresult rv =
|
|
|
|
aWriter(this, aClosure,
|
|
|
|
reinterpret_cast<const char*>(mImageBuffer+mImageBufferReadPoint),
|
|
|
|
0, aCount, _retval);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
NS_ASSERTION(*_retval <= aCount, "bad write count");
|
|
|
|
mImageBufferReadPoint += *_retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
// errors returned from the writer end here!
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* boolean isNonBlocking (); */
|
|
|
|
NS_IMETHODIMP nsPNGEncoder::IsNonBlocking(PRBool *_retval)
|
|
|
|
{
|
2010-06-23 10:24:56 -07:00
|
|
|
*_retval = PR_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-23 10:24:56 -07:00
|
|
|
NS_IMETHODIMP nsPNGEncoder::AsyncWait(nsIInputStreamCallback *aCallback,
|
|
|
|
PRUint32 aFlags,
|
|
|
|
PRUint32 aRequestedCount,
|
|
|
|
nsIEventTarget *aTarget)
|
|
|
|
{
|
|
|
|
if (aFlags != 0)
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
|
|
|
|
if (mCallback || mCallbackTarget)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
mCallbackTarget = aTarget;
|
|
|
|
// 0 means "any number of bytes except 0"
|
|
|
|
mNotifyThreshold = aRequestedCount;
|
|
|
|
if (!aRequestedCount)
|
|
|
|
mNotifyThreshold = 1024; // We don't want to notify incessantly
|
|
|
|
|
|
|
|
// We set the callback absolutely last, because NotifyListener uses it to
|
|
|
|
// determine if someone needs to be notified. If we don't set it last,
|
|
|
|
// NotifyListener might try to fire off a notification to a null target
|
|
|
|
// which will generally cause non-threadsafe objects to be used off the main thread
|
|
|
|
mCallback = aCallback;
|
|
|
|
|
|
|
|
// What we are being asked for may be present already
|
|
|
|
NotifyListener();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPNGEncoder::CloseWithStatus(nsresult aStatus)
|
|
|
|
{
|
|
|
|
return Close();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// nsPNGEncoder::ConvertHostARGBRow
|
|
|
|
//
|
|
|
|
// Our colors are stored with premultiplied alphas, but PNGs use
|
|
|
|
// post-multiplied alpha. This swaps to PNG-style alpha.
|
|
|
|
//
|
|
|
|
// Copied from gfx/cairo/cairo/src/cairo-png.c
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPNGEncoder::ConvertHostARGBRow(const PRUint8* aSrc, PRUint8* aDest,
|
2010-01-05 13:46:26 -08:00
|
|
|
PRUint32 aPixelWidth,
|
|
|
|
PRBool aUseTransparency)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
PRUint32 pixelStride = aUseTransparency ? 4 : 3;
|
|
|
|
for (PRUint32 x = 0; x < aPixelWidth; x ++) {
|
|
|
|
const PRUint32& pixelIn = ((const PRUint32*)(aSrc))[x];
|
|
|
|
PRUint8 *pixelOut = &aDest[x * pixelStride];
|
|
|
|
|
|
|
|
PRUint8 alpha = (pixelIn & 0xff000000) >> 24;
|
|
|
|
if (alpha == 0) {
|
|
|
|
pixelOut[0] = pixelOut[1] = pixelOut[2] = pixelOut[3] = 0;
|
|
|
|
} else {
|
|
|
|
pixelOut[0] = (((pixelIn & 0xff0000) >> 16) * 255 + alpha / 2) / alpha;
|
|
|
|
pixelOut[1] = (((pixelIn & 0x00ff00) >> 8) * 255 + alpha / 2) / alpha;
|
|
|
|
pixelOut[2] = (((pixelIn & 0x0000ff) >> 0) * 255 + alpha / 2) / alpha;
|
|
|
|
if (aUseTransparency)
|
|
|
|
pixelOut[3] = alpha;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// nsPNGEncoder::StripAlpha
|
|
|
|
//
|
|
|
|
// Input is RGBA, output is RGB
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPNGEncoder::StripAlpha(const PRUint8* aSrc, PRUint8* aDest,
|
|
|
|
PRUint32 aPixelWidth)
|
|
|
|
{
|
|
|
|
for (PRUint32 x = 0; x < aPixelWidth; x ++) {
|
|
|
|
const PRUint8* pixelIn = &aSrc[x * 4];
|
|
|
|
PRUint8* pixelOut = &aDest[x * 3];
|
|
|
|
pixelOut[0] = pixelIn[0];
|
|
|
|
pixelOut[1] = pixelIn[1];
|
|
|
|
pixelOut[2] = pixelIn[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-24 22:56:53 -07:00
|
|
|
// nsPNGEncoder::ErrorCallback
|
|
|
|
|
|
|
|
void // static
|
2010-01-05 13:46:26 -08:00
|
|
|
nsPNGEncoder::ErrorCallback(png_structp png_ptr,
|
|
|
|
png_const_charp warning_msg)
|
2007-04-24 22:56:53 -07:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2010-01-05 13:46:26 -08:00
|
|
|
// XXX: these messages are probably useful callers...
|
|
|
|
// use nsIConsoleService?
|
2007-04-24 22:56:53 -07:00
|
|
|
PR_fprintf(PR_STDERR, "PNG Encoder: %s\n", warning_msg);;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// nsPNGEncoder::WriteCallback
|
|
|
|
|
|
|
|
void // static
|
2010-01-05 13:46:26 -08:00
|
|
|
nsPNGEncoder::WriteCallback(png_structp png, png_bytep data,
|
|
|
|
png_size_t size)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
nsPNGEncoder* that = static_cast<nsPNGEncoder*>(png_get_io_ptr(png));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (! that->mImageBuffer)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (that->mImageBufferUsed + size > that->mImageBufferSize) {
|
2010-06-23 10:24:56 -07:00
|
|
|
// When we're reallocing the buffer we need to take the lock to ensure
|
|
|
|
// that nobody is trying to read from the buffer we are destroying
|
|
|
|
mozilla::MonitorAutoEnter autoEnter(that->mMonitor);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// expand buffer, just double each time
|
|
|
|
that->mImageBufferSize *= 2;
|
|
|
|
PRUint8* newBuf = (PRUint8*)PR_Realloc(that->mImageBuffer,
|
|
|
|
that->mImageBufferSize);
|
|
|
|
if (! newBuf) {
|
|
|
|
// can't resize, just zero (this will keep us from writing more)
|
|
|
|
PR_Free(that->mImageBuffer);
|
|
|
|
that->mImageBufferSize = 0;
|
|
|
|
that->mImageBufferUsed = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
that->mImageBuffer = newBuf;
|
|
|
|
}
|
|
|
|
memcpy(&that->mImageBuffer[that->mImageBufferUsed], data, size);
|
|
|
|
that->mImageBufferUsed += size;
|
2010-06-23 10:24:56 -07:00
|
|
|
that->NotifyListener();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPNGEncoder::NotifyListener()
|
|
|
|
{
|
|
|
|
// We might call this function on multiple threads (any threads that call
|
|
|
|
// AsyncWait and any that do encoding) so we lock to avoid notifying the
|
|
|
|
// listener twice about the same data (which generally leads to a truncated
|
|
|
|
// image).
|
|
|
|
mozilla::MonitorAutoEnter autoEnter(mMonitor);
|
|
|
|
|
|
|
|
if (mCallback &&
|
|
|
|
(mImageBufferUsed - mImageBufferReadPoint >= mNotifyThreshold ||
|
|
|
|
mFinished)) {
|
|
|
|
nsCOMPtr<nsIInputStreamCallback> callback;
|
|
|
|
if (mCallbackTarget) {
|
|
|
|
NS_NewInputStreamReadyEvent(getter_AddRefs(callback),
|
|
|
|
mCallback,
|
|
|
|
mCallbackTarget);
|
|
|
|
} else {
|
|
|
|
callback = mCallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(callback, "Shouldn't fail to make the callback");
|
|
|
|
// Null the callback first because OnInputStreamReady could reenter
|
|
|
|
// AsyncWait
|
|
|
|
mCallback = nsnull;
|
|
|
|
mCallbackTarget = nsnull;
|
|
|
|
mNotifyThreshold = 0;
|
|
|
|
|
|
|
|
callback->OnInputStreamReady(this);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|