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
|
/*
$Id: pbm.c,v 1.4 2010/10/06 16:02:49 rice Exp $
PLplot PBM (PPM) device driver.
Contributed by John C. Atkinson and Zulfi Cumali.
Slightly modified by Geoffrey Furnish.
*/
#include "plDevs.h"
#ifdef PLD_pbm
#include "plplotP.h"
#include "drivers.h"
/* Device info */
const char* plD_DEVICE_INFO_pbm = "pbm:PDB (PPM) Driver:0:pbm:38:pbm";
/* pmr: defined in drivers.h */
/* void plD_dispatch_init_pbm ( PLDispatchTable *pdt );*/
void plD_init_pbm (PLStream *);
void plD_line_pbm (PLStream *, short, short, short, short);
void plD_polyline_pbm (PLStream *, short *, short *, PLINT);
void plD_eop_pbm (PLStream *);
void plD_bop_pbm (PLStream *);
void plD_tidy_pbm (PLStream *);
void plD_state_pbm (PLStream *, PLINT);
void plD_esc_pbm (PLStream *, PLINT, void *);
#undef PIXELS_X
#undef PIXELS_Y
#define PIXELS_X 640
#define PIXELS_Y 480
static char *cmap;
#undef MAX
#undef ABS
#define MAX(a,b) ((a>b) ? a : b)
#define ABS(a) ((a<0) ? -a : a)
#define MAX_INTENSITY 255
void plD_dispatch_init_pbm( PLDispatchTable *pdt )
{
#ifndef ENABLE_DYNDRIVERS
pdt->pl_MenuStr = "PDB (PPM) Driver";
pdt->pl_DevName = "pbm";
#endif
pdt->pl_type = plDevType_FileOriented;
pdt->pl_seq = 38;
pdt->pl_init = (plD_init_fp) plD_init_pbm;
pdt->pl_line = (plD_line_fp) plD_line_pbm;
pdt->pl_polyline = (plD_polyline_fp) plD_polyline_pbm;
pdt->pl_eop = (plD_eop_fp) plD_eop_pbm;
pdt->pl_bop = (plD_bop_fp) plD_bop_pbm;
pdt->pl_tidy = (plD_tidy_fp) plD_tidy_pbm;
pdt->pl_state = (plD_state_fp) plD_state_pbm;
pdt->pl_esc = (plD_esc_fp) plD_esc_pbm;
}
/*--------------------------------------------------------------------------*\
* plD_init_pbm()
*
* Initialize device (terminal).
\*--------------------------------------------------------------------------*/
void
plD_init_pbm(PLStream *pls)
{
#if 1
/* Initialize family file info */
plFamInit(pls);
plP_setpxl((PLFLT) 5.905, (PLFLT) 5.905);
#endif
pls->color = 1; /* Is a color device */
pls->dev_fill0 = 0; /* Handle solid fills */
pls->dev_fill1 = 0; /* Handle pattern fills */
pls->nopause = 1; /* Don`t pause between frames */
/* Prompt for a file name if not already set */
plOpenFile(pls);
pls->pdfs = pdf_finit(pls->OutFile);
/* Allocate and initialize device-specific data */
pls->dev = NULL;
/* Set up device parameters */
if (pls->xlength <= 0 || pls->ylength <= 0) {
plspage(0., 0., PIXELS_X, PIXELS_Y, 0, 0);
}
plP_setphy(0, pls->xlength, 0, pls->ylength);
}
#if 0
void
plD_line_pbm(PLStream *pls, short x1a, short y1a, short x2a, short y2a)
{
int steps, i, dx, dy;
double x_off, y_off, dx_step, dy_step;
/* This algoritm is by Steven Harrington
From "Computer Graphics: A Proogramming Approach */
dx = x2a - x1a;
dy = y2a - y1a;
steps = MAX(ABS(dx), ABS(dy)) + 1;
steps *= 2;
dx_step = dx/steps;
dy_step = dy/steps;
x_off = x1a + 0.5;
y_off = y1a + 0.5;
for (i = 0; i < steps; i++) {
cmap[(int)y_off][(int)x_off][0] = pls->curcolor.r;
cmap[(int)y_off][(int)x_off][1] = pls->curcolor.g;
cmap[(int)y_off][(int)x_off][2] = pls->curcolor.b;
x_off += dx_step;
y_off += dy_step;
}
cmap[(int)y_off][(int)x_off][0] = pls->curcolor.r;
cmap[(int)y_off][(int)x_off][1] = pls->curcolor.g;
cmap[(int)y_off][(int)x_off][2] = pls->curcolor.b;
return;
}
#endif
#define sign(a) ((a<0) ? -1 : ((a == 0) ? 0 : 1))
#if 0
#define plot(x,y,c) {cmap[y-1][x-1][0] = (c)->curcolor.r; \
cmap[y-1][x-1][1] = (c)->curcolor.g; \
cmap[y-1][x-1][2] = (c)->curcolor.b; }
/* Bresnham's algorithm for line plotting on a scan lines */
void
plD_line_pbm(PLStream *pls, short x1a, short y1a, short x2a, short y2a)
{
int e, x, y, dx, dy, s1, s2, temp, change, i;
x = x1a;
y = y1a;
dx = ABS(x2a - x1a);
dy = ABS(y2a - y1a);
s1 = sign(x2a - x1a);
s2 = sign(y2a - y1a);
if( dy > dx ){
temp= dx;
dx = dy;
dy = temp;
change = 1;
}
else {
change = 0;
}
e = 2 * dy - dx;
for( i=1; i<dx;i++){
plot(x,y,pls);
while( e>= 0 ) {
if(change == 1) x += s1;
else y += s2;
e = e - 2 * dx;
}
if(change == 1) y += s2;
else x += s1;
e = e + 2 * dy;
}
}
#else
/* pmr: ii not i - shadows another i in the function */
#define plot(x,y,c) {int ii = 3*((y)*(c)->xlength+(x)); \
cmap[ii+0] = (c)->curcolor.r; \
cmap[ii+1] = (c)->curcolor.g; \
cmap[ii+2] = (c)->curcolor.b; }
/* Modified version of the ljii routine (see ljii.c) */
void
plD_line_pbm(PLStream *pls, short x1a, short y1a, short x2a, short y2a)
{
int i;
int xx1 = x1a, yy1 = y1a, xx2 = x2a, yy2 = y2a;
PLINT x1b, y1b, x2b, y2b;
PLFLT length, fx, fy, dx, dy;
/* Take mirror image, since PCL expects (0,0) to be at top left */
yy1 = pls->ylength - (yy1 - 0);
yy2 = pls->ylength - (yy2 - 0);
x1b = xx1, x2b = xx2, y1b = yy1, y2b = yy2;
length = (PLFLT) sqrt((double)
((x2b - x1b) * (x2b - x1b) + (y2b - y1b) * (y2b - y1b)));
if (length == 0.)
length = 1.;
dx = (xx2 - xx1) / length;
dy = (yy2 - yy1) / length;
fx = xx1;
fy = yy1;
plot((PLINT) xx1, (PLINT) yy1, pls);
plot((PLINT) xx2, (PLINT) yy2, pls);
for (i = 1; i <= (int) length; i++) {
fx += dx; fy += dy;
plot((PLINT) fx, (PLINT) fy, pls);
}
}
#endif
void
plD_polyline_pbm(PLStream *pls, short *xa, short *ya, PLINT npts)
{
int i;
for (i=0; i<npts - 1; i++)
plD_line_pbm(pls, xa[i], ya[i], xa[i + 1], ya[i + 1]);
}
void
plD_eop_pbm(PLStream *pls)
{
FILE *fp = pls->OutFile;
int iw;
if (fp != NULL) {
fprintf(fp, "%s\n", "P6");
fprintf(fp, "%d %d\n", pls->xlength, pls->ylength);
fprintf(fp, "%d\n", MAX_INTENSITY);
/*
{
int i, j, k;
for (i=0; i<PIXELS_Y; i++)
for (j=0; j<PIXELS_X; j++)
for (k=0; k<3; k++)
fprintf(fp, "%c", cmap[i][j][k]);
}
*/
iw = fwrite( cmap, 1, pls->xlength * pls->ylength * 3, fp );
fclose(fp);
}
free(cmap);
cmap = 0;
}
void
plD_bop_pbm(PLStream *pls)
{
int i,j,k;
cmap = (char*)malloc(pls->xlength*pls->ylength*3);
for (i=0; i<pls->ylength; i++)
for (j=0; j<pls->xlength; j++) {
k = (i*pls->xlength + j)*3;
cmap[k+0] = pls->cmap0[0].r;
cmap[k+1] = pls->cmap0[0].g;
cmap[k+2] = pls->cmap0[0].b;
}
}
void
plD_tidy_pbm(PLStream *pls)
{
/* Nothing to do here */
(void) pls; /* pmr: make it used */
}
void
plD_state_pbm(PLStream *pls, PLINT op)
{
/* Nothing to do here */
(void) pls; /* pmr: make it used */
(void) op;
}
void
plD_esc_pbm(PLStream *pls, PLINT op, void *ptr)
{
/* Nothing to do here */
(void) pls; /* pmr: make it used */
(void) op;
(void) ptr;
}
#else
int
pldummy_pbm(void)
{
return 0;
}
#endif /* PLD_pbm */
|