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