File: psimage.c

package info (click to toggle)
gmt 3.3.3-3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 3,288 kB
  • ctags: 2,962
  • sloc: ansic: 50,470; sh: 1,760; makefile: 284; asm: 38
file content (280 lines) | stat: -rw-r--r-- 8,965 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*--------------------------------------------------------------------
 *    The GMT-system:	@(#)psimage.c	2.48  11/05/99
 *
 *	Copyright (c) 1991-1999 by P. Wessel and W. H. F. Smith
 *	See COPYING file for copying and redistribution conditions.
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; version 2 of the License.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	Contact info: www.soest.hawaii.edu/gmt
 *--------------------------------------------------------------------*/
/*
 * psimage reads a 1, 8, 24, or 32 bit Sun rasterfile and plots it on the page
 *
 * Author:	Paul Wessel
 * Date:	26-MAR-1999
 * Version:	3.3.3
 *
 */

#include "gmt.h"

void place_image (unsigned char *buffer, double x0, double y0, double xlength, double ylength, int nx, int ny, int depth, int polarity, int rgb[]);

main (int argc, char **argv)
{
	int i, j, nn = -1, n_rep_x = 1, n_rep_y = 1, f_rgb[3], b_rgb[3], polarity = 0, *rgb;
	
	BOOLEAN error = FALSE, invert = TRUE, monochrome = FALSE, replicate = FALSE, colorize = FALSE, frame = FALSE;
	
	double x0 = 0.0, y0 = 0.0, y, xlength = 0.0, ylength = 0.0, dpi = 0.0;
	
	char rasterfile[BUFSIZ], txt_a[32], txt_b[32];

	unsigned char *picture;
	
	struct rasterfile header;
	
	struct GMT_PEN pen;

	FILE *fp = NULL;

	GMT_init_pen (&pen, GMT_PENWIDTH);
	for (i = 0; i < 3; i++) {
		f_rgb[i] = 0;
		b_rgb[i] = 255;
	}
	
	argc = GMT_begin (argc, argv);
	
	/* Check and interpret the command line arguments */
	
	for (i = 1; i < argc; i++) {
		if (argv[i][0] == '-') {
			switch(argv[i][1]) {
		
				/* Common parameters */
			
				case 'K':
				case 'O':
				case 'P':
				case 'U':
				case 'V':
				case 'X':
				case 'x':
				case 'Y':
				case 'y':
				case 'c':
				case ':':
				case '\0':
					error += GMT_get_common_args (argv[i], 0, 0, 0, 0);
					break;
				
				/* Supplemental parameters */
			
				case 'C':
					sscanf (&argv[i][2], "%[^/]/%s", txt_a, txt_b);
					x0 = GMT_convert_units (txt_a, GMT_INCH);
					y0 = GMT_convert_units (txt_b, GMT_INCH);
					break;
				case 'E':	/* Specify image dpi */
					dpi = atof (&argv[i][2]);
					break;
				case 'F':	/* Specify frame pen */
					if (argv[i][2] && GMT_getpen (&argv[i][2], &pen)) {
						GMT_pen_syntax ('F');
						error++;
					}
					frame = TRUE;
					break;
				case 'G':
					colorize = TRUE;
					switch (argv[i][2]) {
						case 'F':
						case 'f':
							if (argv[i][3] == '-' && argv[i][4] == '\0')
	f_rgb[0] = f_rgb[1] = f_rgb[2] = -1;
							else if (GMT_getrgb (&argv[i][3], f_rgb)) {
								GMT_rgb_syntax ('G');
								error++;
							}
							break;
						case 'B':
						case 'b':
							if (argv[i][3] == '-' && argv[i][4] == '\0')
	b_rgb[0] = b_rgb[1] = b_rgb[2] = -1;
							else if (GMT_getrgb (&argv[i][3], b_rgb)) {
								GMT_rgb_syntax ('G');
								error++;
							}
							break;
						default:	/* Same as -Gf */
							if (argv[i][2] == '-' && argv[i][3] == '\0')
	f_rgb[0] = f_rgb[1] = f_rgb[2] = -1;
							else if (GMT_getrgb (&argv[i][2], f_rgb)) {
								GMT_rgb_syntax ('G');
								error++;
							}
							break;
					}
					break;
				case 'I':	/* Opposite sense than expected to match -Gfill behavior */
					invert = FALSE;
					break;
				case 'M':
					monochrome = TRUE;
					break;
				case 'N':
					replicate = TRUE;
					nn = sscanf (&argv[i][2], "%d/%d", &n_rep_x, &n_rep_y);
					break;
				case 'W':
					sscanf (&argv[i][2], "%[^/]/%s", txt_a, txt_b);
					xlength = GMT_convert_units (txt_a, GMT_INCH);
					ylength = GMT_convert_units (txt_b, GMT_INCH);
					break;
					
				/* Options not recognized */
						
				default:
					error = TRUE;
					fprintf (stderr, "GMT SYNTAX ERROR:  Unrecognized option -%c\n", argv[i][1]);
					break;
			}
		}
		else
			strcpy (rasterfile, argv[i]);
	}
	
	if (argc == 1 || GMT_quick) {
		fprintf (stderr,"psimage %s - To plot SUN rasterfiles on maps\n\n", GMT_VERSION);
		fprintf(stderr,"usage: psimage <rasterfile> [-E<dpi> or -W<xlength>[/<ylength>]] [-C<xpos>/<ypos>]\n");
		fprintf (stderr, "	[-F<pen>] [-G[f|b]<rgb>] [-I] [-K] [-M] [-N<nx/ny>] [-O] [-P] [-U[label]]\n");
		fprintf (stderr, "	[-V] [-X<x_shift>] [-Y<y_shift>] [-c<ncopies>]\n\n");
		
		if (GMT_quick) exit (EXIT_FAILURE);
		
		fprintf(stderr,"	<rasterfile> is a 1, 8, 24, or 32-bit Sun rasterfile.\n");
		fprintf (stderr, "	-E sets image dpi (dots per inch), OR\n");
		fprintf (stderr, "	-W sets the size of the image.  If <ylength> = 0, it is\n");
		fprintf (stderr, "	   assumed to be <xlength> * (ny / nx).\n");
		fprintf(stderr,"\n\tOPTIONS:\n");
		fprintf(stderr,"	-C sets the lower left position on the map for raster image [0/0].\n");
		fprintf(stderr,"	-F draws a frame around the image with the given pen.\n");
		fprintf(stderr,"	-Gf and -Gb sets the foreground and background color, respectively,\n");
		fprintf(stderr,"	   for 1-bit images to be colorized [Default is black and white]\n");
		fprintf(stderr,"	   Set <rgb> = - for transparency\n");
		fprintf(stderr,"	-I invert 1-bit images (does not affect 8 or 24-bit images).\n");
		GMT_explain_option ('K');
		fprintf(stderr,"	-M Force color -> monochrome image using YIQ-transformation.\n");
		fprintf(stderr,"	-N Replicate image nx by ny times [Default is no replication]\n");
		GMT_explain_option ('O');
		GMT_explain_option ('P');
		GMT_explain_option ('U');
		GMT_explain_option ('V');
		GMT_explain_option ('X');
		GMT_explain_option ('c');
		GMT_explain_option ('.');
		exit (EXIT_FAILURE);
	}

	/* Check that the options selected are mutually consistant */
	
	if (!rasterfile[0]) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR:  Must specify input raster file\n", GMT_program);
		error++;
	}
	if (xlength <= 0.0 && dpi <= 0.0) {
		fprintf (stderr, "%s: Must specify image width (-W) or dpi (-E)\n", GMT_program);
		error++;
	}
	if ((replicate && nn < 0) || n_rep_x < 1 || n_rep_y < 1) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR -N option:  Must specify positive values for replication\n", GMT_program);
		error++;
	}
	if (f_rgb[0] < 0 && b_rgb[0] < 0) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR -G option:  Only one of fore/back-ground can be transpacent for1-bit images\n", GMT_program);
		error++;
	}
	if (error) exit (EXIT_FAILURE);

	GMT_put_history (argc, argv);	/* Update .gmtcommands */
	
	if ((fp = GMT_fopen (rasterfile, "rb")) == NULL) {
		fprintf (stderr, "%s: Cannot load file %s\n", GMT_program, rasterfile);
		exit (EXIT_FAILURE);
	}

	if (ps_read_rasheader (fp, &header)) {
		fprintf (stderr, "%s: Trouble reading Sun rasterfile header!\n", GMT_program);
		exit (EXIT_FAILURE);
	}
	
	picture = ps_loadraster (fp, &header, invert, monochrome, colorize, f_rgb, b_rgb);

	if (!picture) {
		fprintf (stderr, "%s: Trouble loading/converting Sun rasterfile!\n", GMT_program);
		exit (EXIT_FAILURE);
	}
	
	if (f_rgb[0] < 0 || b_rgb[0] < 0) {
		colorize = FALSE;
		if (f_rgb[0] < 0) {
			polarity = 1;	/* 0 + 1 */
			rgb = f_rgb;
		}
		else {
			polarity = 2;	/* 1 + 1 */
			rgb = b_rgb;
		}
	}

	if (!project_info.x_off_supplied && gmtdefs.overlay) gmtdefs.x_origin = 0.0;	/* Since map_setup is not called here */
	if (!project_info.y_off_supplied && gmtdefs.overlay) gmtdefs.y_origin = 0.0;

	ps_plotinit (NULL, gmtdefs.overlay, gmtdefs.page_orientation, gmtdefs.x_origin, gmtdefs.y_origin,
		gmtdefs.global_x_scale, gmtdefs.global_y_scale, gmtdefs.n_copies,
		gmtdefs.dpi, GMT_INCH, gmtdefs.paper_width, gmtdefs.page_rgb, GMT_epsinfo (argv[0]));
		
	GMT_echo_command (argc, argv);
	if (gmtdefs.unix_time) GMT_timestamp (argc, argv);
	
	if (dpi > 0.0) xlength = (double) header.ras_width / dpi;
	if (ylength == 0.0) ylength = header.ras_height * xlength / header.ras_width;

	for (j = 0; j < n_rep_y; j++) {
		y = y0 + j * ylength;
		if (n_rep_y > 1 && gmtdefs.verbose) fprintf (stderr, "%s: Replicating image %d times for row %d\n", GMT_program, n_rep_x, j);
		for (i = 0; i < n_rep_x; i++) {
			place_image (picture, x0 + i * xlength, y, xlength, ylength, header.ras_width, header.ras_height, header.ras_depth, polarity, rgb);
		}
	}
 
 	if (frame) {
 		GMT_setpen (&pen);
 		ps_rect (x0, y0, x0 + (n_rep_x * xlength), y0 + (n_rep_y * ylength), GMT_no_rgb, TRUE);
 	}
 	
	ps_plotend (gmtdefs.last_page);
	
	ps_free ((void *)picture);

	GMT_end (argc, argv);
}

void place_image (unsigned char *buffer, double x0, double y0, double xlength, double ylength, int nx, int ny, int depth, int polarity, int rgb[])
{
	if (depth == 1 && polarity)
		ps_imagemask (x0, y0, xlength, ylength, buffer, nx, ny, polarity-1, rgb);
	else if (depth <= 8)
		ps_image (x0, y0, xlength, ylength, buffer, nx, ny, depth);
	else
		GMT_color_image (x0, y0, xlength, ylength, buffer, nx, ny);
}