Graph_set.c 3.6 KB

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