JPEG and PNG code taken out of image.cpp

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@2051 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Julian Smart 1999-04-06 13:44:37 +00:00
parent 58abfef64f
commit e9c4b1a2b5
16 changed files with 626 additions and 498 deletions

View File

@ -241,6 +241,14 @@ void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
bool MyApp::OnInit()
{
#if wxUSE_LIBPNG
wxImage::AddHandler( new wxPNGHandler );
#endif
#if wxUSE_LIBJPEG
wxImage::AddHandler( new wxJPEGHandler );
#endif
wxFrame *frame = new MyFrame();
frame->Show( TRUE );

View File

@ -23,14 +23,6 @@
#include "wx/debug.h"
#include "wx/log.h"
#include "wx/app.h"
#if wxUSE_LIBPNG
#include "../png/png.h"
#endif
#if wxUSE_LIBJPEG
extern "C" {
#include <jpeglib.h>
}
#endif
#include "wx/filefn.h"
#include "wx/wfstream.h"
#include "wx/intl.h"
@ -462,9 +454,6 @@ wxImageHandler *wxImage::FindHandler( long bitmapType )
void wxImage::InitStandardHandlers()
{
AddHandler( new wxBMPHandler );
#if wxUSE_LIBPNG
AddHandler( new wxPNGHandler );
#endif
}
void wxImage::CleanUpHandlers()
@ -500,490 +489,6 @@ bool wxImageHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSE
}
#endif // wxUSE_STREAMS
//-----------------------------------------------------------------------------
// wxPNGHandler
//-----------------------------------------------------------------------------
#if wxUSE_LIBPNG
#if !USE_SHARED_LIBRARIES
IMPLEMENT_DYNAMIC_CLASS(wxPNGHandler,wxImageHandler)
#endif
#if wxUSE_STREAMS
static void _PNG_stream_reader( png_structp png_ptr, png_bytep data, png_size_t length )
{
((wxInputStream*) png_get_io_ptr( png_ptr )) -> Read(data, length);
}
static void _PNG_stream_writer( png_structp png_ptr, png_bytep data, png_size_t length )
{
((wxOutputStream*) png_get_io_ptr( png_ptr )) -> Write(data, length);
}
bool wxPNGHandler::LoadFile( wxImage *image, wxInputStream& stream )
{
// VZ: as this function uses setjmp() the only fool proof error handling
// method is to use goto (setjmp is not really C++ dtors friendly...)
unsigned char **lines = (unsigned char **) NULL;
unsigned int i;
png_infop info_ptr = (png_infop) NULL;
image->Destroy();
png_structp png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING,
(voidp) NULL,
(png_error_ptr) NULL,
(png_error_ptr) NULL );
if (!png_ptr)
goto error;
info_ptr = png_create_info_struct( png_ptr );
if (!info_ptr)
goto error;
if (setjmp(png_ptr->jmpbuf))
goto error;
if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
goto error;
png_set_read_fn( png_ptr, &stream, _PNG_stream_reader);
png_uint_32 width,height;
int bit_depth,color_type,interlace_type;
png_read_info( png_ptr, info_ptr );
png_get_IHDR( png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, (int*) NULL, (int*) NULL );
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_expand( png_ptr );
png_set_strip_16( png_ptr );
png_set_packing( png_ptr );
if (png_get_valid( png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_expand( png_ptr );
png_set_filler( png_ptr, 0xff, PNG_FILLER_AFTER );
image->Create( width, height );
if (!image->Ok())
goto error;
lines = (unsigned char **)malloc( height * sizeof(unsigned char *) );
if (lines == NULL)
goto error;
for (i = 0; i < height; i++)
{
if ((lines[i] = (unsigned char *)malloc(width * (sizeof(unsigned char) * 4))) == NULL)
{
for ( unsigned int n = 0; n < i; n++ )
free( lines[n] );
goto error;
}
}
// loaded successfully!
{
int transp = 0;
png_read_image( png_ptr, lines );
png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
unsigned char *ptr = image->GetData();
if ((color_type == PNG_COLOR_TYPE_GRAY) ||
(color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
{
for (unsigned int y = 0; y < height; y++)
{
unsigned char *ptr2 = lines[y];
for (unsigned int x = 0; x < width; x++)
{
unsigned char r = *ptr2++;
unsigned char a = *ptr2++;
if (a < 128)
{
*ptr++ = 255;
*ptr++ = 0;
*ptr++ = 255;
transp = 1;
}
else
{
*ptr++ = r;
*ptr++ = r;
*ptr++ = r;
}
}
}
}
else
{
for (unsigned int y = 0; y < height; y++)
{
unsigned char *ptr2 = lines[y];
for (unsigned int x = 0; x < width; x++)
{
unsigned char r = *ptr2++;
unsigned char g = *ptr2++;
unsigned char b = *ptr2++;
unsigned char a = *ptr2++;
if (a < 128)
{
*ptr++ = 255;
*ptr++ = 0;
*ptr++ = 255;
transp = 1;
}
else
{
if ((r == 255) && (g == 0) && (b == 255)) r = 254;
*ptr++ = r;
*ptr++ = g;
*ptr++ = b;
}
}
}
}
for ( unsigned int j = 0; j < height; j++ )
free( lines[j] );
free( lines );
if (transp)
{
image->SetMaskColour( 255, 0, 255 );
}
else
{
image->SetMask( FALSE );
}
}
return TRUE;
error:
wxLogError(_("Couldn't load a PNG image - probably file is corrupted."));
if ( image->Ok() )
{
image->Destroy();
}
if ( lines )
{
free( lines );
}
if ( png_ptr )
{
if ( info_ptr )
{
png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
free(info_ptr);
}
else
png_destroy_read_struct( &png_ptr, (png_infopp) NULL, (png_infopp) NULL );
}
return FALSE;
}
bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream )
{
{
png_structp png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
{
return FALSE;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
return FALSE;
}
if (setjmp(png_ptr->jmpbuf))
{
png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
return FALSE;
}
png_set_write_fn( png_ptr, &stream, _PNG_stream_writer, NULL);
png_set_IHDR( png_ptr, info_ptr, image->GetWidth(), image->GetHeight(), 8,
PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_color_8 sig_bit;
sig_bit.red = 8;
sig_bit.green = 8;
sig_bit.blue = 8;
sig_bit.alpha = 8;
png_set_sBIT( png_ptr, info_ptr, &sig_bit );
png_write_info( png_ptr, info_ptr );
png_set_shift( png_ptr, &sig_bit );
png_set_packing( png_ptr );
unsigned char *data = (unsigned char *)malloc( image->GetWidth()*4 );
if (!data)
{
png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
return FALSE;
}
for (int y = 0; y < image->GetHeight(); y++)
{
unsigned char *ptr = image->GetData() + (y * image->GetWidth() * 3);
for (int x = 0; x < image->GetWidth(); x++)
{
data[(x << 2) + 0] = *ptr++;
data[(x << 2) + 1] = *ptr++;
data[(x << 2) + 2] = *ptr++;
if ((data[(x << 2) + 0] == image->GetMaskRed()) &&
(data[(x << 2) + 1] == image->GetMaskGreen()) &&
(data[(x << 2) + 2] == image->GetMaskBlue()))
{
data[(x << 2) + 3] = 0;
}
else
{
data[(x << 2) + 3] = 255;
}
}
png_bytep row_ptr = data;
png_write_rows( png_ptr, &row_ptr, 1 );
}
free(data);
png_write_end( png_ptr, info_ptr );
png_destroy_write_struct( &png_ptr, (png_infopp)&info_ptr );
}
return TRUE;
}
#endif // wxUSE_STREAMS
#endif
// wxUSE_LIBPNG
//-----------------------------------------------------------------------------
// wxJPEGHandler
//-----------------------------------------------------------------------------
#if wxUSE_LIBJPEG
#if !USE_SHARED_LIBRARIES
IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler,wxImageHandler)
#endif
#if wxUSE_STREAMS
//------------- JPEG Data Source Manager
typedef struct {
struct jpeg_source_mgr pub; /* public fields */
JOCTET* buffer; /* start of buffer */
} my_source_mgr;
typedef my_source_mgr * my_src_ptr;
METHODDEF(void) my_init_source ( j_decompress_ptr cinfo )
{
}
METHODDEF(boolean) my_fill_input_buffer ( j_decompress_ptr cinfo )
{
return TRUE;
}
METHODDEF(void) my_skip_input_data ( j_decompress_ptr cinfo, long num_bytes )
{
my_src_ptr src = (my_src_ptr) cinfo->src;
src->pub.next_input_byte += (size_t) num_bytes;
src->pub.bytes_in_buffer -= (size_t) num_bytes;
}
METHODDEF(void) my_term_source ( j_decompress_ptr cinfo )
{
my_src_ptr src = (my_src_ptr) cinfo->src;
free (src->buffer);
}
void jpeg_wxio_src( j_decompress_ptr cinfo, wxInputStream& infile )
{
my_src_ptr src;
if (cinfo->src == NULL) { /* first time for this JPEG object? */
cinfo->src = (struct jpeg_source_mgr *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
sizeof(my_source_mgr));
src = (my_src_ptr) cinfo->src;
}
src = (my_src_ptr) cinfo->src;
src->pub.bytes_in_buffer = infile.StreamSize(); /* forces fill_input_buffer on first read */
src->buffer = (JOCTET *) malloc (infile.StreamSize());
src->pub.next_input_byte = src->buffer; /* until buffer loaded */
infile.Read(src->buffer, infile.StreamSize());
src->pub.init_source = my_init_source;
src->pub.fill_input_buffer = my_fill_input_buffer;
src->pub.skip_input_data = my_skip_input_data;
src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
src->pub.term_source = my_term_source;
}
bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream )
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
JSAMPARRAY tempbuf;
unsigned char *ptr;
unsigned stride;
image->Destroy();
cinfo.err = jpeg_std_error( &jerr );
jpeg_create_decompress( &cinfo );
jpeg_wxio_src( &cinfo, stream );
jpeg_read_header( &cinfo, TRUE );
cinfo.out_color_space = JCS_RGB;
jpeg_start_decompress( &cinfo );
image->Create( cinfo.image_width, cinfo.image_height );
if (!image->Ok()) {
jpeg_finish_decompress( &cinfo );
jpeg_destroy_decompress( &cinfo );
return FALSE;
}
image->SetMask( FALSE );
ptr = image->GetData();
stride = cinfo.output_width * 3;
tempbuf = (*cinfo.mem->alloc_sarray)
((j_common_ptr) &cinfo, JPOOL_IMAGE, stride, 1 );
while ( cinfo.output_scanline < cinfo.output_height ) {
jpeg_read_scanlines( &cinfo, tempbuf, 1 );
memcpy( ptr, tempbuf[0], stride );
ptr += stride;
}
jpeg_finish_decompress( &cinfo );
jpeg_destroy_decompress( &cinfo );
return TRUE;
}
typedef struct {
struct jpeg_destination_mgr pub;
wxOutputStream *stream;
JOCTET * buffer;
} my_destination_mgr;
typedef my_destination_mgr * my_dest_ptr;
#define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
METHODDEF(void) init_destination (j_compress_ptr cinfo)
{
my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
/* Allocate the output buffer --- it will be released when done with image */
dest->buffer = (JOCTET *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
OUTPUT_BUF_SIZE * sizeof(JOCTET));
dest->pub.next_output_byte = dest->buffer;
dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
}
METHODDEF(boolean) empty_output_buffer (j_compress_ptr cinfo)
{
my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
dest->stream->Write(dest->buffer, OUTPUT_BUF_SIZE);
dest->pub.next_output_byte = dest->buffer;
dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
return TRUE;
}
METHODDEF(void) term_destination (j_compress_ptr cinfo)
{
my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
/* Write any data remaining in the buffer */
if (datacount > 0)
dest->stream->Write(dest->buffer, datacount);
}
GLOBAL(void) jpeg_wxio_dest (j_compress_ptr cinfo, wxOutputStream& outfile)
{
my_dest_ptr dest;
if (cinfo->dest == NULL) { /* first time for this JPEG object? */
cinfo->dest = (struct jpeg_destination_mgr *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
sizeof(my_destination_mgr));
}
dest = (my_dest_ptr) cinfo->dest;
dest->pub.init_destination = init_destination;
dest->pub.empty_output_buffer = empty_output_buffer;
dest->pub.term_destination = term_destination;
dest->stream = &outfile;
}
bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream )
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
JSAMPLE *image_buffer;
int stride; /* physical row width in image buffer */
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_wxio_dest(&cinfo, stream);
cinfo.image_width = image->GetWidth();
cinfo.image_height = image->GetHeight();
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_start_compress(&cinfo, TRUE);
stride = cinfo.image_width * 3; /* JSAMPLEs per row in image_buffer */
image_buffer = image->GetData();
while (cinfo.next_scanline < cinfo.image_height) {
row_pointer[0] = &image_buffer[cinfo.next_scanline * stride];
jpeg_write_scanlines( &cinfo, row_pointer, 1 );
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
return TRUE;
}
#endif // wxUSE_STREAMS
#endif
// wxUSE_LIBJPEG
//-----------------------------------------------------------------------------
// wxBMPHandler
//-----------------------------------------------------------------------------

