]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/pnghand.cpp
Darwin has commandline args.
[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 __DARWIN__
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 m_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 = m_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 m_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 m_palette;
113 delete[] RawImage ;
114 RawImage = 0;
115 m_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 m_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 (m_palette) {
158 return m_palette->GetRGB(GetIndex(x, y), r, g, b);
159 /* PALETTEENTRY entry;
160 ::GetPaletteEntries((HPALETTE) m_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 (!m_palette) return FALSE;
191 SetIndex(x, y, m_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 m_palette ;
206 if (!colourmap)
207 return FALSE;
208 ColorType |= (COLORTYPE_PALETTE | COLORTYPE_COLOR);
209 m_palette = new wxPalette( *colourmap );
210 return true ;
211 // return (DibSetUsage(lpbi, (HPALETTE) m_palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
212 }
213
214 bool
215 wxPNGReader::SetPalette(int n, byte *r, byte *g, byte *b)
216 {
217 delete m_palette ;
218 m_palette = new wxPalette();
219 if (!m_palette)
220 return FALSE;
221
222 if (!g) g = r;
223 if (!b) b = g;
224 m_palette->Create(n, r, g, b);
225 ColorType |= (COLORTYPE_PALETTE | COLORTYPE_COLOR);
226 return true ;
227 // return (DibSetUsage(lpbi, (HPALETTE) m_palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
228 }
229
230 bool
231 wxPNGReader::SetPalette(int n, rgb_color_struct *rgb_struct)
232 {
233 delete m_palette ;
234 m_palette = new wxPalette();
235 if (!m_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 m_palette->Create(n, r, g, b);
253 ColorType |= (COLORTYPE_PALETTE | COLORTYPE_COLOR);
254 return true ;
255 // return (DibSetUsage(lpbi, (HPALETTE) m_palette->GetHPALETTE(), WXIMA_COLORS ) != 0);
256 }
257
258 void wxPNGReader::NullData()
259 {
260 if (lpbi) {
261 wxMacDestroyGWorld( lpbi ) ;
262 }
263 delete m_palette;
264 lpbi = NULL;
265 m_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 && m_palette )
289 bitmap->SetPalette(*m_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 ( m_palette )
321 {
322 HPALETTE oldPal = ::SelectPalette(dc, (HPALETTE) m_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 && m_palette )
341 bitmap->SetPalette(*m_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( ImageFileName , "rb" );
418
419 if (!fp)
420 return FALSE;
421
422 /* allocate the necessary structures */
423 png_ptr = new (png_struct);
424 if (!png_ptr)
425 {
426 fclose(fp);
427 return FALSE;
428 }
429
430 info_ptr = new (png_info);
431 if (!info_ptr)
432 {
433 fclose(fp);
434 delete(png_ptr);
435 return FALSE;
436 }
437 /* set error handling */
438 if (setjmp(png_ptr->jmpbuf))
439 {
440 png_read_destroy(png_ptr, info_ptr, (png_info *)0);
441 fclose(fp);
442 delete(png_ptr);
443 delete(info_ptr);
444
445 /* If we get here, we had a problem reading the file */
446 return FALSE;
447 }
448 //png_set_error(ima_png_error, NULL);
449
450 /* initialize the structures, info first for error handling */
451 png_info_init(info_ptr);
452 png_read_init(png_ptr);
453
454 /* set up the input control */
455 png_init_io(png_ptr, fp);
456
457 /* read the file information */
458 png_read_info(png_ptr, info_ptr);
459
460 /* allocate the memory to hold the image using the fields
461 of png_info. */
462 png_color_16 my_background={ 0, 31, 127, 255, 0 };
463
464 if (info_ptr->valid & PNG_INFO_bKGD)
465 {
466 png_set_background(png_ptr, &(info_ptr->background),
467 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
468 if ( info_ptr->num_palette > 0 )
469 bgindex = info_ptr->background.index;
470 }
471 else {
472 png_set_background(png_ptr, &my_background,
473 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
474
475 // Added by JACS: guesswork!
476 if ( info_ptr->num_trans != 0 )
477 bgindex = info_ptr->num_trans - 1 ;
478 }
479
480 /* tell libpng to strip 16 bit depth files down to 8 bits */
481 if (info_ptr->bit_depth == 16)
482 png_set_strip_16(png_ptr);
483
484 int pixel_depth=(info_ptr->pixel_depth<24) ? info_ptr->pixel_depth: 24;
485 Create(info_ptr->width, info_ptr->height, pixel_depth,
486 info_ptr->color_type);
487
488 if (info_ptr->num_palette>0)
489 {
490 SetPalette((int)info_ptr->num_palette, (rgb_color_struct*)info_ptr->palette);
491 }
492
493 int row_stride = info_ptr->width * ((pixel_depth+7)>>3);
494 // printf("P = %d D = %d RS= %d ", info_ptr->num_palette, info_ptr->pixel_depth,row_stride);
495 // printf("CT = %d TRS = %d BD= %d ", info_ptr->color_type, info_ptr->valid & PNG_INFO_tRNS,info_ptr->bit_depth);
496
497 byte *row_pointers = new byte[row_stride];
498
499 /* turn on interlace handling */
500 if (info_ptr->interlace_type)
501 number_passes = png_set_interlace_handling(png_ptr);
502 else
503 number_passes = 1;
504 // printf("NP = %d ", number_passes);
505
506 for (int pass=0; pass< number_passes; pass++)
507 {
508 iter.upset();
509 int y=0;
510 CGrafPtr origPort ;
511 GDHandle origDevice ;
512
513 GetGWorld( &origPort , &origDevice ) ;
514 // ignore shapedc
515 SetGWorld( lpbi , NULL ) ;
516 do
517 {
518 // (unsigned char *)iter.GetRow();
519 if (info_ptr->interlace_type)
520 {
521 if (pass>0)
522 iter.GetRow(row_pointers, row_stride);
523 png_read_row(png_ptr, row_pointers, NULL);
524 }
525 else
526 png_read_row(png_ptr, row_pointers, NULL);
527
528 if ( info_ptr->palette )
529 {
530 if ( pixel_depth == 8 )
531 {
532 for ( int i = 0 ; i < info_ptr->width ; ++i )
533 {
534 png_color_struct* color ;
535 RGBColor col ;
536
537 int index = row_pointers[i] ;
538 color = &info_ptr->palette[index] ;
539 col.red = (((int)color->red) << 8) | ((int)color->red) ;
540 col.green = (((int)color->green) << 8) | ((int)color->green) ;
541 col.blue = (((int)color->blue) << 8) | ((int)color->blue) ;
542 SetCPixel( i, y, &col);
543 }
544 /*
545 png_color_struct* color ;
546 RGBColor col ;
547 unsigned char* p = &row_pointers[0] ;
548 PenNormal() ;
549 MoveTo( 0 , y ) ;
550 int index = *p ;
551 color = &info_ptr->palette[index] ;
552 col.red = (color->red << 8) | color->red ;
553 col.green = (color->green << 8) | color->green ;
554 col.blue = (color->blue << 8) | color->blue ;
555 RGBForeColor( &col ) ;
556 col.red = col.green = col.blue = 0xFFFF ;
557 RGBBackColor( &col ) ;
558 for ( int i = 0 ; i < info_ptr->width ; ++i , ++p)
559 {
560 if ( *p != index )
561 {
562 LineTo( i , y ) ;
563 index = *p ;
564 color = &info_ptr->palette[index] ;
565 col.red = (((int)color->red) << 8) | ((int)color->red) ;
566 col.green = (((int)color->green) << 8) | ((int)color->green) ;
567 col.blue = (((int)color->blue) << 8) | ((int)color->blue) ;
568 RGBForeColor( &col ) ;
569 }
570 }
571 LineTo( info_ptr->width , y ) ;
572 */
573 }
574 else
575 {
576 for ( int i = 0 ; i < info_ptr->width ; ++i )
577 {
578 png_color_struct* color ;
579 RGBColor col ;
580
581 int byte = ( i * pixel_depth ) / 8 ;
582 int offset = ( 8 - pixel_depth ) - ( i * pixel_depth ) % 8 ;
583
584 int index = ( row_pointers[byte] >> offset ) & ( 0xFF >> ( 8 - pixel_depth ) );
585 color = &info_ptr->palette[index] ;
586 col.red = (((int)color->red) << 8) | ((int)color->red) ;
587 col.green = (((int)color->green) << 8) | ((int)color->green) ;
588 col.blue = (((int)color->blue) << 8) | ((int)color->blue) ;
589 SetCPixel( i, y, &col);
590 }
591 }
592 }
593 else
594 {
595 for ( int i = 0 ; i < info_ptr->width ; ++i )
596 {
597 png_color_struct* color ;
598 RGBColor col ;
599 color =(png_color_struct*) (&row_pointers[i*3]) ;
600 col.red = (((int)color->red) << 8) | ((int)color->red) ;
601 col.green = (((int)color->green) << 8) | ((int)color->green) ;
602 col.blue = (((int)color->blue) << 8) | ((int)color->blue) ;
603 SetCPixel( i, y, &col);
604 }
605 }
606 if (number_passes)
607 iter.SetRow(row_pointers, row_stride);
608 y++;
609 }
610 while(iter.PrevRow());
611 SetGWorld( origPort , origDevice ) ;
612
613 // printf("Y=%d ",y);
614 }
615 delete[] row_pointers;
616
617 /* read the rest of the file, getting any additional chunks
618 in info_ptr */
619 png_read_end(png_ptr, info_ptr);
620
621 /* clean up after the read, and free any memory allocated */
622 png_read_destroy(png_ptr, info_ptr, (png_info *)0);
623
624 /* free the structures */
625 delete(png_ptr);
626 delete(info_ptr);
627
628 /* close the file */
629 fclose(fp);
630
631 /* that's it */
632 return TRUE;
633 }
634
635
636 /* write a png file */
637
638 bool wxPNGReader::SaveFile(char * ImageFileName)
639 {
640 if (ImageFileName)
641 strcpy(filename, ImageFileName);
642
643 wxPNGReaderIter iter(this);
644 FILE *fp;
645 png_struct *png_ptr;
646 png_info *info_ptr;
647
648 /* open the file */
649 fp = fopen(filename, "wb");
650 if (!fp)
651 return FALSE;
652
653 /* allocate the necessary structures */
654 png_ptr = new (png_struct);
655 if (!png_ptr)
656 {
657 fclose(fp);
658 return FALSE;
659 }
660
661 info_ptr = new (png_info);
662 if (!info_ptr)
663 {
664 fclose(fp);
665 delete(png_ptr);
666 return FALSE;
667 }
668
669 /* set error handling */
670 if (setjmp(png_ptr->jmpbuf))
671 {
672 png_write_destroy(png_ptr);
673 fclose(fp);
674 delete(png_ptr);
675 delete(info_ptr);
676
677 /* If we get here, we had a problem reading the file */
678 return FALSE;
679 }
680 //png_set_error(ima_png_error, NULL);
681
682 // printf("writig pg %s ", filename);
683 /* initialize the structures */
684 png_info_init(info_ptr);
685 png_write_init(png_ptr);
686
687 int row_stride = GetWidth() * ((GetDepth()+7)>>3);
688 /* set up the output control */
689 png_init_io(png_ptr, fp);
690
691 /* set the file information here */
692 info_ptr->width = GetWidth();
693 info_ptr->height = GetHeight();
694 info_ptr->pixel_depth = GetDepth();
695 info_ptr->channels = (GetDepth()>8) ? 3: 1;
696 info_ptr->bit_depth = GetDepth()/info_ptr->channels;
697 info_ptr->color_type = GetColorType();
698 info_ptr->compression_type = info_ptr->filter_type = info_ptr->interlace_type=0;
699 info_ptr->valid = 0;
700 info_ptr->rowbytes = row_stride;
701
702
703 // 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);
704 /* set the palette if there is one */
705 if ((GetColorType() & COLORTYPE_PALETTE) && GetPalette())
706 {
707 // printf("writing paleta[%d %d %x]",GetColorType() ,COLORTYPE_PALETTE, GetPalette());
708 info_ptr->valid |= PNG_INFO_PLTE;
709 info_ptr->palette = new png_color[256];
710 info_ptr->num_palette = 256;
711 for (int i=0; i<256; i++)
712 GetPalette()->GetRGB(i, &info_ptr->palette[i].red, &info_ptr->palette[i].green, &info_ptr->palette[i].blue);
713 }
714 // printf("Paleta [%d %d %x]",GetColorType() ,COLORTYPE_PALETTE, GetPalette());
715
716
717 /* optional significant bit chunk */
718 // info_ptr->valid |= PNG_INFO_sBIT;
719 // info_ptr->sig_bit = true_bit_depth;
720
721 /* optional gamma chunk */
722 // info_ptr->valid |= PNG_INFO_gAMA;
723 // info_ptr->gamma = gamma;
724
725 /* other optional chunks */
726
727 /* write the file information */
728 png_write_info(png_ptr, info_ptr);
729
730 /* set up the transformations you want. Note that these are
731 all optional. Only call them if you want them */
732
733 /* shift the pixels up to a legal bit depth and fill in
734 as appropriate to correctly scale the image */
735 // png_set_shift(png_ptr, &(info_ptr->sig_bit));
736
737 /* pack pixels into bytes */
738 // png_set_packing(png_ptr);
739
740 /* flip bgr pixels to rgb */
741 // png_set_bgr(png_ptr);
742
743 /* swap bytes of 16 bit files to most significant bit first */
744 // png_set_swap(png_ptr);
745
746 /* get rid of filler bytes, pack rgb into 3 bytes */
747 // png_set_rgbx(png_ptr);
748
749 /* If you are only writing one row at a time, this works */
750
751 byte *row_pointers = new byte[row_stride];
752 iter.upset();
753 do {
754 // (unsigned char *)iter.GetRow();
755 iter.GetRow(row_pointers, row_stride);
756 png_write_row(png_ptr, row_pointers);
757 } while(iter.PrevRow());
758
759 delete[] row_pointers;
760
761 /* write the rest of the file */
762 png_write_end(png_ptr, info_ptr);
763
764 /* clean up after the write, and free any memory allocated */
765 png_write_destroy(png_ptr);
766
767 /* if you malloced the palette, free it here */
768 if (info_ptr->palette)
769 delete[] (info_ptr->palette);
770
771 /* free the structures */
772 delete(png_ptr);
773 delete(info_ptr);
774
775 /* close the file */
776 fclose(fp);
777
778 /* that's it */
779 return TRUE;
780 }
781
782 static int Power(int x, int y)
783 {
784 int z = 1;
785 int i;
786 for ( i = 0; i < y; i++)
787 {
788 z *= x;
789 }
790 return z;
791 }
792
793 static char hexArray[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
794 'C', 'D', 'E', 'F' };
795
796 static void DecToHex(int dec, char *buf)
797 {
798 int firstDigit = (int)(dec/16.0);
799 int secondDigit = (int)(dec - (firstDigit*16.0));
800 buf[0] = hexArray[firstDigit];
801 buf[1] = hexArray[secondDigit];
802 buf[2] = 0;
803 }
804
805
806 bool wxPNGReader::SaveXPM(char *filename, char *name)
807 {
808 char nameStr[256];
809 if ( name )
810 strcpy(nameStr, name);
811 else
812 {
813 strcpy(nameStr, filename);
814 wxStripExtension(nameStr);
815 }
816
817 if ( GetDepth() > 4 )
818 {
819 // Only a depth of 4 and below allowed
820 return FALSE;
821 }
822
823 if ( !GetPalette() )
824 return FALSE;
825
826 wxSTD ofstream str(filename);
827 if ( str.bad() )
828 return FALSE;
829
830 int noColours = Power(2, GetDepth());
831
832 // Output header
833 str << "/* XPM */\n";
834 str << "static char * " << nameStr << "_xpm[] = {\n";
835 str << "\"" << GetWidth() << " " << GetHeight() << " " << noColours << " 1\",\n";
836
837 // Output colourmap
838 int base = 97 ; // start from 'a'
839
840 unsigned char red, green, blue;
841 char hexBuf[4];
842 int i;
843 for ( i = 0; i < noColours; i ++)
844 {
845 str << "\"" << (char)(base + i) << " c #";
846 GetPalette()->GetRGB(i, &red, &green, &blue);
847 DecToHex(red, hexBuf);
848 str << hexBuf;
849 DecToHex(green, hexBuf);
850 str << hexBuf;
851 DecToHex(blue, hexBuf);
852 str << hexBuf;
853 str << "\",\n";
854 }
855
856 // Output the data
857 int x, y;
858 for ( y = 0; y < GetHeight(); y++)
859 {
860 str << "\"";
861 for ( x = 0; x < GetWidth(); x++)
862 {
863 int index = GetIndex(x, y);
864 str << (char)(base + index) ;
865 }
866 str << "\",\n";
867 }
868
869 str << "};\n";
870 str.flush();
871
872 return TRUE;
873 }
874
875
876 IMPLEMENT_DYNAMIC_CLASS(wxPNGFileHandler, wxBitmapHandler)
877
878 bool wxPNGFileHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
879 int desiredWidth, int desiredHeight)
880 {
881 wxPNGReader reader;
882 if (reader.ReadFile((char*) (const char*) name))
883 {
884 return reader.InstantiateBitmap(bitmap);
885 }
886 else
887 return FALSE;
888 }
889
890 bool wxPNGFileHandler::SaveFile(const wxBitmap *bitmap, const wxString& name, int type, const wxPalette *pal)
891 {
892 return FALSE;
893 }
894
895