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