gsd_img_ppm.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*!
  2. \file lib/ogsf/gsd_img_ppm.c
  3. \brief OGSF library - PPM stuff
  4. GRASS OpenGL gsurf OGSF Library
  5. (C) 1999-2008 by the GRASS Development Team
  6. - added little/big endian test Markus Neteler
  7. - modified to PPM by Bob Covill <bcovill@tekmap.ns.ca>
  8. - changed 10/99 Jaro
  9. - Created new function GS_write_ppm based on RGB dump
  10. This program is free software under the
  11. GNU General Public License (>=v2).
  12. Read the file COPYING that comes with GRASS
  13. for details.
  14. \author Bill Brown USACERL, GMSL/University of Illinois
  15. \author Markus Neteler
  16. \author Bob Covill
  17. \author Jaro Hofierka
  18. \author Doxygenized by Martin Landa <landa.martin gmail.com> (May 2008)
  19. */
  20. #include <stdlib.h>
  21. #include <grass/gis.h>
  22. #include <grass/glocale.h>
  23. #include <grass/ogsf.h>
  24. /*!
  25. \brief Save current GL screen to ppm file
  26. \param name file name
  27. \return 1 on failure
  28. \return 0 on success
  29. */
  30. int GS_write_ppm(const char *name)
  31. {
  32. unsigned int x;
  33. int y;
  34. unsigned int xsize, ysize;
  35. FILE *fp;
  36. unsigned char *pixbuf;
  37. gsd_getimage(&pixbuf, &xsize, &ysize);
  38. if (NULL == (fp = fopen(name, "w"))) {
  39. G_warning(_("Unable to open file <%s> for writing"), name);
  40. return (1);
  41. }
  42. fprintf(fp, "P6\n%d %d\n255\n", xsize, ysize);
  43. for (y = ysize - 1; y >= 0; y--) {
  44. for (x = 0; x < xsize; x++) {
  45. unsigned char r = pixbuf[(y * xsize + x) * 4 + 0];
  46. unsigned char g = pixbuf[(y * xsize + x) * 4 + 1];
  47. unsigned char b = pixbuf[(y * xsize + x) * 4 + 2];
  48. fputc((int)r, fp);
  49. fputc((int)g, fp);
  50. fputc((int)b, fp);
  51. }
  52. }
  53. G_free(pixbuf);
  54. fclose(fp);
  55. return (0);
  56. }
  57. /*!
  58. \brief Write zoom to file
  59. \param name file name
  60. \param xsize,ysize
  61. \return 1 on failure
  62. \return 0 on success
  63. */
  64. int GS_write_zoom(const char *name, unsigned int xsize, unsigned int ysize)
  65. {
  66. unsigned int x;
  67. int y;
  68. FILE *fp;
  69. unsigned char *pixbuf;
  70. gsd_writeView(&pixbuf, xsize, ysize);
  71. if (NULL == (fp = fopen(name, "w"))) {
  72. G_warning(_("Unable to open file <%s> for writing"), name);
  73. return (1);
  74. }
  75. fprintf(fp, "P6\n%d %d\n255\n", xsize, ysize);
  76. for (y = ysize - 1; y >= 0; y--) {
  77. for (x = 0; x < xsize; x++) {
  78. unsigned char r = pixbuf[(y * xsize + x) * 4 + 0];
  79. unsigned char g = pixbuf[(y * xsize + x) * 4 + 1];
  80. unsigned char b = pixbuf[(y * xsize + x) * 4 + 2];
  81. fputc((int)r, fp);
  82. fputc((int)g, fp);
  83. fputc((int)b, fp);
  84. }
  85. }
  86. free(pixbuf);
  87. fclose(fp);
  88. return (0);
  89. }