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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
|
/*********************************************************************
eps -- functions to write EPS files.
This is part of GNU Astronomy Utilities (Gnuastro) package.
Original author:
Mohammad Akhlaghi <mohammad@akhlaghi.org>
Contributing author(s):
Copyright (C) 2015-2024 Free Software Foundation, Inc.
Gnuastro 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, either version 3 of the License, or (at your
option) any later version.
Gnuastro 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.
You should have received a copy of the GNU General Public License
along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
**********************************************************************/
#include <config.h>
#include <stdio.h>
#include <errno.h>
#include <error.h>
#include <stdlib.h>
#include <string.h>
#include <gnuastro/box.h>
#include <gnuastro/eps.h>
#include <gnuastro/list.h>
#include <gnuastro/color.h>
#include <gnuastro/blank.h>
#include <gnuastro-internal/timing.h>
#include <gnuastro-internal/checkset.h>
/*************************************************************
************** Acceptable EPS names ***************
*************************************************************/
int
gal_eps_name_is_eps(char *name)
{
size_t len;
if(name)
{
len=strlen(name);
if ( ( len>=3 && strcmp(&name[len-3], "eps") == 0 )
|| ( len>=3 && strcmp(&name[len-3], "EPS") == 0 )
|| ( len>=4 && strcmp(&name[len-4], "epsf") == 0 )
|| ( len>=4 && strcmp(&name[len-4], "epsi") == 0 ) )
return 1;
else
return 0;
}
else return 0;
}
int
gal_eps_suffix_is_eps(char *name)
{
if(name)
{
if (strcmp(name, "eps") == 0 || strcmp(name, ".eps") == 0
|| strcmp(name, "EPS") == 0 || strcmp(name, ".EPS") == 0
|| strcmp(name, "epsf") == 0 || strcmp(name, ".epsf") == 0
|| strcmp(name, "epsi") == 0 || strcmp(name, ".epsi") == 0)
return 1;
else
return 0;
}
else return 0;
}
/*************************************************************
************** Write an EPS image **************
*************************************************************/
static int
eps_is_binary(gal_data_t *in, uint8_t *bitone)
{
gal_data_t *channel;
uint8_t second_set=0;
unsigned char *i, *fi, first=0, second=0;
/* Go through all the channels. */
for(channel=in; channel!=NULL; channel=channel->next)
{
/* Go through all the values and see if there is more than two values
in the array. */
fi = (i=channel->array) + channel->size;
first=*i;
do
if(*i!=first)
{
if(second_set)
{ if(*i!=second) break; }
else
{ second=*i; second_set=1; }
}
while(++i<fi);
/* If we didn't get to the end of the array, then we have a
multi-valued (not binary) image. */
if(i!=fi)
return 0;
}
/* If we get to this point, then all the channels were binary, so return
success. */
*bitone = first>second ? first : second;
return 1;
}
/* Convert the channels into into a 0 and 1 bit stream. This function is
only called when the image is binary (has only two values). NOTE: each
row has to have an integer number of bytes, so when the number of pixels
in a row is not a multiple of 8, we'll add one. */
static gal_data_t *
eps_convert_to_bitstream(gal_data_t *in, size_t *numbytes, uint8_t bitone)
{
size_t i, j, k, bytesinrow;
gal_data_t *channel, *out=NULL;
unsigned char *bits, byte, curbit, *arr;
size_t s0=in->dsize[0], s1=in->dsize[1];
/* Find the size values and allocate the array. */
if( s1 % 8 ) bytesinrow = s1/8 + 1;
else bytesinrow = s1/8;
*numbytes = bytesinrow*s0;
/* Go over all the channels. */
for(channel=in; channel!=NULL; channel=channel->next)
{
/* Allocate the array. Note that we currently don't have an
allocation system for bits, so we'll allocate space in bytes, then
convert the type to 'GAL_TYPE_BIT'. */
gal_list_data_add_alloc(&out, NULL, GAL_TYPE_UINT8, 1, numbytes,
NULL, 0, -1, 1, NULL, NULL, NULL);
out->type=GAL_TYPE_BIT;
bits=out->array;
/* Put the values in. */
arr=channel->array;
for(i=0;i<s0;++i) /* i*s0+j is the byte, not bit position. */
for(j=0;j<bytesinrow;++j)
{
/* Set the 8 bits to zero. */
byte=0;
/* Current bit position. */
curbit=0x80;
/* Write the next 8 values as bits. */
for(k=0;k<8;++k)
{
if( j*8+k < s1 )
{
if(arr[i*s1+j*8+k]==bitone)
byte |= curbit;
curbit >>= 1;
}
else break;
}
/* Write the byte into the array. */
bits[i*bytesinrow+j]=byte;
}
}
/* Reverse the list and return it. */
gal_list_data_reverse(&out);
return out;
}
static void
eps_write_hex(gal_data_t *write, FILE *fp, size_t numbytes)
{
unsigned char *arr;
gal_data_t *channel;
size_t i=0, j, elem_for_newline=35;
for(channel=write; channel!=NULL; channel=channel->next)
{
if(channel->status) /* A blank channel has status==1. */
fprintf(fp, "{<00>} %% Channel %zu is blank\n", i);
else
{
arr=channel->array;
fprintf(fp, "{<");
for(j=0;j<numbytes;++j)
{
fprintf(fp, "%02X", arr[j]);
if(j%elem_for_newline==0) fprintf(fp, "\n");
}
fprintf(fp, ">}\n");
}
++i;
}
}
static void
eps_write_ascii85(gal_data_t *write, FILE *fp, size_t numbytes)
{
unsigned char *arr;
gal_data_t *channel;
uint32_t anint, base;
size_t i=0, j, k, elem_for_newline=15; /* 15*5=75 */
for(channel=write; channel!=NULL; channel=channel->next)
{
if(channel->status)
fprintf(fp, "{<00>} %% Channel %zu is blank\n", i);
else
{
arr=channel->array;
fprintf(fp, "{<~");
for(j=0;j<numbytes;j+=4)
{
/* This is the last four bytes */
if(numbytes-j<4)
{
anint=arr[j]*256*256*256;
if(numbytes-j>1) anint+=arr[j+1]*256*256;
if(numbytes-j==3) anint+=arr[j+2]*256;
}
else
anint=( arr[j]*256*256*256 + arr[j+1]*256*256
+ arr[j+2]*256 + arr[j+3] );
/* If all four bytes are zero, then just print 'z'. */
if(anint==0) fprintf(fp, "z");
else
{
/* To check, just change the fprintf below to printf:
printf("\n\n");
printf("%u %u %u %u\n", in[i], in[i+1],
in[i+2], in[i+3]);
*/
base=85*85*85*85;
/* Do the ASCII85 encoding: */
for(k=0;k<5;++k)
{
fprintf(fp, "%c", anint/base+33);
anint%=base;
base/=85;
}
}
/* Go to the next line if on the right place: */
if(j%elem_for_newline==0) fprintf(fp, "\n");
}
fprintf(fp, "~>}\n");
}
++i;
}
}
static void
eps_write_image(gal_data_t *in, FILE *fp, int hex, int dontoptimize,
int forps)
{
int bpc=8;
uint8_t bitone;
gal_data_t *write;
size_t i, numbytes, *dsize=in->dsize;
size_t numch=gal_list_data_number(in);
/* Set the number of bits per component. */
if( numch==1 && dontoptimize==0 && eps_is_binary(in, &bitone) )
{
bpc=1;
write=eps_convert_to_bitstream(in, &numbytes, bitone);
}
else
{
write=in;
numbytes=in->size;
}
/* Write the basic meta data. */
switch(numch)
{
case 1: fprintf(fp, "/DeviceGray setcolorspace\n"); break;
case 3: fprintf(fp, "/DeviceRGB setcolorspace\n"); break;
case 4: fprintf(fp, "/DeviceCMYK setcolorspace\n"); break;
default:
error(EXIT_FAILURE, 0, "%s: a bug! The number of channels (%zu) is "
"not 1, 3 or 4. Please contact us so we can find the issue and "
"fix it", __func__, numch);
}
fprintf(fp, "<<\n");
fprintf(fp, " /ImageType 1\n");
fprintf(fp, " /Width %zu\n", dsize[1]);
fprintf(fp, " /Height %zu\n", dsize[0]);
fprintf(fp, " /ImageMatrix [ %zu 0 0 %zu 0 0 ]\n", dsize[1], dsize[0]);
fprintf(fp, " /MultipleDataSources true\n");
fprintf(fp, " /BitsPerComponent %d\n", bpc);
fprintf(fp, " /Decode[");
for(i=0;i<numch;++i) {fprintf(fp, " 0 1");} fprintf(fp, " ]\n");
fprintf(fp, " /Interpolate false\n");
fprintf(fp, " /DataSource [\n");
/* Based on the encoding, write the contents of the image. */
if(hex)
eps_write_hex(write, fp, numbytes);
else
eps_write_ascii85(write, fp, numbytes);
/* Finish the file. */
fprintf(fp, " ]\n");
fprintf(fp, ">>\n");
fprintf(fp, "image\n\n");
/* Finish the image part based on the final output. Note that 'grestore'
will also fix the translation we done on top so the point moves to
(0,0) before making the next path. */
if(forps) fprintf(fp, "showpage\n\n");
else fprintf(fp, "grestore\n\n");
/* Clean up. */
if(write!=in)
gal_list_data_free(write);
}
/* The "point" is the smallest unit of measure in Postscript (and
typesetting in general). It is defined like this: 72 points is 1
international inch. So 72 points is 2.54 cm. */
void
gal_eps_to_pt(float widthincm, size_t *dsize, size_t *w_h_in_pt)
{
w_h_in_pt[0] = widthincm*72.0f/2.54f;
w_h_in_pt[1] = (float)( dsize[0] * w_h_in_pt[0] )/(float)(dsize[1]);
}
/* Return the macro corresponding the to the given shape. */
uint8_t
gal_eps_shape_name_to_id(char *n)
{
if( !strcasecmp(n, "line") ) return GAL_EPS_MARK_SHAPE_LINE;
else if( !strcasecmp(n, "plus") ) return GAL_EPS_MARK_SHAPE_PLUS;
else if( !strcasecmp(n, "cross") ) return GAL_EPS_MARK_SHAPE_CROSS;
else if( !strcasecmp(n, "point") ) return GAL_EPS_MARK_SHAPE_POINT;
else if( !strcasecmp(n, "circle") ) return GAL_EPS_MARK_SHAPE_CIRCLE;
else if( !strcasecmp(n, "square") ) return GAL_EPS_MARK_SHAPE_SQUARE;
else if( !strcasecmp(n, "ellipse") ) return GAL_EPS_MARK_SHAPE_ELLIPSE;
else if( !strcasecmp(n, "rectangle") ) return GAL_EPS_MARK_SHAPE_RECTANGLE;
else
error(EXIT_FAILURE, 0, "%s: the shape name '%s' is not recognized. "
"The currently recognized shapes are: 'point', 'circle', "
"'square', 'ellipse' or 'rectangle'", __func__, n);
/* Control should not reach here. */
error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at %s to find "
"and solve the problem. Control should not reach this part "
"of the function", __func__, PACKAGE_BUGREPORT);
return GAL_EPS_MARK_SHAPE_INVALID;
}
/* Convert a shape ID to a name */
char *
gal_eps_shape_id_to_name(uint8_t id)
{
/* Return the proper name. */
switch(id)
{
case GAL_EPS_MARK_SHAPE_LINE: return "line";
case GAL_EPS_MARK_SHAPE_PLUS: return "plus";
case GAL_EPS_MARK_SHAPE_CROSS: return "cross";
case GAL_EPS_MARK_SHAPE_POINT: return "point";
case GAL_EPS_MARK_SHAPE_CIRCLE: return "circle";
case GAL_EPS_MARK_SHAPE_SQUARE: return "square";
case GAL_EPS_MARK_SHAPE_ELLIPSE: return "ellipse";
case GAL_EPS_MARK_SHAPE_RECTANGLE: return "rectangle";
default:
error(EXIT_FAILURE, 0, "%s: the shape id '%u' is not recognized. "
"Please see the 'GAL_EPS_MARK_SHAPE_*' macros in "
"'gnuastro/eps.h' for the acceptable ids", __func__, id);
}
/* Control should not reach here. */
error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at %s to find "
"and solve the problem. Control should not reach this part "
"of the function", __func__, PACKAGE_BUGREPORT);
return GAL_BLANK_STRING;
}
/* Find the center coordinate */
static void
eps_mark_to_pt(float x_pix, float y_pix, float size1_pix,
float size2_pix, double pix_in_pt,
uint32_t borderwidth, long ymin, uint8_t shapev,
float *x_pt, float *y_pt,
float *size1_pt, float *size2_pt, float *ymin_pt)
{
/* Necessary shift (in points) because of the coordiante system that has
been edited due to the image and border width */
float shiftpt=borderwidth;
/* Do the conversion: 1) Subtract by half so the bottom-left corners of
the point and FITS X axis match. 2) Multiply by the pixel-to-point
conversion scale. 3) account for the shift. */
*x_pt = (x_pix-0.5) * pix_in_pt + shiftpt;
*y_pt = (y_pix-0.5) * pix_in_pt + shiftpt;
/* Other parameters. Note that for an ellipse, the second size paramter
is the axis ratio. */
*ymin_pt = ymin * pix_in_pt;
*size1_pt = size1_pix * pix_in_pt;
*size2_pt = ( shapev==GAL_EPS_MARK_SHAPE_ELLIPSE
? size2_pix
: size2_pix * pix_in_pt );
}
/* Low-level function to draw the shape in the corrected coordinate system
(after translation and rotation). */
static void
eps_mark_draw_shape(FILE *fp, uint8_t shape, float s1, float s2)
{
/* Draw the shape. */
switch(shape)
{
case GAL_EPS_MARK_SHAPE_LINE:
fprintf(fp, "%f 0 moveto %f 0 lineto \n", -s1/2, s1/2);
break;
case GAL_EPS_MARK_SHAPE_PLUS:
fprintf(fp, "%f 0 moveto %f 0 lineto \n", -s1/2, s1/2);
fprintf(fp, "0 %f moveto 0 %f lineto closepath\n", -s1/2, s1/2);
break;
case GAL_EPS_MARK_SHAPE_CROSS:
fprintf(fp, "%f %f moveto %f %f lineto \n", -s1/M_SQRT2/2,
-s1/M_SQRT2/2, s1/M_SQRT2/2, s1/M_SQRT2/2);
fprintf(fp, "%f %f moveto %f %f lineto \n", -s1/M_SQRT2/2,
s1/M_SQRT2/2, s1/M_SQRT2/2, -s1/M_SQRT2/2);
break;
case GAL_EPS_MARK_SHAPE_POINT:
fprintf(fp, "newpath 0 0 %f 0 360 arc fill closepath\n", s1);
break;
case GAL_EPS_MARK_SHAPE_CIRCLE:
fprintf(fp, "newpath 0 0 %f 0 360 arc closepath\n", s1);
break;
case GAL_EPS_MARK_SHAPE_ELLIPSE:
fprintf(fp, "newpath 0 0 %f %f 0 360 ellipse\n", s1, s1*s2);
break;
case GAL_EPS_MARK_SHAPE_SQUARE:
fprintf(fp, "newpath -%.2f -%.2f %.2f %.2f rectstroke\n",
s1/2, s1/2, s1, s1);
break;
case GAL_EPS_MARK_SHAPE_RECTANGLE:
fprintf(fp, "newpath -%.2f -%.2f %.2f %.2f rectstroke\n",
s1/2, s2/2, s1, s2);
break;
default:
error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at '%s' to "
"fix the problem. The code '%d' is not recognized for the "
"'shape' variable", __func__, PACKAGE_BUGREPORT, shape);
}
}
/* Insert the part about the mark shape. */
static void
eps_mark_add_shape(FILE *fp, size_t index, float x, float y, float s1,
float s2, float linewidth, uint8_t color, uint8_t shape,
float rotate, char *text, float textsize, char *textfont,
float yminpt)
{
float rgb[3];
/* If the rotation angle is negative, add it with 360 so it becomes
positive. */
if(rotate<0.0) rotate+=360.0;
/* Print a comment, identifying that this is a mark. */
fprintf(fp, "%% Mark %zu (a %s):\n", index+1,
gal_eps_shape_id_to_name(shape));
/* If the shape is a circle, or the user gave a rotation angle of zero,
then rotation is irrelevant, so set it to NaN, so we don't even write
it into the file. */
if(shape==GAL_EPS_MARK_SHAPE_CIRCLE || rotate==0.0) rotate=NAN;
/* If 'linewidth' isn't blank, then we need to set it for each mark. */
if( !isnan(linewidth) )
fprintf(fp, "%f setlinewidth\n", linewidth);
/* Set the color (if it hasn't already been set. */
if( color != GAL_BLANK_UINT8 )
{
gal_color_in_rgb(color, rgb);
fprintf(fp, "%.2f %.2f %.2f setrgbcolor\n", rgb[0], rgb[1], rgb[2]);
}
/* Move the coordinate to the desired central coordinate and do the
rotation if requested. */
fprintf(fp, "%f %f translate\n", x, y);
if( !isnan(rotate) ) fprintf(fp, "%f rotate\n", rotate);
/* Draw the shape. */
eps_mark_draw_shape(fp, shape, s1, s2);
/* Correct the rotation. */
if( !isnan(rotate) )
fprintf(fp, "-%f rotate\n", rotate);
fprintf(fp, "-%f -%f translate\n", x, y);
/* Finish the stroke. */
fprintf(fp, "stroke\n\n");
/* Write the text (if 'text' isn't NULL, and it isn't a blank string. */
if( text && strcmp(text, GAL_BLANK_STRING) )
{
fprintf(fp, "%% Text of the mark above\n");
fprintf(fp, "(%s) %g /%s %g %g centertoptext\n\n", text,
textsize, textfont, x, yminpt);
}
}
/* Simplify the checking of various columns. */
static void *
eps_mark_prepare_col(gal_data_t *marks, char *name, uint8_t type,
int abort, char *extrainfo)
{
gal_data_t *tmp=gal_list_data_select_by_name(marks, name);
/* If a list element was found, check if it has the correct type. */
if(tmp)
{
if(tmp->type!=type)
error(EXIT_FAILURE, 0, "%s: the '%s' column should have "
"a %s numeric data type%s", __func__, name,
gal_type_name(type, 1), extrainfo);
return tmp->array;
}
else
{
if(abort)
error(EXIT_FAILURE, 0, "%s: no column with name '%s' was found "
"in the list of mark metadata", __func__, name);
}
/* No list element with the desired name was found. */
return NULL;
}
/* Make sure the inputs are in the desired format and return the usable
arrays for the main function to add marks. */
static void
eps_mark_prepare(gal_data_t *marks, float **x, float **y, uint8_t **shape,
uint8_t **color, float **size1, float **size2,
float **lwidth, float **rotate, char ***text,
char ***font, float **fontsize)
{
size_t i;
gal_data_t *tmp;
/* Make sure all the columns given to 'marks' have the same number
elements. */
for(tmp=marks;tmp!=NULL;tmp=tmp->next)
{
if(tmp->size!=marks->size)
error(EXIT_FAILURE, 0, "%s: the mark column '%s' has "
"a different number of rows, or elements (%zu), than "
"the first (named '%s' with %zu rows)", __func__,
tmp->name, tmp->size, marks->name, marks->size);
}
/* Check each column. */
*x=eps_mark_prepare_col(marks, GAL_EPS_MARK_COLNAME_XPIX,
GAL_TYPE_FLOAT32, 1, "");
*y=eps_mark_prepare_col(marks, GAL_EPS_MARK_COLNAME_YPIX,
GAL_TYPE_FLOAT32, 1, "");
*text=eps_mark_prepare_col(marks, GAL_EPS_MARK_COLNAME_TEXT,
GAL_TYPE_STRING, 0, "");
*font=eps_mark_prepare_col(marks, GAL_EPS_MARK_COLNAME_FONT,
GAL_TYPE_STRING, 0, "");
*size1=eps_mark_prepare_col(marks, GAL_EPS_MARK_COLNAME_SIZE1,
GAL_TYPE_FLOAT32, 0, "");
*size2=eps_mark_prepare_col(marks, GAL_EPS_MARK_COLNAME_SIZE2,
GAL_TYPE_FLOAT32, 0, "");
*rotate=eps_mark_prepare_col(marks, GAL_EPS_MARK_COLNAME_ROTATE,
GAL_TYPE_FLOAT32, 0, "");
*lwidth=eps_mark_prepare_col(marks, GAL_EPS_MARK_COLNAME_LINEWIDTH,
GAL_TYPE_FLOAT32, 0, "");
*fontsize=eps_mark_prepare_col(marks, GAL_EPS_MARK_COLNAME_FONTSIZE,
GAL_TYPE_FLOAT32, 0, "");
*shape=eps_mark_prepare_col(marks, GAL_EPS_MARK_COLNAME_SHAPE,
GAL_TYPE_UINT8, 0, ". Note that the macros "
"containing shape identifiers have the "
"'GAL_EPS_MARK_SHAPE-' prefix and are "
"defined in 'gnuastro/eps.h'");
*color=eps_mark_prepare_col(marks, GAL_EPS_MARK_COLNAME_COLOR,
GAL_TYPE_UINT8, 0, ". Note that the macros "
"containing color identifiers have the "
"'GAL_COLOR_' prefix and are "
"defined in 'gnuastro/color.h'");
/* Some sanity checks for the columns that need one. */
if( *rotate || *shape )
for(i=0;i<marks->size;++i)
{
/* Rotation angle should be between 0 to 360. */
if( *rotate && ((*rotate)[i]<-360 || (*rotate)[i]>360 ) )
error(EXIT_FAILURE, 0, "%s: %g is not a valid rotation angle "
"(in degrees). It belongs to mark number %zu (counting "
"from 1)", __func__, (*rotate)[i], i+1);
/* For an ellipse, the 'size2' column should be positive and
smaller than one. */
if( (*shape)[i]==GAL_EPS_MARK_SHAPE_ELLIPSE
&& *size2
&& ( (*size2)[i]<0 || (*size2)[i]>1 ) )
error(EXIT_FAILURE, 0, "%s: %g is not a valid 'size2' column "
"for an ellipse shape (from mark number %zu, counting "
"from 1). For an ellipse, the 'size2' column is the"
"axis ratio, so it should always be between 0 and 1",
__func__, (*size2)[i], i+1);
}
}
/* Write the default mark properties (like linewidth, color and etc). This
can happen if they aren't given by the user, or that all the values
given by the user are the same. */
static void
eps_mark_add_defaults(FILE *fp, gal_data_t *marks, uint8_t **color_o,
float **lwidth_o, uint8_t *shape, char **text)
{
size_t i;
float rgb[3];
uint8_t *color=*color_o;
float *lwidth=*lwidth_o;
int samelwidth=1, samecolor=1;
/* Parse all the information */
for(i=1;i<marks->size;++i)
{
if(samecolor==1 && color && color[i]!=color[0] ) samecolor=0;
if(samelwidth==1 && lwidth && lwidth[i]!=lwidth[0]) samelwidth=0;
}
/* Default color */
if(samecolor)
{
fprintf(fp, "%% Same color for all marks:\n");
gal_color_in_rgb(color ? color[0] : GAL_EPS_MARK_DEFAULT_COLOR, rgb);
fprintf(fp, "%.2f %.2f %.2f setrgbcolor\n\n", rgb[0], rgb[1], rgb[2]);
}
/* Default line-width. */
if(samelwidth)
{
fprintf(fp, "%% Same line width for all marks:\n");
fprintf(fp, "%f setlinewidth\n\n",
lwidth ? lwidth[0] : GAL_EPS_MARK_DEFAULT_LINEWIDTH);
*lwidth_o=NULL;
}
/* If an ellipse has been requested, define the function (this function
has been inspired from http://www.redgrittybrick.org/ellipse.html */
if(shape)
{
for(i=0;i<marks->size;++i)
if(shape[i]==GAL_EPS_MARK_SHAPE_ELLIPSE)
{
fprintf(fp, "%% Function for ellipse shape:\n");
fprintf(fp, "/ellipse {\n");
fprintf(fp, " /endangle exch def\n");
fprintf(fp, " /startangle exch def\n");
fprintf(fp, " /yrad exch def\n");
fprintf(fp, " /xrad exch def\n");
fprintf(fp, " /y exch def\n");
fprintf(fp, " /x exch def\n");
fprintf(fp, " /savematrix matrix currentmatrix def\n");
fprintf(fp, " x y translate\n");
fprintf(fp, " xrad yrad scale\n");
fprintf(fp, " 0 0 1 startangle endangle arc\n");
fprintf(fp, " savematrix setmatrix\n");
fprintf(fp, "} def\n\n");
break;
}
}
/* In case we have text, use this function to put them such that they the
given coordinate is at the top of the string. This was inspired from
[1] (the main change is that '-2 div' (for 'dy') has been set to '-1
mul' so the given coordinate is at the top. It should be called with
the following format (just set the upper-case words).
(STRING) FONTSIZE FONTNAME X Y centertoptext
[1] https://stackoverflow.com/questions/3618194/how-to-determine-string-height-in-postscript*/
if(text)
{
fprintf(fp, "%% Print text with coordinate at center-top:\n");
fprintf(fp, "/centertoptext {\n");
/* Save the current state. */
fprintf(fp, " gsave\n");
/* Move to the top two popped operands (X and Y), and set the fonts. */
fprintf(fp, " moveto findfont exch scalefont setfont\n");
/* Save the state. */
fprintf(fp, " gsave\n");
/* 1. dup: Duplicate the string.
2. charpath: move to where the string finishes.
3. flattenpath: account for curves.
4. pathbox: put four numbers on the stack:
x0: bottom-left point's X axis position.
y0: bottom-left point's Y axis position.
x1: top-right point's X axis position.
y1: top-right point's Y axis position. */
fprintf(fp, " dup false charpath flattenpath pathbbox\n");
/* Go back to the previous settings (before charpath changed it). */
fprintf(fp, " grestore\n");
/* 3 -1 roll: permutes the top three: y0 x1 y1 ---> x1 y1 y0 */
fprintf(fp, " 3 -1 roll\n");
/* Subtract the second popped operand from the first (at the top, we
now have the height of the text's box or 'dy'. Then multiply this
by -1, giving us '-dy' (which is the distance we want to shift
vertically/downwards before "showing" the string. */
fprintf(fp, " sub -1 mul\n");
/* 3 1 roll: permute the top three points: x0 x1 dy --> dy x0 x1 */
fprintf(fp, " 3 1 roll\n");
/* Subtract x0 from x1 get the negative 'dx' on top of the stack.
Then divide it by two, so we have '-dx/2' (we need to shift the
word by the amount along the horizintal/left-ward before "show"ing
it).*/
fprintf(fp, " sub 2 div\n");
/* Exchange the top operands so they are in this order: '-dx -dy'. */
fprintf(fp, " exch \n");
/* Move (relatively) by the given displacement and print the string
(which has stayed at the bottom of the stack until now) and print
the string.. */
fprintf(fp, " rmoveto show\n");
/* Restore the graphical environment for next paths. */
fprintf(fp, " grestore\n");
fprintf(fp, "} bind def\n\n");
}
}
/* Set the second size parameter (depending on a shape being defined or
not). */
static float
eps_mark_size2(uint8_t *shape, float *size2arr, size_t i)
{
if(size2arr) return size2arr[i];
else
{
if(shape)
switch(shape[i])
{
case GAL_EPS_MARK_SHAPE_ELLIPSE:
return GAL_EPS_MARK_DEFAULT_SIZE2_ELLIPSE;
default: return GAL_EPS_MARK_DEFAULT_SIZE2;
}
else
return GAL_EPS_MARK_DEFAULT_SIZE2;
}
return NAN;
}
/* Fill the 'fpixel' and 'lpixel' arrays for the shapes that may be
affected by rotation. */
static void
eps_mark_in_img_fl_pixel(float x, float y, float s1, float s2,
uint8_t shape, long *fpixel, long *lpixel)
{
long width[2];
double center[2]={x, y};
/* Depending the shape, set the four corners in relation to the shape's
center (these will later be added to the X,Y of the center). */
switch(shape)
{
case GAL_EPS_MARK_SHAPE_LINE:
width[0]=s1; width[1]=0; break; /* EPS line-width will be added */
/* later, this is the box size. */
case GAL_EPS_MARK_SHAPE_PLUS:
case GAL_EPS_MARK_SHAPE_SQUARE:
width[0]=width[1]=s1; break;
case GAL_EPS_MARK_SHAPE_POINT:
case GAL_EPS_MARK_SHAPE_CIRCLE:
width[0]=width[1]=s1*2; break; /* s1 is radius: half of width */
case GAL_EPS_MARK_SHAPE_CROSS:
width[0]=width[1]=M_SQRT2*s1; break;
case GAL_EPS_MARK_SHAPE_RECTANGLE:
width[0]=s1; width[1]=s2;
break;
case GAL_EPS_MARK_SHAPE_ELLIPSE:
width[0]=s1*2; width[1]=s1*s2*2; /* s1: half major axis. */
break; /* s2: axis ratio. */
default:
error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at '%s' to "
"find and fix the problem. The code '%u' isn't recognized "
"for the 'shape' variable", __func__, PACKAGE_BUGREPORT,
shape);
}
/* Find the before-rotation fpixel and lpixel */
gal_box_border_from_center(center, 2, width, fpixel, lpixel);
/* For a check:
printf("%s:center: %g, %g\n", __func__, center[0], center[1]);
printf("%s:width: %ld, %ld\n", __func__, width[0], width[1]);
printf("%s:fpixel: %ld, %ld\n", __func__, fpixel[0], fpixel[1]);
printf("%s:lpixel: %ld, %ld\n", __func__, lpixel[0], lpixel[1]);
*/
}
/* See if the given shape falls within the image or not. Currently, this
doesn't account for rotation due to time limits at development time. */
static int
eps_mark_in_img(size_t *dsize, size_t i, float x, float y,
float *size1arr, float *size2arr,
uint8_t *shapearr, float *rotarr, long *ymin)
{
long naxes[2]={dsize[1], dsize[0]};
float s2 = eps_mark_size2(shapearr, size2arr, i);
long fpixel_i[2], lpixel_i[2], fpixel_o[2], lpixel_o[2];
uint8_t shape = shapearr ? shapearr[i] : GAL_EPS_MARK_DEFAULT_SHAPE;
float s1 = size1arr ? size1arr[i] : GAL_EPS_MARK_DEFAULT_SIZE1;
float rot = rotarr ? rotarr[i] : GAL_EPS_MARK_DEFAULT_ROTATE;
/* Find the first and last pixels of the marks based on its shape. For a
point, things are easy, but for others, we have a separate
function. */
eps_mark_in_img_fl_pixel(x, y, s1, s2, shape, fpixel_i, lpixel_i);
/* Update the box's first and last pixels based on the rotation. This is
not necessary if we don't have any rotation or we are dealing with a
circle shape. */
if( rot!=0.0
&& shape!=GAL_EPS_MARK_SHAPE_POINT
&& shape!=GAL_EPS_MARK_SHAPE_CIRCLE )
gal_box_border_rotate_around_center(fpixel_i, lpixel_i, 2, rot);
/* Set the smallest vertical position of the box. */
*ymin=fpixel_i[1];
/* See if there is any overlap */
return gal_box_overlap(naxes, fpixel_i, lpixel_i,
fpixel_o, lpixel_o, 2);
}
/* Add markers on each desired coordinate. */
static void
eps_mark_add(gal_data_t *in, gal_data_t *marks, FILE *fp,
size_t *w_h_in_pt, uint32_t borderwidth)
{
size_t i;
long ymin;
float lwidthv;
char **text=NULL, **font=NULL;
uint8_t shapev, *shape=NULL, *color=NULL;
float s1, s2, *size1=NULL, *size2=NULL;
float yminpt, *rotate=NULL, *fontsize=NULL;
float x, y, *xarr=NULL, *yarr=NULL, *lwidth=NULL;
/* The size of one pixel in "points". */
double pix_in_pt=(double)(w_h_in_pt[0])/(double)(in->dsize[1]);
/* Load the pointers to the various metdata. */
eps_mark_prepare(marks, &xarr, &yarr, &shape, &color,
&size1, &size2, &lwidth, &rotate, &text,
&font, &fontsize);
/* If the mark parameters are similar, just write them once (instead of
repeating for each mark, and set their pointers to 'NULL' so the rest
of the function doesn't even bother with them). */
eps_mark_add_defaults(fp, marks, &color, &lwidth, shape, text);
/* Write the mark into the EPS file. But only when the requested region
falls within the image (we don't want to waste storage) */
for(i=0;i<marks->size;++i)
if( eps_mark_in_img(in->dsize, i, xarr[i], yarr[i], size1, size2,
shape, rotate, &ymin) )
{
lwidthv=lwidth ? lwidth[i] : GAL_EPS_MARK_DEFAULT_LINEWIDTH;
shapev=shape ? shape[i] : GAL_EPS_MARK_DEFAULT_SHAPE,
eps_mark_to_pt(xarr[i], yarr[i],
size1 ? size1[i] : GAL_EPS_MARK_DEFAULT_SIZE1,
eps_mark_size2(shape, size2, i),
pix_in_pt, borderwidth, ymin, shapev,
&x, &y, &s1, &s2, &yminpt);
eps_mark_add_shape(fp, i, x, y, s1, s2,
lwidth ? lwidth[i] : NAN,
color ? color[i] : GAL_BLANK_UINT8,
shapev,
rotate ? rotate[i] : GAL_BLANK_FLOAT32,
text ? text[i] : NULL,
fontsize ? fontsize[i] : GAL_EPS_MARK_DEFAULT_FONTSIZE,
font ? font[i] : GAL_EPS_MARK_DEFAULT_FONT,
yminpt - lwidthv/2);
}
}
void
gal_eps_write(gal_data_t *in, char *filename, float widthincm,
uint32_t borderwidth, uint8_t bordercolor, int hex,
int dontoptimize, int forps, gal_data_t *marks)
{
FILE *fp;
float hbw;
float rgb[3];
time_t rawtime;
size_t numch=gal_list_data_number(in);
size_t w_h_in_pt[2], *dsize=in->dsize;
/* Sanity checks. */
if(numch==2 || numch>4)
error(EXIT_FAILURE, 0, "%s: only 1, 3, and 4 color channels are "
"acceptable, input is a list of %zu data sets", __func__, numch);
if(in->type!=GAL_TYPE_UINT8)
error(EXIT_FAILURE, 0, "%s: input has a '%s' type, but JPEG images "
"can only have a 'uint8' type", __func__,
gal_type_name(in->type, 1));
/* Read the time to write in the output. */
time(&rawtime);
/* Find the bounding box */
hbw=(float)borderwidth/2.0;
gal_eps_to_pt(widthincm, dsize, w_h_in_pt);
/* Open the output file and write the top comments. */
errno=0;
fp=fopen(filename, "w");
if(fp==NULL)
error(EXIT_FAILURE, errno, "%s", filename);
fprintf(fp, "%%!PS-Adobe-3.0 EPSF-3.0\n");
fprintf(fp, "%%%%BoundingBox: 0 0 %zu %zu\n", w_h_in_pt[0]+2*borderwidth,
w_h_in_pt[1]+2*borderwidth);
fprintf(fp, "%%%%Creator: %s\n", PACKAGE_STRING);
fprintf(fp, "%%%%CreationDate: %s", ctime(&rawtime));
fprintf(fp, "%%%%LanuageLevel: 3\n");
fprintf(fp, "%%%%EndComments\n\n");
if(forps==0)
fprintf(fp, "gsave\n\n");
/* Write the image: */
fprintf(fp, "%% Draw the image:\n");
fprintf(fp, "%d %d translate\n", borderwidth, borderwidth);
fprintf(fp, "%zu %zu scale\n", w_h_in_pt[0], w_h_in_pt[1]);
eps_write_image(in, fp, hex, dontoptimize, forps);
/* Mark parts of the image if necessary. */
if(marks)
eps_mark_add(in, marks, fp, w_h_in_pt, borderwidth);
/* Commands to draw the border: */
if(borderwidth)
{
gal_color_in_rgb(bordercolor, rgb);
fprintf(fp, "%% Draw the border:\n");
fprintf(fp, "%.2f %.2f %.2f setrgbcolor\n", rgb[0], rgb[1], rgb[2]);
fprintf(fp, "%d setlinewidth\n", borderwidth);
fprintf(fp, "%.1f %.1f moveto\n", hbw, hbw);
fprintf(fp, "0 %zu rlineto\n", w_h_in_pt[1]+borderwidth);
fprintf(fp, "%zu 0 rlineto\n", w_h_in_pt[0]+borderwidth);
fprintf(fp, "0 -%zu rlineto\n", w_h_in_pt[1]+borderwidth);
fprintf(fp, "closepath\n");
fprintf(fp, "stroke\n\n");
}
/* Ending of the EPS file: */
fprintf(fp, "%%%%EOF");
fclose(fp);
}
|