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 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
|
// PLplot metafile I/O routines. Originally implemented in the plmeta
// driver and plrender. The decision was made to provide the capability
// to always read/write metafiles, thus the routines have been merged
// into the core of the library.
//
// Copyright (C) 2006 Hazen Babcock
// Copyright (C) 2015 Jim Dishaw
//
// This file is part of PLplot.
//
// PLplot is free software; you can redistribute it and/or modify
// it under the terms of the GNU Library General Public License as published
// by the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// PLplot 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 Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public License
// along with PLplot; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
//
#define NEED_PLDEBUG
#include "plplotP.h"
#include "metadefs.h"
#include <stddef.h> // For the offsetof() macro
#define MAX_BUFFER 256 // Character buffer size for reading records
#if defined ( _MSC_VER ) && _MSC_VER <= 1500
// Older versions of Visual Studio (2005 perhaps 2008) do not define uint8_t
// The newer versions of Visual Studio will not install on Vista or older
// versions of Windows.
typedef unsigned char uint8_t;
#endif
// Not all platforms support the lround() function and the coordinate system
// representation is going to be redone, thus this is just a placeholder until
// everything is sorted out.
#define PLFLT2COORD( a ) ( (short) ceil( a ) )
// Status codes
enum _plm_status
{
PLM_READ_ERROR = -7,
PLM_UNKNOWN_DATATYPE = -6,
PLM_INVALID_STATE = -5,
PLM_INVALID_CMD = -4,
PLM_FORMAT_ERROR = -3,
PLM_UNKNOWN_VERSION = -2,
PLM_NOT_PLMETA_FILE = -1,
PLM_SUCCESS = 0
};
// Portable data format types (perhaps should be defined elsewhere?)
enum _pdf_types
{
PDF_NULL = 0, // No-data type
PDF_UBYTE, // one-byte unsigned integer
PDF_USHORT, // two-byte unsigned integer
PDF_ULONG, // four-byte unsigned integer
PDF_IEEEF // IEEE 32 bit floating point
};
// Data types used by PLplot (perhaps should be defined elsewhere?)
enum _plp_types
{
PLP_NULL = 0, // No-data type
PLP_UBYTE,
PLP_UCHAR,
PLP_USHORT,
PLP_SHORT,
PLP_PLINT,
PLP_PLFLT,
PLP_ULONG,
};
// Data format entry structure
struct _plm_format
{
char *name; // Name of the field
enum _pdf_types pdf_type; // Field data type in metafile
enum _plp_types plp_type; // Field data type in PLplot
const size_t offset; // Byte offset in the plmeta device
};
// Metafile index information header
static const struct _plm_format index_2005_header[] = {
{ "pages", PDF_USHORT, PLP_USHORT, offsetof( PLmIndex, pages ) },
// Last entry, do not change
{ NULL, PDF_NULL, PLP_NULL, 0 }
};
// Device information header
static const struct _plm_format dev_2005_header[] = {
{ "xmin", PDF_USHORT, PLP_PLINT, offsetof( PLmDev, xmin ) },
{ "xmax", PDF_USHORT, PLP_PLINT, offsetof( PLmDev, xmax ) },
{ "ymin", PDF_USHORT, PLP_PLINT, offsetof( PLmDev, ymin ) },
{ "ymax", PDF_USHORT, PLP_PLINT, offsetof( PLmDev, ymax ) },
{ "pxlx", PDF_IEEEF, PLP_PLFLT, offsetof( PLmDev, pxlx ) },
{ "pxly", PDF_IEEEF, PLP_PLFLT, offsetof( PLmDev, pxly ) },
// Last entry, do not change
{ NULL, PDF_NULL, PLP_NULL, 0 }
};
// Plot information header
static const struct _plm_format plot_2005_header[] = {
{ "xdpi", PDF_IEEEF, PLP_PLFLT, offsetof( PLStream, xdpi ) },
{ "ydpi", PDF_IEEEF, PLP_PLFLT, offsetof( PLStream, ydpi ) },
{ "xlength", PDF_USHORT, PLP_PLINT, offsetof( PLStream, xlength ) },
{ "ylength", PDF_USHORT, PLP_PLINT, offsetof( PLStream, ylength ) },
{ "xoffset", PDF_USHORT, PLP_PLINT, offsetof( PLStream, xoffset ) },
{ "yoffset", PDF_USHORT, PLP_PLINT, offsetof( PLStream, yoffset ) },
{ "", PDF_NULL, PLP_NULL, 0 },
// Last entry, do not change
{ NULL, 0, 0 }
};
// Page information header
static const struct _plm_format page_2005_header[] = {
{ "", PDF_USHORT, PLP_PLINT, offsetof( PLmDev, page ) },
{ "", PDF_ULONG, PLP_PLINT, offsetof( PLmDev, lp_offset ) },
{ "", PDF_ULONG, PLP_NULL, 0 }
};
static struct _plm_version
{
PLCHAR_VECTOR identifier;
const struct _plm_format *index_header;
const struct _plm_format *device_header;
const struct _plm_format *plot_header;
} metafile_format[] = {
{ PLMETA_VERSION,
index_2005_header, dev_2005_header, plot_2005_header }
};
static
enum _plm_status set_ubyte_plp_value( uint8_t x,
void *dest, enum _plp_types type )
{
enum _plm_status rc = PLM_SUCCESS;
switch ( type )
{
case PLP_NULL:
break;
case PLP_UBYTE:
*(uint8_t *) dest = x;
break;
case PLP_UCHAR:
*(unsigned char *) dest = x;
break;
case PLP_USHORT:
*(U_SHORT *) dest = x;
break;
case PLP_SHORT:
*(short *) dest = x;
break;
case PLP_PLINT:
*(PLINT *) dest = x;
break;
case PLP_PLFLT:
*(PLFLT *) dest = x;
break;
case PLP_ULONG:
*(U_LONG *) dest = x;
break;
default:
plwarn( "Unhandled datatype conversion in set_plp_value." );
rc = PLM_UNKNOWN_DATATYPE;
break;
}
return rc;
}
static
enum _plm_status set_ushort_plp_value( U_SHORT x,
void *dest, enum _plp_types type )
{
enum _plm_status rc = PLM_SUCCESS;
switch ( type )
{
case PLP_NULL:
break;
case PLP_UBYTE:
*(uint8_t *) dest = x;
break;
case PLP_UCHAR:
*(unsigned char *) dest = x;
break;
case PLP_USHORT:
*(U_SHORT *) dest = x;
break;
case PLP_SHORT:
*(short *) dest = x;
break;
case PLP_PLINT:
*(PLINT *) dest = x;
break;
case PLP_PLFLT:
*(PLFLT *) dest = x;
break;
case PLP_ULONG:
*(U_LONG *) dest = x;
break;
default:
plwarn( "Unhandled datatype conversion in set_plp_value." );
rc = PLM_UNKNOWN_DATATYPE;
break;
}
return rc;
}
static
enum _plm_status set_ulong_plp_value( unsigned long x,
void *dest, enum _plp_types type )
{
enum _plm_status rc = PLM_SUCCESS;
switch ( type )
{
case PLP_NULL:
break;
case PLP_UBYTE:
*(uint8_t *) dest = x;
break;
case PLP_UCHAR:
*(unsigned char *) dest = x;
break;
case PLP_USHORT:
*(U_SHORT *) dest = x;
break;
case PLP_SHORT:
*(short *) dest = x;
break;
case PLP_PLINT:
*(PLINT *) dest = x;
break;
case PLP_PLFLT:
*(PLFLT *) dest = x;
break;
case PLP_ULONG:
*(U_LONG *) dest = x;
break;
default:
plwarn( "Unhandled datatype conversion in set_plp_value." );
rc = PLM_UNKNOWN_DATATYPE;
break;
}
return rc;
}
static
enum _plm_status set_ieeef_plp_value( float x,
void *dest, enum _plp_types type )
{
enum _plm_status rc = PLM_SUCCESS;
switch ( type )
{
case PLP_NULL:
break;
case PLP_UBYTE:
*(uint8_t *) dest = x;
break;
case PLP_UCHAR:
*(unsigned char *) dest = x;
break;
case PLP_USHORT:
*(U_SHORT *) dest = x;
break;
case PLP_SHORT:
*(short *) dest = x;
break;
case PLP_PLINT:
*(PLINT *) dest = x;
break;
case PLP_PLFLT:
*(PLFLT *) dest = x;
break;
case PLP_ULONG:
*(U_LONG *) dest = x;
break;
default:
plwarn( "Unhandled datatype conversion in set_plp_value." );
rc = PLM_UNKNOWN_DATATYPE;
break;
}
return rc;
}
static
enum _plm_status read_entry( PDFstrm *plm,
enum _pdf_types from_type,
enum _plp_types to_type,
void *dest )
{
uint8_t b;
unsigned long l;
U_SHORT x;
float f;
enum _plm_status rc;
int pdf_rc = 0;
switch ( from_type )
{
case PLP_NULL:
pdf_rc = 0;
rc = PLM_SUCCESS;
break;
case PDF_UBYTE: // Unsigned one-byte integer
if ( ( pdf_rc = pdf_rd_1byte( plm, &b ) ) == 0 )
rc = set_ubyte_plp_value( b, dest, to_type );
break;
case PDF_USHORT: // Unsigned two-byte integer
if ( ( pdf_rc = pdf_rd_2bytes( plm, &x ) ) == 0 )
rc = set_ushort_plp_value( x, dest, to_type );
break;
case PDF_ULONG: // Unsigned four-byte integer
if ( ( pdf_rc = pdf_rd_4bytes( plm, &l ) ) == 0 )
rc = set_ulong_plp_value( l, dest, to_type );
break;
case PDF_IEEEF: // IEEE 32 bit float
if ( ( pdf_rc = pdf_rd_ieeef( plm, &f ) ) == 0 )
rc = set_ieeef_plp_value( f, dest, to_type );
break;
default:
plwarn( "Unhandled datatype conversion in read_entry." );
rc = PLM_UNKNOWN_DATATYPE;
break;
}
if ( pdf_rc == 0 )
return rc;
else
return PLM_READ_ERROR;
}
static
enum _plm_status read_string( PDFstrm *plm,
size_t bytes,
char *dest )
{
int rc;
rc = pdf_rd_string( plm, dest, bytes );
if ( rc == 0 )
return PLM_SUCCESS;
else
return PLM_READ_ERROR;
}
//--------------------------------------------------------------------------
// read_metafile_header()
//
// Attempts to read the metafile header information in order to see if
// is a metafile and identify the version.
//--------------------------------------------------------------------------
static
enum _plm_status read_metafile_header( PDFstrm *plm, PLmDev *dev )
{
char buffer[MAX_BUFFER];
PLINT version;
PLINT num_format_entries = sizeof ( metafile_format ) / sizeof ( struct _plm_version );
dbug_enter( "read_metafile_enter()" );
// Attempt to identify that this is a PLplot metafile
plm_rd( pdf_rd_header( plm, buffer ) );
if ( strcmp( buffer, PLMETA_HEADER ) != 0 )
{
return PLM_NOT_PLMETA_FILE;
}
// Attempt to identify the version number
plm_rd( pdf_rd_header( plm, buffer ) );
for ( version = 0;
version < num_format_entries
&& strcmp( metafile_format[version].identifier, buffer ) != 0;
version++ )
;
if ( version >= num_format_entries )
return ( PLM_UNKNOWN_VERSION );
dev->version = version;
return PLM_SUCCESS;
}
static
void check_buffer_size( PLmDev *dev, size_t need_size )
{
// Do we have enough space?
if ( need_size > dev->buffer_size )
{
// Nope, free the current buffer if it is allocated
if ( dev->buffer != NULL )
free( dev->buffer );
dev->buffer = malloc( need_size );
if ( dev->buffer == NULL )
{
plexit( "plmetafie: Insufficient memory for temporary storage" );
}
dev->buffer_size = need_size;
}
}
//--------------------------------------------------------------------------
// read_header()
//
// Read a header block from the plot metafile.
//
// NOTE: Currently we enforce rigid adherence to the order
// specified by the format. This can be changed so that the
// entries a looked up instead.
//--------------------------------------------------------------------------
static
enum _plm_status read_header( PDFstrm *plm,
const struct _plm_format *header,
uint8_t * dest )
{
char buffer[MAX_BUFFER];
unsigned int entry;
enum _plm_status rc;
for ( entry = 0;
header[entry].name != NULL;
entry++ )
{
// Get the name of this field and verify it is correct
plm_rd( pdf_rd_header( plm, buffer ) );
if ( strcmp( header[entry].name, buffer ) != 0 )
{
return PLM_FORMAT_ERROR;
}
rc = read_entry( plm,
header[entry].pdf_type,
header[entry].plp_type,
dest + header[entry].offset );
if ( rc != PLM_SUCCESS )
return rc;
}
return PLM_SUCCESS;
}
//--------------------------------------------------------------------------
// read_line()
//
// Process a LINE command from the metafile
//--------------------------------------------------------------------------
static
enum _plm_status read_line( PDFstrm *plm, PLmDev *dev, PLStream *pls )
{
PLFLT x1, y1, x2, y2;
short x[2], y[2];
enum _plm_status rc;
// Read the start and end points
// The metafile stores the points as x,y pairs
rc = read_entry( plm, PDF_USHORT, PLP_PLFLT, &x1 );
if ( rc != PLM_SUCCESS )
return rc;
rc = read_entry( plm, PDF_USHORT, PLP_PLFLT, &y1 );
if ( rc != PLM_SUCCESS )
return rc;
rc = read_entry( plm, PDF_USHORT, PLP_PLFLT, &x2 );
if ( rc != PLM_SUCCESS )
return rc;
rc = read_entry( plm, PDF_USHORT, PLP_PLFLT, &y2 );
if ( rc != PLM_SUCCESS )
return rc;
// Transform the coordinates from the meta device to the current
// device coordinate system
x[0] = PLFLT2COORD( dev->mfpcxa * x1 + dev->mfpcxb );
y[0] = PLFLT2COORD( dev->mfpcya * y1 + dev->mfpcyb );
x[1] = PLFLT2COORD( dev->mfpcxa * x2 + dev->mfpcxb );
y[1] = PLFLT2COORD( dev->mfpcya * y2 + dev->mfpcyb );
// Draw the line
plP_line( x, y );
// Preserve the last XY coords for the LINETO command
dev->xold = (short) x[1];
dev->yold = (short) y[1];
return PLM_SUCCESS;
}
//--------------------------------------------------------------------------
// read_lineto()
//
// Process a LINE command from the metafile
//--------------------------------------------------------------------------
static
enum _plm_status read_lineto( PDFstrm *plm, PLmDev *dev, PLStream *pls )
{
PLFLT x2, y2;
short x[2], y[2];
int i;
enum _plm_status rc;
// Set the start to the last known position
x[0] = (PLFLT) dev->xold;
y[0] = (PLFLT) dev->yold;
// Read the end point
rc = read_entry( plm, PDF_USHORT, PLP_PLFLT, &x2 );
if ( rc != PLM_SUCCESS )
return rc;
rc = read_entry( plm, PDF_USHORT, PLP_PLFLT, &y2 );
if ( rc != PLM_SUCCESS )
return rc;
// Transform the coordinates from the meta device to the current
// device coordinate system
x[1] = PLFLT2COORD( dev->mfpcxa * x2 + dev->mfpcxb );
y[1] = PLFLT2COORD( dev->mfpcya * y2 + dev->mfpcyb );
// Draw the line
plP_line( x, y );
// Preserve the last XY coords for the LINETO command
dev->xold = (short) x[1];
dev->yold = (short) y[1];
return PLM_SUCCESS;
}
//--------------------------------------------------------------------------
// read_polyline()
//
// Process a POLYLINE command from the metafile
//--------------------------------------------------------------------------
static
enum _plm_status read_polyline( PDFstrm *plm, PLmDev *dev, PLStream *pls )
{
PLINT i, npts;
PLFLT x, y;
short *xd, *yd;
enum _plm_status rc;
// Read the number of control points and put into the plot buffer
rc = read_entry( plm, PDF_USHORT, PLP_PLINT, &npts );
if ( rc != PLM_SUCCESS )
return rc;
// Setup temporary storage. We need 2 * npts to store the X,Y pairs
check_buffer_size( dev, sizeof ( short ) * npts * 2 );
// Setup storage for the x values and y values
xd = (short *) ( dev->buffer );
yd = ( (short *) ( dev->buffer ) ) + npts;
// Read the points and insert into the plot buffer
// The x values
for ( i = 0; i < npts; i++ )
{
rc = read_entry( plm, PDF_USHORT, PLP_PLFLT, &x );
if ( rc != PLM_SUCCESS )
return rc;
// Transform the coordinates from the meta device to the current
// device coordinate system
xd[i] = PLFLT2COORD( dev->mfpcxa * x + dev->mfpcxb );
}
// Preserve the last X value for the LINETO command
dev->xold = xd[npts - 1];
// The y values
for ( i = 0; i < npts; i++ )
{
rc = read_entry( plm, PDF_USHORT, PLP_PLFLT, &y );
if ( rc != PLM_SUCCESS )
return rc;
// Transform the coordinates from the meta device to the current
// device coordinate system
yd[i] = PLFLT2COORD( dev->mfpcya * y + dev->mfpcyb );
}
// Preserve the last Y value for the LINETO command
dev->yold = yd[npts - 1];
// Draw the line
plP_polyline( xd, yd, npts );
return PLM_SUCCESS;
}
//--------------------------------------------------------------------------
// read_escape()
//
// Process a escape command from the metafile
//--------------------------------------------------------------------------
static
enum _plm_status read_escape( PDFstrm *plm, PLmDev *dev, PLStream *pls )
{
uint8_t op;
enum _plm_status rc = PLM_SUCCESS;
// Read the state operation, return if an error
if ( pdf_rd_1byte( plm, &op ) != 0 )
return PLM_FORMAT_ERROR;
switch ( op )
{
case PLESC_FILL:
{
PLINT i, npts;
PLFLT x, y;
short *xd, *yd;
// Get the number of control points for the fill
rc = read_entry( plm, PDF_USHORT, PLP_PLINT, &npts );
if ( rc != PLM_SUCCESS )
return rc;
// Setup temporary storage. We need 2 * npts to store the X,Y pairs
check_buffer_size( dev, sizeof ( short ) * npts * 2 );
// Setup storage for the x values and y values
xd = (short *) ( dev->buffer );
yd = ( (short *) ( dev->buffer ) ) + npts;
for ( i = 0; i < npts; i++ )
{
rc = read_entry( plm, PDF_USHORT, PLP_PLFLT, &x );
if ( rc != PLM_SUCCESS )
return rc;
rc = read_entry( plm, PDF_USHORT, PLP_PLFLT, &y );
if ( rc != PLM_SUCCESS )
return rc;
// Transform the coordinates from the meta device to the current
// device coordinate system
xd[i] = PLFLT2COORD( dev->mfpcxa * x + dev->mfpcxb );
yd[i] = PLFLT2COORD( dev->mfpcya * y + dev->mfpcyb );
}
plP_fill( xd, yd, npts );
}
break;
case PLESC_SWIN:
rc = PLM_SUCCESS;
break;
// Text handling. The metafile contains unprocessed string
// data
case PLESC_HAS_TEXT:
{
EscText text;
PLFLT xform[4];
PLUNICODE fci, ch;
PLINT i;
PLFLT xmin, xmax, ymin, ymax;
PLFLT x, y, refx, refy;
size_t len;
// Setup storage for the transformation matrix
text.xform = xform;
// Read the information from the metafile
rc = read_entry( plm, PDF_IEEEF, PLP_PLFLT, &pls->chrht );
rc = read_entry( plm, PDF_IEEEF, PLP_PLFLT, &pls->diorot );
rc = read_entry( plm, PDF_USHORT, PLP_PLFLT, &xmin );
rc = read_entry( plm, PDF_USHORT, PLP_PLFLT, &xmax );
rc = read_entry( plm, PDF_USHORT, PLP_PLFLT, &ymin );
rc = read_entry( plm, PDF_USHORT, PLP_PLFLT, &ymax );
rc = read_entry( plm, PDF_USHORT, PLP_PLINT, &text.base );
rc = read_entry( plm, PDF_IEEEF, PLP_PLFLT, &text.just );
rc = read_entry( plm, PDF_IEEEF, PLP_PLFLT, &text.xform[0] );
rc = read_entry( plm, PDF_IEEEF, PLP_PLFLT, &text.xform[1] );
rc = read_entry( plm, PDF_IEEEF, PLP_PLFLT, &text.xform[2] );
rc = read_entry( plm, PDF_IEEEF, PLP_PLFLT, &text.xform[3] );
rc = read_entry( plm, PDF_USHORT, PLP_PLFLT, &x );
rc = read_entry( plm, PDF_USHORT, PLP_PLFLT, &y );
rc = read_entry( plm, PDF_USHORT, PLP_PLFLT, &refx );
rc = read_entry( plm, PDF_USHORT, PLP_PLFLT, &refy );
rc = read_entry( plm, PDF_UBYTE, PLP_UCHAR, &text.font_face );
// Check for a size mismatch that indicates a problem in the
// library that the developers need to fix
if ( sizeof ( text.text_type ) != sizeof ( U_LONG ) )
{
plwarn( "plmetafile: Serious library error! text_type != U_LONG" );
return PLM_FORMAT_ERROR;
}
rc = read_entry( plm, PDF_ULONG, PLP_ULONG, &text.text_type );
// Translate coordinates to the device coordinate system
pls->clpxmi = PLFLT2COORD( dev->mfpcxa * xmin + dev->mfpcxb );
pls->clpxma = PLFLT2COORD( dev->mfpcxa * xmax + dev->mfpcxb );
pls->clpymi = PLFLT2COORD( dev->mfpcxa * ymin + dev->mfpcxb );
pls->clpyma = PLFLT2COORD( dev->mfpcxa * ymax + dev->mfpcxb );
text.x = PLFLT2COORD( dev->mfpcxa * x + dev->mfpcxb );
text.y = PLFLT2COORD( dev->mfpcya * y + dev->mfpcyb );
text.refx = PLFLT2COORD( dev->mfpcxa * refx + dev->mfpcxb );
text.refy = PLFLT2COORD( dev->mfpcya * refy + dev->mfpcyb );
if ( text.text_type == PL_STRING_TEXT )
{
// Retrieve the text string
rc = read_entry( plm, PDF_USHORT, PLP_ULONG, &len );
// Add one to the length for the NUL character. The metafile
// format stores the NUL, so we must read it.
len++;
// Check that we have enough storage for the string
check_buffer_size( dev, len * sizeof ( char ) );
// Read the string from the metafile
rc = read_string( plm, len, dev->buffer );
text.string = (char *) dev->buffer;
// Call the text rendering routine
plP_text(
text.base, text.just, text.xform,
text.x, text.y,
text.refx, text.refy,
text.string );
}
else
{
rc = read_entry( plm, PDF_USHORT, PLP_PLINT, &text.symbol );
plhrsh( text.symbol, text.x, text.y );
}
}
rc = PLM_SUCCESS;
break;
// Alternate unicode text handling
case PLESC_BEGIN_TEXT:
case PLESC_TEXT_CHAR:
case PLESC_CONTROL_CHAR:
case PLESC_END_TEXT:
// NOP these for now until a decision is made
// which method should be implemented for metafiles
plwarn( "plmetafile: Alternate Unicode text handling is not implemented" );
rc = PLM_INVALID_CMD;
break;
default:
break;
}
return rc;
}
//--------------------------------------------------------------------------
// read_state()
//
// Process a state command from the metafile
//--------------------------------------------------------------------------
static
enum _plm_status read_state( PDFstrm *plm, PLmDev *dev, PLStream *pls )
{
uint8_t op;
enum _plm_status rc = PLM_SUCCESS;
// Read the state operation, return if an error
if ( pdf_rd_1byte( plm, &op ) != 0 )
return PLM_FORMAT_ERROR;
switch ( op )
{
case PLSTATE_WIDTH:
pldebug( "state: WIDTH" );
rc = read_entry( plm, PDF_USHORT, PLP_PLFLT, &( pls->width ) );
if ( rc != PLM_SUCCESS )
return rc;
break;
case PLSTATE_COLOR0:
case PLSTATE_COLOR1:
pldebug( "state: COLOR0/COLOR1" );
{
PLINT icol;
// Read the color index number
rc = read_entry( plm, PDF_USHORT, PLP_PLINT, &icol );
if ( rc != PLM_SUCCESS )
return rc;
// Was pen 0 set to an RGB value rather than color index?
if ( op == PLSTATE_COLOR0 && icol != PL_RGB_COLOR )
{
pls->icol0 = icol;
pls->curcolor.r = pls->cmap0[icol].r;
pls->curcolor.g = pls->cmap0[icol].g;
pls->curcolor.b = pls->cmap0[icol].b;
pls->curcolor.a = pls->cmap0[icol].a;
}
else if ( op == PLSTATE_COLOR1 )
{
pls->icol1 = icol;
pls->curcolor.r = pls->cmap1[icol].r;
pls->curcolor.g = pls->cmap1[icol].g;
pls->curcolor.b = pls->cmap1[icol].b;
pls->curcolor.a = pls->cmap1[icol].a;
}
else
{
// Get the RGB value and copy to the plot buffer
PLColor color;
rc = read_entry( plm, PDF_UBYTE, PLP_UCHAR, &color.r );
if ( rc != PLM_SUCCESS )
return rc;
rc = read_entry( plm, PDF_UBYTE, PLP_UCHAR, &color.g );
if ( rc != PLM_SUCCESS )
return rc;
rc = read_entry( plm, PDF_UBYTE, PLP_UCHAR, &color.b );
if ( rc != PLM_SUCCESS )
return rc;
pls->icol0 = icol;
pls->curcolor.r = color.r;
pls->curcolor.g = color.g;
pls->curcolor.b = color.b;
pls->curcolor.a = 1.0;
}
}
break;
case PLSTATE_FILL:
pldebug( "state: FILL" );
// Read the pattern and put into the plot buffer
rc = read_entry( plm, PDF_USHORT, PLP_UCHAR, &( pls->patt ) );
if ( rc != PLM_SUCCESS )
return rc;
break;
case PLSTATE_CMAP0:
case PLSTATE_CMAP1:
pldebug( "state: CMAP0/CMAP1" );
{
PLINT i, ncol;
PLINT *r, *g, *b;
PLFLT *alpha;
void *ptr;
// Read the number of colors
rc = read_entry( plm, PDF_USHORT, PLP_PLINT, &ncol );
if ( rc != PLM_SUCCESS )
return rc;
// Check that temporary storage is sized correctly
check_buffer_size(
dev,
sizeof ( PLINT ) * ncol * 3 // R, G, B values
+ sizeof ( PLFLT ) * ncol ); // alpha channel values
ptr = dev->buffer;
r = (PLINT *) ptr;
ptr = ( (PLINT *) ptr ) + ncol;
g = (PLINT *) ptr;
ptr = ( (PLINT *) ptr ) + ncol;
b = (PLINT *) ptr;
ptr = ( (PLINT *) ptr ) + ncol;
alpha = (PLFLT *) ptr;
// Read the colormap
for ( i = 0; i < ncol; i++ )
{
rc = read_entry( plm, PDF_UBYTE, PLP_PLINT, &( r[i] ) );
if ( rc != PLM_SUCCESS )
return rc;
rc = read_entry( plm, PDF_UBYTE, PLP_PLINT, &( g[i] ) );
if ( rc != PLM_SUCCESS )
return rc;
rc = read_entry( plm, PDF_UBYTE, PLP_PLINT, &( b[i] ) );
if ( rc != PLM_SUCCESS )
return rc;
alpha[i] = 1.0;
}
// Call the colormap API so that memory is correctly allocated
// instead of plP_state( PLSTATE_CMAP0 );
if ( op == PLSTATE_CMAP0 )
plscmap0a( r, g, b, alpha, ncol );
else
plscmap1a( r, g, b, alpha, ncol );
// Return here because plscmap0a and plscmap1a call
// plP_state( PLSTATE_CMAP0 or PLSTATE_CMAP1 )
return PLM_SUCCESS;
}
break;
case PLSTATE_CHR:
pldebug( "state: CHR" );
// The 2005 version and earlier do not support this operation
if ( 1 )
{
rc = read_entry( plm, PDF_IEEEF, PLP_PLFLT, &( pls->chrdef ) );
if ( rc != PLM_SUCCESS )
return rc;
rc = read_entry( plm, PDF_IEEEF, PLP_PLFLT, &( pls->chrht ) );
if ( rc != PLM_SUCCESS )
return rc;
}
break;
case PLSTATE_SYM:
pldebug( "state: SYM" );
// The 2005 version and earlier do not support this operation
if ( 1 )
{
rc = read_entry( plm, PDF_IEEEF, PLP_PLFLT, &( pls->symdef ) );
if ( rc != PLM_SUCCESS )
return rc;
rc = read_entry( plm, PDF_IEEEF, PLP_PLFLT, &( pls->symht ) );
if ( rc != PLM_SUCCESS )
return rc;
}
break;
default:
pldebug( "state: INVALID STATE" );
return PLM_INVALID_STATE;
}
plP_state( op );
return PLM_SUCCESS;
}
//--------------------------------------------------------------------------
// read_plot_commands()
//
// Reads the plot commands from the metafile and places them into the
// plot buffer
//--------------------------------------------------------------------------
static
enum _plm_status read_plot_commands( PDFstrm *plm, PLmDev *dev, PLStream *pls )
{
uint8_t cmd;
enum _plm_status rc = PLM_SUCCESS;
dbug_enter( "read_plot_commands()" );
// Read the metafile until a non-zero result occurs, which typically
// indicates an end-of-file condition
while ( rc == PLM_SUCCESS && pdf_rd_1byte( plm, &cmd ) == 0 )
{
switch ( cmd )
{
case INITIALIZE:
pldebug( "cmd: INITIALIZE" );
// No action needed
break;
case CLOSE:
pldebug( "cmd: CLOSE" );
// No action needed
break;
case EOP:
pldebug( "cmd: EOP" );
plP_eop();
break;
case BOP:
case BOP0: // First BOP in a file
pldebug( "cmd: BOP/BOP0" );
// Read the metadata for this page
rc = read_entry( plm,
page_2005_header[0].pdf_type,
page_2005_header[0].plp_type,
(uint8_t *) dev + page_2005_header[0].offset );
if ( rc != PLM_SUCCESS )
break;
rc = read_entry( plm,
page_2005_header[1].pdf_type,
page_2005_header[1].plp_type,
(uint8_t *) dev + page_2005_header[1].offset );
if ( rc != PLM_SUCCESS )
break;
rc = read_entry( plm,
page_2005_header[2].pdf_type,
page_2005_header[2].plp_type,
(uint8_t *) dev + page_2005_header[2].offset );
if ( rc != PLM_SUCCESS )
break;
plP_bop();
break;
case LINE:
pldebug( "cmd: LINE" );
rc = read_line( plm, dev, pls );
break;
case LINETO:
pldebug( "cmd: LINETO" );
rc = read_lineto( plm, dev, pls );
break;
case ESCAPE:
pldebug( "cmd: ESCAPE" );
rc = read_escape( plm, dev, pls );
break;
case POLYLINE:
pldebug( "cmd: POLYLINE" );
rc = read_polyline( plm, dev, pls );
break;
case CHANGE_STATE:
pldebug( "cmd: CHANGE_STATE" );
rc = read_state( plm, dev, pls );
break;
case END_OF_FIELD:
pldebug( "cmd: EOF" );
// No action needed
break;
case SWITCH_TO_TEXT: // Obsolete, replaced by ESCAPE
case SWITCH_TO_GRAPH: // Obsolete, replaced by ESCAPE
case NEW_COLOR: // Obsolete, replaced by CHANGE_STATE
case NEW_WIDTH: // Obsolete, replaced by CHANGE_STATE
case ADVANCE: // Obsolete, BOP/EOP used instead
case NEW_COLOR1:
pldebug( "cmd: OBSOLETE CMD" );
plabort( "OBSOLETE CMD" );
break;
default:
pldebug( "cmd: INVALID CMD" );
plabort( "INVALID CMD" );
return PLM_INVALID_CMD;
}
}
return PLM_SUCCESS;
}
static
void setup_page( PLmDev *mf_dev, PLStream *pls )
{
PLmDev *dev = pls->dev;
mf_dev->mfpcxa = (PLFLT) dev->xlen
/ (PLFLT) ( mf_dev->xmax - mf_dev->xmin );
mf_dev->mfpcxb = (PLFLT) dev->xmin;
mf_dev->mfpcya = (PLFLT) dev->ylen
/ (PLFLT) ( mf_dev->ymax - mf_dev->ymin );
mf_dev->mfpcyb = (PLFLT) dev->ymin;
}
//--------------------------------------------------------------------------
// plreadmetafile()
//
//! Reads a PLplot metafile and uses the current plot stream to display
//! the contents. If the plot stream has not been initialized, this
//! routine will attempt to intialize the plot stream via a plinit()
//! call. For an initialized plot stream, the metafile will start at the
//! current page/subpage.
//!
//! @param infile Input PLplot metafile name.
//!
//! Pass NULL for infile to use the filename passed from the command line
//! option -mfi.
//!
//! Returns void
//--------------------------------------------------------------------------
void plreadmetafile( char *infile )
{
PDFstrm *plm = NULL;
PLStream mf_pls;
PLmDev mf_dev;
PLmIndex index;
enum _plm_status rc;
if ( plsc->mf_infile == NULL && infile == NULL )
{
plexit( "No PLplot metafile set for input" );
}
else if ( infile != NULL )
{
plm = pdf_fopen( infile, "rb" );
}
else
{
plm = pdf_fopen( plsc->mf_infile, "rb" );
}
if ( plm == NULL )
{
plexit( "Unable to open PLplot metafile for input" );
}
// Intialize the metafile device
mf_dev.buffer = NULL;
mf_dev.buffer_size = 0;
// Read the file header
if ( ( rc = read_metafile_header( plm, &mf_dev ) ) != PLM_SUCCESS )
{
pdf_close( plm );
plwarn( "Failed to parse PLplot metafile, ignoring file." );
return;
}
// Read the index header
rc = read_header( plm,
metafile_format[mf_dev.version].index_header,
(U_CHAR *) &index );
if ( rc != PLM_SUCCESS )
{
pdf_close( plm );
plwarn( "Corrupted index in metafile, ignoring file." );
return;
}
// Read the device header
rc = read_header( plm,
metafile_format[mf_dev.version].device_header,
(U_CHAR *) &mf_dev );
if ( rc != PLM_SUCCESS )
{
pdf_close( plm );
plwarn( "Corrupted device information in metafile, ignoring file." );
return;
}
// Read the plot header into a local version of a plot stream.
// We do this because some of the parameters from the metafile may
// be invalid or inappropriate for the current plot device
// (e.g. xlength = 0). The plspage() call should be smart enough
// to setup the page correctly.
rc = read_header( plm,
metafile_format[mf_dev.version].plot_header,
(U_CHAR *) &mf_pls );
if ( rc != PLM_SUCCESS )
{
pdf_close( plm );
plwarn( "Corrupted device information in metafile, ignoring file." );
return;
}
// Is the plot stream initialized?
if ( plsc->level == 0 )
{
// No, we must intialize it in order to get the
// device configuation set
plinit();
}
setup_page( &mf_dev, plsc );
// At this point we should be in the plot commands
rc = read_plot_commands( plm, &mf_dev, plsc );
if ( rc != PLM_SUCCESS )
{
pdf_close( plm );
plwarn( "Corrupted plot information in metafile, ignoring file." );
return;
}
pdf_close( plm );
// Free the temporary storage
if ( mf_dev.buffer != NULL )
free( mf_dev.buffer );
}
|