|
1 | 1 | /****************************************************************************
|
2 |
| - * |
| 2 | + * |
3 | 3 | * MODULE: qgis.d.rast
|
4 | 4 | * AUTHOR(S): Radim Blazek <radim.blazek gmail.com>
|
5 | 5 | * using d.rast from GRASS
|
6 | 6 | * PURPOSE: display raster maps in active graphics display
|
7 |
| - * COPYRIGHT: (C) 2010 by Radim Blazek |
| 7 | + * COPYRIGHT: (C) 2010 by Radim Blazek |
8 | 8 | *
|
9 | 9 | * This program is free software under the GNU General Public
|
10 | 10 | * License (>=v2).
|
11 | 11 | *
|
12 | 12 | *****************************************************************************/
|
13 |
| -#define PACKAGE "grassmods" |
14 |
| - |
15 | 13 | #include <stdlib.h>
|
16 | 14 | #include <stdio.h>
|
17 | 15 | #include <string.h>
|
18 | 16 | #include <grass/gis.h>
|
19 | 17 | #include <grass/raster.h>
|
20 | 18 | #include <grass/display.h>
|
21 |
| -#include <grass/glocale.h> |
22 | 19 |
|
23 |
| -int display(char *name, char *mapset, RASTER_MAP_TYPE data_type); |
| 20 | +int display( char *name, char *mapset, RASTER_MAP_TYPE data_type ); |
24 | 21 |
|
25 |
| -int main(int argc, char **argv) |
| 22 | +int main( int argc, char **argv ) |
26 | 23 | {
|
27 |
| - char *mapset; |
28 |
| - char *name; |
29 |
| - int fp; |
30 |
| - struct GModule *module; |
31 |
| - struct Option *map; |
32 |
| - struct Option *win; |
33 |
| - struct Cell_head window; |
34 |
| - |
35 |
| - /* Initialize the GIS calls */ |
36 |
| - G_gisinit(argv[0]); |
37 |
| - |
38 |
| - module = G_define_module(); |
39 |
| - module->keywords = ("display, raster"); |
40 |
| - module->description = ("Output raster map layers in a format suitable for display in QGIS"); |
41 |
| - |
42 |
| - map = G_define_standard_option(G_OPT_R_MAP); |
43 |
| - map->description = ("Raster map to be displayed"); |
44 |
| - |
45 |
| - win = G_define_option(); |
46 |
| - win->key = "window"; |
47 |
| - win->type = TYPE_DOUBLE; |
48 |
| - win->multiple = YES; |
49 |
| - win->description = "xmin,ymin,xmax,ymax,ncols,nrows"; |
50 |
| - |
51 |
| - if (G_parser(argc, argv)) |
52 |
| - exit(EXIT_FAILURE); |
53 |
| - |
54 |
| - name = map->answer; |
55 |
| - |
56 |
| - G_get_window(&window); |
57 |
| - window.west = atof(win->answers[0]); |
58 |
| - window.south = atof(win->answers[1]); |
59 |
| - window.east = atof(win->answers[2]); |
60 |
| - window.north = atof(win->answers[3]); |
61 |
| - window.cols = atoi(win->answers[4]); |
62 |
| - window.rows = atoi(win->answers[5]); |
63 |
| - G_adjust_Cell_head(&window,1,1); |
64 |
| - G_set_window(&window); |
65 |
| - |
66 |
| - /* Make sure map is available */ |
67 |
| - mapset = G_find_cell2(name, ""); |
68 |
| - if (mapset == NULL) |
69 |
| - G_fatal_error(("Raster map <%s> not found"), name); |
70 |
| - |
71 |
| - |
72 |
| - fp = G_raster_map_is_fp(name, mapset); |
73 |
| - |
74 |
| - /* use DCELL even if the map is FCELL */ |
75 |
| - if (fp) |
76 |
| - display(name, mapset, DCELL_TYPE ); |
77 |
| - else |
78 |
| - display(name, mapset, CELL_TYPE ); |
79 |
| - |
80 |
| - exit(EXIT_SUCCESS); |
| 24 | + char *mapset; |
| 25 | + char *name; |
| 26 | + int fp; |
| 27 | + struct GModule *module; |
| 28 | + struct Option *map; |
| 29 | + struct Option *win; |
| 30 | + struct Cell_head window; |
| 31 | + |
| 32 | + /* Initialize the GIS calls */ |
| 33 | + G_gisinit( argv[0] ); |
| 34 | + |
| 35 | + module = G_define_module(); |
| 36 | + module->keywords = ( "display, raster" ); |
| 37 | + module->description = ( "Output raster map layers in a format suitable for display in QGIS" ); |
| 38 | + |
| 39 | + map = G_define_standard_option( G_OPT_R_MAP ); |
| 40 | + map->description = ( "Raster map to be displayed" ); |
| 41 | + |
| 42 | + win = G_define_option(); |
| 43 | + win->key = "window"; |
| 44 | + win->type = TYPE_DOUBLE; |
| 45 | + win->multiple = YES; |
| 46 | + win->description = "xmin,ymin,xmax,ymax,ncols,nrows"; |
| 47 | + |
| 48 | + if ( G_parser( argc, argv ) ) |
| 49 | + exit( EXIT_FAILURE ); |
| 50 | + |
| 51 | + name = map->answer; |
| 52 | + |
| 53 | + G_get_window( &window ); |
| 54 | + window.west = atof( win->answers[0] ); |
| 55 | + window.south = atof( win->answers[1] ); |
| 56 | + window.east = atof( win->answers[2] ); |
| 57 | + window.north = atof( win->answers[3] ); |
| 58 | + window.cols = atoi( win->answers[4] ); |
| 59 | + window.rows = atoi( win->answers[5] ); |
| 60 | + G_adjust_Cell_head( &window, 1, 1 ); |
| 61 | + G_set_window( &window ); |
| 62 | + |
| 63 | + /* Make sure map is available */ |
| 64 | + mapset = G_find_cell2( name, "" ); |
| 65 | + if ( mapset == NULL ) |
| 66 | + G_fatal_error(( "Raster map <%s> not found" ), name ); |
| 67 | + |
| 68 | + |
| 69 | + fp = G_raster_map_is_fp( name, mapset ); |
| 70 | + |
| 71 | + /* use DCELL even if the map is FCELL */ |
| 72 | + if ( fp ) |
| 73 | + display( name, mapset, DCELL_TYPE ); |
| 74 | + else |
| 75 | + display( name, mapset, CELL_TYPE ); |
| 76 | + |
| 77 | + exit( EXIT_SUCCESS ); |
81 | 78 | }
|
82 | 79 |
|
83 |
| -static int cell_draw(char *, char *, struct Colors *, RASTER_MAP_TYPE); |
| 80 | +static int cell_draw( char *, char *, struct Colors *, RASTER_MAP_TYPE ); |
84 | 81 |
|
85 |
| -int display(char *name, |
86 |
| - char *mapset, |
87 |
| - RASTER_MAP_TYPE data_type) |
| 82 | +int display( char *name, |
| 83 | + char *mapset, |
| 84 | + RASTER_MAP_TYPE data_type ) |
88 | 85 | {
|
89 |
| - struct Colors colors; |
| 86 | + struct Colors colors; |
90 | 87 |
|
91 |
| - if (G_read_colors(name, mapset, &colors) == -1) |
92 |
| - G_fatal_error(("Color file for <%s> not available"), name); |
| 88 | + if ( G_read_colors( name, mapset, &colors ) == -1 ) |
| 89 | + G_fatal_error(( "Color file for <%s> not available" ), name ); |
93 | 90 |
|
94 |
| - //G_set_null_value_color(r, g, b, &colors); |
| 91 | + //G_set_null_value_color(r, g, b, &colors); |
95 | 92 |
|
96 |
| - /* Go draw the raster map */ |
97 |
| - cell_draw(name, mapset, &colors, data_type); |
| 93 | + /* Go draw the raster map */ |
| 94 | + cell_draw( name, mapset, &colors, data_type ); |
98 | 95 |
|
99 |
| - /* release the colors now */ |
100 |
| - G_free_colors(&colors); |
| 96 | + /* release the colors now */ |
| 97 | + G_free_colors( &colors ); |
101 | 98 |
|
102 |
| - return 0; |
| 99 | + return 0; |
103 | 100 | }
|
104 | 101 |
|
105 |
| -static int cell_draw(char *name, |
106 |
| - char *mapset, |
107 |
| - struct Colors *colors, |
108 |
| - RASTER_MAP_TYPE data_type) |
| 102 | +static int cell_draw( char *name, |
| 103 | + char *mapset, |
| 104 | + struct Colors *colors, |
| 105 | + RASTER_MAP_TYPE data_type ) |
109 | 106 | {
|
110 |
| - int cellfile; |
111 |
| - void *xarray; |
112 |
| - int row; |
113 |
| - int ncols, nrows; |
114 |
| - static unsigned char *red, *grn, *blu, *set; |
115 |
| - int i; |
116 |
| - void *ptr; |
117 |
| - int big_endian; |
118 |
| - long one= 1; |
119 |
| - FILE *fo; |
120 |
| - |
121 |
| - big_endian = !(*((char *)(&one))); |
122 |
| - |
123 |
| - ncols = G_window_cols(); |
124 |
| - nrows = G_window_rows(); |
125 |
| - |
126 |
| - /* Make sure map is available */ |
127 |
| - if ((cellfile = G_open_cell_old(name, mapset)) == -1) |
128 |
| - G_fatal_error(("Unable to open raster map <%s>"), name); |
129 |
| - |
130 |
| - /* Allocate space for cell buffer */ |
131 |
| - xarray = G_allocate_raster_buf(data_type); |
132 |
| - red = G_malloc(ncols); |
133 |
| - grn = G_malloc(ncols); |
134 |
| - blu = G_malloc(ncols); |
135 |
| - set = G_malloc(ncols); |
136 |
| - |
137 |
| - /* some buggy C libraries require BOTH setmode() and fdopen(bin) ? */ |
138 |
| - //setmode(fileno(stdin), O_BINARY); |
139 |
| - fo = fdopen (fileno(stdout), "wb"); |
140 |
| - |
141 |
| - /* loop for array rows */ |
142 |
| - for ( row = 0; row < nrows; row++ ) { |
143 |
| - G_get_raster_row(cellfile, xarray, row, data_type); |
144 |
| - ptr = xarray; |
145 |
| - |
146 |
| - G_lookup_raster_colors(xarray, red, grn, blu, set, ncols, colors, |
147 |
| - data_type); |
148 |
| - |
149 |
| - for (i = 0; i < ncols; i++) { |
150 |
| - unsigned char alpha = 255; |
151 |
| - if ( G_is_null_value(ptr, data_type) ) { |
152 |
| - alpha = 0; |
153 |
| - } |
154 |
| - ptr = G_incr_void_ptr(ptr, G_raster_size(data_type)); |
155 |
| - |
156 |
| - |
157 |
| - // We need data suitable for QImage 32-bpp |
158 |
| - // the data are stored in QImage as QRgb which is unsigned int. |
159 |
| - // Because it depends on byte order of the platform we have to |
160 |
| - // consider byte order (well, middle endian ignored) |
161 |
| - if ( big_endian ) { |
162 |
| - // I have never tested this |
163 |
| - fprintf(fo, "%c%c%c%c", alpha, red[i],grn[i],blu[i]); |
164 |
| - } else { |
165 |
| - fprintf(fo, "%c%c%c%c", blu[i],grn[i],red[i],alpha); |
166 |
| - } |
167 |
| - } |
| 107 | + int cellfile; |
| 108 | + void *xarray; |
| 109 | + int row; |
| 110 | + int ncols, nrows; |
| 111 | + static unsigned char *red, *grn, *blu, *set; |
| 112 | + int i; |
| 113 | + void *ptr; |
| 114 | + int big_endian; |
| 115 | + long one = 1; |
| 116 | + FILE *fo; |
| 117 | + |
| 118 | + big_endian = !( *(( char * )( &one ) ) ); |
| 119 | + |
| 120 | + ncols = G_window_cols(); |
| 121 | + nrows = G_window_rows(); |
| 122 | + |
| 123 | + /* Make sure map is available */ |
| 124 | + if (( cellfile = G_open_cell_old( name, mapset ) ) == -1 ) |
| 125 | + G_fatal_error(( "Unable to open raster map <%s>" ), name ); |
| 126 | + |
| 127 | + /* Allocate space for cell buffer */ |
| 128 | + xarray = G_allocate_raster_buf( data_type ); |
| 129 | + red = G_malloc( ncols ); |
| 130 | + grn = G_malloc( ncols ); |
| 131 | + blu = G_malloc( ncols ); |
| 132 | + set = G_malloc( ncols ); |
| 133 | + |
| 134 | + /* some buggy C libraries require BOTH setmode() and fdopen(bin) ? */ |
| 135 | + //setmode(fileno(stdin), O_BINARY); |
| 136 | + fo = fdopen( fileno( stdout ), "wb" ); |
| 137 | + |
| 138 | + /* loop for array rows */ |
| 139 | + for ( row = 0; row < nrows; row++ ) |
| 140 | + { |
| 141 | + G_get_raster_row( cellfile, xarray, row, data_type ); |
| 142 | + ptr = xarray; |
| 143 | + |
| 144 | + G_lookup_raster_colors( xarray, red, grn, blu, set, ncols, colors, |
| 145 | + data_type ); |
| 146 | + |
| 147 | + for ( i = 0; i < ncols; i++ ) |
| 148 | + { |
| 149 | + unsigned char alpha = 255; |
| 150 | + if ( G_is_null_value( ptr, data_type ) ) |
| 151 | + { |
| 152 | + alpha = 0; |
| 153 | + } |
| 154 | + ptr = G_incr_void_ptr( ptr, G_raster_size( data_type ) ); |
| 155 | + |
| 156 | + |
| 157 | + // We need data suitable for QImage 32-bpp |
| 158 | + // the data are stored in QImage as QRgb which is unsigned int. |
| 159 | + // Because it depends on byte order of the platform we have to |
| 160 | + // consider byte order (well, middle endian ignored) |
| 161 | + if ( big_endian ) |
| 162 | + { |
| 163 | + // I have never tested this |
| 164 | + fprintf( fo, "%c%c%c%c", alpha, red[i], grn[i], blu[i] ); |
| 165 | + } |
| 166 | + else |
| 167 | + { |
| 168 | + fprintf( fo, "%c%c%c%c", blu[i], grn[i], red[i], alpha ); |
| 169 | + } |
168 | 170 | }
|
| 171 | + } |
169 | 172 |
|
170 |
| - G_close_cell(cellfile); |
171 |
| - return (0); |
| 173 | + G_close_cell( cellfile ); |
| 174 | + return ( 0 ); |
172 | 175 | }
|
0 commit comments