From e9c4b1a2b5b926ced938130b2694b869403397cc Mon Sep 17 00:00:00 2001 From: Julian Smart Date: Tue, 6 Apr 1999 13:44:37 +0000 Subject: [PATCH] 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 --- samples/image/image.cpp | 8 + src/common/image.cpp | 495 ---------------------------------------- src/common/imagjpeg.cpp | 257 +++++++++++++++++++++ src/common/imagpng.cpp | 316 +++++++++++++++++++++++++ src/gtk.inc | 2 + src/gtk/makefile.unx | 2 + src/gtk1/makefile.unx | 2 + src/makeg95.env | 2 +- src/makevc.env | 12 +- src/motif.inc | 2 + src/motif/makefile.unx | 2 + src/msw/makefile.b32 | 10 +- src/msw/makefile.g95 | 2 + src/msw/makefile.twn | 2 + src/msw/makefile.vc | 2 + src/msw/makefile.wat | 8 + 16 files changed, 626 insertions(+), 498 deletions(-) create mode 100644 src/common/imagjpeg.cpp create mode 100644 src/common/imagpng.cpp diff --git a/samples/image/image.cpp b/samples/image/image.cpp index 7b17838bd5..5f215657ad 100644 --- a/samples/image/image.cpp +++ b/samples/image/image.cpp @@ -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 ); diff --git a/src/common/image.cpp b/src/common/image.cpp index 151ba3b73e..800e61fa59 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -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 -} -#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 //----------------------------------------------------------------------------- diff --git a/src/common/imagjpeg.cpp b/src/common/imagjpeg.cpp new file mode 100644 index 0000000000..f36b632c7f --- /dev/null +++ b/src/common/imagjpeg.cpp @@ -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 +} +#endif +#include "wx/filefn.h" +#include "wx/wfstream.h" +#include "wx/intl.h" +#include "wx/module.h" + +// For memcpy +#include + +#ifdef __SALFORDC__ +#ifdef FAR +#undef FAR +#endif +#endif + +#ifdef __WXMSW__ +#include +#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 + diff --git a/src/common/imagpng.cpp b/src/common/imagpng.cpp new file mode 100644 index 0000000000..e200c00b6d --- /dev/null +++ b/src/common/imagpng.cpp @@ -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 + +#ifdef __SALFORDC__ +#ifdef FAR +#undef FAR +#endif +#endif + +#ifdef __WXMSW__ +#include +#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 + diff --git a/src/gtk.inc b/src/gtk.inc index d609f6c4be..240068c9b5 100644 --- a/src/gtk.inc +++ b/src/gtk.inc @@ -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 \ diff --git a/src/gtk/makefile.unx b/src/gtk/makefile.unx index ae5fd90f3c..ad4654c9e7 100644 --- a/src/gtk/makefile.unx +++ b/src/gtk/makefile.unx @@ -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 \ diff --git a/src/gtk1/makefile.unx b/src/gtk1/makefile.unx index ae5fd90f3c..ad4654c9e7 100644 --- a/src/gtk1/makefile.unx +++ b/src/gtk1/makefile.unx @@ -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 \ diff --git a/src/makeg95.env b/src/makeg95.env index 0fb9dfacee..34d927865f 100644 --- a/src/makeg95.env +++ b/src/makeg95.env @@ -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) diff --git a/src/makevc.env b/src/makevc.env index 28a66ef729..856f75bd8c 100644 --- a/src/makevc.env +++ b/src/makevc.env @@ -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 $< +<< + diff --git a/src/motif.inc b/src/motif.inc index 9429866884..56b9f3e56f 100644 --- a/src/motif.inc +++ b/src/motif.inc @@ -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 \ diff --git a/src/motif/makefile.unx b/src/motif/makefile.unx index 84fb0d06bc..e8c8ab7532 100644 --- a/src/motif/makefile.unx +++ b/src/motif/makefile.unx @@ -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 \ diff --git a/src/msw/makefile.b32 b/src/msw/makefile.b32 index 76d1febd5c..7c0b8760c5 100644 --- a/src/msw/makefile.b32 +++ b/src/msw/makefile.b32 @@ -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 diff --git a/src/msw/makefile.g95 b/src/msw/makefile.g95 index 509a47543e..cb0f949c08 100644 --- a/src/msw/makefile.g95 +++ b/src/msw/makefile.g95 @@ -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) \ diff --git a/src/msw/makefile.twn b/src/msw/makefile.twn index 069d86af37..c9aed2a2cf 100644 --- a/src/msw/makefile.twn +++ b/src/msw/makefile.twn @@ -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) \ diff --git a/src/msw/makefile.vc b/src/msw/makefile.vc index d92ad76e67..1f2ccebc1b 100644 --- a/src/msw/makefile.vc +++ b/src/msw/makefile.vc @@ -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 \ diff --git a/src/msw/makefile.wat b/src/msw/makefile.wat index a3d1c73d82..436cd229ee 100644 --- a/src/msw/makefile.wat +++ b/src/msw/makefile.wat @@ -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) $<