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