static void write_header(FILE * const fp, int const cols, int const rows, int const BitsPerPixel, int const Planes, struct pcxCmapEntry const cmap16[], unsigned int const xPos, unsigned int const yPos) { int i, BytesPerLine; Putbyte(PCX_MAGIC, fp); /* .PCX magic number */ Putbyte(0x05, fp); /* PC Paintbrush version */ Putbyte(0x01, fp); /* .PCX run length encoding */ Putbyte(BitsPerPixel, fp); /* bits per pixel */ Putword(xPos, fp); /* x1 - image left */ Putword(yPos, fp); /* y1 - image top */ Putword(xPos+cols-1, fp); /* x2 - image right */ Putword(yPos+rows-1, fp); /* y2 - image bottom */ Putword(cols, fp); /* horizontal resolution */ Putword(rows, fp); /* vertical resolution */ /* Write out the Color Map for images with 16 colors or less */ if (cmap16) for (i = 0; i < 16; ++i) { Putbyte(cmap16[i].r, fp); Putbyte(cmap16[i].g, fp); Putbyte(cmap16[i].b, fp); } else { unsigned int i; for (i = 0; i < 16; ++i) { Putbyte(0, fp); Putbyte(0, fp); Putbyte(0, fp); } } Putbyte(0, fp); /* reserved byte */ Putbyte(Planes, fp); /* number of color planes */ BytesPerLine = ((cols * BitsPerPixel) + 7) / 8; Putword(BytesPerLine, fp); /* number of bytes per scanline */ Putword(1, fp); /* palette info */ { unsigned int i; for (i = 0; i < 58; ++i) /* fill to end of header */ Putbyte(0, fp); } }
/* * public GIFEncode */ static void GIFEncode( FILE *fp, int GWidth, int GHeight, int GInterlace, int Background, int BitsPerPixel, int Red[], int Green[], int Blue[], ifun_t* GetPixel ) { int B; int RWidth, RHeight; int LeftOfs, TopOfs; int Resolution; int ColorMapSize; int InitCodeSize; int i; cur_accum = 0; /* globals */ cur_bits = 0; Interlace = GInterlace; ColorMapSize = 1 << BitsPerPixel; RWidth = Width = GWidth; RHeight = Height = GHeight; LeftOfs = TopOfs = 0; Resolution = BitsPerPixel; CountDown = (long)Width * (long)Height; Pass = 0; if( BitsPerPixel <= 1 ) InitCodeSize = 2; else InitCodeSize = BitsPerPixel; curx = cury = 0; fwrite( "GIF87a", 1, 6, fp ); Putword( RWidth, fp ); Putword( RHeight, fp ); B = 0x80; /* Yes, there is a color map */ B |= (Resolution - 1) << 5; B |= (BitsPerPixel - 1); fputc( B, fp ); fputc( Background, fp ); fputc( 0, fp ); for( i=0; i<ColorMapSize; i++ ) { fputc( Red[i], fp ); fputc( Green[i], fp ); fputc( Blue[i], fp ); } fputc( ',', fp ); Putword( LeftOfs, fp ); Putword( TopOfs, fp ); Putword( Width, fp ); Putword( Height, fp ); if( Interlace ) fputc( 0x40, fp ); else fputc( 0x00, fp ); fputc( InitCodeSize, fp ); compress( InitCodeSize+1, fp, GetPixel ); fputc( 0, fp ); fputc( ';', fp ); fclose( fp ); }