Graph_set.c 3.3 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. #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 "pngdriver.h"
  23. char *file_name;
  24. int currentColor;
  25. int true_color;
  26. int auto_write;
  27. int has_alpha;
  28. int mapped;
  29. double clip_top, clip_bot, clip_left, clip_rite;
  30. int width, height;
  31. void *image;
  32. unsigned int *grid;
  33. unsigned char png_palette[256][4];
  34. unsigned int background;
  35. int modified;
  36. static void map_file(void)
  37. {
  38. #ifndef __MINGW32__
  39. size_t size = HEADER_SIZE + width * height * sizeof(unsigned int);
  40. void *ptr;
  41. int fd;
  42. fd = open(file_name, O_RDWR);
  43. if (fd < 0)
  44. return;
  45. ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, (off_t) 0);
  46. if (ptr == MAP_FAILED)
  47. return;
  48. if (grid)
  49. G_free(grid);
  50. grid = (int *)((char *)ptr + HEADER_SIZE);
  51. close(fd);
  52. mapped = 1;
  53. #endif
  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. file_name = p;
  66. p = getenv("GRASS_TRUECOLOR");
  67. true_color = p && strcmp(p, "TRUE") == 0;
  68. G_message("PNG: GRASS_TRUECOLOR status: %s",
  69. true_color ? "TRUE" : "FALSE");
  70. p = getenv("GRASS_PNG_AUTO_WRITE");
  71. auto_write = p && strcmp(p, "TRUE") == 0;
  72. p = getenv("GRASS_PNG_MAPPED");
  73. do_map = p && strcmp(p, "TRUE") == 0;
  74. if (do_map) {
  75. char *ext = file_name + strlen(file_name) - 4;
  76. if (G_strcasecmp(ext, ".bmp") != 0)
  77. do_map = 0;
  78. }
  79. p = getenv("GRASS_PNG_READ");
  80. do_read = p && strcmp(p, "TRUE") == 0;
  81. if (do_read && access(file_name, 0) != 0)
  82. do_read = 0;
  83. width = screen_width;
  84. height = screen_height;
  85. clip_top = 0;
  86. clip_bot = height;
  87. clip_left = 0;
  88. clip_rite = width;
  89. p = getenv("GRASS_TRANSPARENT");
  90. has_alpha = p && strcmp(p, "TRUE") == 0;
  91. init_color_table();
  92. p = getenv("GRASS_BACKGROUNDCOLOR");
  93. if (p && *p && sscanf(p, "%02x%02x%02x", &red, &grn, &blu) == 3)
  94. background = get_color(red, grn, blu, has_alpha ? 255 : 0);
  95. else {
  96. /* 0xffffff = white, 0x000000 = black */
  97. if (strcmp(DEFAULT_FG_COLOR, "white") == 0)
  98. /* foreground: white, background: black */
  99. background = get_color(0, 0, 0, has_alpha ? 255 : 0);
  100. else
  101. /* foreground: black, background: white */
  102. background = get_color(255, 255, 255, has_alpha ? 255 : 0);
  103. }
  104. G_message
  105. ("PNG: collecting to file: %s,\n GRASS_WIDTH=%d, GRASS_HEIGHT=%d",
  106. file_name, width, height);
  107. if (do_read && do_map)
  108. map_file();
  109. if (!mapped)
  110. grid = G_malloc(width * height * sizeof(unsigned int));
  111. if (!do_read) {
  112. PNG_Erase();
  113. modified = 1;
  114. }
  115. if (do_read && !mapped)
  116. read_image();
  117. if (do_map && !mapped) {
  118. write_image();
  119. map_file();
  120. }
  121. return 0;
  122. }