write_png.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*!
  2. \file lib/pngdriver/write_png.c
  3. \brief GRASS png display driver - write PPM image (lower level functions)
  4. (C) 2007-2014 by Glynn Clements and the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Glynn Clements
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <png.h>
  12. #include <grass/gis.h>
  13. #include <grass/glocale.h>
  14. #include "pngdriver.h"
  15. static void write_data(png_structp png_ptr, png_bytep data, png_size_t length)
  16. {
  17. png_size_t check;
  18. FILE *fp;
  19. if (png_ptr == NULL )
  20. return;
  21. fp = (FILE *) png_get_io_ptr(png_ptr);
  22. if ( fp == NULL )
  23. return;
  24. check = fwrite(data, 1, length, fp);
  25. if (check != length)
  26. G_fatal_error(_("Unable to write PNG"));
  27. }
  28. static void output_flush(png_structp png_ptr)
  29. {
  30. FILE *fp;
  31. if (png_ptr == NULL )
  32. return;
  33. fp = (FILE *) png_get_io_ptr(png_ptr);
  34. if ( fp == NULL )
  35. return;
  36. fflush( fp );
  37. }
  38. void write_png(void)
  39. {
  40. static jmp_buf jbuf;
  41. static png_struct *png_ptr;
  42. static png_info *info_ptr;
  43. FILE *output;
  44. int x, y;
  45. unsigned int *p;
  46. png_bytep line;
  47. const char *str;
  48. int compress;
  49. png_ptr =
  50. png_create_write_struct(PNG_LIBPNG_VER_STRING, &jbuf, NULL, NULL);
  51. if (!png_ptr)
  52. G_fatal_error(_("Unable to allocate PNG structure"));
  53. info_ptr = png_create_info_struct(png_ptr);
  54. if (!info_ptr)
  55. G_fatal_error(_("Unable to allocate PNG structure"));
  56. if (setjmp(png_jmpbuf(png_ptr)))
  57. G_fatal_error(_("Unable to write PNG file"));
  58. output = fopen(png.file_name, "wb");
  59. if (!output)
  60. G_fatal_error(_("Unable to open output PNG file <%s>"), png.file_name);
  61. png_set_write_fn(png_ptr, output, write_data, output_flush);
  62. png_set_IHDR(png_ptr, info_ptr,
  63. png.width, png.height, 8,
  64. png.true_color ? PNG_COLOR_TYPE_RGB_ALPHA :
  65. PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
  66. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  67. if (png.true_color)
  68. png_set_invert_alpha(png_ptr);
  69. else {
  70. png_color png_pal[256];
  71. int i;
  72. for (i = 0; i < 256; i++) {
  73. png_pal[i].red = png.palette[i][0];
  74. png_pal[i].green = png.palette[i][1];
  75. png_pal[i].blue = png.palette[i][2];
  76. }
  77. png_set_PLTE(png_ptr, info_ptr, png_pal, 256);
  78. if (png.has_alpha) {
  79. png_byte trans = (png_byte) 0;
  80. png_set_tRNS(png_ptr, info_ptr, &trans, 1, NULL);
  81. }
  82. }
  83. str = getenv("GRASS_RENDER_FILE_COMPRESSION");
  84. if (str && sscanf(str, "%d", &compress) == 1)
  85. png_set_compression_level(png_ptr, compress);
  86. png_write_info(png_ptr, info_ptr);
  87. line = G_malloc(png.width * 4);
  88. for (y = 0, p = png.grid; y < png.height; y++) {
  89. png_bytep q = line;
  90. if (png.true_color)
  91. for (x = 0; x < png.width; x++, p++) {
  92. unsigned int c = *p;
  93. int r, g, b, a;
  94. png_get_pixel(c, &r, &g, &b, &a);
  95. *q++ = (png_byte) r;
  96. *q++ = (png_byte) g;
  97. *q++ = (png_byte) b;
  98. *q++ = (png_byte) a;
  99. }
  100. else
  101. for (x = 0; x < png.width; x++, p++, q++)
  102. *q = (png_byte) * p;
  103. png_write_row(png_ptr, line);
  104. }
  105. G_free(line);
  106. png_write_end(png_ptr, info_ptr);
  107. png_destroy_write_struct(&png_ptr, &info_ptr);
  108. fclose(output);
  109. }