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
|
/* xpixmap.c:
*
* XPixMap format file read and identify routines. these can handle any
* "format 1" XPixmap file with up to BUFSIZ - 1 chars per pixel. it's
* not nearly as picky as it might be.
*
* unlike most image loading routines, this is X specific since it
* requires X color name parsing. to handle this we have global X
* variables for display and screen. it's ugly but it keeps the rest
* of the image routines clean.
*
* Copyright 1989 Jim Frost. See included file "copyright.h" for complete
* copyright information.
*/
#include "copyright.h"
#include "xloadimage.h"
/* SUPPRESS 530 */
/* SUPPRESS 560 */
extern Display *Disp; /* X display, null if in "identify" mode */
extern int Scrn; /* X screen number */
#define XPM_FORMAT 1
static void corrupted(fullname, zf)
char *fullname;
ZFILE *zf;
{
zclose(zf);
printf("%s: X Pixmap file is corrupted\n", fullname);
exit(1);
}
Image *xpixmapLoad(fullname, name, verbose)
char *fullname, *name;
unsigned int verbose;
{ ZFILE *zf;
char buf[BUFSIZ];
char what[BUFSIZ];
char *p;
char *imagetitle;
unsigned int value;
unsigned int format; /* image format */
unsigned int w, h; /* image dimensions */
unsigned int cpp; /* chars per pixel */
unsigned int ncolors; /* number of colors */
unsigned int depth; /* depth of image */
char **ctable; /* color table */
Image *image;
XColor xcolor;
unsigned int a, b, x, y;
int c;
byte *dptr;
cpp = 0;
if (! (zf= zopen(fullname)))
return(NULL);
/* read #defines until we have all that are necessary or until we
* get an error
*/
format= w= h= ncolors= 0;
for (;;) {
if (! zgets((byte *)buf, BUFSIZ - 1, zf)) {
zclose(zf);
return(NULL);
}
if (!strncmp(buf, "#define", 7)) {
if (sscanf(buf, "#define %s %d", what, &value) != 2) {
zclose(zf);
return(NULL);
}
if (! (p= rindex(what, '_')))
p= what;
else
p++;
if (!strcmp(p, "format"))
format= value;
else if (!strcmp(p, "width"))
w= value;
else if (!strcmp(p, "height"))
h= value;
else if (!strcmp(p, "ncolors"))
ncolors= value;
/* this one is ugly
*/
else if (!strcmp(p, "pixel")) { /* this isn't pretty but it works */
if (p == what)
continue;
*(--p)= '\0';
if (!(p= rindex(what, '_')) || (p == what) || strcmp(++p, "per"))
continue;
*(--p)= '\0';
if (!(p= rindex(what, '_')))
p= what;
if (strcmp(++p, "chars"))
continue;
cpp= value;
}
}
else if ((sscanf(buf, "static char * %s", what) == 1) &&
(p= rindex(what, '_')) && !strcmp(++p, "colors[]"))
break;
}
if ((format != XPM_FORMAT) || !w || !h || !ncolors || !cpp) {
zclose(zf);
return(NULL);
}
if ((p= rindex(what, '_'))) { /* get the name in the image if there is */
*p= '\0'; /* one */
imagetitle= dupString(what);
}
else {
p= what;
imagetitle= dupString(name);
}
if (verbose)
printf("%s is a %dx%d X Pixmap image with %d colors titled '%s'\n",
name, w, h, ncolors, imagetitle);
for (depth= 1, value= 2; value < ncolors; value <<= 1, depth++)
;
image= newRGBImage(w, h, depth);
image->rgb.used= ncolors;
image->title= dupString(imagetitle);
/* read the colors array and build the image colormap
*/
ctable= (char **)lmalloc(sizeof(char *) * ncolors);
xcolor.flags= DoRed | DoGreen | DoBlue;
for (a= 0; a < ncolors; a++) {
/* read pixel value
*/
*(ctable + a)= (char *)lmalloc(cpp);
while (((c= zgetc(zf)) != EOF) && (c != '"'))
;
if (c == EOF)
corrupted(fullname, zf);
for (b= 0; b < cpp; b++) {
if ((c= zgetc(zf)) == '\\')
c= zgetc(zf);
if (c == EOF)
corrupted(fullname, zf);
*(*(ctable + a) + b)= (char)c;
}
if (((c= zgetc(zf)) == EOF) || (c != '"'))
corrupted(fullname, zf);
/* read color definition and parse it
*/
while (((c= zgetc(zf)) != EOF) && (c != '"'))
;
if (c == EOF)
corrupted(fullname, zf);
for (b= 0; ((c= zgetc(zf)) != EOF) && (c != '"'); b++) {
if (c == '\\')
c= zgetc(zf);
if (c == EOF)
corrupted(fullname, zf);
buf[b]= (char)c;
}
buf[b]= '\0';
if (Disp) {
if (! XParseColor(Disp, DefaultColormap(Disp, Scrn), buf, &xcolor)) {
printf("%s: %s: Bad color name\n", fullname, buf);
exit(1);
}
*(image->rgb.red + a)= xcolor.red;
*(image->rgb.green + a)= xcolor.green;
*(image->rgb.blue + a)= xcolor.blue;
}
}
for (;;) {
if (! zgets((byte *)buf, BUFSIZ - 1, zf))
corrupted(fullname, zf);
if (sscanf(buf, "static char * %s", what) == 1)
break;
}
if ((p= rindex(what, '_')))
p++;
else
p= what;
if (strcmp(p, "pixels[]"))
corrupted(fullname, zf);
/* read in image data
*/
dptr= image->data;
for (y= 0; y < h; y++) {
while (((c= zgetc(zf)) != EOF) && (c != '"'))
;
for (x= 0; x < w; x++) {
for (a= 0; a < cpp; a++) {
if ((c= zgetc(zf)) == '\\')
c= zgetc(zf);
if (c == EOF)
corrupted(fullname, zf);
buf[a]= (char)c;
}
for (a= 0; a < ncolors; a++)
if (!strncmp(*(ctable + a), buf, cpp))
break;
if (a == ncolors) { /* major uncool */
zclose(zf);
printf("%s: Pixel data doesn't match color data\n", fullname);
exit(1);
}
valToMem((unsigned long)a, dptr, image->pixlen);
dptr += image->pixlen;
}
if ((c= zgetc(zf)) != '"')
corrupted(fullname, zf);
}
zclose(zf);
return(image);
}
int xpixmapIdent(fullname, name)
char *fullname, *name;
{ Image *image;
if ((image= xpixmapLoad(fullname, name, (unsigned int)1))) {
freeImage(image);
return(1);
}
return(0);
}
|