graph_set.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*!
  2. \file lib/pngdriver/graph_set.c
  3. \brief GRASS png display driver - set graphics processing
  4. (C) 2003-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 Per Henrik Johansen (original contributor)
  8. \author Glynn Clements
  9. */
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <fcntl.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #ifdef __MINGW32__
  17. #include <windows.h>
  18. #else
  19. #include <sys/mman.h>
  20. #endif
  21. #include <grass/gis.h>
  22. #include <grass/colors.h>
  23. #include <grass/glocale.h>
  24. #include "pngdriver.h"
  25. struct png_state png;
  26. static void map_file(void)
  27. {
  28. size_t size = HEADER_SIZE + png.width * png.height * sizeof(unsigned int);
  29. void *ptr;
  30. int fd;
  31. fd = open(png.file_name, O_RDWR);
  32. if (fd < 0)
  33. return;
  34. #ifdef __MINGW32__
  35. png.handle = CreateFileMapping((HANDLE) _get_osfhandle(fd),
  36. NULL, PAGE_READWRITE,
  37. 0, size, NULL);
  38. if (!png.handle)
  39. return;
  40. ptr = MapViewOfFile(png.handle, FILE_MAP_WRITE, 0, 0, size);
  41. if (!ptr)
  42. return;
  43. #else
  44. ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, (off_t) 0);
  45. if (ptr == MAP_FAILED)
  46. return;
  47. #endif
  48. if (png.grid)
  49. G_free(png.grid);
  50. png.grid = (unsigned int *)((char *) ptr + HEADER_SIZE);
  51. close(fd);
  52. png.mapped = 1;
  53. }
  54. /*!
  55. \brief Start up graphics processing
  56. Anything that needs to be assigned, set up,
  57. started-up, or otherwise initialized happens here. This is called only at
  58. the startup of the graphics driver.
  59. The external variables define the pixle limits of the graphics surface. The
  60. coordinate system used by the applications programs has the (0,0) origin
  61. in the upper left-hand corner. Hence,
  62. screen_left < screen_right
  63. screen_top < screen_bottom
  64. */
  65. int PNG_Graph_set(void)
  66. {
  67. unsigned int red, grn, blu;
  68. int do_read = 0;
  69. int do_map = 0;
  70. char *p;
  71. G_gisinit("PNG driver");
  72. p = getenv("GRASS_RENDER_FILE");
  73. if (!p || strlen(p) == 0)
  74. p = FILE_NAME;
  75. G_debug(1, "png: GRASS_RENDER_FILE: %s", p);
  76. png.file_name = p;
  77. p = getenv("GRASS_RENDER_TRUECOLOR");
  78. png.true_color = !p || strcmp(p, "FALSE") != 0;
  79. G_verbose_message(_("png: truecolor status %s"),
  80. png.true_color ? _("enabled") : _("disabled"));
  81. p = getenv("GRASS_RENDER_FILE_MAPPED");
  82. do_map = p && strcmp(p, "TRUE") == 0;
  83. if (do_map) {
  84. char *ext = png.file_name + strlen(png.file_name) - 4;
  85. if (G_strcasecmp(ext, ".bmp") != 0)
  86. do_map = 0;
  87. }
  88. p = getenv("GRASS_RENDER_FILE_READ");
  89. do_read = p && strcmp(p, "TRUE") == 0;
  90. if (do_read && access(png.file_name, 0) != 0)
  91. do_read = 0;
  92. png.width = screen_width;
  93. png.height = screen_height;
  94. png.clip_top = 0;
  95. png.clip_bot = png.height;
  96. png.clip_left = 0;
  97. png.clip_rite = png.width;
  98. p = getenv("GRASS_RENDER_TRANSPARENT");
  99. png.has_alpha = p && strcmp(p, "TRUE") == 0;
  100. png_init_color_table();
  101. p = getenv("GRASS_RENDER_BACKGROUNDCOLOR");
  102. if (p && *p &&
  103. (sscanf(p, "%02x%02x%02x", &red, &grn, &blu) == 3 ||
  104. G_str_to_color(p, (int *)&red, (int *)&grn, (int *)&blu) == 1)) {
  105. png.background = png_get_color(red, grn, blu, png.has_alpha ? 255 : 0);
  106. }
  107. else {
  108. /* 0xffffff = white, 0x000000 = black */
  109. if (strcmp(DEFAULT_FG_COLOR, "white") == 0)
  110. /* foreground: white, background: black */
  111. png.background = png_get_color(0, 0, 0, png.has_alpha ? 255 : 0);
  112. else
  113. /* foreground: black, background: white */
  114. png.background = png_get_color(255, 255, 255, png.has_alpha ? 255 : 0);
  115. }
  116. G_verbose_message(_("png: collecting to file '%s'"), png.file_name);
  117. G_verbose_message(_("png: image size %dx%d"),
  118. png.width, png.height);
  119. if (do_read && do_map)
  120. map_file();
  121. if (!png.mapped)
  122. png.grid = G_malloc(png.width * png.height * sizeof(unsigned int));
  123. if (!do_read) {
  124. PNG_Erase();
  125. png.modified = 1;
  126. }
  127. if (do_read && !png.mapped)
  128. read_image();
  129. if (do_map && !png.mapped) {
  130. write_image();
  131. map_file();
  132. }
  133. return 0;
  134. }
  135. /*!
  136. \brief Get render file
  137. \return file name
  138. */
  139. const char *PNG_Graph_get_file(void)
  140. {
  141. return png.file_name;
  142. }