File: psscale.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 (614 lines) | stat: -rw-r--r-- 21,551 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/*--------------------------------------------------------------------
 *    The GMT-system:	@(#)psscale.c	2.82  01/21/00
 *
 *	Copyright (c) 1991-2000 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
 *--------------------------------------------------------------------*/
/*
 * psscale draws a grayscale or colorscale either vertically or
 * horizontally.  psscale will interpolate colors if the lower
 * and upper rgb values for an interval are different.  The
 * resolution for this interpolation can be set on the command line.
 * If the scale is to be used with illumninated 2-D and 3-D plots
 * then options for intensities are available.
 *
 * Author:	Paul Wessel
 * Created:	10-MAY-1991
 * Modified:	01-AUG-1998
 *		3.3   13-APR-1999. PW: Fixed bad -X -Y behavior
 *		3.3.2 15-SEP-1999. PW: Fixed bad loop for -Z
 *		3.3.4 21-JAN-2000. PW: Now paint rectangles for discrete cpt files
 *
 */

#include "gmt.h"

int bit_dpi = 300;

void GMT_draw_colorbar (double length, double width, double *z_width, BOOLEAN horizontal, BOOLEAN intens, double max_intens, BOOLEAN skip_lines, BOOLEAN extend, double e_length, BOOLEAN monochrome);
void fix_format (char *unit, char *format);

