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