-
Notifications
You must be signed in to change notification settings - Fork 346
/
gmt_support.c
15728 lines (14121 loc) · 642 KB
/
gmt_support.c
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*--------------------------------------------------------------------
*
* Copyright (c) 1991-2018 by P. Wessel, W. H. F. Smith, R. Scharroo, J. Luis and F. Wobbe
* See LICENSE.TXT 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 Lesser General Public License as published by
* the Free Software Foundation; version 3 or any later version.
*
* 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 Lesser General Public License for more details.
*
* Contact info: gmt.soest.hawaii.edu
*--------------------------------------------------------------------*/
/*
*
* G M T _ S U P P O R T . C
*
*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* GMT_support.c contains code used by most GMT programs
*
* Author: Paul Wessel
* Date: 1-JAN-2010
* Version: 5
*
* Modules in this file:
*
* gmtlib_akima Akima's 1-D spline
* gmt_BC_init Initialize BCs for a grid or image
* gmt_grd_BC_set Set two rows of padding according to bound cond for grid
* gmtlib_image_BC_set Set two rows of padding according to bound cond for image
* support_check_rgb Check rgb for valid range
* support_cmyk_to_rgb Corvert CMYK to RGB
* support_comp_double_asc Used when sorting doubles into ascending order [checks for NaN]
* support_comp_float_asc Used when sorting floats into ascending order [checks for NaN]
* support_comp_int_asc Used when sorting ints into ascending order
* gmt_contours Subroutine for contouring
* gmtlib_cspline Natural cubic 1-D spline solver
* support_csplint Natural cubic 1-D spline evaluator
* gmt_delaunay Performs a Delaunay triangulation
* gmtlib_get_annot_label Construct degree/minute label
* gmtlib_get_annot_offset Return offset in inches for text annotation
* gmt_get_index Return color table entry for given z
* gmt_get_fill_from_z Return fill type for given z
* gmt_get_format Find # of decimals and create format string
* gmt_get_rgb_from_z Return rgb for given z
* gmt_get_plot_array Allocate memory for plotting arrays
* gmt_getfill Decipher and check fill argument
* gmt_getinc Decipher and check increment argument
* gmt_getpen Decipher and check pen argument
* gmt_getrgb Decipher and check color argument
* support_hsv_to_rgb Convert HSV to RGB
* gmt_init_fill Initialize fill attributes
* gmt_init_pen Initialize pen attributes
* gmt_illuminate Add illumination effects to rgb
* gmt_intpol 1-D interpolation
* support_lab_to_rgb Corvert CIELAB LAB to RGB
* support_lab_to_xyz Convert CIELAB LAB to XYZ
* gmt_non_zero_winding Finds if a point is inside/outside a polygon
* gmt_putpen Encode pen argument into textstring
* gmtlib_read_cpt Read color palette file
* support_rgb_to_cmyk Convert RGB to CMYK
* support_rgb_to_hsv Convert RGB to HSV
* support_rgb_to_lab Convert RGB to CMYK
* support_rgb_to_xyz Convert RGB to CIELAB XYZ
* gmt_sample_cpt Resamples the current CPT based on new z-array
* gmt_invert_cpt Flips the current CPT upside down
* support_smooth_contour Use Akima's spline to smooth contour
* GMT_shift_refpoint Adjust reference point based on size and justification of plotted item
* gmt_sprintf_float Make formatted string from float, while checking for %-apostrophe
* support_trace_contour Function that trace the contours in gmt_contours
* support_polar_adjust Adjust label justification for polar projection
* support_xyz_to_rgb Convert CIELAB XYZ to RGB
* support_xyz_to_lab Convert CIELAB XYZ to LAB
*/
/*!
* \file gmt_support.c
* \brief GMT_support.c contains code used by most GMT programs.
*/
#include "gmt_dev.h"
#include "gmt_internals.h"
#include <locale.h>
#ifndef WIN32
#include <glob.h>
#endif
/*! . */
enum GMT_profmode {
GMT_GOT_AZIM = 1,
GMT_GOT_ORIENT = 2,
GMT_GOT_LENGTH = 4,
GMT_GOT_NP = 8,
GMT_GOT_INC = 16,
GMT_GOT_RADIUS = 32,
};
/*! . */
enum gmt_ends {
BEG = 0,
END = 1
};
/*! Internal struct used in the processing of CPT z-scaling and truncation */
struct CPT_Z_SCALE {
unsigned int z_adjust; /* 1 if +u<unit> was parsed and scale set, 3 if z has been adjusted, 0 otherwise */
unsigned int z_mode; /* 1 if +U<unit> was parsed, 0 otherwise */
unsigned int z_unit; /* Unit enum specified via +u<unit> */
double z_unit_to_meter; /* Scale, given z_unit, to convert z from <unit> to meters */
};
EXTERN_MSC double gmt_distance_type (struct GMT_CTRL *GMT, double lonS, double latS, double lonE, double latE, int id);
EXTERN_MSC char * gmtlib_getuserpath (struct GMT_CTRL *GMT, const char *stem, char *path); /* Look for user file */
EXTERN_MSC int64_t gmt_parse_range (struct GMT_CTRL *GMT, char *p, int64_t *start, int64_t *stop);
static char *GMT_just_code[12] = {"--", "LB", "CB", "RB", "--", "LM", "CM", "RM", "--", "LT", "CT", "RT"};
#define gmt_M_uneven_interval(unit) ((unit == 'o' || unit == 'O' || unit == 'k' || unit == 'K' || unit == 'R' || unit == 'r' || unit == 'D' || unit == 'd') ? true : false) /* true for uneven units */
/** @brief XYZ color of the D65 white point */
#define WHITEPOINT_X 0.950456
#define WHITEPOINT_Y 1.0
#define WHITEPOINT_Z 1.088754
/**
* @brief sRGB gamma correction, transforms R to R'
* http:https://en.wikipedia.org/wiki/SRGB
*/
#define GAMMACORRECTION(t) (((t) <= 0.0031306684425005883) ? (12.92*(t)) : (1.055*pow((t), 0.416666666666666667) - 0.055))
/**
* @brief Inverse sRGB gamma correction, transforms R' to R
*/
#define INVGAMMACORRECTION(t) (((t) <= 0.0404482362771076) ? ((t)/12.92) : pow(((t) + 0.055)/1.055, 2.4))
/**
* @brief CIE L*a*b* f function (used to convert XYZ to L*a*b*)
* http:https://en.wikipedia.org/wiki/Lab_color_space
*/
#define LABF(t) ((t >= 8.85645167903563082e-3) ? pow(t,0.333333333333333) : (841.0/108.0)*(t) + (4.0/29.0))
#define LABINVF(t) ((t >= 0.206896551724137931) ? ((t)*(t)*(t)) : (108.0/841.0)*((t) - (4.0/29.0)))
static unsigned char gmt_M_color_rgb[GMT_N_COLOR_NAMES][3] = { /* r/g/b of X11 colors */
#include "gmt_color_rgb.h"
};
/*! Names of pens and their thicknesses */
struct GMT_PEN_NAME {
char name[16];
double width;
};
/*! . */
static struct GMT_PEN_NAME GMT_penname[GMT_N_PEN_NAMES] = { /* Names and widths of pens */
#include "gmt_pennames.h"
};
/*! List of GMT color tables available in makecpt and grd2cpt
* Some are based on MATLAB and Matplotlib color schemes.
* To add more tables, place the master CPT file in share/cpt and
* add a one-line entry with name and explanation in gmt_cpt_masters.h. */
static char *GMT_CPT_master[GMT_N_CPT_MASTERS] = {
#include "gmt_cpt_masters.h"
};
/* Local functions needed for public functions below */
GMT_LOCAL int gmtsupport_parse_pattern_new (struct GMT_CTRL *GMT, char *line, struct GMT_FILL *fill) {
/* Parse the fill pattern syntax: p|P<pattern>[+r<dpi>][+b<color>|-][+f<color>|-] */
char *c = NULL;
unsigned int first = 1;
double fb_rgb[4];
fill->dpi = irint (PSL_DOTS_PER_INCH);
if ((c = strchr (line, '+'))) { /* Got modifiers */
unsigned int pos = 0, uerr = 0;
char p[GMT_BUFSIZ] = {""};
while (gmt_getmodopt (GMT, 0, c, "bfr", &pos, p, &uerr) && uerr == 0) { /* Looking for +b, +f, +r */
switch (p[0]) {
case 'b': /* Background color */
if (p[1] == '-') { /* Transparent */
fill->b_rgb[0] = fill->b_rgb[1] = fill->b_rgb[2] = -1, fill->b_rgb[3] = 0;
GMT_Report (GMT->parent, GMT_MSG_DEBUG, "Background pixels set to transparent!\n");
}
else {
if (gmt_getrgb (GMT, &p[1], fill->b_rgb)) {
GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Background colorizing value %s not recognized!\n", &p[1]);
return 2;
}
GMT_Report (GMT->parent, GMT_MSG_DEBUG, "Background pixels set to colors %s\n", gmt_putrgb (GMT, fill->b_rgb));
}
break;
case 'f': /* Foreround color */
if (p[1] == '-') { /* Transparent */
fill->f_rgb[0] = fill->f_rgb[1] = fill->f_rgb[2] = -1, fill->f_rgb[3] = 0;
GMT_Report (GMT->parent, GMT_MSG_DEBUG, "Foreground pixels set to transparent!\n");
}
else {
if (gmt_getrgb (GMT, &p[1], fill->f_rgb)) {
GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Foreground colorizing value %s not recognized!\n", &p[1]);
return 2;
}
GMT_Report (GMT->parent, GMT_MSG_DEBUG, "Foreground pixels set to colors %s\n", gmt_putrgb (GMT, fill->f_rgb));
}
break;
case 'r': /* Dots-per-inch resolution */
if (p[1] == '-') {
GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Pattern dpi %s is negative!\n", &p[1]);
return 4;
}
fill->dpi = atoi (&p[1]);
GMT_Report (GMT->parent, GMT_MSG_DEBUG, "Pattern dpi set to %d\n", fill->dpi);
break;
default: break;
}
}
if (uerr) return (GMT_PARSE_ERROR);
}
/* Copy name (or number) of pattern */
if (c) c[0] = '\0'; /* Chop off the modifiers */
if (!gmt_M_file_is_memory (&line[1]) && line[1] == '@') { /* Must be a cache file */
first = gmt_download_file_if_not_found (GMT, &line[1], 0) + 1; /* Add one since we started at 1 */
}
strncpy (fill->pattern, &line[first], GMT_BUFSIZ-1);
/* Attempt to convert to integer - will be 0 if not an integer and then we set it to -1 for a filename */
fill->pattern_no = atoi (fill->pattern);
if (fill->pattern_no == 0) {
fill->pattern_no = -1;
gmt_set_pad (GMT, 0); /* No padding */
GMT_Report (GMT->parent, GMT_MSG_DEBUG, "Pattern image is in file %s\n", fill->pattern);
if ((fill->I = GMT_Read_Data (GMT->parent, GMT_IS_IMAGE, GMT_IS_FILE, GMT_IS_SURFACE, GMT_CONTAINER_AND_DATA, NULL, fill->pattern, NULL)) == NULL) {
GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Unable to read image %s, no pattern set\n", fill->pattern);
return (GMT_RUNTIME_ERROR);
}
gmt_set_pad (GMT, GMT->parent->pad); /* Restore to GMT Defaults */
fill->dim[0] = fill->I->header->n_columns;
fill->dim[1] = fill->I->header->n_rows;
if (fill->I->colormap) { /* Got indexed color image, must build rgb stream instead */
/* Convert colormap from integer to unsigned char and count colors */
unsigned char *colormap = gmt_M_memory (GMT, NULL, 4*256, unsigned char);
int64_t n, j, k;
for (n = 0; n < 4 * 256 && fill->I->colormap[n] >= 0; n++) colormap[n] = (unsigned char)fill->I->colormap[n];
n /= 4;
/* Expand 8-bit indexed image to 24-bit image */
fill->I->data = gmt_M_memory (GMT, fill->I->data, 3 * fill->I->header->nm, unsigned char);
n = 3 * fill->I->header->nm - 1;
for (j = (int64_t)fill->I->header->nm - 1; j >= 0; j--) {
k = 4 * fill->I->data[j] + 3;
fill->I->data[n--] = colormap[--k], fill->I->data[n--] = colormap[--k], fill->I->data[n--] = colormap[--k];
}
gmt_M_free (GMT, colormap);
fill->I->header->n_bands = 3;
fill->I->header->size *= 3;
}
else if (fill->I->header->n_bands == 4) { /* RGBA image, with a color map */
uint64_t n4, j4;
for (j4 = n4 = 0; j4 < 4 * fill->I->header->nm; j4++) /* Reduce image from 32- to 24-bit */
fill->I->data[n4++] = fill->I->data[j4++], fill->I->data[n4++] = fill->I->data[j4++], fill->I->data[n4++] = fill->I->data[j4++];
fill->I->header->n_bands = 3;
}
fill->image = fill->I->data;
fill->dim[2] = (fill->I->header->n_bands == 3) ? 24 : 8;
}
else
GMT_Report (GMT->parent, GMT_MSG_DEBUG, "Pattern number %d selected\n", fill->pattern_no);
/* If inverse, simply flip the colors around */
if (line[0] == 'p') {
GMT_Report (GMT->parent, GMT_MSG_DEBUG, "Pattern will be inverted\n");
gmt_M_rgb_copy (fb_rgb, fill->f_rgb);
gmt_M_rgb_copy (fill->f_rgb, fill->b_rgb);
gmt_M_rgb_copy (fill->b_rgb, fb_rgb);
}
if (c) c[0] = '+'; /* Undo previous damage */
return (0);
}
GMT_LOCAL int gmtsupport_parse_pattern_old (struct GMT_CTRL *GMT, char *line, struct GMT_FILL *fill) {
/* Parse the old-style pattern syntax */
int n, i, len, pos, end;
unsigned int first = 1;
char f, word[GMT_LEN256] = {""};
double fb_rgb[4];
/* We only get here if there are no +r,+f,+b strings or : in line, and we know line[1] is integer.
* However, if user gave -Gp1image.jpg then we will fail trying to parse as old. So we do a check
* here if the argument can be accessed as a file and if so we call the new parser and return */
if (!gmt_M_file_is_memory (&line[1]) && line[1] == '@') { /* Must be a cache file */
first = gmt_download_file_if_not_found (GMT, &line[1], 0) + 1; /* Add one since we started at 1 */
}
if (!gmt_access (GMT, &line[first], F_OK))
return (gmtsupport_parse_pattern_new (GMT, line, fill));
n = sscanf (&line[1], "%d/%s", &fill->dpi, fill->pattern);
if (n != 2) return (1);
if (!gmt_M_file_is_memory (fill->pattern) && fill->pattern[0] == '@') { /* Must be a cache file */
first = gmt_download_file_if_not_found (GMT, fill->pattern, 0);
/* Shuffle pattern name to skip the leading @ */
for (i = 1; fill->pattern[i]; i++) fill->pattern[i-1] = fill->pattern[i];
fill->pattern[i-1] = '\0';
}
/* Determine if there are colorizing options applied, i.e. [:F<rgb>B<rgb>] */
len = (int)strlen (fill->pattern);
for (i = 0, pos = -1; i < ((int)strlen (fill->pattern)-1) && fill->pattern[i] && pos == -1; i++)
if (fill->pattern[i] == ':' && i < len && (fill->pattern[i+1] == 'B' || fill->pattern[i+1] == 'F')) pos = i;
if (pos > -1) fill->pattern[pos] = '\0';
fill->pattern_no = atoi (fill->pattern);
if (fill->pattern_no == 0) fill->pattern_no = -1;
/* See if fore- and background colors are given */
len = (int)strlen (line);
for (i = 0, pos = -1; line[i] && pos == -1; i++) if (line[i] == ':' && i < len && (line[i+1] == 'B' || line[i+1] == 'F')) pos = i;
pos++;
if (pos > 0 && line[pos]) { /* Gave colors */
while (line[pos]) {
f = line[pos++];
if (line[pos] == '-') /* Signal for transpacency masking */
fb_rgb[0] = fb_rgb[1] = fb_rgb[2] = -1, fb_rgb[3] = 0;
else {
end = pos;
while (line[end] && !(line[end] == 'F' || line[end] == 'B')) end++;
strncpy (word, &line[pos], (size_t)(end - pos));
word[end - pos] = '\0';
if (gmt_getrgb (GMT, word, fb_rgb)) {
GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Colorizing value %s not recognized!\n", word);
return 2;
}
}
if (f == 'f' || f == 'F')
gmt_M_rgb_copy (fill->f_rgb, fb_rgb);
else if (f == 'b' || f == 'B')
gmt_M_rgb_copy (fill->b_rgb, fb_rgb);
else {
GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Colorizing argument %c not recognized!\n", f);
return 2;
}
while (line[pos] && !(line[pos] == 'F' || line[pos] == 'B')) pos++;
}
}
/* If inverse, simply flip the colors around */
if (line[0] == 'p') {
gmt_M_rgb_copy (fb_rgb, fill->f_rgb);
gmt_M_rgb_copy (fill->f_rgb, fill->b_rgb);
gmt_M_rgb_copy (fill->b_rgb, fb_rgb);
}
return 0;
}
/*! . */
GMT_LOCAL int gmtsupport_parse_pattern (struct GMT_CTRL *GMT, char *line, struct GMT_FILL *fill) {
int err;
/* New syntax may have a modifier */
if (strstr(line, "+r") || strstr(line, "+f") || strstr(line, "+b") || !strchr(line, '/')) /* Clearly new syntax */
err = gmtsupport_parse_pattern_new (GMT, line, fill);
else
err = gmtsupport_parse_pattern_old (GMT, line, fill);
if (err == 2) {
GMT_exit (GMT, GMT_PARSE_ERROR); return 2;
}
fill->use_pattern = true;
return (err);
}
/*! . */
GMT_LOCAL char *support_get_userimagename (struct GMT_CTRL *GMT, char *line, char *cpt_path) {
/* When a cpt is not in the current directory but given by relative or absolute path
* AND that cpt refers to a user pattern file (which may be a relative or absolute path)
* then unless that pattern file can be found we will try to prepend the path to the cpt
* file and see if the pattern can be found that way.
*/
int j, err;
char *name = NULL, path[PATH_MAX] = {""};
struct GMT_FILL fill;
if (!gmt_M_is_pattern (line)) return NULL; /* Not an image specification */
err = gmtsupport_parse_pattern (GMT, line, &fill); /* See if this returns an error or not */
if (err) return NULL; /* Not a valid image specification */
if (fill.pattern_no > 0) return NULL; /* Not a user image */
/* Here we do have a pattern specification */
/* Try the user's default directories */
if (gmtlib_getuserpath (GMT, fill.pattern, path))
return NULL; /* Yes, found so no problems */
/* Now must put our faith in the cpt path and hope it has a path that can help us */
if (cpt_path == NULL || cpt_path[0] == '<') { /* Without an actual file path we must warn and bail */
GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Not enough information to determine location of user pattern %s\n", fill.pattern);
return NULL;
}
j = (int)strlen (cpt_path);
while (j > 0 && cpt_path[j] != '/') j--; /* Find last slash */
if (j > 0) { /* OK, got the cpt directory */
cpt_path[j] = '\0'; /* Temporarily chop off the slash */
sprintf (path, "%s/%s", cpt_path, fill.pattern);
cpt_path[j] = '/'; /* Restore the slash */
if (access (path, R_OK)) {
GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Not enough information to determine location of user pattern %s\n", fill.pattern);
return NULL;
}
name = strdup (path); /* Must use this image name instead as it contains the full working path */
}
return (name);
}
/*! . */
GMT_LOCAL void support_rgb_to_hsv (double rgb[], double hsv[]) {
double diff;
unsigned int i, imax = 0, imin = 0;
/* This had checks using rgb value in doubles (e.g. (max_v == xr)), which failed always on some compilers.
Changed to integer logic: 2009-02-05 by RS.
*/
hsv[3] = rgb[3]; /* Pass transparency unchanged */
for (i = 1; i < 3; i++) {
if (rgb[i] > rgb[imax]) imax = i;
if (rgb[i] < rgb[imin]) imin = i;
}
diff = rgb[imax] - rgb[imin];
hsv[0] = 0.0;
hsv[1] = (rgb[imax] == 0.0) ? 0.0 : diff / rgb[imax];
hsv[2] = rgb[imax];
if (hsv[1] == 0.0) return; /* Hue is undefined */
hsv[0] = 120.0 * imax + 60.0 * (rgb[(imax + 1) % 3] - rgb[(imax + 2) % 3]) / diff;
if (hsv[0] < 0.0) hsv[0] += 360.0;
if (hsv[0] > 360.0) hsv[0] -= 360.0;
}
/*! . */
GMT_LOCAL void support_hsv_to_rgb (double rgb[], double hsv[]) {
int i;
double h, f, p, q, t, rr, gg, bb;
rgb[3] = hsv[3]; /* Pass transparency unchanged */
if (hsv[1] == 0.0)
rgb[0] = rgb[1] = rgb[2] = hsv[2];
else {
h = hsv[0];
while (h >= 360.0) h -= 360.0;
while (h < 0.0) h += 360.0;
h /= 60.0;
i = irint (floor (h));
f = h - i;
p = hsv[2] * (1.0 - hsv[1]);
q = hsv[2] * (1.0 - (hsv[1] * f));
t = hsv[2] * (1.0 - (hsv[1] * (1.0 - f)));
switch (i) {
case 0:
rr = hsv[2]; gg = t; bb = p;
break;
case 1:
rr = q; gg = hsv[2]; bb = p;
break;
case 2:
rr = p; gg = hsv[2]; bb = t;
break;
case 3:
rr = p; gg = q; bb = hsv[2];
break;
case 4:
rr = t; gg = p; bb = hsv[2];
break;
default:
rr = hsv[2]; gg = p; bb = q;
break;
}
rgb[0] = (rr < 0.0) ? 0.0 : rr;
rgb[1] = (gg < 0.0) ? 0.0 : gg;
rgb[2] = (bb < 0.0) ? 0.0 : bb;
}
}
/*! . */
GMT_LOCAL void support_rgb_to_cmyk (double rgb[], double cmyk[]) {
/* Plain conversion; with default undercolor removal or blackgeneration */
/* RGB is in 0-1, CMYK will be in 0-1 range */
int i;
cmyk[4] = rgb[3]; /* Pass transparency unchanged */
for (i = 0; i < 3; i++) cmyk[i] = 1.0 - rgb[i];
cmyk[3] = MIN (cmyk[0], MIN (cmyk[1], cmyk[2])); /* Default Black generation */
if (cmyk[3] < GMT_CONV8_LIMIT) cmyk[3] = 0.0;
/* To implement device-specific blackgeneration, supply lookup table K = BG[cmyk[3]] */
for (i = 0; i < 3; i++) {
cmyk[i] -= cmyk[3]; /* Default undercolor removal */
if (cmyk[i] < GMT_CONV8_LIMIT) cmyk[i] = 0.0;
}
/* To implement device-specific undercolor removal, supply lookup table u = UR[cmyk[3]] */
}
/*! . */
GMT_LOCAL void support_cmyk_to_rgb (double rgb[], double cmyk[]) {
/* Plain conversion; no undercolor removal or blackgeneration */
/* CMYK is in 0-1, RGB will be in 0-1 range */
int i;
rgb[3] = cmyk[4]; /* Pass transparency unchanged */
for (i = 0; i < 3; i++) rgb[i] = 1.0 - cmyk[i] - cmyk[3];
}
/*! . */
GMT_LOCAL void support_cmyk_to_hsv (double hsv[], double cmyk[]) {
/* Plain conversion; no undercolor removal or blackgeneration */
/* CMYK is in 0-1, RGB will be in 0-1 range */
double rgb[4];
support_cmyk_to_rgb (rgb, cmyk);
support_rgb_to_hsv (rgb, hsv);
}
#if 0 /* Unused */
/*!
* Transform sRGB to CIE XYZ with the D65 white point
*
* Poynton, "Frequently Asked Questions About Color," page 10
* Wikipedia: http:https://en.wikipedia.org/wiki/SRGB
* Wikipedia: http:https://en.wikipedia.org/wiki/CIE_1931_color_space
*/
GMT_LOCAL void support_rgb_to_xyz (double rgb[], double xyz[]) {
double R, G, B;
R = INVGAMMACORRECTION(rgb[0]);
G = INVGAMMACORRECTION(rgb[1]);
B = INVGAMMACORRECTION(rgb[2]);
xyz[0] = 0.4123955889674142161*R + 0.3575834307637148171*G + 0.1804926473817015735*B;
xyz[1] = 0.2125862307855955516*R + 0.7151703037034108499*G + 0.07220049864333622685*B;
xyz[2] = 0.01929721549174694484*R + 0.1191838645808485318*G + 0.9504971251315797660*B;
}
/*! . */
GMT_LOCAL void support_xyz_to_rgb (double rgb[], double xyz[]) {
double R, G, B, min;
R = ( 3.2406 * xyz[0] - 1.5372 * xyz[1] - 0.4986 * xyz[2]);
G = (-0.9689 * xyz[0] + 1.8758 * xyz[1] + 0.0415 * xyz[2]);
B = ( 0.0557 * xyz[0] - 0.2040 * xyz[1] + 1.0570 * xyz[2]);
min = MIN(MIN(R, G), B);
/* Force nonnegative values so that gamma correction is well-defined. */
if (min < 0) {
R -= min;
G -= min;
B -= min;
}
/* Transform from RGB to R'G'B' */
rgb[0] = GAMMACORRECTION(R);
rgb[1] = GAMMACORRECTION(G);
rgb[2] = GAMMACORRECTION(B);
}
/*!
* Convert CIE XYZ to CIE L*a*b* (CIELAB) with the D65 white point
*
* Wikipedia: http:https://en.wikipedia.org/wiki/Lab_color_space
*/
GMT_LOCAL void support_xyz_to_lab (double xyz[], double lab[]) {
double X, Y, Z;
X = LABF( xyz[0] / WHITEPOINT_X );
Y = LABF( xyz[1] / WHITEPOINT_Y );
Z = LABF( xyz[2] / WHITEPOINT_Z );
lab[0] = 116 * Y - 16;
lab[1] = 500 * (X - Y);
lab[2] = 200 * (Y - Z);
}
/*! . */
GMT_LOCAL void support_lab_to_xyz (double xyz[], double lab[]) {
xyz[0] = WHITEPOINT_X * LABINVF( lab[0] + lab[1]/500 );
xyz[1] = WHITEPOINT_Y * LABINVF( (lab[0] + 16)/116 );
xyz[2] = WHITEPOINT_Z * LABINVF( lab[0] - lab[2]/200 );
}
/*! . */
GMT_LOCAL void support_rgb_to_lab (double rgb[], double lab[]) {
/* RGB is in 0-1, LAB will be in ??? range */
double xyz[3];
support_rgb_to_xyz (rgb, xyz);
support_xyz_to_lab (xyz, lab);
}
/*! . */
GMT_LOCAL void support_lab_to_rgb (double rgb[], double lab[]) {
double xyz[3];
support_lab_to_xyz (xyz, lab);
support_xyz_to_rgb (rgb, xyz);
}
#endif
/*! . */
GMT_LOCAL int support_comp_double_asc (const void *p_1, const void *p_2) {
/* Returns -1 if point_1 is < that point_2,
+1 if point_2 > point_1, and 0 if they are equal
*/
bool bad_1, bad_2;
const double *point_1 = p_1, *point_2 = p_2;
bad_1 = gmt_M_is_dnan ((*point_1));
bad_2 = gmt_M_is_dnan ((*point_2));
if (bad_1 && bad_2) return (0);
if (bad_1) return (+1);
if (bad_2) return (-1);
if ((*point_1) < (*point_2)) return (-1);
if ((*point_1) > (*point_2)) return (+1);
return (0);
}
/*! . */
GMT_LOCAL int support_comp_float_asc (const void *p_1, const void *p_2) {
/* Returns -1 if point_1 is < that point_2,
+1 if point_2 > point_1, and 0 if they are equal
*/
bool bad_1, bad_2;
const float *point_1 = p_1, *point_2 = p_2;
bad_1 = gmt_M_is_fnan ((*point_1));
bad_2 = gmt_M_is_fnan ((*point_2));
if (bad_1 && bad_2) return (0);
if (bad_1) return (+1);
if (bad_2) return (-1);
if ((*point_1) < (*point_2)) return (-1);
if ((*point_1) > (*point_2)) return (+1);
return (0);
}
/*! . */
GMT_LOCAL int support_comp_ulong_asc (const void *p_1, const void *p_2) {
/* Returns -1 if point_1 is < that point_2,
+1 if point_2 > point_1, and 0 if they are equal
*/
const uint64_t *point_1 = p_1, *point_2 = p_2;
if ((*point_1) < (*point_2)) return (-1);
if ((*point_1) > (*point_2)) return (+1);
return (0);
}
/*! . */
GMT_LOCAL int support_comp_long_asc (const void *p_1, const void *p_2) {
/* Returns -1 if point_1 is < that point_2,
+1 if point_2 > point_1, and 0 if they are equal
*/
const int64_t *point_1 = p_1, *point_2 = p_2;
if ((*point_1) < (*point_2)) return (-1);
if ((*point_1) > (*point_2)) return (+1);
return (0);
}
/*! . */
GMT_LOCAL int support_comp_uint_asc (const void *p_1, const void *p_2) {
/* Returns -1 if point_1 is < that point_2,
+1 if point_2 > point_1, and 0 if they are equal
*/
const unsigned int *point_1 = p_1, *point_2 = p_2;
if ((*point_1) < (*point_2)) return (-1);
if ((*point_1) > (*point_2)) return (+1);
return (0);
}
/*! . */
GMT_LOCAL int support_comp_int_asc (const void *p_1, const void *p_2) {
/* Returns -1 if point_1 is < that point_2,
+1 if point_2 > point_1, and 0 if they are equal
*/
const int *point_1 = p_1, *point_2 = p_2;
if ((*point_1) < (*point_2)) return (-1);
if ((*point_1) > (*point_2)) return (+1);
return (0);
}
/*! . */
GMT_LOCAL int support_comp_ushort_asc (const void *p_1, const void *p_2) {
/* Returns -1 if point_1 is < that point_2,
+1 if point_2 > point_1, and 0 if they are equal
*/
const unsigned short int *point_1 = p_1, *point_2 = p_2;
if ((*point_1) < (*point_2)) return (-1);
if ((*point_1) > (*point_2)) return (+1);
return (0);
}
/*! . */
GMT_LOCAL int support_comp_short_asc (const void *p_1, const void *p_2) {
/* Returns -1 if point_1 is < that point_2,
+1 if point_2 > point_1, and 0 if they are equal
*/
const short int *point_1 = p_1, *point_2 = p_2;
if ((*point_1) < (*point_2)) return (-1);
if ((*point_1) > (*point_2)) return (+1);
return (0);
}
/*! . */
GMT_LOCAL int support_comp_uchar_asc (const void *p_1, const void *p_2) {
/* Returns -1 if point_1 is < that point_2,
+1 if point_2 > point_1, and 0 if they are equal
*/
const unsigned char *point_1 = p_1, *point_2 = p_2;
if ((*point_1) < (*point_2)) return (-1);
if ((*point_1) > (*point_2)) return (+1);
return (0);
}
/*! . */
GMT_LOCAL int support_comp_char_asc (const void *p_1, const void *p_2) {
/* Returns -1 if point_1 is < that point_2,
+1 if point_2 > point_1, and 0 if they are equal
*/
const char *point_1 = p_1, *point_2 = p_2;
if ((*point_1) < (*point_2)) return (-1);
if ((*point_1) > (*point_2)) return (+1);
return (0);
}
/*! . */
GMT_LOCAL bool support_check_irgb (int irgb[], double rgb[]) {
if ((irgb[0] < 0 || irgb[0] > 255) || (irgb[1] < 0 || irgb[1] > 255) || (irgb[2] < 0 || irgb[2] > 255)) return (true);
rgb[0] = gmt_M_is255 (irgb[0]);
rgb[1] = gmt_M_is255 (irgb[1]);
rgb[2] = gmt_M_is255 (irgb[2]);
return (false);
}
/*! . */
GMT_LOCAL bool support_check_rgb (double rgb[]) {
return ((rgb[0] < 0.0 || rgb[0] > 1.0) || (rgb[1] < 0.0 || rgb[1] > 1.0) || (rgb[2] < 0.0 || rgb[2] > 1.0));
}
/*! . */
GMT_LOCAL bool support_check_hsv (double hsv[]) {
return ((hsv[0] < 0.0 || hsv[0] > 360.0) || (hsv[1] < 0.0 || hsv[1] > 1.0) || (hsv[2] < 0.0 || hsv[2] > 1.0));
}
/*! . */
GMT_LOCAL bool support_check_cmyk (double cmyk[]) {
unsigned int i;
for (i = 0; i < 4; i++) cmyk[i] *= 0.01;
for (i = 0; i < 4; i++) if (cmyk[i] < 0.0 || cmyk[i] > 1.0) return (true);
return (false);
}
/*! . */
GMT_LOCAL unsigned int support_char_count (char *txt, char c) {
unsigned int i = 0, n = 0;
while (txt[i]) if (txt[i++] == c) n++;
return (n);
}
/*! . */
GMT_LOCAL bool support_gethsv (struct GMT_CTRL *GMT, char *line, double hsv[]) {
int n, i, count, irgb[3], c = 0;
double rgb[4], cmyk[5];
char buffer[GMT_LEN64] = {""}, *t = NULL;
if (!line) { GMT_Report (GMT->parent, GMT_MSG_NORMAL, "No argument given to support_gethsv\n"); GMT_exit (GMT, GMT_PARSE_ERROR); return false; }
if (!line[0]) return (false); /* Nothing to do - accept default action */
rgb[3] = hsv[3] = cmyk[4] = 0.0; /* Default is no transparency */
if (line[0] == '-') {
hsv[0] = -1.0; hsv[1] = -1.0; hsv[2] = -1.0;
return (false);
}
strncpy (buffer, line, GMT_LEN64-1); /* Make local copy */
if ((t = strstr (buffer, "@")) && strlen (t) > 1) { /* User requested transparency via @<transparency> */
double transparency = atof (&t[1]);
if (transparency < 0.0 || transparency > 100.0)
GMT_Report (GMT->parent, GMT_MSG_VERBOSE, "Representation of transparency (%s) not recognized. Using default [0 or opaque].\n", t);
else
rgb[3] = hsv[3] = cmyk[4] = transparency / 100.0; /* Transparency is in 0-1 range */
t[0] = '\0'; /* Chop off transparency for the rest of this function */
}
if (buffer[0] == '#') { /* #rrggbb */
n = sscanf (buffer, "#%2x%2x%2x", (unsigned int *)&irgb[0], (unsigned int *)&irgb[1], (unsigned int *)&irgb[2]);
if (n != 3 || support_check_irgb (irgb, rgb)) return (true);
support_rgb_to_hsv (rgb, hsv);
return (false);
}
/* If it starts with a letter, then it could be a name */
if (isalpha ((unsigned char) buffer[0])) {
if ((n = (int)gmt_colorname2index (GMT, buffer)) < 0) {
GMT_Report (GMT->parent, GMT_MSG_NORMAL, "Colorname %s not recognized!\n", buffer);
return (true);
}
for (i = 0; i < 3; i++) rgb[i] = gmt_M_is255 (gmt_M_color_rgb[n][i]);
support_rgb_to_hsv (rgb, hsv);
return (false);
}
/* Definitely wrong, at this point, is something that does not end in a number */
if (strlen(buffer) < 1) return (true); /* Nothing, which is bad */
c = buffer[strlen(buffer)-1];
if (!(isdigit (c) || c == '.')) return (true);
count = (int)support_char_count (buffer, '/');
if (count == 3) { /* c/m/y/k */
n = sscanf (buffer, "%lf/%lf/%lf/%lf", &cmyk[0], &cmyk[1], &cmyk[2], &cmyk[3]);
if (n != 4 || support_check_cmyk (cmyk)) return (true);
support_cmyk_to_hsv (hsv, cmyk);
return (false);
}
if (count == 2) { /* r/g/b or h/s/v */
if (GMT->current.setting.color_model & GMT_HSV) { /* h/s/v */
n = sscanf (buffer, "%lf/%lf/%lf", &hsv[0], &hsv[1], &hsv[2]);
return (n != 3 || support_check_hsv (hsv));
}
else { /* r/g/b */
n = sscanf (buffer, "%d/%d/%d", &irgb[0], &irgb[1], &irgb[2]);
if (n != 3 || support_check_irgb (irgb, rgb)) return (true);
support_rgb_to_hsv (rgb, hsv);
return (false);
}
}
if (support_char_count (buffer, '-') == 2) { /* h-s-v */
n = sscanf (buffer, "%lf-%lf-%lf", &hsv[0], &hsv[1], &hsv[2]);
return (n != 3 || support_check_hsv (hsv));
}
if (count == 0) { /* gray */
n = sscanf (buffer, "%d", &irgb[0]);
irgb[1] = irgb[2] = irgb[0];
if (n != 1 || support_check_irgb (irgb, rgb)) return (true);
support_rgb_to_hsv (rgb, hsv);
return (false);
}
/* Get here if there is a problem */
return (true);
}
/*! . */
GMT_LOCAL bool support_is_pattern_old (char *word) {
/* Returns true if the word is a pattern specification
* Old style: P|p<dpi>/<pattern>[:B<color>[F<color>]]
*/
if (strchr (word, ':')) return (true); /* Only patterns may have a colon */
if (!(word[0] == 'P' || word[0] == 'p')) return false; /* Patterns must start with P or p */
if (!strchr (word, '/')) return (false); /* Patterns separate dpi and pattern with a slash */
/* Here we know we start with P|p and there is a slash - this can only be a pattern specification */
return (true);
}
/*! . */
GMT_LOCAL bool support_is_pattern_new (struct GMT_CTRL *GMT, char *word) {
/* Returns true if the word is a pattern specification
* New style: P|p<pattern>[+b<color>][+f<color>][+r<dpi>]
*/
char *c = NULL;
int n;
if ((c = strchr (word, '+')) && strchr ("bfr", c[1])) return (true); /* Found +b, +f or +r */
/* Here we have no modifiers at all */
if (!(word[0] == 'P' || word[0] == 'p')) return false; /* Patterns must start with P or p */
/* Pattern without modifier must be an integer OR a valid file */
if ((n = atoi (&word[1])) > 0 && n < PSL_N_PATTERNS) return (true); /* Got a valid integer */
/* Se if we can access the file */
if (!gmt_access (GMT, &word[1], F_OK)) return (true); /* Got a file that exists */
return (false); /* Not a pattern */
}
/*! . */
GMT_LOCAL bool support_is_pattern (struct GMT_CTRL *GMT, char *word) {
/* Returns true if the word is a pattern specification
* Old style: P|p<dpi>/<pattern>[:B<color>[F<color>]]
* New style: P|p<pattern>[+b<color>][+f<color>][+r<dpi>]
*/
bool val;
/* New syntax may have a modifier or no slash AND no colon */
if ((strstr(word, "+r") || strstr(word, "+f") || strstr(word, "+b") || !strchr(word, '/')) && !strchr (word,':'))
val = support_is_pattern_new (GMT, word);
else
val = support_is_pattern_old (word);
return (val);
}
/*! . */
bool gmtlib_is_color (struct GMT_CTRL *GMT, char *word) {
int i, k, n, n_hyphen = 0, n_slashes = 0;
/* Returns true if we are sure the word is a color string - else false.
* color syntax is <r/g/b>|<h-s-v>|<c/m/y/k>|<colorname>.
* NOTES: 1) <gray> is excluded since this function is called in places where
* a single integer may be used for font size or pen width...
* 2) We are not checking if the values are kosher; just that they follow the pattern */
n = (int)strlen (word);
if (n == 0) return (false);
if (word[0] == '#') return (true); /* Probably #rrggbb */
if (gmt_colorname2index (GMT, word) >= 0) return (true); /* Valid color name */
/* Skip dimension specifications with units c|i|m|p or a colon */
if (strchr(word,'t')) return (false); /* Got a t somewhere */
if (strchr(word,':')) return (false); /* Got a : somewhere */
if (strchr(word,'c')) return (false); /* Got a c somewhere */
if (strchr(word,'i')) return (false); /* Got a i somewhere */
if (strchr(word,'m')) return (false); /* Got a m somewhere */
if (strchr(word,'p')) return (false); /* Got a p somewhere */
for (i = k = 0; word[i]; i++) if (word[i] == '/') n_slashes++; /* Count slashes */
if (n_slashes == 1 || n_slashes > 3) return (false); /* No color spec takes only 1 slash or more than 3 */
n--;
/* Check for h-s-v as well. Must find 2 hyphens */
while (n >= 0 && (strchr ("/-.", word[n]) || isdigit ((int)word[n]))) {
if (word[n] == '-') n_hyphen++;
n--; /* Wind down as long as we find -,.,/ or digits */
}
return ((n == -1 && (n_slashes || n_hyphen == 2))); /* Hence <gray> will fail the slash test */
}
/*! . */
GMT_LOCAL int support_getfonttype (struct GMT_CTRL *GMT, char *name) {
unsigned int i;
if (!name[0]) return (-1);
if (!isdigit ((unsigned char) name[0])) { /* Does not start with number. Try font name */
int ret = -1;
for (i = 0; i < GMT->session.n_fonts && strcmp (name, GMT->session.font[i].name); i++);
if (i < GMT->session.n_fonts) ret = i;
return (ret);
}
if (!isdigit ((unsigned char) name[strlen(name)-1])) return (-1); /* Starts with digit, ends with something else: cannot be */
return (atoi (name));
}
/*! . */
GMT_LOCAL bool support_is_fontname (struct GMT_CTRL *GMT, char *word) {
/* Returns true if the word is one of the named fonts */
unsigned int i;
if (!word[0]) return (false);
for (i = 0; i < GMT->session.n_fonts && strcmp (word, GMT->session.font[i].name); i++);
if (i == GMT->session.n_fonts) return (false);
return (true);
}
/*! . */
GMT_LOCAL int support_name2pen (char *name) {
/* Return index into structure with pennames and width, for given name */
int i, k;
char Lname[GMT_LEN64] = {""};
strncpy (Lname, name, GMT_LEN64-1);
gmt_str_tolower (Lname);
for (i = 0, k = -1; k < 0 && i < GMT_N_PEN_NAMES; i++) if (!strcmp (Lname, GMT_penname[i].name)) k = i;
return (k);
}
/*! . */
GMT_LOCAL int support_pen2name (double width) {
/* Return index into structure with pennames and width, for given width */
int i, k;
for (i = 0, k = -1; k < 0 && i < GMT_N_PEN_NAMES; i++) if (gmt_M_eq (width, GMT_penname[i].width)) k = i;
return (k);
}
/*! . */
GMT_LOCAL int support_getpenwidth (struct GMT_CTRL *GMT, char *line, struct GMT_PEN *P) {
int n;