Bug 245684, add JPEG encoder. r=pavlov, sr=vladimir

This commit is contained in:
brettw%gmail.com 2005-11-15 18:10:08 +00:00
Родитель dbe1421d72
Коммит 9783de077a
7 изменённых файлов: 576 добавлений и 3 удалений

2
configure поставляемый
Просмотреть файл

@ -12674,7 +12674,7 @@ MOZ_DBGRINFO_MODULES=
MOZ_ENABLE_LIBXUL=
MOZ_EXTENSIONS_ALL=" cookie wallet content-packs xml-rpc xmlextras help p3p pref venkman inspector irc universalchardet typeaheadfind webservices spellcheck gnomevfs auth sroaming xmlterm datetime finger cview layout-debug tasks sql xforms permissions schema-validation reporter"
MOZ_IMG_DECODERS_DEFAULT="png gif jpeg bmp xbm"
MOZ_IMG_ENCODERS_DEFAULT="png"
MOZ_IMG_ENCODERS_DEFAULT="png jpeg"
MOZ_IPCD=
MOZ_JSDEBUGGER=1
MOZ_JSLOADER=1

Просмотреть файл

@ -4046,7 +4046,7 @@ MOZ_DBGRINFO_MODULES=
MOZ_ENABLE_LIBXUL=
MOZ_EXTENSIONS_ALL=" cookie wallet content-packs xml-rpc xmlextras help p3p pref venkman inspector irc universalchardet typeaheadfind webservices spellcheck gnomevfs auth sroaming xmlterm datetime finger cview layout-debug tasks sql xforms permissions schema-validation reporter"
MOZ_IMG_DECODERS_DEFAULT="png gif jpeg bmp xbm"
MOZ_IMG_ENCODERS_DEFAULT="png"
MOZ_IMG_ENCODERS_DEFAULT="png jpeg"
MOZ_IPCD=
MOZ_JSDEBUGGER=1
MOZ_JSLOADER=1

Просмотреть файл

