Graph_set.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #include <fcntl.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #ifdef __MINGW32__
  19. #include <windows.h>
  20. #else
  21. #include <sys/mman.h>
  22. #endif
  23. #include <grass/gis.h>
  24. #include <grass/glocale.h>
  25. #include "pngdriver.h"
  26. struct png_state png;
  27. static void map_file(void)
  28. {
  29. size_t size = HEADER_SIZE + png.width * png.height * sizeof(unsigned int);
  30. void *ptr;
  31. int fd;
  32. fd = open(png.file_name, O_RDWR);
  33. if (fd < 0)
  34. return;
  35. #ifdef __MINGW32__
  36. png.handle = CreateFileMapping((HANDLE) _get_osfhandle(fd),
  37. NULL, PAGE_READWRITE,
  38. 0, size, NULL);
  39. if (!png.handle)
  40. return;
  41. ptr = MapViewOfFile(png.handle, FILE_MAP_WRITE, 0, 0, size);
  42. if (!ptr)
  43. return;
  44. #else
  45. ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, (off_t) 0);
  46. if (ptr == MAP_FAILED)
  47. return;
  48. #endif
  49. if (png.grid)
  50. G_free(png.grid);
  51. png.grid = (int *)((char *)ptr + HEADER_SIZE);
  52. close(fd);
  53. png.mapped = 1;
  54. }
  55. int PNG_Graph_set(void)
  56. {
  57. unsigned int red, grn, blu;
  58. int do_read = 0;
  59. int do_map = 0;
  60. char *p;
  61. G_gisinit("PNG driver");
  62. p = getenv("GRASS_PNGFILE");
  63. if (!p || strlen(p) == 0)
  64. p = FILE_NAME;
  65. png.file_name = p;
  66. p = getenv("GRASS_TRUECOLOR");
  67. png.true_color = !p || strcmp(p, "FALSE") != 0;
  68. G_verbose_message(_("PNG: GRASS_TRUECOLOR status: %s"),
  69. png.true_color ? "TRUE" : "FALSE");
  70. p = getenv("GRASS_PNG_MAPPED");
  71. do_map = p && strcmp(p, "TRUE") == 0;
  72. if (do_map) {
  73. char *ext = png.file_name + strlen(png.file_name) - 4;
  74. if (G_strcasecmp(ext, ".bmp") != 0)
  75. do_map = 0;
  76. }
  77. p = getenv("GRASS_PNG_READ");
  78. do_read = p && strcmp(p, "TRUE") == 0;
  79. if (do_read && access(png.file_name, 0) != 0)
  80. do_read = 0;
  81. png.width = screen_width;
  82. png.height = screen_height;
  83. png.clip_top = 0;
  84. png.clip_bot = png.height;
  85. png.clip_left = 0;
  86. png.clip_rite = png.width;
  87. p = getenv("GRASS_TRANSPARENT");
  88. png.has_alpha = p && strcmp(p, "TRUE") == 0;
  89. png_init_color_table();
  90. p = getenv("GRASS_BACKGROUNDCOLOR");
  91. if (p && *p && sscanf(p, "%02x%02x%02x", &red, &grn, &blu) == 3)
  92. png.background = png_get_color(red, grn, blu, png.has_alpha ? 255 : 0);
  93. else {
  94. /* 0xffffff = white, 0x000000 = black */
  95. if (strcmp(DEFAULT_FG_COLOR, "white") == 0)
  96. /* foreground: white, background: black */
  97. png.background = png_get_color(0, 0, 0, png.has_alpha ? 255 : 0);
  98. else
  99. /* foreground: black, background: white */
  100. png.background = png_get_color(255, 255, 255, png.has_alpha ? 255 : 0);
  101. }
  102. G_verbose_message(_("PNG: collecting to file <%s>"), png.file_name);
  103. G_verbose_message(_("GRASS_WIDTH=%d, GRASS_HEIGHT=%d"),
  104. png.file_name, png.width, png.height);
  105. if (do_read && do_map)
  106. map_file();
  107. if (!png.mapped)
  108. png.grid = G_malloc(png.width * png.height * sizeof(unsigned int));
  109. if (!do_read) {
  110. PNG_Erase();
  111. png.modified = 1;
  112. }
  113. if (do_read && !png.mapped)
  114. read_image();
  115. if (do_map && !png.mapped) {
  116. write_image();
  117. map_file();
  118. }
  119. return 0;
  120. }