From 8acd9a15490f3c14c7256f0701a154d16ed31cc9 Mon Sep 17 00:00:00 2001 From: gered Date: Sat, 2 Nov 2019 17:35:48 -0400 Subject: [PATCH] replace ackiff.c and ackpcx.c with the faster borland dos versions these were on the book cd under /ACK/DOS/BORLAND. this version of ackiff.c is SIGNIFICANTLY faster then the windows one ported back to dos (i don't think i did anything wrong when i did that, but who knows...) --- ack_lib/ACKIFF.C | 291 ++++++++++++++++++++++++----------------------- ack_lib/ACKPCX.C | 144 +++++++++++------------ 2 files changed, 216 insertions(+), 219 deletions(-) diff --git a/ack_lib/ACKIFF.C b/ack_lib/ACKIFF.C index 59f97d9..131d56e 100644 --- a/ack_lib/ACKIFF.C +++ b/ack_lib/ACKIFF.C @@ -21,21 +21,29 @@ #include "ackext.h" #include "iff.h" -extern int errno; - extern char rsName[]; -unsigned char colordat[768]; // maximum it can be...256 colors +unsigned char colordat[768]; /* maximum it can be...256 colors */ -unsigned char cplanes[8][80]; // setting max at 640 pixels width - // thats 8 pixels per byte per plane -unsigned char *pplanes= &cplanes[0][0]; // for a form pbm +unsigned char cplanes[8][80]; /* setting max at 640 pixels width */ + /* thats 8 pixels per byte per plane */ +unsigned char *pplanes= &cplanes[0][0]; /* for a form pbm */ #define MAX_BUF_POS 4096 - int rdbufpos; - int rdSize; - UCHAR rdBuffer[MAX_BUF_POS+2]; + int rdbufpos; + int rdSize; + UCHAR rdBuffer[MAX_BUF_POS+2]; + +short ReadNxtBlock(short handle) +{ + short retlen; + +retlen = read(handle,rdBuffer,MAX_BUF_POS); +rdbufpos = 0; + +return(retlen); +} //============================================================================= // @@ -48,19 +56,21 @@ if (rsHandle) { rsHandle = open(rsName,O_RDONLY|O_BINARY); if (rsHandle < 1) - rsHandle = 0; + rsHandle = 0; } } + unsigned char *AckReadiff(char *picname) { - int handle; + FILE *pic; + short handle; form_chunk fchunk; ChunkHeader chunk; BitMapHeader bmhd; - long length,fpos; - char value; // must remain signed, no matter what. ignore any warnings. + long length; + char value; /* must remain signed, no matter what. ignore any warnings. */ short sofar; short width,height,planes; short pixw; @@ -70,142 +80,144 @@ unsigned char *AckReadiff(char *picname) rdSize = MAX_BUF_POS; if (!rsHandle) - handle = open(picname,O_RDONLY|O_BINARY); + { + if ((pic = fopen(picname,"rb")) == NULL) + { + ErrorCode = ERR_BADPICNAME; + return(0L); + } + } else + { + pic = fdopen(rsHandle,"rb"); + if (pic == NULL) { - handle = rsHandle; - lseek(rsHandle,rbaTable[(ULONG)picname],SEEK_SET); + ErrorCode = ERR_BADPICNAME; + return(0L); } - read(handle,&fchunk,sizeof(form_chunk)); + fseek(pic,rbaTable[(ULONG)picname],SEEK_SET); + } + fread(&fchunk,1,sizeof(form_chunk),pic); /* read in the first 12 bytes*/ - if (fchunk.type != FORM) - { - if (!rsHandle) - close(handle); - - ErrorCode = ERR_INVALIDFORM; - return(0L); - } - - if (fchunk.subtype != ID_PBM) - { - if (!rsHandle) - close(handle); - ErrorCode = ERR_NOPBM; - return(0L); - } - // now lets loop...Because the Chunks can be in any order! - while(1) - { - read(handle,&chunk,sizeof(ChunkHeader)); - chunk.ckSize = ByteFlipLong(chunk.ckSize); - if (chunk.ckSize & 1) chunk.ckSize ++; // must be word aligned - if(chunk.ckID == ID_BMHD) - { - read(handle,&bmhd,sizeof(BitMapHeader)); - bmhd.w=iffswab(bmhd.w); // the only things we need. - bmhd.h=iffswab(bmhd.h); - destx = (unsigned char *)AckMalloc((bmhd.w * bmhd.h)+4); - if ( !destx ) - { - if (!rsHandle) - close(handle); - ErrorCode = ERR_NOMEMORY; - return(0L); - } - - savedestx = destx; - - destx[0] = bmhd.w%256; - destx[1] = bmhd.w/256; - destx[2] = bmhd.h%256; - destx[3] = bmhd.h/256; - destx += 4; - continue; - } - if(chunk.ckID == ID_CMAP) - { - short i; - unsigned char r,g; - - read(handle,colordat,chunk.ckSize); - for (i=0;i<768;i++) - { - r = colordat[i]; // r,g do not stand for red and green - g = r >> 2; - colordat[i] = g; - } - continue; - } - if(chunk.ckID == ID_BODY) - { - for(height = 0; height 0) - { - short len; - len = value +1; - sofar -= len; - if (!(read(handle,dest,len))) - { - if (!rsHandle) - close(handle); - ErrorCode = ERR_BADPICFILE; - AckFree(savedestx); - return(0L); - } - dest +=len; - } - else - { - short count; - count = -value; // get amount to dup - count ++; - sofar -= count; - value = 0; - read(handle,&value,1); - while (--count >= 0) *dest++ = value; - } - } - else - { - read(handle,dest,sofar); - sofar = 0; - } - } - if (sofar < 0) - { - if (!rsHandle) - close(handle); - } - memcpy(destx,pplanes,bmhd.w); - destx += bmhd.w; - } - break; // leave if we've unpacked the BODY - } - - lseek(handle,chunk.ckSize,SEEK_CUR); - } - - if (!rsHandle) - close(handle); - return((char *)savedestx); + if (fchunk.type != FORM) + { + CloseFile(pic); + ErrorCode = ERR_INVALIDFORM; + return(0L); } + if (fchunk.subtype != ID_PBM) + { + CloseFile(pic); + ErrorCode = ERR_NOPBM; + return(0L); + } + /* now lets loop...Because the Chunks can be in any order! */ + while(1) + { + fread(&chunk,1,sizeof(ChunkHeader),pic); + chunk.ckSize = ByteFlipLong(chunk.ckSize); + if (chunk.ckSize & 1) chunk.ckSize ++; /* must be word aligned */ + if(chunk.ckID == ID_BMHD) + { + fread(&bmhd,1,sizeof(BitMapHeader),pic); + bmhd.w=iffswab(bmhd.w); /* the only things we need. */ + bmhd.h=iffswab(bmhd.h); + destx = (unsigned char *)AckMalloc((bmhd.w * bmhd.h)+4); + if ( !destx ) + { + CloseFile(pic); + ErrorCode = ERR_NOMEMORY; + return(0L); + } + + savedestx = destx; + + destx[0] = bmhd.w%256; + destx[1] = bmhd.w/256; + destx[2] = bmhd.h%256; + destx[3] = bmhd.h/256; + destx += 4; + continue; + } + if(chunk.ckID == ID_CMAP) + { + short i; + unsigned char r,g; + + fread(colordat,1,chunk.ckSize,pic); + for (i=0;i<768;i++) + { + r = colordat[i]; /* r,g do not stand for red and green */ + g = r >> 2; + colordat[i] = g; + } + continue; + } + if(chunk.ckID == ID_BODY) + { + for(height = 0; height 0) + { + short len; + len = value +1; + sofar -= len; + if(!(fread(dest,len,1,pic))) + { + CloseFile(pic); + ErrorCode = ERR_BADPICFILE; + AckFree(savedestx); + return(0L); + } + dest +=len; + } + else + { + short count; + count = -value; /* get amount to dup */ + count ++; + sofar -= count; + value=fgetc(pic); + while (--count >= 0) *dest++ = value; + } + } + else + { + fread(dest,sofar,1,pic); /* just throw on plane */ + sofar = 0; + } + } + if (sofar < 0) + { + CloseFile(pic); + } + _fmemcpy(destx,pplanes,bmhd.w); + destx += bmhd.w; + } + break; /* leave if we've unpacked the BODY*/ + } + + fseek(pic,chunk.ckSize,SEEK_CUR); + } + + CloseFile(pic); + return((char *)savedestx); + } long ByteFlipLong(long NUMBER) { + /* Hey, I didn;t write this function!!! */ long Y, T; short I; @@ -222,6 +234,5 @@ short iffswab(unsigned short number) xx1 = number <<8; xx2 = number >>8; result = xx1|xx2; return(result); } - // **** End of Source **** diff --git a/ack_lib/ACKPCX.C b/ack_lib/ACKPCX.C index 0c9d265..aa59a38 100644 --- a/ack_lib/ACKPCX.C +++ b/ack_lib/ACKPCX.C @@ -14,146 +14,132 @@ typedef struct { - char manufacturer; // Always set to 0 - char version; // Always 5 for 256-color files - char encoding; // Always set to 1 - char bits_per_pixel; // Should be 8 for 256-color files - short xmin,ymin; // Coordinates for top left corner - short xmax,ymax; // Width and height of image - short hres; // Horizontal resolution of image - short vres; // Vertical resolution of image - char palette16[48]; // EGA palette; not used for 256-color files - char reserved; // Reserved for future use - char color_planes; // Color planes - short bytes_per_line; // Number of bytes in 1 line of pixels - short palette_type; // Should be 2 for color palette - char filler[58]; // Reserved + char manufacturer; /* Always set to 0 */ + char version; /* Always 5 for 256-color files */ + char encoding; /* Always set to 1 */ + char bits_per_pixel; /* Should be 8 for 256-color files */ + short xmin,ymin; /* Coordinates for top left corner */ + short xmax,ymax; /* Width and height of image */ + short hres; /* Horizontal resolution of image */ + short vres; /* Vertical resolution of image */ + char palette16[48]; /* EGA palette; not used for 256-color files */ + char reserved; /* Reserved for future use */ + char color_planes; /* Color planes */ + short bytes_per_line; /* Number of bytes in 1 line of pixels */ + short palette_type; /* Should be 2 for color palette */ + char filler[58]; /* Nothing but junk */ } PcxHeader; typedef struct { - PcxHeader hdr; // Header information - UCHAR *bitmap; // The bitmap data - UCHAR pal[768]; // Color palette for the bitmap data - unsigned short imagebytes,width,height; // Size of the bitmap + PcxHeader hdr; + UCHAR *bitmap; + UCHAR pal[768]; + unsigned short imagebytes,width,height; } PcxFile; #define PCX_MAX_SIZE 64000L enum {PCX_OK,PCX_NOMEM,PCX_TOOBIG,PCX_NOFILE}; enum {NORMAL,RLE}; -//enum {FALSE,TRUE}; +enum {FALSE,TRUE}; PcxFile pcxGlobal; // data structure for reading PCX files extern unsigned char colordat[]; -//============================================================================= -// This routine loads a 256 color PCX file. The file can be a standalone -// PCX file or it can be combined with a resource. If the data is part -// of a resource, the rshandle flag will be set. The bitmap data is read -// into a buffer that is the size of the bitmap + 4 bytes. The first 4 -// bytes in the buffer contain the width and height of the bitmap. -//============================================================================= +// +// This routine loads a 256 color PCX file. +// unsigned char *AckReadPCX(char *filename) { long i; int mode=NORMAL,nbytes; char abyte,*p; - int handle; + FILE *f; PcxFile *pcx; pcx = &pcxGlobal; -// Open the file since no resource is open. + if (!rsHandle) { - handle = open(filename,O_RDONLY|O_BINARY); // Open the file for reading - if (handle < 1) // Make sure file is opened - { - ErrorCode = ERR_BADFILE; - return NULL; - } - } -else // Use the resource instead + f=fopen(filename,"rb"); + if (f==NULL) { - handle = rsHandle; // Use the handle to the resource file - // Move to the location in the resource where the data is stored - lseek(handle,rbaTable[(ULONG)filename],SEEK_SET); + ErrorCode = ERR_BADFILE; + return NULL; + } + } +else + { + f = fdopen (rsHandle, "rb"); + if (f == NULL) + { + ErrorCode = ERR_BADPICNAME; + return (0L); + } + + fseek (f, rbaTable[(ULONG) filename], SEEK_SET); } -read(handle,&pcx->hdr,sizeof(PcxHeader)); // Read in the header data -pcx->width=1+pcx->hdr.xmax-pcx->hdr.xmin; // Store width and height +fread(&pcx->hdr,sizeof(PcxHeader),1,f); +pcx->width=1+pcx->hdr.xmax-pcx->hdr.xmin; pcx->height=1+pcx->hdr.ymax-pcx->hdr.ymin; -// Store number of bytes used for image pcx->imagebytes=(unsigned int)(pcx->width*pcx->height); -// Make sure bitmap is correct size if (pcx->imagebytes > PCX_MAX_SIZE) { if (!rsHandle) - close(handle); + fclose(f); ErrorCode = ERR_INVALIDFORM; return(NULL); } -// Allocate size for bitmap. 4 extra bytes are included to give -// room to store bitmap width and height info. pcx->bitmap=(char*)AckMalloc(pcx->imagebytes+4); -if (pcx->bitmap == NULL) // Make sure memory is allocated +if (pcx->bitmap == NULL) { if (!rsHandle) - close(handle); + fclose(f); ErrorCode = ERR_NOMEMORY; return(NULL); } -p=&pcx->bitmap[4]; // Get address of data area +p=&pcx->bitmap[4]; -// Loop and read in pixel data for bitmap -// Uses RLE decompression for (i=0;iimagebytes;i++) { - if (mode == NORMAL) // Normal color read mode + if(mode == NORMAL) + { + abyte=fgetc(f); + if ((unsigned char)abyte > 0xbf) { - read(handle,&abyte,1); // Read in pixel value from file - if ((unsigned char)abyte > 0xbf) // Value read > 191 - { - nbytes=abyte & 0x3f; // Get the RLE counter - read(handle,&abyte,1); - if (--nbytes > 0) // Is counter greater than 1? - mode=RLE; // Yes, we're in RLE mode - } + nbytes=abyte & 0x3f; + abyte=fgetc(f); + if (--nbytes > 0) + mode=RLE; } - else if (--nbytes == 0) // When counter down to 0 - mode=NORMAL; // return to color read mode - *p++=abyte; // Store pixel value + } + else if (--nbytes == 0) + mode=NORMAL; + *p++=abyte; } -// Get palette from PCX file, 256 color palette store 768 bytes from -// end of file. For a resource file we need to find the position where -// the next file starts and then backup 768 bytes -if (rsHandle) - lseek(handle,rbaTable[(ULONG)(filename + 1)]-768L,SEEK_CUR); -else - lseek(handle,-768L,SEEK_END); - -// Store the palette data in our global colordat array -read(handle,colordat,768); +fseek(f,-768L,SEEK_END); // get palette from pcx file +fread(colordat,768,1,f); p=colordat; -for (i=0;i<768;i++) // bit shift palette - *p++ = *p >> 2; +for (i=0;i<768;i++) // bit shift palette + *p++=*p >>2; -if (!rsHandle) // Close pcx file if not using a resource - close(handle); +if (!rsHandle) + fclose(f); -// Add in bitmap width and height to first 4 bytes of buffer p = pcx->bitmap; (*(short *)p) = pcx->width; p += sizeof(short); (*(short *)p) = pcx->height; -return(pcx->bitmap); // return bitmap buffer +return(pcx->bitmap); // return success } // **** End of Source ****