main(int argc, char **argv)
{
	int i, n;
	
	BOOLEAN horizontal = FALSE, equi = FALSE, intens = FALSE, error = FALSE;
	BOOLEAN skip_lines = FALSE, extend = FALSE, must_shift_back = FALSE, monochrome = FALSE;
	
	double xpos, ypos, length, width, max_intens = 1.0, dz;
	double *z_width, e_length = 0.0, x_origin, y_origin;
	
	char flag, *cpt_file = NULL, *z_file = NULL, line[BUFSIZ];
	char txt_a[32], txt_b[32], txt_c[32], txt_d[32], text[50];
	
	FILE *fp = NULL;
	
	argc = GMT_begin (argc, argv);

	length = width = xpos = ypos = x_origin = y_origin = 0.0;
	
	/* Check and interpret the command line arguments */
	
	for (i = 1; i < argc; i++) {
		if (argv[i][0] == '-') {
			switch(argv[i][1]) {
		
				/* Common parameters */

				case 'B':
				case 'K':
				case 'O':
				case 'P':
				case 'U':
				case 'V':
				case 'X':
				case 'x':
				case 'Y':
				case 'y':
				case 'c':
				case '\0':
					error += GMT_get_common_args (argv[i], 0, 0, 0, 0);
					break;
				
				/* Supplemental parameters */
			
				case 'C':
					cpt_file = &argv[i][2];
					break;
				case 'D':
					n = strlen(argv[i]) - 1;
					flag = argv[i][n];
					if (flag == 'h' || flag == 'H') {
						horizontal = TRUE;
						argv[i][n] = 0;
					}
					sscanf (&argv[i][2], "%[^/]/%[^/]/%[^/]/%s", txt_a, txt_b, txt_c, txt_d);
					xpos   = GMT_convert_units (txt_a, GMT_INCH);
					ypos   = GMT_convert_units (txt_b, GMT_INCH);
					length = GMT_convert_units (txt_c, GMT_INCH);
					width  = GMT_convert_units (txt_d, GMT_INCH);
					break;
				case 'E':
					if (argv[i][2]) e_length = GMT_convert_units (&argv[i][2], GMT_INCH);
					extend = TRUE;
					break;
				case 'I':
					intens = TRUE;
					if (argv[i][2]) max_intens = atof (&argv[i][2]);
					break;
				case 'M':
					monochrome = TRUE;
					break;
				case 'N':
					if (argv[i][2]) bit_dpi = atoi (&argv[i][2]);
					break;
				case 'L':
					equi = TRUE;
					break;
				case 'S':
					skip_lines = TRUE;
					break;
				case 'Z':
					z_file = &argv[i][2];
					break;
					
				/* Options not recognized */
						
				default:
					error = TRUE;
					GMT_default_error (argv[i][1]);
					break;
			}
		}
	}
	
	if (argc == 1 || GMT_quick) {
		fprintf (stderr,"%s %s - To create grayscale or colorscale for maps\n\n", GMT_program, GMT_VERSION);
		fprintf(stderr,"usage: psscale -D<xpos/ypos/length/width[h]> [-C<cpt_file>] [-E[<length>]] [-B<tickinfo>] [-I[<intens>]\n");
		fprintf(stderr,"	[-K] [-L] [-M] [-N<dpi>] [-O] [-P] [-S] [-U] [-V] [-X<x_shift>] [-Y<y_shift>] [-Z<zfile>] [-c<ncopies>]\n\n");
		if (GMT_quick) exit (EXIT_FAILURE);
		
		fprintf (stderr,"	-D set mid-point position and length/width for scale.\n");
		fprintf (stderr,"	   Give negative length to reverse the scalebar\n");
		fprintf (stderr,"	   Append h for horizontal scale\n");
		fprintf(stderr,"\n\tOPTIONS:\n");
		fprintf (stderr,"	-B Set scale anotation interval and label. Use y-label to set unit label\n");
		fprintf (stderr,"	   If no anotation interval is set it is taken from the cpt file\n");
		fprintf (stderr,"	-C Color palette file. If not set, stdin is read.\n");
		fprintf (stderr,"	   By default all color changes are anotated (but see -B).  To use a subset,\n");
		fprintf (stderr,"	   add an extra column to the cpt-file with a L, U, or B\n");
		fprintf (stderr,"	   to anotate Lower, Upper, or Both color segment boundaries\n");
		fprintf (stderr,"	-E add sidebar triangles for back- and forground colors.\n");
		fprintf (stderr,"	   Optionally, append triangle height [Default scales with barwidth]\n");
		fprintf (stderr,"	-I add illumination for +- <max_intens> [1.0]\n");
		GMT_explain_option ('K');
		fprintf (stderr,"	-L For equal-sized color rectangles\n");
		fprintf (stderr, "	-M force monochrome colorbar using YIQ transformation\n");
		fprintf (stderr,"	-N effective dots-per-inch for color scale [300]\n");
		GMT_explain_option ('O');
		GMT_explain_option ('P');
		fprintf (stderr,"	-S Skip drawing color boundary lines on color scale [Default draws lines]\n");
		GMT_explain_option ('U');
		GMT_explain_option ('V');
		GMT_explain_option ('X');
		fprintf (stderr,"	-Z give colorbar-width (in %s) per color entry\n", GMT_unit_names[gmtdefs.measure_unit]);
		fprintf (stderr,"	   By default, width of entry is scaled to color range\n");
		fprintf (stderr,"	   i.e., z = 0-100 gives twice the width as z = 100-150.\n");
		GMT_explain_option ('c');
		GMT_explain_option ('.');
		exit (EXIT_FAILURE);
	}

	/* Check that the options selected are mutually consistant */
	
	if (z_project.view_azimuth > 360.0 || z_project.view_elevation <= 0.0 || z_project.view_elevation > 90.0) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR -E option:  Enter azimuth in 0-360 range, elevation in 0-90 range\n", GMT_program);
		error++;
	}
	if (fabs (length) < SMALL ) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR -D option: scale length must be nonzero\n", GMT_program);
		error++;
	}
	if (width <= 0.0) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR -D option: scale width must be positive\n", GMT_program);
		error++;
	}
	if (bit_dpi < 1) {
		fprintf (stderr, "%s: GMT SYNTAX ERROR -N option: dpi must be > 0\n", GMT_program);
		error++;
	}
	      
	if (error) exit (EXIT_FAILURE);

	GMT_put_history (argc, argv);	/* Update .gmtcommands */

	if (gmtdefs.verbose) {
		if (cpt_file)
			fprintf (stderr, "%s: Reading CPT file %s.", GMT_program, cpt_file);
		else
			fprintf (stderr, "%s: Reading standard input.", GMT_program);
	}

	GMT_read_cpt (cpt_file);
	if (gmtdefs.verbose) fprintf (stderr, "  CPT range from %lg to %lg\n",
		GMT_lut[0].z_low, GMT_lut[GMT_n_colors-1].z_high);

	if (extend && e_length == 0.0) e_length = width * 0.5;

	z_width = (double *) GMT_memory (VNULL, (size_t)GMT_n_colors, sizeof (double), GMT_program);
	
	if (z_file && (fp = GMT_fopen (z_file, "r")) == NULL) {
		fprintf (stderr, "%s: Unable to open file %s\n", GMT_program, z_file);
		exit (EXIT_FAILURE);
	}
	else if (z_file) {	/* Opened successfully, now read */
		i = 0;
		while (fgets (line, BUFSIZ, fp)) {
			if (i == GMT_n_colors) {
				fprintf (stderr, "%s: -Z file %s has more slices than -C file %s!\n", GMT_program, z_file, cpt_file);
				exit (EXIT_FAILURE);
			}
			if (line[0] == '#' || line[0] == '\n') continue;	/* Skip comments and blank lines */
			sscanf (line, "%lf", &z_width[i++]);
		}
		GMT_fclose (fp);
		if (i < GMT_n_colors) {
			fprintf (stderr, "%s: -Z file %s has fewer slices than -C file %s!\n", GMT_program, z_file, cpt_file);
			exit (EXIT_FAILURE);
		}
	}
	else if (equi) {
		dz = fabs (length) / GMT_n_colors;
		for (i = 0; i < GMT_n_colors; i++) z_width[i] = dz;
	}
	else {
		for (i = 0; i < GMT_n_colors; i++) z_width[i] = fabs (length) * (GMT_lut[i].z_high - GMT_lut[i].z_low) / (GMT_lut[GMT_n_colors-1].z_high - GMT_lut[0].z_low);
	}

