|
1 /* |
|
2 * pixmap.cc: 2D image class |
|
3 * |
|
4 * This file is part of Pyrit Ray Tracer. |
|
5 * |
|
6 * Copyright 2008 Radek Brich |
|
7 * |
|
8 * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
9 * of this software and associated documentation files (the "Software"), to deal |
|
10 * in the Software without restriction, including without limitation the rights |
|
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
12 * copies of the Software, and to permit persons to whom the Software is |
|
13 * furnished to do so, subject to the following conditions: |
|
14 * |
|
15 * The above copyright notice and this permission notice shall be included in |
|
16 * all copies or substantial portions of the Software. |
|
17 * |
|
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
24 * THE SOFTWARE. |
|
25 */ |
|
26 |
|
27 #include <stdio.h> |
|
28 |
|
29 #ifdef HAVE_PNG |
|
30 # include <png.h> |
|
31 #endif |
|
32 |
|
33 #include "pixmap.h" |
|
34 |
|
35 unsigned char *Pixmap::getCharData() const |
|
36 { |
|
37 unsigned char *cdata = new unsigned char[w*h*3]; |
|
38 Float *fd = fdata; |
|
39 for (unsigned char *cd = cdata; cd != cdata + w*h*3; cd++, fd++) |
|
40 { |
|
41 if (*fd > 1.0) |
|
42 *cd = 255; |
|
43 else |
|
44 *cd = (unsigned char)(*fd * 255.0); |
|
45 } |
|
46 return cdata; |
|
47 } |
|
48 |
|
49 int Pixmap::writePNG(const char *fname) const |
|
50 { |
|
51 #ifndef HAVE_PNG |
|
52 return -3; |
|
53 #else |
|
54 int y; |
|
55 FILE *f; |
|
56 png_structp png; /* PNG data */ |
|
57 png_infop pnginfo; /* PNG info */ |
|
58 unsigned char *cdata, *d; |
|
59 |
|
60 if ((f = fopen(fname, "wb")) == NULL) |
|
61 return -1; |
|
62 |
|
63 png = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); |
|
64 if (!png) { |
|
65 fclose(f); |
|
66 return -2; |
|
67 } |
|
68 |
|
69 pnginfo = png_create_info_struct(png); |
|
70 if (!pnginfo) { |
|
71 fclose(f); |
|
72 png_destroy_write_struct(&png, 0); |
|
73 return -2; |
|
74 } |
|
75 |
|
76 if (setjmp(png_jmpbuf(png))) { |
|
77 fclose(f); |
|
78 png_destroy_info_struct(png, &pnginfo); |
|
79 png_destroy_write_struct(&png, &pnginfo); |
|
80 return -2; |
|
81 } |
|
82 |
|
83 /* predat knihovne PNG ukazatel na soubor */ |
|
84 png_init_io(png, f); |
|
85 |
|
86 /* parametry PNG */ |
|
87 png_set_compression_level(png, Z_BEST_COMPRESSION); |
|
88 png_set_IHDR(png, pnginfo, w, h, 8, PNG_COLOR_TYPE_RGB, |
|
89 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, |
|
90 PNG_FILTER_TYPE_DEFAULT); |
|
91 png_set_sRGB(png, pnginfo, PNG_sRGB_INTENT_PERCEPTUAL); |
|
92 png_set_sRGB_gAMA_and_cHRM(png, pnginfo, PNG_INFO_sRGB); |
|
93 |
|
94 /* zapsat hlavicku */ |
|
95 png_write_info(png, pnginfo); |
|
96 |
|
97 /* zapsat data */ |
|
98 d = cdata = getCharData(); |
|
99 for (y = 0; y < h; y++, d += w * 3) |
|
100 png_write_row(png, (png_byte *) d); |
|
101 |
|
102 /* ukoncit soubor a uvolnit pomocne struktury */ |
|
103 png_write_end(png, pnginfo); |
|
104 png_destroy_info_struct(png, &pnginfo); |
|
105 png_destroy_write_struct(&png, 0); |
|
106 |
|
107 fclose(f); |
|
108 |
|
109 delete[] cdata; |
|
110 |
|
111 return 0; |
|
112 #endif |
|
113 } |