@ -89,6 +89,10 @@
// png
#include "nsPNGEncoder.h"
#endif
#ifdef IMG_BUILD_ENCODER_jpeg
// jpeg
#include "nsJPEGEncoder.h"
#endif
// objects that just require generic constructors
@ -108,6 +112,10 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(nsGIFDecoder2)
// jpeg
NS_GENERIC_FACTORY_CONSTRUCTOR(nsJPEGDecoder)
#endif
#ifdef IMG_BUILD_ENCODER_jpeg
// jpeg
NS_GENERIC_FACTORY_CONSTRUCTOR(nsJPEGEncoder)
#endif
#ifdef IMG_BUILD_DECODER_bmp
// bmp
@ -237,6 +245,13 @@ static const nsModuleComponentInfo components[] =
"@mozilla.org/image/decoder;2?type=image/jpg",
nsJPEGDecoderConstructor, },
#endif
#ifdef IMG_BUILD_ENCODER_jpeg
// jpeg (encoder)
{ "JPEG Encoder",
NS_JPEGENCODER_CID,
"@mozilla.org/image/encoder;2?type=image/jpeg",
nsJPEGEncoderConstructor, },
#endif
#ifdef IMG_BUILD_DECODER_bmp
// bmp
@ -267,7 +282,7 @@ static const nsModuleComponentInfo components[] =
#endif
#ifdef IMG_BUILD_ENCODER_png
// png
{ "PNG Decoder",
{ "PNG Encoder",
NS_PNGENCODER_CID,
"@mozilla.org/image/encoder;2?type=image/png",
nsPNGEncoderConstructor, },

Просмотреть файл

@ -0,0 +1,2 @@
Makefile
.deps

Просмотреть файл

@ -0,0 +1,62 @@
# ***** 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 mozilla.org code.
#
# The Initial Developer of the Original Code is Mozilla Foundation
# Portions created by the Initial Developer are Copyright (C) 2005
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#
# 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 *****
DEPTH = ../../../..
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
include $(DEPTH)/config/autoconf.mk
MODULE = imgjpege
LIBRARY_NAME = imgjpege_s
FORCE_STATIC_LIB = 1
MODULE_NAME = imgEncoderJPEGModule
LIBXUL_LIBRARY = 1
EXTRA_DSO_LIBS = gkgfx
REQUIRES = xpcom \
gfx \
string \
imglib2 \
$(JPEG_REQUIRES) \
$(NULL)
CPPSRCS = nsJPEGEncoder.cpp
include $(topsrcdir)/config/rules.mk

Просмотреть файл

@ -0,0 +1,405 @@
/* -*- 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 JPEG 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>
*
* 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 ***** */
#include "nsJPEGEncoder.h"
#include "prmem.h"
#include "prprf.h"
#include "nsString.h"
#include <setjmp.h>
#include "jerror.h"
NS_IMPL_ISUPPORTS2(nsJPEGEncoder, imgIEncoder, nsIInputStream)
// used to pass error info through the JPEG library
struct encoder_error_mgr {
jpeg_error_mgr pub;
jmp_buf setjmp_buffer;
};
nsJPEGEncoder::nsJPEGEncoder() : mImageBuffer(nsnull), mImageBufferSize(0),
mImageBufferUsed(0), mImageBufferReadPoint(0)
{
}
nsJPEGEncoder::~nsJPEGEncoder()
{
if (mImageBuffer) {
PR_Free(mImageBuffer);
mImageBuffer = nsnull;
}
}
// nsJPEGEncoder::InitFromData
//
// One output option is supported: "quality=X" where X is an integer in the
// range 0-100. Higher values for X give better quality.
//
// Transparency is always discarded.
NS_IMETHODIMP nsJPEGEncoder::InitFromData(const PRUint8* aData,
PRUint32 aLength, // (unused, req'd by JS)
PRUint32 aWidth,
PRUint32 aHeight,
PRUint32 aStride,
PRUint32 aInputFormat,
const nsAString& aOutputOptions)
{
// validate input format
if (aInputFormat != INPUT_FORMAT_RGB &&
aInputFormat != INPUT_FORMAT_RGBA &&
aInputFormat != INPUT_FORMAT_HOSTARGB)
return NS_ERROR_INVALID_ARG;
// 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)
if ((aInputFormat == INPUT_FORMAT_RGB &&
aStride < aWidth * 3) ||
((aInputFormat == INPUT_FORMAT_RGBA || aInputFormat == INPUT_FORMAT_HOSTARGB) &&
aStride < aWidth * 4)) {
NS_WARNING("Invalid stride for InitFromData");
return NS_ERROR_INVALID_ARG;
}
// can't initialize more than once
if (mImageBuffer != nsnull)
return NS_ERROR_ALREADY_INITIALIZED;
// options: we only have one option so this is easy
int quality = 50;
if (aOutputOptions.Length() > 0) {
// have options string
const nsString qualityPrefix(NS_LITERAL_STRING("quality="));
if (aOutputOptions.Length() > qualityPrefix.Length() &&
StringBeginsWith(aOutputOptions, qualityPrefix)) {
// have quality string
nsCString value = NS_ConvertUTF16toUTF8(Substring(aOutputOptions,
qualityPrefix.Length()));
int newquality = -1;
if (PR_sscanf(PromiseFlatCString(value).get(), "%d", &newquality) == 1) {
if (newquality >= 0 && newquality <= 100) {
quality = newquality;
} else {
NS_WARNING("Quality value out of range, should be 0-100, using default");
}
} else {
NS_WARNING("Quality value invalid, should be integer 0-100, using default");
}
}
}
jpeg_compress_struct cinfo;
// We set up the normal JPEG error routines, then override error_exit.
// This must be done before the call to create_compress
encoder_error_mgr errmgr;
cinfo.err = jpeg_std_error(&errmgr.pub);
errmgr.pub.error_exit = errorExit;
// Establish the setjmp return context for my_error_exit to use.
if (setjmp(errmgr.setjmp_buffer)) {
// If we get here, the JPEG code has signaled an error.
// We need to clean up the JPEG object, close the input file, and return.
return NS_ERROR_FAILURE;
}
jpeg_create_compress(&cinfo);
cinfo.image_width = aWidth;
cinfo.image_height = aHeight;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
cinfo.data_precision = 8;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, 1); // quality here is 0-100
// set up the destination manager
jpeg_destination_mgr destmgr;
destmgr.init_destination = initDestination;
destmgr.empty_output_buffer = emptyOutputBuffer;
destmgr.term_destination = termDestination;
cinfo.dest = &destmgr;
cinfo.client_data = this;
jpeg_start_compress(&cinfo, 1);
// feed it the rows
if (aInputFormat == INPUT_FORMAT_RGB) {
while (cinfo.next_scanline < cinfo.image_height) {
const PRUint8* row = &aData[cinfo.next_scanline * aStride];
jpeg_write_scanlines(&cinfo, NS_CONST_CAST(PRUint8**, &row), 1);
}
} else if (aInputFormat == INPUT_FORMAT_RGBA) {
PRUint8* row = new PRUint8[aWidth * 3];
while (cinfo.next_scanline < cinfo.image_height) {
StripAlpha(&aData[cinfo.next_scanline * aStride], row, aWidth);
jpeg_write_scanlines(&cinfo, &row, 1);
}
delete[] row;
} else if (aInputFormat == INPUT_FORMAT_HOSTARGB) {
PRUint8* row = new PRUint8[aWidth * 3];
while (cinfo.next_scanline < cinfo.image_height) {
ConvertHostARGBRow(&aData[cinfo.next_scanline * aStride], row, aWidth);
jpeg_write_scanlines(&cinfo, &row, 1);
}
delete[] row;
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
// if output callback can't get enough memory, it will free our buffer
if (mImageBuffer)
return NS_OK;
else
return NS_ERROR_OUT_OF_MEMORY;
}
/* void close (); */
NS_IMETHODIMP nsJPEGEncoder::Close()
{
if (mImageBuffer != nsnull) {
PR_Free(mImageBuffer);
mImageBuffer = nsnull;
mImageBufferSize = 0;
mImageBufferUsed = 0;
}
return NS_OK;
}
/* unsigned long available (); */
NS_IMETHODIMP nsJPEGEncoder::Available(PRUint32 *_retval)
{
*_retval = mImageBufferUsed - mImageBufferReadPoint;
return NS_OK;
}
/* [noscript] unsigned long read (in charPtr aBuf, in unsigned long aCount); */
NS_IMETHODIMP nsJPEGEncoder::Read(char * aBuf, PRUint32 aCount, PRUint32 *_retval)
{
if (mImageBufferReadPoint >= mImageBufferUsed) {
// done reading
*_retval = 0;
return NS_OK;
}
PRUint32 toRead = mImageBufferUsed - mImageBufferReadPoint;
if (aCount < toRead)
toRead = aCount;
memcpy(aBuf, &mImageBuffer[mImageBufferReadPoint], toRead);
mImageBufferReadPoint += toRead;
*_retval = toRead;
return NS_OK;
}
/* [noscript] unsigned long readSegments (in nsWriteSegmentFun aWriter, in voidPtr aClosure, in unsigned long aCount); */
NS_IMETHODIMP nsJPEGEncoder::ReadSegments(nsWriteSegmentFun aWriter, void *aClosure, PRUint32 aCount, PRUint32 *_retval)
{
if (mImageBufferUsed == 0)
return NS_ERROR_FAILURE;
PRUint32 writeCount = 0;
aWriter(this, aClosure, (const char*)mImageBuffer, 0, mImageBufferUsed, &writeCount);
if (writeCount > 0)
return NS_OK;
else
return NS_ERROR_FAILURE; // some problem with callback
}
/* boolean isNonBlocking (); */
NS_IMETHODIMP nsJPEGEncoder::IsNonBlocking(PRBool *_retval)
{
*_retval = PR_TRUE;
return NS_OK;
}
// nsJPEGEncoder::ConvertHostARGBRow
//
// Our colors are stored with premultiplied alphas, but we need
// an output with no alpha in machine-independent byte order.
//
// See gfx/cairo/cairo/src/cairo-png.c
void
nsJPEGEncoder::ConvertHostARGBRow(const PRUint8* aSrc, PRUint8* aDest,
PRUint32 aPixelWidth)
{
for (PRUint32 x = 0; x < aPixelWidth; x ++) {
const PRUint32& pixelIn = ((const PRUint32*)(aSrc))[x];
PRUint8 *pixelOut = &aDest[x * 3];
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;
}
}
}
// nsJPEGEncoder::StripAlpha
//
// Input is RGBA, output is RGB
void
nsJPEGEncoder::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];
}
}
// nsJPEGEncoder::initDestination
//
// Initialize destination. This is called by jpeg_start_compress() before
// any data is actually written. It must initialize next_output_byte and
// free_in_buffer. free_in_buffer must be initialized to a positive value.
void // static
nsJPEGEncoder::initDestination(jpeg_compress_struct* cinfo)
{
nsJPEGEncoder* that = NS_STATIC_CAST(nsJPEGEncoder*, cinfo->client_data);
NS_ASSERTION(! that->mImageBuffer, "Image buffer already initialized");
that->mImageBufferSize = 8192;
that->mImageBuffer = (PRUint8*)PR_Malloc(that->mImageBufferSize);
that->mImageBufferUsed = 0;
cinfo->dest->next_output_byte = that->mImageBuffer;
cinfo->dest->free_in_buffer = that->mImageBufferSize;
}
// nsJPEGEncoder::emptyOutputBuffer
//
// This is called whenever the buffer has filled (free_in_buffer reaches
// zero). In typical applications, it should write out the *entire* buffer
// (use the saved start address and buffer length; ignore the current state
// of next_output_byte and free_in_buffer). Then reset the pointer & count
// to the start of the buffer, and return TRUE indicating that the buffer
// has been dumped. free_in_buffer must be set to a positive value when
// TRUE is returned. A FALSE return should only be used when I/O suspension
// is desired (this operating mode is discussed in the next section).
boolean // static
nsJPEGEncoder::emptyOutputBuffer(jpeg_compress_struct* cinfo)
{
nsJPEGEncoder* that = NS_STATIC_CAST(nsJPEGEncoder*, cinfo->client_data);
NS_ASSERTION(that->mImageBuffer, "No buffer to empty!");
that->mImageBufferUsed = that->mImageBufferSize;
// expand buffer, just double size 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->mImageBuffer = nsnull;
that->mImageBufferSize = 0;
that->mImageBufferUsed = 0;
// this seems to be the only way to do errors through the JPEG library
longjmp(((encoder_error_mgr*)(cinfo->err))->setjmp_buffer,
NS_ERROR_OUT_OF_MEMORY);
}
that->mImageBuffer = newBuf;
cinfo->dest->next_output_byte = &that->mImageBuffer[that->mImageBufferUsed];
cinfo->dest->free_in_buffer = that->mImageBufferSize - that->mImageBufferUsed;
return 1;
}
// nsJPEGEncoder::termDestination
//
// Terminate destination --- called by jpeg_finish_compress() after all data
// has been written. In most applications, this must flush any data
// remaining in the buffer. Use either next_output_byte or free_in_buffer
// to determine how much data is in the buffer.
void // static
nsJPEGEncoder::termDestination(jpeg_compress_struct* cinfo)
{
nsJPEGEncoder* that = NS_STATIC_CAST(nsJPEGEncoder*, cinfo->client_data);
if (! that->mImageBuffer)
return;
that->mImageBufferUsed = cinfo->dest->next_output_byte - that->mImageBuffer;
NS_ASSERTION(that->mImageBufferUsed < that->mImageBufferSize,
"JPEG library busted, got a bad image buffer size");
}
// nsJPEGEncoder::errorExit
//
// Override the standard error method in the IJG JPEG decoder code. This
// was mostly copied from nsJPEGDecoder.cpp
void // static
nsJPEGEncoder::errorExit(jpeg_common_struct* cinfo)
{
nsresult error_code;
encoder_error_mgr *err = (encoder_error_mgr *) cinfo->err;
// Convert error to a browser error code
switch (cinfo->err->msg_code) {
case JERR_OUT_OF_MEMORY:
error_code = NS_ERROR_OUT_OF_MEMORY;
default:
error_code = NS_ERROR_FAILURE;
}
// Return control to the setjmp point.
longjmp(err->setjmp_buffer, error_code);
}