257
src/common/imagjpeg.cpp Normal file
View File

@ -0,0 +1,257 @@
/////////////////////////////////////////////////////////////////////////////
// Name: imagjpeg.cpp
// Purpose: wxImage JPEG handler
// Author: Robert Roebling
// RCS-ID: $Id$
// Copyright: (c) Robert Roebling
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#ifdef __GNUG__
#pragma implementation "image.h"
#endif
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#include "wx/image.h"
#include "wx/bitmap.h"
#include "wx/debug.h"
#include "wx/log.h"
#include "wx/app.h"
#if wxUSE_LIBJPEG
extern "C" {
#include <jpeglib.h>
}
#endif
#include "wx/filefn.h"
#include "wx/wfstream.h"
#include "wx/intl.h"
#include "wx/module.h"
// For memcpy
#include <string.h>
#ifdef __SALFORDC__
#ifdef FAR
#undef FAR
#endif
#endif
#ifdef __WXMSW__
#include <windows.h>
#endif
//-----------------------------------------------------------------------------
// wxJPEGHandler
//-----------------------------------------------------------------------------
#if wxUSE_LIBJPEG
#if !USE_SHARED_LIBRARIES
IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler,wxImageHandler)
#endif
#if wxUSE_STREAMS
//------------- JPEG Data Source Manager
typedef struct {
struct jpeg_source_mgr pub; /* public fields */
JOCTET* buffer; /* start of buffer */
} my_source_mgr;
typedef my_source_mgr * my_src_ptr;
METHODDEF(void) my_init_source ( j_decompress_ptr cinfo )
{
}
METHODDEF(boolean) my_fill_input_buffer ( j_decompress_ptr cinfo )
{
return TRUE;
}
METHODDEF(void) my_skip_input_data ( j_decompress_ptr cinfo, long num_bytes )
{
my_src_ptr src = (my_src_ptr) cinfo->src;
src->pub.next_input_byte += (size_t) num_bytes;
src->pub.bytes_in_buffer -= (size_t) num_bytes;
}
METHODDEF(void) my_term_source ( j_decompress_ptr cinfo )
{
my_src_ptr src = (my_src_ptr) cinfo->src;
free (src->buffer);
}
void jpeg_wxio_src( j_decompress_ptr cinfo, wxInputStream& infile )
{
my_src_ptr src;
if (cinfo->src == NULL) { /* first time for this JPEG object? */
cinfo->src = (struct jpeg_source_mgr *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
sizeof(my_source_mgr));
src = (my_src_ptr) cinfo->src;
}
src = (my_src_ptr) cinfo->src;
src->pub.bytes_in_buffer = infile.StreamSize(); /* forces fill_input_buffer on first read */
src->buffer = (JOCTET *) malloc (infile.StreamSize());
src->pub.next_input_byte = src->buffer; /* until buffer loaded */
infile.Read(src->buffer, infile.StreamSize());
src->pub.init_source = my_init_source;
src->pub.fill_input_buffer = my_fill_input_buffer;
src->pub.skip_input_data = my_skip_input_data;
src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
src->pub.term_source = my_term_source;
}
bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream )
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
JSAMPARRAY tempbuf;
unsigned char *ptr;
unsigned stride;
image->Destroy();
cinfo.err = jpeg_std_error( &jerr );
jpeg_create_decompress( &cinfo );
jpeg_wxio_src( &cinfo, stream );
jpeg_read_header( &cinfo, TRUE );
cinfo.out_color_space = JCS_RGB;
jpeg_start_decompress( &cinfo );
image->Create( cinfo.image_width, cinfo.image_height );
if (!image->Ok()) {
jpeg_finish_decompress( &cinfo );
jpeg_destroy_decompress( &cinfo );
return FALSE;
}
image->SetMask( FALSE );
ptr = image->GetData();
stride = cinfo.output_width * 3;
tempbuf = (*cinfo.mem->alloc_sarray)
((j_common_ptr) &cinfo, JPOOL_IMAGE, stride, 1 );
while ( cinfo.output_scanline < cinfo.output_height ) {
jpeg_read_scanlines( &cinfo, tempbuf, 1 );
memcpy( ptr, tempbuf[0], stride );
ptr += stride;
}
jpeg_finish_decompress( &cinfo );
jpeg_destroy_decompress( &cinfo );
return TRUE;
}
typedef struct {
struct jpeg_destination_mgr pub;
wxOutputStream *stream;
JOCTET * buffer;
} my_destination_mgr;
typedef my_destination_mgr * my_dest_ptr;
#define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
METHODDEF(void) init_destination (j_compress_ptr cinfo)
{
my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
/* Allocate the output buffer --- it will be released when done with image */
dest->buffer = (JOCTET *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
OUTPUT_BUF_SIZE * sizeof(JOCTET));
dest->pub.next_output_byte = dest->buffer;
dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
}
METHODDEF(boolean) empty_output_buffer (j_compress_ptr cinfo)
{
my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
dest->stream->Write(dest->buffer, OUTPUT_BUF_SIZE);
dest->pub.next_output_byte = dest->buffer;
dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
return TRUE;
}
METHODDEF(void) term_destination (j_compress_ptr cinfo)
{
my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
/* Write any data remaining in the buffer */
if (datacount > 0)
dest->stream->Write(dest->buffer, datacount);
}
GLOBAL(void) jpeg_wxio_dest (j_compress_ptr cinfo, wxOutputStream& outfile)
{
my_dest_ptr dest;
if (cinfo->dest == NULL) { /* first time for this JPEG object? */
cinfo->dest = (struct jpeg_destination_mgr *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
sizeof(my_destination_mgr));
}
dest = (my_dest_ptr) cinfo->dest;
dest->pub.init_destination = init_destination;
dest->pub.empty_output_buffer = empty_output_buffer;
dest->pub.term_destination = term_destination;
dest->stream = &outfile;
}
bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream )
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
JSAMPLE *image_buffer;
int stride; /* physical row width in image buffer */
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_wxio_dest(&cinfo, stream);
cinfo.image_width = image->GetWidth();
cinfo.image_height = image->GetHeight();
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_start_compress(&cinfo, TRUE);
stride = cinfo.image_width * 3; /* JSAMPLEs per row in image_buffer */
image_buffer = image->GetData();
while (cinfo.next_scanline < cinfo.image_height) {
row_pointer[0] = &image_buffer[cinfo.next_scanline * stride];
jpeg_write_scanlines( &cinfo, row_pointer, 1 );
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
return TRUE;
}
#endif // wxUSE_STREAMS
#endif
// wxUSE_LIBJPEG