/*-----------------start of kludge-------------------------------------------*/

	/* Because psscale uses -D to position things we need to make some
	 * changes so that BoundingBox and others are set ~correctly */

	sprintf (text, "X%lg/%lg\0", length, width);
	GMT_map_getproject (text);	/* Fake linear projection */
	GMT_map_setup (GMT_lut[0].z_low, GMT_lut[GMT_n_colors-1].z_high, 0.0, width);

	/* We must do any origin translation manually in psscale */

	if (! (gmtdefs.x_origin == 0.0 && gmtdefs.y_origin == 0.0)) {	/* Must shift */
		must_shift_back = (gmtdefs.page_orientation & 8);	/* TRUE if absolute */
		x_origin = gmtdefs.x_origin;
		y_origin = gmtdefs.y_origin;
	}

	if (horizontal) {
		gmtdefs.x_origin = xpos - 0.5 * fabs (length);
		gmtdefs.y_origin = ypos - width;
		if (frame_info.anot_int[0] == 0.0) frame_info.plot = TRUE;
		frame_info.side[1] = frame_info.side[2] = frame_info.side[3] = 0;
	}
	else {
		gmtdefs.x_origin = xpos;
		gmtdefs.y_origin = ypos - 0.5 * fabs (length);
		if (frame_info.anot_int[0] == 0.0) frame_info.plot = TRUE;
		frame_info.side[0] = frame_info.side[2] = frame_info.side[3] = 0;
		d_swap (z_project.xmin, z_project.ymin);
		d_swap (z_project.xmax, z_project.ymax);
	}
	gmtdefs.page_orientation |= 8;	/* Ensure absolute coordinates */

/*-----------------end of kludge-------------------------------------------*/

	ps_plotinit (CNULL, 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);
	
	ps_transrotate (x_origin, y_origin, 0.0);

	ps_setpaint (gmtdefs.basemap_frame_rgb);
	
	GMT_draw_colorbar (length, width, z_width, horizontal, intens, max_intens, skip_lines, extend, e_length, monochrome);

	if (must_shift_back) ps_transrotate (-x_origin, -y_origin, 0.0);

	ps_plotend (gmtdefs.last_page);
	
	GMT_free ((void *)z_width);
	
	GMT_end (argc, argv);
}

