зеркало из https://github.com/mozilla/gecko-dev.git
some code for new imagelib (not part of bulid)
This commit is contained in:
Родитель
3cea0ec423
Коммит
a9cbc5ee24
|
@ -0,0 +1,2 @@
|
|||
Makefile
|
||||
.deps
|
|
@ -0,0 +1,32 @@
|
|||
#
|
||||
# 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 Netscape
|
||||
# Communications Corporation. Portions created by Netscape are
|
||||
# Copyright (C) 2001 Netscape Communications Corporation. All
|
||||
# Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
#
|
||||
|
||||
DEPTH = ../../..
|
||||
topsrcdir = @top_srcdir@
|
||||
srcdir = @srcdir@
|
||||
VPATH = @srcdir@
|
||||
|
||||
include $(DEPTH)/config/autoconf.mk
|
||||
|
||||
DIRS = ppm png
|
||||
|
||||
include $(topsrcdir)/config/rules.mk
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
#!nmake
|
||||
#
|
||||
# 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 Netscape
|
||||
# Communications Corporation. Portions created by Netscape are
|
||||
# Copyright (C) 2001 Netscape Communications Corporation. All
|
||||
# Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# Stuart Parmenter <pavlov@netscape.com>
|
||||
#
|
||||
|
||||
DEPTH=..\..\..\..
|
||||
include <$(DEPTH)/config/config.mak>
|
||||
|
||||
MODULE = imgjpeg
|
||||
LIBRARY_NAME = imgjpeg
|
||||
DLL = $(OBJDIR)\$(LIBRARY_NAME).dll
|
||||
MAKE_OBJ_TYPE = DLL
|
||||
|
||||
OBJS = \
|
||||
.\$(OBJDIR)\nsJPEGDecoder.obj \
|
||||
.\$(OBJDIR)\nsJPEGFactory.obj \
|
||||
$(NULL)
|
||||
|
||||
LLIBS=\
|
||||
$(LIBNSPR) \
|
||||
$(DIST)\lib\jpeg3250.lib \
|
||||
$(DIST)\lib\xpcom.lib \
|
||||
$(NULL)
|
||||
|
||||
include <$(DEPTH)\config\rules.mak>
|
||||
|
||||
install:: $(DLL)
|
||||
$(MAKE_INSTALL) .\$(OBJDIR)\$(LIBRARY_NAME).dll $(DIST)\bin\components
|
||||
$(MAKE_INSTALL) .\$(OBJDIR)\$(LIBRARY_NAME).lib $(DIST)\lib
|
||||
|
||||
clobber::
|
||||
rm -f $(DIST)\bin\components\$(LIBRARY_NAME).dll
|
||||
rm -f $(DIST)\lib\$(LIBRARY_NAME).lib
|
|
@ -0,0 +1,723 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* 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 Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 2001 Netscape Communications Corporation.
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Stuart Parmenter <pavlov@netscape.com>
|
||||
*
|
||||
*/
|
||||
|
||||
#include "nsJPEGDecoder.h"
|
||||
|
||||
#include "nsIInputStream.h"
|
||||
|
||||
#include "nspr.h"
|
||||
|
||||
#include "nsUnitConverters.h"
|
||||
|
||||
#include "nsCRT.h"
|
||||
|
||||
NS_IMPL_ISUPPORTS2(nsJPEGDecoder, nsIImageDecoder, nsIOutputStream)
|
||||
|
||||
|
||||
void PR_CALLBACK init_source (j_decompress_ptr jd);
|
||||
boolean PR_CALLBACK fill_input_buffer (j_decompress_ptr jd);
|
||||
void PR_CALLBACK skip_input_data (j_decompress_ptr jd, long num_bytes);
|
||||
void PR_CALLBACK term_source (j_decompress_ptr jd);
|
||||
void PR_CALLBACK il_error_exit (j_common_ptr cinfo);
|
||||
|
||||
/* Normal JFIF markers can't have more bytes than this. */
|
||||
#define MAX_JPEG_MARKER_LENGTH (((PRUint32)1 << 16) - 1)
|
||||
|
||||
|
||||
/* Possible states for JPEG source manager */
|
||||
enum data_source_state {
|
||||
dss_consuming_backtrack_buffer = 0, /* Must be zero for init purposes */
|
||||
dss_consuming_netlib_buffer
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* Implementation of a JPEG src object that understands our state machine
|
||||
*/
|
||||
typedef struct {
|
||||
/* public fields; must be first in this struct! */
|
||||
struct jpeg_source_mgr pub;
|
||||
|
||||
|
||||
nsJPEGDecoder *decoder;
|
||||
|
||||
|
||||
int bytes_to_skip; /* remaining bytes to skip */
|
||||
|
||||
JOCTET *netlib_buffer; /* next buffer for fill_input_buffer */
|
||||
PRUint32 netlib_buflen;
|
||||
|
||||
enum data_source_state state;
|
||||
|
||||
|
||||
/*
|
||||
* Buffer of "remaining" characters left over after a call to
|
||||
* fill_input_buffer(), when no additional data is available.
|
||||
*/
|
||||
JOCTET *backtrack_buffer;
|
||||
size_t backtrack_buffer_size; /* Allocated size of backtrack_buffer */
|
||||
size_t backtrack_buflen; /* Offset of end of active backtrack data */
|
||||
size_t backtrack_num_unread_bytes; /* Length of active backtrack data */
|
||||
} decoder_source_mgr;
|
||||
|
||||
|
||||
nsJPEGDecoder::nsJPEGDecoder()
|
||||
{
|
||||
NS_INIT_ISUPPORTS();
|
||||
|
||||
mState = JPEG_HEADER;
|
||||
|
||||
mBuf = nsnull;
|
||||
mBufLen = 0;
|
||||
|
||||
mCurDataLen = 0;
|
||||
mSamples = nsnull;
|
||||
mSamples3 = nsnull;
|
||||
|
||||
mBytesToSkip = 0;
|
||||
}
|
||||
|
||||
nsJPEGDecoder::~nsJPEGDecoder()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
/** nsIImageDecoder methods **/
|
||||
|
||||
/* void init (in nsIImageRequest aRequest); */
|
||||
NS_IMETHODIMP nsJPEGDecoder::Init(nsIImageRequest *aRequest)
|
||||
{
|
||||
mRequest = aRequest;
|
||||
|
||||
aRequest->GetImage(getter_AddRefs(mImage));
|
||||
|
||||
|
||||
/* Step 1: allocate and initialize JPEG decompression object */
|
||||
|
||||
/* Now we can initialize the JPEG decompression object. */
|
||||
jpeg_create_decompress(&mInfo);
|
||||
|
||||
decoder_source_mgr *src;
|
||||
|
||||
if (mInfo.src == NULL) {
|
||||
src = PR_NEWZAP(decoder_source_mgr);
|
||||
if (!src) {
|
||||
return PR_FALSE;
|
||||
}
|
||||
mInfo.src = (struct jpeg_source_mgr *) src;
|
||||
}
|
||||
|
||||
mInfo.err = jpeg_std_error(&mErr.pub);
|
||||
|
||||
mErr.pub.error_exit = il_error_exit;
|
||||
|
||||
/* Setup callback functions. */
|
||||
src->pub.init_source = init_source;
|
||||
src->pub.fill_input_buffer = fill_input_buffer;
|
||||
src->pub.skip_input_data = skip_input_data;
|
||||
src->pub.resync_to_restart = jpeg_resync_to_restart;
|
||||
src->pub.term_source = term_source;
|
||||
|
||||
src->decoder = this;
|
||||
|
||||
|
||||
#if 0
|
||||
/* We set up the normal JPEG error routines, then override error_exit. */
|
||||
mInfo.err = jpeg_std_error(&jerr.pub);
|
||||
jerr.pub.error_exit = my_error_exit;
|
||||
/* Establish the setjmp return context for my_error_exit to use. */
|
||||
if (setjmp(jerr.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.
|
||||
*/
|
||||
jpeg_destroy_decompress(&cinfo);
|
||||
fclose(infile);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* readonly attribute nsIImageRequest request; */
|
||||
NS_IMETHODIMP nsJPEGDecoder::GetRequest(nsIImageRequest * *aRequest)
|
||||
{
|
||||
*aRequest = mRequest;
|
||||
NS_ADDREF(*aRequest);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/** nsIOutputStream methods **/
|
||||
|
||||
/* void close (); */
|
||||
NS_IMETHODIMP nsJPEGDecoder::Close()
|
||||
{
|
||||
// XXX progressive? ;)
|
||||
OutputScanlines(mInfo.output_height);
|
||||
|
||||
/* Step 7: Finish decompression */
|
||||
|
||||
(void) jpeg_finish_decompress(&mInfo);
|
||||
/* We can ignore the return value since suspension is not possible
|
||||
* with the stdio data source.
|
||||
*/
|
||||
|
||||
|
||||
/* Step 8: Release JPEG decompression object */
|
||||
|
||||
/* This is an important step since it will release a good deal of memory. */
|
||||
jpeg_destroy_decompress(&mInfo);
|
||||
|
||||
|
||||
|
||||
|
||||
printf("nsJPEGDecoder::Close()\n");
|
||||
|
||||
|
||||
PR_FREEIF(mBuf);
|
||||
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* void flush (); */
|
||||
NS_IMETHODIMP nsJPEGDecoder::Flush()
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
/* unsigned long write (in string buf, in unsigned long count); */
|
||||
NS_IMETHODIMP nsJPEGDecoder::Write(const char *buf, PRUint32 count, PRUint32 *_retval)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
/* unsigned long writeFrom (in nsIInputStream inStr, in unsigned long count); */
|
||||
NS_IMETHODIMP nsJPEGDecoder::WriteFrom(nsIInputStream *inStr, PRUint32 count, PRUint32 *_retval)
|
||||
{
|
||||
/* We use our private extension JPEG error handler.
|
||||
* Note that this struct must live as long as the main JPEG parameter
|
||||
* struct, to avoid dangling-pointer problems.
|
||||
*/
|
||||
// XXX above what is this?
|
||||
|
||||
PRUint32 readLen;
|
||||
|
||||
if (!mBuf) {
|
||||
mBuf = (char*)PR_Malloc(count);
|
||||
mBufLen = count;
|
||||
}
|
||||
|
||||
if (mBuf && count > mBufLen)
|
||||
{
|
||||
mBuf = (char *)PR_Realloc(mBuf, count);
|
||||
mBufLen = count;
|
||||
}
|
||||
|
||||
nsresult rv = inStr->Read(mBuf, count, &readLen);
|
||||
mCurDataLen = readLen;
|
||||
|
||||
/* Step 2: specify data source (eg, a file) */
|
||||
|
||||
// i think this magically happens
|
||||
// jpeg_stdio_src(&cinfo, infile);
|
||||
|
||||
|
||||
|
||||
/* Register new buffer contents with data source manager. */
|
||||
|
||||
decoder_source_mgr *src = NS_REINTERPRET_CAST(decoder_source_mgr *, mInfo.src);
|
||||
|
||||
if (!mSamples && jpeg_read_header(&mInfo, TRUE) != JPEG_SUSPENDED) {
|
||||
|
||||
|
||||
/* FIXME -- Should reset dct_method and dither mode
|
||||
* for final pass of progressive JPEG
|
||||
*/
|
||||
mInfo.dct_method = JDCT_FASTEST;
|
||||
mInfo.dither_mode = JDITHER_ORDERED;
|
||||
mInfo.do_fancy_upsampling = FALSE;
|
||||
mInfo.enable_2pass_quant = FALSE;
|
||||
mInfo.do_block_smoothing = TRUE;
|
||||
|
||||
/*
|
||||
* Don't allocate a giant and superfluous memory buffer
|
||||
* when the image is a sequential JPEG.
|
||||
*/
|
||||
mInfo.buffered_image = jpeg_has_multiple_scans(&mInfo);
|
||||
|
||||
/* Used to set up image size so arrays can be allocated */
|
||||
jpeg_calc_output_dimensions(&mInfo);
|
||||
|
||||
mImage->Init(mInfo.image_width, mInfo.image_height, nsIGFXFormat::RGB);
|
||||
|
||||
|
||||
/*
|
||||
* Make a one-row-high sample array that will go away
|
||||
* when done with image. Always make it big enough to
|
||||
* hold an RGB row. Since this uses the IJG memory
|
||||
* manager, it must be allocated before the call to
|
||||
* jpeg_start_compress().
|
||||
*/
|
||||
int row_stride;
|
||||
row_stride = mInfo.output_width * mInfo.output_components;
|
||||
mSamples = (*mInfo.mem->alloc_sarray)((j_common_ptr) &mInfo,
|
||||
JPOOL_IMAGE,
|
||||
row_stride, 1);
|
||||
|
||||
/* Allocate RGB buffer for conversion from greyscale. */
|
||||
if (mInfo.output_components != 3) {
|
||||
row_stride = mInfo.output_width * 3;
|
||||
mSamples3 = (*mInfo.mem->alloc_sarray)((j_common_ptr) &mInfo,
|
||||
JPOOL_IMAGE,
|
||||
row_stride, 1);
|
||||
}
|
||||
|
||||
} else {
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
/* Step 3: read file parameters with jpeg_read_header() */
|
||||
// (void) jpeg_read_header(&mInfo, TRUE);
|
||||
/* We can ignore the return value from jpeg_read_header since
|
||||
* (a) suspension is not possible with the stdio data source, and
|
||||
* (b) we passed TRUE to reject a tables-only JPEG file as an error.
|
||||
* See libjpeg.doc for more info.
|
||||
*/
|
||||
|
||||
/* Step 4: set parameters for decompression */
|
||||
|
||||
/* In this example, we don't need to change any of the defaults set by
|
||||
* jpeg_read_header(), so we do nothing here.
|
||||
*/
|
||||
|
||||
/* Step 5: Start decompressor */
|
||||
|
||||
(void) jpeg_start_decompress(&mInfo);
|
||||
/* We can ignore the return value since suspension is not possible
|
||||
* with the stdio data source.
|
||||
*/
|
||||
|
||||
int status;
|
||||
do {
|
||||
status = jpeg_consume_input(&mInfo);
|
||||
} while (!((status == JPEG_SUSPENDED) ||
|
||||
(status == JPEG_REACHED_EOI)));
|
||||
|
||||
/* We may need to do some setup of our own at this point before reading
|
||||
* the data. After jpeg_start_decompress() we have the correct scaled
|
||||
* output image dimensions available, as well as the output colormap
|
||||
* if we asked for color quantization.
|
||||
* In this example, we need to make an output work buffer of the right size.
|
||||
*/
|
||||
/* JSAMPLEs per row in output buffer */
|
||||
#if 0
|
||||
int row_stride = mInfo.output_width * mInfo.output_components; /* physical row width in output buffer */
|
||||
|
||||
/* Make a one-row-high sample array that will go away when done with image */
|
||||
JSAMPARRAY buffer = (*mInfo.mem->alloc_sarray) /* Output row buffer */
|
||||
((j_common_ptr) &mInfo, JPOOL_IMAGE, row_stride, 1);
|
||||
|
||||
/* Step 6: while (scan lines remain to be read) */
|
||||
/* jpeg_read_scanlines(...); */
|
||||
|
||||
/* Here we use the library's state variable cinfo.output_scanline as the
|
||||
* loop counter, so that we don't have to keep track ourselves.
|
||||
*/
|
||||
while (mInfo.output_scanline < mInfo.output_height) {
|
||||
/* jpeg_read_scanlines expects an array of pointers to scanlines.
|
||||
* Here the array is only one element long, but you could ask for
|
||||
* more than one scanline at a time if that's more convenient.
|
||||
*/
|
||||
(void) jpeg_read_scanlines(&mInfo, buffer, 1);
|
||||
/* Assume put_scanline_someplace wants a pointer and sample count. */
|
||||
mImage->SetBits(buffer[0], row_stride, row_stride*mInfo.output_scanline /* XXX ??? */);
|
||||
}
|
||||
#endif
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
nsJPEGDecoder::OutputScanlines(int num_scanlines)
|
||||
{
|
||||
int input_exhausted;
|
||||
int pass;
|
||||
|
||||
#ifdef DEBUG
|
||||
PRUintn start_scanline = mInfo.output_scanline;
|
||||
#endif
|
||||
|
||||
if (mState == JPEG_FINAL_PROGRESSIVE_SCAN_OUTPUT)
|
||||
pass = -1;
|
||||
else
|
||||
pass = mCompletedPasses + 1;
|
||||
|
||||
while ((mInfo.output_scanline < mInfo.output_height) && num_scanlines--) {
|
||||
JSAMPROW samples;
|
||||
|
||||
/* Request one scanline. Returns 0 or 1 scanlines. */
|
||||
int ns = jpeg_read_scanlines(&mInfo, mSamples, 1);
|
||||
#if 0
|
||||
ILTRACE(15,("il:jpeg: scanline %d, ns = %d",
|
||||
mInfo.output_scanline, ns));
|
||||
#endif
|
||||
if (ns != 1) {
|
||||
// ILTRACE(5,("il:jpeg: suspending scanline"));
|
||||
input_exhausted = TRUE;
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* If grayscale image ... */
|
||||
if (mInfo.output_components == 1) {
|
||||
JSAMPLE j, *j1, *j1end, *j3;
|
||||
|
||||
/* Convert from grayscale to RGB. */
|
||||
j1 = mSamples[0];
|
||||
j1end = j1 + mInfo.output_width;
|
||||
j3 = mSamples3[0];
|
||||
while (j1 < j1end) {
|
||||
j = *j1++;
|
||||
j3[0] = j;
|
||||
j3[1] = j;
|
||||
j3[2] = j;
|
||||
j3 += 3;
|
||||
}
|
||||
samples = mSamples3[0];
|
||||
} else { /* 24-bit color image */
|
||||
samples = mSamples[0];
|
||||
}
|
||||
|
||||
|
||||
mImage->SetBits(samples, strlen((const char *)samples), strlen((const char *)samples)*mInfo.output_scanline /* XXX ??? */);
|
||||
#if 0
|
||||
ic->imgdcb->ImgDCBHaveRow( 0, samples, 0, mInfo.output_width, mInfo.output_scanline-1,
|
||||
1, ilErase, pass);
|
||||
#endif
|
||||
}
|
||||
|
||||
input_exhausted = FALSE;
|
||||
|
||||
done:
|
||||
|
||||
return input_exhausted;
|
||||
}
|
||||
|
||||
|
||||
/* [noscript] unsigned long writeSegments (in nsReadSegmentFun reader, in voidPtr closure, in unsigned long count); */
|
||||
NS_IMETHODIMP nsJPEGDecoder::WriteSegments(nsReadSegmentFun reader, void * closure, PRUint32 count, PRUint32 *_retval)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
/* attribute boolean nonBlocking; */
|
||||
NS_IMETHODIMP nsJPEGDecoder::GetNonBlocking(PRBool *aNonBlocking)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
NS_IMETHODIMP nsJPEGDecoder::SetNonBlocking(PRBool aNonBlocking)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
/* attribute nsIOutputStreamObserver observer; */
|
||||
NS_IMETHODIMP nsJPEGDecoder::GetObserver(nsIOutputStreamObserver * *aObserver)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
NS_IMETHODIMP nsJPEGDecoder::SetObserver(nsIOutputStreamObserver * aObserver)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* Override the standard error method in the IJG JPEG decoder code. */
|
||||
void PR_CALLBACK
|
||||
il_error_exit (j_common_ptr cinfo)
|
||||
{
|
||||
#if 0
|
||||
int error_code;
|
||||
il_error_mgr *err = (il_error_mgr *) cinfo->err;
|
||||
|
||||
#ifdef DEBUG
|
||||
#if 0
|
||||
/*ptn fix later */
|
||||
if (il_debug >= 1) {
|
||||
char buffer[JMSG_LENGTH_MAX];
|
||||
|
||||
/* Create the message */
|
||||
(*cinfo->err->format_message) (cinfo, buffer);
|
||||
|
||||
ILTRACE(1,("%s\n", buffer));
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* Convert error to a browser error code */
|
||||
if (cinfo->err->msg_code == JERR_OUT_OF_MEMORY)
|
||||
error_code = MK_OUT_OF_MEMORY;
|
||||
else
|
||||
error_code = MK_IMAGE_LOSSAGE;
|
||||
|
||||
/* Return control to the setjmp point. */
|
||||
longjmp(err->setjmp_buffer, error_code);
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
void PR_CALLBACK
|
||||
init_source (j_decompress_ptr jd)
|
||||
{
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------------------------
|
||||
* This is the callback routine from the IJG JPEG library used to supply new
|
||||
* data to the decompressor when its input buffer is exhausted. It juggles
|
||||
* multiple buffers in an attempt to avoid unnecessary copying of input data.
|
||||
*
|
||||
* (A simpler scheme is possible: It's much easier to use only a single
|
||||
* buffer; when fill_input_buffer() is called, move any unconsumed data
|
||||
* (beyond the current pointer/count) down to the beginning of this buffer and
|
||||
* then load new data into the remaining buffer space. This approach requires
|
||||
* a little more data copying but is far easier to get right.)
|
||||
*
|
||||
* At any one time, the JPEG decompressor is either reading from the netlib
|
||||
* input buffer, which is volatile across top-level calls to the IJG library,
|
||||
* or the "backtrack" buffer. The backtrack buffer contains the remaining
|
||||
* unconsumed data from the netlib buffer after parsing was suspended due
|
||||
* to insufficient data in some previous call to the IJG library.
|
||||
*
|
||||
* When suspending, the decompressor will back up to a convenient restart
|
||||
* point (typically the start of the current MCU). The variables
|
||||
* next_input_byte & bytes_in_buffer indicate where the restart point will be
|
||||
* if the current call returns FALSE. Data beyond this point must be
|
||||
* rescanned after resumption, so it must be preserved in case the decompressor
|
||||
* decides to backtrack.
|
||||
*
|
||||
* Returns:
|
||||
* TRUE if additional data is available, FALSE if no data present and
|
||||
* the JPEG library should therefore suspend processing of input stream
|
||||
*---------------------------------------------------------------------------*/
|
||||
boolean PR_CALLBACK
|
||||
fill_input_buffer (j_decompress_ptr jd)
|
||||
{
|
||||
decoder_source_mgr *src = (decoder_source_mgr *)jd->src;
|
||||
|
||||
return src->decoder->FillInput(jd);
|
||||
}
|
||||
|
||||
PRBool nsJPEGDecoder::FillInput(j_decompress_ptr jd)
|
||||
{
|
||||
// read the data from the input stram...
|
||||
|
||||
decoder_source_mgr *src = (decoder_source_mgr *)jd->src;
|
||||
|
||||
if (mCurDataLen == 0)
|
||||
return FALSE;
|
||||
|
||||
unsigned char *newBuffer = (unsigned char *)mBuf;
|
||||
size_t newBufLen = mCurDataLen;
|
||||
|
||||
if (mBytesToSkip < mCurDataLen) {
|
||||
newBuffer += mBytesToSkip;
|
||||
newBufLen -= mBytesToSkip;
|
||||
mBytesToSkip = 0;
|
||||
} else {
|
||||
mBytesToSkip -= newBufLen;
|
||||
return FALSE; // suspend
|
||||
}
|
||||
|
||||
src->pub.next_input_byte = newBuffer;
|
||||
src->pub.bytes_in_buffer = newBufLen;
|
||||
|
||||
return TRUE;
|
||||
|
||||
#if 0
|
||||
enum data_source_state src_state = src->state;
|
||||
PRUint32 bytesToSkip, new_backtrack_buflen, new_buflen, roundup_buflen;
|
||||
unsigned char *new_buffer;
|
||||
|
||||
#if 0
|
||||
ILTRACE(5,("il:jpeg: fill, state=%d, nib=0x%x, bib=%d", src_state,
|
||||
src->pub.next_input_byte, src->pub.bytes_in_buffer));
|
||||
#endif
|
||||
|
||||
switch (src_state) {
|
||||
|
||||
/* Decompressor reached end of backtrack buffer. Return netlib buffer.*/
|
||||
case dss_consuming_backtrack_buffer:
|
||||
new_buffer = (unsigned char *)src->netlib_buffer;
|
||||
new_buflen = src->netlib_buflen;
|
||||
if ((new_buffer == NULL) || (new_buflen == 0))
|
||||
goto suspend;
|
||||
|
||||
/*
|
||||
* Clear, so that repeated calls to fill_input_buffer() do not
|
||||
* deliver the same netlib buffer more than once.
|
||||
*/
|
||||
src->netlib_buflen = 0;
|
||||
|
||||
/* Discard data if asked by skip_input_data(). */
|
||||
bytesToSkip = src->bytes_to_skip;
|
||||
if (bytesToSkip) {
|
||||
if (bytesToSkip < new_buflen) {
|
||||
/* All done skipping bytes; Return what's left. */
|
||||
new_buffer += bytesToSkip;
|
||||
new_buflen -= bytesToSkip;
|
||||
src->bytes_to_skip = 0;
|
||||
} else {
|
||||
/* Still need to skip some more data in the future */
|
||||
src->bytes_to_skip -= (size_t)new_buflen;
|
||||
goto suspend;
|
||||
}
|
||||
}
|
||||
|
||||
/* Save old backtrack buffer parameters, in case the decompressor
|
||||
* backtracks and we're forced to restore its contents.
|
||||
*/
|
||||
src->backtrack_num_unread_bytes = src->pub.bytes_in_buffer;
|
||||
|
||||
src->pub.next_input_byte = new_buffer;
|
||||
src->pub.bytes_in_buffer = (size_t)new_buflen;
|
||||
src->state = dss_consuming_netlib_buffer;
|
||||
return TRUE;
|
||||
|
||||
/* Reached end of netlib buffer. Suspend */
|
||||
case dss_consuming_netlib_buffer:
|
||||
if (src->pub.next_input_byte != src->netlib_buffer) {
|
||||
/* Backtrack data has been permanently consumed. */
|
||||
src->backtrack_num_unread_bytes = 0;
|
||||
src->backtrack_buflen = 0;
|
||||
}
|
||||
|
||||
/* Save remainder of netlib buffer in backtrack buffer */
|
||||
new_backtrack_buflen = src->pub.bytes_in_buffer + src->backtrack_buflen;
|
||||
|
||||
/* Make sure backtrack buffer is big enough to hold new data. */
|
||||
if (src->backtrack_buffer_size < new_backtrack_buflen) {
|
||||
|
||||
/* Round up to multiple of 16 bytes. */
|
||||
roundup_buflen = ((new_backtrack_buflen + 15) >> 4) << 4;
|
||||
if (src->backtrack_buffer_size) {
|
||||
src->backtrack_buffer =
|
||||
(JOCTET *)PR_REALLOC(src->backtrack_buffer, roundup_buflen);
|
||||
} else {
|
||||
src->backtrack_buffer = (JOCTET*)PR_MALLOC(roundup_buflen);
|
||||
}
|
||||
|
||||
/* Check for OOM */
|
||||
if (! src->backtrack_buffer) {
|
||||
#if 0
|
||||
j_common_ptr cinfo = (j_common_ptr)(&src->js->jd);
|
||||
cinfo->err->msg_code = JERR_OUT_OF_MEMORY;
|
||||
il_error_exit(cinfo);
|
||||
#endif
|
||||
}
|
||||
|
||||
src->backtrack_buffer_size = (size_t)roundup_buflen;
|
||||
|
||||
/* Check for malformed MARKER segment lengths. */
|
||||
if (new_backtrack_buflen > MAX_JPEG_MARKER_LENGTH) {
|
||||
// il_error_exit((j_common_ptr)(&src->js->jd));
|
||||
}
|
||||
}
|
||||
|
||||
/* Copy remainder of netlib buffer into backtrack buffer. */
|
||||
nsCRT::memmove(src->backtrack_buffer + src->backtrack_buflen,
|
||||
src->pub.next_input_byte,
|
||||
src->pub.bytes_in_buffer);
|
||||
|
||||
/* Point to start of data to be rescanned. */
|
||||
src->pub.next_input_byte = src->backtrack_buffer +
|
||||
src->backtrack_buflen - src->backtrack_num_unread_bytes;
|
||||
src->pub.bytes_in_buffer += src->backtrack_num_unread_bytes;
|
||||
src->backtrack_buflen = (size_t)new_backtrack_buflen;
|
||||
|
||||
src->state = dss_consuming_backtrack_buffer;
|
||||
goto suspend;
|
||||
|
||||
default:
|
||||
PR_ASSERT(0);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
suspend:
|
||||
// ILTRACE(5,(" Suspending, bib=%d", src->pub.bytes_in_buffer));
|
||||
return FALSE;
|
||||
#endif
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
void PR_CALLBACK
|
||||
skip_input_data (j_decompress_ptr jd, long num_bytes)
|
||||
{
|
||||
decoder_source_mgr *src = (decoder_source_mgr *)jd->src;
|
||||
|
||||
#if 0
|
||||
ILTRACE(5, ("il:jpeg: skip_input_data js->buf=0x%x js->buflen=%d skip=%d",
|
||||
src->netlib_buffer, src->netlib_buflen,
|
||||
num_bytes));
|
||||
#endif
|
||||
|
||||
if (num_bytes > (long)src->pub.bytes_in_buffer) {
|
||||
/*
|
||||
* Can't skip it all right now until we get more data from
|
||||
* network stream. Set things up so that fill_input_buffer
|
||||
* will skip remaining amount.
|
||||
*/
|
||||
src->decoder->mBytesToSkip = (size_t)num_bytes - src->pub.bytes_in_buffer;
|
||||
src->pub.next_input_byte += src->pub.bytes_in_buffer;
|
||||
src->pub.bytes_in_buffer = 0;
|
||||
} else {
|
||||
/* Simple case. Just advance buffer pointer */
|
||||
src->pub.bytes_in_buffer -= (size_t)num_bytes;
|
||||
src->pub.next_input_byte += num_bytes;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Terminate source --- called by jpeg_finish_decompress() after all
|
||||
* data has been read to clean up JPEG source manager.
|
||||
*/
|
||||
void PR_CALLBACK
|
||||
term_source (j_decompress_ptr jd)
|
||||
{
|
||||
/* No work necessary here */
|
||||
}
|
|
@ -0,0 +1,100 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* 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 Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 2001 Netscape Communications Corporation.
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Stuart Parmenter <pavlov@netscape.com>
|
||||
*/
|
||||
|
||||
#ifndef nsJPEGDecoder_h__
|
||||
#define nsJPEGDecoder_h__
|
||||
|
||||
#include "nsIImageDecoder.h"
|
||||
|
||||
#include "nsCOMPtr.h"
|
||||
|
||||
#include "nsIImage2.h"
|
||||
#include "nsIImageRequest.h"
|
||||
#include "nsIInputStream.h"
|
||||
|
||||
#include "jpeglib.h"
|
||||
|
||||
#define NS_JPEGDECODER_CID \
|
||||
{ /* 5871a422-1dd2-11b2-ab3f-e2e56be5da9c */ \
|
||||
0x5871a422, \
|
||||
0x1dd2, \
|
||||
0x11b2, \
|
||||
{0xab, 0x3f, 0xe2, 0xe5, 0x6b, 0xe5, 0xda, 0x9c} \
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
struct jpeg_error_mgr pub; /* "public" fields for IJG library*/
|
||||
jmp_buf setjmp_buffer; /* For handling catastropic errors */
|
||||
} decoder_error_mgr;
|
||||
|
||||
|
||||
typedef enum {
|
||||
JPEG_HEADER, /* Reading JFIF headers */
|
||||
JPEG_START_DECOMPRESS,
|
||||
JPEG_DECOMPRESS_PROGRESSIVE, /* Output progressive pixels */
|
||||
JPEG_DECOMPRESS_SEQUENTIAL, /* Output sequential pixels */
|
||||
JPEG_FINAL_PROGRESSIVE_SCAN_OUTPUT,
|
||||
JPEG_DONE,
|
||||
JPEG_SINK_NON_JPEG_TRAILER, /* Some image files have a */
|
||||
/* non-JPEG trailer */
|
||||
JPEG_ERROR
|
||||
} jstate;
|
||||
|
||||
class nsJPEGDecoder : public nsIImageDecoder
|
||||
{
|
||||
public:
|
||||
NS_DECL_ISUPPORTS
|
||||
NS_DECL_NSIIMAGEDECODER
|
||||
NS_DECL_NSIOUTPUTSTREAM
|
||||
|
||||
nsJPEGDecoder();
|
||||
virtual ~nsJPEGDecoder();
|
||||
|
||||
PRBool FillInput(j_decompress_ptr jd);
|
||||
|
||||
PRUint32 mBytesToSkip;
|
||||
|
||||
protected:
|
||||
int OutputScanlines(int num_scanlines);
|
||||
|
||||
private:
|
||||
nsCOMPtr<nsIImage2> mImage;
|
||||
nsCOMPtr<nsIImageRequest> mRequest;
|
||||
nsCOMPtr<nsIInputStream> mStream;
|
||||
|
||||
struct jpeg_decompress_struct mInfo;
|
||||
decoder_error_mgr mErr;
|
||||
jstate mState;
|
||||
|
||||
char *mBuf;
|
||||
PRUint32 mBufLen;
|
||||
PRUint32 mCurDataLen;
|
||||
|
||||
JSAMPARRAY mSamples;
|
||||
JSAMPARRAY mSamples3;
|
||||
|
||||
PRInt32 mCompletedPasses;
|
||||
PRInt32 mPasses;
|
||||
};
|
||||
|
||||
#endif // nsJPEGDecoder_h__
|
|
@ -0,0 +1,42 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* 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 Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 2001 Netscape Communications Corporation. All
|
||||
* Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Stuart Parmenter <pavlov@netscape.com>
|
||||
*/
|
||||
|
||||
#include "nsIGenericFactory.h"
|
||||
#include "nsIModule.h"
|
||||
|
||||
#include "nsJPEGDecoder.h"
|
||||
|
||||
// objects that just require generic constructors
|
||||
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsJPEGDecoder)
|
||||
|
||||
static nsModuleComponentInfo components[] =
|
||||
{
|
||||
{ "ppm decoder",
|
||||
NS_JPEGDECODER_CID,
|
||||
"@mozilla.org/image/decoder?image/jpeg;1",
|
||||
nsJPEGDecoderConstructor, },
|
||||
};
|
||||
|
||||
NS_IMPL_NSGETMODULE("nsJPEGDecoderModule", components)
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
#
|
||||
# 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 Netscape
|
||||
# Communications Corporation. Portions created by Netscape are
|
||||
# Copyright (C) 2001 Netscape Communications Corporation. All
|
||||
# Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
#
|
||||
|
||||
DEPTH=..\..\..
|
||||
|
||||
DIRS = ppm png
|
||||
|
||||
!include $(DEPTH)\config\rules.mak
|
|
@ -0,0 +1,2 @@
|
|||
Makefile
|
||||
.deps
|
|
@ -0,0 +1,40 @@
|
|||
#
|
||||
# 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 Netscape
|
||||
# Communications Corporation. Portions created by Netscape are
|
||||
# Copyright (C) 2001 Netscape Communications Corporation. All
|
||||
# Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
#
|
||||
|
||||
DEPTH = ../../../..
|
||||
topsrcdir = @top_srcdir@
|
||||
srcdir = @srcdir@
|
||||
VPATH = @srcdir@
|
||||
|
||||
include $(DEPTH)/config/autoconf.mk
|
||||
|
||||
MODULE = imgpng
|
||||
LIBRARY_NAME = imgpng
|
||||
IS_COMPONENT = 1
|
||||
|
||||
CPPSRCS = nsPNGDecoder.cpp nsPNGFactory.cpp
|
||||
|
||||
EXTRA_DSO_LDOPTS = $(PNG_LIBS) $(ZLIB_LIBS) \
|
||||
$(MOZ_COMPONENT_LIBS) \
|
||||
$(NULL)
|
||||
|
||||
include $(topsrcdir)/config/rules.mk
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
#!nmake
|
||||
#
|
||||
# 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 Netscape
|
||||
# Communications Corporation. Portions created by Netscape are
|
||||
# Copyright (C) 2001 Netscape Communications Corporation. All
|
||||
# Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# Stuart Parmenter <pavlov@netscape.com>
|
||||
#
|
||||
|
||||
DEPTH=..\..\..\..
|
||||
include <$(DEPTH)/config/config.mak>
|
||||
|
||||
MODULE = imgpng
|
||||
LIBRARY_NAME = imgpng
|
||||
DLL = $(OBJDIR)\$(LIBRARY_NAME).dll
|
||||
MAKE_OBJ_TYPE = DLL
|
||||
|
||||
OBJS = \
|
||||
.\$(OBJDIR)\nsPNGDecoder.obj \
|
||||
.\$(OBJDIR)\nsPNGFactory.obj \
|
||||
$(NULL)
|
||||
|
||||
LLIBS=\
|
||||
$(LIBNSPR) \
|
||||
$(DIST)\lib\xpcom.lib \
|
||||
$(DIST)\lib\png.lib \
|
||||
$(DIST)\lib\zlib.lib \
|
||||
$(NULL)
|
||||
|
||||
include <$(DEPTH)\config\rules.mak>
|
||||
|
||||
install:: $(DLL)
|
||||
$(MAKE_INSTALL) .\$(OBJDIR)\$(LIBRARY_NAME).dll $(DIST)\bin\components
|
||||
$(MAKE_INSTALL) .\$(OBJDIR)\$(LIBRARY_NAME).lib $(DIST)\lib
|
||||
|
||||
clobber::
|
||||
rm -f $(DIST)\bin\components\$(LIBRARY_NAME).dll
|
||||
rm -f $(DIST)\lib\$(LIBRARY_NAME).lib
|
|
@ -0,0 +1,415 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* 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 Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 2001 Netscape Communications Corporation.
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Stuart Parmenter <pavlov@netscape.com>
|
||||
*
|
||||
*/
|
||||
|
||||
#include "nsPNGDecoder.h"
|
||||
|
||||
#include "nsIInputStream.h"
|
||||
|
||||
#include "nspr.h"
|
||||
|
||||
#include "nsIComponentManager.h"
|
||||
|
||||
#include "png.h"
|
||||
|
||||
|
||||
NS_IMPL_ISUPPORTS2(nsPNGDecoder, nsIImageDecoder, nsIOutputStream)
|
||||
|
||||
|
||||
nsPNGDecoder::nsPNGDecoder()
|
||||
{
|
||||
NS_INIT_ISUPPORTS();
|
||||
|
||||
mPNG = nsnull;
|
||||
mInfo = nsnull;
|
||||
}
|
||||
|
||||
nsPNGDecoder::~nsPNGDecoder()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
/** nsIImageDecoder methods **/
|
||||
|
||||
/* void init (in nsIImageRequest aRequest); */
|
||||
NS_IMETHODIMP nsPNGDecoder::Init(nsIImageRequest *aRequest)
|
||||
{
|
||||
mRequest = aRequest;
|
||||
|
||||
nsCOMPtr<nsIImageContainer> container;
|
||||
aRequest->GetImage(getter_AddRefs(container));
|
||||
|
||||
mImage = do_CreateInstance("@mozilla.org/gfx/image/frame;2");
|
||||
if (!mImage)
|
||||
return NS_ERROR_FAILURE;
|
||||
|
||||
container->AppendFrame(mImage);
|
||||
|
||||
/* do png init stuff */
|
||||
|
||||
/* Initialize the container's source image header. */
|
||||
/* Always decode to 24 bit pixdepth */
|
||||
|
||||
|
||||
mPNG = png_create_read_struct(PNG_LIBPNG_VER_STRING,
|
||||
NULL, NULL,
|
||||
NULL);
|
||||
if (!mPNG) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
mInfo = png_create_info_struct(mPNG);
|
||||
if (!mInfo) {
|
||||
png_destroy_read_struct(&mPNG, NULL, NULL);
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
/* use ic as libpng "progressive pointer" (retrieve in callbacks) */
|
||||
png_set_progressive_read_fn(mPNG, this, nsPNGDecoder::info_callback, nsPNGDecoder::row_callback, nsPNGDecoder::end_callback);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
/* readonly attribute nsIImageRequest request; */
|
||||
NS_IMETHODIMP nsPNGDecoder::GetRequest(nsIImageRequest * *aRequest)
|
||||
{
|
||||
*aRequest = mRequest;
|
||||
NS_ADDREF(*aRequest);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/** nsIOutputStream methods **/
|
||||
|
||||
/* void close (); */
|
||||
NS_IMETHODIMP nsPNGDecoder::Close()
|
||||
{
|
||||
if (mPNG)
|
||||
png_destroy_read_struct(&mPNG, mInfo ? &mInfo : NULL, NULL);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* void flush (); */
|
||||
NS_IMETHODIMP nsPNGDecoder::Flush()
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
/* unsigned long write (in string buf, in unsigned long count); */
|
||||
NS_IMETHODIMP nsPNGDecoder::Write(const char *buf, PRUint32 count, PRUint32 *_retval)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
|
||||
static NS_METHOD ReadDataOut(nsIInputStream* in,
|
||||
void* closure,
|
||||
const char* fromRawSegment,
|
||||
PRUint32 toOffset,
|
||||
PRUint32 count,
|
||||
PRUint32 *writeCount)
|
||||
{
|
||||
nsPNGDecoder *decoder = NS_STATIC_CAST(nsPNGDecoder*, closure);
|
||||
*writeCount = decoder->ProcessData((unsigned char*)fromRawSegment, count);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
PRUint32 nsPNGDecoder::ProcessData(unsigned char *data, PRUint32 count)
|
||||
{
|
||||
png_process_data(mPNG, mInfo, data, count);
|
||||
|
||||
return count; // we always consume all the data
|
||||
}
|
||||
|
||||
/* unsigned long writeFrom (in nsIInputStream inStr, in unsigned long count); */
|
||||
NS_IMETHODIMP nsPNGDecoder::WriteFrom(nsIInputStream *inStr, PRUint32 count, PRUint32 *_retval)
|
||||
{
|
||||
PRUint32 sourceOffset = *_retval;
|
||||
|
||||
if (setjmp(mPNG->jmpbuf)) {
|
||||
png_destroy_read_struct(&mPNG, &mInfo, NULL);
|
||||
// is this NS_ERROR_FAILURE enough?
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
inStr->ReadSegments(ReadDataOut, this, count, _retval);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* [noscript] unsigned long writeSegments (in nsReadSegmentFun reader, in voidPtr closure, in unsigned long count); */
|
||||
NS_IMETHODIMP nsPNGDecoder::WriteSegments(nsReadSegmentFun reader, void * closure, PRUint32 count, PRUint32 *_retval)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
/* attribute boolean nonBlocking; */
|
||||
NS_IMETHODIMP nsPNGDecoder::GetNonBlocking(PRBool *aNonBlocking)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
NS_IMETHODIMP nsPNGDecoder::SetNonBlocking(PRBool aNonBlocking)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
/* attribute nsIOutputStreamObserver observer; */
|
||||
NS_IMETHODIMP nsPNGDecoder::GetObserver(nsIOutputStreamObserver * *aObserver)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
NS_IMETHODIMP nsPNGDecoder::SetObserver(nsIOutputStreamObserver * aObserver)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void
|
||||
nsPNGDecoder::info_callback(png_structp png_ptr, png_infop info_ptr)
|
||||
{
|
||||
/* int number_passes; NOT USED */
|
||||
png_uint_32 width, height;
|
||||
int bit_depth, color_type, interlace_type, compression_type, filter_type;
|
||||
int channels;
|
||||
double LUT_exponent, CRT_exponent = 2.2, display_exponent, aGamma;
|
||||
|
||||
png_bytep trans=NULL;
|
||||
int num_trans =0;
|
||||
|
||||
/* always decode to 24-bit RGB or 32-bit RGBA */
|
||||
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
|
||||
&interlace_type, &compression_type, &filter_type);
|
||||
|
||||
if (color_type == PNG_COLOR_TYPE_PALETTE)
|
||||
png_set_expand(png_ptr);
|
||||
|
||||
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
|
||||
png_set_expand(png_ptr);
|
||||
|
||||
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
|
||||
png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, NULL);
|
||||
png_set_expand(png_ptr);
|
||||
}
|
||||
|
||||
if (bit_depth == 16)
|
||||
png_set_strip_16(png_ptr);
|
||||
|
||||
if (color_type == PNG_COLOR_TYPE_GRAY ||
|
||||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
|
||||
png_set_gray_to_rgb(png_ptr);
|
||||
|
||||
|
||||
#ifdef XP_PC
|
||||
// windows likes BGR
|
||||
png_set_bgr(png_ptr);
|
||||
#endif
|
||||
|
||||
/* set up gamma correction for Mac, Unix and (Win32 and everything else)
|
||||
* using educated guesses for display-system exponents; do preferences
|
||||
* later */
|
||||
|
||||
#if defined(XP_MAC)
|
||||
LUT_exponent = 1.8 / 2.61;
|
||||
#elif defined(XP_UNIX)
|
||||
# if defined(__sgi)
|
||||
LUT_exponent = 1.0 / 1.7; /* typical default for SGI console */
|
||||
# elif defined(NeXT)
|
||||
LUT_exponent = 1.0 / 2.2; /* typical default for NeXT cube */
|
||||
# else
|
||||
LUT_exponent = 1.0; /* default for most other Unix workstations */
|
||||
# endif
|
||||
#else
|
||||
LUT_exponent = 1.0; /* virtually all PCs and most other systems */
|
||||
#endif
|
||||
|
||||
/* (alternatively, could check for SCREEN_GAMMA environment variable) */
|
||||
display_exponent = LUT_exponent * CRT_exponent;
|
||||
|
||||
if (png_get_gAMA(png_ptr, info_ptr, &aGamma))
|
||||
png_set_gamma(png_ptr, display_exponent, aGamma);
|
||||
else
|
||||
png_set_gamma(png_ptr, display_exponent, 0.45455);
|
||||
|
||||
/* let libpng expand interlaced images */
|
||||
if (interlace_type == PNG_INTERLACE_ADAM7) {
|
||||
/* number_passes = */
|
||||
png_set_interlace_handling(png_ptr);
|
||||
}
|
||||
|
||||
/* now all of those things we set above are used to update various struct
|
||||
* members and whatnot, after which we can get channels, rowbytes, etc. */
|
||||
png_read_update_info(png_ptr, info_ptr);
|
||||
channels = png_get_channels(png_ptr, info_ptr);
|
||||
PR_ASSERT(channels == 3 || channels == 4);
|
||||
|
||||
/* set the ic values */
|
||||
|
||||
nsPNGDecoder *decoder = NS_STATIC_CAST(nsPNGDecoder*, png_get_progressive_ptr(png_ptr));
|
||||
|
||||
/*---------------------------------------------------------------*/
|
||||
/* copy PNG info into imagelib structs (formerly png_set_dims()) */
|
||||
/*---------------------------------------------------------------*/
|
||||
|
||||
PRInt32 alpha_bits = 1;
|
||||
|
||||
if (channels > 3) {
|
||||
|
||||
if (color_type || PNG_COLOR_MASK_ALPHA) {
|
||||
/* check if alpha is coming from a tRNS chunk and is binary */
|
||||
if (num_trans) {
|
||||
alpha_bits = 1;
|
||||
|
||||
/* if it's not a indexed color image, tRNS means binary */
|
||||
if (color_type == PNG_COLOR_TYPE_PALETTE) {
|
||||
for (int i=0; i<num_trans; i++) {
|
||||
if ((trans[i] != 0) && (trans[i] != 255)) {
|
||||
alpha_bits = 8;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
alpha_bits = 8/*png_ptr->pixel_depth*/; /* 8 */
|
||||
}
|
||||
}
|
||||
} else {
|
||||
alpha_bits = 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// since the png is only 1 frame, initalize the container to the width and height of the frame
|
||||
nsCOMPtr<nsIImageContainer> container;
|
||||
decoder->mRequest->GetImage(getter_AddRefs(container));
|
||||
container->Init(width, height);
|
||||
|
||||
// then initalize the frame (which was appended above in nsPNGDecoder::Init())
|
||||
if (channels == 3) {
|
||||
decoder->mImage->Init(0, 0, width, height, nsIGFXFormat::RGB);
|
||||
} else if (channels > 3) {
|
||||
if (alpha_bits == 8) {
|
||||
decoder->mImage->Init(0, 0, width, height, nsIGFXFormat::RGBA);
|
||||
} else if (alpha_bits == 1) {
|
||||
decoder->mImage->Init(0, 0, width, height, nsIGFXFormat::RGB_A1);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void
|
||||
nsPNGDecoder::row_callback(png_structp png_ptr, png_bytep new_row,
|
||||
png_uint_32 row_num, int pass)
|
||||
{
|
||||
/* libpng comments:
|
||||
*
|
||||
* this function is called for every row in the image. If the
|
||||
* image is interlacing, and you turned on the interlace handler,
|
||||
* this function will be called for every row in every pass.
|
||||
* Some of these rows will not be changed from the previous pass.
|
||||
* When the row is not changed, the new_row variable will be NULL.
|
||||
* The rows and passes are called in order, so you don't really
|
||||
* need the row_num and pass, but I'm supplying them because it
|
||||
* may make your life easier.
|
||||
*
|
||||
* For the non-NULL rows of interlaced images, you must call
|
||||
* png_progressive_combine_row() passing in the row and the
|
||||
* old row. You can call this function for NULL rows (it will
|
||||
* just return) and for non-interlaced images (it just does the
|
||||
* memcpy for you) if it will make the code easier. Thus, you
|
||||
* can just do this for all cases:
|
||||
*
|
||||
* png_progressive_combine_row(png_ptr, old_row, new_row);
|
||||
*
|
||||
* where old_row is what was displayed for previous rows. Note
|
||||
* that the first pass (pass == 0 really) will completely cover
|
||||
* the old row, so the rows do not have to be initialized. After
|
||||
* the first pass (and only for interlaced images), you will have
|
||||
* to pass the current row, and the function will combine the
|
||||
* old row and the new row.
|
||||
*/
|
||||
nsPNGDecoder *decoder = NS_STATIC_CAST(nsPNGDecoder*, png_get_progressive_ptr(png_ptr));
|
||||
|
||||
PRUint32 bpr;
|
||||
decoder->mImage->GetBytesPerRow(&bpr);
|
||||
|
||||
PRUint32 length;
|
||||
PRUint8 *bits;
|
||||
decoder->mImage->GetBits(&bits, &length);
|
||||
|
||||
png_bytep line;
|
||||
if (bits) {
|
||||
line = bits+(row_num*bpr);
|
||||
png_progressive_combine_row(png_ptr, line, new_row);
|
||||
}
|
||||
else
|
||||
line = new_row;
|
||||
|
||||
if (new_row) {
|
||||
decoder->mImage->SetBits((PRUint8*)line, bpr, row_num*bpr);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
nsPNGDecoder::end_callback(png_structp png_ptr, png_infop info_ptr)
|
||||
{
|
||||
/* libpng comments:
|
||||
*
|
||||
* this function is called when the whole image has been read,
|
||||
* including any chunks after the image (up to and including
|
||||
* the IEND). You will usually have the same info chunk as you
|
||||
* had in the header, although some data may have been added
|
||||
* to the comments and time fields.
|
||||
*
|
||||
* Most people won't do much here, perhaps setting a flag that
|
||||
* marks the image as finished.
|
||||
*/
|
||||
|
||||
nsPNGDecoder *decoder = NS_STATIC_CAST(nsPNGDecoder*, png_get_progressive_ptr(png_ptr));
|
||||
|
||||
gfx_format format;
|
||||
decoder->mImage->GetFormat(&format);
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* 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 Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 2001 Netscape Communications Corporation.
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Stuart Parmenter <pavlov@netscape.com>
|
||||
*/
|
||||
|
||||
#ifndef nsPNGDecoder_h__
|
||||
#define nsPNGDecoder_h__
|
||||
|
||||
#include "nsIImageDecoder.h"
|
||||
|
||||
#include "nsIImageContainer.h"
|
||||
#include "nsIImageFrame.h"
|
||||
#include "nsIImageRequest.h"
|
||||
|
||||
#include "nsCOMPtr.h"
|
||||
|
||||
#include "png.h"
|
||||
|
||||
#define NS_PNGDECODER_CID \
|
||||
{ /* 36fa00c2-1dd2-11b2-be07-d16eeb4c50ed */ \
|
||||
0x36fa00c2, \
|
||||
0x1dd2, \
|
||||
0x11b2, \
|
||||
{0xbe, 0x07, 0xd1, 0x6e, 0xeb, 0x4c, 0x50, 0xed} \
|
||||
}
|
||||
|
||||
class nsPNGDecoder : public nsIImageDecoder
|
||||
{
|
||||
public:
|
||||
NS_DECL_ISUPPORTS
|
||||
NS_DECL_NSIIMAGEDECODER
|
||||
NS_DECL_NSIOUTPUTSTREAM
|
||||
|
||||
nsPNGDecoder();
|
||||
virtual ~nsPNGDecoder();
|
||||
|
||||
static void
|
||||
info_callback(png_structp png_ptr, png_infop info_ptr);
|
||||
static void
|
||||
row_callback(png_structp png_ptr, png_bytep new_row,
|
||||
png_uint_32 row_num, int pass);
|
||||
|
||||
static void
|
||||
end_callback(png_structp png_ptr, png_infop info_ptr);
|
||||
|
||||
inline PRUint32 ProcessData(unsigned char *data, PRUint32 count);
|
||||
|
||||
|
||||
private:
|
||||
nsCOMPtr<nsIImageFrame> mImage;
|
||||
nsCOMPtr<nsIImageRequest> mRequest;
|
||||
|
||||
png_structp mPNG;
|
||||
png_infop mInfo;
|
||||
};
|
||||
|
||||
#endif // nsPNGDecoder_h__
|
|
@ -0,0 +1,42 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* 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 Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 2001 Netscape Communications Corporation. All
|
||||
* Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Stuart Parmenter <pavlov@netscape.com>
|
||||
*/
|
||||
|
||||
#include "nsIGenericFactory.h"
|
||||
#include "nsIModule.h"
|
||||
|
||||
#include "nsPNGDecoder.h"
|
||||
|
||||
// objects that just require generic constructors
|
||||
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsPNGDecoder)
|
||||
|
||||
static nsModuleComponentInfo components[] =
|
||||
{
|
||||
{ "PNG decoder",
|
||||
NS_PNGDECODER_CID,
|
||||
"@mozilla.org/image/decoder?image/png;1",
|
||||
nsPNGDecoderConstructor, },
|
||||
};
|
||||
|
||||
NS_IMPL_NSGETMODULE("nsPNGDecoderModule", components)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
Makefile
|
||||
.deps
|
|
@ -0,0 +1,40 @@
|
|||
#
|
||||
# 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 Netscape
|
||||
# Communications Corporation. Portions created by Netscape are
|
||||
# Copyright (C) 2001 Netscape Communications Corporation. All
|
||||
# Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
#
|
||||
|
||||
DEPTH = ../../../..
|
||||
topsrcdir = @top_srcdir@
|
||||
srcdir = @srcdir@
|
||||
VPATH = @srcdir@
|
||||
|
||||
include $(DEPTH)/config/autoconf.mk
|
||||
|
||||
MODULE = imgppm
|
||||
LIBRARY_NAME = imgppm
|
||||
IS_COMPONENT = 1
|
||||
|
||||
CPPSRCS = nsPPMDecoder.cpp nsPPMFactory.cpp
|
||||
|
||||
EXTRA_DSO_LDOPTS = \
|
||||
$(MOZ_COMPONENT_LIBS) \
|
||||
$(NULL)
|
||||
|
||||
include $(topsrcdir)/config/rules.mk
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
#!nmake
|
||||
#
|
||||
# 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 Netscape
|
||||
# Communications Corporation. Portions created by Netscape are
|
||||
# Copyright (C) 2001 Netscape Communications Corporation. All
|
||||
# Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# Stuart Parmenter <pavlov@netscape.com>
|
||||
#
|
||||
|
||||
DEPTH=..\..\..\..
|
||||
include <$(DEPTH)/config/config.mak>
|
||||
|
||||
MODULE = imgppm
|
||||
LIBRARY_NAME = imgppm
|
||||
DLL = $(OBJDIR)\$(LIBRARY_NAME).dll
|
||||
MAKE_OBJ_TYPE = DLL
|
||||
|
||||
OBJS = \
|
||||
.\$(OBJDIR)\nsPPMDecoder.obj \
|
||||
.\$(OBJDIR)\nsPPMFactory.obj \
|
||||
$(NULL)
|
||||
|
||||
LLIBS=\
|
||||
$(LIBNSPR) \
|
||||
$(DIST)\lib\xpcom.lib \
|
||||
$(NULL)
|
||||
|
||||
include <$(DEPTH)\config\rules.mak>
|
||||
|
||||
install:: $(DLL)
|
||||
$(MAKE_INSTALL) .\$(OBJDIR)\$(LIBRARY_NAME).dll $(DIST)\bin\components
|
||||
$(MAKE_INSTALL) .\$(OBJDIR)\$(LIBRARY_NAME).lib $(DIST)\lib
|
||||
|
||||
clobber::
|
||||
rm -f $(DIST)\bin\components\$(LIBRARY_NAME).dll
|
||||
rm -f $(DIST)\lib\$(LIBRARY_NAME).lib
|
|
@ -0,0 +1,296 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* 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 Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 2001 Netscape Communications Corporation.
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Stuart Parmenter <pavlov@netscape.com>
|
||||
*
|
||||
*
|
||||
* the ppm decoding function is from Tim Rowley <tor@cs.brown.edu>
|
||||
* i dunno its license
|
||||
*
|
||||
*/
|
||||
|
||||
#include "nsPPMDecoder.h"
|
||||
|
||||
#include "nsIInputStream.h"
|
||||
#include "nsIImageContainer.h"
|
||||
|
||||
#include "nspr.h"
|
||||
|
||||
#include "nsUnitConverters.h"
|
||||
|
||||
#include "nsIComponentManager.h"
|
||||
|
||||
NS_IMPL_ISUPPORTS2(nsPPMDecoder, nsIImageDecoder, nsIOutputStream)
|
||||
|
||||
|
||||
nsPPMDecoder::nsPPMDecoder()
|
||||
{
|
||||
NS_INIT_ISUPPORTS();
|
||||
mDataReceived = 0;
|
||||
mDataWritten = 0;
|
||||
|
||||
mDataLeft = 0;
|
||||
mPrevData = nsnull;
|
||||
}
|
||||
|
||||
nsPPMDecoder::~nsPPMDecoder()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
/** nsIImageDecoder methods **/
|
||||
|
||||
/* void init (in nsIImageRequest aRequest); */
|
||||
NS_IMETHODIMP nsPPMDecoder::Init(nsIImageRequest *aRequest)
|
||||
{
|
||||
mRequest = aRequest;
|
||||
|
||||
nsCOMPtr<nsIImageContainer> container;
|
||||
aRequest->GetImage(getter_AddRefs(container));
|
||||
|
||||
mImage = do_CreateInstance("@mozilla.org/gfx/image/frame;2");
|
||||
if (!mImage)
|
||||
return NS_ERROR_FAILURE;
|
||||
|
||||
container->AppendFrame(mImage);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* readonly attribute nsIImageRequest request; */
|
||||
NS_IMETHODIMP nsPPMDecoder::GetRequest(nsIImageRequest * *aRequest)
|
||||
{
|
||||
*aRequest = mRequest;
|
||||
NS_ADDREF(*aRequest);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/** nsIOutputStream methods **/
|
||||
|
||||
/* void close (); */
|
||||
NS_IMETHODIMP nsPPMDecoder::Close()
|
||||
{
|
||||
printf("nsPPMDecoder::Close()\n");
|
||||
|
||||
// XXX hack
|
||||
gfx_format format;
|
||||
mImage->GetFormat(&format);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* void flush (); */
|
||||
NS_IMETHODIMP nsPPMDecoder::Flush()
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
/* unsigned long write (in string buf, in unsigned long count); */
|
||||
NS_IMETHODIMP nsPPMDecoder::Write(const char *buf, PRUint32 count, PRUint32 *_retval)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
static char *__itoa(int n)
|
||||
{
|
||||
char *s;
|
||||
int i, j, sign, tmp;
|
||||
|
||||
/* check sign and convert to positive to stringify numbers */
|
||||
if ( (sign = n) < 0)
|
||||
n = -n;
|
||||
i = 0;
|
||||
s = (char*) malloc(sizeof(char));
|
||||
|
||||
/* grow string as needed to add numbers from powers of 10
|
||||
* down till none left
|
||||
*/
|
||||
do
|
||||
{
|
||||
s = (char*) realloc(s, (i+1)*sizeof(char));
|
||||
s[i++] = n % 10 + '0'; /* '0' or 30 is where ASCII numbers start */
|
||||
s[i] = '\0';
|
||||
}
|
||||
while( (n /= 10) > 0);
|
||||
|
||||
/* tack on minus sign if we found earlier that this was negative */
|
||||
if (sign < 0)
|
||||
{
|
||||
s = (char*) realloc(s, (i+1)*sizeof(char));
|
||||
s[i++] = '-';
|
||||
}
|
||||
s[i] = '\0';
|
||||
|
||||
/* pop numbers (and sign) off of string to push back into right direction */
|
||||
for (i = 0, j = strlen(s) - 1; i < j; i++, j--)
|
||||
{
|
||||
tmp = s[i];
|
||||
s[i] = s[j];
|
||||
s[j] = tmp;
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
/* unsigned long writeFrom (in nsIInputStream inStr, in unsigned long count); */
|
||||
NS_IMETHODIMP nsPPMDecoder::WriteFrom(nsIInputStream *inStr, PRUint32 count, PRUint32 *_retval)
|
||||
{
|
||||
nsresult rv;
|
||||
|
||||
char *buf = (char *)PR_Malloc(count + mDataLeft);
|
||||
if (!buf)
|
||||
return NS_ERROR_OUT_OF_MEMORY; /* we couldn't allocate the object */
|
||||
|
||||
|
||||
// read the data from the input stram...
|
||||
PRUint32 readLen;
|
||||
rv = inStr->Read(buf+mDataLeft, count, &readLen);
|
||||
|
||||
PRUint32 dataLen = readLen + mDataLeft;
|
||||
|
||||
if (mPrevData) {
|
||||
strncpy(buf, mPrevData, mDataLeft);
|
||||
PR_Free(mPrevData);
|
||||
mPrevData = nsnull;
|
||||
mDataLeft = 0;
|
||||
}
|
||||
|
||||
char *data = buf;
|
||||
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
if (mDataReceived == 0) {
|
||||
|
||||
// Check the magic number
|
||||
char type;
|
||||
if ((sscanf(data, "P%c\n", &type) !=1) || (type != '6')) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
int i = 3;
|
||||
data += i;
|
||||
|
||||
#if 0
|
||||
// XXX
|
||||
// Ignore comments
|
||||
while ((input = fgetc(f)) == '#')
|
||||
fgets(junk, 512, f);
|
||||
ungetc(input, f);
|
||||
#endif
|
||||
|
||||
// Read size
|
||||
int w, h, mcv;
|
||||
|
||||
if (sscanf(data, "%d %d\n%d\n", &w, &h, &mcv) != 3) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
char *ws = __itoa(w), *hs = __itoa(h), *mcvs = __itoa(mcv);
|
||||
int j = strlen(ws) + strlen(hs) + strlen(mcvs) + 3;
|
||||
data += j;
|
||||
// free(ws);
|
||||
// free(hs);
|
||||
// free(mcvs);
|
||||
|
||||
readLen -= i + j;
|
||||
dataLen = readLen; // since this is the first pass, we don't have any data waiting that we need to keep track of
|
||||
|
||||
// XXX this isn't the width/height that was actually requested.. get that from mRequest.
|
||||
mImage->Init(0, 0, w, h, nsIGFXFormat::RGB);
|
||||
|
||||
}
|
||||
|
||||
PRUint32 bpr;
|
||||
gfx_dimension width;
|
||||
mImage->GetBytesPerRow(&bpr);
|
||||
mImage->GetWidth(&width);
|
||||
|
||||
PRUint32 real_bpr = GFXCoordToIntCeil(width) * 3;
|
||||
|
||||
PRUint32 i = 0;
|
||||
PRUint32 rownum = mDataWritten / real_bpr; // XXX this better not have a decimal
|
||||
|
||||
PRUint32 wroteLen = 0;
|
||||
|
||||
if (readLen > real_bpr) {
|
||||
|
||||
do {
|
||||
PRUint8 *line = (PRUint8*)data + i*real_bpr;
|
||||
mImage->SetBits(line, real_bpr, (rownum++)*bpr);
|
||||
wroteLen += real_bpr ;
|
||||
i++;
|
||||
} while(dataLen >= real_bpr * (i+1));
|
||||
|
||||
}
|
||||
|
||||
mDataReceived += readLen; // don't double count previous data that is in 'dataLen'
|
||||
mDataWritten += wroteLen;
|
||||
|
||||
PRUint32 dataLeft = dataLen - wroteLen;
|
||||
|
||||
if (dataLeft > 0) {
|
||||
if (mPrevData) {
|
||||
mPrevData = (char *)PR_Realloc(mPrevData, mDataLeft + dataLeft);
|
||||
strncpy(mPrevData + mDataLeft, data+wroteLen, dataLeft);
|
||||
mDataLeft += dataLeft;
|
||||
|
||||
} else {
|
||||
mDataLeft = dataLeft;
|
||||
mPrevData = (char *)PR_Malloc(mDataLeft);
|
||||
strncpy(mPrevData, data+wroteLen, mDataLeft);
|
||||
}
|
||||
}
|
||||
|
||||
PR_FREEIF(buf);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* [noscript] unsigned long writeSegments (in nsReadSegmentFun reader, in voidPtr closure, in unsigned long count); */
|
||||
NS_IMETHODIMP nsPPMDecoder::WriteSegments(nsReadSegmentFun reader, void * closure, PRUint32 count, PRUint32 *_retval)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
/* attribute boolean nonBlocking; */
|
||||
NS_IMETHODIMP nsPPMDecoder::GetNonBlocking(PRBool *aNonBlocking)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
NS_IMETHODIMP nsPPMDecoder::SetNonBlocking(PRBool aNonBlocking)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
/* attribute nsIOutputStreamObserver observer; */
|
||||
NS_IMETHODIMP nsPPMDecoder::GetObserver(nsIOutputStreamObserver * *aObserver)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
NS_IMETHODIMP nsPPMDecoder::SetObserver(nsIOutputStreamObserver * aObserver)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* 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 Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 2001 Netscape Communications Corporation.
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Stuart Parmenter <pavlov@netscape.com>
|
||||
*/
|
||||
|
||||
#ifndef nsPPMDecoder_h__
|
||||
#define nsPPMDecoder_h__
|
||||
|
||||
#include "nsIImageDecoder.h"
|
||||
|
||||
#include "nsCOMPtr.h"
|
||||
|
||||
#include "nsIImageFrame.h"
|
||||
#include "nsIImageRequest.h"
|
||||
|
||||
#define NS_PPMDECODER_CID \
|
||||
{ /* e90bfa06-1dd1-11b2-8217-f38fe5d431a2 */ \
|
||||
0xe90bfa06, \
|
||||
0x1dd1, \
|
||||
0x11b2, \
|
||||
{0x82, 0x17, 0xf3, 0x8f, 0xe5, 0xd4, 0x31, 0xa2} \
|
||||
}
|
||||
|
||||
class nsPPMDecoder : public nsIImageDecoder
|
||||
{
|
||||
public:
|
||||
NS_DECL_ISUPPORTS
|
||||
NS_DECL_NSIIMAGEDECODER
|
||||
NS_DECL_NSIOUTPUTSTREAM
|
||||
|
||||
nsPPMDecoder();
|
||||
virtual ~nsPPMDecoder();
|
||||
|
||||
private:
|
||||
nsCOMPtr<nsIImageFrame> mImage;
|
||||
nsCOMPtr<nsIImageRequest> mRequest;
|
||||
PRUint32 mDataReceived;
|
||||
PRUint32 mDataWritten;
|
||||
|
||||
PRUint32 mDataLeft;
|
||||
char *mPrevData;
|
||||
};
|
||||
|
||||
#endif // nsPPMDecoder_h__
|
|
@ -0,0 +1,42 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* 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 Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 2001 Netscape Communications Corporation. All
|
||||
* Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Stuart Parmenter <pavlov@netscape.com>
|
||||
*/
|
||||
|
||||
#include "nsIGenericFactory.h"
|
||||
#include "nsIModule.h"
|
||||
|
||||
#include "nsPPMDecoder.h"
|
||||
|
||||
// objects that just require generic constructors
|
||||
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsPPMDecoder)
|
||||
|
||||
static nsModuleComponentInfo components[] =
|
||||
{
|
||||
{ "ppm decoder",
|
||||
NS_PPMDECODER_CID,
|
||||
"@mozilla.org/image/decoder?image/x-portable-pixmap;1",
|
||||
nsPPMDecoderConstructor, },
|
||||
};
|
||||
|
||||
NS_IMPL_NSGETMODULE("nsPPMDecoderModule", components)
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
#
|
||||
# The contents of this file are subject to the Netscape 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/NPL/
|
||||
#
|
||||
# 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 Netscape
|
||||
# Communications Corporation. Portions created by Netscape are
|
||||
# Copyright (C) 1998 Netscape Communications Corporation. All
|
||||
# Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
|
||||
DEPTH=..\..
|
||||
|
||||
DIRS = public src decoders
|
||||
|
||||
!include $(DEPTH)\config\rules.mak
|
|
@ -0,0 +1,38 @@
|
|||
#
|
||||
# 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 Netscape
|
||||
# Communications Corporation. Portions created by Netscape are
|
||||
# Copyright (C) 2001 Netscape Communications Corporation. All
|
||||
# Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# Stuart Parmenter <pavlov@netscape.com>
|
||||
#
|
||||
|
||||
DEPTH = ..\..\..
|
||||
|
||||
include <$(DEPTH)/config/config.mak>
|
||||
|
||||
MODULE = imglib2
|
||||
XPIDL_MODULE = imglib2
|
||||
|
||||
XPIDLSRCS = \
|
||||
.\nsIImageDecoder.idl \
|
||||
.\nsIImageLoader.idl \
|
||||
.\nsIImageRequest.idl \
|
||||
$(NULL)
|
||||
|
||||
|
||||
include <$(DEPTH)\config\rules.mak>
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* 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 Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 2001 Netscape Communications Corporation.
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Stuart Parmenter <pavlov@netscape.com>
|
||||
*/
|
||||
|
||||
#include "nsISupports.idl"
|
||||
#include "nsIOutputStream.idl"
|
||||
#include "gfxtypes.idl"
|
||||
|
||||
interface nsIImageRequest;
|
||||
|
||||
[scriptable, uuid(9eebf43a-1dd1-11b2-953e-f1782f4cbad3)]
|
||||
interface nsIImageDecoder : nsIOutputStream
|
||||
{
|
||||
void init(in nsIImageRequest aRequest);
|
||||
|
||||
/// allows access to the nsIImage we have to put bits in to.
|
||||
readonly attribute nsIImageRequest request;
|
||||
};
|
|
@ -0,0 +1,42 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* 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 Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 2001 Netscape Communications Corporation.
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Stuart Parmenter <pavlov@netscape.com>
|
||||
*/
|
||||
|
||||
#include "nsISupports.idl"
|
||||
#include "gfxtypes.idl"
|
||||
|
||||
interface nsIImage;
|
||||
interface nsIImageRequest;
|
||||
interface nsISimpleEnumerator;
|
||||
interface nsIURI;
|
||||
|
||||
[scriptable, uuid(4c8cf1e0-1dd2-11b2-aff9-c51cdbfcb6da)]
|
||||
interface nsIImageLoader : nsISupports
|
||||
{
|
||||
nsIImageRequest loadImage(in nsIURI uri);
|
||||
|
||||
/**
|
||||
* Returns the channels contained directly in this group.
|
||||
* Enumerator element type: nsIImageRequest.
|
||||
*/
|
||||
readonly attribute nsISimpleEnumerator requests;
|
||||
};
|
|
@ -0,0 +1,39 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* 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 Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 2001 Netscape Communications Corporation.
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Stuart Parmenter <pavlov@netscape.com>
|
||||
*/
|
||||
|
||||
#include "nsISupports.idl"
|
||||
#include "nsIRequest.idl"
|
||||
#include "gfxtypes.idl"
|
||||
|
||||
interface nsIChannel;
|
||||
interface nsIImageContainer;
|
||||
interface nsIImageLoader;
|
||||
|
||||
[scriptable, uuid(ccf705f6-1dd1-11b2-82ef-e18eccf7f7ec)]
|
||||
interface nsIImageRequest : nsIRequest
|
||||
{
|
||||
void init(in nsIChannel aChannel);
|
||||
|
||||
/// @return the image object associated with the request.
|
||||
readonly attribute nsIImageContainer image;
|
||||
};
|
|
@ -0,0 +1,51 @@
|
|||
#!nmake
|
||||
#
|
||||
# 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 Netscape
|
||||
# Communications Corporation. Portions created by Netscape are
|
||||
# Copyright (C) 2001 Netscape Communications Corporation. All
|
||||
# Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# Stuart Parmenter <pavlov@netscape.com>
|
||||
#
|
||||
|
||||
DEPTH=..\..\..
|
||||
include <$(DEPTH)/config/config.mak>
|
||||
|
||||
MODULE = imglib2
|
||||
LIBRARY_NAME = imglib2
|
||||
DLL = $(OBJDIR)\$(LIBRARY_NAME).dll
|
||||
MAKE_OBJ_TYPE = DLL
|
||||
|
||||
OBJS = \
|
||||
.\$(OBJDIR)\nsImageLoader.obj \
|
||||
.\$(OBJDIR)\nsImageRequest.obj \
|
||||
.\$(OBJDIR)\nsImageFactory.obj \
|
||||
$(NULL)
|
||||
|
||||
LLIBS=\
|
||||
$(LIBNSPR) \
|
||||
$(DIST)\lib\xpcom.lib \
|
||||
$(NULL)
|
||||
|
||||
include <$(DEPTH)\config\rules.mak>
|
||||
|
||||
install:: $(DLL)
|
||||
$(MAKE_INSTALL) .\$(OBJDIR)\$(LIBRARY_NAME).dll $(DIST)\bin\components
|
||||
$(MAKE_INSTALL) .\$(OBJDIR)\$(LIBRARY_NAME).lib $(DIST)\lib
|
||||
|
||||
clobber::
|
||||
rm -f $(DIST)\bin\components\$(LIBRARY_NAME).dll
|
||||
rm -f $(DIST)\lib\$(LIBRARY_NAME).lib
|
|
@ -0,0 +1,49 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* 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 Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 2001 Netscape Communications Corporation. All
|
||||
* Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Stuart Parmenter <pavlov@netscape.com>
|
||||
*/
|
||||
|
||||
#include "nsIGenericFactory.h"
|
||||
#include "nsIModule.h"
|
||||
|
||||
#include "nsImageLoader.h"
|
||||
#include "nsImageRequest.h"
|
||||
|
||||
// objects that just require generic constructors
|
||||
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsImageLoader)
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsImageRequest)
|
||||
|
||||
static nsModuleComponentInfo components[] =
|
||||
{
|
||||
{ "image loader",
|
||||
NS_IMAGELOADER_CID,
|
||||
"@mozilla.org/image/loader;1",
|
||||
nsImageLoaderConstructor, },
|
||||
{ "image request",
|
||||
NS_IMAGEREQUEST_CID,
|
||||
"@mozilla.org/image/request;1",
|
||||
nsImageRequestConstructor, },
|
||||
|
||||
};
|
||||
|
||||
NS_IMPL_NSGETMODULE("nsImageLib2Module", components)
|
||||
|
|
@ -0,0 +1,81 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* 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 Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 2001 Netscape Communications Corporation.
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Stuart Parmenter <pavlov@netscape.com>
|
||||
*/
|
||||
|
||||
#include "nsImageLoader.h"
|
||||
|
||||
#include "nsIImageRequest.h"
|
||||
|
||||
#include "nsIServiceManager.h"
|
||||
|
||||
#include "nsIChannel.h"
|
||||
#include "nsIIOService.h"
|
||||
#include "nsIStreamListener.h"
|
||||
#include "nsIURI.h"
|
||||
|
||||
NS_IMPL_ISUPPORTS1(nsImageLoader, nsIImageLoader)
|
||||
|
||||
nsImageLoader::nsImageLoader()
|
||||
{
|
||||
NS_INIT_ISUPPORTS();
|
||||
/* member initializers and constructor code */
|
||||
}
|
||||
|
||||
nsImageLoader::~nsImageLoader()
|
||||
{
|
||||
/* destructor code */
|
||||
}
|
||||
|
||||
/* nsIImageRequest loadImage (in nsIURI uri, in gfx_dimension width, in gfx_dimension height); */
|
||||
NS_IMETHODIMP nsImageLoader::LoadImage(nsIURI *aURI, nsIImageRequest **_retval)
|
||||
{
|
||||
|
||||
/* do we need a loadgroup of channels here? what does that buy us?
|
||||
if (!mLoadGroup)
|
||||
NS_NewLoadGroup
|
||||
*/
|
||||
|
||||
nsCOMPtr<nsIIOService> ioserv(do_GetService("@mozilla.org/network/io-service;1"));
|
||||
if (!ioserv) return NS_ERROR_FAILURE;
|
||||
|
||||
nsCOMPtr<nsIChannel> newChannel;
|
||||
ioserv->NewChannelFromURI(aURI, getter_AddRefs(newChannel));
|
||||
if (!newChannel) return NS_ERROR_FAILURE;
|
||||
|
||||
newChannel->SetOwner(this); // the channel is now holding a strong ref to 'this'
|
||||
|
||||
// XXX look at the progid
|
||||
nsCOMPtr<nsIImageRequest> imgRequest(do_CreateInstance("@mozilla.org/image/request;1"));
|
||||
imgRequest->Init(newChannel);
|
||||
|
||||
*_retval = imgRequest;
|
||||
NS_ADDREF(*_retval);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* readonly attribute nsISimpleEnumerator requests; */
|
||||
NS_IMETHODIMP nsImageLoader::GetRequests(nsISimpleEnumerator * *aRequests)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* 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 Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 2001 Netscape Communications Corporation.
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Stuart Parmenter <pavlov@netscape.com>
|
||||
*/
|
||||
|
||||
#include "nsIImageLoader.h"
|
||||
//#include "nsIDeviceContext.h"
|
||||
#include "nsILoadGroup.h"
|
||||
#include "nsCOMPtr.h"
|
||||
|
||||
#define NS_IMAGELOADER_CID \
|
||||
{ /* 9f6a0d2e-1dd1-11b2-a5b8-951f13c846f7 */ \
|
||||
0x9f6a0d2e, \
|
||||
0x1dd1, \
|
||||
0x11b2, \
|
||||
{0xa5, 0xb8, 0x95, 0x1f, 0x13, 0xc8, 0x46, 0xf7} \
|
||||
}
|
||||
|
||||
class nsImageLoader : public nsIImageLoader
|
||||
{
|
||||
public:
|
||||
NS_DECL_ISUPPORTS
|
||||
NS_DECL_NSIIMAGELOADER
|
||||
|
||||
nsImageLoader();
|
||||
virtual ~nsImageLoader();
|
||||
|
||||
private:
|
||||
// nsCOMPtr<nsIDeviceContext> mDeviceContext;
|
||||
nsCOMPtr<nsILoadGroup> mLoadGroup;
|
||||
};
|
|
@ -0,0 +1,175 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* 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 Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 2001 Netscape Communications Corporation.
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Stuart Parmenter <pavlov@netscape.com>
|
||||
*/
|
||||
|
||||
#include "nsImageRequest.h"
|
||||
|
||||
#include "nsXPIDLString.h"
|
||||
|
||||
#include "nsIChannel.h"
|
||||
#include "nsIInputStream.h"
|
||||
#include "nsIImageLoader.h"
|
||||
#include "nsIComponentManager.h"
|
||||
|
||||
#include "nsIComponentManager.h"
|
||||
|
||||
#include "nsString.h"
|
||||
|
||||
#include "nspr.h"
|
||||
|
||||
NS_IMPL_ISUPPORTS4(nsImageRequest, nsIImageRequest, nsIRequest, nsIStreamListener, nsIStreamObserver)
|
||||
|
||||
nsImageRequest::nsImageRequest()
|
||||
{
|
||||
NS_INIT_ISUPPORTS();
|
||||
/* member initializers and constructor code */
|
||||
}
|
||||
|
||||
nsImageRequest::~nsImageRequest()
|
||||
{
|
||||
/* destructor code */
|
||||
}
|
||||
|
||||
/* void init (in nsIChannel aChannel, in gfx_dimension width, in gfx_dimension height); */
|
||||
NS_IMETHODIMP nsImageRequest::Init(nsIChannel *aChannel)
|
||||
{
|
||||
if (mImage)
|
||||
return NS_ERROR_FAILURE; // XXX
|
||||
|
||||
mChannel = aChannel;
|
||||
|
||||
// XXX do not init the image here. this has to be done from the image decoder.
|
||||
mImage = do_CreateInstance("@mozilla.org/gfx/image;2");
|
||||
|
||||
return aChannel->AsyncRead(NS_STATIC_CAST(nsIStreamListener*, this), nsnull);
|
||||
}
|
||||
|
||||
/* readonly attribute nsIImage image; */
|
||||
NS_IMETHODIMP nsImageRequest::GetImage(nsIImageContainer * *aImage)
|
||||
{
|
||||
*aImage = mImage;
|
||||
NS_IF_ADDREF(*aImage);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/** nsIRequest methods **/
|
||||
|
||||
/* readonly attribute wstring name; */
|
||||
NS_IMETHODIMP nsImageRequest::GetName(PRUnichar * *aName)
|
||||
{
|
||||
return mChannel->GetName(aName);
|
||||
}
|
||||
|
||||
/* boolean isPending (); */
|
||||
NS_IMETHODIMP nsImageRequest::IsPending(PRBool *_retval)
|
||||
{
|
||||
return mChannel->IsPending(_retval);
|
||||
}
|
||||
|
||||
/* readonly attribute nsresult status; */
|
||||
NS_IMETHODIMP nsImageRequest::GetStatus(nsresult *aStatus)
|
||||
{
|
||||
return mChannel->GetStatus(aStatus);
|
||||
}
|
||||
|
||||
/* void cancel (in nsresult status); */
|
||||
NS_IMETHODIMP nsImageRequest::Cancel(nsresult status)
|
||||
{
|
||||
return mChannel->Cancel(status);
|
||||
}
|
||||
|
||||
/* void suspend (); */
|
||||
NS_IMETHODIMP nsImageRequest::Suspend()
|
||||
{
|
||||
return mChannel->Suspend();
|
||||
}
|
||||
|
||||
/* void resume (); */
|
||||
NS_IMETHODIMP nsImageRequest::Resume()
|
||||
{
|
||||
return mChannel->Resume();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/** nsIStreamObserver methods **/
|
||||
|
||||
/* void onStartRequest (in nsIChannel channel, in nsISupports ctxt); */
|
||||
NS_IMETHODIMP nsImageRequest::OnStartRequest(nsIChannel *channel, nsISupports *ctxt)
|
||||
{
|
||||
nsXPIDLCString contentType;
|
||||
channel->GetContentType(getter_Copies(contentType));
|
||||
printf("content type is %s\n", contentType.get());
|
||||
|
||||
nsCAutoString conid("@mozilla.org/image/decoder?");
|
||||
conid += contentType.get();
|
||||
conid += ";1";
|
||||
|
||||
mDecoder = do_CreateInstance(conid);
|
||||
|
||||
if (!mDecoder) {
|
||||
// no image decoder for this mimetype :(
|
||||
channel->Cancel(NS_BINDING_ABORTED);
|
||||
|
||||
// XXX notify the person that owns us now that wants the nsIImageContainer off of us?
|
||||
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
mDecoder->Init(NS_STATIC_CAST(nsIImageRequest*, this));
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* void onStopRequest (in nsIChannel channel, in nsISupports ctxt, in nsresult status, in wstring statusArg); */
|
||||
NS_IMETHODIMP nsImageRequest::OnStopRequest(nsIChannel *channel, nsISupports *ctxt, nsresult status, const PRUnichar *statusArg)
|
||||
{
|
||||
if (!mDecoder) return NS_ERROR_FAILURE;
|
||||
|
||||
return mDecoder->Close();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/** nsIStreamListener methods **/
|
||||
|
||||
/* void onDataAvailable (in nsIChannel channel, in nsISupports ctxt, in nsIInputStream inStr, in unsigned long sourceOffset, in unsigned long count); */
|
||||
NS_IMETHODIMP nsImageRequest::OnDataAvailable(nsIChannel *channel, nsISupports *ctxt, nsIInputStream *inStr, PRUint32 sourceOffset, PRUint32 count)
|
||||
{
|
||||
if (!mDecoder) return NS_ERROR_FAILURE;
|
||||
|
||||
PRUint32 wrote;
|
||||
return mDecoder->WriteFrom(inStr, count, &wrote);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* 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 Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 2001 Netscape Communications Corporation.
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Stuart Parmenter <pavlov@netscape.com>
|
||||
*/
|
||||
|
||||
#include "nsIImageRequest.h"
|
||||
|
||||
#include "nsIChannel.h"
|
||||
#include "nsIImageContainer.h"
|
||||
#include "nsIImageDecoder.h"
|
||||
#include "nsIStreamListener.h"
|
||||
#include "nsCOMPtr.h"
|
||||
|
||||
#define NS_IMAGEREQUEST_CID \
|
||||
{ /* 9f733dd6-1dd1-11b2-8cdf-effb70d1ea71 */ \
|
||||
0x9f733dd6, \
|
||||
0x1dd1, \
|
||||
0x11b2, \
|
||||
{0x8c, 0xdf, 0xef, 0xfb, 0x70, 0xd1, 0xea, 0x71} \
|
||||
}
|
||||
|
||||
class nsImageRequest : public nsIImageRequest, public nsIStreamListener
|
||||
{
|
||||
public:
|
||||
NS_DECL_ISUPPORTS
|
||||
NS_DECL_NSIIMAGEREQUEST
|
||||
NS_DECL_NSIREQUEST
|
||||
NS_DECL_NSISTREAMLISTENER
|
||||
NS_DECL_NSISTREAMOBSERVER
|
||||
|
||||
nsImageRequest();
|
||||
virtual ~nsImageRequest();
|
||||
/* additional members */
|
||||
|
||||
private:
|
||||
nsCOMPtr<nsIChannel> mChannel;
|
||||
nsCOMPtr<nsIImageContainer> mImage;
|
||||
nsCOMPtr<nsIImageDecoder> mDecoder;
|
||||
};
|
Загрузка…
Ссылка в новой задаче