12 /* color to grey conversion methods */
21 #define MakeID(a,b,c,d) ( ((a)<<24) | ((b)<<16) | ((c)<<8) | (d) )
23 #define ID_FORM MakeID('F','O','R','M')
24 #define ID_ILBM MakeID('I','L','B','M')
25 #define ID_BMHD MakeID('B','M','H','D')
26 #define ID_CMAP MakeID('C','M','A','P')
27 #define ID_CAMG MakeID('C','A','M','G')
28 #define ID_BODY MakeID('B','O','D','Y')
30 #define ROUNDUP(x) ( ((x)+1) & (~1L) )
35 unsigned long Id
, Size
;
38 static unsigned char cmap
[32][3];
42 char npl
,mask
,compress
,pad1
;
43 short transparentColor
;
45 short pageWidth
,pageHeight
;
49 static short Color
[32], /* output colors */
50 Method
= AVERAGE
; /* color conversion */
51 static short numcolors
;
54 FILE *fin
; /* input file */
57 static void ReadBMHD();
58 static void ReadChunk();
59 static void RastOut();
61 static char GetIlbmVal();
62 static void InitColorMappings();
63 static short Convert();
64 static void ReadDecomLine();
65 static void ProcessRows();
67 #define ABORT(str) { OutErr(str); goto ErrorExit; }
70 void SetGreyModel( model
)
77 * main routine for reading in an iff file
80 void ReadIlbm(filename
)
85 unsigned char cmapFlag
= FALSE
,
89 if ((fin
= fopen (filename
, "r")) == NULL
) {
90 OutErr ("ERROR: cannot open input file");
93 /* read in iff file */
96 if (Chunk
.Id
!= ID_FORM
)
97 ABORT ("Not an IFF File");
100 if (ILBMid
!= ID_ILBM
)
101 ABORT ("Not an ILBM File");
108 if (Chunk
.Id
== ID_BODY
)
112 ABORT("reached end of file without seeing body\n");
117 FRead (cmap
, Chunk
.Size
);
118 numcolors
= Chunk
.Size
/3;
126 FRead( &camgdata
, sizeof(camgdata
) );
128 default: /* unknown identifier */
129 fseek( fin
, Chunk
.Size
, 1);
134 ABORT("IFF file does not contain a CMAP chunk before the BODY\n");
138 ABORT("IFF file does not contain a BMHD chunk before the BODY\n");
142 if( OpenImgPix( bmhd
.w
, bmhd
.h
, Convert(0xf, 0xf, 0xf)) ) {
150 static void ProcessRows(bmhd
)
155 int depth
, i
, v
, pixwidth
;
160 BytePerLine
=(pixwidth
+7) / 8;
163 Raster
= (char *) malloc (BytePerLine
* depth
);
166 OutErr("ProcessRows:could not allocate Raster");
170 for( i
= 0; i
< depth
; i
++ ) {
171 rastlist
[i
] = Raster
+ BytePerLine
*i
;
174 for( v
= 0; v
< bmhd
->h
; v
++) {
175 switch (bmhd
->compress
) {
177 FRead (Raster
, BytePerLine
* depth
);
180 for( i
= 0; i
< depth
; i
++) {
181 ReadDecomLine( BytePerLine
, rastlist
[i
]);
185 ABORT ("Unknown Compression type in BODY");
189 HamOut( rastlist
, pixwidth
, v
);
192 RastOut( rastlist
, pixwidth
, v
, depth
);
197 if( Raster
) free(Raster
);
201 static void ReadDecomLine(linebytes
, rp
)
219 runlen
= -runlen
+ 1;
230 * Convert - convert (r,g,b) to hex greyscale.
233 static short Convert(r
,g
,b
)
242 /* convert color according to 'Method' */
244 case AVERAGE
: /* average r,g,b to obtain grey level */
245 return ((short)((r
+ g
+ b
) / 3));
246 case LUMIN
: /* use NTSC luminescence as grey level */
247 return ((short)((r
* 30 + g
* 59 + b
* 11) / 100));
248 case DIST
: /* use grey with minimum distance in color */
250 for( i
= 0; i
< numcolors
; i
++ ) {
254 dist
= rd
* rd
+ gd
* gd
+ bd
* bd
;
256 min
= dist
; best
= i
;
267 exit(-1); /* error, big one */
271 static void InitColorMappings()
275 /* put colors in 4-bit range and Convert */
276 for (i
= 0; i
< 32; i
++) {
280 Color
[i
] = Convert (cmap
[i
][RED
], cmap
[i
][GRN
], cmap
[i
][BLU
]);
286 * leftmost pixel of byte is in most significant bit of byte
288 static char GetIlbmVal( rastlist
, h
, bpp
)
296 mask
= 0x80 >> ( h
& 7);
299 for( i
= bpp
-1; i
>= 0; i
-- ) {
301 value
|= (*(rastlist
[i
]+bytep
) & mask
) ? 1: 0;
307 * HamOut - output ham image in hex.
309 static void HamOut(rastlist
, pixwidth
, v
)
313 unsigned char lastred
= 0,
318 for( h
= 0; h
<pixwidth
; h
++ ) {
321 shade
= GetIlbmVal(rastlist
, h
, 6);
322 pixval
= shade
& 0x0F;
323 switch (shade
& 0x30) {
325 lastred
= cmap
[(int)pixval
][RED
];
326 lastgreen
= cmap
[(int)pixval
][GRN
];
327 lastblue
= cmap
[(int)pixval
][BLU
];
328 shade
= Color
[(int)pixval
];
332 shade
= Convert(lastred
,lastgreen
,lastblue
);
336 shade
= Convert(lastred
,lastgreen
,lastblue
);
340 shade
= Convert(lastred
,lastgreen
,lastblue
);
342 SetImgPix(h
, v
, shade
);
347 * RastOut - handle normal bit mapped images
349 static void RastOut(rastlist
, pixwidth
, v
, depth
)
351 int pixwidth
, v
, depth
;
354 for( h
= 0; h
< pixwidth
; h
++ ) {
356 shade
= Color
[(int)GetIlbmVal( rastlist
, h
, depth
)];
357 SetImgPix( h
, v
, shade
);
362 * ReadChunk - read in an IFF Chunk.
365 static void ReadChunk()
367 FRead (&Chunk
, sizeof (Chunk
));
372 * ReadBMHD - read the BMHD structure.
375 static void ReadBMHD(bmhd
)
378 FRead (bmhd
, Chunk
.Size
);
383 * FRead - read 'len' bytes to 'pointer' while checking for an error.
386 static void FRead(pointer
,len
)
390 if (fread (pointer
, len
, 1, fin
) == 0) {
392 sprintf(outbuff
,"Fread Error in reading input file at %d ", (int)ftell(fin
));