Просмотреть файл

@ -0,0 +1,89 @@
/* -*- 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 JPEG 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>
*
* 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 ***** */
#include "imgIEncoder.h"
// needed for JPEG library
#include <stdio.h>
extern "C" {
#include "jpeglib.h"
}
#define NS_JPEGENCODER_CID \
{ /* ac2bb8fe-eeeb-4572-b40f-be03932b56e0 */ \
0xac2bb8fe, \
0xeeeb, \
0x4572, \
{0xb4, 0x0f, 0xbe, 0x03, 0x93, 0x2b, 0x56, 0xe0} \
}
// Provides JPEG encoding functionality. Use InitFromData() to do the
// encoding. See that function definition for encoding options.
class nsJPEGEncoder : public imgIEncoder
{
public:
NS_DECL_ISUPPORTS
NS_DECL_IMGIENCODER
NS_DECL_NSIINPUTSTREAM
nsJPEGEncoder();
private:
~nsJPEGEncoder();
protected:
void ConvertHostARGBRow(const PRUint8* aSrc, PRUint8* aDest,
PRUint32 aPixelWidth);
void StripAlpha(const PRUint8* aSrc, PRUint8* aDest, PRUint32 aPixelWidth);
static void initDestination(jpeg_compress_struct* cinfo);
static boolean emptyOutputBuffer(jpeg_compress_struct* cinfo);
static void termDestination(jpeg_compress_struct* cinfo);
static void errorExit(jpeg_common_struct* cinfo);
// image buffer
PRUint8* mImageBuffer;
PRUint32 mImageBufferSize;
PRUint32 mImageBufferUsed;
PRUint32 mImageBufferReadPoint;
};