This repository has been archived by the owner on Jun 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
xvxpm.c
719 lines (584 loc) · 17.7 KB
/
xvxpm.c
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
/*
* xvxpm.c - load routine for X11 XPM v3 format pictures
*
* LoadXPM(fname,pinfo)
* WriteXPM(fp,pic,ptype,w,h,rp,gp,bp,nc,col,name)
*/
/*
* Written by Chris P. Ross ([email protected]) to support XPM v3
* format images.
*
* Thanks go to Sam Yates ([email protected]) for
* providing inspiration.
*/
#define VALUES_LEN 80 /* Max length of values line */
#define TOKEN_LEN 8 /* Max length of color token */
#include "xv.h"
/*
* File format:
* (A file format def should go here)
*
*/
typedef struct hent {
char token[TOKEN_LEN];
union {
byte index;
byte rgb[3];
} color_val;
struct hent *next;
} hentry;
#define cv_index color_val.index
#define cv_rgb color_val.rgb
/* Local (Global) Variables */
static byte hex[256];
static hentry **hashtab; /* Hash table */
static int hash_len; /* number of hash buckets */
static int bufchar; /* Buffered character from XpmGetc */
static short in_quote; /* Is the current point in the file in */
/* a quoted string? */
/* Local Functions */
static int XpmLoadError PARM((const char *, const char *));
static int XpmGetc PARM((FILE *));
static int hash PARM((char *));
static int hash_init PARM((int));
static int hash_insert PARM((hentry *));
static hentry *hash_search PARM((char *));
static void hash_destroy PARM((void));
/**************************************/
int LoadXPM(fname, pinfo)
char *fname;
PICINFO *pinfo;
{
/* returns '1' on success */
FILE *fp;
hentry item;
int c;
const char *bname;
char values[VALUES_LEN];
byte *pic;
byte *i_sptr; /* image search pointer */
long filesize;
int w, h, nc, cpp, line_pos;
int npixels;
short i, j, k; /* for() loop indexes */
hentry *clmp; /* colormap hash-table */
hentry *c_sptr; /* cmap hash-table search pointer*/
XColor col;
bname = BaseName(fname);
fp = fopen(fname, "r");
if (!fp)
return (XpmLoadError(bname, "couldn't open file"));
if (DEBUG)
printf("LoadXPM(): Loading xpm from %s\n", fname);
fseek(fp, 0L, 2);
filesize = ftell(fp);
fseek(fp, 0L, 0);
bufchar = -2;
in_quote = FALSE;
/* Read in the values line. It is the first string in the
* xpm, and contains four numbers. w, h, num_colors, and
* chars_per_pixel. */
/* First, get to the first string */
while (((c = XpmGetc(fp))!=EOF) && (c != '"')) ;
line_pos = 0;
/* Now, read in the string */
while (((c = XpmGetc(fp))!=EOF) && (line_pos < VALUES_LEN) && (c != '"')) {
values[line_pos++] = c;
}
if (c != '"')
return (XpmLoadError(bname, "error parsing values line"));
values[line_pos] = '\0';
sscanf(values, "%d%d%d%d", &w, &h, &nc, &cpp);
if (nc <= 0 || cpp <= 0)
return (XpmLoadError(bname, "No colours in Xpm?"));
npixels = w * h;
if (w <= 0 || h <= 0 || npixels/w != h)
return (XpmLoadError(bname, "Image dimensions out of range"));
if (nc > 256)
pinfo->type = PIC24;
else
pinfo->type = PIC8;
if (DEBUG)
printf("LoadXPM(): reading a %dx%d image (%d colors)\n", w, h, nc);
/* We got this far... */
WaitCursor();
if (!hash_init(nc))
return (XpmLoadError(bname, "Not enough memory to hash colormap"));
clmp = (hentry *) malloc(nc * sizeof(hentry)); /* Holds the colormap */
if (pinfo->type == PIC8)
pic = (byte *) malloc((size_t) npixels);
else {
int bufsize = 3*npixels;
if (bufsize/3 != npixels)
return (XpmLoadError(bname, "Image dimensions out of range"));
pic = (byte *) malloc((size_t) bufsize);
}
if (!clmp || !pic)
return (XpmLoadError(bname, "Not enough memory to load pixmap"));
c_sptr = clmp;
i_sptr = pic;
/* initialize the 'hex' array for zippy ASCII-hex -> int conversion */
for (i = 0 ; i < 256 ; i++) hex[i] = 0;
for (i = '0'; i <= '9' ; i++) hex[i] = i - '0';
for (i = 'a'; i <= 'f' ; i++) hex[i] = i - 'a' + 10;
for (i = 'A'; i <= 'F' ; i++) hex[i] = i - 'A' + 10;
/* Again, we've made progress. */
WaitCursor();
/* Now, we need to read the colormap. */
pinfo->colType = F_BWDITHER;
for (i = 0 ; i < nc ; i++) {
while (((c = XpmGetc(fp))!=EOF) && (c != '"')) ;
if (c != '"')
return (XpmLoadError(bname, "Error reading colormap"));
for (j = 0 ; j < cpp ; j++)
c_sptr->token[j] = XpmGetc(fp);
c_sptr->token[j] = '\0';
while (((c = XpmGetc(fp))!=EOF) && ((c == ' ') || (c == '\t'))) ;
if (c == EOF) /* The failure condition of getc() */
return (XpmLoadError(bname, "Error parsing colormap line"));
do {
char key[3];
char color[80]; /* Need to figure a good size for this... */
/*
* Problem with spaces in color names
*
* X s Color Name m Other Name c Last Name
*
* ... this parser doesn't find `Any Name'
*/
for (j=0; j<2 && (c != ' ') && (c != '\t') && (c != EOF); j++) {
key[j] = c;
c = XpmGetc(fp);
}
key[j] = '\0';
while (((c = XpmGetc(fp))!=EOF) && ((c == ' ') || (c == '\t'))) ;
if (c == EOF) /* The failure condition of getc() */
return (XpmLoadError(bname, "Error parsing colormap line"));
for (j=0; j<79 && (c!=' ') && (c!='\t') && (c!='"') && c!=EOF; j++) {
color[j] = c;
c = XpmGetc(fp);
}
color[j]='\0';
while ((c == ' ') || (c == '\t'))
c = XpmGetc(fp);
if (DEBUG > 1)
printf("LoadXPM(): Got color key '%s', color '%s'\n",
key, color);
if (key[0] == 's') /* Don't find a color for a symbolic name */
continue;
if (XParseColor(theDisp,theCmap,color,&col)) {
if (pinfo->type == PIC8) {
pinfo->r[i] = col.red >> 8;
pinfo->g[i] = col.green >> 8;
pinfo->b[i] = col.blue >> 8;
c_sptr->cv_index = i;
/* Is there a better way to do this? */
if (pinfo->colType != F_FULLCOLOR) {
if (pinfo->colType == F_GREYSCALE) {
if (pinfo->r[i] == pinfo->g[i] &&
pinfo->g[i] == pinfo->b[i])
/* Still greyscale... */
;
else
/* It's color */
pinfo->colType = F_FULLCOLOR;
} else {
if (pinfo->r[i] == pinfo->g[i] &&
pinfo->g[i] == pinfo->b[i]) {
if ((pinfo->r[i] == 0 || pinfo->r[i] == 0xff) &&
(pinfo->g[i] == 0 || pinfo->g[i] == 0xff) &&
(pinfo->b[i] == 0 || pinfo->b[i] == 0xff))
/* It's B/W */
;
else
/* It's greyscale */
pinfo->colType = F_GREYSCALE;
} else
/* It's color */
pinfo->colType = F_FULLCOLOR;
}
}
}
else { /* PIC24 */
c_sptr->cv_rgb[0] = col.red >> 8;
c_sptr->cv_rgb[1] = col.green >> 8;
c_sptr->cv_rgb[2] = col.blue >> 8;
}
}
else { /* 'None' or unrecognized color spec */
int rgb;
if (strcasecmp(color, "None") == 0) rgb = 0xb2c0dc; /* infobg */
else {
SetISTR(ISTR_INFO, "%s: unknown color spec '%s'", bname, color);
Timer(1000);
rgb = 0x808080;
}
if (pinfo->type == PIC8) {
pinfo->r[i] = (rgb>>16) & 0xff;
pinfo->g[i] = (rgb>> 8) & 0xff;
pinfo->b[i] = (rgb>> 0) & 0xff;
c_sptr->cv_index = i;
}
else {
c_sptr->cv_rgb[0] = (rgb>>16) & 0xff;
c_sptr->cv_rgb[1] = (rgb>> 8) & 0xff;
c_sptr->cv_rgb[2] = (rgb>> 0) & 0xff;
}
}
xvbcopy((char *) c_sptr, (char *) &item, sizeof(item));
hash_insert(&item);
if (DEBUG > 1)
printf("LoadXPM(): Cmap entry %d, 0x%02x 0x%02x 0x%02x, token '%s'\n",
i, pinfo->r[i], pinfo->g[i], pinfo->b[i], c_sptr->token);
if (*key == 'c') { /* This is the color entry, keep it. */
while (c!='"' && c!=EOF) c = XpmGetc(fp);
break;
}
} while (c != '"');
c_sptr++;
if (!(i%13)) WaitCursor();
} /* for */
if (DEBUG)
printf("LoadXPM(): Read and stored colormap.\n");
/* Now, read the pixmap. */
for (i = 0 ; i < h ; i++) {
while (((c = XpmGetc(fp))!=EOF) && (c != '"')) ;
if (c != '"')
return (XpmLoadError(bname, "Error reading colormap"));
for (j = 0 ; j < w ; j++) {
char pixel[TOKEN_LEN];
hentry *mapentry;
for (k = 0 ; k < cpp ; k++)
pixel[k] = XpmGetc(fp);
pixel[k] = '\0';
if (!(mapentry = (hentry *) hash_search(pixel))) {
/* No colormap entry found. What do we do? Bail for now */
if (DEBUG)
printf("LoadXPM(): Found token '%s', can't find entry in colormap\n",
pixel);
return (XpmLoadError(bname, "Can't map resolve into colormap"));
}
if (pinfo->type == PIC8)
*i_sptr++ = mapentry->cv_index;
else {
*i_sptr++ = mapentry->cv_rgb[0];
*i_sptr++ = mapentry->cv_rgb[1];
*i_sptr++ = mapentry->cv_rgb[2];
}
} /* for ( j < w ) */
while (((c = XpmGetc(fp))!=EOF) && /* Throw away the close " and */
(c != '"')); /* erase all remaining pixels */
if (!(i%7)) WaitCursor();
} /* for ( i < h ) */
pinfo->pic = pic;
pinfo->normw = pinfo->w = w;
pinfo->normh = pinfo->h = h;
pinfo->frmType = F_XPM;
if (DEBUG) printf("LoadXPM(): pinfo->colType is %d\n", pinfo->colType);
sprintf(pinfo->fullInfo, "Xpm v3 Pixmap (%ld bytes)", filesize);
sprintf(pinfo->shrtInfo, "%dx%d Xpm.", w, h);
pinfo->comment = (char *)NULL;
hash_destroy();
free(clmp);
if (fp != stdin)
fclose(fp);
return(1);
}
/***************************************/
static int XpmLoadError(fname, st)
const char *fname, *st;
{
SetISTR(ISTR_WARNING, "%s: %s", fname, st);
return 0;
}
/***************************************/
static int XpmGetc(f)
FILE *f;
{
int c, d, lastc;
if (bufchar != -2) {
/* The last invocation of this routine read the character... */
c = bufchar;
bufchar = -2;
return(c);
}
if ((c = getc(f)) == EOF)
return(EOF);
if (c == '"')
in_quote = !in_quote;
else if (!in_quote && c == '/') { /* might be a C-style comment */
if ((d = getc(f)) == EOF)
return(EOF);
if (d == '*') { /* yup, it *is* a comment */
if ((lastc = getc(f)) == EOF)
return(EOF);
do { /* skip through to the end */
if ((c = getc(f)) == EOF)
return(EOF);
if (lastc != '*' || c != '/') /* end of comment */
lastc = c;
} while (lastc != '*' || c != '/');
if ((c = getc(f)) == EOF)
return(EOF);
} else /* nope, not a comment */
bufchar = d;
}
return(c);
}
/***************************************/
/* hashing functions */
/***************************************/
/***************************************/
static int hash(token)
char *token;
{
int i, sum;
for (i=sum=0; token[i] != '\0'; i++)
sum += token[i];
sum = sum % hash_len;
return (sum);
}
/***************************************/
static int hash_init(hsize)
int hsize;
{
/*
* hash_init() - This function takes an arg, but doesn't do anything with
* it. It could easily be expanded to figure out the "optimal" number of
* buckets. But, for now, a hard-coded 257 will do. (Until I finish the
* 24-bit image writing code. :-)
*/
int i;
hash_len = 257;
hashtab = (hentry **) malloc(sizeof(hentry *) * hash_len);
if (!hashtab) {
SetISTR(ISTR_WARNING, "Couldn't malloc hashtable in LoadXPM()!\n");
return 0;
}
for (i = 0 ; i < hash_len ; i++)
hashtab[i] = NULL;
return 1;
}
/***************************************/
static int hash_insert(entry)
hentry *entry;
{
int key;
hentry *tmp;
key = hash(entry->token);
tmp = (hentry *) malloc(sizeof(hentry));
if (!tmp) {
SetISTR(ISTR_WARNING, "Couldn't malloc hash entry in LoadXPM()!\n");
return 0;
}
xvbcopy((char *)entry, (char *)tmp, sizeof(hentry));
if (hashtab[key]) tmp->next = hashtab[key];
else tmp->next = NULL;
hashtab[key] = tmp;
return 1;
}
/***************************************/
static hentry *hash_search(token)
char *token;
{
int key;
hentry *tmp;
key = hash(token);
tmp = hashtab[key];
while (tmp && strcmp(token, tmp->token)) {
tmp = tmp->next;
}
return tmp;
}
/***************************************/
static void hash_destroy()
{
int i;
hentry *tmp;
for (i=0; i<hash_len; i++) {
while (hashtab[i]) {
tmp = hashtab[i]->next;
free(hashtab[i]);
hashtab[i] = tmp;
}
}
free(hashtab);
return;
}
/**************************************************************************/
int WriteXPM(fp, pic, ptype, w, h, rp, gp, bp, nc, col, name, comments)
FILE *fp; /* File to write to */
byte *pic; /* Image data */
int ptype; /* PIC8 or PIC24 */
int w, h; /* width, & height */
byte *rp, *gp, *bp; /* Colormap pointers */
int nc, col; /* number of colors, & colorstyle */
char *name; /* name of file (/image) */
char *comments; /* image comments (not currently used */
{
/* Note here, that tokenchars is assumed to contain 64 valid token */
/* characters. It's hardcoded to assume this for benefit of generating */
/* tokens, when there are more than 64^2 colors. */
short i, imax, j; /* for() loop indices */
short cpp = 0;
const char *tokenchars =
".#abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
char *tokens;
char image_name[256], *foo;
byte grey;
#ifndef USE_UNFINISHED_24BIT_WRITING_CODE
byte *pic8, *sptr;
byte rtemp[256], gtemp[256], btemp[256], hist[256], trans[256];
long li; /* for() loop index */
int numcol;
#endif
if (DEBUG) {
if (ptype == PIC8)
printf("WriteXPM(): Write a %d color, colortype %d, PIC8 image.\n",
nc, col);
else
printf("WriteXPM(): Write a colortype %d, PIC24 image.\n", col);
}
strcpy(image_name, BaseName(name));
foo = (char *)strchr(image_name, '.');
if (foo)
*foo = '\0'; /* Truncate name at first '.' */
#ifdef USE_UNFINISHED_24BIT_WRITING_CODE
if (ptype == PIC24)
return -1;
#else
/* XXX - The colormap returned from Conv24to8 is not a specific */
/* size (and Conv24to8 doesn't tell me how many entries there */
/* are, or not in this rev of code at least) and not necessarily */
/* 'packed'. Code in here to do that should be removed if */
/* Conv24to8 is "fixed" to do this... */
/* Chris P. Ross ([email protected]) 28-Sept-94 */
numcol = 0;
if (ptype == PIC24) {
/* Reduce to an 8-bit image. Would be nice to actually write */
/* the 24-bit image. I'll have to code that someday... */
pic8 = Conv24to8(pic, w, h, 256, rtemp, gtemp, btemp);
if (!pic8) {
SetISTR(ISTR_WARNING,
"%s: Unable to convert to 8-bit image in WriteXPM()",
image_name);
return 1;
}
if (DEBUG) printf("WriteXPM(): Converted 24bit image.\n");
/* This will count the number of colors in use, and build an */
/* index array into the colormap (which may not be 'packed') */
/* Thanks to John Bradley for this code.. */
xvbzero((char *) hist, sizeof(hist));
numcol = 0;
if (DEBUG)
printf("WriteXPM(): Counting colors (width: %d, height: %d)...\n", w, h);
for (li = 0, sptr = pic8 ; li < (w*h) ; li++, sptr++) {
hist[*sptr] = 1;
}
if (DEBUG)
printf("WriteXPM(): building translation table...\n");
for (i = 0 ; i < 256 ; i++) {
trans[i] = numcol;
rtemp[numcol] = rtemp[i];
gtemp[numcol] = gtemp[i];
btemp[numcol] = btemp[i];
numcol += hist[i];
}
rp=rtemp; gp=gtemp; bp=btemp;
if (DEBUG)
printf("WriteXPM(): Converted 24bit image now has %d colors.\n", numcol);
} else {
pic8 = pic;
numcol = nc;
}
#endif
#ifdef USE_UNFINISHED_24BIT_WRITING_CODE
if (ptype == PIC24) cpp = 4;
else if (numcol > 64) cpp = 2;
else cpp = 1;
#else
if (numcol > 64) cpp = 2;
else cpp = 1;
#endif
fprintf(fp, "/* XPM */\n");
fprintf(fp, "static char *%s[] = {\n", image_name);
fprintf(fp, "/* width height num_colors chars_per_pixel */\n");
fprintf(fp, "\" %3d %3d %6d %1d\",\n", w, h, numcol, cpp);
fprintf(fp, "/* colors */\n");
switch (cpp) {
case 1: /* <= 64 colors; index into tokenchars */
if (col == F_GREYSCALE)
for (i = 0 ; i < numcol ; i ++) {
grey = MONO(rp[i], gp[i], bp[i]);
fprintf(fp, "\"%c c #%02x%02x%02x\",\n", tokenchars[i],
grey, grey, grey);
}
else
for (i = 0 ; i < numcol ; i ++)
fprintf(fp, "\"%c c #%02x%02x%02x\",\n", tokenchars[i],
rp[i], gp[i], bp[i]);
fprintf(fp, "/* pixels */\n");
for (i = 0 ; i < h ; i ++) {
fprintf(fp, "\"");
if (!(i%20))
WaitCursor();
for (j = 0 ; j < w ; j++) {
if (rp == rtemp)
fprintf(fp, "%c", tokenchars[trans[*pic8++]]);
else
fprintf(fp, "%c", tokenchars[*pic8++]);
}
if (i < h-1)
fprintf(fp, "\",\n");
}
break;
case 2: /* 64 < colors < 64^2; build new token list */
tokens = (char *) malloc((size_t) ((2*numcol) + 1) );
if (numcol & 0x3f)
imax = (numcol >> 6) + 1;
else
imax = (numcol >> 6);
for (i = 0 ; i < imax ; i++)
for (j = 0 ; j < 64 && ((i<<6)+j) < numcol ; j++) {
*(tokens+((i<<6)+j)*2) = tokenchars[i];
*(tokens+((i<<6)+j)*2+1) = tokenchars[j];
}
if (col == F_GREYSCALE)
for (i = 0 ; i < numcol ; i++) {
grey = MONO(rp[i], gp[i], bp[i]);
fprintf(fp, "\"%c%c c #%02x%02x%02x\",\n", tokens[i*2],
tokens[i*2+1], grey, grey, grey);
}
else
for (i = 0 ; i < numcol ; i++)
fprintf(fp, "\"%c%c c #%02x%02x%02x\",\n", tokens[i*2],
tokens[i*2+1], rp[i], gp[i], bp[i]);
fprintf(fp, "/* pixels */\n");
for (i = 0 ; i < h ; i++) {
fprintf(fp, "\"");
if (!(i%13))
WaitCursor();
for (j = 0 ; j < w ; j++) {
if (rp == rtemp)
fprintf(fp, "%c%c", tokens[trans[*pic8]*2],
tokens[(trans[*pic8]*2)+1]);
else
fprintf(fp, "%c%c", tokens[(*pic8*2)],
tokens[(*pic8*2)+1]);
pic8++;
}
if (i < h-1)
fprintf(fp, "\",\n");
}
break;
case 4:
/* Generate a colormap */
break;
default:
break;
}
if (fprintf(fp, "\"\n};\n") == EOF) {
return 1;
} else
return 0;
}