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
|
/*
This is the PNG (Portable Network Graphics) driver for PGPLOT.
For more information on the PNG standard, and to get the
necessary libraries, see http://www.cdrom.com/pub/png/
This driver is intended to be used in the same ways as one would
use PGPLOT's GIF driver, and as such uses many of the same conventions.
The default plotting dimensions are 850x680, and can be
manipulated via the PGPLOT_PNG_WIDTH and PGPLOT_PNG_HEIGHT
environment variables.
The driver can be opened as many times as the caller likes (i.e., more
than one device number available). Associated with each device
is a single filename, and after each page advance the filename is
modified to have a trailing "_X", where "X" is the current
page number. This does not apply to the first page output,
however.
For compilation, both libpng and zlib must be installed. These
libraries are Free Software, and can be obtained at the following
URLs:
libpng: http://www.cdrom.com/pub/png/
zlib: http://www.cdrom.com/pub/infozip/zlib/
March, 1999
Pete Ratzlaff <pratzlaff@cfa.harvard.edu>
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <png.h>
#ifdef VMS
#include <descrip.h>
#include <ssdef.h>
#endif
/* make the driver callable from FORTRAN */
#ifdef PG_PPU
#define PNDRIV pndriv_
#else
#define PNDRIV pndriv
#endif
/*
Flags passed by grexec(). We may need others in the future. Set in
drivers.list.
*/
#define TRANS_OFF 1
#define TRANS_ON 2
/* miscellaneous constants */
#define DEFAULT_WIDTH 850
#define DEFAULT_HEIGHT 680
#define NCOLORS 256
#define DEVICE_CAPABILITIES "HNNNNRPNYN"
#define DEFAULT_FILENAME "pgplot.png"
#define boolean unsigned char
#define true 1
#define false 0
#define PNG_IDENT_BASIC "PGPLOT /png" /* used in warning messages */
#define PNG_IDENT_TRANS "PGPLOT /tpng" /* used in warning messages */
static char *png_ident; /* should be set each time pndriv() is entered */
/* use for opcode = 1 */
#define DEVICE_NAME_BASIC "PNG (Portable Network Graphics file)"
#define DEVICE_NAME_TRANS "TPNG (Portable Network Graphics file - transparent background)"
/* simple way of specifiying the current device structure pointer */
#define ACTIVE_DEVICE (all_devices.devices[all_devices.active])
typedef unsigned char ColorComponent; /* red, green, or blue component of a colortable entry */
typedef unsigned char ColorIndex; /* index into a color table */
/* taken from the GIF driver */
static ColorComponent base_colors[] = {
0, 0, 0,
255, 255, 255,
255, 0, 0,
0, 255, 0,
0, 0, 255,
0, 255, 255,
255, 0, 255,
255, 255, 0,
255, 128, 0,
128, 255, 0,
0, 255, 128,
0, 128, 255,
128, 0, 255,
255, 0, 128,
85, 85, 85,
170, 170, 170,
};
/* each new device initially copies its colortable from here */
static ColorComponent default_colortable[NCOLORS * 3];
/* data for a single open device */
typedef struct _DeviceData DeviceData, *DeviceDataPtr;
struct _DeviceData {
int w, h;
long npix; /* w*h */
boolean trans; /* transparent background flag */
boolean error; /* if true, we can plot no more on this device */
ColorIndex *pixmap; /* image consisting of array of color indicies */
int npages; /* running total of plot pages */
char *filename;
ColorComponent ctable[NCOLORS * 3];
ColorIndex cindex; /* current plotting color index */
int devnum; /* this device's identifier */
};
/* global data holding all devices */
typedef struct _Devices Devices;
struct _Devices {
DeviceDataPtr *devices;
int nallocated;
int active;
};
static Devices all_devices;
/* number of DeviceData structures to allocate at a time */
#define devices_ALLOC_INCREMENT 128
/* copy the default colortable to a newly-opened device's ctable entry */
static void initialize_device_ctable(DeviceData *dev) {
memcpy(dev->ctable, default_colortable, 3 * NCOLORS * sizeof(ColorComponent));
}
/* use to set the RGB components of a colortable entry */
static void set_color_rep(DeviceData *dev, int index, ColorComponent r, ColorComponent g, ColorComponent b) {
dev->ctable[index*3+0] = r;
dev->ctable[index*3+1] = g;
dev->ctable[index*3+2] = b;
}
static void get_color_rep(DeviceData *dev, int index, ColorComponent *r, ColorComponent *g, ColorComponent *b) {
*r = dev->ctable[index*3+0];
*g = dev->ctable[index*3+1];
*b = dev->ctable[index*3+2];
}
/* If one were to port this driver to a new image format, then this is the
* only routine that would need to be rewritten.
*/
static void write_image_file(DeviceData *dev) {
int i;
char *filename;
FILE *fp;
png_structp png_ptr;
png_infop info_ptr;
png_color colors[NCOLORS];
ColorComponent r, g, b;
if (dev->error == true)
return;
/* fill the color table for libpng */
for (i=0; i<NCOLORS; i++) {
get_color_rep(dev, i, &r, &g, &b);
colors[i].red = r;
colors[i].green = g;
colors[i].blue = b;
}
/*
For multiple pages, we make a new file for each page.
We name them sequentially, based on the original filename
specified.
*/
#define EXTRA_CHARS 16 /* allow 10^15 image files per device */
filename = malloc(strlen(dev->filename)+EXTRA_CHARS);
if (!filename) {
fprintf(stderr,"%s: out of memory, plotting disabled\n", png_ident);
dev->error = true;
return;
}
strcpy(filename,dev->filename);
if (strcmp("-",filename) != 0 && dev->npages > 1) {
sprintf(filename,"%s_%d",dev->filename,dev->npages);
fprintf(stderr,"%s: writing new file as %s\n",png_ident,filename);
}
/* open the file */
if (strcmp("-",filename) != 0) {
if (! (fp = fopen(filename,"wb"))) {
fprintf(stderr,"%s: could not open file %s for writing, plotting disabled\n",png_ident,filename);
dev->error = true;
free(filename);
return;
}
} else {
fp = stdout;
}
png_ptr = png_create_write_struct(
PNG_LIBPNG_VER_STRING,
NULL,
NULL,
NULL
);
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
fprintf(stderr,"%s: error in libpng while writing file %s, plotting disabled\n", png_ident, filename);
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
dev->error = true;
if (fp != stdout)
fclose(fp);
free(filename);
return;
}
if (setjmp(png_jmpbuf(png_ptr))) { /* not really sure what I'm doing here... */
fprintf(stderr,"%s: error in libpng while writing file %s, plotting disabled\n", png_ident, filename);
png_destroy_write_struct(&png_ptr,&info_ptr);
dev->error = true;
if (fp != stdout)
fclose(fp);
free(filename);
return;
}
png_init_io(png_ptr, fp);
png_set_IHDR(png_ptr, info_ptr,
dev->w, dev->h, 8,
PNG_COLOR_TYPE_PALETTE,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT
);
png_set_PLTE(png_ptr, info_ptr, colors, NCOLORS);
/* text to go in PNG file */
{
png_text text[1];
text[0].key = "Software";
text[0].text = "PGPLOT Graphics Subroutine Library";
text[0].text_length = strlen(text[0].text);
text[0].compression = PNG_TEXT_COMPRESSION_NONE;
png_set_text(png_ptr, info_ptr, text, 1);
};
/* set bKGD chunk */
/* not sure I know what image readers will do with this, so it's commented out
{
png_color_16 background;
background.index = 0;
png_set_bKGD(png_ptr,info_ptr,&background);
}
*/
/* set transparency */
if (dev->trans == true) {
png_byte i = 0;
png_set_tRNS(png_ptr, info_ptr, &i, 1, NULL);
};
png_write_info(png_ptr, info_ptr);
for (i=dev->h-1; i>=0; i--)
png_write_row(png_ptr,&dev->pixmap[i * dev->w]);
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
if (fp != stdout)
fclose(fp);
free(filename);
}
static void swap_coords(int *x1, int *y1, int *x2, int *y2) {
int tmp;
tmp = *x1;
*x1 = *x2;
*x2 = tmp;
tmp = *y1;
*y1 = *y2;
*y2 = tmp;
}
static void fill_rectangle( DeviceData *dev, int x1, int y1, int x2, int y2, ColorIndex index ) {
int y;
int npix = (x2 - x1 + 1); /* number of pixels to fill on a single line */
if (dev->error == true)
return;
/* ensure coords are lower left and upper right */
if (x2<x1 && y2<y1)
swap_coords(&x1, &y1, &x2, &y2);
else if (y2 < y1) {
y = y1;
y1 = y2;
y2 = y;
}
else if (x2 < x1) {
y = x1;
x1 = x2;
x2 = y;
npix = (x2 - x1 + 1);
}
for (y=y1; y<=y2; y++)
memset( &dev->pixmap[ y * dev->w + x1 ], index, npix * sizeof(ColorIndex) );
}
/*
Begins a new plot page. Allocates memory for the pixmap, which
should be freed after the page has been written to file
*/
static void start_plot(DeviceData *dev, int w, int h) {
dev->w = w;
dev->h = h;
dev->npix = dev->w * dev->h;
dev->pixmap = malloc( dev->npix * sizeof(ColorIndex) );
if (!dev->pixmap) {
fprintf(stderr,"%s: out of memory, plotting disabled\n",png_ident);
dev->error = true;
}
dev->npages++;
fill_rectangle(dev, 0, 0, dev->w-1, dev->h-1, 0);
return;
}
/*
Called when page is done. Should write file and free
memory allocated in start_plot() for pixmap.
*/
static void end_plot(DeviceData *dev) {
if (dev->error == true)
return;
write_image_file(dev);
free(dev->pixmap);
}
static void make_device_active(float devnum) {
all_devices.active = devnum;
if (ACTIVE_DEVICE == NULL)
fprintf(stderr,"%s: one SIGSEGV coming right up! ACTIVE_DEVICE == NULL\n",png_ident);
}
/*
Used first time through PNDRIV()
*/
static void initialize_default_colortable(void) {
int i;
ColorComponent half_colors[] = { 128, 128, 128 };
memcpy(default_colortable, base_colors, 3 * 16 * sizeof(ColorComponent) );
for (i=16; i<NCOLORS; i++)
memcpy( default_colortable + (i * 3), half_colors, 3 * sizeof(ColorComponent) );
}
static void get_default_dimensions( int *width, int *height) {
char *width_string = NULL;
char *height_string = NULL;
if (! (width_string = getenv("PGPLOT_PNG_WIDTH")))
width_string = "DEFAULT_WIDTH";
if (! (height_string = getenv("PGPLOT_PNG_HEIGHT")))
height_string = "DEFAULT_HEIGHT";
*width = atoi(width_string);
*height = atoi(height_string);
if (!(*width > 0) || !(*height>0)) {
*width = DEFAULT_WIDTH;
*height = DEFAULT_HEIGHT;
}
return;
}
static void draw_line(DeviceData *dev, int x1, int y1, int x2, int y2, ColorIndex index) {
int x, y;
float rate;
if (dev->error == true)
return;
if (x1 == x2 || y1 == y2) { /* rate of change calculation below doesn't like this case */
fill_rectangle(dev,x1,y1,x2,y2,index);
return;
}
if (abs(y2-y1) > abs(x2-x1)) {
if (y1 > y2)
swap_coords(&x1, &y1, &x2, &y2);
rate = (x2 - x1) / (float)(y2 - y1);
for (y=y1; y<y2; y++) {
x = x1 + (y - y1) * rate;
dev->pixmap[ y * dev->w + x ] = index;
}
} else {
if (x1 > x2)
swap_coords(&x1, &y1, &x2, &y2);
rate = (float)(y2 - y1) / (float)(x2 - x1);
for (x=x1; x<x2; x++) {
y = y1 + (x - x1) * rate;
dev->pixmap[ y * dev->w + x ] = index;
}
}
}
/* set a single pixel's color */
static void fill_pixel(DeviceData *dev, int x, int y, ColorIndex index) {
if (dev->error == true)
return;
dev->pixmap[ y * dev->w + x ] = index;
}
static void initialize_all_devices(void) {
all_devices.devices = NULL;
all_devices.nallocated = 0;
all_devices.active = -1;
}
static void open_new_device(char *file, int length, float *id, float *err, int mode) {
DeviceDataPtr *tmp;
int i;
int devnum = -1;
/* find an empty slot */
for (i=0; i<all_devices.nallocated; i++) {
if (all_devices.devices[i] == NULL) { /* got one */
devnum = i;
break;
}
}
if (devnum < 0) /* didn't find one */
devnum = all_devices.nallocated;
*err = 0.0;
/* allocate more device slots, if necessary */
if (devnum >= all_devices.nallocated) {
tmp = realloc(all_devices.devices, sizeof(DeviceDataPtr) * (all_devices.nallocated + devices_ALLOC_INCREMENT));
/* didn't get the memory needed */
if (!tmp) {
fprintf(stderr,"%s: out of memory\n", png_ident);
return;
}
else {
all_devices.devices = tmp;
for (i=all_devices.nallocated; i<all_devices.nallocated+devices_ALLOC_INCREMENT; i++)
all_devices.devices[i] = NULL;
all_devices.nallocated += devices_ALLOC_INCREMENT;
}
}
if (!(all_devices.devices[devnum] = malloc(sizeof(DeviceData)))) {
fprintf(stderr,"%s: out of memory\n", png_ident);
return;
}
all_devices.devices[devnum]->filename = malloc(length+1);
if (! all_devices.devices[devnum]->filename) {
fprintf(stderr,"%s: out of memory\n",png_ident);
free(all_devices.devices[devnum]);
all_devices.devices[devnum] = NULL;
return;
}
make_device_active(devnum);
ACTIVE_DEVICE->filename[length] = '\0';
strncpy(ACTIVE_DEVICE->filename,file,length);
initialize_device_ctable(ACTIVE_DEVICE);
ACTIVE_DEVICE->devnum = devnum;
ACTIVE_DEVICE->npages = 0;
if (mode & TRANS_ON)
ACTIVE_DEVICE->trans = true;
else
ACTIVE_DEVICE->trans = false;
*id = (float)devnum;
*err = 1.0;
return;
}
static void close_device( DeviceData *dev ) {
int devnum = dev->devnum;
if (dev->filename)
free(dev->filename);
free(all_devices.devices[devnum]);
all_devices.devices[devnum] = NULL;
if (all_devices.active == devnum)
all_devices.active = -1;
}
#ifdef VMS
void pndriv(int *opcode, float *rbuf, int *nbuf, struct dsc$descriptor_s *chrdsc, int *lchr, int *mode) {
int len = chrdsc->dsc$w_length;
char *chr = chrdsc->dsc$a_pointer;
#else
void PNDRIV(int *opcode, float *rbuf, int *nbuf, char *chr, int *lchr, int *mode, int len) {
#endif
static int firsttime = 1;
/* text used in warning messages */
if (*mode & TRANS_ON)
png_ident = PNG_IDENT_TRANS;
else
png_ident = PNG_IDENT_BASIC;
if (firsttime) {
initialize_default_colortable();
initialize_all_devices();
firsttime = 0;
}
switch (*opcode) {
/* Return device name */
case 1:
{
int i;
char *name;
if (*mode & TRANS_ON)
name = DEVICE_NAME_TRANS;
else
name = DEVICE_NAME_BASIC;
strncpy(chr,name,len);
*lchr = strlen(name);
for (i=*lchr; i<len; i++)
chr[i] = ' ';
};
break;
/* min and max dimensions of plot device, color indicies */
case 2:
rbuf[0] = 0.0;
rbuf[1] = -1.0;
rbuf[2] = 0.0;
rbuf[3] = -1.0;
rbuf[4] = 0.0;
rbuf[5] = 255.0;
*nbuf = 6;
break;
/* return device scale */
case 3:
rbuf[0] = 85.0; /* same as used in GIF drivers */
rbuf[1] = 85.0;
rbuf[2] = 1.0;
*nbuf = 3;
break;
/* return device capabilities */
case 4:
*lchr = strlen(DEVICE_CAPABILITIES);
strncpy(chr,DEVICE_CAPABILITIES,*lchr);
break;
/* return default device filename */
case 5:
*lchr = strlen(DEFAULT_FILENAME);
strncpy(chr,DEFAULT_FILENAME,*lchr);
break;
/* default edge coordinates of view surface */
case 6:
{
int width, height;
get_default_dimensions(&width, &height);
rbuf[0] = 0.0;
rbuf[1] = width - 1.0;
rbuf[2] = 0.0;
rbuf[3] = height - 1.0;
*nbuf = 4;
};
break;
/* scale factor of obsolete character set */
case 7:
rbuf[0] = 1.0;
*nbuf = 1;
break;
/* select active device */
case 8:
make_device_active(rbuf[1]);
break;
/* open device */
case 9:
open_new_device(chr,*lchr,&rbuf[0],&rbuf[1],*mode);
break;
/* close device */
case 10:
close_device(ACTIVE_DEVICE);
break;
/* begin picture */
case 11:
start_plot(ACTIVE_DEVICE, (int)rbuf[0] + 1, (int)rbuf[1] + 1);
break;
/* draw a line */
case 12:
draw_line(
ACTIVE_DEVICE,
(int)rbuf[0],
(int)rbuf[1],
(int)rbuf[2],
(int)rbuf[3],
ACTIVE_DEVICE->cindex
);
break;
/* fill dot */
case 13:
fill_pixel(ACTIVE_DEVICE, (int)rbuf[0], (int)rbuf[1], ACTIVE_DEVICE->cindex);
break;
/* end picture */
case 14:
end_plot(ACTIVE_DEVICE);
break;
/* set current color index */
case 15:
ACTIVE_DEVICE->cindex = (ColorIndex)rbuf[0];
/* flush buffer */
case 16:
break;
/* erase alpha (text) screen */
case 18:
break;
/* set color representation */
case 21:
set_color_rep(
ACTIVE_DEVICE,
(ColorIndex)rbuf[0],
(ColorComponent)(rbuf[1]*255.0),
(ColorComponent)(rbuf[2]*255.0),
(ColorComponent)(rbuf[3]*255.0)
);
break;
/* escape function */
case 23:
break;
/* rectangle fill */
case 24:
fill_rectangle(
ACTIVE_DEVICE,
(int)rbuf[0],
(int)rbuf[1],
(int)rbuf[2],
(int)rbuf[3],
ACTIVE_DEVICE->cindex
);
break;
/* fill line with data */
case 26:
{
int x = rbuf[0];
int y = rbuf[1];
int base = ACTIVE_DEVICE->w * y + x;
int i;
for (i = 0; i<(int)*nbuf-2; i++)
ACTIVE_DEVICE->pixmap[base+i] = (ColorIndex)rbuf[i+2];
}
break;
/* query color representation */
case 29:
{
ColorComponent r, g, b;
get_color_rep( ACTIVE_DEVICE, (ColorIndex)rbuf[0], &r, &g, &b );
rbuf[1] = r / 255.0;
rbuf[2] = g / 255.0;
rbuf[3] = b / 255.0;
*nbuf = 4;
};
break;
default:
fprintf(stderr,"%s: unhandled opcode = %d (please notify Pete Ratzlaff: pratzlaff@cfa.harvard.edu)\n",png_ident, *opcode);
}
}
|