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