[comp.sys.sgi] GIF file converter

S090726@UMRVMA.UMR.EDU ("Bob B. Funchess") (09/30/89)

I recently posted a note wanting a rgb to GIF file converter.  Since there was
no response, I wrote my own...  well, actually, I used large chunks of existing
code, and just interfaced that with the SGI rgb file format.

Since I've done this, and I hate to see anyone reinvent the wheel, I will
provide source (in C) to anyone wanting it.  I would love to get improved
versions back, this is my first C program.

Now if someone would provide an easy way to turn .im8 files into rgb...


                             < Bob  S090726@UMRVMA.UMR.EDU  Funchess >

paul@manray.sgi.com (Paul Haeberli) (10/03/89)

/*
 *	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@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@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);
}

jh34607@suntc.UUCP (john howell) (10/03/89)

In article <42429@sgi.sgi.com>, paul@manray.sgi.com (Paul Haeberli) writes:
> 
> /*
>  *	fromsun.c -
>  *		convert a sun image file to Iris format.  
>  *

Fantastic!!  Now .. does anyone have a "tosun" program to move Iris
imagefiles to a Sun Rasterfile format?

msc@ramoth.esd.sgi.com (Mark Callow) (10/04/89)

In article <125@suntc.UUCP>, jh34607@suntc.UUCP (john howell) writes: 
> Fantastic!!  Now .. does anyone have a "tosun" program to move Iris
> imagefiles to a Sun Rasterfile format?

It's in 4Dgifts, called tonews.

--
From the TARDIS of Mark Callow
msc@ramoth.sgi.com, ...{ames,decwrl,sun}!sgi!msc
"There is much virtue in a window.  It is to a human being as a frame is to
a painting, as a proscenium to a play.  It strongly defines its content."

jh34607@suntc.UUCP (john howell) (10/05/89)

In article <771@odin.SGI.COM>, msc@ramoth.esd.sgi.com (Mark Callow) writes:
> In article <125@suntc.UUCP>, jh34607@suntc.UUCP (john howell) writes: 
> > Fantastic!!  Now .. does anyone have a "tosun" program to move Iris
> > imagefiles to a Sun Rasterfile format?
> 
> It's in 4Dgifts, called tonews.
> 
> --
> From the TARDIS of Mark Callow
> msc@ramoth.sgi.com, ...{ames,decwrl,sun}!sgi!msc
> "There is much virtue in a window.  It is to a human being as a frame is to
> a painting, as a proscenium to a play.  It strongly defines its content."

I am aware that "tonews" exists, but I want to make Sun raster files
that can be used on pre-NeWS suns (like pretty much all of them).  Isn't
the native Sun raster image file format a different beast than NeWS?

 

msc@ramoth.esd.sgi.com (Mark Callow) (10/06/89)

In article <128@suntc.UUCP>, jh34607@suntc.UUCP (john howell) writes:
> I am aware that "tonews" exists, but I want to make Sun raster files
> that can be used on pre-NeWS suns (like pretty much all of them).  Isn't
> the native Sun raster image file format a different beast than NeWS?

As far as I'm aware, they are the same.  We certainly use the same rasterfile.h
file that is (or was when we received the source code) shipped with
SunOS/SunWindows.
If tonews has a limitation it's that it only knows how to make .im8 files.
--
From the TARDIS of Mark Callow
msc@ramoth.sgi.com, ...{ames,decwrl,sun}!sgi!msc
"There is much virtue in a window.  It is to a human being as a frame is to
a painting, as a proscenium to a play.  It strongly defines its content."