Annotation of QuadraticMandel/mypng.c, revision 1.2
1.1 cvsmgr 1: /*******************************************************************************
1.2 ! cvsmgr 2: +* Fractal set generation for quadratic maps.
! 3: +* Copyright (C) 2011, Raúl Durán Díaz, raul.duran@uah.es
! 4: +*
! 5: +* This program is free software: you can redistribute it and/or modify
! 6: +* it under the terms of the GNU General Public License as published by
! 7: +* the Free Software Foundation, either version 3 of the License, or
! 8: +* (at your option) any later version.
! 9: +*
! 10: +* This program is distributed in the hope that it will be useful,
! 11: +* but WITHOUT ANY WARRANTY; without even the implied warranty of
! 12: +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! 13: +* GNU General Public License for more details.
! 14: +*
! 15: +* You should have received a copy of the GNU General Public License
! 16: +* along with this program. If not, see <http://www.gnu.org/licenses/>.
! 17: *******************************************************************************/
! 18: /*******************************************************************************
1.1 cvsmgr 19: +* Soporte para librería png.
20: +*
21: +* 7.07.2005
22: +*
23: +* $Id: mypng.c,v 1.6 2009/11/25 09:23:03 rduran Exp $
24: +* $Name: $
25: *******************************************************************************/
26: # include <stdlib.h>
27: # include <time.h>
28: # include "mypng.h"
29:
30: static int num_rows = NUM_ROWS;
31: static int num_cols = NUM_COLS;
32: static double lado = SIDE;
33: static double inc = INCR;
34: static double offset_x = 0.0;
35: static double offset_y = 0.0;
36:
37:
38: static png_bytepp AllocateRowPointers(png_uint_32 height, png_uint_32 width)
39: {
40: png_bytepp row_pointers = png_bytepp_NULL;
41: int i;
42:
43: row_pointers = (png_bytepp) malloc(height*sizeof(png_bytep));
44:
45: if (row_pointers)
46: for (i = 0; i < height; i++)
47: row_pointers[i] = (png_bytep) malloc(3*width*sizeof(png_byte));
48:
49: return row_pointers;
50: }
51:
52: static void FreeRowPointers(png_bytepp row_pointers, png_uint_32 height)
53: {
54: int i;
55:
56: for (i = 0; i < height; i++)
57: free(row_pointers[i]);
58:
59: free(row_pointers);
60: }
61:
62: static void CopyImage(png_colorpp pixels, png_bytepp row_pointers,
63: png_uint_32 height, png_uint_32 width)
64: {
65: png_uint_32 i, j;
66:
67: for (i = 0; i < height; i++)
68: {
69: for (j = 0; j < width; j++)
70: {
71: row_pointers[i][3*j ] = pixels[i][j].red; /* RED */
72: row_pointers[i][3*j+1] = pixels[i][j].green; /* GREEN */
73: row_pointers[i][3*j+2] = pixels[i][j].blue; /* BLUE */
74: }
75: }
76: }
77:
78: void BuildTextPtr(png_textp info_text, double alpha, double beta, int itermax,
79: double side_len, double offset_x, double offset_y)
80: {
81: int i;
82: time_t hora = time(NULL);
83: struct tm *t_hora = localtime(&hora);
84: static char texts[NUM_TEXT][TEXT_LENGTH];
85:
86: for (i = 0; i < NUM_TEXT; i++)
87: {
88: info_text[i].compression = PNG_TEXT_COMPRESSION_NONE;
89: info_text[i].text = texts[i];
90: }
91:
92: info_text[0].key = "Xi";
93: info_text[0].text_length = 0;
94: sprintf(texts[0], "%lf", alpha);
95:
96: info_text[1].key = "Eta";
97: info_text[1].text_length = 0;
98: sprintf(texts[1], "%lf", beta);
99:
100: info_text[2].key = "Iterations";
101: info_text[2].text_length = 0;
102: sprintf(texts[2], "%d", itermax);
103:
104: info_text[3].key = "Side length";
105: info_text[3].text_length = 0;
106: sprintf(texts[3], "%lf", side_len);
107:
108: info_text[4].key = "Offset x";
109: info_text[4].text_length = 0;
110: sprintf(texts[4], "%lf", offset_x);
111:
112: info_text[5].key = "Offset y";
113: info_text[5].text_length = 0;
114: sprintf(texts[5], "%lf", offset_y);
115:
116: info_text[6].key = "Date";
117: info_text[6].text_length = 0;
118: sprintf(texts[6], "%2d.%02d.%d %2d:%02d", t_hora->tm_mday,
119: t_hora->tm_mon + 1,
120: t_hora->tm_year + 1900,
121: t_hora->tm_hour,
122: t_hora->tm_min);
123: }
124:
125: void DrawVertiLine(png_colorpp image, double coord_x, png_color p)
126: {
127: int x = CoordXToPix(coord_x);
128: int i;
129:
130: for (i = 0; i < num_rows; i++)
131: {
132: image[i][x].red = p.red;
133: image[i][x].green = p.green;
134: image[i][x].blue = p.blue;
135: }
136: }
137:
138: void DrawHorizLine(png_colorpp image, double coord_y, png_color p)
139: {
140: int y = CoordYToPix(coord_y);
141: int i;
142:
143: for (i = 0; i < num_cols; i++)
144: {
145: image[y][i].red = p.red;
146: image[y][i].green = p.green;
147: image[y][i].blue = p.blue;
148: }
149: }
150:
151: int CoordXToPix(double x)
152: {
153: int xtemp = (x - offset_x)/inc + 0.5;
154:
155: if (xtemp >= num_cols/2) xtemp = num_cols/2 - 1;
156: if (xtemp < -num_cols/2) xtemp = -num_cols/2;
157:
158: return xtemp + num_cols/2;
159: }
160:
161: int CoordYToPix(double y)
162: {
163: int ytemp = (offset_y - y)/inc + 0.5;
164:
165: if (ytemp >= num_rows/2) ytemp = num_rows/2 - 1;
166: if (ytemp < -num_rows/2) ytemp = -num_rows/2;
167:
168: return ytemp + num_rows/2;
169: }
170:
171: double PixToCoordX(int x)
172: {
173: return (x - num_cols/2)*inc + offset_x;
174: }
175:
176: double PixToCoordY(int y)
177: {
178: return (num_rows/2 - y)*inc + offset_y;
179: }
180:
181: void SetSide(double l)
182: {
183: lado = l;
184: inc = lado/num_cols;
185: }
186:
187: void SetOffsetX(double off_x)
188: {
189: offset_x = off_x;
190: }
191:
192: void SetOffsetY(double off_y)
193: {
194: offset_y = off_y;
195: }
196:
197: void SetImageSize(int rows, int columns)
198: {
199: num_rows = rows;
200: num_cols = columns;
201: inc = lado/num_cols;
202: }
203:
204: void DeallocatePNG(png_colorpp p, png_uint_32 height, png_uint_32 width)
205: {
206: int i = 0;
207:
208: if (p)
209: for (i = 0; i < height; i++)
210: free(p[i]);
211:
212: free(p);
213: }
214:
215: png_colorpp AllocatePNG(png_uint_32 height, png_uint_32 width)
216: {
217: png_colorpp p = (png_colorpp) NULL;
218: int i = 0;
219:
220: p = (png_colorpp) calloc(height, sizeof(png_colorp));
221:
222: if (p)
223: for (i = 0; i < height; i++)
224: p[i] = (png_colorp) calloc(width, sizeof(png_color));
225:
226: SetImageSize(height, width);
227: return p;
228: }
229:
230: BOOL WritePNG(char *file_name, png_colorpp pixels,
231: png_textp text_ptr, int num_text)
232: {
233: FILE *fp;
234: png_structp png_ptr;
235: png_infop info_ptr;
236: int png_transforms = 0;
237: png_bytepp row_pointers = AllocateRowPointers(num_rows, num_cols);
238:
239: if ((fp = fopen(file_name, "wb")) == NULL)
240: {
241: FreeRowPointers(row_pointers, num_rows);
242: return ERROR;
243: }
244:
245: png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
246: png_voidp_NULL,
247: png_voidp_NULL,
248: png_voidp_NULL);
249:
250: if (png_ptr == NULL)
251: {
252: fclose(fp);
253: FreeRowPointers(row_pointers, num_rows);
254: return ERROR;
255: }
256:
257: info_ptr = png_create_info_struct(png_ptr);
258: if (info_ptr == NULL)
259: {
260: fclose(fp);
261: png_destroy_write_struct(&png_ptr, png_infopp_NULL);
262: FreeRowPointers(row_pointers, num_rows);
263: return ERROR;
264: }
265:
266: if (setjmp(png_jmpbuf(png_ptr)))
267: {
268: fclose(fp);
269: png_destroy_write_struct(&png_ptr, &info_ptr);
270: FreeRowPointers(row_pointers, num_rows);
271: return ERROR;
272: }
273:
274: png_init_io(png_ptr, fp);
275:
276: png_set_IHDR(png_ptr, info_ptr,
277: num_cols, /* width in pixels */
278: num_rows, /* height in pixels */
279: 8, /* bit depth of one of the */
280: /* image channels: 1, 2, 4, 8, 16 */
281: PNG_COLOR_TYPE_RGB, /* describes which color/alpha */
282: /* channels are present */
283: PNG_INTERLACE_ADAM7, /* PNG_INTERLACE_NONE */
284: PNG_COMPRESSION_TYPE_DEFAULT,
285: PNG_FILTER_TYPE_DEFAULT);
286:
287:
288: CopyImage(pixels, row_pointers, num_rows, num_cols);
289: png_set_rows(png_ptr, info_ptr, row_pointers);
290: png_set_text(png_ptr, info_ptr, text_ptr, num_text);
291:
292: png_write_png(png_ptr, info_ptr, png_transforms, png_voidp_NULL);
293:
294: /* clean up after the write, and free any memory allocated */
295: png_destroy_write_struct(&png_ptr, &info_ptr);
296:
297: /* close the file */
298: fclose(fp);
299: FreeRowPointers(row_pointers, num_rows);
300:
301: /* that's it */
302: return OK;
303: }
304:
305: BOOL ReadPNGText(char *file_name)
306: {
307: FILE *fp;
308: png_structp png_ptr;
309: png_infop info_ptr;
310: png_textp text_ptr;
311: int num_comments = 0;
312: int num_text;
313: int i;
314:
315: if ((fp = fopen(file_name, "rb")) == NULL)
316: return ERROR;
317:
318: png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
319: png_voidp_NULL,
320: png_voidp_NULL,
321: png_voidp_NULL);
322:
323: if (png_ptr == NULL)
324: {
325: fclose(fp);
326: return ERROR;
327: }
328:
329: info_ptr = png_create_info_struct(png_ptr);
330: if (info_ptr == NULL)
331: {
332: fclose(fp);
333: png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
334: return ERROR;
335: }
336:
337: if (setjmp(png_jmpbuf(png_ptr)))
338: {
339: fclose(fp);
340: png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
341: return ERROR;
342: }
343:
344: png_init_io(png_ptr, fp);
345:
346: do
347: {
348: png_read_info(png_ptr, info_ptr);
349: } while (/* png_get_valid(png_ptr, info_ptr, PNG_INFO_text) < */0);
350:
351:
352: num_comments = png_get_text(png_ptr, info_ptr, &text_ptr, &num_text);
353:
354: if (num_comments == 0)
355: printf("No information available.\n");
356: else
357: {
358: for (i = 0; i < num_comments; i++)
359: printf("%-11s: %12s\n", text_ptr[i].key, text_ptr[i].text);
360: }
361:
362: /* clean up after the write, and free any memory allocated */
363: png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
364:
365: /* close the file */
366: fclose(fp);
367:
368: /* that's it */
369: return OK;
370: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>