| 1 |
/* |
| 2 |
* emspecialex.c |
| 3 |
* This routine handles the emTeX special commands. |
| 4 |
*/ |
| 5 |
#include "xdvips.h" /* The copyright notice in that file is included too!*/ |
| 6 |
|
| 7 |
#include <ctype.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include <math.h> |
| 10 |
/* |
| 11 |
* The external declarations: |
| 12 |
*/ |
| 13 |
#include "protos.h" |
| 14 |
#include "bitmap.h" |
| 15 |
|
| 16 |
#if defined(WIN32) |
| 17 |
#include "wingdi.h" |
| 18 |
#endif |
| 19 |
|
| 20 |
#define WIDTHBYTES(bits) (((bits) + 31) / 32 * 4) |
| 21 |
#define PALVERSION 0x300 |
| 22 |
#define tobyte(x) ((x/8) + (x%8 ? 1 : 0)) |
| 23 |
|
| 24 |
extern Boolean RESIZE_MODE; |
| 25 |
extern char RESIZE_FILTER_BW; |
| 26 |
extern char RESIZE_FILTER_Gray; |
| 27 |
extern char RESIZE_FILTER_RGB; |
| 28 |
extern char RESIZE_FILTER_CMYK; |
| 29 |
extern int GDI_STRETCH_MODE_BW; |
| 30 |
extern int GDI_STRETCH_MODE_Gray; |
| 31 |
extern int GDI_STRETCH_MODE_RGB; |
| 32 |
extern Boolean EMSEARCH_MODE; |
| 33 |
|
| 34 |
#ifdef EMTEX |
| 35 |
/* emtex specials, added by rjl */ |
| 36 |
|
| 37 |
#ifndef TRUE |
| 38 |
#define TRUE 1 |
| 39 |
#define FALSE 0 |
| 40 |
#endif |
| 41 |
|
| 42 |
static long emmax = 161; |
| 43 |
|
| 44 |
/* |
| 45 |
* We define these seek constants if they don't have their |
| 46 |
* values already defined. |
| 47 |
*/ |
| 48 |
#ifndef SEEK_SET |
| 49 |
#define SEEK_SET (0) |
| 50 |
#endif |
| 51 |
#ifndef SEEK_END |
| 52 |
#define SEEK_END (2) |
| 53 |
#endif |
| 54 |
|
| 55 |
struct empt { |
| 56 |
struct empt *next; |
| 57 |
shalfword point; |
| 58 |
integer x, y; |
| 59 |
}; |
| 60 |
|
| 61 |
static struct empt **empoints = NULL; |
| 62 |
boolean emused = FALSE; /* true if em points used on this page */ |
| 63 |
integer emx, emy; |
| 64 |
|
| 65 |
struct emunit { |
| 66 |
const char *unit; |
| 67 |
float factor; |
| 68 |
}; |
| 69 |
struct emunit emtable[] = { |
| 70 |
{"pt",72.27F}, |
| 71 |
{"pc",72.27F/12}, |
| 72 |
{"in",1.0F}, |
| 73 |
{"bp",72.0F}, |
| 74 |
{"cm",2.54F}, |
| 75 |
{"mm",25.4F}, |
| 76 |
{"dd",72.27F/(1238.0F/1157)}, |
| 77 |
{"cc",72.27F/12/(1238.0F/1157)}, |
| 78 |
{"sp",72.27F*65536}, |
| 79 |
{0,0.0F} |
| 80 |
}; |
| 81 |
|
| 82 |
const char *extarr[] = |
| 83 |
{ ".pcx", ".bmp", ".tif", ".jpg", ".png", NULL }; |
| 84 |
|
| 85 |
static void emgraph(char *filename, float emwidth, float emheight); |
| 86 |
|
| 87 |
/* clear the empoints array if necessary */ |
| 88 |
void |
| 89 |
emclear(void) |
| 90 |
{ |
| 91 |
int i; |
| 92 |
if (emused && empoints) |
| 93 |
for (i=0; i<emmax; i++) |
| 94 |
empoints[i] = 0; |
| 95 |
emused = FALSE; |
| 96 |
} |
| 97 |
|
| 98 |
/* put an empoint into the empoints array */ |
| 99 |
static struct empt * |
| 100 |
emptput(shalfword point, integer x, integer y) |
| 101 |
{ |
| 102 |
struct empt *p; |
| 103 |
int start; |
| 104 |
|
| 105 |
emused = TRUE; |
| 106 |
start = point % emmax; |
| 107 |
p = empoints[start]; |
| 108 |
while ( p ) { |
| 109 |
if ( p->point == point ) |
| 110 |
break; |
| 111 |
p = p->next; |
| 112 |
} |
| 113 |
if (p == 0) { |
| 114 |
p = (struct empt *)mymalloc(sizeof(struct empt)); |
| 115 |
p->next = empoints[start]; |
| 116 |
empoints[start] = p; |
| 117 |
} |
| 118 |
p->point = point; |
| 119 |
p->x = x; |
| 120 |
p->y = y; |
| 121 |
return(p); |
| 122 |
} |
| 123 |
|
| 124 |
/* get an empoint from the empoints array */ |
| 125 |
static struct empt * |
| 126 |
emptget(shalfword point) |
| 127 |
{ |
| 128 |
struct empt *p; |
| 129 |
int start; |
| 130 |
|
| 131 |
start = point % emmax; |
| 132 |
if (emused == TRUE) { |
| 133 |
p = empoints[start]; |
| 134 |
while (p) { |
| 135 |
if (p->point == point) |
| 136 |
return p; |
| 137 |
p = p->next; |
| 138 |
} |
| 139 |
} |
| 140 |
sprintf(errbuf,"!em: point %d not defined",point); |
| 141 |
specerror(errbuf); |
| 142 |
return(NULL); /* never returns due to error */ |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
/* convert width into dpi units */ |
| 147 |
static float |
| 148 |
emunits(float width, char *unit) |
| 149 |
{ |
| 150 |
struct emunit *p; |
| 151 |
for (p=emtable; p->unit; p++) { |
| 152 |
if (strcmp(p->unit,unit)==0) |
| 153 |
return( width * actualdpi / p->factor ); |
| 154 |
} |
| 155 |
return (-1.0); /* invalid unit */ |
| 156 |
} |
| 157 |
|
| 158 |
/* The main routine for \special{em:graph ...} called from dospecial.c */ |
| 159 |
/* the line cut parameter is not supported (and is ignored) */ |
| 160 |
|
| 161 |
void |
| 162 |
emspecial(char *p) |
| 163 |
{ |
| 164 |
float emwidth, emheight; |
| 165 |
shalfword empoint1, empoint2; |
| 166 |
struct empt *empoint; |
| 167 |
char emunit[30]; |
| 168 |
char emstr[500]; |
| 169 |
char *emp; |
| 170 |
|
| 171 |
hvpos(); |
| 172 |
for (emp = p+3; *emp && isspace((unsigned char)*emp); emp++); /* skip blanks */ |
| 173 |
if (strncmp(emp, "linewidth", 9) == 0) { |
| 174 |
/* code for linewidth */ |
| 175 |
for (emp = emp+9; *emp && isspace((unsigned char)*emp); emp++); /* skip blanks */ |
| 176 |
sscanf(emp, "%f%2s", &emwidth, emunit); |
| 177 |
emwidth = emunits(emwidth,emunit); |
| 178 |
if (emwidth!=-1.0) { |
| 179 |
snprintf(emstr, sizeof(emstr), "%.1f setlinewidth", emwidth); |
| 180 |
cmdout(emstr); |
| 181 |
#ifdef DEBUG |
| 182 |
if (dd(D_SPECIAL)) |
| 183 |
fprintf(stderr, "em special: Linewidth set to %.1f dots\n", |
| 184 |
emwidth); |
| 185 |
#endif |
| 186 |
} else { |
| 187 |
sprintf(errbuf,"Unknown em: special width"); |
| 188 |
specerror(errbuf); |
| 189 |
} |
| 190 |
} |
| 191 |
else if (strncmp(emp, "moveto", 6) == 0) { |
| 192 |
#ifdef DEBUG |
| 193 |
if (dd(D_SPECIAL)) |
| 194 |
#ifdef SHORTINT |
| 195 |
fprintf(stderr, "em special: moveto %ld,%ld\n", hh, vv); |
| 196 |
#else |
| 197 |
fprintf(stderr, "em special: moveto %d,%d\n", hh, vv); |
| 198 |
#endif |
| 199 |
#endif |
| 200 |
emx = hh; |
| 201 |
emy = vv; |
| 202 |
} |
| 203 |
else if (strncmp(emp, "lineto", 6) == 0) { |
| 204 |
#ifdef DEBUG |
| 205 |
if (dd(D_SPECIAL)) |
| 206 |
#ifdef SHORTINT |
| 207 |
fprintf(stderr, "em special: lineto %ld,%ld\n", hh, vv); |
| 208 |
#else |
| 209 |
fprintf(stderr, "em special: lineto %d,%d\n", hh, vv); |
| 210 |
#endif |
| 211 |
#endif |
| 212 |
cmdout("np"); |
| 213 |
numout(emx); |
| 214 |
numout(emy); |
| 215 |
cmdout("a"); |
| 216 |
numout(hh); |
| 217 |
numout(vv); |
| 218 |
cmdout("li"); |
| 219 |
cmdout("st"); |
| 220 |
emx = hh; |
| 221 |
emy = vv; |
| 222 |
} |
| 223 |
else if (strncmp(emp, "point", 5) == 0) { |
| 224 |
if (empoints == NULL) { |
| 225 |
empoints = |
| 226 |
(struct empt **)mymalloc((integer)emmax * sizeof(struct empt *)); |
| 227 |
emused = TRUE; |
| 228 |
emclear(); |
| 229 |
} |
| 230 |
for (emp = emp+5; *emp && isspace((unsigned char)*emp); emp++) /* skip blanks */ |
| 231 |
; |
| 232 |
empoint1 = (shalfword)atoi(emp); |
| 233 |
empoint = emptput(empoint1,hh,vv); |
| 234 |
#ifdef DEBUG |
| 235 |
if (dd(D_SPECIAL)) |
| 236 |
#ifdef SHORTINT |
| 237 |
fprintf(stderr, "em special: Point %d is %ld,%ld\n", |
| 238 |
#else |
| 239 |
fprintf(stderr, "em special: Point %d is %d,%d\n", |
| 240 |
#endif |
| 241 |
empoint->point, empoint->x, empoint->y); |
| 242 |
#endif |
| 243 |
} |
| 244 |
else if (strncmp(emp, "line", 4) == 0) { |
| 245 |
for (emp = emp+4; *emp && isspace((unsigned char)*emp); emp++) /* skip blanks */ |
| 246 |
; |
| 247 |
empoint1 = (shalfword)atoi(emp); |
| 248 |
for (; *emp && isdigit((unsigned char)*emp); emp++) /* skip point 1 */ |
| 249 |
; |
| 250 |
if ( *emp && strchr("hvp",*emp)!=0 ) |
| 251 |
emp++; /* skip line cut */ |
| 252 |
for (; *emp && isspace((unsigned char)*emp); emp++) /* skip blanks */ |
| 253 |
; |
| 254 |
if ( *emp && (*emp==',') ) |
| 255 |
emp++; /* skip comma separator */ |
| 256 |
for (; *emp && isspace((unsigned char)*emp); emp++) /* skip blanks */ |
| 257 |
; |
| 258 |
empoint2 = (shalfword)atoi(emp); |
| 259 |
for (; *emp && isdigit((unsigned char)*emp); emp++) /* skip point 2 */ |
| 260 |
; |
| 261 |
if ( *emp && strchr("hvp",*emp)!=0 ) |
| 262 |
emp++; /* skip line cut */ |
| 263 |
for (; *emp && isspace((unsigned char)*emp); emp++) /* skip blanks */ |
| 264 |
; |
| 265 |
if ( *emp && (*emp==',') ) |
| 266 |
emp++; /* skip comma separator */ |
| 267 |
emwidth = -1.0; |
| 268 |
emunit[0]='\0'; |
| 269 |
sscanf(emp, "%f%2s", &emwidth, emunit); |
| 270 |
emwidth = emunits(emwidth,emunit); |
| 271 |
#ifdef DEBUG |
| 272 |
if (dd(D_SPECIAL)) |
| 273 |
fprintf(stderr, "em special: Line from point %d to point %d\n", |
| 274 |
empoint1, empoint2); |
| 275 |
#endif |
| 276 |
cmdout("np"); |
| 277 |
if (emwidth!=-1.0) { |
| 278 |
#ifdef DEBUG |
| 279 |
if (dd(D_SPECIAL)) |
| 280 |
fprintf(stderr,"em special: Linewidth temporarily set to %.1f dots\n", |
| 281 |
emwidth); |
| 282 |
#endif |
| 283 |
strcpy(emstr,"currentlinewidth"); |
| 284 |
cmdout(emstr); |
| 285 |
snprintf(emstr, sizeof(emstr), "%.1f setlinewidth", emwidth); |
| 286 |
cmdout(emstr); |
| 287 |
} |
| 288 |
empoint = emptget(empoint1); |
| 289 |
numout(empoint->x); |
| 290 |
numout(empoint->y); |
| 291 |
cmdout("a"); |
| 292 |
empoint = emptget(empoint2); |
| 293 |
numout(empoint->x); |
| 294 |
numout(empoint->y); |
| 295 |
cmdout("li"); |
| 296 |
cmdout("st"); |
| 297 |
if (emwidth!=-1.0) { |
| 298 |
strcpy(emstr,"setlinewidth"); |
| 299 |
cmdout(emstr); |
| 300 |
} |
| 301 |
} |
| 302 |
else if (strncmp(emp, "message", 7) == 0) { |
| 303 |
fprintf_str(stderr, "em message: %s\n", emp+7); |
| 304 |
} |
| 305 |
else if (strncmp(emp, "graph", 5) == 0) { |
| 306 |
int i; |
| 307 |
for (emp = emp+5; *emp && isspace((unsigned char)*emp); emp++); /* skip blanks */ |
| 308 |
memset(emstr, 0, sizeof(emstr)); |
| 309 |
for (i = 0; *emp && !isspace((unsigned char)*emp) && !(*emp == ','); emp++) { |
| 310 |
if ((int)strlen(emstr) - 2 >= (int)sizeof(emstr)) { |
| 311 |
specerror("em:graph: special too long, truncating"); |
| 312 |
break; |
| 313 |
} |
| 314 |
emstr[i++] = *emp; /* copy filename */ |
| 315 |
} |
| 316 |
emstr[i] = '\0'; |
| 317 |
/* now get optional width and height */ |
| 318 |
emwidth = emheight = -1.0; /* no dimension is <= 0 */ |
| 319 |
for (; *emp && ( isspace((unsigned char)*emp) || (*emp==',') ); emp++) |
| 320 |
; /* skip blanks and comma */ |
| 321 |
if (*emp) { |
| 322 |
sscanf(emp, "%f%2s", &emwidth, emunit); /* read width */ |
| 323 |
emwidth = emunits(emwidth,emunit); /* convert to pixels */ |
| 324 |
for (; *emp && (*emp=='.'||isdigit((unsigned char)*emp)||isalpha((unsigned char)*emp)); emp++) |
| 325 |
; /* skip width dimension */ |
| 326 |
for (; *emp && ( isspace((unsigned char)*emp) || (*emp==',') ); emp++) |
| 327 |
; /* skip blanks and comma */ |
| 328 |
if (*emp) { |
| 329 |
sscanf(emp, "%f%2s", &emheight, emunit); /* read height */ |
| 330 |
emheight = emunits(emheight,emunit)*vactualdpi/actualdpi; |
| 331 |
} |
| 332 |
} |
| 333 |
if (emstr[0]) { |
| 334 |
emgraph(emstr,emwidth,emheight); |
| 335 |
} |
| 336 |
else { |
| 337 |
specerror("em:graph: no file given"); |
| 338 |
} |
| 339 |
} |
| 340 |
else { |
| 341 |
sprintf(errbuf, |
| 342 |
"Unknown em: command (%s) in \\special will be ignored", p); |
| 343 |
specerror(errbuf); |
| 344 |
} |
| 345 |
return; |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
/* em:graph routines */ |
| 350 |
|
| 351 |
/* The graphics routines currently decode 5 types of widely used graphics */ |
| 352 |
/* files: .pcx, .bmp, .png, .jpg, .tif. */ |
| 353 |
|
| 354 |
void |
| 355 |
imagehead(char *filename, int wide, int high, int dpi, |
| 356 |
float emwidth, float emheight) |
| 357 |
{ |
| 358 |
char *fullname = NULL, *name; |
| 359 |
if (!quiet) { |
| 360 |
#ifdef KPATHSEA |
| 361 |
fullname = (char *)kpse_find_file (filename, pictpath, 0); |
| 362 |
#endif |
| 363 |
if (!fullname) |
| 364 |
name = filename; |
| 365 |
else |
| 366 |
name = fullname; |
| 367 |
if (strlen(name) + prettycolumn > STDOUTSIZE) { |
| 368 |
fprintf(stderr,"\n"); |
| 369 |
prettycolumn = 0; |
| 370 |
} |
| 371 |
(void)fprintf_str(stderr,"<%s",name); |
| 372 |
(void)fflush(stderr); |
| 373 |
prettycolumn += 2+strlen(name); |
| 374 |
if (fullname) free (fullname); |
| 375 |
} |
| 376 |
hvpos(); |
| 377 |
nlcmdout("@beginspecial @setspecial"); |
| 378 |
if (!disablecomments) { |
| 379 |
cmdout("%%BeginDocument: em:graph"); |
| 380 |
cmdout(filename); |
| 381 |
newline(); |
| 382 |
} |
| 383 |
cmdout("gsave"); |
| 384 |
newline(); |
| 385 |
/* set the image size */ |
| 386 |
if (emwidth <= 0.0) emwidth = (float)wide / dpi * actualdpi; |
| 387 |
if (emheight <= 0.0) emheight = (float)high / dpi * vactualdpi; |
| 388 |
floatout(emwidth*72/actualdpi); |
| 389 |
floatout(emheight*72/vactualdpi); |
| 390 |
newline(); |
| 391 |
cmdout("scale"); |
| 392 |
#ifdef DEBUG |
| 393 |
if (dd(D_SPECIAL)) { |
| 394 |
(void)fprintf(stderr, |
| 395 |
"\nem:graph: %s width %d pixels scaled to %.1f pixels\n", |
| 396 |
filename, wide, emwidth); |
| 397 |
(void)fprintf_str(stderr, |
| 398 |
"em:graph: %s height %d pixels scaled to %.1f pixels\n", |
| 399 |
filename, high, emheight); |
| 400 |
} |
| 401 |
#endif |
| 402 |
} |
| 403 |
|
| 404 |
void |
| 405 |
imagetail(void) |
| 406 |
{ |
| 407 |
nlcmdout("grestore") ; |
| 408 |
if (!disablecomments) { |
| 409 |
(void)fprintf(bitfile, "\n%%%%EndDocument\n") ; |
| 410 |
linepos = 0; |
| 411 |
} |
| 412 |
nlcmdout("@endspecial") ; |
| 413 |
if (!quiet) { |
| 414 |
(void)fprintf(stderr,">"); |
| 415 |
(void)fflush(stderr); |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
#if defined(WIN32) |
| 420 |
HPALETTE FIDIBLogPalette(HDC hDC, FIBITMAP *FIDib) |
| 421 |
{ |
| 422 |
LPSTR lpDIBHdr; // pointer to DIB header |
| 423 |
HPALETTE hPal; // handle to a palette |
| 424 |
LPLOGPALETTE lpPal; // pointer to a logical palette |
| 425 |
int wNumColors, i; |
| 426 |
LPBITMAPINFO lpbmi; |
| 427 |
|
| 428 |
// if invalid handle, return NULL |
| 429 |
|
| 430 |
if (!FIDib) |
| 431 |
return NULL; |
| 432 |
|
| 433 |
// get a pointers to the DIB header |
| 434 |
|
| 435 |
lpDIBHdr = (LPSTR)Bitmap_GetInfoHeader(FIDib); |
| 436 |
|
| 437 |
// select and realize palette |
| 438 |
|
| 439 |
hPal = NULL; |
| 440 |
switch (Bitmap_GetBPP(FIDib)) { |
| 441 |
case 8: |
| 442 |
wNumColors = 256; |
| 443 |
break; |
| 444 |
case 1: |
| 445 |
wNumColors = 2; |
| 446 |
break; |
| 447 |
default: |
| 448 |
wNumColors = 0; |
| 449 |
} |
| 450 |
if (wNumColors) |
| 451 |
{ |
| 452 |
lpPal = (LPLOGPALETTE)mymalloc(sizeof(LOGPALETTE) + |
| 453 |
sizeof(PALETTEENTRY) * wNumColors); |
| 454 |
if ( lpPal == NULL ) |
| 455 |
return NULL; |
| 456 |
|
| 457 |
lpPal->palVersion = PALVERSION; |
| 458 |
lpPal->palNumEntries = (WORD) wNumColors; |
| 459 |
|
| 460 |
lpbmi = (LPBITMAPINFO)lpDIBHdr; |
| 461 |
for (i = 0; i < wNumColors; i++) |
| 462 |
{ |
| 463 |
lpPal->palPalEntry[i].peRed = lpbmi->bmiColors[i].rgbRed; |
| 464 |
lpPal->palPalEntry[i].peGreen = lpbmi->bmiColors[i].rgbGreen; |
| 465 |
lpPal->palPalEntry[i].peBlue = lpbmi->bmiColors[i].rgbBlue; |
| 466 |
lpPal->palPalEntry[i].peFlags = 0; |
| 467 |
} |
| 468 |
} |
| 469 |
else { |
| 470 |
wNumColors = (1 << (GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES))); |
| 471 |
if ( wNumColors > 256 ) |
| 472 |
wNumColors = 256; |
| 473 |
|
| 474 |
lpPal = (LPLOGPALETTE)mymalloc(sizeof(LOGPALETTE) + |
| 475 |
sizeof(PALETTEENTRY) * wNumColors); |
| 476 |
if ( lpPal == NULL ) |
| 477 |
return NULL; |
| 478 |
|
| 479 |
lpPal->palVersion = PALVERSION; |
| 480 |
lpPal->palNumEntries = (WORD) wNumColors; |
| 481 |
|
| 482 |
// Copy the current system palette into our logical palette |
| 483 |
|
| 484 |
GetSystemPaletteEntries(hDC, 0, wNumColors, |
| 485 |
(LPPALETTEENTRY)(lpPal->palPalEntry)); |
| 486 |
} |
| 487 |
|
| 488 |
// create the palette and get handle to it |
| 489 |
hPal = CreatePalette(lpPal); |
| 490 |
free(lpPal); |
| 491 |
if ( ! hPal ) { |
| 492 |
return NULL; |
| 493 |
} |
| 494 |
|
| 495 |
return hPal; |
| 496 |
} |
| 497 |
|
| 498 |
HBITMAP copyToBitmap(HDC hDC, FIBITMAP *FIDib, Boolean initBitmap, void **DibBits) |
| 499 |
{ |
| 500 |
LPBITMAPINFO lpDIBHdr; // pointer to DIB header |
| 501 |
HBITMAP hBitmap; // handle to device-dependent bitmap |
| 502 |
DWORD k; |
| 503 |
BYTE *source, *dest; |
| 504 |
|
| 505 |
// if invalid handle, return NULL |
| 506 |
|
| 507 |
if (!FIDib) |
| 508 |
return NULL; |
| 509 |
|
| 510 |
// get a pointers to the DIB header |
| 511 |
|
| 512 |
lpDIBHdr = (LPBITMAPINFO)Bitmap_GetInfoHeader(FIDib); |
| 513 |
lpDIBHdr->bmiHeader.biSizeImage = Bitmap_GetPitch(FIDib) * Bitmap_GetHeight(FIDib); |
| 514 |
|
| 515 |
// create bitmap from DIB info. and bits |
| 516 |
if ( lpDIBHdr->bmiHeader.biBitCount > 1 ) { |
| 517 |
hBitmap = CreateDIBSection( hDC, lpDIBHdr, DIB_RGB_COLORS, DibBits, NULL, 0 ); |
| 518 |
if ( hBitmap ) { |
| 519 |
if ( initBitmap ) { |
| 520 |
for (k = 0; k<Bitmap_GetHeight(FIDib); k++) { |
| 521 |
source = Bitmap_GetScanLine(FIDib, k); |
| 522 |
dest = (BYTE *)*DibBits + k * WIDTHBYTES(Bitmap_GetWidth(FIDib) * Bitmap_GetBPP(FIDib)); |
| 523 |
memcpy(dest, source, Bitmap_GetPitch(FIDib)); |
| 524 |
} |
| 525 |
} |
| 526 |
} |
| 527 |
} |
| 528 |
else { |
| 529 |
if ( initBitmap ) |
| 530 |
hBitmap = CreateDIBitmap(hDC, (LPBITMAPINFOHEADER)lpDIBHdr, CBM_INIT, |
| 531 |
Bitmap_GetBits(FIDib), (LPBITMAPINFO)lpDIBHdr, DIB_RGB_COLORS); |
| 532 |
else |
| 533 |
hBitmap = CreateDIBitmap(hDC, (LPBITMAPINFOHEADER)lpDIBHdr, 0, |
| 534 |
NULL, (LPBITMAPINFO)lpDIBHdr, DIB_RGB_COLORS); |
| 535 |
} |
| 536 |
|
| 537 |
// return handle to the bitmap |
| 538 |
return hBitmap; |
| 539 |
} |
| 540 |
|
| 541 |
FIBITMAP *copyFromBitmap(HDC dc, HBITMAP hbmp) { |
| 542 |
if(hbmp) { |
| 543 |
int Success; |
| 544 |
BITMAP bm; |
| 545 |
int nColors; |
| 546 |
FIBITMAP *_dib = NULL; |
| 547 |
|
| 548 |
// Get informations about the bitmap |
| 549 |
GetObject(hbmp, sizeof(BITMAP), (LPSTR) &bm); |
| 550 |
|
| 551 |
// Allocate a new FIBITMAP |
| 552 |
if ((_dib = Bitmap_AllocateT(FIT_BITMAP, (WORD)bm.bmWidth, (WORD)bm.bmHeight, |
| 553 |
(WORD)bm.bmBitsPixel,0,0,0)) == NULL) { |
| 554 |
return NULL; |
| 555 |
} |
| 556 |
|
| 557 |
// The GetDIBits function clears the biClrUsed and biClrImportant BITMAPINFO members (dont't know why) |
| 558 |
// So we save these infos below. This is needed for palettized images only. |
| 559 |
nColors = Bitmap_GetColorsUsed(_dib); |
| 560 |
|
| 561 |
// Copy the pixels |
| 562 |
Success = GetDIBits(dc, // handle to DC |
| 563 |
hbmp, // handle to bitmap |
| 564 |
0, // first scan line to set |
| 565 |
Bitmap_GetHeight(_dib), // number of scan lines to copy |
| 566 |
Bitmap_GetBits(_dib), // array for bitmap bits |
| 567 |
Bitmap_GetInfo(_dib), // bitmap data buffer |
| 568 |
DIB_RGB_COLORS // RGB |
| 569 |
); |
| 570 |
if(Success == 0) { |
| 571 |
ReleaseDC(NULL, dc); |
| 572 |
return NULL; |
| 573 |
} |
| 574 |
ReleaseDC(NULL, dc); |
| 575 |
|
| 576 |
// restore BITMAPINFO members |
| 577 |
|
| 578 |
Bitmap_GetInfoHeader(_dib)->biClrUsed = nColors; |
| 579 |
Bitmap_GetInfoHeader(_dib)->biClrImportant = nColors; |
| 580 |
|
| 581 |
return _dib; |
| 582 |
} |
| 583 |
|
| 584 |
return NULL; |
| 585 |
} |
| 586 |
|
| 587 |
FIBITMAP *ResizeBitmapWnd(char *filename, int mode, FIBITMAP *bitmap, int destWidth, int destHeight) |
| 588 |
{ |
| 589 |
HDC hSourceDC, hDestDC; |
| 590 |
HPALETTE hPal, hSourcePal, hDestPal; |
| 591 |
HBITMAP hSourceBitmap, hDestBitmap; |
| 592 |
HGDIOBJ objSrc, objDest; |
| 593 |
BYTE *SrcDibBits, *DestDibBits; |
| 594 |
int oldmode, i; |
| 595 |
FIBITMAP *dib; |
| 596 |
RGBQUAD *palette; |
| 597 |
|
| 598 |
hSourceDC = CreateCompatibleDC(GetDC(NULL)); |
| 599 |
hPal = FIDIBLogPalette(hSourceDC,bitmap); |
| 600 |
if ( hPal == NULL ) { |
| 601 |
DeleteDC(hSourceDC); |
| 602 |
sprintf(errbuf, "em:graph: Unable to create image logical palette\n"); |
| 603 |
specerror(errbuf); |
| 604 |
return NULL; |
| 605 |
} |
| 606 |
hSourcePal = SelectPalette(hSourceDC, hPal, FALSE); |
| 607 |
RealizePalette(hSourceDC); |
| 608 |
hSourceBitmap = copyToBitmap(hSourceDC,bitmap,true,(void **)&SrcDibBits); |
| 609 |
if ( hSourceBitmap == NULL ) { |
| 610 |
SelectPalette(hSourceDC, hSourcePal, TRUE); |
| 611 |
RealizePalette(hSourceDC); |
| 612 |
DeleteObject(hPal); |
| 613 |
DeleteDC(hSourceDC); |
| 614 |
sprintf(errbuf, "em:graph: Unable to create source image HBITMAP\n"); |
| 615 |
specerror(errbuf); |
| 616 |
return NULL; |
| 617 |
} |
| 618 |
objSrc = SelectObject (hSourceDC,hSourceBitmap); |
| 619 |
hDestDC = CreateCompatibleDC(GetDC(NULL)); |
| 620 |
hDestPal = SelectPalette(hDestDC, hPal, FALSE); |
| 621 |
RealizePalette(hDestDC); |
| 622 |
dib = Bitmap_AllocateT(FIT_BITMAP, destWidth, destHeight, Bitmap_GetBPP(bitmap), 0, 0, 0); |
| 623 |
if ((Bitmap_GetColorType(bitmap) == FIC_MINISBLACK) && (Bitmap_GetBPP(bitmap) == 8)) { |
| 624 |
palette = Bitmap_GetPalette(dib); |
| 625 |
for (i = 0; i < 256; i++) { |
| 626 |
palette[i].rgbBlue = i; |
| 627 |
palette[i].rgbGreen = i; |
| 628 |
palette[i].rgbRed = i; |
| 629 |
palette[i].rgbReserved = 0; |
| 630 |
} |
| 631 |
} |
| 632 |
hDestBitmap = copyToBitmap(hDestDC,dib,false,(void **)&DestDibBits); |
| 633 |
Bitmap_Unload(dib); |
| 634 |
if ( hDestBitmap == NULL ) { |
| 635 |
SelectPalette(hSourceDC, hSourcePal, TRUE); |
| 636 |
RealizePalette(hSourceDC); |
| 637 |
DeleteObject(hSourceBitmap); |
| 638 |
DeleteDC(hSourceDC); |
| 639 |
SelectPalette(hDestDC, hDestPal, TRUE); |
| 640 |
RealizePalette(hDestDC); |
| 641 |
DeleteDC(hDestDC); |
| 642 |
DeleteObject(hPal); |
| 643 |
sprintf(errbuf, "em:graph: Unable to create destination image HBITMAP\n"); |
| 644 |
specerror(errbuf); |
| 645 |
return NULL; |
| 646 |
} |
| 647 |
objDest = SelectObject (hDestDC,hDestBitmap); |
| 648 |
oldmode = SetStretchBltMode(hDestDC,mode); |
| 649 |
if ( ! StretchBlt(hDestDC,0,0,destWidth,destHeight,hSourceDC,0,0, |
| 650 |
Bitmap_GetWidth(bitmap), Bitmap_GetHeight(bitmap), SRCCOPY)) { |
| 651 |
SelectPalette(hSourceDC, hSourcePal, TRUE); |
| 652 |
RealizePalette(hSourceDC); |
| 653 |
SelectPalette(hDestDC, hDestPal, TRUE); |
| 654 |
RealizePalette(hDestDC); |
| 655 |
DeleteObject(hPal); |
| 656 |
DeleteObject(hSourceBitmap); |
| 657 |
DeleteObject(hDestBitmap); |
| 658 |
SetStretchBltMode(hDestDC,oldmode); |
| 659 |
DeleteDC(hSourceDC); |
| 660 |
DeleteDC(hDestDC); |
| 661 |
sprintf(errbuf, "em:graph: Unable to resize image %s\n",filename); |
| 662 |
specerror(errbuf); |
| 663 |
return NULL; |
| 664 |
} |
| 665 |
GdiFlush(); |
| 666 |
dib = copyFromBitmap(hDestDC,hDestBitmap); |
| 667 |
SetStretchBltMode(hDestDC,oldmode); |
| 668 |
SelectPalette(hSourceDC, hSourcePal, TRUE); |
| 669 |
RealizePalette(hSourceDC); |
| 670 |
SelectPalette(hDestDC, hDestPal, TRUE); |
| 671 |
RealizePalette(hDestDC); |
| 672 |
DeleteObject(hPal); |
| 673 |
DeleteObject(hSourceBitmap); |
| 674 |
DeleteObject(hDestBitmap); |
| 675 |
DeleteDC(hSourceDC); |
| 676 |
DeleteDC(hDestDC); |
| 677 |
if ( dib == NULL ) { |
| 678 |
sprintf(errbuf, "em:graph: Unable to copy image from HBITMAP\n"); |
| 679 |
specerror(errbuf); |
| 680 |
return NULL; |
| 681 |
} |
| 682 |
|
| 683 |
return dib; |
| 684 |
} |
| 685 |
#endif |
| 686 |
|
| 687 |
FIBITMAP *ResizeBitmap(FIBITMAP *bitmap, int destWidth, int destHeight, char filter) |
| 688 |
{ |
| 689 |
BITMAP_FILTER FIfilter; |
| 690 |
FIBITMAP *dst; |
| 691 |
FIICCPROFILE *icc_src, *icc; |
| 692 |
|
| 693 |
switch ( filter ) { |
| 694 |
case 'b': |
| 695 |
FIfilter = FILTER_BOX; |
| 696 |
break; |
| 697 |
case 't': |
| 698 |
FIfilter = FILTER_BILINEAR; |
| 699 |
break; |
| 700 |
case 'B': |
| 701 |
FIfilter = FILTER_BSPLINE; |
| 702 |
break; |
| 703 |
case 'l': |
| 704 |
FIfilter = FILTER_LANCZOS3; |
| 705 |
break; |
| 706 |
case 'm': |
| 707 |
FIfilter = FILTER_BICUBIC; |
| 708 |
break; |
| 709 |
case 'c': |
| 710 |
FIfilter = FILTER_CATMULLROM; |
| 711 |
break; |
| 712 |
default: |
| 713 |
FIfilter = FILTER_BICUBIC; |
| 714 |
} |
| 715 |
dst = Bitmap_Rescale(bitmap, destWidth, destHeight, FIfilter); |
| 716 |
if ( dst ) { |
| 717 |
icc_src = Bitmap_GetICCProfile(bitmap); |
| 718 |
if ( icc_src == NULL ) { |
| 719 |
if (Bitmap_GetColorType(bitmap) == FIC_CMYK) { |
| 720 |
icc = Bitmap_CreateICCProfile(dst, NULL, 0); |
| 721 |
icc->flags = FIICC_COLOR_IS_CMYK; |
| 722 |
} |
| 723 |
} |
| 724 |
else { |
| 725 |
icc = Bitmap_CreateICCProfile(dst, icc_src->data, icc_src->size); |
| 726 |
icc->flags = icc_src->flags; |
| 727 |
} |
| 728 |
} |
| 729 |
return dst; |
| 730 |
} |
| 731 |
|
| 732 |
unsigned |
| 733 |
EmReadProc(void *buffer, unsigned size, unsigned count, fi_handle handle) { |
| 734 |
return (unsigned)fread(buffer, size, count, (FILE *)handle); |
| 735 |
} |
| 736 |
|
| 737 |
int |
| 738 |
EmSeekProc(fi_handle handle, long offset, int origin) { |
| 739 |
return fseek((FILE *)handle, offset, origin); |
| 740 |
} |
| 741 |
|
| 742 |
long |
| 743 |
EmTellProc(fi_handle handle) { |
| 744 |
return ftell((FILE *)handle); |
| 745 |
} |
| 746 |
|
| 747 |
void |
| 748 |
bitmapgraph(FILE *f, BitmapIO *io, char *filename, BITMAP_FORMAT fif, float emwidth, float emheight) |
| 749 |
{ |
| 750 |
BITMAPINFOHEADER *bm; |
| 751 |
RGBQUAD *palette; |
| 752 |
|
| 753 |
unsigned char isblack[256]; |
| 754 |
unsigned char rr; |
| 755 |
unsigned char gg; |
| 756 |
unsigned char bb; |
| 757 |
unsigned char c = 0; |
| 758 |
|
| 759 |
unsigned char *line; |
| 760 |
char *pshexa; |
| 761 |
|
| 762 |
int clrtablesize; |
| 763 |
int dpi; |
| 764 |
int i; |
| 765 |
int j; |
| 766 |
|
| 767 |
unsigned char emask = 0; |
| 768 |
integer ewidth = 0; |
| 769 |
float mw, mh; |
| 770 |
int nWidth, nHeight; |
| 771 |
|
| 772 |
//Resize conditions |
| 773 |
unsigned int RESIZE_COND = actualdpi / 400; |
| 774 |
unsigned int RESIZE_CONDV = vactualdpi / 400; |
| 775 |
|
| 776 |
FIBITMAP *bitmap, *tmpbmp; |
| 777 |
BITMAP_COLOR_TYPE bct; |
| 778 |
unsigned int bpp; |
| 779 |
|
| 780 |
bitmap = NULL; |
| 781 |
switch (fif) { |
| 782 |
case FIF_PCX: |
| 783 |
bitmap = Bitmap_LoadFromHandle(FIF_PCX, io, (fi_handle)f, 0); |
| 784 |
break; |
| 785 |
case FIF_BMP: |
| 786 |
bitmap = Bitmap_LoadFromHandle(FIF_BMP, io, (fi_handle)f, 0); |
| 787 |
break; |
| 788 |
case FIF_TIFF: |
| 789 |
bitmap = Bitmap_LoadFromHandle(FIF_TIFF, io, (fi_handle)f, TIFF_CMYK); |
| 790 |
break; |
| 791 |
case FIF_JPEG: |
| 792 |
bitmap = Bitmap_LoadFromHandle(FIF_JPEG, io, (fi_handle)f, JPEG_ACCURATE | JPEG_CMYK); |
| 793 |
break; |
| 794 |
case FIF_PNG: |
| 795 |
bitmap = Bitmap_LoadFromHandle(FIF_PNG, io, (fi_handle)f, PNG_DEFAULT); |
| 796 |
break; |
| 797 |
} |
| 798 |
if ( bitmap == NULL ) { |
| 799 |
sprintf(errbuf, "em:graph: Unable to read file %s\n",filename); |
| 800 |
specerror(errbuf); |
| 801 |
return; |
| 802 |
} |
| 803 |
if (Bitmap_GetImageType(bitmap) != FIT_BITMAP) { |
| 804 |
sprintf(errbuf, "em:graph: image %s format not supported\n", filename); |
| 805 |
specerror(errbuf); |
| 806 |
Bitmap_Unload(bitmap); |
| 807 |
return; |
| 808 |
} |
| 809 |
|
| 810 |
bct = Bitmap_GetColorType(bitmap); |
| 811 |
bpp = Bitmap_GetBPP(bitmap); |
| 812 |
if (bct == FIC_RGBALPHA) { |
| 813 |
tmpbmp = Bitmap_ConvertRGBATo24Bits(bitmap); |
| 814 |
if (tmpbmp == NULL) { |
| 815 |
sprintf(errbuf, "em:graph: Unable to convert RGBALPHA image from file %s\n", filename); |
| 816 |
specerror(errbuf); |
| 817 |
Bitmap_Unload(bitmap); |
| 818 |
return; |
| 819 |
} |
| 820 |
Bitmap_Unload(bitmap); |
| 821 |
bitmap = tmpbmp; |
| 822 |
} |
| 823 |
else if (((bct == FIC_RGB) && (bpp == 32))) { |
| 824 |
tmpbmp = Bitmap_ConvertTo24Bits(bitmap); |
| 825 |
if (tmpbmp == NULL) { |
| 826 |
sprintf(errbuf, "em:graph: Unable to convert RGBALPHA image from file %s\n", filename); |
| 827 |
specerror(errbuf); |
| 828 |
Bitmap_Unload(bitmap); |
| 829 |
return; |
| 830 |
} |
| 831 |
Bitmap_Unload(bitmap); |
| 832 |
bitmap = tmpbmp; |
| 833 |
} |
| 834 |
|
| 835 |
/* get BITMAP header */ |
| 836 |
bm = Bitmap_GetInfoHeader(bitmap); |
| 837 |
dpi = (int)((float)Bitmap_GetDotsPerMeterX(bitmap) / 100 * 2.54 + 0.5); |
| 838 |
|
| 839 |
if ( RESIZE_MODE ) { |
| 840 |
nWidth = bm->biWidth; |
| 841 |
nHeight = bm->biHeight; |
| 842 |
if (emwidth > 0.0) { |
| 843 |
mw = (float)bm->biWidth / dpi * actualdpi; |
| 844 |
if ( fabsf(mw - emwidth) > RESIZE_COND ) { |
| 845 |
nWidth = (int)((float)bm->biWidth / mw * emwidth + 0.5); |
| 846 |
} |
| 847 |
} |
| 848 |
if (emheight > 0.0) { |
| 849 |
mh = (float)bm->biHeight / dpi * vactualdpi; |
| 850 |
if ( fabsf(mh - emheight) > RESIZE_CONDV ) { |
| 851 |
nHeight = (int)((float)bm->biHeight / mh * emheight + 0.5); |
| 852 |
} |
| 853 |
} |
| 854 |
if ( (nWidth != bm->biWidth) || (nHeight != bm->biHeight) ) { |
| 855 |
//Resize bitmap |
| 856 |
switch (bm->biBitCount) { |
| 857 |
case 1: |
| 858 |
//Mono bitmap |
| 859 |
if ( RESIZE_FILTER_BW == 'w' ) { |
| 860 |
#if defined(WIN32) |
| 861 |
//WinGDI resize |
| 862 |
tmpbmp = ResizeBitmapWnd(filename,GDI_STRETCH_MODE_BW,bitmap,nWidth,nHeight); |
| 863 |
if ( tmpbmp == NULL ) { |
| 864 |
Bitmap_Unload(bitmap); |
| 865 |
return; |
| 866 |
} |
| 867 |
#else |
| 868 |
//resize with resample filter |
| 869 |
tmpbmp = Bitmap_Resample(bitmap, nWidth, nHeight); |
| 870 |
if ( tmpbmp == NULL ) { |
| 871 |
sprintf(errbuf, "em:graph: Unable to resize BW image %s\n",filename); |
| 872 |
specerror(errbuf); |
| 873 |
Bitmap_Unload(bitmap); |
| 874 |
return; |
| 875 |
} |
| 876 |
#endif |
| 877 |
} |
| 878 |
else { |
| 879 |
tmpbmp = Bitmap_Resample(bitmap, nWidth, nHeight); |
| 880 |
if ( tmpbmp == NULL ) { |
| 881 |
sprintf(errbuf, "em:graph: Unable to resize BW image %s\n",filename); |
| 882 |
specerror(errbuf); |
| 883 |
Bitmap_Unload(bitmap); |
| 884 |
return; |
| 885 |
} |
| 886 |
} |
| 887 |
break; |
| 888 |
case 4: |
| 889 |
case 8: |
| 890 |
if (Bitmap_GetColorType(bitmap) == FIC_PALETTE) { |
| 891 |
if ( RESIZE_FILTER_RGB == 'w' ) { |
| 892 |
#if defined(WIN32) |
| 893 |
//WinGDI resize |
| 894 |
tmpbmp = ResizeBitmapWnd(filename,GDI_STRETCH_MODE_RGB,bitmap,nWidth,nHeight); |
| 895 |
if ( tmpbmp == NULL ) { |
| 896 |
Bitmap_Unload(bitmap); |
| 897 |
return; |
| 898 |
} |
| 899 |
#else |
| 900 |
//resize with resample filter |
| 901 |
tmpbmp = Bitmap_Resample(bitmap,nWidth,nHeight); |
| 902 |
if ( tmpbmp == NULL ) { |
| 903 |
sprintf(errbuf, "em:graph: Unable to resize BW image %s\n",filename); |
| 904 |
specerror(errbuf); |
| 905 |
Bitmap_Unload(bitmap); |
| 906 |
return; |
| 907 |
} |
| 908 |
#endif |
| 909 |
} |
| 910 |
else { |
| 911 |
if ( RESIZE_FILTER_RGB == 'r' ) { |
| 912 |
//resize with resample filter |
| 913 |
tmpbmp = Bitmap_Resample(bitmap, nWidth, nHeight); |
| 914 |
if ( tmpbmp == NULL ) { |
| 915 |
sprintf(errbuf, "em:graph: Unable to resize image %s\n",filename); |
| 916 |
specerror(errbuf); |
| 917 |
Bitmap_Unload(bitmap); |
| 918 |
return; |
| 919 |
} |
| 920 |
} |
| 921 |
else { |
| 922 |
//resize with math filter |
| 923 |
tmpbmp = ResizeBitmap(bitmap,nWidth,nHeight,RESIZE_FILTER_RGB); |
| 924 |
if ( tmpbmp == NULL ) { |
| 925 |
sprintf(errbuf, "em:graph: Unable to resize image %s\n",filename); |
| 926 |
specerror(errbuf); |
| 927 |
Bitmap_Unload(bitmap); |
| 928 |
return; |
| 929 |
} |
| 930 |
} |
| 931 |
} |
| 932 |
} |
| 933 |
else { |
| 934 |
if ( RESIZE_FILTER_Gray == 'w' ) { |
| 935 |
#if defined(WIN32) |
| 936 |
//WinGDI resize |
| 937 |
tmpbmp = ResizeBitmapWnd(filename,GDI_STRETCH_MODE_Gray,bitmap,nWidth,nHeight); |
| 938 |
if ( tmpbmp == NULL ) { |
| 939 |
Bitmap_Unload(bitmap); |
| 940 |
return; |
| 941 |
} |
| 942 |
#else |
| 943 |
//resize with resample filter |
| 944 |
tmpbmp = Bitmap_Resample(bitmap,nWidth,nHeight); |
| 945 |
if ( tmpbmp == NULL ) { |
| 946 |
sprintf(errbuf, "em:graph: Unable to resize BW image %s\n",filename); |
| 947 |
specerror(errbuf); |
| 948 |
Bitmap_Unload(bitmap); |
| 949 |
return; |
| 950 |
} |
| 951 |
#endif |
| 952 |
} |
| 953 |
else { |
| 954 |
if ( RESIZE_FILTER_Gray == 'r' ) { |
| 955 |
//resize with resample filter |
| 956 |
tmpbmp = Bitmap_Resample(bitmap, nWidth, nHeight); |
| 957 |
if ( tmpbmp == NULL ) { |
| 958 |
sprintf(errbuf, "em:graph: Unable to resize image %s\n",filename); |
| 959 |
specerror(errbuf); |
| 960 |
Bitmap_Unload(bitmap); |
| 961 |
return; |
| 962 |
} |
| 963 |
} |
| 964 |
else { |
| 965 |
//resize with math filter |
| 966 |
tmpbmp = ResizeBitmap(bitmap,nWidth,nHeight,RESIZE_FILTER_Gray); |
| 967 |
if ( tmpbmp == NULL ) { |
| 968 |
sprintf(errbuf, "em:graph: Unable to resize image %s\n",filename); |
| 969 |
specerror(errbuf); |
| 970 |
Bitmap_Unload(bitmap); |
| 971 |
return; |
| 972 |
} |
| 973 |
} |
| 974 |
} |
| 975 |
} |
| 976 |
break; |
| 977 |
case 24: |
| 978 |
if ( RESIZE_FILTER_RGB == 'w' ) { |
| 979 |
#if defined(WIN32) |
| 980 |
//WinGDI resize |
| 981 |
tmpbmp = ResizeBitmapWnd(filename,GDI_STRETCH_MODE_RGB,bitmap,nWidth,nHeight); |
| 982 |
if ( tmpbmp == NULL ) { |
| 983 |
Bitmap_Unload(bitmap); |
| 984 |
return; |
| 985 |
} |
| 986 |
#else |
| 987 |
//resize with resample filter |
| 988 |
tmpbmp = Bitmap_Resample(bitmap,nWidth,nHeight); |
| 989 |
if ( tmpbmp == NULL ) { |
| 990 |
sprintf(errbuf, "em:graph: Unable to resize BW image %s\n",filename); |
| 991 |
specerror(errbuf); |
| 992 |
Bitmap_Unload(bitmap); |
| 993 |
return; |
| 994 |
} |
| 995 |
#endif |
| 996 |
} |
| 997 |
else { |
| 998 |
if ( RESIZE_FILTER_RGB == 'r' ) { |
| 999 |
//resize with resample filter |
| 1000 |
tmpbmp = Bitmap_Resample(bitmap, nWidth, nHeight); |
| 1001 |
if ( tmpbmp == NULL ) { |
| 1002 |
sprintf(errbuf, "em:graph: Unable to resize image %s\n",filename); |
| 1003 |
specerror(errbuf); |
| 1004 |
Bitmap_Unload(bitmap); |
| 1005 |
return; |
| 1006 |
} |
| 1007 |
} |
| 1008 |
else { |
| 1009 |
//resize with math filter |
| 1010 |
tmpbmp = ResizeBitmap(bitmap,nWidth,nHeight,RESIZE_FILTER_RGB); |
| 1011 |
if ( tmpbmp == NULL ) { |
| 1012 |
sprintf(errbuf, "em:graph: Unable to resize image %s\n",filename); |
| 1013 |
specerror(errbuf); |
| 1014 |
Bitmap_Unload(bitmap); |
| 1015 |
return; |
| 1016 |
} |
| 1017 |
} |
| 1018 |
} |
| 1019 |
break; |
| 1020 |
case 32: |
| 1021 |
if ( (RESIZE_FILTER_CMYK == 'w') || (RESIZE_FILTER_CMYK == 'r') ) { |
| 1022 |
//resize with resample filter |
| 1023 |
tmpbmp = Bitmap_Resample(bitmap, nWidth, nHeight); |
| 1024 |
if ( tmpbmp == NULL ) { |
| 1025 |
sprintf(errbuf, "em:graph: Unable to resize image %s\n",filename); |
| 1026 |
specerror(errbuf); |
| 1027 |
Bitmap_Unload(bitmap); |
| 1028 |
return; |
| 1029 |
} |
| 1030 |
} |
| 1031 |
else { |
| 1032 |
//resize with math filter |
| 1033 |
tmpbmp = ResizeBitmap(bitmap,nWidth,nHeight,RESIZE_FILTER_CMYK); |
| 1034 |
if ( tmpbmp == NULL ) { |
| 1035 |
sprintf(errbuf, "em:graph: Unable to resize image %s\n",filename); |
| 1036 |
specerror(errbuf); |
| 1037 |
Bitmap_Unload(bitmap); |
| 1038 |
return; |
| 1039 |
} |
| 1040 |
} |
| 1041 |
break; |
| 1042 |
default: |
| 1043 |
tmpbmp = Bitmap_Clone(bitmap); |
| 1044 |
} |
| 1045 |
Bitmap_Unload(bitmap); |
| 1046 |
bitmap = tmpbmp; |
| 1047 |
bm = Bitmap_GetInfoHeader(bitmap); |
| 1048 |
} |
| 1049 |
} |
| 1050 |
|
| 1051 |
imagehead(filename,(int)bm->biWidth,(int)bm->biHeight,dpi,emwidth,emheight); |
| 1052 |
|
| 1053 |
/* determine the size of the color table to read */ |
| 1054 |
clrtablesize = Bitmap_GetColorsUsed(bitmap); |
| 1055 |
|
| 1056 |
/* read in the color table */ |
| 1057 |
palette = Bitmap_GetPalette(bitmap); |
| 1058 |
|
| 1059 |
pshexa = NULL; |
| 1060 |
|
| 1061 |
switch (bm->biBitCount) { |
| 1062 |
case 1: |
| 1063 |
for (i = 0; i < clrtablesize; i++) { |
| 1064 |
bb = palette[i].rgbBlue; |
| 1065 |
gg = palette[i].rgbGreen; |
| 1066 |
rr = palette[i].rgbRed; |
| 1067 |
isblack[i] = (rr < 0xff) || (gg < 0xff) || (bb < 0xff); |
| 1068 |
} |
| 1069 |
pshexa = (char *) mymalloc((integer)tobyte(bm->biWidth)); |
| 1070 |
/* output the postscript image size preamble */ |
| 1071 |
cmdout("/picstr"); |
| 1072 |
numout((integer)tobyte(bm->biWidth)); |
| 1073 |
cmdout("string def"); |
| 1074 |
numout((integer)bm->biWidth); |
| 1075 |
numout((integer)bm->biHeight); |
| 1076 |
cmdout("false"); |
| 1077 |
cmdout("["); |
| 1078 |
numout((integer)bm->biWidth); |
| 1079 |
numout((integer)0); |
| 1080 |
numout((integer)0); |
| 1081 |
numout((integer)bm->biHeight); |
| 1082 |
numout((integer)0); |
| 1083 |
numout((integer)bm->biHeight); |
| 1084 |
cmdout("]"); |
| 1085 |
nlcmdout("{currentfile picstr readhexstring pop} imagemask"); |
| 1086 |
if (bm->biWidth%8) |
| 1087 |
emask = (1<<(8-(bm->biWidth%8)))-1; /* mask for edge of bitmap */ |
| 1088 |
else |
| 1089 |
emask = 0; |
| 1090 |
ewidth = tobyte(bm->biWidth); |
| 1091 |
for (i = 0; i < bm->biHeight; i++) { |
| 1092 |
memset(pshexa,0xff,tobyte(bm->biWidth)); |
| 1093 |
line = Bitmap_GetScanLine(bitmap,i); |
| 1094 |
if (isblack[0]) |
| 1095 |
for (j = 0; j < ewidth ; j++) |
| 1096 |
pshexa[j] = line[j]; |
| 1097 |
else |
| 1098 |
for (j = 0; j < ewidth ; j++) |
| 1099 |
pshexa[j] = ~line[j]; |
| 1100 |
pshexa[ewidth-1] |= emask; |
| 1101 |
newline(); |
| 1102 |
mhexout((unsigned char *) pshexa,(long)tobyte(bm->biWidth)); |
| 1103 |
} |
| 1104 |
break; |
| 1105 |
case 4: |
| 1106 |
tmpbmp = Bitmap_ConvertTo8Bits(bitmap); |
| 1107 |
Bitmap_Unload(bitmap); |
| 1108 |
bitmap = tmpbmp; |
| 1109 |
bm = Bitmap_GetInfoHeader(bitmap); |
| 1110 |
clrtablesize = Bitmap_GetColorsUsed(bitmap); |
| 1111 |
palette = Bitmap_GetPalette(bitmap); |
| 1112 |
case 8: |
| 1113 |
if (Bitmap_GetColorType(bitmap) == FIC_PALETTE) { |
| 1114 |
cmdout("/picstr"); |
| 1115 |
numout((integer)(bm->biWidth)); |
| 1116 |
cmdout("string def"); |
| 1117 |
cmdout("/rgbstr"); |
| 1118 |
numout((integer)(bm->biWidth * 3)); |
| 1119 |
cmdout("string def"); |
| 1120 |
nlcmdout("/read8{currentfile picstr readhexstring pop /rgb_ind 0 def"); |
| 1121 |
nlcmdout(" {3 mul pal exch 3 getinterval rgbstr exch rgb_ind exch putinterval"); |
| 1122 |
nlcmdout(" /rgb_ind rgb_ind 3 add def}forall rgbstr}bind def"); |
| 1123 |
nlcmdout("/pal <"); |
| 1124 |
pshexa = (char *) mymalloc((integer)(clrtablesize * 3)); |
| 1125 |
for (i = 0; i < clrtablesize; i++) { |
| 1126 |
pshexa[i*3] = palette[i].rgbRed; |
| 1127 |
pshexa[i*3+1] = palette[i].rgbGreen; |
| 1128 |
pshexa[i*3+2] = palette[i].rgbBlue; |
| 1129 |
} |
| 1130 |
mhexout((unsigned char *) pshexa,(long)(clrtablesize * 3)); |
| 1131 |
free(pshexa); |
| 1132 |
nlcmdout("> def"); |
| 1133 |
numout((integer)bm->biWidth); |
| 1134 |
numout((integer)bm->biHeight); |
| 1135 |
numout((integer)8); |
| 1136 |
cmdout("["); |
| 1137 |
numout((integer)bm->biWidth); |
| 1138 |
numout((integer)0); |
| 1139 |
numout((integer)0); |
| 1140 |
numout((integer)bm->biHeight); |
| 1141 |
numout((integer)0); |
| 1142 |
numout((integer)bm->biHeight); |
| 1143 |
cmdout("]"); |
| 1144 |
nlcmdout("{read8} false 3 colorimage"); |
| 1145 |
pshexa = (char *) mymalloc((integer)(bm->biWidth)); |
| 1146 |
for (i = 0; i < bm->biHeight; i++) { |
| 1147 |
memset(pshexa,0xff,bm->biWidth); |
| 1148 |
line = Bitmap_GetScanLine(bitmap,i); |
| 1149 |
memcpy(pshexa,line,bm->biWidth); |
| 1150 |
newline(); |
| 1151 |
mhexout((unsigned char *) pshexa,(long)bm->biWidth); |
| 1152 |
} |
| 1153 |
} |
| 1154 |
else { |
| 1155 |
pshexa = (char *) mymalloc((integer)(bm->biWidth)); |
| 1156 |
cmdout("/picstr"); |
| 1157 |
numout((integer)(bm->biWidth)); |
| 1158 |
cmdout("string def"); |
| 1159 |
numout((integer)bm->biWidth); |
| 1160 |
numout((integer)bm->biHeight); |
| 1161 |
numout((integer)8); |
| 1162 |
cmdout("["); |
| 1163 |
numout((integer)bm->biWidth); |
| 1164 |
numout((integer)0); |
| 1165 |
numout((integer)0); |
| 1166 |
numout((integer)bm->biHeight); |
| 1167 |
numout((integer)0); |
| 1168 |
numout((integer)bm->biHeight); |
| 1169 |
cmdout("]"); |
| 1170 |
nlcmdout("{currentfile picstr readhexstring pop} image"); |
| 1171 |
for (i = 0; i < bm->biHeight; i++) { |
| 1172 |
memset(pshexa,0xff,bm->biWidth); |
| 1173 |
line = Bitmap_GetScanLine(bitmap,i); |
| 1174 |
memcpy(pshexa,line,bm->biWidth); |
| 1175 |
newline(); |
| 1176 |
mhexout((unsigned char *) pshexa,(long)bm->biWidth); |
| 1177 |
} |
| 1178 |
} |
| 1179 |
break; |
| 1180 |
case 24: |
| 1181 |
pshexa = (char *) mymalloc((integer)(bm->biWidth * 3)); |
| 1182 |
/* output the postscript image size preamble */ |
| 1183 |
cmdout("/picstr"); |
| 1184 |
numout((integer)(bm->biWidth * 3)); |
| 1185 |
cmdout("string def"); |
| 1186 |
numout((integer)bm->biWidth); |
| 1187 |
numout((integer)bm->biHeight); |
| 1188 |
numout((integer)8); |
| 1189 |
cmdout("["); |
| 1190 |
numout((integer)bm->biWidth); |
| 1191 |
numout((integer)0); |
| 1192 |
numout((integer)0); |
| 1193 |
numout((integer)bm->biHeight); |
| 1194 |
numout((integer)0); |
| 1195 |
numout((integer)bm->biHeight); |
| 1196 |
cmdout("]"); |
| 1197 |
nlcmdout("{currentfile picstr readhexstring pop} false 3 colorimage"); |
| 1198 |
for (i = 0; i < bm->biHeight; i++) { |
| 1199 |
memset(pshexa,0xff,bm->biWidth * 3); |
| 1200 |
line = Bitmap_GetScanLine(bitmap,i); |
| 1201 |
for (j = 0; j < bm->biWidth; j++) { |
| 1202 |
pshexa[j * 3] = line[j * 3]; |
| 1203 |
pshexa[j * 3 + 1] = line[j * 3 + 1]; |
| 1204 |
pshexa[j * 3 + 2] = line[j * 3 + 2]; |
| 1205 |
} |
| 1206 |
newline(); |
| 1207 |
mhexout((unsigned char *) pshexa,(long)(bm->biWidth * 3)); |
| 1208 |
} |
| 1209 |
break; |
| 1210 |
case 32: |
| 1211 |
pshexa = (char *) mymalloc((integer)(bm->biWidth * 4)); |
| 1212 |
/* output the postscript image size preamble */ |
| 1213 |
cmdout("/picstr"); |
| 1214 |
numout((integer)(bm->biWidth * 4)); |
| 1215 |
cmdout("string def"); |
| 1216 |
numout((integer)bm->biWidth); |
| 1217 |
numout((integer)bm->biHeight); |
| 1218 |
numout((integer)8); |
| 1219 |
cmdout("["); |
| 1220 |
numout((integer)bm->biWidth); |
| 1221 |
numout((integer)0); |
| 1222 |
numout((integer)0); |
| 1223 |
numout((integer)bm->biHeight); |
| 1224 |
numout((integer)0); |
| 1225 |
numout((integer)bm->biHeight); |
| 1226 |
cmdout("]"); |
| 1227 |
nlcmdout("{currentfile picstr readhexstring pop} false 4 colorimage"); |
| 1228 |
for (i = 0; i < bm->biHeight; i++) { |
| 1229 |
memset(pshexa,0xff,bm->biWidth * 3); |
| 1230 |
line = Bitmap_GetScanLine(bitmap,i); |
| 1231 |
for (j = 0; j < bm->biWidth; j++) { |
| 1232 |
pshexa[j*4] = line[j*4]; |
| 1233 |
pshexa[j*4+1] = line[j*4+1]; |
| 1234 |
pshexa[j*4+2] = line[j*4+2]; |
| 1235 |
pshexa[j*4+3] = line[j*4+3]; |
| 1236 |
} |
| 1237 |
newline(); |
| 1238 |
mhexout((unsigned char *) pshexa,(long)(bm->biWidth * 4)); |
| 1239 |
} |
| 1240 |
break; |
| 1241 |
} |
| 1242 |
|
| 1243 |
imagetail() ; |
| 1244 |
if ( pshexa ) |
| 1245 |
free(pshexa); |
| 1246 |
Bitmap_Unload(bitmap); |
| 1247 |
} |
| 1248 |
/* ------------------------------------------------------------------------ */ |
| 1249 |
|
| 1250 |
void |
| 1251 |
emgraph(char *filename, float emwidth, float emheight) |
| 1252 |
{ |
| 1253 |
BitmapIO io; |
| 1254 |
char fname[260]; |
| 1255 |
FILE *f; |
| 1256 |
char *env; |
| 1257 |
int i; |
| 1258 |
|
| 1259 |
strcpy(fname, filename); |
| 1260 |
|
| 1261 |
/* find the file */ |
| 1262 |
f = search(figpath, fname, READBIN); |
| 1263 |
if (f == (FILE *)NULL) { |
| 1264 |
if ( (env = getenv("DVIDRVGRAPH")) != NULL ) |
| 1265 |
#ifdef KPATHSEA |
| 1266 |
f = search((kpse_file_format_type)env[0],filename,READBIN); |
| 1267 |
#else |
| 1268 |
f = search(env,filename,READBIN); |
| 1269 |
#endif |
| 1270 |
} |
| 1271 |
/* if still haven't found it try adding extensions */ |
| 1272 |
if ( (f == (FILE *)NULL) && EMSEARCH_MODE ) { |
| 1273 |
i = 0; |
| 1274 |
while (extarr[i] != NULL) { |
| 1275 |
strcpy(fname, filename); |
| 1276 |
env = strrchr(fname,'.'); |
| 1277 |
if ( env ) |
| 1278 |
*env = 0; |
| 1279 |
strcat(fname, extarr[i]); |
| 1280 |
f = search(figpath, fname, READBIN); |
| 1281 |
if (f == (FILE *)NULL) { |
| 1282 |
if ( (env = getenv("DVIDRVGRAPH")) != NULL ) |
| 1283 |
#ifdef KPATHSEA |
| 1284 |
f = search((kpse_file_format_type)env[0],filename,READBIN); |
| 1285 |
#else |
| 1286 |
f = search(env,filename,READBIN); |
| 1287 |
#endif |
| 1288 |
} |
| 1289 |
if (f != (FILE *)NULL) |
| 1290 |
break; |
| 1291 |
i++; |
| 1292 |
} |
| 1293 |
} |
| 1294 |
|
| 1295 |
io.read_proc = EmReadProc; |
| 1296 |
io.seek_proc = EmSeekProc; |
| 1297 |
io.tell_proc = EmTellProc; |
| 1298 |
io.write_proc = NULL; |
| 1299 |
if (f != (FILE *)NULL) { |
| 1300 |
if (Bitmap_ValidateFromHandle(FIF_BMP, &io, (fi_handle)f)) |
| 1301 |
bitmapgraph(f, &io, fname, FIF_BMP, emwidth, emheight); |
| 1302 |
else if (Bitmap_ValidateFromHandle(FIF_PCX, &io, (fi_handle)f)) |
| 1303 |
bitmapgraph(f, &io, fname, FIF_PCX, emwidth, emheight); |
| 1304 |
else if (Bitmap_ValidateFromHandle(FIF_JPEG, &io, (fi_handle)f)) |
| 1305 |
bitmapgraph(f, &io, fname, FIF_JPEG, emwidth, emheight); |
| 1306 |
else if (Bitmap_ValidateFromHandle(FIF_PNG, &io, (fi_handle)f)) |
| 1307 |
bitmapgraph(f, &io, fname, FIF_PNG, emwidth, emheight); |
| 1308 |
else if (Bitmap_ValidateFromHandle(FIF_TIFF, &io, (fi_handle)f)) |
| 1309 |
bitmapgraph(f, &io, fname, FIF_TIFF, emwidth, emheight); |
| 1310 |
else { |
| 1311 |
snprintf(fname, sizeof(fname), "em:graph: %s: Unknown file format", filename); |
| 1312 |
error(fname); |
| 1313 |
} |
| 1314 |
} |
| 1315 |
else { |
| 1316 |
snprintf(fname, sizeof(fname), "em:graph: %s: File not found", filename); |
| 1317 |
error(fname); |
| 1318 |
} |
| 1319 |
if (f != (FILE *)NULL) |
| 1320 |
close_file(f); |
| 1321 |
} |
| 1322 |
|
| 1323 |
#else |
| 1324 |
void |
| 1325 |
emspecial(char *p) |
| 1326 |
{ |
| 1327 |
sprintf(errbuf,"emTeX specials not compiled in this version"); |
| 1328 |
specerror(errbuf); |
| 1329 |
} |
| 1330 |
#endif /* EMTEX */ |