write_ppm.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*!
  2. \file lib/cairodriver/write_ppm.c
  3. \brief GRASS cairo display driver - write PPM image (lower level functions)
  4. (C) 2007-2008 by Lars Ahlzen 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 Lars Ahlzen <lars ahlzen.com> (original contibutor)
  8. \author Glynn Clements
  9. */
  10. #include <grass/glocale.h>
  11. #include "cairodriver.h"
  12. void cairo_write_ppm(void)
  13. {
  14. char *mask_name = G_store(ca.file_name);
  15. FILE *output, *mask;
  16. int x, y;
  17. output = fopen(ca.file_name, "wb");
  18. if (!output)
  19. G_fatal_error(_("Cairo: unable to open output file <%s>"),
  20. ca.file_name);
  21. mask_name[strlen(mask_name) - 2] = 'g';
  22. mask = fopen(mask_name, "wb");
  23. if (!mask)
  24. G_fatal_error(_("Cairo: unable to open mask file <%s>"),
  25. mask_name);
  26. G_free(mask_name);
  27. fprintf(output, "P6\n%d %d\n255\n", ca.width, ca.height);
  28. fprintf(mask, "P5\n%d %d\n255\n", ca.width, ca.height);
  29. for (y = 0; y < ca.height; y++) {
  30. const unsigned int *row = (const unsigned int *)(ca.grid + y * ca.stride);
  31. for (x = 0; x < ca.width; x++) {
  32. unsigned int c = row[x];
  33. int a = (c >> 24) & 0xFF;
  34. int r = (c >> 16) & 0xFF;
  35. int g = (c >> 8) & 0xFF;
  36. int b = (c >> 0) & 0xFF;
  37. if (a > 0 && a < 0xFF) {
  38. r = r * 0xFF / a;
  39. g = g * 0xFF / a;
  40. b = b * 0xFF / a;
  41. }
  42. fputc((unsigned char)r, output);
  43. fputc((unsigned char)g, output);
  44. fputc((unsigned char)b, output);
  45. fputc((unsigned char)a, mask);
  46. }
  47. }
  48. fclose(output);
  49. fclose(mask);
  50. }