write_ppm.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*!
  2. \file lib/pngdriver/write_ppm.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 <string.h>
  12. #include <grass/gis.h>
  13. #include "pngdriver.h"
  14. void write_ppm(void)
  15. {
  16. FILE *output;
  17. int x, y;
  18. unsigned int *p;
  19. output = fopen(png.file_name, "wb");
  20. if (!output)
  21. G_fatal_error("PNG: couldn't open output file %s", png.file_name);
  22. fprintf(output, "P6\n%d %d\n255\n", png.width, png.height);
  23. for (y = 0, p = png.grid; y < png.height; y++) {
  24. for (x = 0; x < png.width; x++, p++) {
  25. unsigned int c = *p;
  26. int r, g, b, a;
  27. png_get_pixel(c, &r, &g, &b, &a);
  28. fputc((unsigned char)r, output);
  29. fputc((unsigned char)g, output);
  30. fputc((unsigned char)b, output);
  31. }
  32. }
  33. fclose(output);
  34. }
  35. void write_pgm(void)
  36. {
  37. char *mask_name = G_store(png.file_name);
  38. FILE *output;
  39. int x, y;
  40. unsigned int *p;
  41. mask_name[strlen(mask_name) - 2] = 'g';
  42. output = fopen(mask_name, "wb");
  43. if (!output)
  44. G_fatal_error("PNG: couldn't open mask file %s", mask_name);
  45. G_free(mask_name);
  46. fprintf(output, "P5\n%d %d\n255\n", png.width, png.height);
  47. for (y = 0, p = png.grid; y < png.height; y++) {
  48. for (x = 0; x < png.width; x++, p++) {
  49. unsigned int c = *p;
  50. int r, g, b, a;
  51. png_get_pixel(c, &r, &g, &b, &a);
  52. fputc((unsigned char)(255 - a), output);
  53. }
  54. }
  55. fclose(output);
  56. }