316
src/common/imagpng.cpp Normal file
View File

@ -0,0 +1,316 @@
/////////////////////////////////////////////////////////////////////////////
// Name: imagepng.cpp
// Purpose: wxImage PNG handler
// Author: Robert Roebling
// RCS-ID: $Id$
// Copyright: (c) Robert Roebling
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#ifdef __GNUG__
#pragma implementation "image.h"
#endif
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#include "wx/image.h"
#include "wx/bitmap.h"
#include "wx/debug.h"
#include "wx/log.h"
#include "wx/app.h"
#if wxUSE_LIBPNG
#include "../png/png.h"
#endif
#include "wx/filefn.h"
#include "wx/wfstream.h"
#include "wx/intl.h"
#include "wx/module.h"
// For memcpy
#include <string.h>
#ifdef __SALFORDC__
#ifdef FAR
#undef FAR
#endif
#endif
#ifdef __WXMSW__
#include <windows.h>
#endif
//-----------------------------------------------------------------------------
// wxPNGHandler
//-----------------------------------------------------------------------------
#if wxUSE_LIBPNG
#if !USE_SHARED_LIBRARIES
IMPLEMENT_DYNAMIC_CLASS(wxPNGHandler,wxImageHandler)
#endif
#if wxUSE_STREAMS
static void _PNG_stream_reader( png_structp png_ptr, png_bytep data, png_size_t length )
{
((wxInputStream*) png_get_io_ptr( png_ptr )) -> Read(data, length);
}
static void _PNG_stream_writer( png_structp png_ptr, png_bytep data, png_size_t length )
{
((wxOutputStream*) png_get_io_ptr( png_ptr )) -> Write(data, length);
}
bool wxPNGHandler::LoadFile( wxImage *image, wxInputStream& stream )
{
// VZ: as this function uses setjmp() the only fool proof error handling
// method is to use goto (setjmp is not really C++ dtors friendly...)
unsigned char **lines = (unsigned char **) NULL;
unsigned int i;
png_infop info_ptr = (png_infop) NULL;
image->Destroy();
png_structp png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING,
(voidp) NULL,
(png_error_ptr) NULL,
(png_error_ptr) NULL );
if (!png_ptr)
goto error;
info_ptr = png_create_info_struct( png_ptr );
if (!info_ptr)
goto error;
if (setjmp(png_ptr->jmpbuf))
goto error;
if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
goto error;
png_set_read_fn( png_ptr, &stream, _PNG_stream_reader);
png_uint_32 width,height;
int bit_depth,color_type,interlace_type;
png_read_info( png_ptr, info_ptr );
png_get_IHDR( png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, (int*) NULL, (int*) NULL );
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_expand( png_ptr );
png_set_strip_16( png_ptr );
png_set_packing( png_ptr );
if (png_get_valid( png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_expand( png_ptr );
png_set_filler( png_ptr, 0xff, PNG_FILLER_AFTER );
image->Create( width, height );
if (!image->Ok())
goto error;
lines = (unsigned char **)malloc( height * sizeof(unsigned char *) );
if (lines == NULL)
goto error;
for (i = 0; i < height; i++)
{
if ((lines[i] = (unsigned char *)malloc(width * (sizeof(unsigned char) * 4))) == NULL)
{
for ( unsigned int n = 0; n < i; n++ )
free( lines[n] );
goto error;
}
}
// loaded successfully!
{
int transp = 0;
png_read_image( png_ptr, lines );
png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
unsigned char *ptr = image->GetData();
if ((color_type == PNG_COLOR_TYPE_GRAY) ||
(color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
{
for (unsigned int y = 0; y < height; y++)
{
unsigned char *ptr2 = lines[y];
for (unsigned int x = 0; x < width; x++)
{
unsigned char r = *ptr2++;
unsigned char a = *ptr2++;
if (a < 128)
{
*ptr++ = 255;
*ptr++ = 0;
*ptr++ = 255;
transp = 1;
}
else
{
*ptr++ = r;
*ptr++ = r;
*ptr++ = r;
}
}
}
}
else
{
for (unsigned int y = 0; y < height; y++)
{
unsigned char *ptr2 = lines[y];
for (unsigned int x = 0; x < width; x++)
{
unsigned char r = *ptr2++;
unsigned char g = *ptr2++;
unsigned char b = *ptr2++;
unsigned char a = *ptr2++;
if (a < 128)
{
*ptr++ = 255;
*ptr++ = 0;
*ptr++ = 255;
transp = 1;
}
else
{
if ((r == 255) && (g == 0) && (b == 255)) r = 254;
*ptr++ = r;
*ptr++ = g;
*ptr++ = b;
}
}
}
}
for ( unsigned int j = 0; j < height; j++ )
free( lines[j] );
free( lines );
if (transp)
{
image->SetMaskColour( 255, 0, 255 );
}
else
{
image->SetMask( FALSE );
}
}
return TRUE;
error:
wxLogError(_("Couldn't load a PNG image - probably file is corrupted."));
if ( image->Ok() )
{
image->Destroy();
}
if ( lines )
{
free( lines );
}
if ( png_ptr )
{
if ( info_ptr )
{
png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
free(info_ptr);
}
else
png_destroy_read_struct( &png_ptr, (png_infopp) NULL, (png_infopp) NULL );
}
return FALSE;
}
bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream )
{
{
png_structp png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
{
return FALSE;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
return FALSE;
}
if (setjmp(png_ptr->jmpbuf))
{
png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
return FALSE;
}
png_set_write_fn( png_ptr, &stream, _PNG_stream_writer, NULL);
png_set_IHDR( png_ptr, info_ptr, image->GetWidth(), image->GetHeight(), 8,
PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_color_8 sig_bit;
sig_bit.red = 8;
sig_bit.green = 8;
sig_bit.blue = 8;
sig_bit.alpha = 8;
png_set_sBIT( png_ptr, info_ptr, &sig_bit );
png_write_info( png_ptr, info_ptr );
png_set_shift( png_ptr, &sig_bit );
png_set_packing( png_ptr );
unsigned char *data = (unsigned char *)malloc( image->GetWidth()*4 );
if (!data)
{
png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
return FALSE;
}
for (int y = 0; y < image->GetHeight(); y++)
{
unsigned char *ptr = image->GetData() + (y * image->GetWidth() * 3);
for (int x = 0; x < image->GetWidth(); x++)
{
data[(x << 2) + 0] = *ptr++;
data[(x << 2) + 1] = *ptr++;
data[(x << 2) + 2] = *ptr++;
if ((data[(x << 2) + 0] == image->GetMaskRed()) &&
(data[(x << 2) + 1] == image->GetMaskGreen()) &&
(data[(x << 2) + 2] == image->GetMaskBlue()))
{
data[(x << 2) + 3] = 0;
}
else
{
data[(x << 2) + 3] = 255;
}
}
png_bytep row_ptr = data;
png_write_rows( png_ptr, &row_ptr, 1 );
}
free(data);
png_write_end( png_ptr, info_ptr );
png_destroy_write_struct( &png_ptr, (png_infopp)&info_ptr );
}
return TRUE;
}
#endif // wxUSE_STREAMS
#endif
// wxUSE_LIBPNG

View File

@ -26,6 +26,8 @@ LIB_CPP_SRC=\
common/helpbase.cpp \
common/intl.cpp \
common/image.cpp \
common/imagjpeg.cpp \
common/imagpng.cpp \
common/ipcbase.cpp \
common/layout.cpp \
common/list.cpp \

View File

@ -30,6 +30,8 @@ LIB_CPP_SRC=\
../common/helpbase.cpp \
../common/intl.cpp \
../common/image.cpp \
../common/imagjpeg.cpp \
../common/imagpng.cpp \
../common/ipcbase.cpp \
../common/layout.cpp \
../common/list.cpp \

View File

@ -30,6 +30,8 @@ LIB_CPP_SRC=\
../common/helpbase.cpp \
../common/intl.cpp \
../common/image.cpp \
../common/imagjpeg.cpp \
../common/imagpng.cpp \
../common/ipcbase.cpp \
../common/layout.cpp \
../common/list.cpp \

View File

@ -121,7 +121,7 @@ WXSRC=$(WXDIR)/src/msw
WXINC=$(WXDIR)/include
WXBASESRC=$(WXDIR)/src/common
WXLIB=$(WXDIR)/lib/$(LIBPREFIX)wx.$(LIBSUFF)
INC = -I$(WXINC) -I$(WXDIR)/src/png -I$(WXDIR)/include/wx/msw/gnuwin32 -I$(WXDIR)/src/zlib $(EXTRAINC) $(COMPPATHS)
INC = -I$(WXINC) -I$(WXDIR)/src/png -I$(WXDIR)/src/jpeg -I$(WXDIR)/include/wx/msw/gnuwin32 -I$(WXDIR)/src/zlib $(EXTRAINC) $(COMPPATHS)
RCLFLAGS=-cpp "cpp -lang-c++ -DWIN32 -D_WIN32 -DRCL_INVOKED -I$(WXWIN)\include"
#LIBS = -lctl3d32 $(WXLIB) $(WINLIBS) $(COMPLIBS)

View File

@ -132,7 +132,7 @@ D=$(D)DLL
WXLIB=$(WXDIR)\lib\$(WXLIBNAME).lib
INC=-I$(WXINC) -I$(WXDIR)/src/png -I$(WXDIR)/src/zlib -I$(WXDIR)/src/jpeg $(EXTRAINC)
LIBS = $(EXTRALIBS) $(WXLIB) $(WINLIBS) $(WXDIR)\lib\winpng.lib $(WXDIR)\lib\zlib.lib $(WXDIR)\lib\xpm.lib
LIBS = $(EXTRALIBS) $(WXLIB) $(WINLIBS) $(WXDIR)\lib\winpng.lib $(WXDIR)\lib\zlib.lib $(WXDIR)\lib\jpeg.lib $(WXDIR)\lib\xpm.lib
MAKEPRECOMP=/YcWX/WXPREC.H
OPTIONS=
@ -188,3 +188,13 @@ LINKFLAGS=$(LINK_DEBUG_FLAGS) -machine:i386 -subsystem:windows,$(APPVER) -dll #
DUMMYOBJ=$(WXDIR)\src\msw\$D\$(DUMMY).obj
.c.obj:
cl @<<
$(CPPFLAGS2) /Fo$@ /c $<
<<
.cpp.obj:
cl @<<
$(CPPFLAGS2) /Fo$@ /c $<
<<

View File

@ -25,6 +25,8 @@ LIB_CPP_SRC=\
common/hash.cpp \
common/helpbase.cpp \
common/image.cpp \
common/imagjpeg.cpp \
common/imagpng.cpp \
common/intl.cpp \
common/ipcbase.cpp \
common/layout.cpp \

View File

@ -31,6 +31,8 @@ LIB_CPP_SRC=\
../common/intl.cpp \
../common/ipcbase.cpp \
../common/image.cpp \
../common/imagjpeg.cpp \
../common/imagpng.cpp \
../common/layout.cpp \
../common/list.cpp \
../common/log.cpp \

View File

@ -110,6 +110,8 @@ COMMONOBJS = \
$(MSWDIR)\gdicmn.obj \
$(MSWDIR)\helpbase.obj \
$(MSWDIR)\image.obj \
$(MSWDIR)\imagpng.obj \
$(MSWDIR)\imagjpeg.obj \
$(MSWDIR)\intl.obj \
$(MSWDIR)\ipcbase.obj \
$(MSWDIR)\log.obj \
@ -504,6 +506,12 @@ $(MSWDIR)\gdicmn.obj: $(COMMDIR)\gdicmn.$(SRCSUFF)
$(MSWDIR)\image.obj: $(COMMDIR)\image.$(SRCSUFF)
$(MSWDIR)\imagpng.obj: $(COMMDIR)\imagpng.$(SRCSUFF)
$(MSWDIR)\imagjpeg.obj: $(COMMDIR)\imagjpeg.$(SRCSUFF)
$(MSWDIR)\image.obj: $(COMMDIR)\image.$(SRCSUFF)
$(MSWDIR)\intl.obj: $(COMMDIR)\intl.$(SRCSUFF)
$(MSWDIR)\ipcbase.obj: $(COMMDIR)\ipcbase.$(SRCSUFF)
@ -699,7 +707,7 @@ $(CFG): makefile.b32
-WE
-tWM
-I$(WXINC);$(BCCDIR)\include;$(WXDIR)/src/png;$(WXDIR)/src/zlib;$(WXDIR)/src/xpm
-I$(WXINC);$(BCCDIR)\include;$(WXDIR)/src/png;$(WXDIR)/src/jpeg;$(WXDIR)/src/zlib;$(WXDIR)/src/xpm
-I$(WXDIR)\include\wx\msw\gnuwin32
-L$(BCCDIR)\lib

View File

@ -89,6 +89,8 @@ COMMONOBJS = \
$(COMMDIR)/gdicmn.$(OBJSUFF) \
$(COMMDIR)/helpbase.$(OBJSUFF) \
$(COMMDIR)/image.$(OBJSUFF) \
$(COMMDIR)/imagpng.$(OBJSUFF) \
$(COMMDIR)/imagjpeg.$(OBJSUFF) \
$(COMMDIR)/intl.$(OBJSUFF) \
$(COMMDIR)/ipcbase.$(OBJSUFF) \
$(COMMDIR)/layout.$(OBJSUFF) \

View File

@ -91,6 +91,8 @@ COMMONOBJS = \
$(COMMDIR)/gdicmn.$(OBJSUFF) \
$(COMMDIR)/helpbase.$(OBJSUFF) \
$(COMMDIR)/image.$(OBJSUFF) \
$(COMMDIR)/imagpng.$(OBJSUFF) \
$(COMMDIR)/imagjpeg.$(OBJSUFF) \
$(COMMDIR)/intl.$(OBJSUFF) \
$(COMMDIR)/ipcbase.$(OBJSUFF) \
$(COMMDIR)/layout.$(OBJSUFF) \

View File

@ -112,6 +112,8 @@ COMMONOBJS = \
..\common\$D\ftp.obj \
..\common\$D\gdicmn.obj \
..\common\$D\image.obj \
..\common\$D\imagjpeg.obj \
..\common\$D\imagpng.obj \
..\common\$D\intl.obj \
..\common\$D\ipcbase.obj \
..\common\$D\helpbase.obj \

View File

@ -62,6 +62,8 @@ COMMONOBJS = cmndata.obj &
framecmn.obj &
gdicmn.obj &
image.obj &
imagpng.obj &
imagjpeg.obj &
intl.obj &
ipcbase.obj &
helpbase.obj &
@ -558,6 +560,12 @@ gdicmn.obj: $(COMMDIR)\gdicmn.cpp
image.obj: $(COMMDIR)\image.cpp
*$(CCC) $(CPPFLAGS) $(IFLAGS) $<
imagpng.obj: $(COMMDIR)\imagpng.cpp
*$(CCC) $(CPPFLAGS) $(IFLAGS) $<
imagjpeg.obj: $(COMMDIR)\imagjpeg.cpp
*$(CCC) $(CPPFLAGS) $(IFLAGS) $<
intl.obj: $(COMMDIR)\intl.cpp
*$(CCC) $(CPPFLAGS) $(IFLAGS) $<