Category : C Source Code
Archive   : IMDSRC78.ZIP
Filename : BUFFER.C

 
Output of file : BUFFER.C contained in archive : IMDSRC78.ZIP
/*** IMDISP module BUFFER.C

BUFFER.C contains all the routines for manipulating images into
buffers.
Written by Ron Baalke - 06/15/91.

***/

#define __MSC

/* * * * INCLUDE files * * * */

#include
#include
#include
#include
#include
#include
#include
#include "imdef.h"
#include "imdisp.h"
#include "dispio.h"
#include "refresh.h"
#include "textutil.h"
#include "display.h"
#include "keywutil.h"
#include "mem.h"

/* * * * External functions * * * */

void DoCopy(void);
void DoMerge(void);
int AllocBuffer(int, int, int, int);
int PutBuffer (int, unsigned char *, int, int, int);
int GetBuffer (int, unsigned char *, int, int, int);
void DeallocBuffer(int);

/* * * * Global Variables * * * */

Buffer Buffers[MAXBUFFERS];
char BufferFileName[40] = "C:\\";
/*
extern unsigned char buffer1[MAXDISPNS];
extern unsigned char buffer2[MAXDISPNS];
extern unsigned char outbuffer[MAXDISPNS];
*/
extern unsigned char *buffer1;
extern unsigned char *buffer2;
extern unsigned char *outbuffer;

/*************************************************************************/
/* DoCopy */
/* */
/* Written by Ron Baalke - 06/16/91 */
/* */
/* This routine will copy either an image, screen or another buffer */
/* to the specified buffer. */
/*************************************************************************/

