]>
Commit | Line | Data |
---|---|---|
63415778 DW |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: pnghand.cpp | |
3 | // Purpose: Implements a PNG reader class + handler | |
4 | // Author: David Webster | |
5 | // Modified by: | |
6 | // Created: 10/10/99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) David Webster | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #include <stdlib.h> | |
16 | #include <stdio.h> | |
17 | #include <string.h> | |
18 | ||
19 | #if wxUSE_IOSTREAMH | |
20 | # include <fstream.h> | |
21 | #else | |
22 | # include <fstream> | |
23 | #endif | |
24 | ||
25 | #define INCL_DOS | |
26 | #define INCL_PM | |
27 | #define INCL_GPI | |
28 | #include <os2.h> | |
29 | #include "wx/palette.h" | |
30 | #include "wx/bitmap.h" | |
31 | #include "wx/utils.h" | |
fb46a9a6 | 32 | #include "wx/os2/pngread.h" |
63415778 DW |
33 | |
34 | extern "C" { | |
35 | #include "../png/png.h" | |
36 | } | |
37 | ||
38 | extern "C" void png_read_init PNGARG((png_structp png_ptr)); | |
39 | extern "C" void png_write_init PNGARG((png_structp png_ptr)); | |
40 | ||
41 | #ifndef GlobalAllocPtr | |
42 | #define GlobalPtrHandle(lp) \ | |
43 | ((HGLOBAL)GlobalHandle(lp)) | |
44 | ||
45 | #define GlobalLockPtr(lp) \ | |
46 | ((BOOL)GlobalLock(GlobalPtrHandle(lp))) | |
47 | #define GlobalUnlockPtr(lp) \ | |
48 | GlobalUnlock(GlobalPtrHandle(lp)) | |
49 | ||
50 | #define GlobalAllocPtr(flags, cb) \ | |
51 | (GlobalLock(GlobalAlloc((flags), (cb)))) | |
52 | #define GlobalReAllocPtr(lp, cbNew, flags) \ | |
53 | (GlobalUnlockPtr(lp), GlobalLock(GlobalReAlloc(GlobalPtrHandle(lp) , (cbNew), (flags)))) | |
54 | #define GlobalFreePtr(lp) \ | |
55 | (GlobalUnlockPtr(lp), (BOOL)GlobalFree(GlobalPtrHandle(lp))) | |
56 | #endif | |
57 | ||
58 | ||
59 | void | |
60 | ima_png_error(png_struct *png_ptr, char *message) | |
61 | { | |
62 | // wxMessageBox(message, "PNG error"); | |
63 | ||
64 | longjmp(png_ptr->jmpbuf, 1); | |
65 | } | |
66 | ||
67 | ||
68 | // static wxGifReaderIter* iter; | |
69 | wxPalette *wxCopyPalette(const wxPalette *cmap); | |
70 | ||
71 | wxPNGReader::wxPNGReader() | |
72 | { | |
73 | filetype = 0; | |
74 | RawImage = NULL; // Image data | |
75 | ||
76 | Width = 0; Height = 0; // Dimensions | |
77 | Depth = 0; // (bits x pixel) | |
78 | ColorType = 0; // Bit 1 = Palette used | |
79 | // Bit 2 = Color used | |
80 | // Bit 3 = Alpha used | |
81 | ||
82 | EfeWidth = 0; // Efective Width | |
83 | ||
84 | lpbi = NULL; | |
85 | bgindex = -1; | |
86 | Palette = 0; | |
87 | imageOK = FALSE; | |
88 | } | |
89 | ||
90 | wxPNGReader::wxPNGReader ( wxChar* ImageFileName ) | |
91 | { | |
92 | imageOK = FALSE; | |
93 | filetype = 0; | |
94 | RawImage = NULL; // Image data | |
95 | ||
96 | Width = 0; Height = 0; // Dimensions | |
97 | Depth = 0; // (bits x pixel) | |
98 | ColorType = 0; // Bit 1 = Palette used | |
99 | // Bit 2 = Color used | |
100 | // Bit 3 = Alpha used | |
101 | ||
102 | EfeWidth = 0; // Efective Width | |
103 | ||
104 | lpbi = NULL; | |
105 | bgindex = -1; | |
106 | Palette = 0; | |
107 | ||
108 | imageOK = ReadFile (ImageFileName); | |
109 | } | |
110 | ||
111 | void | |
112 | wxPNGReader::Create(int width, int height, int depth, int colortype) | |
113 | { | |
114 | Width = width; Height = height; Depth = depth; | |
115 | ColorType = (colortype>=0) ? colortype: ((Depth>8) ? COLORTYPE_COLOR: 0); | |
116 | ||
117 | if (lpbi) { | |
fb46a9a6 | 118 | // GlobalFreePtr(lpbi); |
63415778 DW |
119 | // delete Palette; |
120 | } | |
121 | RawImage = 0; | |
122 | Palette = 0; | |
fb46a9a6 | 123 | lpbi = 0; // TODO: wxDibCreate(Depth, Width, Height); |
63415778 | 124 | if (lpbi) { |
fb46a9a6 | 125 | RawImage = 0; //TODO: (ImagePointerType)wxDibPtr(lpbi); |
63415778 DW |
126 | EfeWidth = (long)(((long)Width*Depth + 31) / 32) * 4; |
127 | imageOK = TRUE; | |
128 | } | |
129 | } | |
130 | ||
131 | wxPNGReader::~wxPNGReader ( ) | |
132 | { | |
133 | if (lpbi) { | |
fb46a9a6 | 134 | // TODO: GlobalFreePtr(lpbi); |
63415778 DW |
135 | delete Palette; |
136 | } | |
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 | if (!colourmap) | |
202 | return FALSE; | |
203 | ColorType |= (COLORTYPE_PALETTE | COLORTYPE_COLOR); | |
204 | Palette = colourmap; | |
fb46a9a6 DW |
205 | // TODO: return (wxDibSetUsage(lpbi, (HPALETTE) Palette->GetHPALETTE(), WXIMA_COLORS ) != 0); |
206 | return FALSE; | |
63415778 DW |
207 | } |
208 | ||
209 | bool | |
210 | wxPNGReader::SetPalette(int n, byte *r, byte *g, byte *b) | |
211 | { | |
212 | Palette = new wxPalette(); | |
213 | if (!Palette) | |
214 | return FALSE; | |
215 | ||
216 | if (!g) g = r; | |
217 | if (!b) b = g; | |
218 | Palette->Create(n, r, g, b); | |
219 | ColorType |= (COLORTYPE_PALETTE | COLORTYPE_COLOR); | |
fb46a9a6 DW |
220 | // TODO: return (wxDibSetUsage(lpbi, (HPALETTE) Palette->GetHPALETTE(), WXIMA_COLORS ) != 0); |
221 | return FALSE; | |
63415778 DW |
222 | } |
223 | ||
224 | bool | |
225 | wxPNGReader::SetPalette(int n, rgb_color_struct *rgb_struct) | |
226 | { | |
227 | Palette = new wxPalette(); | |
228 | if (!Palette) | |
229 | return FALSE; | |
230 | ||
231 | byte r[256], g[256], b[256]; | |
232 | ||
233 | for(int i=0; i<n; i++) | |
234 | { | |
235 | r[i] = rgb_struct[i].red; | |
236 | g[i] = rgb_struct[i].green; | |
237 | b[i] = rgb_struct[i].blue; | |
238 | } | |
239 | // Added by JACS copying from Andrew Davison's additions | |
240 | // to GIF-reading code | |
241 | // Make transparency colour black... | |
242 | if (bgindex != -1) | |
243 | r[bgindex] = g[bgindex] = b[bgindex] = 0; | |
244 | ||
245 | Palette->Create(n, r, g, b); | |
246 | ColorType |= (COLORTYPE_PALETTE | COLORTYPE_COLOR); | |
fb46a9a6 DW |
247 | // TODO: return (wxDibSetUsage(lpbi, (HPALETTE) Palette->GetHPALETTE(), WXIMA_COLORS ) != 0); |
248 | return FALSE; | |
63415778 DW |
249 | } |
250 | ||
251 | void wxPNGReader::NullData() | |
252 | { | |
253 | lpbi = NULL; | |
254 | Palette = NULL; | |
255 | } | |
256 | ||
257 | wxBitmap* wxPNGReader::GetBitmap() | |
258 | { | |
259 | wxBitmap *bitmap = new wxBitmap; | |
260 | if ( InstantiateBitmap(bitmap) ) | |
261 | return bitmap; | |
262 | else | |
263 | { | |
264 | delete bitmap; | |
265 | return NULL; | |
266 | } | |
267 | } | |
268 | ||
269 | bool wxPNGReader::InstantiateBitmap(wxBitmap *bitmap) | |
270 | { | |
fb46a9a6 DW |
271 | // TODO: |
272 | /* | |
63415778 DW |
273 | HDC dc = ::CreateCompatibleDC(NULL); |
274 | ||
275 | if (dc) | |
276 | { | |
277 | // tmpBitmap is a dummy, to satisfy ::CreateCompatibleDC (it | |
278 | // is a memory dc that must have a bitmap selected into it) | |
279 | HDC dc2 = GetDC(NULL); | |
280 | HBITMAP tmpBitmap = ::CreateCompatibleBitmap(dc2, GetWidth(), GetHeight()); | |
281 | ReleaseDC(NULL, dc2); | |
282 | HBITMAP oldBitmap = (HBITMAP) ::SelectObject(dc, tmpBitmap); | |
283 | ||
284 | if ( Palette ) | |
285 | { | |
286 | ::SelectPalette(dc, (HPALETTE) Palette->GetHPALETTE(), FALSE); | |
287 | ::RealizePalette(dc); | |
288 | } | |
289 | ||
290 | HBITMAP hBitmap = ::CreateDIBitmap(dc, lpbi, | |
291 | CBM_INIT, RawImage, (LPBITMAPINFO) lpbi, DIB_PAL_COLORS); | |
292 | ||
293 | ::SelectPalette(dc, NULL, TRUE); | |
294 | ::SelectObject(dc, oldBitmap); | |
295 | ::DeleteObject(tmpBitmap); | |
296 | ::DeleteDC(dc); | |
297 | ||
298 | if ( hBitmap ) | |
299 | { | |
300 | bitmap->SetHBITMAP((WXHBITMAP) hBitmap); | |
301 | bitmap->SetWidth(GetWidth()); | |
302 | bitmap->SetHeight(GetHeight()); | |
303 | bitmap->SetDepth(GetDepth()); | |
304 | if ( GetDepth() > 1 && Palette ) | |
305 | bitmap->SetPalette(*Palette); | |
306 | bitmap->SetOk(TRUE); | |
307 | ||
308 | ||
309 | // Make a mask if appropriate | |
310 | if ( bgindex > -1 ) | |
311 | { | |
312 | wxMask *mask = CreateMask(); | |
313 | bitmap->SetMask(mask); | |
314 | } | |
315 | return TRUE; | |
316 | } | |
317 | else | |
318 | { | |
319 | return FALSE; | |
320 | } | |
321 | } | |
322 | else | |
323 | { | |
324 | return FALSE; | |
325 | } | |
fb46a9a6 DW |
326 | */ |
327 | return FALSE; | |
63415778 DW |
328 | } |
329 | ||
330 | wxPalette *wxCopyPalette(const wxPalette *cmap) | |
331 | { | |
332 | // To get number of entries... | |
333 | WORD count = 0; | |
fb46a9a6 DW |
334 | // TODO: ::GetObject((HPALETTE) cmap->GetHPALETTE(), sizeof(WORD), &count); |
335 | // TODO: | |
336 | /* | |
63415778 DW |
337 | LOGPALETTE* logPal = (LOGPALETTE*) |
338 | new BYTE[sizeof(LOGPALETTE) + count*sizeof(PALETTEENTRY)]; | |
339 | logPal->palVersion = 0x300; | |
340 | logPal->palNumEntries = count; | |
341 | ::GetPaletteEntries((HPALETTE) cmap->GetHPALETTE(), 0, count, logPal->palPalEntry); | |
342 | ||
343 | HPALETTE hPalette = ::CreatePalette(logPal); | |
344 | delete[] logPal; | |
fb46a9a6 | 345 | */ |
63415778 | 346 | wxPalette *newCmap = new wxPalette; |
fb46a9a6 | 347 | // TODO: newCmap->SetHPALETTE((WXHPALETTE) hPalette); |
63415778 DW |
348 | return newCmap; |
349 | } | |
350 | ||
351 | wxMask *wxPNGReader::CreateMask() | |
352 | { | |
fb46a9a6 DW |
353 | // TODO: |
354 | /* | |
63415778 DW |
355 | HBITMAP hBitmap = ::CreateBitmap(GetWidth(), GetHeight(), 1, 1, NULL); |
356 | ||
357 | HDC dc = ::CreateCompatibleDC(NULL); | |
358 | HBITMAP oldBitmap = (HBITMAP) ::SelectObject(dc, hBitmap); | |
359 | ||
360 | int bgIndex = GetBGIndex(); | |
361 | ||
362 | int x,y; | |
363 | ||
364 | for (x=0; x<GetWidth(); x++) | |
365 | { | |
366 | for (y=0; y<GetHeight(); y++) | |
367 | { | |
368 | int index = GetIndex(x, y); | |
369 | if ( index == bgIndex ) | |
370 | ::SetPixel(dc, x, GetHeight() - y - 1, RGB(0, 0, 0)); | |
371 | else | |
372 | ::SetPixel(dc, x, GetHeight() - y - 1, RGB(255, 255, 255)); | |
373 | ||
374 | } | |
375 | } | |
376 | ::SelectObject(dc, oldBitmap); | |
fb46a9a6 | 377 | */ |
63415778 | 378 | wxMask *mask = new wxMask; |
fb46a9a6 | 379 | // TODO: mask->SetMaskBitmap((WXHBITMAP) hBitmap); |
63415778 DW |
380 | return mask; |
381 | } | |
382 | ||
383 | bool wxPNGReader::ReadFile(wxChar * ImageFileName) | |
384 | { | |
385 | if (ImageFileName) | |
386 | wxStrcpy(filename, ImageFileName); | |
387 | ||
388 | /* open the file */ | |
389 | FILE *fp = fopen(wxConvFile.cWX2MB(filename), "rb"); | |
390 | if (!fp) | |
391 | return FALSE; | |
392 | ||
393 | /* allocate the necessary structures */ | |
394 | png_struct *png_ptr = new (png_struct); | |
395 | if (!png_ptr) | |
396 | { | |
397 | fclose(fp); | |
398 | return FALSE; | |
399 | } | |
400 | ||
401 | png_info *info_ptr = new (png_info); | |
402 | if (!info_ptr) | |
403 | { | |
404 | fclose(fp); | |
405 | delete(png_ptr); | |
406 | return FALSE; | |
407 | } | |
408 | ||
409 | /* set error handling */ | |
410 | if (setjmp(png_ptr->jmpbuf)) | |
411 | { | |
412 | png_read_destroy(png_ptr, info_ptr, (png_info *)0); | |
413 | fclose(fp); | |
414 | delete(png_ptr); | |
415 | delete(info_ptr); | |
416 | ||
417 | /* If we get here, we had a problem reading the file */ | |
418 | return FALSE; | |
419 | } | |
420 | //png_set_error(ima_png_error, NULL); | |
421 | ||
422 | /* initialize the structures, info first for error handling */ | |
423 | png_info_init(info_ptr); | |
424 | png_read_init(png_ptr); | |
425 | ||
426 | /* set up the input control */ | |
427 | png_init_io(png_ptr, fp); | |
428 | ||
429 | /* read the file information */ | |
430 | png_read_info(png_ptr, info_ptr); | |
431 | ||
432 | /* allocate the memory to hold the image using the fields | |
433 | of png_info. */ | |
434 | png_color_16 my_background={ 0, 31, 127, 255, 0 }; | |
435 | ||
436 | if (info_ptr->valid & PNG_INFO_bKGD) | |
437 | { | |
438 | png_set_background(png_ptr, &(info_ptr->background), | |
439 | PNG_BACKGROUND_GAMMA_FILE, 1, 1.0); | |
440 | if ( info_ptr->num_palette > 0 ) | |
441 | bgindex = info_ptr->background.index; | |
442 | } | |
443 | else { | |
444 | png_set_background(png_ptr, &my_background, | |
445 | PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0); | |
446 | ||
447 | // Added by JACS: guesswork! | |
448 | if ( info_ptr->num_trans != 0 ) | |
449 | bgindex = info_ptr->num_trans - 1 ; | |
450 | } | |
451 | ||
452 | /* tell libpng to strip 16 bit depth files down to 8 bits */ | |
453 | if (info_ptr->bit_depth == 16) | |
454 | png_set_strip_16(png_ptr); | |
455 | ||
456 | int pixel_depth=(info_ptr->pixel_depth<24) ? info_ptr->pixel_depth: 24; | |
457 | Create(info_ptr->width, info_ptr->height, pixel_depth, | |
458 | info_ptr->color_type); | |
459 | ||
460 | if (info_ptr->num_palette>0) | |
461 | { | |
462 | SetPalette((int)info_ptr->num_palette, (rgb_color_struct*)info_ptr->palette); | |
463 | } | |
464 | ||
465 | int row_stride = info_ptr->width * ((pixel_depth+7)>>3); | |
466 | // printf("P = %d D = %d RS= %d ", info_ptr->num_palette, info_ptr->pixel_depth,row_stride); | |
467 | // printf("CT = %d TRS = %d BD= %d ", info_ptr->color_type, info_ptr->valid & PNG_INFO_tRNS,info_ptr->bit_depth); | |
468 | ||
469 | byte *row_pointers = new byte[row_stride]; | |
470 | ||
471 | /* turn on interlace handling */ | |
472 | int number_passes; | |
473 | if (info_ptr->interlace_type) | |
474 | number_passes = png_set_interlace_handling(png_ptr); | |
475 | else | |
476 | number_passes = 1; | |
477 | // printf("NP = %d ", number_passes); | |
478 | ||
479 | // don't use the object to prevent warnings from VC++ about "unportable | |
480 | // interaction between setjmp and C++ object destruction" (this is a correct | |
481 | // warning, of course!) | |
482 | wxPNGReaderIter *iter = new wxPNGReaderIter(this); | |
483 | for (int pass=0; pass< number_passes; pass++) | |
484 | { | |
485 | iter->upset(); | |
486 | int y=0; | |
487 | do { | |
488 | //(unsigned char *)iter.GetRow(); | |
489 | if (info_ptr->interlace_type) { | |
490 | if (pass>0) | |
491 | iter->GetRow(row_pointers, row_stride); | |
492 | png_read_row(png_ptr, row_pointers, NULL); | |
493 | } | |
494 | else | |
495 | png_read_row(png_ptr, row_pointers, NULL); | |
496 | ||
497 | iter->SetRow(row_pointers, row_stride); | |
498 | y++; | |
499 | } while(iter->PrevRow()); | |
500 | // printf("Y=%d ",y); | |
501 | } | |
502 | ||
503 | delete iter; | |
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; | |
522 | } | |
523 | ||
524 | ||
525 | /* write a png file */ | |
526 | ||
527 | bool wxPNGReader::SaveFile(wxChar * ImageFileName) | |
528 | { | |
529 | if (ImageFileName) | |
530 | wxStrcpy(filename, ImageFileName); | |
531 | ||
532 | wxPNGReaderIter iter(this); | |
533 | FILE *fp; | |
534 | png_struct *png_ptr; | |
535 | png_info *info_ptr; | |
536 | ||
537 | /* open the file */ | |
538 | fp = fopen(wxConvFile.cWX2MB(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 | } | |
569 | //png_set_error(ima_png_error, NULL); | |
570 | ||
571 | // printf("writig pg %s ", filename); | |
572 | /* initialize the structures */ | |
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 */ | |
578 | png_init_io(png_ptr, fp); | |
579 | ||
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; | |
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); | |
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()); | |
604 | ||
605 | ||
606 | /* optional significant bit chunk */ | |
607 | // info_ptr->valid |= PNG_INFO_sBIT; | |
608 | // info_ptr->sig_bit = true_bit_depth; | |
609 | ||
610 | /* optional gamma chunk */ | |
611 | // info_ptr->valid |= PNG_INFO_gAMA; | |
612 | // info_ptr->gamma = gamma; | |
613 | ||
614 | /* other optional chunks */ | |
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 | ||
622 | /* shift the pixels up to a legal bit depth and fill in | |
623 | as appropriate to correctly scale the image */ | |
624 | // png_set_shift(png_ptr, &(info_ptr->sig_bit)); | |
625 | ||
626 | /* pack pixels into bytes */ | |
627 | // png_set_packing(png_ptr); | |
628 | ||
629 | /* flip bgr pixels to rgb */ | |
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 | ||
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 | ||
648 | delete[] row_pointers; | |
649 | ||
650 | /* write the rest of the file */ | |
651 | png_write_end(png_ptr, info_ptr); | |
652 | ||
653 | /* clean up after the write, and free any memory allocated */ | |
654 | png_write_destroy(png_ptr); | |
655 | ||
656 | /* if you malloced the palette, free it here */ | |
657 | if (info_ptr->palette) | |
658 | delete[] (info_ptr->palette); | |
659 | ||
660 | /* free the structures */ | |
661 | delete(png_ptr); | |
662 | delete(info_ptr); | |
663 | ||
664 | /* close the file */ | |
665 | fclose(fp); | |
666 | ||
667 | /* that's it */ | |
668 | return TRUE; | |
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 | } | |
693 | ||
694 | ||
695 | bool wxPNGReader::SaveXPM(wxChar *filename, wxChar *name) | |
696 | { | |
697 | wxChar nameStr[256]; | |
698 | if ( name ) | |
699 | wxStrcpy(nameStr, name); | |
700 | else | |
701 | { | |
702 | wxStrcpy(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(wxConvFile.cWX2MB(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/os2/pnghand.h> | |
765 | ||
766 | IMPLEMENT_DYNAMIC_CLASS(wxPNGFileHandler, wxBitmapHandler) | |
767 | ||
768 | bool wxPNGFileHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags, | |
769 | int desiredWidth, int desiredHeight) | |
770 | { | |
771 | wxPNGReader reader; | |
772 | if (reader.ReadFile(WXSTRINGCAST name)) | |
773 | { | |
774 | return reader.InstantiateBitmap(bitmap); | |
775 | } | |
776 | else | |
777 | return FALSE; | |
778 | } | |
779 | ||
780 | bool wxPNGFileHandler::SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *pal) | |
781 | { | |
782 | return FALSE; | |
783 | } | |
784 | ||
785 |