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 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
|
/* $Id: plbuf.c,v 1.5 2008/01/14 15:04:01 ajb Exp $
Handle plot buffer.
Copyright (C) 1992 Maurice LeBrun
Copyright (C) 2004 Alan W. Irwin
Copyright (C) 2005 Thomas J. Duck
Copyright (C) 2006 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 General Library 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 "drivers.h"
#include "metadefs.h"
#include <string.h>
/* Function prototypes */
void * plbuf_save(PLStream *pls, void *state);
static int rd_command (PLStream *pls, U_CHAR *p_c);
static void rd_data (PLStream *pls, void *buf, size_t buf_size);
static void wr_command (PLStream *pls, U_CHAR c);
static void wr_data (PLStream *pls, void *buf, size_t buf_size);
static void plbuf_control (PLStream *pls, U_CHAR c);
static void rdbuf_init (PLStream *pls);
static void rdbuf_line (PLStream *pls);
static void rdbuf_polyline (PLStream *pls);
static void rdbuf_eop (PLStream *pls);
static void rdbuf_bop (PLStream *pls);
static void rdbuf_state (PLStream *pls);
static void rdbuf_esc (PLStream *pls);
static void plbuf_fill (PLStream *pls);
static void rdbuf_fill (PLStream *pls);
static void plbuf_swin (PLStream *pls, PLWindow *plwin);
static void rdbuf_swin (PLStream *pls);
/*--------------------------------------------------------------------------*\
* plbuf_init()
*
* Initialize device.
* Actually just disables writes if plot buffer is already open (occurs
* when one stream is cloned, as in printing).
\*--------------------------------------------------------------------------*/
void
plbuf_init(PLStream *pls)
{
dbug_enter("plbuf_init");
pls->plbuf_read = FALSE;
#ifdef BUFFERED_FILE
if (pls->plbufFile != NULL)
pls->plbuf_write = FALSE;
#else
if (pls->plbuf_buffer != NULL)
pls->plbuf_write = FALSE;
#endif
}
/*--------------------------------------------------------------------------*\
* plbuf_line()
*
* Draw a line in the current color from (x1,y1) to (x2,y2).
\*--------------------------------------------------------------------------*/
void
plbuf_line(PLStream *pls, short x1a, short y1a, short x2a, short y2a)
{
short xpl[2], ypl[2];
dbug_enter("plbuf_line");
wr_command(pls, (U_CHAR) LINE);
xpl[0] = x1a;
xpl[1] = x2a;
ypl[0] = y1a;
ypl[1] = y2a;
wr_data(pls, xpl, sizeof(short) * 2);
wr_data(pls, ypl, sizeof(short) * 2);
}
/*--------------------------------------------------------------------------*\
* plbuf_polyline()
*
* Draw a polyline in the current color.
\*--------------------------------------------------------------------------*/
void
plbuf_polyline(PLStream *pls, short *xa, short *ya, PLINT npts)
{
dbug_enter("plbuf_polyline");
wr_command(pls, (U_CHAR) POLYLINE);
wr_data(pls, &npts, sizeof(PLINT));
wr_data(pls, xa, sizeof(short) * npts);
wr_data(pls, ya, sizeof(short) * npts);
}
/*--------------------------------------------------------------------------*\
* plbuf_eop()
*
* End of page.
\*--------------------------------------------------------------------------*/
void
plbuf_eop(PLStream *pls)
{
(void) pls; /* pmr: make it used */
dbug_enter("plbuf_eop");
}
/*--------------------------------------------------------------------------*\
* plbuf_bop()
*
* Set up for the next page.
* To avoid problems redisplaying partially filled pages, on each BOP the
* old file is thrown away and a new one is obtained. This way we can just
* read up to EOF to get everything on the current page.
*
* Also write state information to ensure the next page is correct.
\*--------------------------------------------------------------------------*/
void
plbuf_bop(PLStream *pls)
{
dbug_enter("plbuf_bop");
plbuf_tidy(pls);
#ifdef BUFFERED_FILE
pls->plbufFile = tmpfile();
if (pls->plbufFile == NULL)
plexit("plbuf_bop: Error opening plot data storage file.");
#else
/* Need a better place to initialize this value */
pls->plbuf_buffer_grow = 128 * 1024;
if (pls->plbuf_buffer == NULL) {
/* We have not allocated a buffer, so do it now */
if ((pls->plbuf_buffer = malloc(pls->plbuf_buffer_grow)) == NULL)
plexit("plbuf_bop: Error allocating plot buffer.");
pls->plbuf_buffer_size = pls->plbuf_buffer_grow;
pls->plbuf_top = 0;
pls->plbuf_readpos = 0;
} else {
/* Buffer is allocated, move the top to the beginning */
pls->plbuf_top = 0;
}
#endif
wr_command(pls, (U_CHAR) BOP);
plbuf_state(pls, PLSTATE_COLOR0);
plbuf_state(pls, PLSTATE_WIDTH);
}
/*--------------------------------------------------------------------------*\
* plbuf_tidy()
*
* Close graphics file
\*--------------------------------------------------------------------------*/
void
plbuf_tidy(PLStream *pls)
{
dbug_enter("plbuf_tidy");
#ifdef BUFFERED_FILE
if (pls->plbufFile == NULL)
return;
fclose(pls->plbufFile)
pls->plbufFile = NULL;
#else
(void) pls; /* pmr: make it used */
#endif
}
/*--------------------------------------------------------------------------*\
* plbuf_state()
*
* Handle change in PLStream state (color, pen width, fill attribute, etc).
\*--------------------------------------------------------------------------*/
void
plbuf_state(PLStream *pls, PLINT op)
{
dbug_enter("plbuf_state");
wr_command(pls, (U_CHAR) CHANGE_STATE);
wr_command(pls, (U_CHAR) op);
switch (op) {
case PLSTATE_WIDTH:
wr_data(pls, &(pls->width), sizeof(pls->width));
break;
case PLSTATE_COLOR0:
wr_data(pls, &(pls->icol0), sizeof(pls->icol0));
if (pls->icol0 == PL_RGB_COLOR) {
wr_data(pls, &(pls->curcolor.r), sizeof(pls->curcolor.r));
wr_data(pls, &(pls->curcolor.g), sizeof(pls->curcolor.g));
wr_data(pls, &(pls->curcolor.b), sizeof(pls->curcolor.b));
}
break;
case PLSTATE_COLOR1:
wr_data(pls, &(pls->icol1), sizeof(pls->icol1));
break;
case PLSTATE_FILL:
wr_data(pls, &(pls->patt), sizeof(pls->patt));
break;
}
}
/*--------------------------------------------------------------------------*\
* plbuf_image()
*
* write image described in points pls->dev_x[], pls->dev_y[], pls->dev_z[].
* pls->nptsX, pls->nptsY.
\*--------------------------------------------------------------------------*/
static void
plbuf_image(PLStream *pls, IMG_DT *img_dt)
{
PLINT npts = pls->dev_nptsX * pls->dev_nptsY;
dbug_enter("plbuf_image");
wr_data(pls, &pls->dev_nptsX, sizeof(PLINT));
wr_data(pls, &pls->dev_nptsY, sizeof(PLINT));
wr_data(pls, &img_dt->xmin, sizeof(PLFLT));
wr_data(pls, &img_dt->ymin, sizeof(PLFLT));
wr_data(pls, &img_dt->dx, sizeof(PLFLT));
wr_data(pls, &img_dt->dy, sizeof(PLFLT));
wr_data(pls, &pls->dev_zmin, sizeof(short));
wr_data(pls, &pls->dev_zmax, sizeof(short));
wr_data(pls, pls->dev_ix, sizeof(short) * npts);
wr_data(pls, pls->dev_iy, sizeof(short) * npts);
wr_data(pls, pls->dev_z, sizeof(unsigned short) * (pls->dev_nptsX-1)*(pls->dev_nptsY-1));
}
/*--------------------------------------------------------------------------*\
* plbuf_text()
*
* Handle text call.
\*--------------------------------------------------------------------------*/
static void
plbuf_text(PLStream *pls, EscText *text)
{
PLUNICODE fci;
dbug_enter("plbuf_text");
/* Retrieve the font characterization integer */
plgfci(&fci);
/* Write the text information */
wr_data(pls, &fci, sizeof(PLUNICODE));
wr_data(pls, &pls->chrht, sizeof(PLFLT));
wr_data(pls, &pls->diorot, sizeof(PLFLT));
wr_data(pls, &pls->clpxmi, sizeof(PLFLT));
wr_data(pls, &pls->clpxma, sizeof(PLFLT));
wr_data(pls, &pls->clpymi, sizeof(PLFLT));
wr_data(pls, &pls->clpyma, sizeof(PLFLT));
wr_data(pls, &text->base, sizeof(PLINT));
wr_data(pls, &text->just, sizeof(PLFLT));
wr_data(pls, text->xform, sizeof(PLFLT) * 4);
wr_data(pls, &text->x, sizeof(PLINT));
wr_data(pls, &text->y, sizeof(PLINT));
wr_data(pls, &text->refx, sizeof(PLINT));
wr_data(pls, &text->refy, sizeof(PLINT));
wr_data(pls, &text->unicode_array_len, sizeof(PLINT));
if(text->unicode_array_len)
wr_data(pls, text->unicode_array, sizeof(PLUNICODE) * text->unicode_array_len);
}
/*--------------------------------------------------------------------------*\
* plbuf_esc()
*
* Escape function. Note that any data written must be in device
* independent form to maintain the transportability of the metafile.
*
* Functions:
*
* PLESC_FILL Fill polygon
* PLESC_SWIN Set plot window parameters
* PLESC_IMAGE Draw image
* PLESC_HAS_TEXT Draw PostScript text
\*--------------------------------------------------------------------------*/
void
plbuf_esc(PLStream *pls, PLINT op, void *ptr)
{
dbug_enter("plbuf_esc");
wr_command(pls, (U_CHAR) ESCAPE);
wr_command(pls, (U_CHAR) op);
switch (op) {
case PLESC_FILL:
plbuf_fill(pls);
break;
case PLESC_SWIN:
plbuf_swin(pls, (PLWindow *) ptr);
break;
case PLESC_IMAGE:
plbuf_image(pls, (IMG_DT *) ptr);
break;
case PLESC_HAS_TEXT:
if(ptr!=NULL) /* Check required by GCW driver, please don't remove */
plbuf_text(pls, (EscText *) ptr);
break;
}
}
/*--------------------------------------------------------------------------*\
* plbuf_fill()
*
* Fill polygon described in points pls->dev_x[] and pls->dev_y[].
\*--------------------------------------------------------------------------*/
static void
plbuf_fill(PLStream *pls)
{
dbug_enter("plbuf_fill");
wr_data(pls, &pls->dev_npts, sizeof(PLINT));
wr_data(pls, pls->dev_x, sizeof(short) * pls->dev_npts);
wr_data(pls, pls->dev_y, sizeof(short) * pls->dev_npts);
}
/*--------------------------------------------------------------------------*\
* plbuf_swin()
*
* Set up plot window parameters.
\*--------------------------------------------------------------------------*/
static void
plbuf_swin(PLStream *pls, PLWindow *plwin)
{
wr_data(pls, &plwin->dxmi, sizeof(PLFLT));
wr_data(pls, &plwin->dxma, sizeof(PLFLT));
wr_data(pls, &plwin->dymi, sizeof(PLFLT));
wr_data(pls, &plwin->dyma, sizeof(PLFLT));
wr_data(pls, &plwin->wxmi, sizeof(PLFLT));
wr_data(pls, &plwin->wxma, sizeof(PLFLT));
wr_data(pls, &plwin->wymi, sizeof(PLFLT));
wr_data(pls, &plwin->wyma, sizeof(PLFLT));
}
/*--------------------------------------------------------------------------*\
* Routines to read from & process the plot buffer.
\*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*\
* rdbuf_init()
*
* Initialize device.
\*--------------------------------------------------------------------------*/
static void
rdbuf_init(PLStream *pls)
{
(void) pls; /* pmr: make it used */
dbug_enter("rdbuf_init");
}
/*--------------------------------------------------------------------------*\
* rdbuf_line()
*
* Draw a line in the current color from (x1,y1) to (x2,y2).
\*--------------------------------------------------------------------------*/
static void
rdbuf_line(PLStream *pls)
{
short xpl[2], ypl[2];
PLINT npts = 2;
dbug_enter("rdbuf_line");
rd_data(pls, xpl, sizeof(short) * npts);
rd_data(pls, ypl, sizeof(short) * npts);
plP_line(xpl, ypl);
}
/*--------------------------------------------------------------------------*\
* rdbuf_polyline()
*
* Draw a polyline in the current color.
\*--------------------------------------------------------------------------*/
static void
rdbuf_polyline(PLStream *pls)
{
short xpl[PL_MAXPOLY], ypl[PL_MAXPOLY];
PLINT npts;
dbug_enter("rdbuf_polyline");
rd_data(pls, &npts, sizeof(PLINT));
rd_data(pls, xpl, sizeof(short) * npts);
rd_data(pls, ypl, sizeof(short) * npts);
plP_polyline(xpl, ypl, npts);
}
/*--------------------------------------------------------------------------*\
* rdbuf_eop()
*
* End of page.
\*--------------------------------------------------------------------------*/
static void
rdbuf_eop(PLStream *pls)
{
(void) pls; /* pmr: make it used */
dbug_enter("rdbuf_eop");
}
/*--------------------------------------------------------------------------*\
* rdbuf_bop()
*
* Set up for the next page.
\*--------------------------------------------------------------------------*/
static void
rdbuf_bop(PLStream *pls)
{
dbug_enter("rdbuf_bop");
pls->nplwin = 0;
}
/*--------------------------------------------------------------------------*\
* rdbuf_state()
*
* Handle change in PLStream state (color, pen width, fill attribute, etc).
\*--------------------------------------------------------------------------*/
static void
rdbuf_state(PLStream *pls)
{
U_CHAR op;
dbug_enter("rdbuf_state");
rd_data(pls, &op, sizeof(U_CHAR));
switch (op) {
case PLSTATE_WIDTH:{
U_CHAR width;
rd_data(pls, &width, sizeof(U_CHAR));
pls->width = width;
plP_state(PLSTATE_WIDTH);
break;
}
case PLSTATE_COLOR0:{
short icol0;
U_CHAR r, g, b;
rd_data(pls, &icol0, sizeof(short));
if (icol0 == PL_RGB_COLOR) {
rd_data(pls, &r, sizeof(U_CHAR));
rd_data(pls, &g, sizeof(U_CHAR));
rd_data(pls, &b, sizeof(U_CHAR));
}
else {
if ((int) icol0 >= pls->ncol0) {
char buffer[256];
sprintf(buffer, "rdbuf_state: Invalid color map entry: %d", (int) icol0);
plabort(buffer);
return;
}
r = pls->cmap0[icol0].r;
g = pls->cmap0[icol0].g;
b = pls->cmap0[icol0].b;
}
pls->icol0 = icol0;
pls->curcolor.r = r;
pls->curcolor.g = g;
pls->curcolor.b = b;
plP_state(PLSTATE_COLOR0);
break;
}
case PLSTATE_COLOR1: {
short icol1;
rd_data(pls, &icol1, sizeof(short));
pls->icol1 = icol1;
pls->curcolor.r = pls->cmap1[icol1].r;
pls->curcolor.g = pls->cmap1[icol1].g;
pls->curcolor.b = pls->cmap1[icol1].b;
plP_state(PLSTATE_COLOR1);
break;
}
case PLSTATE_FILL: {
signed char patt;
rd_data(pls, &patt, sizeof(signed char));
pls->patt = patt;
plP_state(PLSTATE_FILL);
break;
}
}
}
/*--------------------------------------------------------------------------*\
* rdbuf_esc()
*
* Escape function.
* Must fill data structure with whatever data that was written,
* then call escape function.
*
* Note: it is best to only call the escape function for op-codes that
* are known to be supported.
*
* Functions:
*
* PLESC_FILL Fill polygon
* PLESC_SWIN Set plot window parameters
* PLESC_IMAGE Draw image
* PLESC_HAS_TEXT Draw PostScript text
\*--------------------------------------------------------------------------*/
static void
rdbuf_image(PLStream *pls);
static void
rdbuf_text(PLStream *pls);
static void
rdbuf_esc(PLStream *pls)
{
U_CHAR op;
dbug_enter("rdbuf_esc");
rd_data(pls, &op, sizeof(U_CHAR));
switch (op) {
case PLESC_FILL:
rdbuf_fill(pls);
break;
case PLESC_SWIN:
rdbuf_swin(pls);
break;
case PLESC_IMAGE:
rdbuf_image(pls);
break;
case PLESC_HAS_TEXT:
rdbuf_text(pls);
break;
}
}
/*--------------------------------------------------------------------------*\
* rdbuf_fill()
*
* Fill polygon described by input points.
\*--------------------------------------------------------------------------*/
static void
rdbuf_fill(PLStream *pls)
{
short xpl[PL_MAXPOLY], ypl[PL_MAXPOLY];
PLINT npts;
dbug_enter("rdbuf_fill");
rd_data(pls, &npts, sizeof(PLINT));
rd_data(pls, xpl, sizeof(short) * npts);
rd_data(pls, ypl, sizeof(short) * npts);
plP_fill(xpl, ypl, npts);
}
/*--------------------------------------------------------------------------*\
* rdbuf_image()
*
* .
\*--------------------------------------------------------------------------*/
static void
rdbuf_image(PLStream *pls)
{
short *dev_ix, *dev_iy;
unsigned short *dev_z, dev_zmin, dev_zmax;
PLINT nptsX,nptsY, npts;
PLFLT xmin, ymin, dx, dy;
dbug_enter("rdbuf_image");
rd_data(pls, &nptsX, sizeof(PLINT));
rd_data(pls, &nptsY, sizeof(PLINT));
npts = nptsX*nptsY;
rd_data(pls, &xmin, sizeof(PLFLT));
rd_data(pls, &ymin, sizeof(PLFLT));
rd_data(pls, &dx, sizeof(PLFLT));
rd_data(pls, &dy, sizeof(PLFLT));
rd_data(pls, &dev_zmin, sizeof(short));
rd_data(pls, &dev_zmax, sizeof(short));
/* NOTE: Even though for memory buffered version all the data is in memory,
* we still allocate and copy the data because I think that method works
* better in a multithreaded environment. I could be wrong.
*/
dev_ix=(short *)malloc(npts*sizeof(short));
dev_iy=(short *)malloc(npts*sizeof(short));
dev_z=(unsigned short *)malloc((nptsX-1)*(nptsY-1)*sizeof(unsigned short));
rd_data(pls, dev_ix, sizeof(short) * npts);
rd_data(pls, dev_iy, sizeof(short) * npts);
rd_data(pls, dev_z, sizeof(unsigned short) * (nptsX-1)*(nptsY-1));
plP_image(dev_ix, dev_iy, dev_z, nptsX, nptsY, xmin, ymin, dx, dy, dev_zmin, dev_zmax);
free(dev_ix);
free(dev_iy);
free(dev_z);
}
/*--------------------------------------------------------------------------*\
* rdbuf_swin()
*
* Set up plot window parameters.
\*--------------------------------------------------------------------------*/
static void
rdbuf_swin(PLStream *pls)
{
PLWindow plwin;
rd_data(pls, &plwin.dxmi, sizeof(PLFLT));
rd_data(pls, &plwin.dxma, sizeof(PLFLT));
rd_data(pls, &plwin.dymi, sizeof(PLFLT));
rd_data(pls, &plwin.dyma, sizeof(PLFLT));
rd_data(pls, &plwin.wxmi, sizeof(PLFLT));
rd_data(pls, &plwin.wxma, sizeof(PLFLT));
rd_data(pls, &plwin.wymi, sizeof(PLFLT));
rd_data(pls, &plwin.wyma, sizeof(PLFLT));
plP_swin(&plwin);
}
/*--------------------------------------------------------------------------*\
* rdbuf_text()
*
* Draw PostScript text.
\*--------------------------------------------------------------------------*/
static void
rdbuf_text(PLStream *pls)
{
PLUNICODE(fci);
EscText text;
PLFLT xform[4];
PLUNICODE* unicode;
text.xform = xform;
/* Read in the data */
rd_data(pls, &fci, sizeof(PLUNICODE));
rd_data(pls, &pls->chrht, sizeof(PLFLT));
rd_data(pls, &pls->diorot, sizeof(PLFLT));
rd_data(pls, &pls->clpxmi, sizeof(PLFLT));
rd_data(pls, &pls->clpxma, sizeof(PLFLT));
rd_data(pls, &pls->clpymi, sizeof(PLFLT));
rd_data(pls, &pls->clpyma, sizeof(PLFLT));
rd_data(pls, &text.base, sizeof(PLINT));
rd_data(pls, &text.just, sizeof(PLFLT));
rd_data(pls, text.xform, sizeof(PLFLT) * 4);
rd_data(pls, &text.x, sizeof(PLINT));
rd_data(pls, &text.y, sizeof(PLINT));
rd_data(pls, &text.refx, sizeof(PLINT));
rd_data(pls, &text.refy, sizeof(PLINT));
rd_data(pls, &text.unicode_array_len, sizeof(PLINT));
if(text.unicode_array_len) {
if((unicode=(PLUNICODE *)malloc(text.unicode_array_len*sizeof(PLUNICODE)))
== NULL)
plexit("rdbuf_text: Insufficient memory");
rd_data(pls, unicode, sizeof(PLUNICODE) * text.unicode_array_len);
text.unicode_array = unicode;
}
else text.unicode_array = NULL;
/* Make the call for unicode devices */
if(pls->dev_unicode) {
plsfci(fci);
plP_esc(PLESC_HAS_TEXT,&text);
}
}
/*--------------------------------------------------------------------------*\
* plRemakePlot()
*
* Rebuilds plot from plot buffer, usually in response to a window
* resize or exposure event.
\*--------------------------------------------------------------------------*/
void
plRemakePlot(PLStream *pls)
{
U_CHAR c;
int plbuf_status;
PLStream *save_pls;
dbug_enter("plRemakePlot");
/* Change the status of the flags before checking for a buffer.
* Actually, more thought is needed if we want to support multithreaded
* code correctly, specifically the case where two threads are using
* the same plot stream (e.g. one thread is drawing the plot and another
* thread is processing window manager messages).
*/
plbuf_status = pls->plbuf_write;
pls->plbuf_write = FALSE;
pls->plbuf_read = TRUE;
#ifdef BUFFERED_FILE
if (pls->plbufFile) {
rewind(pls->plbufFile);
#else
if (pls->plbuf_buffer) {
pls->plbuf_readpos = 0;
#endif
/* Need to change where plsc points to before processing the commands.
* If we have multiple plot streams, this will prevent the commands from
* going to the wrong plot stream.
*/
save_pls = plsc;
plsc = pls;
while (rd_command(pls, &c)) {
plbuf_control(pls, c);
}
plsc = save_pls;
}
pls->plbuf_read = FALSE;
pls->plbuf_write = plbuf_status;
}
/*--------------------------------------------------------------------------*\
* plbuf_control()
*
* Processes commands read from the plot buffer.
\*--------------------------------------------------------------------------*/
static void
plbuf_control(PLStream *pls, U_CHAR c)
{
static U_CHAR c_old = 0;
dbug_enter("plbuf_control");
switch ((int) c) {
case INITIALIZE:
rdbuf_init(pls);
break;
case EOP:
rdbuf_eop(pls);
break;
case BOP:
rdbuf_bop(pls);
break;
case CHANGE_STATE:
rdbuf_state(pls);
break;
case LINE:
rdbuf_line(pls);
break;
case POLYLINE:
rdbuf_polyline(pls);
break;
case ESCAPE:
rdbuf_esc(pls);
break;
default:
pldebug("plbuf_control", "Unrecognized command %d, previous %d\n", c, c_old);
}
c_old = c;
}
/*--------------------------------------------------------------------------*\
* rd_command()
*
* Read & return the next command
\*--------------------------------------------------------------------------*/
static int
rd_command(PLStream *pls, U_CHAR *p_c)
{
int count;
#ifdef BUFFERED_FILE
count = fread(p_c, sizeof(U_CHAR), 1, pls->plbufFile);
#else
if (pls->plbuf_readpos < pls->plbuf_top) {
*p_c = *(U_CHAR *)((U_CHAR *)pls->plbuf_buffer + pls->plbuf_readpos);
pls->plbuf_readpos += sizeof(U_CHAR);
count = sizeof(U_CHAR);
} else {
count = 0;
}
#endif
return(count);
}
/*--------------------------------------------------------------------------*\
* rd_data()
*
* Read the data associated with the command
\*--------------------------------------------------------------------------*/
static void
rd_data(PLStream *pls, void *buf, size_t buf_size)
{
#ifdef BUFFERED_FILE
plio_fread(buf, buf_size, 1, pls->plbufFile);
#else
/* If U_CHAR is not the same size as what memcpy() expects (typically 1 byte)
* then this code will have problems. A better approach might be to use
* uint8_t from <stdint.h> but I do not know how portable that approach is
*/
memcpy(buf, (U_CHAR *)pls->plbuf_buffer + pls->plbuf_readpos, buf_size);
pls->plbuf_readpos += buf_size;
#endif
}
/*--------------------------------------------------------------------------*\
* wr_command()
*
* Write the next command
\*--------------------------------------------------------------------------*/
static void
wr_command(PLStream *pls, U_CHAR c)
{
#ifdef BUFFERED_FILE
plio_fwrite(&c1, sizeof(U_CHAR), 1, pls->plbufFile);
#else
if ((pls->plbuf_top + sizeof(U_CHAR)) >= pls->plbuf_buffer_size) {
/* Not enough space, need to grow the buffer */
pls->plbuf_buffer_size += pls->plbuf_buffer_grow;
if (pls->verbose)
printf("Growing buffer to %d KB\n", (int)(pls->plbuf_buffer_size / 1024));
if ((pls->plbuf_buffer = realloc(pls->plbuf_buffer, pls->plbuf_buffer_size)) == NULL)
plexit("plbuf wr_data: Plot buffer grow failed");
}
*(U_CHAR *)((U_CHAR *)pls->plbuf_buffer + pls->plbuf_top) = c;
pls->plbuf_top += sizeof(U_CHAR);
#endif
}
/*--------------------------------------------------------------------------*\
* wr_data()
*
* Write the data associated with a command
\*--------------------------------------------------------------------------*/
static void
wr_data(PLStream *pls, void *buf, size_t buf_size)
{
#ifdef BUFFERED_FILE
plio_fwrite(buf, buf_size, 1, pls->plbufFile);
#else
if ((pls->plbuf_top + buf_size) >= pls->plbuf_buffer_size) {
/* Not enough space, need to grow the buffer */
/* Must make sure the increase is enough for this data */
pls->plbuf_buffer_size += pls->plbuf_buffer_grow *
((pls->plbuf_top + buf_size - pls->plbuf_buffer_size) /
pls->plbuf_buffer_grow + 1);
while (pls->plbuf_top + buf_size >= pls->plbuf_buffer_size);
if ((pls->plbuf_buffer = realloc(pls->plbuf_buffer, pls->plbuf_buffer_size)) == NULL)
plexit("plbuf wr_data: Plot buffer grow failed");
}
/* If U_CHAR is not the same size as what memcpy() expects (typically 1 byte)
* then this code will have problems. A better approach might be to use
* uint8_t from <stdint.h> but I do not know how portable that approach is
*/
memcpy((U_CHAR *)pls->plbuf_buffer + pls->plbuf_top, buf, buf_size);
pls->plbuf_top += buf_size;
#endif
}
/* plbuf_save(state)
*
* Saves the current state of the plot into a save buffer.
* This code was originally in gcw.c and gcw-lib.c. The original
* code used a temporary file for the plot buffer and memory
* to perserve colormaps. That method does not offer a clean
* break between using memory buffers and file buffers. This
* function preserves the same functionality by returning a data
* structure that saves the plot buffer and colormaps seperately.
*
* The caller passes an existing save buffer for reuse or NULL
* to force the allocation of a new buffer. Since one malloc()
* is used for everything, the entire save buffer can be freed
* with one free() call.
*
*/
struct _color_map {
PLColor *cmap;
PLINT icol;
PLINT ncol;
};
struct _state {
size_t size; /* Size of the save buffer */
int valid; /* Flag to indicate a valid save state */
#ifdef BUFFERED_FILE
FILE *plbufFile;
#else
char Padding[4];
void *plbuf_buffer;
size_t plbuf_buffer_size;
size_t plbuf_top;
size_t plbuf_readpos;
#endif
struct _color_map *color_map;
};
void * plbuf_save(PLStream *pls, void *state)
{
size_t save_size;
struct _state *plot_state = (struct _state *)state;
int i;
U_CHAR *buf; /* Assume that this is byte-sized */
if(pls->plbuf_write) {
pls->plbuf_write = FALSE;
pls->plbuf_read = TRUE;
/* Determine the size of the buffer required to save everything. We
* assume that there are only two colormaps, but have written the code
* that more than two can be handled with minimal changes.
*/
save_size = sizeof(struct _state)
+ 2 * sizeof(struct _color_map)
+ pls->ncol0 * sizeof(PLColor)
+ pls->ncol1 * sizeof(PLColor);
#ifndef BUFFERED_FILE
/* Only copy as much of the plot buffer that is being used */
save_size += pls->plbuf_top;
#endif
/* If a buffer exists, determine if we need to resize it */
if( state != NULL ) {
/* We have a save buffer, is it smaller than the current size requirement? */
if(plot_state->size < save_size) {
/* Yes, reallocate a larger one */
if((plot_state = (struct _state *)realloc(state, save_size)) == NULL) {
/* NOTE: If realloc fails, then plot_state ill be NULL.
* This will leave the original buffer untouched, thus we
* mark it as invalid and return it back to the caller.
*/
plwarn("plbuf: Unable to reallocate sufficient memory to save state");
plot_state->valid = 0;
return state;
}
plot_state->size = save_size;
}
} else {
/* A buffer does not exist, so we need to allocate one */
if((plot_state = (struct _state *)malloc(save_size)) == NULL) {
plwarn("plbuf: Unable to allocate sufficient memory to save state");
return NULL;
}
plot_state->size = save_size;
#ifdef BUFFERED_FILE
/* Make sure the FILE pointer is NULL in order to preven bad things from happening... */
plot_state->plbufFile = NULL;
#endif
}
/* At this point we have an appropriately sized save buffer.
* We need to invalidate the state of the save buffer, since it
* will not be valid until after everything is copied. We use
* this approach vice freeing the memory and returning a NULL pointer
* in order to prevent allocating and freeing memory needlessly.
*/
plot_state->valid = 0;
/* Point buf to the space after the struct _state */
buf = (U_CHAR *)(plot_state + 1);
#ifdef BUFFERED_FILE
/* Remove the old tempfile, if it exists */
if( plot_state->plbufFile != NULL ) {
fclose(plot_state->plbufFile);
}
/* Copy the plot buffer to a tempfile */
if((plot_state->plbufFile = tmpfile()) == NULL) {
/* Throw a warning since this might be a permissions problem
* and we may not want to force an exit
*/
plwarn("plbuf: Unable to open temporary file to save state");
return (void *)plot_state;
} else {
U_CHAR tmp;
rewind(pls->plbufFile);
while(count = fread(&tmp, sizeof(U_CHAR), 1, pls->plbufFile)) {
if(fwrite(&tmp, sizeof(U_CHAR), 1, plot_state->plbufFile)!=count) {
/* Throw a warning since this might be a permissions problem
* and we may not want to force an exit
*/
plwarn("plbuf: Unable to write to temporary file");
fclose(plot_state->plbufFile);
plot_state->plbufFile = NULL;
return (void *)plot_state;
}
}
}
#else
/* Again, note, that we only copy the portion of the plot buffer that is being used */
plot_state->plbuf_buffer_size = pls->plbuf_top;
plot_state->plbuf_top = pls->plbuf_top;
plot_state->plbuf_readpos = 0;
/* Create a pointer that points in the space we allocated after struct _state */
plot_state->plbuf_buffer = (void *)buf;
buf += pls->plbuf_top;
/* Copy the plot buffer to our new buffer. Again, I must stress, that we only
* are copying the portion of the plot buffer that is being used
*/
if(memcpy(plot_state->plbuf_buffer, pls->plbuf_buffer, pls->plbuf_top ) == NULL) {
/* This should never be NULL */
plwarn("plbuf: Got a NULL in memcpy!");
return (void *)plot_state;
}
#endif
pls->plbuf_write = TRUE;
pls->plbuf_read = FALSE;
/* Save the colormaps. First create a pointer that points in the space we allocated
* after the plot buffer */
plot_state->color_map = (struct _color_map *)buf;
buf += sizeof(struct _color_map) * 2;
/* Then we need to make space for the colormaps themselves */
plot_state->color_map[0].cmap = (PLColor *)buf;
buf += sizeof(PLColor) * pls->ncol0;
plot_state->color_map[1].cmap = (PLColor *)buf;
buf += sizeof(PLColor) * pls->ncol1;
/* Save cmap 0 */
plot_state->color_map[0].icol = pls->icol0;
plot_state->color_map[0].ncol = pls->ncol0;
for (i = 0; i < pls->ncol0; i++) {
pl_cpcolor(&(plot_state->color_map[0].cmap[i]),
&pls->cmap0[i]);
}
/* Save cmap 1 */
plot_state->color_map[1].icol = pls->icol1;
plot_state->color_map[1].ncol = pls->ncol1;
for (i = 0; i < pls->ncol1; i++) {
pl_cpcolor(&(plot_state->color_map[1].cmap[i]),
&pls->cmap1[i]);
}
plot_state->valid = 1;
return (void *)plot_state;
}
return NULL;
}
/* plbuf_restore(PLStream *, state)
*
* Restores the passed state
*/
void plbuf_restore(PLStream *pls, void *state);
void * plbuf_switch(PLStream *pls, void *state);
void plbuf_restore(PLStream *pls, void *state)
{
struct _state *new_state = (struct _state *)state; /* pmr: should use state */
#ifdef BUFFERED_FILE
pls->plbufFile = new_state->save_file;
#else
pls->plbuf_buffer = new_state->plbuf_buffer;
pls->plbuf_buffer_size = new_state->plbuf_buffer_size;
pls->plbuf_top = new_state->plbuf_top;
pls->plbuf_readpos = new_state->plbuf_readpos;
#endif
/* cmap 0 */
pls->cmap0 = new_state->color_map[0].cmap;
pls->icol0 = new_state->color_map[0].icol;
pls->ncol0 = new_state->color_map[0].ncol;
/* cmap 1 */
pls->cmap1 = new_state->color_map[1].cmap;
pls->icol1 = new_state->color_map[1].icol;
pls->ncol1 = new_state->color_map[1].ncol;
}
/* plbuf_switch(PLStream *, state)
*
* Makes the passed state the current one. Preserves the previous state
* by returning a save buffer.
*
* NOTE: The current implementation can cause a memory leak under the
* following scenario:
* 1) plbuf_save() is called
* 2) plbuf_switch() is called
* 3) Commands are called which cause the plot buffer to grow
* 4) plbuf_swtich() is called
*/
void * plbuf_switch(PLStream *pls, void *state)
{
struct _state *new_state = (struct _state *)state;
struct _state *prev_state;
size_t save_size;
/* No saved state was passed, return a NULL--we hope the caller
* is smart enough to notice
*/
if(state == NULL) return NULL;
if(! new_state->valid) {
plwarn("plbuf: Attempting to switch to an invalid saved state");
return NULL;
}
save_size = sizeof(struct _state)
+ 2 * sizeof(struct _color_map);
if((prev_state = (struct _state *)malloc(save_size)) == NULL) {
plwarn("plbuf: Unable to allocate memory to save state");
return NULL;
}
/* Set some housekeeping variables */
prev_state->size = save_size;
prev_state->valid = 1;
/* Preserve the existing state */
#ifdef BUFFERED_FILE
prev_state->plbufFile = pls->plbufFile;
#else
prev_state->plbuf_buffer = pls->plbuf_buffer;
prev_state->plbuf_buffer_size = pls->plbuf_buffer_size;
prev_state->plbuf_top = pls->plbuf_top;
prev_state->plbuf_readpos = pls->plbuf_readpos;
#endif
/* cmap 0 */
prev_state->color_map[0].cmap = pls->cmap0;
prev_state->color_map[0].icol = pls->icol0;
prev_state->color_map[0].ncol = pls->ncol0;
/* cmap 1 */
prev_state->color_map[1].cmap = pls->cmap1;
prev_state->color_map[1].icol = pls->icol1;
prev_state->color_map[1].ncol = pls->ncol1;
plbuf_restore(pls, new_state);
return (void *) prev_state;
}
|