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