void GMT_draw_colorbar (double length, double width, double *z_width, BOOLEAN horizontal, BOOLEAN intens, double max_intens, BOOLEAN skip_lines, BOOLEAN extend, double e_length, BOOLEAN monochrome)
{
	int i, ii, j, ndec = -1, dec, rgb[3], rrggbb[3];
	int nx, ny, nm, barmem, k;
	BOOLEAN reverse, all = TRUE, simple;
	char format[64], text[64], test[64];
	unsigned char *bar;
	double off, anot_off, label_off, len, len2, size, x0, x1, dx, xx, xt[3], yt[3];
	double z, xleft, xright, inc_i, inc_j, get_z(double x, double *width, int n);
	
	ps_setfont (gmtdefs.anot_font);

	gmtdefs.anot_offset = fabs (gmtdefs.anot_offset);	/* No 'inside' anotations allowed in colorbar */

	/* Find max decimals needed */
	
	if (frame_info.anot_int[0] > 0.0) {     /* Used -B */
		ndec = GMT_get_format (frame_info.anot_int[0], frame_info.unit[0], format);
	}
	else {
		for (i = 0; i < GMT_n_colors; i++) {
			if (GMT_lut[i].anot & 1) {
				if ((dec = GMT_get_format (GMT_lut[i].z_low, CNULL, text)) > ndec) {
					strcpy (format, text);
					ndec = dec;
				}
			}
			if (GMT_lut[i].anot & 2) {
				if ((dec = GMT_get_format (GMT_lut[i].z_high, CNULL, text)) > ndec) {
					strcpy (format, text);
					ndec = dec;
				}
			}
			if (GMT_lut[i].anot) all = FALSE;
		}
	}
	
	if (ndec == 0) {	/* Not -B and no decimals are needed */
		strcpy (format, gmtdefs.d_format);
		fix_format (frame_info.unit[0], format);	/* Add units if needed */
	}

	len = gmtdefs.tick_length;	/* +ve means draw on the outside of bar */
	len2 = 0.5 * len;
	xleft = x0 = 0.0;

	reverse = (length < 0.0);
	length = fabs (length);
	xright = length;

	simple = (GMT_continuous && !intens);
	
	if (simple) {	/* Make bitimage for colorbar using bit_dpi */
		nx = irint (length * bit_dpi);
		ny = irint (width * bit_dpi);
		nm = nx * ny;
		inc_i = 1.0 / bit_dpi;
		inc_j = (intens) ? 2.0 * max_intens / (ny - 1) : 0.0;
		barmem = (monochrome || GMT_gray) ? nm : 3 * nm;
		bar = (unsigned char *) GMT_memory (VNULL, (size_t)barmem, sizeof (char), GMT_program);
	
		/* Load bar image */
	
		for (i = 0; i < nx; i++) {
			z = get_z (i * inc_i, z_width, GMT_n_colors);
			GMT_get_rgb24 (z, rrggbb);
			ii = (reverse) ? nx - i - 1 : i;
			for (j = 0; j < ny; j++) {
				for (k = 0; k < 3; k++) rgb[k] = rrggbb[k];
				k = j * nx + ii;
				if (intens) GMT_illuminate (max_intens - j * inc_j, rgb);
				if (GMT_gray)	/* All gray, pick red */
					bar[k] =  (unsigned char) rgb[0];
				else if (monochrome)	/* Convert to gray using the YIQ transformation */
					bar[k] =  (unsigned char) YIQ (rgb);
				else {
					k *= 3;
					bar[k++] = (unsigned char) rgb[0];
					bar[k++] = (unsigned char) rgb[1];
					bar[k++] = (unsigned char) rgb[2];
				}
			}
		}
	}
	
	GMT_setpen (&gmtdefs.frame_pen);

	/* Current point (0,0) is now at lower left location of bar */

	if (horizontal) {
		if (simple) {	/* Must plot as image */
			if (monochrome || GMT_gray)
				ps_image (0.0, 0.0, length, width, bar, nx, ny, 8);
			else 
				GMT_color_image (0.0, 0.0, length, width, bar, nx, ny);
		}
		else {			/* Plot as rectangles */
			x0 = x1 = 0.0;
			for (i = 0; i < GMT_n_colors; i++) {
				x1 += z_width[i];
				ps_rect (x0, 0.0, x1, width, GMT_lut[i].rgb_low, FALSE);
				x0 = x1;
			}
		}
		
		if (extend) {	/* Add color triangles for fore- and background */
			xt[0] = xt[1] = xleft;	xt[2] = xt[0] - e_length;
			yt[0] = width;	yt[1] = 0.0;	yt[2] = 0.5 * width;
			ps_polygon (xt, yt, 3, GMT_bfn.background_rgb, 1);
			xt[0] = xt[1] = xright;	xt[2] = xt[0] + e_length;
			yt[0] = width;	yt[1] = 0.0;	yt[2] = 0.5 * width;
			ps_polygon (xt, yt, 3, GMT_bfn.foreground_rgb, 1);
		}
		
		ps_plot (xleft, 0.0, 3);	ps_plotr (length, 0.0, 2);
		ps_plot (xleft, width, 3);	ps_plotr (length, 0.0, 2);
		ps_plot (xleft, 0.0, 3);	ps_plotr (0.0, width, 2);
		ps_plot (xright, 0.0, 3);	ps_plotr (0.0, width, 2);

		if (fabs (frame_info.anot_int[0]) > 0.0) {	/* Used -B */
			GMT_x_axis (xleft, 0.0, length, GMT_lut[0].z_low, GMT_lut[GMT_n_colors-1].z_high, frame_info.anot_int[0], frame_info.frame_int[0], frame_info.label[0], frame_info.unit[0], LINEAR, 0, TRUE, 1);
			dx = fabs (frame_info.grid_int[0]); 
			if (dx > 0.0) GMT_linearx_grid (GMT_lut[0].z_low, GMT_lut[GMT_n_colors-1].z_high, 0.0, width, dx);
		}
		else {
			anot_off = (((len > 0.0) ? len : 0.0) + gmtdefs.anot_offset);
			label_off = -(anot_off + 1.5 * gmtdefs.anot_offset + (gmtdefs.anot_font_size * GMT_u2u[GMT_PT][GMT_INCH]) * GMT_font_height[gmtdefs.anot_font]);
		
			/* First draw gridlines, unless skip_lines is TRUE */
			
			if (!skip_lines) {
				GMT_setpen (&gmtdefs.grid_pen);
				x1 = xleft;
				for (i = 0; i < GMT_n_colors; i++) {
					xx = (reverse) ? xright - x1 : x1;
					ps_plot (xx, 0.0, 3);
					ps_plotr (0.0, width, 2);
					x1 += z_width[i];
				}
				xx = (reverse) ? xleft : xright;
				ps_plot (xx, 0.0, 3);
				ps_plotr (0.0, width, 2);
			}
			
			/* Then anotate and draw tickmarks */
			
			GMT_setpen (&gmtdefs.tick_pen);
			x1 = xleft;
			for (i = 0; i < GMT_n_colors; i++) {
				xx = (reverse) ? xright - x1 : x1;
				ps_plot (xx, 0.0, 3);
				if (all || (GMT_lut[i].anot & 1)) {	/* Anotate this */
					ps_plotr (0.0, -len, 2);
					sprintf (text, format, GMT_lut[i].z_low);
					ps_text (xx, -anot_off, gmtdefs.anot_font_size, text, 0.0, 10, 0);
				}
				else
					ps_plotr (0.0, -len2, 2);
				x1 += z_width[i];
			}
			xx = (reverse) ? xleft : xright;
			ps_plot (xx, 0.0, 3);
			if (all || (GMT_lut[GMT_n_colors-1].anot & 2)) {
				ps_plotr (0.0, -len, 2);
				sprintf (text, format, GMT_lut[GMT_n_colors-1].z_high);
				ps_text (xx, -anot_off, gmtdefs.anot_font_size, text, 0.0, 10, 0);
			}
			else
				ps_plotr (0.0, -len2, 2);

			if (frame_info.label[0][0]) {
				ps_setfont (gmtdefs.label_font);
				ps_text (xleft+0.5*length, label_off, gmtdefs.label_font_size, frame_info.label[0], 0.0, 10, 0);
			}
		}
		if (frame_info.label[1][0]) {	/* Add unit label */
			ps_setfont (gmtdefs.anot_font);
			ps_text (xright+e_length+gmtdefs.anot_offset, 0.5*width, gmtdefs.anot_font_size, frame_info.label[1], 0.0, 5, 0);
		}
	}
	else {	/* Vertical scale */
		ps_transrotate (width, 0.0, 90.0);
		if (simple) {	/* Must plot with image */
			if (monochrome || GMT_gray)
				ps_image (0.0, 0.0, length, width, bar, nx, ny, 8);
			else 
				GMT_color_image (0.0, 0.0, length, width, bar, nx, ny);
		}
		else {			/* Plot as rectangles */
			x0 = x1 = 0.0;
			for (i = 0; i < GMT_n_colors; i++) {
				x1 += z_width[i];
				ps_rect (x0, 0.0, x1, width, GMT_lut[i].rgb_low, FALSE);
				x0 = x1;
			}
		}
		sprintf (text, "%d\0", (int)fabs (floor (GMT_lut[0].z_low)));
		sprintf (test, "%d\0", (int)fabs (ceil (GMT_lut[GMT_n_colors-1].z_high)));
		off = ((MAX ((int)strlen (text), (int)strlen (test)) + ndec) * 0.49 + ((ndec > 0) ? 0.3 : 0.0) + ((GMT_lut[0].z_low < 0.0) ? 0.3 : 0.0))
			* gmtdefs.anot_font_size * GMT_u2u[GMT_PT][GMT_INCH];
		anot_off = -(((len > 0.0) ? len : 0.0) + gmtdefs.anot_offset + off);
		label_off = anot_off - 0.7 * gmtdefs.anot_font_size * GMT_u2u[GMT_PT][GMT_INCH] - 1.5 * gmtdefs.anot_offset;
		if (extend) {	/* Add color triangles for fore- and background */
			xt[0] = xt[1] = xleft;	xt[2] = xt[0] - e_length;
			yt[0] = width;	yt[1] = 0.0;	yt[2] = 0.5 * width;
			ps_polygon (xt, yt, 3, GMT_bfn.background_rgb, 1);
			xt[0] = xt[1] = xright;	xt[2] = xt[0] + e_length;
			yt[0] = width;	yt[1] = 0.0;	yt[2] = 0.5 * width;
			ps_polygon (xt, yt, 3, GMT_bfn.foreground_rgb, 1);
		}
		ps_plot (xleft, 0.0, 3);	ps_plotr (length, 0.0, 2);
		ps_plot (xleft, width, 3);	ps_plotr (length, 0.0, 2);
		ps_plot (xleft, 0.0, 3);	ps_plotr (0.0, width, 2);
		ps_plot (xright, 0.0, 3);	ps_plotr (0.0, width, 2);
		if (fabs (frame_info.anot_int[0]) > 0.0) {	/* Used -B */
			dx = fabs (frame_info.grid_int[0]); 
			if (dx > 0.0) GMT_linearx_grid (GMT_lut[0].z_low, GMT_lut[GMT_n_colors-1].z_high, 0.0, width, dx);
			ps_transrotate (0.0, 0.0, -90.0);
			GMT_y_axis (0.0, 0.0, length, GMT_lut[0].z_low, GMT_lut[GMT_n_colors-1].z_high, frame_info.anot_int[0], frame_info.frame_int[0], CNULL, CNULL, LINEAR, 0, FALSE, 1);
			ps_rotatetrans (0.0, 0.0, 90.0);
		}
		else {
			if (!skip_lines) {	/* First draw gridlines */
				GMT_setpen (&gmtdefs.grid_pen);
				x1 = xleft;
				for (i = 0; i < GMT_n_colors; i++) {
					xx = (reverse) ? xright - x1 : x1;
					ps_plot (xx, 0.0, 3);
					ps_plotr (0.0, width, 2);
					x1 += z_width[i];
				}
				xx = (reverse) ? xleft : xright;
				ps_plot (xx, 0.0, 3);
				ps_plotr (0.0, width, 2);
			}
			
			/* Then anotate and draw tickmarks */
			
			GMT_setpen (&gmtdefs.tick_pen);
			x1 = xleft;
			for (i = 0; i < GMT_n_colors; i++) {
				xx = (reverse) ? xright - x1 : x1;
				ps_plot (xx, 0.0, 3);
				if (all || (GMT_lut[i].anot & 1)) {
					ps_plotr (0.0, -len, 2);
					sprintf (text, format, GMT_lut[i].z_low);
					ps_text (xx, anot_off, gmtdefs.anot_font_size, text, -90.0, 7, 0);
				}
				else
					ps_plotr (0.0, -len2, 2);
				x1 += z_width[i];
			}
			xx = (reverse) ? xleft : xright;
			ps_plot (xx, 0.0, 3);
			if (all || (GMT_lut[GMT_n_colors-1].anot & 2)) {
				ps_plotr (0.0, -len, 2);
				sprintf (text, format, GMT_lut[GMT_n_colors-1].z_high);
				ps_text (xx, anot_off, gmtdefs.anot_font_size, text, -90.0, 7, 0);
			}
			else
				ps_plotr (0.0, -len2, 2);
		}

		if (frame_info.label[0][0]) {
			ps_setfont (gmtdefs.label_font);
			size = 0.9 * gmtdefs.label_font_size * GMT_u2u[GMT_PT][GMT_INCH];
			x0 = 0.5 * (length + ((int)strlen (frame_info.label[0]) -1) * size);
			text[1] = 0;
			for (i = 0; i < (int)strlen (frame_info.label[0]); i++) {
				x1 = x0 - i * size;
				text[0] = frame_info.label[0][i];
				ps_text (x1, label_off, gmtdefs.label_font_size, text, -90.0, 6, 0);
			}
		}
		if (frame_info.label[1][0]) {	/* Add unit label */
			ps_setfont (gmtdefs.anot_font);
			ps_text (xright + gmtdefs.anot_offset + e_length, 0.5 * width, gmtdefs.anot_font_size, frame_info.label[1], -90.0, 2, 0);
		}
		ps_rotatetrans (-width, 0.0, -90.0);

	}
	if (simple) GMT_free ((void *)bar);
}

double get_z (double x, double *width, int n)
{
	int i = 0;
	double tmp;
	
	tmp = width[0];
	while (i < n && x > tmp) tmp += width[++i];
	if (i == n) return (GMT_lut[GMT_n_colors-1].z_high);
	return (GMT_lut[i].z_low + (x - tmp + width[i]) * (GMT_lut[i].z_high - GMT_lut[i].z_low) / width[i]);
}

void fix_format (char *unit, char *format)
{
	int i, j;
	char text[80], new_format[BUFSIZ];
	
	/* Check if anotation units should be added */
	
	if (unit && unit[0]) {	/* Must append the unit string */
		if (!strchr (unit, '%'))	/* No percent signs */
			strncpy (text, unit, 80);
		else {
			for (i = j = 0; i < (int)strlen (unit); i++) {
				text[j++] = unit[i];
				if (unit[i] == '%') text[j++] = unit[i];
			}
			text[j] = 0;
		}
		if (text[0] == '-')	/* No space between anotation and unit */
			sprintf (new_format, "%s%s\0", format, &text[1]);
		else		/* 1 space between anotation and unit */
			sprintf (new_format, "%s %s\0", format, text);
		strcpy (format, new_format);
	}
}