1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implements a PNG reader class + handler
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "pngread.h"
14 #pragma implementation "pnghand.h"
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
35 #include <wx/palette.h>
36 #include <wx/bitmap.h>
37 #include <wx/mac/pnghand.h>
38 #include <wx/mac/pngread.h>
44 extern "C" void png_read_init
PNGARG((png_structp png_ptr
));
45 extern "C" void png_write_init
PNGARG((png_structp png_ptr
));
47 extern CTabHandle
wxMacCreateColorTable( int numColors
) ;
48 extern void wxMacDestroyColorTable( CTabHandle colors
) ;
49 extern void wxMacSetColorTableEntry( CTabHandle newColors
, int index
, int red
, int green
, int blue
) ;
50 extern GWorldPtr
wxMacCreateGWorld( int height
, int width
, int depth
) ;
51 extern void wxMacDestroyGWorld( GWorldPtr gw
) ;
54 ima_png_error(png_struct
*png_ptr
, char *message
)
56 wxMessageBox(message
, "PNG error");
57 longjmp(png_ptr
->jmpbuf
, 1);
61 // static wxGifReaderIter* iter;
62 wxPalette
*wxCopyPalette(const wxPalette
*cmap
);
64 wxPNGReader::wxPNGReader(void)
67 RawImage
= NULL
; // Image data
69 Width
= 0; Height
= 0; // Dimensions
70 Depth
= 0; // (bits x pixel)
71 ColorType
= 0; // Bit 1 = Palette used
75 EfeWidth
= 0; // Efective Width
83 wxPNGReader::wxPNGReader ( char* ImageFileName
)
87 RawImage
= NULL
; // Image data
89 Width
= 0; Height
= 0; // Dimensions
90 Depth
= 0; // (bits x pixel)
91 ColorType
= 0; // Bit 1 = Palette used
95 EfeWidth
= 0; // Efective Width
101 imageOK
= ReadFile (ImageFileName
);
105 wxPNGReader::Create(int width
, int height
, int depth
, int colortype
)
107 Width
= width
; Height
= height
; Depth
= depth
;
108 ColorType
= (colortype
>=0) ? colortype
: ((Depth
>8) ? COLORTYPE_COLOR
: 0);
116 wxMacDestroyGWorld( lpbi
) ;
118 if (lpbi
= wxMacCreateGWorld( Width
, Height
, Depth
) )
120 EfeWidth
= (long)(((long)Width
*Depth
+ 31) / 32) * 4;
121 int bitwidth
= width
;
122 if ( EfeWidth
> bitwidth
)
123 bitwidth
= EfeWidth
;
125 RawImage
= (byte
*) new char[ ( bitwidth
* Height
* ((Depth
+7)>>3) ) ];
130 wxPNGReader::~wxPNGReader ( )
134 wxMacDestroyGWorld( lpbi
) ;
140 int wxPNGReader::GetIndex(int x
, int y
)
142 if (!Inside(x
, y
) || (Depth
>8)) return -1;
144 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
145 int index
= (int)(*ImagePointer
);
149 bool wxPNGReader::GetRGB(int x
, int y
, byte
* r
, byte
* g
, byte
* b
)
151 if (!Inside(x
, y
)) return FALSE
;
154 return Palette
->GetRGB(GetIndex(x
, y
), r
, g
, b
);
155 /* PALETTEENTRY entry;
156 ::GetPaletteEntries((HPALETTE) Palette->GetHPALETTE(), GetIndex(x, y), 1, &entry);
159 *b = entry.peBlue; */
161 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
162 *b
= ImagePointer
[0];
163 *g
= ImagePointer
[1];
164 *r
= ImagePointer
[2];
170 bool wxPNGReader::SetIndex(int x
, int y
, int index
)
172 if (!Inside(x
, y
) || (Depth
>8)) return FALSE
;
174 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
175 *ImagePointer
= index
;
180 bool wxPNGReader::SetRGB(int x
, int y
, byte r
, byte g
, byte b
)
182 if (!Inside(x
, y
)) return FALSE
;
184 if (ColorType
& COLORTYPE_PALETTE
)
186 if (!Palette
) return FALSE
;
187 SetIndex(x
, y
, Palette
->GetPixel(r
, g
, b
));
190 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
199 bool wxPNGReader::SetPalette(wxPalette
* colourmap
)
204 ColorType
|= (COLORTYPE_PALETTE
| COLORTYPE_COLOR
);
205 Palette
= new wxPalette( *colourmap
);
207 // return (DibSetUsage(lpbi, (HPALETTE) Palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
211 wxPNGReader::SetPalette(int n
, byte
*r
, byte
*g
, byte
*b
)
214 Palette
= new wxPalette();
220 Palette
->Create(n
, r
, g
, b
);
221 ColorType
|= (COLORTYPE_PALETTE
| COLORTYPE_COLOR
);
223 // return (DibSetUsage(lpbi, (HPALETTE) Palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
227 wxPNGReader::SetPalette(int n
, rgb_color_struct
*rgb_struct
)
230 Palette
= new wxPalette();
234 byte r
[256], g
[256], b
[256];
236 for(int i
=0; i
<n
; i
++)
238 r
[i
] = rgb_struct
[i
].red
;
239 g
[i
] = rgb_struct
[i
].green
;
240 b
[i
] = rgb_struct
[i
].blue
;
242 // Added by JACS copying from Andrew Davison's additions
243 // to GIF-reading code
244 // Make transparency colour black...
246 r
[bgindex
] = g
[bgindex
] = b
[bgindex
] = 0;
248 Palette
->Create(n
, r
, g
, b
);
249 ColorType
|= (COLORTYPE_PALETTE
| COLORTYPE_COLOR
);
251 // return (DibSetUsage(lpbi, (HPALETTE) Palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
254 void wxPNGReader::NullData()
257 wxMacDestroyGWorld( lpbi
) ;
264 wxBitmap
* wxPNGReader::GetBitmap(void)
266 wxBitmap
*bitmap
= new wxBitmap
;
267 if ( InstantiateBitmap(bitmap
) )
276 bool wxPNGReader::InstantiateBitmap(wxBitmap
*bitmap
)
280 bitmap
->SetHBITMAP((WXHBITMAP
) lpbi
);
281 bitmap
->SetWidth(GetWidth());
282 bitmap
->SetHeight(GetHeight());
283 bitmap
->SetDepth(GetDepth());
284 if ( GetDepth() > 1 && Palette
)
285 bitmap
->SetPalette(*Palette
);
289 // Make a mask if appropriate
293 wxMask *mask = CreateMask();
294 bitmap->SetMask(mask);
297 lpbi
= NULL
; // bitmap has taken over ownership
305 HDC dc = ::CreateCompatibleDC(NULL);
309 // tmpBitmap is a dummy, to satisfy ::CreateCompatibleDC (it
310 // is a memory dc that must have a bitmap selected into it)
311 HDC dc2 = GetDC(NULL);
312 HBITMAP tmpBitmap = ::CreateCompatibleBitmap(dc2, GetWidth(), GetHeight());
313 ReleaseDC(NULL, dc2);
314 HBITMAP oldBitmap = (HBITMAP) ::SelectObject(dc, tmpBitmap);
318 HPALETTE oldPal = ::SelectPalette(dc, (HPALETTE) Palette->GetHPALETTE(), FALSE);
319 ::RealizePalette(dc);
322 HBITMAP hBitmap = ::CreateDIBitmap(dc, lpbi,
323 CBM_INIT, RawImage, (LPBITMAPINFO) lpbi, DIB_PAL_COLORS);
325 ::SelectPalette(dc, NULL, TRUE);
326 ::SelectObject(dc, oldBitmap);
327 ::DeleteObject(tmpBitmap);
332 bitmap->SetHBITMAP((WXHBITMAP) hBitmap);
333 bitmap->SetWidth(GetWidth());
334 bitmap->SetHeight(GetHeight());
335 bitmap->SetDepth(GetDepth());
336 if ( GetDepth() > 1 && Palette )
337 bitmap->SetPalette(*Palette);
341 // Make a mask if appropriate
344 wxMask *mask = CreateMask();
345 bitmap->SetMask(mask);
362 wxPalette
*wxCopyPalette(const wxPalette
*cmap
)
364 wxPalette
*newCmap
= new wxPalette( *cmap
) ;
368 wxMask
*wxPNGReader::CreateMask(void)
371 HBITMAP hBitmap = ::CreateBitmap(GetWidth(), GetHeight(), 1, 1, NULL);
373 HDC dc = ::CreateCompatibleDC(NULL);
374 HBITMAP oldBitmap = (HBITMAP) ::SelectObject(dc, hBitmap);
376 int bgIndex = GetBGIndex();
380 for (x=0; x<GetWidth(); x++)
382 for (y=0; y<GetHeight(); y++)
384 int index = GetIndex(x, y);
385 if ( index == bgIndex )
386 ::SetPixel(dc, x, GetHeight() - y - 1, RGB(0, 0, 0));
388 ::SetPixel(dc, x, GetHeight() - y - 1, RGB(255, 255, 255));
392 ::SelectObject(dc, oldBitmap);
393 wxMask *mask = new wxMask;
394 mask->SetMaskBitmap((WXHBITMAP) hBitmap);
400 bool wxPNGReader::ReadFile(char * ImageFileName
)
405 strcpy(filename
, ImageFileName
);
410 wxPNGReaderIter
iter(this);
413 fp
= fopen(wxUnix2MacFilename( ImageFileName
), "rb");
417 /* allocate the necessary structures */
418 png_ptr
= new (png_struct
);
425 info_ptr
= new (png_info
);
432 /* set error handling */
433 if (setjmp(png_ptr
->jmpbuf
))
435 png_read_destroy(png_ptr
, info_ptr
, (png_info
*)0);
440 /* If we get here, we had a problem reading the file */
443 //png_set_error(ima_png_error, NULL);
445 /* initialize the structures, info first for error handling */
446 png_info_init(info_ptr
);
447 png_read_init(png_ptr
);
449 /* set up the input control */
450 png_init_io(png_ptr
, fp
);
452 /* read the file information */
453 png_read_info(png_ptr
, info_ptr
);
455 /* allocate the memory to hold the image using the fields
457 png_color_16 my_background
={ 0, 31, 127, 255, 0 };
459 if (info_ptr
->valid
& PNG_INFO_bKGD
)
461 png_set_background(png_ptr
, &(info_ptr
->background
),
462 PNG_BACKGROUND_GAMMA_FILE
, 1, 1.0);
463 if ( info_ptr
->num_palette
> 0 )
464 bgindex
= info_ptr
->background
.index
;
467 png_set_background(png_ptr
, &my_background
,
468 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
470 // Added by JACS: guesswork!
471 if ( info_ptr
->num_trans
!= 0 )
472 bgindex
= info_ptr
->num_trans
- 1 ;
475 /* tell libpng to strip 16 bit depth files down to 8 bits */
476 if (info_ptr
->bit_depth
== 16)
477 png_set_strip_16(png_ptr
);
479 int pixel_depth
=(info_ptr
->pixel_depth
<24) ? info_ptr
->pixel_depth
: 24;
480 Create(info_ptr
->width
, info_ptr
->height
, pixel_depth
,
481 info_ptr
->color_type
);
483 if (info_ptr
->num_palette
>0)
485 SetPalette((int)info_ptr
->num_palette
, (rgb_color_struct
*)info_ptr
->palette
);
488 int row_stride
= info_ptr
->width
* ((pixel_depth
+7)>>3);
489 // printf("P = %d D = %d RS= %d ", info_ptr->num_palette, info_ptr->pixel_depth,row_stride);
490 // printf("CT = %d TRS = %d BD= %d ", info_ptr->color_type, info_ptr->valid & PNG_INFO_tRNS,info_ptr->bit_depth);
492 byte
*row_pointers
= new byte
[row_stride
];
494 /* turn on interlace handling */
495 if (info_ptr
->interlace_type
)
496 number_passes
= png_set_interlace_handling(png_ptr
);
499 // printf("NP = %d ", number_passes);
501 for (int pass
=0; pass
< number_passes
; pass
++)
506 GDHandle origDevice
;
508 GetGWorld( &origPort
, &origDevice
) ;
510 SetGWorld( lpbi
, NULL
) ;
513 // (unsigned char *)iter.GetRow();
514 if (info_ptr
->interlace_type
)
517 iter
.GetRow(row_pointers
, row_stride
);
518 png_read_row(png_ptr
, row_pointers
, NULL
);
521 png_read_row(png_ptr
, row_pointers
, NULL
);
523 if ( info_ptr
->palette
)
525 if ( pixel_depth
== 8 )
527 for ( int i
= 0 ; i
< info_ptr
->width
; ++i
)
529 png_color_struct
* color
;
532 int index
= row_pointers
[i
] ;
533 color
= &info_ptr
->palette
[index
] ;
534 col
.red
= (((int)color
->red
) << 8) | ((int)color
->red
) ;
535 col
.green
= (((int)color
->green
) << 8) | ((int)color
->green
) ;
536 col
.blue
= (((int)color
->blue
) << 8) | ((int)color
->blue
) ;
537 SetCPixel( i
, y
, &col
);
540 png_color_struct* color ;
542 unsigned char* p = &row_pointers[0] ;
546 color = &info_ptr->palette[index] ;
547 col.red = (color->red << 8) | color->red ;
548 col.green = (color->green << 8) | color->green ;
549 col.blue = (color->blue << 8) | color->blue ;
550 RGBForeColor( &col ) ;
551 col.red = col.green = col.blue = 0xFFFF ;
552 RGBBackColor( &col ) ;
553 for ( int i = 0 ; i < info_ptr->width ; ++i , ++p)
559 color = &info_ptr->palette[index] ;
560 col.red = (((int)color->red) << 8) | ((int)color->red) ;
561 col.green = (((int)color->green) << 8) | ((int)color->green) ;
562 col.blue = (((int)color->blue) << 8) | ((int)color->blue) ;
563 RGBForeColor( &col ) ;
566 LineTo( info_ptr->width , y ) ;
571 for ( int i
= 0 ; i
< info_ptr
->width
; ++i
)
573 png_color_struct
* color
;
576 int byte
= ( i
* pixel_depth
) / 8 ;
577 int offset
= ( 8 - pixel_depth
) - ( i
* pixel_depth
) % 8 ;
579 int index
= ( row_pointers
[byte
] >> offset
) & ( 0xFF >> ( 8 - pixel_depth
) );
580 color
= &info_ptr
->palette
[index
] ;
581 col
.red
= (((int)color
->red
) << 8) | ((int)color
->red
) ;
582 col
.green
= (((int)color
->green
) << 8) | ((int)color
->green
) ;
583 col
.blue
= (((int)color
->blue
) << 8) | ((int)color
->blue
) ;
584 SetCPixel( i
, y
, &col
);
590 for ( int i
= 0 ; i
< info_ptr
->width
; ++i
)
592 png_color_struct
* color
;
594 color
=(png_color_struct
*) (&row_pointers
[i
*3]) ;
595 col
.red
= (((int)color
->red
) << 8) | ((int)color
->red
) ;
596 col
.green
= (((int)color
->green
) << 8) | ((int)color
->green
) ;
597 col
.blue
= (((int)color
->blue
) << 8) | ((int)color
->blue
) ;
598 SetCPixel( i
, y
, &col
);
602 iter
.SetRow(row_pointers
, row_stride
);
605 while(iter
.PrevRow());
606 SetGWorld( origPort
, origDevice
) ;
608 // printf("Y=%d ",y);
610 delete[] row_pointers
;
612 /* read the rest of the file, getting any additional chunks
614 png_read_end(png_ptr
, info_ptr
);
616 /* clean up after the read, and free any memory allocated */
617 png_read_destroy(png_ptr
, info_ptr
, (png_info
*)0);
619 /* free the structures */
631 /* write a png file */
633 bool wxPNGReader::SaveFile(char * ImageFileName
)
636 strcpy(filename
, ImageFileName
);
638 wxPNGReaderIter
iter(this);
644 fp
= fopen(filename
, "wb");
648 /* allocate the necessary structures */
649 png_ptr
= new (png_struct
);
656 info_ptr
= new (png_info
);
664 /* set error handling */
665 if (setjmp(png_ptr
->jmpbuf
))
667 png_write_destroy(png_ptr
);
672 /* If we get here, we had a problem reading the file */
675 //png_set_error(ima_png_error, NULL);
677 // printf("writig pg %s ", filename);
678 /* initialize the structures */
679 png_info_init(info_ptr
);
680 png_write_init(png_ptr
);
682 int row_stride
= GetWidth() * ((GetDepth()+7)>>3);
683 /* set up the output control */
684 png_init_io(png_ptr
, fp
);
686 /* set the file information here */
687 info_ptr
->width
= GetWidth();
688 info_ptr
->height
= GetHeight();
689 info_ptr
->pixel_depth
= GetDepth();
690 info_ptr
->channels
= (GetDepth()>8) ? 3: 1;
691 info_ptr
->bit_depth
= GetDepth()/info_ptr
->channels
;
692 info_ptr
->color_type
= GetColorType();
693 info_ptr
->compression_type
= info_ptr
->filter_type
= info_ptr
->interlace_type
=0;
695 info_ptr
->rowbytes
= row_stride
;
698 // printf("P = %d D = %d RS= %d GD= %d CH= %d ", info_ptr->pixel_depth, info_ptr->bit_depth, row_stride, GetDepth(), info_ptr->channels);
699 /* set the palette if there is one */
700 if ((GetColorType() & COLORTYPE_PALETTE
) && GetPalette())
702 // printf("writing paleta[%d %d %x]",GetColorType() ,COLORTYPE_PALETTE, GetPalette());
703 info_ptr
->valid
|= PNG_INFO_PLTE
;
704 info_ptr
->palette
= new png_color
[256];
705 info_ptr
->num_palette
= 256;
706 for (int i
=0; i
<256; i
++)
707 GetPalette()->GetRGB(i
, &info_ptr
->palette
[i
].red
, &info_ptr
->palette
[i
].green
, &info_ptr
->palette
[i
].blue
);
709 // printf("Paleta [%d %d %x]",GetColorType() ,COLORTYPE_PALETTE, GetPalette());
712 /* optional significant bit chunk */
713 // info_ptr->valid |= PNG_INFO_sBIT;
714 // info_ptr->sig_bit = true_bit_depth;
716 /* optional gamma chunk */
717 // info_ptr->valid |= PNG_INFO_gAMA;
718 // info_ptr->gamma = gamma;
720 /* other optional chunks */
722 /* write the file information */
723 png_write_info(png_ptr
, info_ptr
);
725 /* set up the transformations you want. Note that these are
726 all optional. Only call them if you want them */
728 /* shift the pixels up to a legal bit depth and fill in
729 as appropriate to correctly scale the image */
730 // png_set_shift(png_ptr, &(info_ptr->sig_bit));
732 /* pack pixels into bytes */
733 // png_set_packing(png_ptr);
735 /* flip bgr pixels to rgb */
736 // png_set_bgr(png_ptr);
738 /* swap bytes of 16 bit files to most significant bit first */
739 // png_set_swap(png_ptr);
741 /* get rid of filler bytes, pack rgb into 3 bytes */
742 // png_set_rgbx(png_ptr);
744 /* If you are only writing one row at a time, this works */
746 byte
*row_pointers
= new byte
[row_stride
];
749 // (unsigned char *)iter.GetRow();
750 iter
.GetRow(row_pointers
, row_stride
);
751 png_write_row(png_ptr
, row_pointers
);
752 } while(iter
.PrevRow());
754 delete[] row_pointers
;
756 /* write the rest of the file */
757 png_write_end(png_ptr
, info_ptr
);
759 /* clean up after the write, and free any memory allocated */
760 png_write_destroy(png_ptr
);
762 /* if you malloced the palette, free it here */
763 if (info_ptr
->palette
)
764 delete[] (info_ptr
->palette
);
766 /* free the structures */
777 static int Power(int x
, int y
)
781 for ( i
= 0; i
< y
; i
++)
788 static char hexArray
[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
789 'C', 'D', 'E', 'F' };
791 static void DecToHex(int dec
, char *buf
)
793 int firstDigit
= (int)(dec
/16.0);
794 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
795 buf
[0] = hexArray
[firstDigit
];
796 buf
[1] = hexArray
[secondDigit
];
801 bool wxPNGReader::SaveXPM(char *filename
, char *name
)
805 strcpy(nameStr
, name
);
808 strcpy(nameStr
, filename
);
809 wxStripExtension(nameStr
);
812 if ( GetDepth() > 4 )
814 // Only a depth of 4 and below allowed
821 ofstream
str(filename
);
825 int noColours
= Power(2, GetDepth());
828 str
<< "/* XPM */\n";
829 str
<< "static char * " << nameStr
<< "_xpm[] = {\n";
830 str
<< "\"" << GetWidth() << " " << GetHeight() << " " << noColours
<< " 1\",\n";
833 int base
= 97 ; // start from 'a'
835 unsigned char red
, green
, blue
;
838 for ( i
= 0; i
< noColours
; i
++)
840 str
<< "\"" << (char)(base
+ i
) << " c #";
841 GetPalette()->GetRGB(i
, &red
, &green
, &blue
);
842 DecToHex(red
, hexBuf
);
844 DecToHex(green
, hexBuf
);
846 DecToHex(blue
, hexBuf
);
853 for ( y
= 0; y
< GetHeight(); y
++)
856 for ( x
= 0; x
< GetWidth(); x
++)
858 int index
= GetIndex(x
, y
);
859 str
<< (char)(base
+ index
) ;
871 IMPLEMENT_DYNAMIC_CLASS(wxPNGFileHandler
, wxBitmapHandler
)
873 bool wxPNGFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
874 int desiredWidth
, int desiredHeight
)
877 if (reader
.ReadFile((char*) (const char*) name
))
879 return reader
.InstantiateBitmap(bitmap
);
885 bool wxPNGFileHandler::SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*pal
)