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"
37 #include "wx/msgdlg.h"
38 #include "wx/palette.h"
39 #include "wx/bitmap.h"
40 #include "wx/mac/pnghand.h"
41 #include "wx/mac/pngread.h"
47 extern "C" void png_read_init
PNGARG((png_structp png_ptr
));
48 extern "C" void png_write_init
PNGARG((png_structp png_ptr
));
50 extern CTabHandle
wxMacCreateColorTable( int numColors
) ;
51 extern void wxMacDestroyColorTable( CTabHandle colors
) ;
52 extern void wxMacSetColorTableEntry( CTabHandle newColors
, int index
, int red
, int green
, int blue
) ;
53 extern GWorldPtr
wxMacCreateGWorld( int width
, int height
, int depth
) ;
54 extern void wxMacDestroyGWorld( GWorldPtr gw
) ;
57 ima_png_error(png_struct
*png_ptr
, char *message
)
59 wxMessageBox(message
, "PNG error");
60 longjmp(png_ptr
->jmpbuf
, 1);
64 // static wxGifReaderIter* iter;
65 wxPalette
*wxCopyPalette(const wxPalette
*cmap
);
67 wxPNGReader::wxPNGReader(void)
70 RawImage
= NULL
; // Image data
72 Width
= 0; Height
= 0; // Dimensions
73 Depth
= 0; // (bits x pixel)
74 ColorType
= 0; // Bit 1 = Palette used
78 EfeWidth
= 0; // Efective Width
86 wxPNGReader::wxPNGReader ( char* ImageFileName
)
90 RawImage
= NULL
; // Image data
92 Width
= 0; Height
= 0; // Dimensions
93 Depth
= 0; // (bits x pixel)
94 ColorType
= 0; // Bit 1 = Palette used
98 EfeWidth
= 0; // Efective Width
104 imageOK
= ReadFile (ImageFileName
);
108 wxPNGReader::Create(int width
, int height
, int depth
, int colortype
)
110 Width
= width
; Height
= height
; Depth
= depth
;
111 ColorType
= (colortype
>=0) ? colortype
: ((Depth
>8) ? COLORTYPE_COLOR
: 0);
119 wxMacDestroyGWorld( lpbi
) ;
121 lpbi
= wxMacCreateGWorld( Width
, Height
, Depth
);
124 EfeWidth
= (long)(((long)Width
*Depth
+ 31) / 32) * 4;
125 int bitwidth
= width
;
126 if ( EfeWidth
> bitwidth
)
127 bitwidth
= EfeWidth
;
129 RawImage
= (byte
*) new char[ ( bitwidth
* Height
* ((Depth
+7)>>3) ) ];
134 wxPNGReader::~wxPNGReader ( )
138 wxMacDestroyGWorld( lpbi
) ;
144 int wxPNGReader::GetIndex(int x
, int y
)
146 if (!Inside(x
, y
) || (Depth
>8)) return -1;
148 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
149 int index
= (int)(*ImagePointer
);
153 bool wxPNGReader::GetRGB(int x
, int y
, byte
* r
, byte
* g
, byte
* b
)
155 if (!Inside(x
, y
)) return FALSE
;
158 return Palette
->GetRGB(GetIndex(x
, y
), r
, g
, b
);
159 /* PALETTEENTRY entry;
160 ::GetPaletteEntries((HPALETTE) Palette->GetHPALETTE(), GetIndex(x, y), 1, &entry);
163 *b = entry.peBlue; */
165 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
166 *b
= ImagePointer
[0];
167 *g
= ImagePointer
[1];
168 *r
= ImagePointer
[2];
174 bool wxPNGReader::SetIndex(int x
, int y
, int index
)
176 if (!Inside(x
, y
) || (Depth
>8)) return FALSE
;
178 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
179 *ImagePointer
= index
;
184 bool wxPNGReader::SetRGB(int x
, int y
, byte r
, byte g
, byte b
)
186 if (!Inside(x
, y
)) return FALSE
;
188 if (ColorType
& COLORTYPE_PALETTE
)
190 if (!Palette
) return FALSE
;
191 SetIndex(x
, y
, Palette
->GetPixel(r
, g
, b
));
194 ImagePointerType ImagePointer
= RawImage
+ EfeWidth
*y
+ (x
*Depth
>> 3);
203 bool wxPNGReader::SetPalette(wxPalette
* colourmap
)
208 ColorType
|= (COLORTYPE_PALETTE
| COLORTYPE_COLOR
);
209 Palette
= new wxPalette( *colourmap
);
211 // return (DibSetUsage(lpbi, (HPALETTE) Palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
215 wxPNGReader::SetPalette(int n
, byte
*r
, byte
*g
, byte
*b
)
218 Palette
= new wxPalette();
224 Palette
->Create(n
, r
, g
, b
);
225 ColorType
|= (COLORTYPE_PALETTE
| COLORTYPE_COLOR
);
227 // return (DibSetUsage(lpbi, (HPALETTE) Palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
231 wxPNGReader::SetPalette(int n
, rgb_color_struct
*rgb_struct
)
234 Palette
= new wxPalette();
238 byte r
[256], g
[256], b
[256];
240 for(int i
=0; i
<n
; i
++)
242 r
[i
] = rgb_struct
[i
].red
;
243 g
[i
] = rgb_struct
[i
].green
;
244 b
[i
] = rgb_struct
[i
].blue
;
246 // Added by JACS copying from Andrew Davison's additions
247 // to GIF-reading code
248 // Make transparency colour black...
250 r
[bgindex
] = g
[bgindex
] = b
[bgindex
] = 0;
252 Palette
->Create(n
, r
, g
, b
);
253 ColorType
|= (COLORTYPE_PALETTE
| COLORTYPE_COLOR
);
255 // return (DibSetUsage(lpbi, (HPALETTE) Palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
258 void wxPNGReader::NullData()
261 wxMacDestroyGWorld( lpbi
) ;
268 wxBitmap
* wxPNGReader::GetBitmap(void)
270 wxBitmap
*bitmap
= new wxBitmap
;
271 if ( InstantiateBitmap(bitmap
) )
280 bool wxPNGReader::InstantiateBitmap(wxBitmap
*bitmap
)
284 bitmap
->SetHBITMAP((WXHBITMAP
) lpbi
);
285 bitmap
->SetWidth(GetWidth());
286 bitmap
->SetHeight(GetHeight());
287 bitmap
->SetDepth(GetDepth());
288 if ( GetDepth() > 1 && Palette
)
289 bitmap
->SetPalette(*Palette
);
293 // Make a mask if appropriate
297 wxMask *mask = CreateMask();
298 bitmap->SetMask(mask);
301 lpbi
= NULL
; // bitmap has taken over ownership
309 HDC dc = ::CreateCompatibleDC(NULL);
313 // tmpBitmap is a dummy, to satisfy ::CreateCompatibleDC (it
314 // is a memory dc that must have a bitmap selected into it)
315 HDC dc2 = GetDC(NULL);
316 HBITMAP tmpBitmap = ::CreateCompatibleBitmap(dc2, GetWidth(), GetHeight());
317 ReleaseDC(NULL, dc2);
318 HBITMAP oldBitmap = (HBITMAP) ::SelectObject(dc, tmpBitmap);
322 HPALETTE oldPal = ::SelectPalette(dc, (HPALETTE) Palette->GetHPALETTE(), FALSE);
323 ::RealizePalette(dc);
326 HBITMAP hBitmap = ::CreateDIBitmap(dc, lpbi,
327 CBM_INIT, RawImage, (LPBITMAPINFO) lpbi, DIB_PAL_COLORS);
329 ::SelectPalette(dc, NULL, TRUE);
330 ::SelectObject(dc, oldBitmap);
331 ::DeleteObject(tmpBitmap);
336 bitmap->SetHBITMAP((WXHBITMAP) hBitmap);
337 bitmap->SetWidth(GetWidth());
338 bitmap->SetHeight(GetHeight());
339 bitmap->SetDepth(GetDepth());
340 if ( GetDepth() > 1 && Palette )
341 bitmap->SetPalette(*Palette);
345 // Make a mask if appropriate
348 wxMask *mask = CreateMask();
349 bitmap->SetMask(mask);
366 wxPalette
*wxCopyPalette(const wxPalette
*cmap
)
368 wxPalette
*newCmap
= new wxPalette( *cmap
) ;
372 wxMask
*wxPNGReader::CreateMask(void)
375 HBITMAP hBitmap = ::CreateBitmap(GetWidth(), GetHeight(), 1, 1, NULL);
377 HDC dc = ::CreateCompatibleDC(NULL);
378 HBITMAP oldBitmap = (HBITMAP) ::SelectObject(dc, hBitmap);
380 int bgIndex = GetBGIndex();
384 for (x=0; x<GetWidth(); x++)
386 for (y=0; y<GetHeight(); y++)
388 int index = GetIndex(x, y);
389 if ( index == bgIndex )
390 ::SetPixel(dc, x, GetHeight() - y - 1, RGB(0, 0, 0));
392 ::SetPixel(dc, x, GetHeight() - y - 1, RGB(255, 255, 255));
396 ::SelectObject(dc, oldBitmap);
397 wxMask *mask = new wxMask;
398 mask->SetMaskBitmap((WXHBITMAP) hBitmap);
404 bool wxPNGReader::ReadFile(char * ImageFileName
)
409 strcpy(filename
, ImageFileName
);
414 wxPNGReaderIter
iter(this);
417 fp
= fopen(wxUnix2MacFilename( ImageFileName
), "rb");
421 /* allocate the necessary structures */
422 png_ptr
= new (png_struct
);
429 info_ptr
= new (png_info
);
436 /* set error handling */
437 if (setjmp(png_ptr
->jmpbuf
))
439 png_read_destroy(png_ptr
, info_ptr
, (png_info
*)0);
444 /* If we get here, we had a problem reading the file */
447 //png_set_error(ima_png_error, NULL);
449 /* initialize the structures, info first for error handling */
450 png_info_init(info_ptr
);
451 png_read_init(png_ptr
);
453 /* set up the input control */
454 png_init_io(png_ptr
, fp
);
456 /* read the file information */
457 png_read_info(png_ptr
, info_ptr
);
459 /* allocate the memory to hold the image using the fields
461 png_color_16 my_background
={ 0, 31, 127, 255, 0 };
463 if (info_ptr
->valid
& PNG_INFO_bKGD
)
465 png_set_background(png_ptr
, &(info_ptr
->background
),
466 PNG_BACKGROUND_GAMMA_FILE
, 1, 1.0);
467 if ( info_ptr
->num_palette
> 0 )
468 bgindex
= info_ptr
->background
.index
;
471 png_set_background(png_ptr
, &my_background
,
472 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
474 // Added by JACS: guesswork!
475 if ( info_ptr
->num_trans
!= 0 )
476 bgindex
= info_ptr
->num_trans
- 1 ;
479 /* tell libpng to strip 16 bit depth files down to 8 bits */
480 if (info_ptr
->bit_depth
== 16)
481 png_set_strip_16(png_ptr
);
483 int pixel_depth
=(info_ptr
->pixel_depth
<24) ? info_ptr
->pixel_depth
: 24;
484 Create(info_ptr
->width
, info_ptr
->height
, pixel_depth
,
485 info_ptr
->color_type
);
487 if (info_ptr
->num_palette
>0)
489 SetPalette((int)info_ptr
->num_palette
, (rgb_color_struct
*)info_ptr
->palette
);
492 int row_stride
= info_ptr
->width
* ((pixel_depth
+7)>>3);
493 // printf("P = %d D = %d RS= %d ", info_ptr->num_palette, info_ptr->pixel_depth,row_stride);
494 // printf("CT = %d TRS = %d BD= %d ", info_ptr->color_type, info_ptr->valid & PNG_INFO_tRNS,info_ptr->bit_depth);
496 byte
*row_pointers
= new byte
[row_stride
];
498 /* turn on interlace handling */
499 if (info_ptr
->interlace_type
)
500 number_passes
= png_set_interlace_handling(png_ptr
);
503 // printf("NP = %d ", number_passes);
505 for (int pass
=0; pass
< number_passes
; pass
++)
510 GDHandle origDevice
;
512 GetGWorld( &origPort
, &origDevice
) ;
514 SetGWorld( lpbi
, NULL
) ;
517 // (unsigned char *)iter.GetRow();
518 if (info_ptr
->interlace_type
)
521 iter
.GetRow(row_pointers
, row_stride
);
522 png_read_row(png_ptr
, row_pointers
, NULL
);
525 png_read_row(png_ptr
, row_pointers
, NULL
);
527 if ( info_ptr
->palette
)
529 if ( pixel_depth
== 8 )
531 for ( int i
= 0 ; i
< info_ptr
->width
; ++i
)
533 png_color_struct
* color
;
536 int index
= row_pointers
[i
] ;
537 color
= &info_ptr
->palette
[index
] ;
538 col
.red
= (((int)color
->red
) << 8) | ((int)color
->red
) ;
539 col
.green
= (((int)color
->green
) << 8) | ((int)color
->green
) ;
540 col
.blue
= (((int)color
->blue
) << 8) | ((int)color
->blue
) ;
541 SetCPixel( i
, y
, &col
);
544 png_color_struct* color ;
546 unsigned char* p = &row_pointers[0] ;
550 color = &info_ptr->palette[index] ;
551 col.red = (color->red << 8) | color->red ;
552 col.green = (color->green << 8) | color->green ;
553 col.blue = (color->blue << 8) | color->blue ;
554 RGBForeColor( &col ) ;
555 col.red = col.green = col.blue = 0xFFFF ;
556 RGBBackColor( &col ) ;
557 for ( int i = 0 ; i < info_ptr->width ; ++i , ++p)
563 color = &info_ptr->palette[index] ;
564 col.red = (((int)color->red) << 8) | ((int)color->red) ;
565 col.green = (((int)color->green) << 8) | ((int)color->green) ;
566 col.blue = (((int)color->blue) << 8) | ((int)color->blue) ;
567 RGBForeColor( &col ) ;
570 LineTo( info_ptr->width , y ) ;
575 for ( int i
= 0 ; i
< info_ptr
->width
; ++i
)
577 png_color_struct
* color
;
580 int byte
= ( i
* pixel_depth
) / 8 ;
581 int offset
= ( 8 - pixel_depth
) - ( i
* pixel_depth
) % 8 ;
583 int index
= ( row_pointers
[byte
] >> offset
) & ( 0xFF >> ( 8 - pixel_depth
) );
584 color
= &info_ptr
->palette
[index
] ;
585 col
.red
= (((int)color
->red
) << 8) | ((int)color
->red
) ;
586 col
.green
= (((int)color
->green
) << 8) | ((int)color
->green
) ;
587 col
.blue
= (((int)color
->blue
) << 8) | ((int)color
->blue
) ;
588 SetCPixel( i
, y
, &col
);
594 for ( int i
= 0 ; i
< info_ptr
->width
; ++i
)
596 png_color_struct
* color
;
598 color
=(png_color_struct
*) (&row_pointers
[i
*3]) ;
599 col
.red
= (((int)color
->red
) << 8) | ((int)color
->red
) ;
600 col
.green
= (((int)color
->green
) << 8) | ((int)color
->green
) ;
601 col
.blue
= (((int)color
->blue
) << 8) | ((int)color
->blue
) ;
602 SetCPixel( i
, y
, &col
);
606 iter
.SetRow(row_pointers
, row_stride
);
609 while(iter
.PrevRow());
610 SetGWorld( origPort
, origDevice
) ;
612 // printf("Y=%d ",y);
614 delete[] row_pointers
;
616 /* read the rest of the file, getting any additional chunks
618 png_read_end(png_ptr
, info_ptr
);
620 /* clean up after the read, and free any memory allocated */
621 png_read_destroy(png_ptr
, info_ptr
, (png_info
*)0);
623 /* free the structures */
635 /* write a png file */
637 bool wxPNGReader::SaveFile(char * ImageFileName
)
640 strcpy(filename
, ImageFileName
);
642 wxPNGReaderIter
iter(this);
648 fp
= fopen(filename
, "wb");
652 /* allocate the necessary structures */
653 png_ptr
= new (png_struct
);
660 info_ptr
= new (png_info
);
668 /* set error handling */
669 if (setjmp(png_ptr
->jmpbuf
))
671 png_write_destroy(png_ptr
);
676 /* If we get here, we had a problem reading the file */
679 //png_set_error(ima_png_error, NULL);
681 // printf("writig pg %s ", filename);
682 /* initialize the structures */
683 png_info_init(info_ptr
);
684 png_write_init(png_ptr
);
686 int row_stride
= GetWidth() * ((GetDepth()+7)>>3);
687 /* set up the output control */
688 png_init_io(png_ptr
, fp
);
690 /* set the file information here */
691 info_ptr
->width
= GetWidth();
692 info_ptr
->height
= GetHeight();
693 info_ptr
->pixel_depth
= GetDepth();
694 info_ptr
->channels
= (GetDepth()>8) ? 3: 1;
695 info_ptr
->bit_depth
= GetDepth()/info_ptr
->channels
;
696 info_ptr
->color_type
= GetColorType();
697 info_ptr
->compression_type
= info_ptr
->filter_type
= info_ptr
->interlace_type
=0;
699 info_ptr
->rowbytes
= row_stride
;
702 // 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);
703 /* set the palette if there is one */
704 if ((GetColorType() & COLORTYPE_PALETTE
) && GetPalette())
706 // printf("writing paleta[%d %d %x]",GetColorType() ,COLORTYPE_PALETTE, GetPalette());
707 info_ptr
->valid
|= PNG_INFO_PLTE
;
708 info_ptr
->palette
= new png_color
[256];
709 info_ptr
->num_palette
= 256;
710 for (int i
=0; i
<256; i
++)
711 GetPalette()->GetRGB(i
, &info_ptr
->palette
[i
].red
, &info_ptr
->palette
[i
].green
, &info_ptr
->palette
[i
].blue
);
713 // printf("Paleta [%d %d %x]",GetColorType() ,COLORTYPE_PALETTE, GetPalette());
716 /* optional significant bit chunk */
717 // info_ptr->valid |= PNG_INFO_sBIT;
718 // info_ptr->sig_bit = true_bit_depth;
720 /* optional gamma chunk */
721 // info_ptr->valid |= PNG_INFO_gAMA;
722 // info_ptr->gamma = gamma;
724 /* other optional chunks */
726 /* write the file information */
727 png_write_info(png_ptr
, info_ptr
);
729 /* set up the transformations you want. Note that these are
730 all optional. Only call them if you want them */
732 /* shift the pixels up to a legal bit depth and fill in
733 as appropriate to correctly scale the image */
734 // png_set_shift(png_ptr, &(info_ptr->sig_bit));
736 /* pack pixels into bytes */
737 // png_set_packing(png_ptr);
739 /* flip bgr pixels to rgb */
740 // png_set_bgr(png_ptr);
742 /* swap bytes of 16 bit files to most significant bit first */
743 // png_set_swap(png_ptr);
745 /* get rid of filler bytes, pack rgb into 3 bytes */
746 // png_set_rgbx(png_ptr);
748 /* If you are only writing one row at a time, this works */
750 byte
*row_pointers
= new byte
[row_stride
];
753 // (unsigned char *)iter.GetRow();
754 iter
.GetRow(row_pointers
, row_stride
);
755 png_write_row(png_ptr
, row_pointers
);
756 } while(iter
.PrevRow());
758 delete[] row_pointers
;
760 /* write the rest of the file */
761 png_write_end(png_ptr
, info_ptr
);
763 /* clean up after the write, and free any memory allocated */
764 png_write_destroy(png_ptr
);
766 /* if you malloced the palette, free it here */
767 if (info_ptr
->palette
)
768 delete[] (info_ptr
->palette
);
770 /* free the structures */
781 static int Power(int x
, int y
)
785 for ( i
= 0; i
< y
; i
++)
792 static char hexArray
[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
793 'C', 'D', 'E', 'F' };
795 static void DecToHex(int dec
, char *buf
)
797 int firstDigit
= (int)(dec
/16.0);
798 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
799 buf
[0] = hexArray
[firstDigit
];
800 buf
[1] = hexArray
[secondDigit
];
805 bool wxPNGReader::SaveXPM(char *filename
, char *name
)
809 strcpy(nameStr
, name
);
812 strcpy(nameStr
, filename
);
813 wxStripExtension(nameStr
);
816 if ( GetDepth() > 4 )
818 // Only a depth of 4 and below allowed
825 ofstream
str(filename
);
829 int noColours
= Power(2, GetDepth());
832 str
<< "/* XPM */\n";
833 str
<< "static char * " << nameStr
<< "_xpm[] = {\n";
834 str
<< "\"" << GetWidth() << " " << GetHeight() << " " << noColours
<< " 1\",\n";
837 int base
= 97 ; // start from 'a'
839 unsigned char red
, green
, blue
;
842 for ( i
= 0; i
< noColours
; i
++)
844 str
<< "\"" << (char)(base
+ i
) << " c #";
845 GetPalette()->GetRGB(i
, &red
, &green
, &blue
);
846 DecToHex(red
, hexBuf
);
848 DecToHex(green
, hexBuf
);
850 DecToHex(blue
, hexBuf
);
857 for ( y
= 0; y
< GetHeight(); y
++)
860 for ( x
= 0; x
< GetWidth(); x
++)
862 int index
= GetIndex(x
, y
);
863 str
<< (char)(base
+ index
) ;
875 IMPLEMENT_DYNAMIC_CLASS(wxPNGFileHandler
, wxBitmapHandler
)
877 bool wxPNGFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
878 int desiredWidth
, int desiredHeight
)
881 if (reader
.ReadFile((char*) (const char*) name
))
883 return reader
.InstantiateBitmap(bitmap
);
889 bool wxPNGFileHandler::SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*pal
)