gsd_img_ppm.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. if (0 == gsd_getimage(&pixbuf, &xsize, &ysize)) {
  38. G_warning(_("Unable to get image of current GL screen"));
  39. return (1);
  40. }
  41. if (NULL == (fp = fopen(name, "w"))) {
  42. G_warning(_("Unable to open file <%s> for writing"), name);
  43. return (1);
  44. }
  45. fprintf(fp, "P6\n%d %d\n255\n", xsize, ysize);
  46. for (y = ysize - 1; y >= 0; y--) {
  47. for (x = 0; x < xsize; x++) {
  48. unsigned char r = pixbuf[(y * xsize + x) * 4 + 0];
  49. unsigned char g = pixbuf[(y * xsize + x) * 4 + 1];
  50. unsigned char b = pixbuf[(y * xsize + x) * 4 + 2];
  51. fputc((int)r, fp);
  52. fputc((int)g, fp);
  53. fputc((int)b, fp);
  54. }
  55. }
  56. G_free(pixbuf);
  57. fclose(fp);
  58. return (0);
  59. }
  60. /*!
  61. \brief Write zoom to file
  62. \param name file name
  63. \param xsize,ysize
  64. \return 1 on failure
  65. \return 0 on success
  66. */
  67. int GS_write_zoom(const char *name, unsigned int xsize, unsigned int ysize)
  68. {
  69. unsigned int x;
  70. int y;
  71. FILE *fp;
  72. unsigned char *pixbuf;
  73. if (0 == gsd_writeView(&pixbuf, xsize, ysize)) {
  74. G_warning(_("Unable to write view"));
  75. return (1);
  76. }
  77. if (NULL == (fp = fopen(name, "w"))) {
  78. G_warning(_("Unable to open file <%s> for writing"), name);
  79. return (1);
  80. }
  81. fprintf(fp, "P6\n%d %d\n255\n", xsize, ysize);
  82. for (y = ysize - 1; y >= 0; y--) {
  83. for (x = 0; x < xsize; x++) {
  84. unsigned char r = pixbuf[(y * xsize + x) * 4 + 0];
  85. unsigned char g = pixbuf[(y * xsize + x) * 4 + 1];
  86. unsigned char b = pixbuf[(y * xsize + x) * 4 + 2];
  87. fputc((int)r, fp);
  88. fputc((int)g, fp);
  89. fputc((int)b, fp);
  90. }
  91. }
  92. free(pixbuf);
  93. fclose(fp);
  94. return (0);
  95. }