void DoCopy(void)
{
int i,j;
int source, destination;
int index;
char string1[40];
char string2[40];
char string3[40];
int flag, toflag, fileflag;
unsigned char buffer[MAXDISPNS];
int save;

source = IMAGE;
destination = NOWHERE;

strcat(CommandString," ");
GetKeywordString (CommandString, "COP", " ",string1, &flag);
GetKeywordString (CommandString, "TO" , " ",string2, &toflag);
GetKeywordString (CommandString, "FIL", " ",string3, &fileflag);
if (flag <= 0)
{
StatusLine(0,"Incomplete command");
return;
}

/* Perform parameters logic */

if (fileflag != -1)
fileflag = VIRTUAL_FILE;
else
fileflag = 0;

if ((toflag) &&
(strlen(string2) == 1) &&
(string2[0] >= 'A') &&
(string2[0] <= 'Z'))
destination = string2[0] - 'A';

if (strnicmp (string1, "SCR", 3) == 0)
{
source = SCREEN;
if (destination == NOWHERE)
{
strcat(string1," ");
GetKeywordString (CommandString, string1, " ",string2, &flag);
if ((flag) &&
(strlen(string2) == 1) &&
(string2[0] >= 'A') &&
(string2[0] <= 'Z'))
destination = string2[0] - 'A';
}
}

if ((strlen(string1) == 1) &&
(string1[0] >= 'A') &&
(string1[0] <= 'Z'))
{
if (destination != NOWHERE)
source = string1[0] - 'A';
else
{
strcat(string1," ");
GetKeywordString (CommandString, string1, " ",string2, &flag);
if ((flag) &&
(strlen(string2) == 1) &&
(string2[0] >= 'A') &&
(string2[0] <= 'Z'))
{
source = string1[0] - 'A';
destination = string2[0] - 'A';
}
else
destination = string1[0] - 'A';
}
}

/* Do error checks */

if (destination == NOWHERE)
{
StatusLine(0,"Illegal destination");
return;
}

if (source == destination)
{
StatusLine(0,"Source and destination can't be the same");
return;
}

if ((source != SCREEN) &&
(source != IMAGE) &&
(Buffers[source].location == NOWHERE))
{
strcpy(string1,"A");
string1[0] += source;
sprintf(buffer,"Buffer %s is empty",string1);
StatusLine(0,buffer);
return;
}

/* Allocate destination buffer */

if (Buffers[destination].location != NOWHERE)
DeallocBuffer(destination);

if (source == IMAGE)
flag = AllocBuffer(destination,fileflag,Image_Length,Image_Height);
else if (source == SCREEN)
flag = AllocBuffer(destination,fileflag,dispns,dispnl);
else
flag = AllocBuffer(destination,fileflag,Buffers[source].dispns,
Buffers[source].dispnl);


if (Buffers[destination].location == NOWHERE)
{
StatusLine(0,"Not enough memory");
return;
}

/* Do the actual copy */

if (source == SCREEN)
{
/* Refresh the bottom of the screen to cover up the command prompt */

if (RefreshLines > 0)
{
for (i = dispnl-2*TextHeight-15; i<=dispnl; i++)
{
GetRefresh(buffer,i,1,dispns);
DisplayLine(buffer,i,1,dispns);
}
}
save = RefreshLines; /* temporarily disable GetLine() from using*/
RefreshLines = 0; /* the refresh buffer */

for (i=1; i<=dispnl; i++)
{
GetLine(buffer,i,1,dispns);
PutBuffer(destination,buffer,i,1,dispns);
}

RefreshLines = save; /* Restore */
}
else if (source == IMAGE)
{
for (i=Image_Line,j=1; i {
GetLine(buffer,i,Image_Sample,Image_Length);
PutBuffer(destination,buffer,j,1,Image_Length);
}
}
else
{
for (i=1; i<=dispnl; i++)
{
GetBuffer(source,buffer,i,1,dispns);
PutBuffer(destination,buffer,i,1,dispns);
}
}


}

/*************************************************************************/
/* DoMerge */
/* */
/* Written by Ron Baalke - 06/16/91 */
/* */
/* This routine will merge buffers together or with the screen and */
/* output the result to either another buffer or the screen. */
/*************************************************************************/


void DoMerge(void)
{
int i,j;
int source1, source2, destination;
int index;
char string1[40];
char string2[40];
char string3[40];
char string4[40];
int flag, toflag, fileflag, withflag;
float scale1, scale2;
int length, length1, length2;
int height, height1, height2;
int save;
int iscale1, iscale2;
int temp;

source1 = NOWHERE;
source2 = IMAGE;
destination = SCREEN;
scale1 = 0.0;
scale2 = 0.0;

strcat(CommandString," ");
GetKeywordString (CommandString, "MER", " ",string1, &flag);
GetKeywordString (CommandString, "TO" , " ",string2, &toflag);
GetKeywordString (CommandString, "FIL", " ",string3, &fileflag);
GetKeywordString (CommandString, "WIT", " ",string4, &withflag);
if (flag <= 0)
{
StatusLine(0,"Incomplete command");
return;
}

/* Perform parameter logic */

if (fileflag != -1)
fileflag = VIRTUAL_FILE;
else
fileflag = 0;

if ((toflag) &&
(strlen(string2) == 1) &&
(string2[0] >= 'A') &&
(string2[0] <= 'Z'))
destination = string2[0] - 'A';

if (withflag)
{
if ((strlen(string4) == 1) &&
(string4[0] >= 'A') &&
(string4[0] <= 'Z'))
{
source2 = string4[0] - 'A';
source1 = IMAGE;
}
else if (strnicmp (string4, "SCR", 3) == 0)
source2 = SCREEN;
}

if (flag)
{
if ((strlen(string1) == 1) &&
(string1[0] >= 'A') &&
(string1[0] <= 'Z'))
{
source1 = string1[0] - 'A';
}
else if (strnicmp (string1, "SCR", 3) == 0)
source1 = SCREEN;
}

/* Do some error checking */

if (source1 == source2)
{
StatusLine(0,"Sources can't be the same");
return;
}

if ((source1 == IMAGE && source2 == SCREEN) ||
(source2 == IMAGE && source1 == SCREEN))
{
StatusLine(0,"Sources can't be the same");
return;
}

if (source1 == NOWHERE || source2 == NOWHERE)
{
StatusLine(0,"Source not specified");
return;
}

if ((source1 >= 0) &&
(Buffers[source1].location == NOWHERE))
{
strcpy(string1,"A");
string1[0] += source1;
sprintf(buffer1,"Buffer %s is empty",string1);
StatusLine(0,buffer1);
return;
}
if ((source2 >= 0) &&
(Buffers[source2].location == NOWHERE))
{
strcpy(string1,"A");
string1[0] += source2;
sprintf(buffer1,"Buffer %s is empty",string1);
StatusLine(0,buffer1);
return;
}

/* Retrieve scaling paramters */

if (source1 == SCREEN)
{
GetKeywordString (CommandString, "SCR", " ",string2, &flag);
scale1 = atof(string2);
}
else if (source1 >= 0)
{
strcpy(string1,"A");
string1[0] += source1;
strcat(string1," ");
GetKeywordString (CommandString, string1, " ", string2, &flag);
scale1 = atof(string2);
}

if (source2 == SCREEN)
{
GetKeywordString (CommandString, "SCR", " ",string2, &flag);
scale2 = atof(string2);
}
else if (source2 >= 0)
{
strcpy(string1,"A");
string1[0] += source2;
strcat(string1," ");
GetKeywordString (CommandString, string1, " ", string2, &flag);
scale2 = atof(string2);
}

iscale1 = 100.0 * scale1 + 0.5;
iscale2 = 100.0 * scale2 + 0.5;

if (iscale1 == 0 && iscale2 == 0)
{
iscale1 = 50;
iscale2 = 50;
}

if (iscale1 == 0 && iscale2 > 0 && iscale2 < 100)
iscale1 = 100 - iscale2;
else if (iscale2 == 0 && iscale1 > 0 && iscale1 < 100)
iscale2 = 100 - iscale1;
else if (iscale1 == 0)
iscale1 == 100;
else if (iscale2 == 0)
iscale2 == 100;


if (source1 == IMAGE)
{
length1 = Image_Length;
height1 = Image_Height;
}
else if (source1 == SCREEN)
{
length1 = dispns;
height1 = dispnl;
}
else
{
length1 = Buffers[source1].dispns;
height1 = Buffers[source1].dispnl;
}

if (source2 == IMAGE)
{
length2 = Image_Length;
height2 = Image_Height;
}
else if (source2 == SCREEN)
{
length2 = dispns;
height2 = dispnl;
}
else
{
length2 = Buffers[source2].dispns;
height2 = Buffers[source2].dispnl;
}

length = length1;
if (length2 > length) length = length2;
height = height1;
if (height2 > height) height = height2;

/* Allocate buffer if applicable */

if (destination >= 0)
{
if (Buffers[destination].location != NOWHERE)
DeallocBuffer(destination);

flag = AllocBuffer(destination,fileflag,length,height);

if (Buffers[destination].location == NOWHERE)
{
StatusLine(0,"Not enough memory");
return;
}
}

if ((source1 == SCREEN) || (source2 == SCREEN))
{
/* Refresh the bottom of the screen to cover up the command prompt */

if (RefreshLines > 0)
{
for (i = dispnl-2*TextHeight-15; i<=dispnl; i++)
{
GetRefresh(buffer1,i,1,dispns);
DisplayLine(buffer1,i,1,dispns);
}
}
}

/* Perform the merge here */

for (i=1; i<=height; i++)
{
save = RefreshLines; /* temporarily disable GetLine() from using*/
RefreshLines = 0; /* the refresh buffer */
if (i <= height1)
{
if (source1 == SCREEN)
GetLine(buffer1,i,1,dispns);
else if (source1 == IMAGE)
GetLine(buffer1,Image_Line+i-1,Image_Sample,Image_Length);
else
GetBuffer(source1,buffer1,i,1,length1);
}
else
memset( buffer1, 0, length);

if (i <= height2)
{
if (source2 == SCREEN)
GetLine(buffer2,i,1,dispns);
else if (source2 == IMAGE)
GetLine(buffer2,Image_Line+i-1,Image_Sample,Image_Length);
else
GetBuffer(source2,buffer2,i,1,length2);
}
else
memset( buffer2, 0, length);

for (j=0; j {
temp = 0;
if (buffer2[j] == 0)
temp = buffer1[j];
else if (buffer1[j] == 0)
temp = buffer2[j];
else
{
if ((j < length1) && (buffer2[j] > 0))
temp = iscale1 * buffer1[j];
if (j < length2)
temp += iscale2 * buffer2[j];
temp = temp/100;
}
if (temp < 0) temp = 0;
if (temp >= numDN) temp = numDN-1;
outbuffer[j] = temp;
}
RefreshLines = save;

if (destination == SCREEN)
{
DisplayLine(outbuffer,i,1,length);
if (RefreshLines > 0)
PutRefresh(outbuffer,i,1,length);
}
else
PutBuffer(destination,outbuffer,i,1,length);

}

}

int AllocBuffer(int index, int type, int length, int height)
/*** Allocbuffer allocates memory for the buffer in either extended
memory or it creates a file and initializes it (if
it's newly created) to all NULLs.

***/
{
int i, n_bytes;
unsigned long lsize;
int size;
long offset;
char letter[2];

Buffers[index].handle = 0;
Buffers[index].dispns = length;
Buffers[index].dispnl = height;
Buffers[index].location = NOWHERE;

if (type == VIRTUAL_FILE)
{
strcpy(letter,"A");
letter[0] += index;
strcpy(Buffers[index].file,BufferFileName);
strcat(Buffers[index].file,"___BUF");
strcat(Buffers[index].file,letter);
strcat(Buffers[index].file,".TMP");
Buffers[index].handle = open( Buffers[index].file, O_CREAT|O_RDWR|O_BINARY, S_IREAD|S_IWRITE );
if (Buffers[index].handle == -1)
{
/* File must already exist - reopen w/o initialize */
Buffers[index].handle = open( Buffers[index].file, O_RDWR|O_BINARY, S_IREAD|S_IWRITE );
if (Buffers[index].handle == -1) /* If open fails, just set #lines to 0 */
Buffers[index].dispnl = 0;
}
else
{
/* Initialize the file to all 0's, one line at a time */
memset( StageRefresh, 0, Buffers[index].dispns);
n_bytes = Buffers[index].dispns;
i = 0;
while ( (n_bytes == Buffers[index].dispns) &&
(i < Buffers[index].dispnl) )
{
n_bytes = write( Buffers[index].handle, StageRefresh,
Buffers[index].dispns);
if (n_bytes == Buffers[index].dispns)
i++;
}
if (i == Buffers[index].dispnl)
Buffers[index].location = VIRTUAL_FILE;
else
{
close(Buffers[index].handle);
remove(Buffers[index].file);
}
}
return(0);
}

/* Attempt extended memory */

if (Buffers[index].location == NOWHERE)
{
lsize = (long)Buffers[index].dispns * (long)Buffers[index].dispnl;
if (Memory_Alloc(lsize,&Buffers[index].handle,&Buffers[index].location) == 0)
{
memset( StageRefresh, 0, Buffers[index].dispns);
size = Buffers[index].dispns;

for (i=0; i {
offset = (long) i * (long) Buffers[index].dispns;
Memory_PutLine(Buffers[index].handle, Buffers[index].location,
StageRefresh, offset,size);
}
}
}

return(0);
}

int PutBuffer (int index, unsigned char * buffer, int line, int ss, int ns)
/*** PutBuffer stores a line of pixels into the appropriate buffer.
Parameter type description
index interger The buffer number
buffer char ptr The array of pixel values
line integer The line coordinate of the first pixel
sample integer The sample coordinate of the first pixel
ns integer The number of pixels to store
***/
{
int n_bytes;
long offset;
long current;

offset = (long)(line-1) * Buffers[index].dispns + ss - 1;

if (Buffers[index].location != VIRTUAL_FILE)
{
Memory_PutLine(Buffers[index].handle, Buffers[index].location,
buffer, offset, ns);
n_bytes = ns;
}
else
{
current = lseek( Buffers[index].handle, offset, SEEK_SET);
n_bytes = write( Buffers[index].handle, buffer, ns);
}

return( n_bytes );
}

int GetBuffer (int index, unsigned char * buffer, int line, int ss, int ns)
/*** GetBuffer reads a line of pixels from the appropriate buffer.
Parameter type description
index integer The buffer number
buffer char ptr The receiving array of pixel values
line integer The line coordinate of the first pixel
sample integer The sample coordinate of the first pixel
ns integer The number of pixels to read
***/
{
int n_bytes;
long offset;
long current;

offset = (long)(line-1) * Buffers[index].dispns + ss - 1;

if (Buffers[index].location != VIRTUAL_FILE)
{
Memory_GetLine(Buffers[index].handle, Buffers[index].location,
buffer, offset, ns);
n_bytes = ns;
}
else
{
current = lseek( Buffers[index].handle, offset, SEEK_SET);
n_bytes = read( Buffers[index].handle, buffer, ns);
}

return( n_bytes );
}

void DeallocBuffer(int index)
/*** DeallocBuffer releases the buffer back to DOS.
***/
{
if (Buffers[index].location != VIRTUAL_FILE) /* Ron Baalke - 05/91 */
Memory_Free(Buffers[index].handle, Buffers[index].location);
else
{
close(Buffers[index].handle);
remove(Buffers[index].file);
}

Buffers[index].location = NOWHERE;
}


  3 Responses to “Category : C Source Code
Archive   : IMDSRC78.ZIP
Filename : BUFFER.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/