Graph_set.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Start up graphics processing. Anything that needs to be assigned, set up,
  3. * started-up, or otherwise initialized happens here. This is called only at
  4. * the startup of the graphics driver.
  5. *
  6. * The external variables define the pixle limits of the graphics surface. The
  7. * coordinate system used by the applications programs has the (0,0) origin
  8. * in the upper left-hand corner. Hence,
  9. * screen_left < screen_right
  10. * screen_top < screen_bottom
  11. */
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <unistd.h>
  15. #ifndef __MINGW32__
  16. #include <fcntl.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <sys/mman.h>
  20. #endif
  21. #include <grass/gis.h>
  22. #include <grass/glocale.h>
  23. #include "pngdriver.h"
  24. struct png_state png;
  25. static void map_file(void)
  26. {
  27. #ifndef __MINGW32__
  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. ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, (off_t) 0);
  35. if (ptr == MAP_FAILED)
  36. return;
  37. if (png.grid)
  38. G_free(png.grid);
  39. png.grid = (int *)((char *)ptr + HEADER_SIZE);
  40. close(fd);
  41. png.mapped = 1;
  42. #endif
  43. }
  44. int PNG_Graph_set(void)
  45. {
  46. unsigned int red, grn, blu;
  47. int do_read = 0;
  48. int do_map = 0;
  49. char *p;
  50. G_gisinit("PNG driver");
  51. p = getenv("GRASS_PNGFILE");
  52. if (!p || strlen(p) == 0)
  53. p = FILE_NAME;
  54. png.file_name = p;
  55. p = getenv("GRASS_TRUECOLOR");
  56. png.true_color = !p || strcmp(p, "FALSE") != 0;
  57. G_verbose_message(_("PNG: GRASS_TRUECOLOR status: %s"),
  58. png.true_color ? "TRUE" : "FALSE");
  59. p = getenv("GRASS_PNG_MAPPED");
  60. do_map = p && strcmp(p, "TRUE") == 0;
  61. if (do_map) {
  62. char *ext = png.file_name + strlen(png.file_name) - 4;
  63. if (G_strcasecmp(ext, ".bmp") != 0)
  64. do_map = 0;
  65. }
  66. p = getenv("GRASS_PNG_READ");
  67. do_read = p && strcmp(p, "TRUE") == 0;
  68. if (do_read && access(png.file_name, 0) != 0)
  69. do_read = 0;
  70. png.width = screen_width;
  71. png.height = screen_height;
  72. png.clip_top = 0;
  73. png.clip_bot = png.height;
  74. png.clip_left = 0;
  75. png.clip_rite = png.width;
  76. p = getenv("GRASS_TRANSPARENT");
  77. png.has_alpha = p && strcmp(p, "TRUE") == 0;
  78. png_init_color_table();
  79. p = getenv("GRASS_BACKGROUNDCOLOR");
  80. if (p && *p && sscanf(p, "%02x%02x%02x", &red, &grn, &blu) == 3)
  81. png.background = png_get_color(red, grn, blu, png.has_alpha ? 255 : 0);
  82. else {
  83. /* 0xffffff = white, 0x000000 = black */
  84. if (strcmp(DEFAULT_FG_COLOR, "white") == 0)
  85. /* foreground: white, background: black */
  86. png.background = png_get_color(0, 0, 0, png.has_alpha ? 255 : 0);
  87. else
  88. /* foreground: black, background: white */
  89. png.background = png_get_color(255, 255, 255, png.has_alpha ? 255 : 0);
  90. }
  91. G_verbose_message(_("PNG: collecting to file <%s>"), png.file_name);
  92. G_verbose_message(_("GRASS_WIDTH=%d, GRASS_HEIGHT=%d"),
  93. png.file_name, png.width, png.height);
  94. if (do_read && do_map)
  95. map_file();
  96. if (!png.mapped)
  97. png.grid = G_malloc(png.width * png.height * sizeof(unsigned int));
  98. if (!do_read) {
  99. PNG_Erase();
  100. png.modified = 1;
  101. }
  102. if (do_read && !png.mapped)
  103. read_image();
  104. if (do_map && !png.mapped) {
  105. write_image();
  106. map_file();
  107. }
  108. return 0;
  109. }