write_png.c 2.2 KB

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