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