write_png.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <png.h>
  4. #include <grass/gis.h>
  5. #include "pngdriver.h"
  6. static void write_data(png_structp png_ptr, png_bytep data, png_size_t length)
  7. {
  8. png_size_t check;
  9. FILE *fp;
  10. if (png_ptr == NULL )
  11. return;
  12. fp = (FILE *) png_get_io_ptr(png_ptr);
  13. if ( fp == NULL )
  14. return;
  15. check = fwrite(data, 1, length, fp);
  16. if (check != length)
  17. G_fatal_error("PNG: Write Error");
  18. }
  19. static void output_flush(png_structp png_ptr)
  20. {
  21. FILE *fp;
  22. if (png_ptr == NULL )
  23. return;
  24. fp = (FILE *) png_get_io_ptr(png_ptr);
  25. if ( fp == NULL )
  26. return;
  27. fflush( fp );
  28. }
  29. void write_png(void)
  30. {
  31. static jmp_buf jbuf;
  32. static png_struct *png_ptr;
  33. static png_info *info_ptr;
  34. FILE *output;
  35. int x, y;
  36. unsigned int *p;
  37. png_bytep line;
  38. const char *str;
  39. int compress;
  40. png_ptr =
  41. png_create_write_struct(PNG_LIBPNG_VER_STRING, &jbuf, NULL, NULL);
  42. if (!png_ptr)
  43. G_fatal_error("PNG: couldn't allocate PNG structure");
  44. info_ptr = png_create_info_struct(png_ptr);
  45. if (!info_ptr)
  46. G_fatal_error("PNG: couldn't allocate PNG structure");
  47. if (setjmp(png_jmpbuf(png_ptr)))
  48. G_fatal_error("error writing PNG file");
  49. output = fopen(png.file_name, "wb");
  50. if (!output)
  51. G_fatal_error("PNG: couldn't open output file %s", png.file_name);
  52. png_set_write_fn(png_ptr, output, write_data, output_flush);
  53. png_set_IHDR(png_ptr, info_ptr,
  54. png.width, png.height, 8,
  55. png.true_color ? PNG_COLOR_TYPE_RGB_ALPHA :
  56. PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
  57. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  58. if (png.true_color)
  59. png_set_invert_alpha(png_ptr);
  60. else {
  61. png_color png_pal[256];
  62. int i;
  63. for (i = 0; i < 256; i++) {
  64. png_pal[i].red = png.palette[i][0];
  65. png_pal[i].green = png.palette[i][1];
  66. png_pal[i].blue = png.palette[i][2];
  67. }
  68. png_set_PLTE(png_ptr, info_ptr, png_pal, 256);
  69. if (png.has_alpha) {
  70. png_byte trans = (png_byte) 0;
  71. png_set_tRNS(png_ptr, info_ptr, &trans, 1, NULL);
  72. }
  73. }
  74. str = getenv("GRASS_PNG_COMPRESSION");
  75. if (str && sscanf(str, "%d", &compress) == 1)
  76. png_set_compression_level(png_ptr, compress);
  77. png_write_info(png_ptr, info_ptr);
  78. line = G_malloc(png.width * 4);
  79. for (y = 0, p = png.grid; y < png.height; y++) {
  80. png_bytep q = line;
  81. if (png.true_color)
  82. for (x = 0; x < png.width; x++, p++) {
  83. unsigned int c = *p;
  84. int r, g, b, a;
  85. png_get_pixel(c, &r, &g, &b, &a);
  86. *q++ = (png_byte) r;
  87. *q++ = (png_byte) g;
  88. *q++ = (png_byte) b;
  89. *q++ = (png_byte) a;
  90. }
  91. else
  92. for (x = 0; x < png.width; x++, p++, q++)
  93. *q = (png_byte) * p;
  94. png_write_row(png_ptr, line);
  95. }
  96. G_free(line);
  97. png_write_end(png_ptr, info_ptr);
  98. png_destroy_write_struct(&png_ptr, &info_ptr);
  99. fclose(output);
  100. }