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