1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implements a PNG reader class + handler
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
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"
39 #include "wx/msgdlg.h"
40 #include "wx/palette.h"
41 #include "wx/bitmap.h"
42 #include "wx/mac/pnghand.h"
43 #include "wx/mac/pngread.h"
44 #include "wx/mac/private.h"
50 extern "C" void png_read_init
PNGARG((png_structp png_ptr
));
51 extern "C" void png_write_init
PNGARG((png_structp png_ptr
));
53 extern CTabHandle
wxMacCreateColorTable( int numColors
) ;
54 extern void wxMacDestroyColorTable( CTabHandle colors
) ;
55 extern void wxMacSetColorTableEntry( CTabHandle newColors
, int index
, int red
, int green
, int blue
) ;
56 extern GWorldPtr
wxMacCreateGWorld( int width
, int height
, int depth
) ;
57 extern void wxMacDestroyGWorld( GWorldPtr gw
) ;
60 ima_png_error(png_struct
*png_ptr
, char *message
)
62 wxMessageBox(wxString::FromAscii(message
), wxT("PNG error"));
63 longjmp(png_ptr
->jmpbuf
, 1);
67 // static wxGifReaderIter* iter;
68 wxPalette
*wxCopyPalette(const wxPalette
*cmap
);
70 wxPNGReader::wxPNGReader(void)
73 RawImage
= NULL
; // Image data
75 Width
= 0; Height
= 0; // Dimensions
76 Depth
= 0; // (bits x pixel)
77 ColorType
= 0; // Bit 1 = Palette used
81 EfeWidth
= 0; // Efective Width
89 wxPNGReader::wxPNGReader ( char* ImageFileName
)
93 RawImage
= NULL
; // Image data
95 Width
= 0; Height
= 0; // Dimensions
96 Depth
= 0; // (bits x pixel)
97 ColorType
= 0; // Bit 1 = m_palette used
101 EfeWidth
= 0; // Efective Width
107 imageOK
= ReadFile (ImageFileName
);
111 wxPNGReader::Create(int width
, int height
, int depth
, int colortype
)
113 Width
= width
; Height
= height
; Depth
= depth
;
114 ColorType
= (colortype
>=0) ? colortype
: ((Depth
>8) ? COLORTYPE_COLOR
: 0);
121 wxMacDestroyGWorld( (GWorldPtr
) lpbi
) ;
123 lpbi
= wxMacCreateGWorld( Width
, Height
, Depth
);
126 EfeWidth
= (long)(((long)Width
*Depth
+ 31) / 32) * 4;
127 int bitwidth
= width
;
128 if ( EfeWidth
> bitwidth
)
129 bitwidth
= EfeWidth
;
131 RawImage
= (byte
*) new char[ ( bitwidth
* Height
* ((Depth
+7)>>3) ) ];
136 wxPNGReader::~wxPNGReader ( )
138 if (RawImage
!= NULL
) {
143 wxMacDestroyGWorld( (GWorldPtr
) lpbi
) ;
146 if (m_palette
!= NULL
) {
153 int wxPNGReader::GetIndex(int x
, int y
)
155 if (!Inside(x
, y
) || (Depth
>8)) return -1;
157 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
158 int index
= (int)(*ImagePointer
);
162 bool wxPNGReader::GetRGB(int x
, int y
, byte
* r
, byte
* g
, byte
* b
)
164 if (!Inside(x
, y
)) return FALSE
;
167 return m_palette
->GetRGB(GetIndex(x
, y
), r
, g
, b
);
168 /* PALETTEENTRY entry;
169 ::GetPaletteEntries((HPALETTE) m_palette->GetHPALETTE(), GetIndex(x, y), 1, &entry);
172 *b = entry.peBlue; */
174 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
175 *b
= ImagePointer
[0];
176 *g
= ImagePointer
[1];
177 *r
= ImagePointer
[2];
183 bool wxPNGReader::SetIndex(int x
, int y
, int index
)
185 if (!Inside(x
, y
) || (Depth
>8)) return FALSE
;
187 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
188 *ImagePointer
= index
;
193 bool wxPNGReader::SetRGB(int x
, int y
, byte r
, byte g
, byte b
)
195 if (!Inside(x
, y
)) return FALSE
;
197 if (ColorType
& COLORTYPE_PALETTE
)
199 if (!m_palette
) return FALSE
;
200 SetIndex(x
, y
, m_palette
->GetPixel(r
, g
, b
));
203 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
212 bool wxPNGReader::SetPalette(wxPalette
* colourmap
)
217 ColorType
|= (COLORTYPE_PALETTE
| COLORTYPE_COLOR
);
218 m_palette
= new wxPalette( *colourmap
);
220 // return (DibSetUsage(lpbi, (HPALETTE) m_palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
224 wxPNGReader::SetPalette(int n
, byte
*r
, byte
*g
, byte
*b
)
227 m_palette
= new wxPalette();
233 m_palette
->Create(n
, r
, g
, b
);
234 ColorType
|= (COLORTYPE_PALETTE
| COLORTYPE_COLOR
);
236 // return (DibSetUsage(lpbi, (HPALETTE) m_palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
240 wxPNGReader::SetPalette(int n
, rgb_color_struct
*rgb_struct
)
243 m_palette
= new wxPalette();
247 byte r
[256], g
[256], b
[256];
249 for(int i
=0; i
<n
; i
++)
251 r
[i
] = rgb_struct
[i
].red
;
252 g
[i
] = rgb_struct
[i
].green
;
253 b
[i
] = rgb_struct
[i
].blue
;
255 // Added by JACS copying from Andrew Davison's additions
256 // to GIF-reading code
257 // Make transparency colour black...
259 r
[bgindex
] = g
[bgindex
] = b
[bgindex
] = 0;
261 m_palette
->Create(n
, r
, g
, b
);
262 ColorType
|= (COLORTYPE_PALETTE
| COLORTYPE_COLOR
);
264 // return (DibSetUsage(lpbi, (HPALETTE) m_palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
267 void wxPNGReader::NullData()
270 wxMacDestroyGWorld( (GWorldPtr
) lpbi
) ;
273 if (m_palette
!= NULL
) {
279 wxBitmap
* wxPNGReader::GetBitmap(void)
281 wxBitmap
*bitmap
= new wxBitmap
;
282 if ( InstantiateBitmap(bitmap
) )
291 bool wxPNGReader::InstantiateBitmap(wxBitmap
*bitmap
)
295 bitmap
->SetHBITMAP((WXHBITMAP
) lpbi
);
296 bitmap
->SetWidth(GetWidth());
297 bitmap
->SetHeight(GetHeight());
298 bitmap
->SetDepth(GetDepth());
299 if ( GetDepth() > 1 && m_palette
)
300 bitmap
->SetPalette(*m_palette
);
304 // Make a mask if appropriate
308 wxMask *mask = CreateMask();
309 bitmap->SetMask(mask);
312 lpbi
= NULL
; // bitmap has taken over ownership
320 HDC dc = ::CreateCompatibleDC(NULL);
324 // tmpBitmap is a dummy, to satisfy ::CreateCompatibleDC (it
325 // is a memory dc that must have a bitmap selected into it)
326 HDC dc2 = GetDC(NULL);
327 HBITMAP tmpBitmap = ::CreateCompatibleBitmap(dc2, GetWidth(), GetHeight());
328 ReleaseDC(NULL, dc2);
329 HBITMAP oldBitmap = (HBITMAP) ::SelectObject(dc, tmpBitmap);
333 HPALETTE oldPal = ::SelectPalette(dc, (HPALETTE) m_palette->GetHPALETTE(), FALSE);
334 ::RealizePalette(dc);
337 HBITMAP hBitmap = ::CreateDIBitmap(dc, lpbi,
338 CBM_INIT, RawImage, (LPBITMAPINFO) lpbi, DIB_PAL_COLORS);
340 ::SelectPalette(dc, NULL, TRUE);
341 ::SelectObject(dc, oldBitmap);
342 ::DeleteObject(tmpBitmap);
347 bitmap->SetHBITMAP((WXHBITMAP) hBitmap);
348 bitmap->SetWidth(GetWidth());
349 bitmap->SetHeight(GetHeight());
350 bitmap->SetDepth(GetDepth());
351 if ( GetDepth() > 1 && m_palette )
352 bitmap->SetPalette(*m_palette);
356 // Make a mask if appropriate
359 wxMask *mask = CreateMask();
360 bitmap->SetMask(mask);
377 wxPalette
*wxCopyPalette(const wxPalette
*cmap
)
379 wxPalette
*newCmap
= new wxPalette( *cmap
) ;
383 wxMask
*wxPNGReader::CreateMask(void)
386 HBITMAP hBitmap = ::CreateBitmap(GetWidth(), GetHeight(), 1, 1, NULL);
388 HDC dc = ::CreateCompatibleDC(NULL);
389 HBITMAP oldBitmap = (HBITMAP) ::SelectObject(dc, hBitmap);
391 int bgIndex = GetBGIndex();
395 for (x=0; x<GetWidth(); x++)
397 for (y=0; y<GetHeight(); y++)
399 int index = GetIndex(x, y);
400 if ( index == bgIndex )
401 ::SetPixel(dc, x, GetHeight() - y - 1, RGB(0, 0, 0));
403 ::SetPixel(dc, x, GetHeight() - y - 1, RGB(255, 255, 255));
407 ::SelectObject(dc, oldBitmap);
408 wxMask *mask = new wxMask;
409 mask->SetMaskBitmap((WXHBITMAP) hBitmap);
415 bool wxPNGReader::ReadFile(char * ImageFileName
)
420 strcpy(filename
, ImageFileName
);
425 wxPNGReaderIter
iter(this);
428 fp
= fopen( ImageFileName
, "rb" );
433 /* allocate the necessary structures */
434 png_ptr
= new (png_struct
);
441 info_ptr
= new (png_info
);
448 /* set error handling */
449 if (setjmp(png_ptr
->jmpbuf
))
451 png_read_destroy(png_ptr
, info_ptr
, (png_info
*)0);
456 /* If we get here, we had a problem reading the file */
459 //png_set_error(ima_png_error, NULL);
461 /* initialize the structures, info first for error handling */
462 png_info_init(info_ptr
);
463 png_read_init(png_ptr
);
465 /* set up the input control */
466 png_init_io(png_ptr
, fp
);
468 /* read the file information */
469 png_read_info(png_ptr
, info_ptr
);
471 /* allocate the memory to hold the image using the fields
473 png_color_16 my_background
={ 0, 31, 127, 255, 0 };
475 if (info_ptr
->valid
& PNG_INFO_bKGD
)
477 png_set_background(png_ptr
, &(info_ptr
->background
),
478 PNG_BACKGROUND_GAMMA_FILE
, 1, 1.0);
479 if ( info_ptr
->num_palette
> 0 )
480 bgindex
= info_ptr
->background
.index
;
483 png_set_background(png_ptr
, &my_background
,
484 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
486 // Added by JACS: guesswork!
487 if ( info_ptr
->num_trans
!= 0 )
488 bgindex
= info_ptr
->num_trans
- 1 ;
491 /* tell libpng to strip 16 bit depth files down to 8 bits */
492 if (info_ptr
->bit_depth
== 16)
493 png_set_strip_16(png_ptr
);
495 int pixel_depth
=(info_ptr
->pixel_depth
<24) ? info_ptr
->pixel_depth
: 24;
496 Create(info_ptr
->width
, info_ptr
->height
, pixel_depth
,
497 info_ptr
->color_type
);
499 if (info_ptr
->num_palette
>0)
501 SetPalette((int)info_ptr
->num_palette
, (rgb_color_struct
*)info_ptr
->palette
);
504 int row_stride
= info_ptr
->width
* ((pixel_depth
+7)>>3);
505 // printf("P = %d D = %d RS= %d ", info_ptr->num_palette, info_ptr->pixel_depth,row_stride);
506 // printf("CT = %d TRS = %d BD= %d ", info_ptr->color_type, info_ptr->valid & PNG_INFO_tRNS,info_ptr->bit_depth);
508 byte
*row_pointers
= new byte
[row_stride
];
510 /* turn on interlace handling */
511 if (info_ptr
->interlace_type
)
512 number_passes
= png_set_interlace_handling(png_ptr
);
515 // printf("NP = %d ", number_passes);
517 for (int pass
=0; pass
< number_passes
; pass
++)
522 GDHandle origDevice
;
524 GetGWorld( &origPort
, &origDevice
) ;
526 SetGWorld( (GWorldPtr
) lpbi
, NULL
) ;
529 // (unsigned char *)iter.GetRow();
530 if (info_ptr
->interlace_type
)
533 iter
.GetRow(row_pointers
, row_stride
);
534 png_read_row(png_ptr
, row_pointers
, NULL
);
537 png_read_row(png_ptr
, row_pointers
, NULL
);
539 if ( info_ptr
->palette
)
541 if ( pixel_depth
== 8 )
543 for ( size_t i
= 0 ; i
< info_ptr
->width
; ++i
)
545 png_color_struct
* color
;
548 int index
= row_pointers
[i
] ;
549 color
= &info_ptr
->palette
[index
] ;
550 col
.red
= (((int)color
->red
) << 8) | ((int)color
->red
) ;
551 col
.green
= (((int)color
->green
) << 8) | ((int)color
->green
) ;
552 col
.blue
= (((int)color
->blue
) << 8) | ((int)color
->blue
) ;
553 SetCPixel( i
, y
, &col
);
556 png_color_struct* color ;
558 unsigned char* p = &row_pointers[0] ;
562 color = &info_ptr->palette[index] ;
563 col.red = (color->red << 8) | color->red ;
564 col.green = (color->green << 8) | color->green ;
565 col.blue = (color->blue << 8) | color->blue ;
566 RGBForeColor( &col ) ;
567 col.red = col.green = col.blue = 0xFFFF ;
568 RGBBackColor( &col ) ;
569 for ( int i = 0 ; i < info_ptr->width ; ++i , ++p)
575 color = &info_ptr->palette[index] ;
576 col.red = (((int)color->red) << 8) | ((int)color->red) ;
577 col.green = (((int)color->green) << 8) | ((int)color->green) ;
578 col.blue = (((int)color->blue) << 8) | ((int)color->blue) ;
579 RGBForeColor( &col ) ;
582 LineTo( info_ptr->width , y ) ;
587 for ( size_t i
= 0 ; i
< info_ptr
->width
; ++i
)
589 png_color_struct
* color
;
592 int byte
= ( i
* pixel_depth
) / 8 ;
593 int offset
= ( 8 - pixel_depth
) - ( i
* pixel_depth
) % 8 ;
595 int index
= ( row_pointers
[byte
] >> offset
) & ( 0xFF >> ( 8 - pixel_depth
) );
596 color
= &info_ptr
->palette
[index
] ;
597 col
.red
= (((int)color
->red
) << 8) | ((int)color
->red
) ;
598 col
.green
= (((int)color
->green
) << 8) | ((int)color
->green
) ;
599 col
.blue
= (((int)color
->blue
) << 8) | ((int)color
->blue
) ;
600 SetCPixel( i
, y
, &col
);
606 for ( size_t i
= 0 ; i
< info_ptr
->width
; ++i
)
608 png_color_struct
* color
;
610 color
=(png_color_struct
*) (&row_pointers
[i
*3]) ;
611 col
.red
= (((int)color
->red
) << 8) | ((int)color
->red
) ;
612 col
.green
= (((int)color
->green
) << 8) | ((int)color
->green
) ;
613 col
.blue
= (((int)color
->blue
) << 8) | ((int)color
->blue
) ;
614 SetCPixel( i
, y
, &col
);
618 iter
.SetRow(row_pointers
, row_stride
);
621 while(iter
.PrevRow());
622 SetGWorld( origPort
, origDevice
) ;
624 // printf("Y=%d ",y);
626 delete[] row_pointers
;
628 /* read the rest of the file, getting any additional chunks
630 png_read_end(png_ptr
, info_ptr
);
632 /* clean up after the read, and free any memory allocated */
633 png_read_destroy(png_ptr
, info_ptr
, (png_info
*)0);
635 /* free the structures */
647 /* write a png file */
649 bool wxPNGReader::SaveFile(char * ImageFileName
)
652 strcpy(filename
, ImageFileName
);
654 wxPNGReaderIter
iter(this);
660 fp
= fopen(filename
, "wb");
664 /* allocate the necessary structures */
665 png_ptr
= new (png_struct
);
672 info_ptr
= new (png_info
);
680 /* set error handling */
681 if (setjmp(png_ptr
->jmpbuf
))
683 png_write_destroy(png_ptr
);
688 /* If we get here, we had a problem reading the file */
691 //png_set_error(ima_png_error, NULL);
693 // printf("writig pg %s ", filename);
694 /* initialize the structures */
695 png_info_init(info_ptr
);
696 png_write_init(png_ptr
);
698 int row_stride
= GetWidth() * ((GetDepth()+7)>>3);
699 /* set up the output control */
700 png_init_io(png_ptr
, fp
);
702 /* set the file information here */
703 info_ptr
->width
= GetWidth();
704 info_ptr
->height
= GetHeight();
705 info_ptr
->pixel_depth
= GetDepth();
706 info_ptr
->channels
= (GetDepth()>8) ? 3: 1;
707 info_ptr
->bit_depth
= GetDepth()/info_ptr
->channels
;
708 info_ptr
->color_type
= GetColorType();
709 info_ptr
->compression_type
= info_ptr
->filter_type
= info_ptr
->interlace_type
=0;
711 info_ptr
->rowbytes
= row_stride
;
714 // 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);
715 /* set the palette if there is one */
716 if ((GetColorType() & COLORTYPE_PALETTE
) && GetPalette())
718 // printf("writing paleta[%d %d %x]",GetColorType() ,COLORTYPE_PALETTE, GetPalette());
719 info_ptr
->valid
|= PNG_INFO_PLTE
;
720 info_ptr
->palette
= new png_color
[256];
721 info_ptr
->num_palette
= 256;
722 for (int i
=0; i
<256; i
++)
723 GetPalette()->GetRGB(i
, &info_ptr
->palette
[i
].red
, &info_ptr
->palette
[i
].green
, &info_ptr
->palette
[i
].blue
);
725 // printf("Paleta [%d %d %x]",GetColorType() ,COLORTYPE_PALETTE, GetPalette());
728 /* optional significant bit chunk */
729 // info_ptr->valid |= PNG_INFO_sBIT;
730 // info_ptr->sig_bit = true_bit_depth;
732 /* optional gamma chunk */
733 // info_ptr->valid |= PNG_INFO_gAMA;
734 // info_ptr->gamma = gamma;
736 /* other optional chunks */
738 /* write the file information */
739 png_write_info(png_ptr
, info_ptr
);
741 /* set up the transformations you want. Note that these are
742 all optional. Only call them if you want them */
744 /* shift the pixels up to a legal bit depth and fill in
745 as appropriate to correctly scale the image */
746 // png_set_shift(png_ptr, &(info_ptr->sig_bit));
748 /* pack pixels into bytes */
749 // png_set_packing(png_ptr);
751 /* flip bgr pixels to rgb */
752 // png_set_bgr(png_ptr);
754 /* swap bytes of 16 bit files to most significant bit first */
755 // png_set_swap(png_ptr);
757 /* get rid of filler bytes, pack rgb into 3 bytes */
758 // png_set_rgbx(png_ptr);
760 /* If you are only writing one row at a time, this works */
762 byte
*row_pointers
= new byte
[row_stride
];
765 // (unsigned char *)iter.GetRow();
766 iter
.GetRow(row_pointers
, row_stride
);
767 png_write_row(png_ptr
, row_pointers
);
768 } while(iter
.PrevRow());
770 delete[] row_pointers
;
772 /* write the rest of the file */
773 png_write_end(png_ptr
, info_ptr
);
775 /* clean up after the write, and free any memory allocated */
776 png_write_destroy(png_ptr
);
778 /* if you malloced the palette, free it here */
779 if (info_ptr
->palette
)
780 delete[] (info_ptr
->palette
);
782 /* free the structures */
793 static int Power(int x
, int y
)
797 for ( i
= 0; i
< y
; i
++)
804 static char hexArray
[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
805 'C', 'D', 'E', 'F' };
807 static void DecToHex(int dec
, char *buf
)
809 int firstDigit
= (int)(dec
/16.0);
810 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
811 buf
[0] = hexArray
[firstDigit
];
812 buf
[1] = hexArray
[secondDigit
];
817 bool wxPNGReader::SaveXPM(char *filename
, char *name
)
821 strcpy(nameStr
, name
);
824 wxString str
= wxString::FromAscii(filename
) ;
825 wxStripExtension( str
) ;
826 strcpy(nameStr
, str
.ToAscii() );
829 if ( GetDepth() > 4 )
831 // Only a depth of 4 and below allowed
838 wxSTD ofstream
str(filename
);
842 int noColours
= Power(2, GetDepth());
845 str
<< "/* XPM */\n";
846 str
<< "static char * " << nameStr
<< "_xpm[] = {\n";
847 str
<< "\"" << GetWidth() << " " << GetHeight() << " " << noColours
<< " 1\",\n";
850 int base
= 97 ; // start from 'a'
852 unsigned char red
, green
, blue
;
855 for ( i
= 0; i
< noColours
; i
++)
857 str
<< "\"" << (char)(base
+ i
) << " c #";
858 GetPalette()->GetRGB(i
, &red
, &green
, &blue
);
859 DecToHex(red
, hexBuf
);
861 DecToHex(green
, hexBuf
);
863 DecToHex(blue
, hexBuf
);
870 for ( y
= 0; y
< GetHeight(); y
++)
873 for ( x
= 0; x
< GetWidth(); x
++)
875 int index
= GetIndex(x
, y
);
876 str
<< (char)(base
+ index
) ;
888 IMPLEMENT_DYNAMIC_CLASS(wxPNGFileHandler
, wxBitmapHandler
)
890 bool wxPNGFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
891 int desiredWidth
, int desiredHeight
)
894 if (reader
.ReadFile( (char*)(const char*) name
.ToAscii() ) )
896 return reader
.InstantiateBitmap(bitmap
);
902 bool wxPNGFileHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*pal
)
907 #endif //wxUSE_LIBPNG