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...)
This commit is contained in:
Gered 2019-11-02 17:35:48 -04:00
parent e2176a10f0
commit 8acd9a1549
2 changed files with 216 additions and 219 deletions

View file

@ -21,15 +21,13 @@
#include "ackext.h" #include "ackext.h"
#include "iff.h" #include "iff.h"
extern int errno;
extern char rsName[]; 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 unsigned char cplanes[8][80]; /* setting max at 640 pixels width */
// thats 8 pixels per byte per plane /* thats 8 pixels per byte per plane */
unsigned char *pplanes= &cplanes[0][0]; // for a form pbm unsigned char *pplanes= &cplanes[0][0]; /* for a form pbm */
#define MAX_BUF_POS 4096 #define MAX_BUF_POS 4096
@ -37,6 +35,16 @@ unsigned char *pplanes= &cplanes[0][0]; // for a form pbm
int rdSize; int rdSize;
UCHAR rdBuffer[MAX_BUF_POS+2]; UCHAR rdBuffer[MAX_BUF_POS+2];
short ReadNxtBlock(short handle)
{
short retlen;
retlen = read(handle,rdBuffer,MAX_BUF_POS);
rdbufpos = 0;
return(retlen);
}
//============================================================================= //=============================================================================
// //
//============================================================================= //=============================================================================
@ -53,14 +61,16 @@ if (rsHandle)
} }
unsigned char *AckReadiff(char *picname) unsigned char *AckReadiff(char *picname)
{ {
int handle; FILE *pic;
short handle;
form_chunk fchunk; form_chunk fchunk;
ChunkHeader chunk; ChunkHeader chunk;
BitMapHeader bmhd; BitMapHeader bmhd;
long length,fpos; long length;
char value; // must remain signed, no matter what. ignore any warnings. char value; /* must remain signed, no matter what. ignore any warnings. */
short sofar; short sofar;
short width,height,planes; short width,height,planes;
short pixw; short pixw;
@ -70,47 +80,54 @@ unsigned char *AckReadiff(char *picname)
rdSize = MAX_BUF_POS; rdSize = MAX_BUF_POS;
if (!rsHandle) if (!rsHandle)
handle = open(picname,O_RDONLY|O_BINARY); {
if ((pic = fopen(picname,"rb")) == NULL)
{
ErrorCode = ERR_BADPICNAME;
return(0L);
}
}
else else
{ {
handle = rsHandle; pic = fdopen(rsHandle,"rb");
lseek(rsHandle,rbaTable[(ULONG)picname],SEEK_SET); if (pic == NULL)
{
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 (fchunk.type != FORM)
{ {
if (!rsHandle) CloseFile(pic);
close(handle);
ErrorCode = ERR_INVALIDFORM; ErrorCode = ERR_INVALIDFORM;
return(0L); return(0L);
} }
if (fchunk.subtype != ID_PBM) if (fchunk.subtype != ID_PBM)
{ {
if (!rsHandle) CloseFile(pic);
close(handle);
ErrorCode = ERR_NOPBM; ErrorCode = ERR_NOPBM;
return(0L); return(0L);
} }
// now lets loop...Because the Chunks can be in any order! /* now lets loop...Because the Chunks can be in any order! */
while(1) while(1)
{ {
read(handle,&chunk,sizeof(ChunkHeader)); fread(&chunk,1,sizeof(ChunkHeader),pic);
chunk.ckSize = ByteFlipLong(chunk.ckSize); chunk.ckSize = ByteFlipLong(chunk.ckSize);
if (chunk.ckSize & 1) chunk.ckSize ++; // must be word aligned if (chunk.ckSize & 1) chunk.ckSize ++; /* must be word aligned */
if(chunk.ckID == ID_BMHD) if(chunk.ckID == ID_BMHD)
{ {
read(handle,&bmhd,sizeof(BitMapHeader)); fread(&bmhd,1,sizeof(BitMapHeader),pic);
bmhd.w=iffswab(bmhd.w); // the only things we need. bmhd.w=iffswab(bmhd.w); /* the only things we need. */
bmhd.h=iffswab(bmhd.h); bmhd.h=iffswab(bmhd.h);
destx = (unsigned char *)AckMalloc((bmhd.w * bmhd.h)+4); destx = (unsigned char *)AckMalloc((bmhd.w * bmhd.h)+4);
if ( !destx ) if ( !destx )
{ {
if (!rsHandle) CloseFile(pic);
close(handle);
ErrorCode = ERR_NOMEMORY; ErrorCode = ERR_NOMEMORY;
return(0L); return(0L);
} }
@ -129,10 +146,10 @@ unsigned char *AckReadiff(char *picname)
short i; short i;
unsigned char r,g; unsigned char r,g;
read(handle,colordat,chunk.ckSize); fread(colordat,1,chunk.ckSize,pic);
for (i=0;i<768;i++) for (i=0;i<768;i++)
{ {
r = colordat[i]; // r,g do not stand for red and green r = colordat[i]; /* r,g do not stand for red and green */
g = r >> 2; g = r >> 2;
colordat[i] = g; colordat[i] = g;
} }
@ -143,24 +160,22 @@ unsigned char *AckReadiff(char *picname)
for(height = 0; height<bmhd.h; height ++) for(height = 0; height<bmhd.h; height ++)
{ {
unsigned char *dest; unsigned char *dest;
dest = (unsigned char *)&(pplanes[0]); // point at first char dest = (unsigned char *)&(pplanes[0]); /* point at first char */
sofar = bmhd.w; // number of bytes = 8 sofar = bmhd.w; /* number of bytes = 8 */
if (sofar&1) sofar++; if (sofar&1) sofar++;
while (sofar) while (sofar)
{ {
if (bmhd.compression) if (bmhd.compression)
{ {
value = 0; value=fgetc(pic); /* get the next byte */
read(handle,&value,1);
if (value > 0) if (value > 0)
{ {
short len; short len;
len = value +1; len = value +1;
sofar -= len; sofar -= len;
if (!(read(handle,dest,len))) if(!(fread(dest,len,1,pic)))
{ {
if (!rsHandle) CloseFile(pic);
close(handle);
ErrorCode = ERR_BADPICFILE; ErrorCode = ERR_BADPICFILE;
AckFree(savedestx); AckFree(savedestx);
return(0L); return(0L);
@ -170,42 +185,39 @@ unsigned char *AckReadiff(char *picname)
else else
{ {
short count; short count;
count = -value; // get amount to dup count = -value; /* get amount to dup */
count ++; count ++;
sofar -= count; sofar -= count;
value = 0; value=fgetc(pic);
read(handle,&value,1);
while (--count >= 0) *dest++ = value; while (--count >= 0) *dest++ = value;
} }
} }
else else
{ {
read(handle,dest,sofar); fread(dest,sofar,1,pic); /* just throw on plane */
sofar = 0; sofar = 0;
} }
} }
if (sofar < 0) if (sofar < 0)
{ {
if (!rsHandle) CloseFile(pic);
close(handle);
} }
memcpy(destx,pplanes,bmhd.w); _fmemcpy(destx,pplanes,bmhd.w);
destx += bmhd.w; destx += bmhd.w;
} }
break; // leave if we've unpacked the BODY break; /* leave if we've unpacked the BODY*/
} }
lseek(handle,chunk.ckSize,SEEK_CUR); fseek(pic,chunk.ckSize,SEEK_CUR);
} }
if (!rsHandle) CloseFile(pic);
close(handle);
return((char *)savedestx); return((char *)savedestx);
} }
long ByteFlipLong(long NUMBER) long ByteFlipLong(long NUMBER)
{ {
/* Hey, I didn;t write this function!!! */
long Y, T; long Y, T;
short I; short I;
@ -222,6 +234,5 @@ short iffswab(unsigned short number)
xx1 = number <<8; xx2 = number >>8; result = xx1|xx2; xx1 = number <<8; xx2 = number >>8; result = xx1|xx2;
return(result); return(result);
} }
// **** End of Source **** // **** End of Source ****

View file

@ -14,146 +14,132 @@
typedef struct typedef struct
{ {
char manufacturer; // Always set to 0 char manufacturer; /* Always set to 0 */
char version; // Always 5 for 256-color files char version; /* Always 5 for 256-color files */
char encoding; // Always set to 1 char encoding; /* Always set to 1 */
char bits_per_pixel; // Should be 8 for 256-color files char bits_per_pixel; /* Should be 8 for 256-color files */
short xmin,ymin; // Coordinates for top left corner short xmin,ymin; /* Coordinates for top left corner */
short xmax,ymax; // Width and height of image short xmax,ymax; /* Width and height of image */
short hres; // Horizontal resolution of image short hres; /* Horizontal resolution of image */
short vres; // Vertical resolution of image short vres; /* Vertical resolution of image */
char palette16[48]; // EGA palette; not used for 256-color files char palette16[48]; /* EGA palette; not used for 256-color files */
char reserved; // Reserved for future use char reserved; /* Reserved for future use */
char color_planes; // Color planes char color_planes; /* Color planes */
short bytes_per_line; // Number of bytes in 1 line of pixels short bytes_per_line; /* Number of bytes in 1 line of pixels */
short palette_type; // Should be 2 for color palette short palette_type; /* Should be 2 for color palette */
char filler[58]; // Reserved char filler[58]; /* Nothing but junk */
} PcxHeader; } PcxHeader;
typedef struct typedef struct
{ {
PcxHeader hdr; // Header information PcxHeader hdr;
UCHAR *bitmap; // The bitmap data UCHAR *bitmap;
UCHAR pal[768]; // Color palette for the bitmap data UCHAR pal[768];
unsigned short imagebytes,width,height; // Size of the bitmap unsigned short imagebytes,width,height;
} PcxFile; } PcxFile;
#define PCX_MAX_SIZE 64000L #define PCX_MAX_SIZE 64000L
enum {PCX_OK,PCX_NOMEM,PCX_TOOBIG,PCX_NOFILE}; enum {PCX_OK,PCX_NOMEM,PCX_TOOBIG,PCX_NOFILE};
enum {NORMAL,RLE}; enum {NORMAL,RLE};
//enum {FALSE,TRUE}; enum {FALSE,TRUE};
PcxFile pcxGlobal; // data structure for reading PCX files PcxFile pcxGlobal; // data structure for reading PCX files
extern unsigned char colordat[]; extern unsigned char colordat[];
//============================================================================= //
// This routine loads a 256 color PCX file. The file can be a standalone // This routine loads a 256 color PCX file.
// 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.
//=============================================================================
unsigned char *AckReadPCX(char *filename) unsigned char *AckReadPCX(char *filename)
{ {
long i; long i;
int mode=NORMAL,nbytes; int mode=NORMAL,nbytes;
char abyte,*p; char abyte,*p;
int handle; FILE *f;
PcxFile *pcx; PcxFile *pcx;
pcx = &pcxGlobal; pcx = &pcxGlobal;
// Open the file since no resource is open.
if (!rsHandle) if (!rsHandle)
{ {
handle = open(filename,O_RDONLY|O_BINARY); // Open the file for reading f=fopen(filename,"rb");
if (handle < 1) // Make sure file is opened if (f==NULL)
{ {
ErrorCode = ERR_BADFILE; ErrorCode = ERR_BADFILE;
return NULL; return NULL;
} }
} }
else // Use the resource instead else
{ {
handle = rsHandle; // Use the handle to the resource file f = fdopen (rsHandle, "rb");
// Move to the location in the resource where the data is stored if (f == NULL)
lseek(handle,rbaTable[(ULONG)filename],SEEK_SET); {
ErrorCode = ERR_BADPICNAME;
return (0L);
}
fseek (f, rbaTable[(ULONG) filename], SEEK_SET);
} }
read(handle,&pcx->hdr,sizeof(PcxHeader)); // Read in the header data fread(&pcx->hdr,sizeof(PcxHeader),1,f);
pcx->width=1+pcx->hdr.xmax-pcx->hdr.xmin; // Store width and height pcx->width=1+pcx->hdr.xmax-pcx->hdr.xmin;
pcx->height=1+pcx->hdr.ymax-pcx->hdr.ymin; pcx->height=1+pcx->hdr.ymax-pcx->hdr.ymin;
// Store number of bytes used for image
pcx->imagebytes=(unsigned int)(pcx->width*pcx->height); pcx->imagebytes=(unsigned int)(pcx->width*pcx->height);
// Make sure bitmap is correct size
if (pcx->imagebytes > PCX_MAX_SIZE) if (pcx->imagebytes > PCX_MAX_SIZE)
{ {
if (!rsHandle) if (!rsHandle)
close(handle); fclose(f);
ErrorCode = ERR_INVALIDFORM; ErrorCode = ERR_INVALIDFORM;
return(NULL); 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); pcx->bitmap=(char*)AckMalloc(pcx->imagebytes+4);
if (pcx->bitmap == NULL) // Make sure memory is allocated if (pcx->bitmap == NULL)
{ {
if (!rsHandle) if (!rsHandle)
close(handle); fclose(f);
ErrorCode = ERR_NOMEMORY; ErrorCode = ERR_NOMEMORY;
return(NULL); 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;i<pcx->imagebytes;i++) for (i=0;i<pcx->imagebytes;i++)
{ {
if (mode == NORMAL) // Normal color read mode if(mode == NORMAL)
{ {
read(handle,&abyte,1); // Read in pixel value from file abyte=fgetc(f);
if ((unsigned char)abyte > 0xbf) // Value read > 191 if ((unsigned char)abyte > 0xbf)
{ {
nbytes=abyte & 0x3f; // Get the RLE counter nbytes=abyte & 0x3f;
read(handle,&abyte,1); abyte=fgetc(f);
if (--nbytes > 0) // Is counter greater than 1? if (--nbytes > 0)
mode=RLE; // Yes, we're in RLE mode mode=RLE;
} }
} }
else if (--nbytes == 0) // When counter down to 0 else if (--nbytes == 0)
mode=NORMAL; // return to color read mode mode=NORMAL;
*p++=abyte; // Store pixel value *p++=abyte;
} }
// Get palette from PCX file, 256 color palette store 768 bytes from fseek(f,-768L,SEEK_END); // get palette from pcx file
// end of file. For a resource file we need to find the position where fread(colordat,768,1,f);
// 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);
p=colordat; p=colordat;
for (i=0;i<768;i++) // bit shift palette for (i=0;i<768;i++) // bit shift palette
*p++ = *p >> 2; *p++=*p >>2;
if (!rsHandle) // Close pcx file if not using a resource if (!rsHandle)
close(handle); fclose(f);
// Add in bitmap width and height to first 4 bytes of buffer
p = pcx->bitmap; p = pcx->bitmap;
(*(short *)p) = pcx->width; (*(short *)p) = pcx->width;
p += sizeof(short); p += sizeof(short);
(*(short *)p) = pcx->height; (*(short *)p) = pcx->height;
return(pcx->bitmap); // return bitmap buffer return(pcx->bitmap); // return success
} }
// **** End of Source **** // **** End of Source ****