write_ppm.c 1.3 KB

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