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,21 +21,29 @@
#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
int rdbufpos; int rdbufpos;
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);
}
//============================================================================= //=============================================================================
// //
@ -48,19 +56,21 @@ if (rsHandle)
{ {
rsHandle = open(rsName,O_RDONLY|O_BINARY); rsHandle = open(rsName,O_RDONLY|O_BINARY);
if (rsHandle < 1) if (rsHandle < 1)
rsHandle = 0; rsHandle = 0;
} }
} }
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,142 +80,144 @@ 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
{
pic = fdopen(rsHandle,"rb");
if (pic == NULL)
{ {
handle = rsHandle; ErrorCode = ERR_BADPICNAME;
lseek(rsHandle,rbaTable[(ULONG)picname],SEEK_SET); 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;
return(0L);
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<bmhd.h; height ++)
{
unsigned char *dest;
dest = (unsigned char *)&(pplanes[0]); // point at first char
sofar = bmhd.w; // number of bytes = 8
if (sofar&1) sofar++;
while (sofar)
{
if (bmhd.compression)
{
value = 0;
read(handle,&value,1);
if (value > 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.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<bmhd.h; height ++)
{
unsigned char *dest;
dest = (unsigned char *)&(pplanes[0]); /* point at first char */
sofar = bmhd.w; /* number of bytes = 8 */
if (sofar&1) sofar++;
while (sofar)
{
if (bmhd.compression)
{
value=fgetc(pic); /* get the next byte */
if (value > 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) 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;
return NULL;
}
}
else // Use the resource instead
{ {
handle = rsHandle; // Use the handle to the resource file ErrorCode = ERR_BADFILE;
// Move to the location in the resource where the data is stored return NULL;
lseek(handle,rbaTable[(ULONG)filename],SEEK_SET); }
}
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 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)
{
abyte=fgetc(f);
if ((unsigned char)abyte > 0xbf)
{ {
read(handle,&abyte,1); // Read in pixel value from file nbytes=abyte & 0x3f;
if ((unsigned char)abyte > 0xbf) // Value read > 191 abyte=fgetc(f);
{ if (--nbytes > 0)
nbytes=abyte & 0x3f; // Get the RLE counter mode=RLE;
read(handle,&abyte,1);
if (--nbytes > 0) // Is counter greater than 1?
mode=RLE; // Yes, we're in RLE mode
}
} }
else if (--nbytes == 0) // When counter down to 0 }
mode=NORMAL; // return to color read mode else if (--nbytes == 0)
*p++=abyte; // Store pixel value mode=NORMAL;
*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 ****