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