GIF file converter
Paul Haeberli
paul at manray.sgi.com
Tue Oct 3 09:36:03 AEST 1989
/*
* fromsun.c -
* convert a sun image file to Iris format.
*
* This program should handle 1-bit, 8-bit and 24-bit sun rasterfiles.
* Please mail paul at sgi.com if you have a problem rasterfile. This
* program will not work on run length encoded raster files yet,
* send me info and I might make it work for you. . . .
*
* To use:
* 1. copy /usr/include/rasterfile.h from a sun system
* 2. cc -I/usr/inlcude/gl fromsun.c -o fromsun -limage
* 3. to convert: fromsun blat.im8 t.rgb
* 4. to display: ipaste t.rgb
*
* Paul Haeberli at Silicon Graphics - 1989
*
*/
#include "image.h"
#include "rasterfile.h"
#define MAXWIDTH 4096
char cbuf[3*MAXWIDTH];
short rbuf[MAXWIDTH];
short gbuf[MAXWIDTH];
short bbuf[MAXWIDTH];
unsigned char rmap[256];
unsigned char gmap[256];
unsigned char bmap[256];
main(argc,argv)
int argc;
char **argv;
{
IMAGE *image;
FILE *inf;
int xsize, ysize, zsize, rowbytes;
int y, depth, maplen;
struct rasterfile hdr;
if(argc<3) {
fprintf(stderr,"usage: fromsun image.im8 outimage\n");
exit(1);
}
inf = fopen(argv[1],"r");
if(!inf) {
fprintf(stderr,"fromsun: can't open %s\n",argv[1]);
exit(1);
}
fread(&hdr,1,sizeof(hdr),inf);
hdr.ras_magic = RAS_MAGIC;
xsize = hdr.ras_width;
ysize = hdr.ras_height;
depth = hdr.ras_depth;
if(depth != 8 && depth != 24 && depth != 1) {
fprintf(stderr,"fromsun: bad ras_depth is %d\n",hdr.ras_depth);
exit(1);
}
rowbytes = hdr.ras_length/ysize;
switch(hdr.ras_type) {
case RT_OLD:
hdr.ras_length = ysize*linebytes(xsize,depth);
rowbytes = hdr.ras_length/ysize;
break;
case RT_STANDARD:
rowbytes = hdr.ras_length/ysize;
break;
case RT_BYTE_ENCODED:
fprintf(stderr,"fromsun: don't know about RT_BYTE_ENCODED\n");
exit(1);
break;
default:
fprintf(stderr,"fromsun: bad ras_type is %d\n",hdr.ras_type);
exit(1);
break;
}
maplen = hdr.ras_maplength;
if(maplen == 0 && depth == 8) {
fprintf(stderr,"fromsun: no map on 8 bit image\n");
exit(1);
}
if(maplen > 0) {
fread(rmap,maplen/3,1,inf);
fread(gmap,maplen/3,1,inf);
fread(bmap,maplen/3,1,inf);
}
if(depth == 1)
image = iopen(argv[2],"w",RLE(1),3,xsize,ysize,1);
else
image = iopen(argv[2],"w",RLE(1),3,xsize,ysize,3);
for(y=0; y<ysize; y++) {
switch(depth) {
case 1:
fread(cbuf,rowbytes,1,inf);
expand1(cbuf,rbuf,xsize);
putrow(image,rbuf,(ysize-1)-y,0);
break;
case 8:
fread(cbuf,xsize,1,inf);
cvtrow8(cbuf,rbuf,gbuf,bbuf,xsize);
putrow(image,rbuf,(ysize-1)-y,0);
putrow(image,gbuf,(ysize-1)-y,1);
putrow(image,bbuf,(ysize-1)-y,2);
break;
case 24:
fread(cbuf,3*xsize,1,inf);
unswizzle24(cbuf,rbuf,gbuf,bbuf,xsize);
putrow(image,rbuf,(ysize-1)-y,0);
putrow(image,gbuf,(ysize-1)-y,1);
putrow(image,bbuf,(ysize-1)-y,2);
break;
}
}
iclose(image);
exit(0);
}
cvtrow8(cptr,rptr,gptr,bptr,n)
register unsigned char *cptr;
register unsigned short *rptr, *gptr, *bptr;
register int n;
{
while(n--) {
*rptr++ = rmap[*cptr];
*gptr++ = gmap[*cptr];
*bptr++ = bmap[*cptr++];
}
}
unswizzle24(cptr,rptr,gptr,bptr,n)
register unsigned char *cptr;
register unsigned short *rptr, *gptr, *bptr;
register int n;
{
while(n--) {
*rptr++ = *cptr++;
*gptr++ = *cptr++;
*bptr++ = *cptr++;
}
}
expand1(cptr,sptr,n)
register unsigned char *cptr;
register unsigned short *sptr;
register int n;
{
register int i;
while(n>0) {
if(*cptr & 0x80)
*sptr++ = 0;
else
*sptr++ = 255;
if(*cptr & 0x40)
*sptr++ = 0;
else
*sptr++ = 255;
if(*cptr & 0x20)
*sptr++ = 0;
else
*sptr++ = 255;
if(*cptr & 0x10)
*sptr++ = 0;
else
*sptr++ = 255;
if(*cptr & 0x08)
*sptr++ = 0;
else
*sptr++ = 255;
if(*cptr & 0x04)
*sptr++ = 0;
else
*sptr++ = 255;
if(*cptr & 0x02)
*sptr++ = 0;
else
*sptr++ = 255;
if(*cptr & 0x01)
*sptr++ = 0;
else
*sptr++ = 255;
cptr++;
n -= 8;
}
}
linebytes(xsize,depth)
int xsize, depth;
{
return (( ((xsize*depth) + (16-1)) >> 3) &~ 1);
}
More information about the Comp.sys.sgi
mailing list