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
|
/*
* fbm.c:
*
* adapted from code by Michael Mauldin, (mlm) at Carnegie-Mellon
* University, (fbm tools) and Kirk L. Johnson, (tuna@athena.mit.edu),
* (gif.c).
*
* fbmin.c
* Mark Majhor
* August 1990
*
* routines for reading FBM files
*
* Copyright 1990 Mark Majhor (see the included file
* "mrmcpyrght.h" for complete copyright information)
*/
# include <stdio.h>
# include <math.h>
# include <ctype.h>
# include "image.h"
# include "fbm.h"
/****
**
** local variables
**
****/
static BYTE file_open = 0; /* status flags */
static BYTE image_open = 0;
static ZFILE *ins; /* input stream */
static FBMFILEHDR phdr; /* header structure */
/****
**
** global variables
**
****/
static int fbmin_img_width; /* image width */
static int fbmin_img_height; /* image height */
static int fbmin_img_depth; /* image depth */
static int fbmin_img_bits; /* color bits */
static int fbmin_img_rowlen; /* length of one row of data */
static int fbmin_img_plnlen; /* length of one plane of data */
static int fbmin_img_clrlen; /* length of the colormap */
static double fbmin_img_aspect; /* image aspect ratio */
static int fbmin_img_physbits; /* physical bits per pixel */
static char *fbmin_img_title; /* name of image */
static char *fbmin_img_credit; /* credit for image */
static int fbmin_image_test()
{
if (fbmin_img_width < 1 || fbmin_img_width > 32767) {
fprintf (stderr, "Invalid width (%d) on input\n", fbmin_img_width);
return FBMIN_ERR_BAD_SD;
}
if (fbmin_img_height < 1 || fbmin_img_height > 32767) {
fprintf (stderr, "Invalid height (%d) on input\n", fbmin_img_height);
return (0);
}
if (fbmin_img_depth != 1 && fbmin_img_depth != 3) {
fprintf (stderr, "Invalid number of planes (%d) on input %s\n",
fbmin_img_depth, "(must be 1 or 3)");
return FBMIN_ERR_BAD_SD;
}
if (fbmin_img_bits < 1 || fbmin_img_bits > 8) {
fprintf (stderr, "Invalid number of bits (%d) on input %s\n",
fbmin_img_bits, "(must be [1..8])");
return FBMIN_ERR_BAD_SD;
}
if (fbmin_img_physbits != 1 && fbmin_img_physbits != 8) {
fprintf (stderr, "Invalid number of physbits (%d) on input %s\n",
fbmin_img_physbits, "(must be 1 or 8)");
return FBMIN_ERR_BAD_SD;
}
if (fbmin_img_rowlen < 1 || fbmin_img_rowlen > 32767) {
fprintf (stderr, "Invalid row length (%d) on input\n",
fbmin_img_rowlen);
return FBMIN_ERR_BAD_SD;
}
if (fbmin_img_depth > 1 && fbmin_img_plnlen < 1) {
fprintf (stderr, "Invalid plane length (%d) on input\n",
fbmin_img_plnlen);
return FBMIN_ERR_BAD_SD;
}
if (fbmin_img_aspect < 0.01 || fbmin_img_aspect > 100.0) {
fprintf (stderr, "Invalid aspect ratio %1.3f on input\n",
fbmin_img_aspect);
return FBMIN_ERR_BAD_SD;
}
return FBMIN_SUCCESS;
}
/*
* open FBM image in the input stream; returns FBMIN_SUCCESS if
* successful. (might also return various FBMIN_ERR codes.)
*/
static int fbmin_open_image(s)
ZFILE *s;
{
char *hp; /* header pointer */
/* make sure there isn't already a file open */
if (file_open)
return(FBMIN_ERR_FAO);
/* remember that we've got this file open */
file_open = 1;
ins = s;
/* read in the fbm file header */
hp = (char *) &phdr;
if (zread(ins, (byte *)hp, sizeof(phdr)) != sizeof(phdr))
return FBMIN_ERR_EOF;
if (strncmp(FBM_MAGIC, phdr.magic, sizeof(FBM_MAGIC)) != 0)
return FBMIN_ERR_BAD_SIG;
/* Now extract relevant features of FBM file header */
fbmin_img_width = atoi(phdr.cols);
fbmin_img_height = atoi(phdr.rows);
fbmin_img_depth = atoi(phdr.planes);
fbmin_img_bits = atoi(phdr.bits);
fbmin_img_rowlen = atoi(phdr.rowlen);
fbmin_img_plnlen = atoi(phdr.plnlen);
fbmin_img_clrlen = atoi(phdr.clrlen);
fbmin_img_aspect = atof(phdr.aspect);
fbmin_img_physbits = atoi(phdr.physbits);
fbmin_img_title = phdr.title;
fbmin_img_credit = phdr.credits;
if (fbmin_image_test() != FBMIN_SUCCESS)
return FBMIN_ERR_BAD_SD;
return FBMIN_SUCCESS;
}
/*
* close an open FBM file
*/
static int fbmin_close_file()
{
/* make sure there's a file open */
if (!file_open)
return FBMIN_ERR_NFO;
/* mark file (and image) as closed */
file_open = 0;
image_open = 0;
/* done! */
return FBMIN_SUCCESS;
}
#if 0
/*
* semi-graceful fatal error mechanism
*/
static fbmin_fatal(msg)
char *msg;
{
printf("Error reading FBM file: %s\n", msg);
exit(0);
}
#endif
/*
* these are the routines added for interfacing to xloadimage
*/
/*
* tell someone what the image we're loading is. this could be a little more
* descriptive but I don't care
*/
static void tellAboutImage(name)
char *name;
{
if (fbmin_img_clrlen > 0)
printf("%s is a %dx%d FBM image with %d colors\n", name,
fbmin_img_width, fbmin_img_height, fbmin_img_clrlen / 3);
else
printf("%s is a %dx%d FBM image with %d greyscale planes\n",
name, fbmin_img_width, fbmin_img_height, fbmin_img_bits);
}
Image *fbmLoad(fullname, name, verbose)
char *fullname, *name;
unsigned int verbose;
{
ZFILE *zf;
Image *image;
register int x, y, j, k, rowlen, plnlen;
unsigned char *pixptr, *cm;
unsigned int map_size;
unsigned char *r, *g, *b;
if (! (zf= zopen(fullname)))
return(NULL);
if (fbmin_open_image(zf) != FBMIN_SUCCESS) { /* read image header */
fbmin_close_file();
zclose(zf);
return(NULL);
}
if (verbose)
tellAboutImage(name);
znocache(zf);
image = newRGBImage(fbmin_img_width, fbmin_img_height, fbmin_img_bits);
/* if image has a local colormap, override global colormap
*/
if (fbmin_img_clrlen > 0) {
cm = (unsigned char *) lmalloc(fbmin_img_clrlen);
if (zread(ins, cm, fbmin_img_clrlen) != fbmin_img_clrlen) {
fprintf (stderr, "can't read colormap (%d bytes)\n", fbmin_img_clrlen);
return(NULL);
}
/*
* fbm color map is organized as
* buf[3][16]
*/
y = fbmin_img_clrlen / 3;
r = &cm[0], g = &cm[y], b = &cm[2 * y];
for (x = 0; x < y; x++, r++, g++, b++) {
image->rgb.red[x] = *r << 8;
image->rgb.green[x] = *g << 8;
image->rgb.blue[x] = *b << 8;
}
image->rgb.used = y;
} else if (fbmin_img_bits > 0) {
map_size = 3 * (int) pow(2.0, (double) fbmin_img_bits);
cm = (unsigned char *) lmalloc(map_size);
y = map_size / 3;
for (x = 0; x < y; x++) {
cm[x] = cm[y+x] = cm[2*y+x] = x;
}
r = &cm[0], g = &cm[y], b = &cm[2 * y];
for (x = 0; x < y; x++, r++, g++, b++) {
image->rgb.red[x] = *r << 8;
image->rgb.green[x] = *g << 8;
image->rgb.blue[x] = *b << 8;
}
image->rgb.used = y;
} else
cm = NULL;
rowlen = fbmin_img_rowlen;
plnlen = fbmin_img_plnlen;
for (k = 0; k < fbmin_img_depth; k++) {
pixptr = &(image->data[k * plnlen]);
for (j = 0; j < fbmin_img_height; j++, pixptr += rowlen) {
if (zread(ins, pixptr, rowlen) != rowlen) {
printf("%s: Short read within image data\n", fullname);
exit(1);
}
}
}
if (cm != NULL)
lfree(cm);
fbmin_close_file();
zclose(zf);
if (strlen(fbmin_img_title) != 0)
image->title= dupString(fbmin_img_title);
else
image->title= dupString(name);
return(image);
}
int fbmIdent(fullname, name)
char *fullname, *name;
{
ZFILE *zf;
unsigned int ret;
if (! (zf= zopen(fullname)))
return(0);
if (fbmin_open_image(zf) == FBMIN_SUCCESS) {
tellAboutImage(name);
ret = 1;
} else
ret = 0;
fbmin_close_file();
zclose(zf);
return(ret);
}
|