X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/c6eba8f8b204b0d722db0007e3e0d5e2d2d20085..c9057ae1c565cc4ae6c2cf84dd19d65a64ee2b7b:/src/msw/pnghand.cpp?ds=sidebyside diff --git a/src/msw/pnghand.cpp b/src/msw/pnghand.cpp index ec8bbd781d..9e1d34945c 100644 --- a/src/msw/pnghand.cpp +++ b/src/msw/pnghand.cpp @@ -32,10 +32,11 @@ #endif #include -#include -#include -#include -#include +#include "wx/palette.h" +#include "wx/bitmap.h" +#include "wx/utils.h" +#include "wx/msw/pngread.h" +#include "wx/msw/dibutils.h" extern "C" { #include "../png/png.h" @@ -74,7 +75,7 @@ ima_png_error(png_struct *png_ptr, char *message) // static wxGifReaderIter* iter; wxPalette *wxCopyPalette(const wxPalette *cmap); -wxPNGReader::wxPNGReader(void) +wxPNGReader::wxPNGReader() { filetype = 0; RawImage = NULL; // Image data @@ -93,7 +94,7 @@ wxPNGReader::wxPNGReader(void) imageOK = FALSE; } -wxPNGReader::wxPNGReader ( char* ImageFileName ) +wxPNGReader::wxPNGReader ( wxChar* ImageFileName ) { imageOK = FALSE; filetype = 0; @@ -130,9 +131,9 @@ wxPNGReader::Create(int width, int height, int depth, int colortype) } RawImage = 0; Palette = 0; - lpbi = DibCreate(Depth, Width, Height); + lpbi = wxDibCreate(Depth, Width, Height); if (lpbi) { - RawImage = (ImagePointerType)DibPtr(lpbi); + RawImage = (ImagePointerType)wxDibPtr(lpbi); EfeWidth = (long)(((long)Width*Depth + 31) / 32) * 4; imageOK = TRUE; } @@ -216,7 +217,7 @@ bool wxPNGReader::SetPalette(wxPalette* colourmap) return FALSE; ColorType |= (COLORTYPE_PALETTE | COLORTYPE_COLOR); Palette = colourmap; - return (DibSetUsage(lpbi, (HPALETTE) Palette->GetHPALETTE(), WXIMA_COLORS ) != 0); + return (wxDibSetUsage(lpbi, (HPALETTE) Palette->GetHPALETTE(), WXIMA_COLORS ) != 0); } bool @@ -230,7 +231,7 @@ wxPNGReader::SetPalette(int n, byte *r, byte *g, byte *b) if (!b) b = g; Palette->Create(n, r, g, b); ColorType |= (COLORTYPE_PALETTE | COLORTYPE_COLOR); - return (DibSetUsage(lpbi, (HPALETTE) Palette->GetHPALETTE(), WXIMA_COLORS ) != 0); + return (wxDibSetUsage(lpbi, (HPALETTE) Palette->GetHPALETTE(), WXIMA_COLORS ) != 0); } bool @@ -256,7 +257,7 @@ wxPNGReader::SetPalette(int n, rgb_color_struct *rgb_struct) Palette->Create(n, r, g, b); ColorType |= (COLORTYPE_PALETTE | COLORTYPE_COLOR); - return (DibSetUsage(lpbi, (HPALETTE) Palette->GetHPALETTE(), WXIMA_COLORS ) != 0); + return (wxDibSetUsage(lpbi, (HPALETTE) Palette->GetHPALETTE(), WXIMA_COLORS ) != 0); } void wxPNGReader::NullData() @@ -265,7 +266,7 @@ void wxPNGReader::NullData() Palette = NULL; } -wxBitmap* wxPNGReader::GetBitmap(void) +wxBitmap* wxPNGReader::GetBitmap() { wxBitmap *bitmap = new wxBitmap; if ( InstantiateBitmap(bitmap) ) @@ -354,7 +355,7 @@ wxPalette *wxCopyPalette(const wxPalette *cmap) return newCmap; } -wxMask *wxPNGReader::CreateMask(void) +wxMask *wxPNGReader::CreateMask() { HBITMAP hBitmap = ::CreateBitmap(GetWidth(), GetHeight(), 1, 1, NULL); @@ -383,152 +384,154 @@ wxMask *wxPNGReader::CreateMask(void) return mask; } -bool wxPNGReader::ReadFile(char * ImageFileName) +bool wxPNGReader::ReadFile(wxChar * ImageFileName) { - int number_passes; + if (ImageFileName) + wxStrcpy(filename, ImageFileName); - if (ImageFileName) - strcpy(filename, ImageFileName); - - FILE *fp; - png_struct *png_ptr; - png_info *info_ptr; - wxPNGReaderIter iter(this); + /* open the file */ + FILE *fp = fopen(wxConvFile.cWX2MB(filename), "rb"); + if (!fp) + return FALSE; - /* open the file */ - fp = fopen(filename, "rb"); - if (!fp) - return FALSE; + /* allocate the necessary structures */ + png_struct *png_ptr = new (png_struct); + if (!png_ptr) + { + fclose(fp); + return FALSE; + } - /* allocate the necessary structures */ - png_ptr = new (png_struct); - if (!png_ptr) - { - fclose(fp); - return FALSE; - } + png_info *info_ptr = new (png_info); + if (!info_ptr) + { + fclose(fp); + delete(png_ptr); + return FALSE; + } - info_ptr = new (png_info); - if (!info_ptr) - { - fclose(fp); - delete(png_ptr); - return FALSE; - } - /* set error handling */ - if (setjmp(png_ptr->jmpbuf)) - { - png_read_destroy(png_ptr, info_ptr, (png_info *)0); - fclose(fp); - delete(png_ptr); - delete(info_ptr); + /* set error handling */ + if (setjmp(png_ptr->jmpbuf)) + { + png_read_destroy(png_ptr, info_ptr, (png_info *)0); + fclose(fp); + delete(png_ptr); + delete(info_ptr); - /* If we get here, we had a problem reading the file */ - return FALSE; - } - //png_set_error(ima_png_error, NULL); + /* If we get here, we had a problem reading the file */ + return FALSE; + } + //png_set_error(ima_png_error, NULL); - /* initialize the structures, info first for error handling */ - png_info_init(info_ptr); - png_read_init(png_ptr); + /* initialize the structures, info first for error handling */ + png_info_init(info_ptr); + png_read_init(png_ptr); - /* set up the input control */ - png_init_io(png_ptr, fp); + /* set up the input control */ + png_init_io(png_ptr, fp); - /* read the file information */ - png_read_info(png_ptr, info_ptr); + /* read the file information */ + png_read_info(png_ptr, info_ptr); - /* allocate the memory to hold the image using the fields - of png_info. */ - png_color_16 my_background={ 0, 31, 127, 255, 0 }; + /* allocate the memory to hold the image using the fields + of png_info. */ + png_color_16 my_background={ 0, 31, 127, 255, 0 }; - if (info_ptr->valid & PNG_INFO_bKGD) + if (info_ptr->valid & PNG_INFO_bKGD) { - png_set_background(png_ptr, &(info_ptr->background), - PNG_BACKGROUND_GAMMA_FILE, 1, 1.0); + png_set_background(png_ptr, &(info_ptr->background), + PNG_BACKGROUND_GAMMA_FILE, 1, 1.0); if ( info_ptr->num_palette > 0 ) bgindex = info_ptr->background.index; } - else { - png_set_background(png_ptr, &my_background, - PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0); + else { + png_set_background(png_ptr, &my_background, + PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0); // Added by JACS: guesswork! if ( info_ptr->num_trans != 0 ) bgindex = info_ptr->num_trans - 1 ; - } + } - /* tell libpng to strip 16 bit depth files down to 8 bits */ - if (info_ptr->bit_depth == 16) - png_set_strip_16(png_ptr); + /* tell libpng to strip 16 bit depth files down to 8 bits */ + if (info_ptr->bit_depth == 16) + png_set_strip_16(png_ptr); - int pixel_depth=(info_ptr->pixel_depth<24) ? info_ptr->pixel_depth: 24; - Create(info_ptr->width, info_ptr->height, pixel_depth, - info_ptr->color_type); + int pixel_depth=(info_ptr->pixel_depth<24) ? info_ptr->pixel_depth: 24; + Create(info_ptr->width, info_ptr->height, pixel_depth, + info_ptr->color_type); - if (info_ptr->num_palette>0) + if (info_ptr->num_palette>0) { - SetPalette((int)info_ptr->num_palette, (rgb_color_struct*)info_ptr->palette); + SetPalette((int)info_ptr->num_palette, (rgb_color_struct*)info_ptr->palette); } - int row_stride = info_ptr->width * ((pixel_depth+7)>>3); - // printf("P = %d D = %d RS= %d ", info_ptr->num_palette, info_ptr->pixel_depth,row_stride); -// printf("CT = %d TRS = %d BD= %d ", info_ptr->color_type, info_ptr->valid & PNG_INFO_tRNS,info_ptr->bit_depth); + int row_stride = info_ptr->width * ((pixel_depth+7)>>3); + // printf("P = %d D = %d RS= %d ", info_ptr->num_palette, info_ptr->pixel_depth,row_stride); + // printf("CT = %d TRS = %d BD= %d ", info_ptr->color_type, info_ptr->valid & PNG_INFO_tRNS,info_ptr->bit_depth); - byte *row_pointers = new byte[row_stride]; + byte *row_pointers = new byte[row_stride]; - /* turn on interlace handling */ - if (info_ptr->interlace_type) - number_passes = png_set_interlace_handling(png_ptr); - else - number_passes = 1; -// printf("NP = %d ", number_passes); + /* turn on interlace handling */ + int number_passes; + if (info_ptr->interlace_type) + number_passes = png_set_interlace_handling(png_ptr); + else + number_passes = 1; + // printf("NP = %d ", number_passes); + + // don't use the object to prevent warnings from VC++ about "unportable + // interaction between setjmp and C++ object destruction" (this is a correct + // warning, of course!) + wxPNGReaderIter *iter = new wxPNGReaderIter(this); + for (int pass=0; pass< number_passes; pass++) + { + iter->upset(); + int y=0; + do { + //(unsigned char *)iter.GetRow(); + if (info_ptr->interlace_type) { + if (pass>0) + iter->GetRow(row_pointers, row_stride); + png_read_row(png_ptr, row_pointers, NULL); + } + else + png_read_row(png_ptr, row_pointers, NULL); - for (int pass=0; pass< number_passes; pass++) { - iter.upset(); - int y=0; - do { -// (unsigned char *)iter.GetRow(); - if (info_ptr->interlace_type) { - if (pass>0) - iter.GetRow(row_pointers, row_stride); - png_read_row(png_ptr, row_pointers, NULL); + iter->SetRow(row_pointers, row_stride); + y++; + } while(iter->PrevRow()); + // printf("Y=%d ",y); } - else - png_read_row(png_ptr, row_pointers, NULL); - iter.SetRow(row_pointers, row_stride); - y++; - } while(iter.PrevRow()); -// printf("Y=%d ",y); - } - delete[] row_pointers; + delete iter; + delete[] row_pointers; - /* read the rest of the file, getting any additional chunks - in info_ptr */ - png_read_end(png_ptr, info_ptr); + /* read the rest of the file, getting any additional chunks + in info_ptr */ + png_read_end(png_ptr, info_ptr); - /* clean up after the read, and free any memory allocated */ - png_read_destroy(png_ptr, info_ptr, (png_info *)0); + /* clean up after the read, and free any memory allocated */ + png_read_destroy(png_ptr, info_ptr, (png_info *)0); - /* free the structures */ - delete(png_ptr); - delete(info_ptr); + /* free the structures */ + delete(png_ptr); + delete(info_ptr); - /* close the file */ - fclose(fp); + /* close the file */ + fclose(fp); - /* that's it */ - return TRUE; + /* that's it */ + return TRUE; } /* write a png file */ -bool wxPNGReader::SaveFile(char * ImageFileName) +bool wxPNGReader::SaveFile(wxChar * ImageFileName) { if (ImageFileName) - strcpy(filename, ImageFileName); + wxStrcpy(filename, ImageFileName); wxPNGReaderIter iter(this); FILE *fp; @@ -536,7 +539,7 @@ bool wxPNGReader::SaveFile(char * ImageFileName) png_info *info_ptr; /* open the file */ - fp = fopen(filename, "wb"); + fp = fopen(wxConvFile.cWX2MB(filename), "wb"); if (!fp) return FALSE; @@ -693,14 +696,14 @@ static void DecToHex(int dec, char *buf) } -bool wxPNGReader::SaveXPM(char *filename, char *name) +bool wxPNGReader::SaveXPM(wxChar *filename, wxChar *name) { - char nameStr[256]; + wxChar nameStr[256]; if ( name ) - strcpy(nameStr, name); + wxStrcpy(nameStr, name); else { - strcpy(nameStr, filename); + wxStrcpy(nameStr, filename); wxStripExtension(nameStr); } @@ -713,7 +716,7 @@ bool wxPNGReader::SaveXPM(char *filename, char *name) if ( !GetPalette() ) return FALSE; - ofstream str(filename); + ofstream str(wxConvFile.cWX2MB(filename)); if ( str.bad() ) return FALSE; @@ -770,7 +773,7 @@ bool wxPNGFileHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long fla int desiredWidth, int desiredHeight) { wxPNGReader reader; - if (reader.ReadFile((char*) (const char*) name)) + if (reader.ReadFile(WXSTRINGCAST name)) { return reader.InstantiateBitmap(bitmap); }