GRASS Programmer's Manual
6.4.2(2012)
|
00001 /* 00002 * Start up graphics processing. Anything that needs to be assigned, set up, 00003 * started-up, or otherwise initialized happens here. This is called only at 00004 * the startup of the graphics driver. 00005 * 00006 * The external variables define the pixle limits of the graphics surface. The 00007 * coordinate system used by the applications programs has the (0,0) origin 00008 * in the upper left-hand corner. Hence, 00009 * screen_left < screen_right 00010 * screen_top < screen_bottom 00011 */ 00012 00013 #include <string.h> 00014 #include <stdlib.h> 00015 #include <unistd.h> 00016 #ifndef __MINGW32__ 00017 #include <fcntl.h> 00018 #include <sys/types.h> 00019 #include <sys/stat.h> 00020 #include <sys/mman.h> 00021 #endif 00022 00023 #include <grass/gis.h> 00024 #include "pngdriver.h" 00025 00026 char *file_name; 00027 int currentColor; 00028 int true_color; 00029 int auto_write; 00030 int has_alpha; 00031 int mapped; 00032 00033 int clip_top, clip_bot, clip_left, clip_rite; 00034 int width, height; 00035 void *image; 00036 unsigned int *grid; 00037 unsigned char png_palette[256][4]; 00038 unsigned int background; 00039 int modified; 00040 00041 static void map_file(void) 00042 { 00043 #ifndef __MINGW32__ 00044 size_t size = HEADER_SIZE + width * height * sizeof(unsigned int); 00045 void *ptr; 00046 int fd; 00047 00048 fd = open(file_name, O_RDWR); 00049 if (fd < 0) 00050 return; 00051 00052 ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, (off_t) 0); 00053 if (ptr == MAP_FAILED) 00054 return; 00055 00056 if (grid) 00057 G_free(grid); 00058 grid = (int *)((char *)ptr + HEADER_SIZE); 00059 00060 close(fd); 00061 00062 mapped = 1; 00063 #endif 00064 } 00065 00066 int PNG_Graph_set(int argc, char **argv) 00067 { 00068 unsigned int red, grn, blu; 00069 int do_read = 0; 00070 int do_map = 0; 00071 char *p; 00072 00073 G_gisinit("PNG driver"); 00074 00075 p = getenv("GRASS_PNGFILE"); 00076 if (!p || strlen(p) == 0) 00077 p = FILE_NAME; 00078 00079 file_name = p; 00080 00081 p = getenv("GRASS_TRUECOLOR"); 00082 true_color = p && strcmp(p, "TRUE") == 0; 00083 00084 G_message("PNG: GRASS_TRUECOLOR status: %s", 00085 true_color ? "TRUE" : "FALSE"); 00086 00087 p = getenv("GRASS_PNG_AUTO_WRITE"); 00088 auto_write = p && strcmp(p, "TRUE") == 0; 00089 00090 p = getenv("GRASS_PNG_MAPPED"); 00091 do_map = p && strcmp(p, "TRUE") == 0; 00092 00093 if (do_map) { 00094 char *ext = file_name + strlen(file_name) - 4; 00095 00096 if (G_strcasecmp(ext, ".bmp") != 0) 00097 do_map = 0; 00098 } 00099 00100 p = getenv("GRASS_PNG_READ"); 00101 do_read = p && strcmp(p, "TRUE") == 0; 00102 00103 if (do_read && access(file_name, 0) != 0) 00104 do_read = 0; 00105 00106 width = screen_right - screen_left; 00107 height = screen_bottom - screen_top; 00108 00109 clip_top = screen_top; 00110 clip_bot = screen_bottom; 00111 clip_left = screen_left; 00112 clip_rite = screen_right; 00113 00114 p = getenv("GRASS_TRANSPARENT"); 00115 has_alpha = p && strcmp(p, "TRUE") == 0; 00116 00117 init_color_table(); 00118 00119 p = getenv("GRASS_BACKGROUNDCOLOR"); 00120 if (p && *p && sscanf(p, "%02x%02x%02x", &red, &grn, &blu) == 3) 00121 background = get_color(red, grn, blu, has_alpha ? 255 : 0); 00122 else { 00123 /* 0xffffff = white, 0x000000 = black */ 00124 if (strcmp(DEFAULT_FG_COLOR, "white") == 0) 00125 /* foreground: white, background: black */ 00126 background = get_color(0, 0, 0, has_alpha ? 255 : 0); 00127 else 00128 /* foreground: black, background: white */ 00129 background = get_color(255, 255, 255, has_alpha ? 255 : 0); 00130 } 00131 00132 G_message 00133 ("PNG: collecting to file: %s,\n GRASS_WIDTH=%d, GRASS_HEIGHT=%d", 00134 file_name, width, height); 00135 00136 if (do_read && do_map) 00137 map_file(); 00138 00139 if (!mapped) 00140 grid = G_malloc(width * height * sizeof(unsigned int)); 00141 00142 if (!do_read) { 00143 PNG_Erase(); 00144 modified = 1; 00145 } 00146 00147 if (do_read && !mapped) 00148 read_image(); 00149 00150 if (do_map && !mapped) { 00151 write_image(); 00152 map_file(); 00153 } 00154 00155 return 0; 00156 }