home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************
- * Copyright (c) 1987 *
- * by CompuServe Inc, Columbus, Ohio. All Rights Reserved *
- ***********************************************************/
-
- /*
- * IFF_TO_GIF
- *
- * Usage: IFF_TO_GIF <input filename>
- * Output file: <input filename>.GIF
- */
-
- #include <exec/types.h>
- #include <exec/memory.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include "buildgif.h"
-
- #define UBYTE unsigned char
- #define WORD short
- #define UWORD unsigned short
-
- #define mskNone 0
- #define mskHasMask 1
- #define mskHasTransparentColor 2
- #define mskLasso 3
- #define cmpNone 0
- #define cmpByteRun1 1
- #define MakeID(a, b, c, d) ( (a)<<24 | (b)<<16 | (c)<<8 | (d) )
- #define FORM MakeID('F', 'O', 'R', 'M')
- #define LIST MakeID('L', 'I', 'S', 'T')
- #define BODY MakeID('B', 'O', 'D', 'Y')
- #define BMHD MakeID('B', 'M', 'H', 'D')
- #define CMAP MakeID('C', 'M', 'A', 'P')
- #define ILBM MakeID('I', 'L', 'B', 'M')
- #define MILLION 1000000
- #define true -1
- #define false 0
-
- ULONG class, type;
- UBYTE *buffer, *tempbuffer, **psource, **pdest;
- UBYTE *filebuffer, *tempfilebuffer, *savebuffer;
- UWORD BytesPerPlane, plane, BytesPerRow, pixcol, pixrow;
- ULONG pixel_count;
- long TotalBytes;
- int status, i, shift;
-
- #define output_buffer_size (512*16)
-
- static long input_file, output_file;
- static unsigned char output_buffer[output_buffer_size];
- static unsigned char *output_bufptr;
- static short bytes_collected;
-
- typedef unsigned long ID;
-
- struct Chunk
- {
- ID ckID ;
- long ckSize ;
- };
-
- struct Chunk chk;
-
- typedef UBYTE Masking;
- typedef UBYTE Compression;
-
- struct BitMapHeader
- {
- UWORD w, h;
- WORD x, y;
- UBYTE nPlanes;
- Masking masking;
- Compression compression;
- UBYTE pad1;
- UWORD transparentColor;
- UBYTE xAspect, yAspect;
- WORD pageWidth, pageHeight;
- };
-
- struct BitMapHeader bmhd;
-
- #define UGetByte() (*source++)
- #define UPutByte(c) (*dest++ = (c))
-
- /* Interlace Tables */
-
- static UWORD
- base_row[4] = {0, 4, 2, 1},
- row_disp[4] = {8, 8, 4, 2},
- interlaced_mode = 0,
- interlace_pass = 0;
-
- GetBits(psource, pdest, dstBytes0)
- BYTE **psource, **pdest; WORD dstBytes0;
- {
- register BYTE *source = *psource;
- register BYTE *dest = *pdest;
- register WORD dstBytes = dstBytes0;
-
- while(dstBytes>0)
- {
- UPutByte(UGetByte());
- dstBytes--;
- }
-
- *psource = source;
- *pdest = dest;
- }
-
-
- UnPackBits(psource, pdest, dstBytes0)
- BYTE **psource, **pdest; WORD dstBytes0;
- {
- register BYTE *source = *psource;
- register BYTE *dest = *pdest;
- register BYTE n;
- register BYTE c;
- register WORD dstBytes = dstBytes0;
- WORD minus128 = -128;
-
- while(dstBytes > 0)
- {
- n = UGetByte();
-
- if (n >= 0)
- {
- n++;
- dstBytes -= n;
- do UPutByte(UGetByte()); while (--n > 0);
- }
- else if (n != minus128)
- {
- n = -n + 1;
- dstBytes -= n;
- c = UGetByte();
- do UPutByte(c); while (--n >0);
- }
- }
-
- *psource = source;
- *pdest = dest;
- }
-
- short Write_Byte(pbyte)
- UBYTE pbyte;
- {
- if (bytes_collected == output_buffer_size)
- {
- if (Write(output_file, output_buffer, output_buffer_size) != output_buffer_size)
- return -5;
-
- bytes_collected = 0;
- }
-
- output_buffer[bytes_collected++] = pbyte;
- return 0;
- }
-
-
- OutputUsage (Name)
- char *Name;
- {
- printf ("usage: iff2gif [-iv] input-filename [output-filename]\n");
- printf (" where -i = interlaced mode\n");
- printf (" -v = verbose\n");
- printf (" default output-file = input-file.GIF\n");
- exit(0);
- }
-
- short Read_Pixel()
- {
- register short pixel, plane;
- register UBYTE *buf, mask;;
-
- if (pixel_count == 0)
- return -1;
-
- pixel = 0;
- buf = &buffer[pixrow*BytesPerRow + (pixcol >> 3)];
- mask = 1 << (7 - (pixcol & 7));
-
- for (plane = 0; plane < bmhd.nPlanes; plane++)
- {
- if (*buf & mask) pixel |= 1 << plane;
- buf += BytesPerPlane;
- }
-
- pixcol++;;
-
- if (pixcol == bmhd.w)
- {
- pixcol = 0;
-
- if (interlaced_mode)
- {
- pixrow += row_disp[interlace_pass];
-
- if (pixrow >= bmhd.h)
- {
- interlace_pass++;
- pixrow = base_row[interlace_pass];
- }
- }
- else
- pixrow++;
- }
-
- pixel_count--;
- return pixel;
- }
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- char *infile, *outfile, *FlagArguments;
- char *malloc(), *strcat();
- int numColors, temp, ArgumentIndex;
-
- struct ColorEntry color_map[32];
-
- UBYTE
- VerboseFlag = false;
-
- struct ColorRegister {
- UBYTE red, green, blue;
- };
-
- struct ColorRegister ColorMap[32];
- long pos;
-
- /* Evaluate arguments */
-
- if (argc == 1)
- OutputUsage(argv[0]);
-
- FlagArguments = argv[1];
-
- if (FlagArguments[0] == '-')
- {
- int i;
-
- i = 1;
-
- while (FlagArguments[i] != '\0')
- {
- switch (FlagArguments[i])
- {
- case 'i':
- case 'I':
- interlaced_mode = 1;
- break;
-
- case 'v':
- case 'V':
- VerboseFlag = true;
- break;
-
- default:
- printf ("%c is an invalid flag!\n", FlagArguments[i]);
- }
-
- i++;
- }
-
- ArgumentIndex = 2;
- }
- else
- ArgumentIndex = 1;
-
- if ((ArgumentIndex + 1) <= argc)
- {
- infile = argv[ArgumentIndex];
- ArgumentIndex += 1;
- }
- else
- OutputUsage(argv[0]);
-
- if ((ArgumentIndex + 1) <= argc)
- outfile = argv[ArgumentIndex];
- else
- {
- outfile = (char *)malloc (strlen(infile) + 5);
- strcpy (outfile, infile);
- outfile = strcat(outfile, ".GIF");
- }
-
- input_file = Open(infile, 1005);
-
- if (input_file == 0)
- {
- printf("input file open failed\n");
- exit(0);
- }
-
- output_file = Open(outfile, 1006);
-
- if (output_file == 0)
- {
- printf("output file open failed\n");
- Close(input_file);
- exit(0);
- }
-
- status = Read(input_file, &chk, sizeof(chk)); /* check to see if FORM type */
-
- if (status == -1)
- {
- printf("read failed\n");
- goto CleanUp;
- }
-
- if (chk.ckID != FORM)
- {
- printf("not a picture file\n");
- goto CleanUp;
- }
-
- status = Read(input_file, &type, 4); /* get bit map header */
-
- if (status == -1)
- {
- printf("read failed\n");
- goto CleanUp;
- }
-
- if (type != ILBM)
- {
- printf("not a picture file\n");
- goto CleanUp;
- }
-
- status = Read(input_file, &chk, sizeof(chk));
-
- if (chk.ckID != BMHD)
- {
- printf("could not find bit map header\n");
- goto CleanUp;
- }
-
- status = Read(input_file, &bmhd, sizeof(bmhd)); /* get bmhd info */
-
- if (status == -1)
- {
- printf("read failed\n");
- goto CleanUp;
- }
-
- status = Read(input_file, &chk, sizeof(chk));
-
- while (chk.ckID != CMAP)
- {
- pos = Seek(input_file, chk.ckSize, 0);
-
- if (pos == -1)
- {
- printf("seek failed\n");
- exit(0);
- }
-
- status = Read(input_file, &chk, sizeof(chk));
- }
-
- /* we have got the cmap!! */
-
- numColors = chk.ckSize/3;
-
- for (i = 0; i < numColors; i++)
- status = Read(input_file, &ColorMap[i], 3);
-
- /* right justify color map entires */
-
- for (i = 0; i < numColors; i++)
- {
- color_map[i].red = ColorMap[i].red >> 4;
- color_map[i].blue = ColorMap[i].blue >> 4;
- color_map[i].green = ColorMap[i].green >> 4;
- }
-
- /* skip over everything else until body is found */
-
- status = Read(input_file, &chk, sizeof(chk));
-
- while (chk.ckID != BODY)
- {
- pos = Seek(input_file, chk.ckSize, 0);
-
- if (pos == -1)
- {
- printf("seek failed\n");
- exit(0);
- }
-
- status = Read(input_file, &chk, sizeof(chk));
- }
-
- /* we have got the body!! */
-
- BytesPerRow = ((bmhd.w + 15) >> 4) << 1;
- BytesPerPlane = BytesPerRow*bmhd.h;
- TotalBytes = BytesPerPlane*(UWORD)bmhd.nPlanes;
- buffer = (UBYTE *)malloc(TotalBytes);
-
- if (buffer == NULL)
- {
- printf("no memory for buffer\n");
- exit(0);
- }
-
- filebuffer = (UBYTE *)malloc(chk.ckSize);
-
- if (filebuffer == NULL)
- {
- printf("no memory for file buffer\n");
- exit(0);
- }
-
- savebuffer = buffer;
- tempfilebuffer = filebuffer;
- status = Read(input_file, filebuffer, chk.ckSize);
- pdest = &tempbuffer;
- psource = &tempfilebuffer;
-
- for (i = 0; i < bmhd.h; i++)
- {
- for (plane = 0; plane < bmhd.nPlanes; plane++)
- {
- tempbuffer = buffer + (BytesPerRow*i) + (BytesPerPlane*plane);
-
- if (bmhd.compression == cmpByteRun1)
- UnPackBits(psource, pdest, BytesPerRow);
- else
- GetBits(psource, pdest, BytesPerRow);
- }
- }
-
- if (VerboseFlag)
- {
- printf("Converting IFF file %s.\n", argv[1]);
- printf(" Image size(width,height) = (%d,%d)\n", bmhd.w, bmhd.h);
- printf(" Page size(width,height) = (%d,%d)\n",
- bmhd.pageWidth, bmhd.pageHeight);
- printf(" Position (x,y) = (%d,%d)\n", bmhd.x, bmhd.y);
- printf(" Aspect (x,y) = (%d,%d)\n", bmhd.xAspect, bmhd.yAspect);
- printf(" Number of planes = %d\n", bmhd.nPlanes);
- printf(" Masking = %d\n", bmhd.masking);
- printf(" Compression = %d\n", bmhd.compression);
- printf(" Transparent color = %d\n", bmhd.transparentColor);
- }
-
- bytes_collected = 0;
- pixrow = 0;
- pixcol = 0;
- pixel_count = bmhd.w*bmhd.h;
-
- status = Create_GIF(Read_Pixel, Write_Byte, bmhd.w, bmhd.h,
- 4, interlaced_mode, bmhd.nPlanes, color_map);
-
- Write(output_file, output_buffer, bytes_collected);
- printf("Conversion completed.\n");
-
- CleanUp:
- Close(input_file);
- Close(output_file);
- exit(0);
- }
-