]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/pnghand.cpp
Lots of fixes/cleanups for OSX taskbar
[wxWidgets.git] / src / mac / carbon / pnghand.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: pnghand.cpp
3 // Purpose: Implements a PNG reader class + handler
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 # pragma implementation "pngread.h"
14 # pragma implementation "pnghand.h"
15 #endif
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 # pragma hdrstop
22 #endif
23
24 #if wxUSE_LIBPNG
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #if wxUSE_IOSTREAMH
31 # include <fstream.h>
32 #else
33 # include <fstream>
34 #endif
35
36 #ifndef __DARWIN__
37 # include <windows.h>
38 #endif
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"
45
46 extern "C" {
47 #include "png.h"
48 }
49
50 extern "C" void png_read_init PNGARG((png_structp png_ptr));
51 extern "C" void png_write_init PNGARG((png_structp png_ptr));
52
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 ) ;
58
59 void
60 ima_png_error(png_struct *png_ptr, char *message)
61 {
62 wxMessageBox(wxString::FromAscii(message), wxT("PNG error"));
63 longjmp(png_ptr->jmpbuf, 1);
64 }
65
66
67 // static wxGifReaderIter* iter;
68 wxPalette *wxCopyPalette(const wxPalette *cmap);
69
70 wxPNGReader::wxPNGReader(void)
71 {
72 filetype = 0;
73 RawImage = NULL; // Image data
74
75 Width = 0; Height = 0; // Dimensions
76 Depth = 0; // (bits x pixel)
77 ColorType = 0; // Bit 1 = Palette used
78 // Bit 2 = Color used
79 // Bit 3 = Alpha used
80
81 EfeWidth = 0; // Efective Width
82
83 lpbi = NULL;
84 bgindex = -1;
85 m_palette = 0;
86 imageOK = FALSE;
87 }
88
89 wxPNGReader::wxPNGReader ( char* ImageFileName )
90 {
91 imageOK = FALSE;
92 filetype = 0;
93 RawImage = NULL; // Image data
94
95 Width = 0; Height = 0; // Dimensions
96 Depth = 0; // (bits x pixel)
97 ColorType = 0; // Bit 1 = m_palette used
98 // Bit 2 = Color used
99 // Bit 3 = Alpha used
100
101 EfeWidth = 0; // Efective Width
102
103 lpbi = NULL;
104 bgindex = -1;
105 m_palette = 0;
106
107 imageOK = ReadFile (ImageFileName);
108 }
109
110 void
111 wxPNGReader::Create(int width, int height, int depth, int colortype)
112 {
113 Width = width; Height = height; Depth = depth;
114 ColorType = (colortype>=0) ? colortype: ((Depth>8) ? COLORTYPE_COLOR: 0);
115 delete m_palette;
116 m_palette = NULL;
117 delete[] RawImage;
118 RawImage = NULL;
119
120 if (lpbi) {
121 wxMacDestroyGWorld( (GWorldPtr) lpbi ) ;
122 }
123 lpbi = wxMacCreateGWorld( Width , Height , Depth);
124 if (lpbi)
125 {
126 EfeWidth = (long)(((long)Width*Depth + 31) / 32) * 4;
127 int bitwidth = width ;
128 if ( EfeWidth > bitwidth )
129 bitwidth = EfeWidth ;
130
131 RawImage = (byte*) new char[ ( bitwidth * Height * ((Depth+7)>>3) ) ];
132 imageOK = TRUE;
133 }
134 }
135
136 wxPNGReader::~wxPNGReader ( )
137 {
138 if (RawImage != NULL) {
139 delete[] RawImage ;
140 RawImage = NULL;
141 }
142 if (lpbi) {
143 wxMacDestroyGWorld( (GWorldPtr) lpbi ) ;
144 lpbi = NULL;
145 }
146 if (m_palette != NULL) {
147 delete m_palette;
148 m_palette = NULL;
149 }
150 }
151
152
153 int wxPNGReader::GetIndex(int x, int y)
154 {
155 if (!Inside(x, y) || (Depth>8)) return -1;
156
157 ImagePointerType ImagePointer = RawImage + EfeWidth*y + (x*Depth >> 3);
158 int index = (int)(*ImagePointer);
159 return index;
160 }
161
162 bool wxPNGReader::GetRGB(int x, int y, byte* r, byte* g, byte* b)
163 {
164 if (!Inside(x, y)) return FALSE;
165
166 if (m_palette) {
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);
170 *r = entry.peRed;
171 *g = entry.peGreen;
172 *b = entry.peBlue; */
173 } else {
174 ImagePointerType ImagePointer = RawImage + EfeWidth*y + (x*Depth >> 3);
175 *b = ImagePointer[0];
176 *g = ImagePointer[1];
177 *r = ImagePointer[2];
178 }
179 return TRUE;
180 }
181
182
183 bool wxPNGReader::SetIndex(int x, int y, int index)
184 {
185 if (!Inside(x, y) || (Depth>8)) return FALSE;
186
187 ImagePointerType ImagePointer = RawImage + EfeWidth*y + (x*Depth >> 3);
188 *ImagePointer = index;
189
190 return TRUE;
191 }
192
193 bool wxPNGReader::SetRGB(int x, int y, byte r, byte g, byte b)
194 {
195 if (!Inside(x, y)) return FALSE;
196
197 if (ColorType & COLORTYPE_PALETTE)
198 {
199 if (!m_palette) return FALSE;
200 SetIndex(x, y, m_palette->GetPixel(r, g, b));
201
202 } else {
203 ImagePointerType ImagePointer = RawImage + EfeWidth*y + (x*Depth >> 3);
204 ImagePointer[0] = b;
205 ImagePointer[1] = g;
206 ImagePointer[2] = r;
207 }
208
209 return TRUE;
210 }
211
212 bool wxPNGReader::SetPalette(wxPalette* colourmap)
213 {
214 delete m_palette ;
215 if (!colourmap)
216 return FALSE;
217 ColorType |= (COLORTYPE_PALETTE | COLORTYPE_COLOR);
218 m_palette = new wxPalette( *colourmap );
219 return true ;
220 // return (DibSetUsage(lpbi, (HPALETTE) m_palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
221 }
222
223 bool
224 wxPNGReader::SetPalette(int n, byte *r, byte *g, byte *b)
225 {
226 delete m_palette ;
227 m_palette = new wxPalette();
228 if (!m_palette)
229 return FALSE;
230
231 if (!g) g = r;
232 if (!b) b = g;
233 m_palette->Create(n, r, g, b);
234 ColorType |= (COLORTYPE_PALETTE | COLORTYPE_COLOR);
235 return true ;
236 // return (DibSetUsage(lpbi, (HPALETTE) m_palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
237 }
238
239 bool
240 wxPNGReader::SetPalette(int n, rgb_color_struct *rgb_struct)
241 {
242 delete m_palette ;
243 m_palette = new wxPalette();
244 if (!m_palette)
245 return FALSE;
246
247 byte r[256], g[256], b[256];
248
249 for(int i=0; i<n; i++)
250 {
251 r[i] = rgb_struct[i].red;
252 g[i] = rgb_struct[i].green;
253 b[i] = rgb_struct[i].blue;
254 }
255 // Added by JACS copying from Andrew Davison's additions
256 // to GIF-reading code
257 // Make transparency colour black...
258 if (bgindex != -1)
259 r[bgindex] = g[bgindex] = b[bgindex] = 0;
260
261 m_palette->Create(n, r, g, b);
262 ColorType |= (COLORTYPE_PALETTE | COLORTYPE_COLOR);
263 return true ;
264 // return (DibSetUsage(lpbi, (HPALETTE) m_palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
265 }
266
267 void wxPNGReader::NullData()
268 {
269 if (lpbi) {
270 wxMacDestroyGWorld( (GWorldPtr) lpbi ) ;
271 lpbi = NULL;
272 }
273 if (m_palette != NULL) {
274 delete m_palette;
275 m_palette = NULL;
276 }
277 }
278
279 wxBitmap* wxPNGReader::GetBitmap(void)
280 {
281 wxBitmap *bitmap = new wxBitmap;
282 if ( InstantiateBitmap(bitmap) )
283 return bitmap;
284 else
285 {
286 delete bitmap;
287 return NULL;
288 }
289 }
290
291 bool wxPNGReader::InstantiateBitmap(wxBitmap *bitmap)
292 {
293 if ( lpbi )
294 {
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);
301 bitmap->SetOk(TRUE);
302
303
304 // Make a mask if appropriate
305 /*
306 if ( bgindex > -1 )
307 {
308 wxMask *mask = CreateMask();
309 bitmap->SetMask(mask);
310 }
311 */
312 lpbi = NULL ; // bitmap has taken over ownership
313 return TRUE;
314 }
315 else
316 {
317 return FALSE;
318 }
319 /*
320 HDC dc = ::CreateCompatibleDC(NULL);
321
322 if (dc)
323 {
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);
330
331 if ( m_palette )
332 {
333 HPALETTE oldPal = ::SelectPalette(dc, (HPALETTE) m_palette->GetHPALETTE(), FALSE);
334 ::RealizePalette(dc);
335 }
336
337 HBITMAP hBitmap = ::CreateDIBitmap(dc, lpbi,
338 CBM_INIT, RawImage, (LPBITMAPINFO) lpbi, DIB_PAL_COLORS);
339
340 ::SelectPalette(dc, NULL, TRUE);
341 ::SelectObject(dc, oldBitmap);
342 ::DeleteObject(tmpBitmap);
343 ::DeleteDC(dc);
344
345 if ( hBitmap )
346 {
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);
353 bitmap->SetOk(TRUE);
354
355
356 // Make a mask if appropriate
357 if ( bgindex > -1 )
358 {
359 wxMask *mask = CreateMask();
360 bitmap->SetMask(mask);
361 }
362 return TRUE;
363 }
364 else
365 {
366 return FALSE;
367 }
368 }
369 else
370 {
371 return FALSE;
372 }
373 */
374 return false ;
375 }
376
377 wxPalette *wxCopyPalette(const wxPalette *cmap)
378 {
379 wxPalette *newCmap = new wxPalette( *cmap ) ;
380 return newCmap;
381 }
382
383 wxMask *wxPNGReader::CreateMask(void)
384 {
385 /*
386 HBITMAP hBitmap = ::CreateBitmap(GetWidth(), GetHeight(), 1, 1, NULL);
387
388 HDC dc = ::CreateCompatibleDC(NULL);
389 HBITMAP oldBitmap = (HBITMAP) ::SelectObject(dc, hBitmap);
390
391 int bgIndex = GetBGIndex();
392
393 int x,y;
394
395 for (x=0; x<GetWidth(); x++)
396 {
397 for (y=0; y<GetHeight(); y++)
398 {
399 int index = GetIndex(x, y);
400 if ( index == bgIndex )
401 ::SetPixel(dc, x, GetHeight() - y - 1, RGB(0, 0, 0));
402 else
403 ::SetPixel(dc, x, GetHeight() - y - 1, RGB(255, 255, 255));
404
405 }
406 }
407 ::SelectObject(dc, oldBitmap);
408 wxMask *mask = new wxMask;
409 mask->SetMaskBitmap((WXHBITMAP) hBitmap);
410 return mask;
411 */
412 return NULL ;
413 }
414
415 bool wxPNGReader::ReadFile(char * ImageFileName)
416 {
417 int number_passes;
418
419 if (ImageFileName)
420 strcpy(filename, ImageFileName);
421
422 FILE *fp;
423 png_struct *png_ptr;
424 png_info *info_ptr;
425 wxPNGReaderIter iter(this);
426
427 /* open the file */
428 fp = fopen( ImageFileName , "rb" );
429
430 if (!fp)
431 return FALSE;
432
433 /* allocate the necessary structures */
434 png_ptr = new (png_struct);
435 if (!png_ptr)
436 {
437 fclose(fp);
438 return FALSE;
439 }
440
441 info_ptr = new (png_info);
442 if (!info_ptr)
443 {
444 fclose(fp);
445 delete png_ptr;
446 return FALSE;
447 }
448 /* set error handling */
449 if (setjmp(png_ptr->jmpbuf))
450 {
451 png_read_destroy(png_ptr, info_ptr, (png_info *)0);
452 fclose(fp);
453 delete png_ptr;
454 delete info_ptr;
455
456 /* If we get here, we had a problem reading the file */
457 return FALSE;
458 }
459 //png_set_error(ima_png_error, NULL);
460
461 /* initialize the structures, info first for error handling */
462 png_info_init(info_ptr);
463 png_read_init(png_ptr);
464
465 /* set up the input control */
466 png_init_io(png_ptr, fp);
467
468 /* read the file information */
469 png_read_info(png_ptr, info_ptr);
470
471 /* allocate the memory to hold the image using the fields
472 of png_info. */
473 png_color_16 my_background={ 0, 31, 127, 255, 0 };
474
475 if (info_ptr->valid & PNG_INFO_bKGD)
476 {
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;
481 }
482 else {
483 png_set_background(png_ptr, &my_background,
484 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
485
486 // Added by JACS: guesswork!
487 if ( info_ptr->num_trans != 0 )
488 bgindex = info_ptr->num_trans - 1 ;
489 }
490
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);
494
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);
498
499 if (info_ptr->num_palette>0)
500 {
501 SetPalette((int)info_ptr->num_palette, (rgb_color_struct*)info_ptr->palette);
502 }
503
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);
507
508 byte *row_pointers = new byte[row_stride];
509
510 /* turn on interlace handling */
511 if (info_ptr->interlace_type)
512 number_passes = png_set_interlace_handling(png_ptr);
513 else
514 number_passes = 1;
515 // printf("NP = %d ", number_passes);
516
517 for (int pass=0; pass< number_passes; pass++)
518 {
519 iter.upset();
520 int y=0;
521 CGrafPtr origPort ;
522 GDHandle origDevice ;
523
524 GetGWorld( &origPort , &origDevice ) ;
525 // ignore shapedc
526 SetGWorld( (GWorldPtr) lpbi , NULL ) ;
527 do
528 {
529 // (unsigned char *)iter.GetRow();
530 if (info_ptr->interlace_type)
531 {
532 if (pass>0)
533 iter.GetRow(row_pointers, row_stride);
534 png_read_row(png_ptr, row_pointers, NULL);
535 }
536 else
537 png_read_row(png_ptr, row_pointers, NULL);
538
539 if ( info_ptr->palette )
540 {
541 if ( pixel_depth == 8 )
542 {
543 for ( size_t i = 0 ; i < info_ptr->width ; ++i )
544 {
545 png_color_struct* color ;
546 RGBColor col ;
547
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);
554 }
555 /*
556 png_color_struct* color ;
557 RGBColor col ;
558 unsigned char* p = &row_pointers[0] ;
559 PenNormal() ;
560 MoveTo( 0 , y ) ;
561 int index = *p ;
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)
570 {
571 if ( *p != index )
572 {
573 LineTo( i , y ) ;
574 index = *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 ) ;
580 }
581 }
582 LineTo( info_ptr->width , y ) ;
583 */
584 }
585 else
586 {
587 for ( size_t i = 0 ; i < info_ptr->width ; ++i )
588 {
589 png_color_struct* color ;
590 RGBColor col ;
591
592 int byte = ( i * pixel_depth ) / 8 ;
593 int offset = ( 8 - pixel_depth ) - ( i * pixel_depth ) % 8 ;
594
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);
601 }
602 }
603 }
604 else
605 {
606 for ( size_t i = 0 ; i < info_ptr->width ; ++i )
607 {
608 png_color_struct* color ;
609 RGBColor col ;
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);
615 }
616 }
617 if (number_passes)
618 iter.SetRow(row_pointers, row_stride);
619 y++;
620 }
621 while(iter.PrevRow());
622 SetGWorld( origPort , origDevice ) ;
623
624 // printf("Y=%d ",y);
625 }
626 delete[] row_pointers;
627
628 /* read the rest of the file, getting any additional chunks
629 in info_ptr */
630 png_read_end(png_ptr, info_ptr);
631
632 /* clean up after the read, and free any memory allocated */
633 png_read_destroy(png_ptr, info_ptr, (png_info *)0);
634
635 /* free the structures */
636 delete png_ptr;
637 delete info_ptr;
638
639 /* close the file */
640 fclose(fp);
641
642 /* that's it */
643 return TRUE;
644 }
645
646
647 /* write a png file */
648
649 bool wxPNGReader::SaveFile(char * ImageFileName)
650 {
651 if (ImageFileName)
652 strcpy(filename, ImageFileName);
653
654 wxPNGReaderIter iter(this);
655 FILE *fp;
656 png_struct *png_ptr;
657 png_info *info_ptr;
658
659 /* open the file */
660 fp = fopen(filename, "wb");
661 if (!fp)
662 return FALSE;
663
664 /* allocate the necessary structures */
665 png_ptr = new (png_struct);
666 if (!png_ptr)
667 {
668 fclose(fp);
669 return FALSE;
670 }
671
672 info_ptr = new (png_info);
673 if (!info_ptr)
674 {
675 fclose(fp);
676 delete png_ptr;
677 return FALSE;
678 }
679
680 /* set error handling */
681 if (setjmp(png_ptr->jmpbuf))
682 {
683 png_write_destroy(png_ptr);
684 fclose(fp);
685 delete png_ptr;
686 delete info_ptr;
687
688 /* If we get here, we had a problem reading the file */
689 return FALSE;
690 }
691 //png_set_error(ima_png_error, NULL);
692
693 // printf("writig pg %s ", filename);
694 /* initialize the structures */
695 png_info_init(info_ptr);
696 png_write_init(png_ptr);
697
698 int row_stride = GetWidth() * ((GetDepth()+7)>>3);
699 /* set up the output control */
700 png_init_io(png_ptr, fp);
701
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;
710 info_ptr->valid = 0;
711 info_ptr->rowbytes = row_stride;
712
713
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())
717 {
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);
724 }
725 // printf("Paleta [%d %d %x]",GetColorType() ,COLORTYPE_PALETTE, GetPalette());
726
727
728 /* optional significant bit chunk */
729 // info_ptr->valid |= PNG_INFO_sBIT;
730 // info_ptr->sig_bit = true_bit_depth;
731
732 /* optional gamma chunk */
733 // info_ptr->valid |= PNG_INFO_gAMA;
734 // info_ptr->gamma = gamma;
735
736 /* other optional chunks */
737
738 /* write the file information */
739 png_write_info(png_ptr, info_ptr);
740
741 /* set up the transformations you want. Note that these are
742 all optional. Only call them if you want them */
743
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));
747
748 /* pack pixels into bytes */
749 // png_set_packing(png_ptr);
750
751 /* flip bgr pixels to rgb */
752 // png_set_bgr(png_ptr);
753
754 /* swap bytes of 16 bit files to most significant bit first */
755 // png_set_swap(png_ptr);
756
757 /* get rid of filler bytes, pack rgb into 3 bytes */
758 // png_set_rgbx(png_ptr);
759
760 /* If you are only writing one row at a time, this works */
761
762 byte *row_pointers = new byte[row_stride];
763 iter.upset();
764 do {
765 // (unsigned char *)iter.GetRow();
766 iter.GetRow(row_pointers, row_stride);
767 png_write_row(png_ptr, row_pointers);
768 } while(iter.PrevRow());
769
770 delete[] row_pointers;
771
772 /* write the rest of the file */
773 png_write_end(png_ptr, info_ptr);
774
775 /* clean up after the write, and free any memory allocated */
776 png_write_destroy(png_ptr);
777
778 /* if you malloced the palette, free it here */
779 if (info_ptr->palette)
780 delete[] (info_ptr->palette);
781
782 /* free the structures */
783 delete png_ptr;
784 delete info_ptr;
785
786 /* close the file */
787 fclose(fp);
788
789 /* that's it */
790 return TRUE;
791 }
792
793 static int Power(int x, int y)
794 {
795 int z = 1;
796 int i;
797 for ( i = 0; i < y; i++)
798 {
799 z *= x;
800 }
801 return z;
802 }
803
804 static char hexArray[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
805 'C', 'D', 'E', 'F' };
806
807 static void DecToHex(int dec, char *buf)
808 {
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];
813 buf[2] = 0;
814 }
815
816
817 bool wxPNGReader::SaveXPM(char *filename, char *name)
818 {
819 char nameStr[256];
820 if ( name )
821 strcpy(nameStr, name);
822 else
823 {
824 wxString str = wxString::FromAscii(filename) ;
825 wxStripExtension( str ) ;
826 strcpy(nameStr, str.ToAscii() );
827 }
828
829 if ( GetDepth() > 4 )
830 {
831 // Only a depth of 4 and below allowed
832 return FALSE;
833 }
834
835 if ( !GetPalette() )
836 return FALSE;
837
838 wxSTD ofstream str(filename);
839 if ( str.bad() )
840 return FALSE;
841
842 int noColours = Power(2, GetDepth());
843
844 // Output header
845 str << "/* XPM */\n";
846 str << "static char * " << nameStr << "_xpm[] = {\n";
847 str << "\"" << GetWidth() << " " << GetHeight() << " " << noColours << " 1\",\n";
848
849 // Output colourmap
850 int base = 97 ; // start from 'a'
851
852 unsigned char red, green, blue;
853 char hexBuf[4];
854 int i;
855 for ( i = 0; i < noColours; i ++)
856 {
857 str << "\"" << (char)(base + i) << " c #";
858 GetPalette()->GetRGB(i, &red, &green, &blue);
859 DecToHex(red, hexBuf);
860 str << hexBuf;
861 DecToHex(green, hexBuf);
862 str << hexBuf;
863 DecToHex(blue, hexBuf);
864 str << hexBuf;
865 str << "\",\n";
866 }
867
868 // Output the data
869 int x, y;
870 for ( y = 0; y < GetHeight(); y++)
871 {
872 str << "\"";
873 for ( x = 0; x < GetWidth(); x++)
874 {
875 int index = GetIndex(x, y);
876 str << (char)(base + index) ;
877 }
878 str << "\",\n";
879 }
880
881 str << "};\n";
882 str.flush();
883
884 return TRUE;
885 }
886
887
888 IMPLEMENT_DYNAMIC_CLASS(wxPNGFileHandler, wxBitmapHandler)
889
890 bool wxPNGFileHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
891 int desiredWidth, int desiredHeight)
892 {
893 wxPNGReader reader;
894 if (reader.ReadFile( (char*)(const char*) name.ToAscii() ) )
895 {
896 return reader.InstantiateBitmap(bitmap);
897 }
898 else
899 return FALSE;
900 }
901
902 bool wxPNGFileHandler::SaveFile(const wxBitmap *bitmap, const wxString& name, int type, const wxPalette *pal)
903 {
904 return FALSE;
905 }
906
907 #endif //wxUSE_LIBPNG