home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: Science
/
Science.zip
/
imdisp79.zip
/
DISPLAY.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-04-17
|
17KB
|
542 lines
/*** IMDISP module DISPLAY.C
DISPLAY contains the routines to handle the actual display
screen.
***/
#define __MSC
/* * * * INCLUDE files * * * */
#include <conio.h>
#include <malloc.h>
#include <math.h>
#include <stdio.h>
#include <io.h>
#include "mshell.h"
#include "imdef.h"
#include "imdisp.h"
#include "imdutil.h"
#include "dispio.h"
#include "disputil.h"
#include "imageio.h"
#include "fileio.h"
#include "labutil.h"
#include "refresh.h"
#include "textutil.h"
#include "buffer.h"
#include "keywutil.h"
/* * * * External functions * * * */
/* * * * Function declarations * * * */
int DisplayImage (void);
/* * * * Global Variables * * * */
char Source_File_Name[64] = {""};
int Sampling_Factor;
int Image_Line;
int Image_Sample;
int Image_Height;
int Image_Length;
int Image_Zoom;
int Image_Subsample;
int Image_Line1;
int Image_Sample1;
int Image_Height1;
int Image_Length1;
int Image_Zoom1;
int Image_Subsample1;
int DisplayImage(void)
/* DisplayImage performs the display command.
First it gets the values (or defaults) of all of the possible
parameters. Then it checks them for validity and processes
the parameters into useful form. It then loops through the
image, reading a line, formatting it, displaying it, and stuffing
it into the refresh buffer.
*/
{
int line, samp, i, j, k;
int bitshift, NoScale, flag;
int centsub, centerflag, flipflag;
int up, down, left, right, upflag, downflag, leftflag, rightflag;
int noprompt_flag;
int buffer_flag=0;
int index;
int auto_stretch_flag;
int Source_Flag;
int auto_amount;
char dispstr[80];
long histbuf[256];
long limit,total_samples,count=0L;
int nl_save, ns_save;
int nld_save, nsd_save, sl_save, ss_save;
int nldd_save, nsdd_save, sldd_save, ssdd_save;
int center1_save, center2_save;
long high_save, low_save;
int bits_save, sub_save;
int htile,vtile,tile;
int fmidr,c1midr,c2midr,c3midr;
FILE *fp;
unsigned int bytesneeded;
char status[100];
unsigned char *tempbuf,ch;
fmidr = FALSE;
c1midr = FALSE;
c2midr = FALSE;
c3midr = FALSE;
for (i = 0; i < 256; i++)
histbuf[i] = 0L;
if (OpenFileFlag == 0)
{
StatusLine(0,"Use the \'FILE fname\' command to select image");
return(0);
}
if (IMCB[0].nl == 0)
{
StatusLine(0,"No lines in image");
return(0);
}
/* Check to see if buffer is to be displayed - Ron Baalke - 06/91 */
GetKeywordString (CommandString, "DIS", " ",dispstr, &flag);
if ((flag) &&
(strlen(dispstr) == 1) &&
(dispstr[0] >= 'A') &&
(dispstr[0] <= 'Z'))
{
buffer_flag = 1;
index = dispstr[0] - 'A';
if (Buffers[index].location == NOWHERE)
{
strcpy(status,"A");
status[0] += index;
sprintf(dispstr,"Buffer %s is empty",status);
StatusLine(0,dispstr);
return(1);
}
nl_save = nl;
ns_save = ns;
nl = Buffers[index].dispnl;
ns = Buffers[index].dispns;
nld_save = nld; nsd_save = nsd; sl_save = sl; ss_save = ss;
nldd_save = nldd; nsdd_save = nsdd; sldd_save = sldd; ssdd_save = ssdd;
center1_save = centerline;
center2_save = centersamp;
high_save = DNhigh;
low_save = DNlow;
DNhigh = numDN-1;
DNlow = 0;
bits_save = bitsperpix;
bitsperpix = 8;
}
GetKeywordInteger (CommandString, "SL", 1, &sl, &flag);
GetKeywordInteger (CommandString, "SS", 1, &ss, &flag);
if (ss > ns) ss = 1;
GetKeywordInteger (CommandString, "NL", nl-sl+1, &nld, &flag);
GetKeywordInteger (CommandString, "NS", ns-ss+1, &nsd, &flag);
GetKeywordInteger (CommandString, "DSL", 1, &sldd, &flag);
GetKeywordInteger (CommandString, "DSS", 1, &ssdd, &flag);
GetKeywordSubcommand (CommandString, "CUR", &flag);
if (flag > 0) MoveCursor(&sldd,&ssdd);
GetKeywordInteger (CommandString, "SUB", 1, &sub_save, &flag);
if (sub_save != 1)
{
/*
* If we found a value for SUB, multiply it into the old value
* and set the flag to say we're subsampling
*/
SubsampleFlag = TRUE;
subsample = sub_save;
}
else if (flag > 0)
{
/*
* If we found a value for SUB, but it was 1, we're resetting back
* to no subsampling - reset the flag and the subsample factor
*/
SubsampleFlag = FALSE;
subsample = 1;
}
GetKeywordInteger (CommandString, "ZOO", 1, &zoom, &flag);
GetKeywordSubcommand (CommandString, "CEN", ¢erflag);
GetKeywordInteger (CommandString, "LEF", dispns, &left, &leftflag);
GetKeywordInteger (CommandString, "RIG", dispns, &right, &rightflag);
GetKeywordInteger (CommandString, "UP", dispnl, &up, &upflag);
GetKeywordInteger (CommandString, "DOW", dispnl, &down, &downflag);
GetKeywordSubcommand (CommandString, "FLI", &flipflag);
GetKeywordSubcommand (CommandString, "NOP", &noprompt_flag);
GetKeywordSubcommand (CommandString, "SOU", &Source_Flag);
GetKeywordInteger (CommandString, "AUT", 5 ,&auto_amount, &auto_stretch_flag);
strcat(CommandString," ");
if (Source_Flag > 0) /* display source image */
{
if (Source_File_Name[0] == '\0')
StatusLine(0,"No source file specified in label");
else
{
fp = fopen (BrowseName,"w");
if (fp == NULL)
StatusLine(0,"File can not be opened. Use SET BROWSE filename");
else
{
if (strcmp(Source_File_Name,"FFXX.LBL") == 0)
fmidr = TRUE;
if (strcmp(Source_File_Name,"C1FXX.LBL") == 0)
c1midr = TRUE;
if (strcmp(Source_File_Name,"C2FXX.LBL") == 0)
c2midr = TRUE;
if (strcmp(Source_File_Name,"C3FXX.LBL") == 0)
c3midr = TRUE;
if (fmidr || c1midr || c2midr || c3midr)
{
/* MJM's replacement code */
htile = centersamp/128 + 1;
vtile = centerline/128;
tile = vtile*8 + htile;
centerline = (centerline % 128);
centersamp = (centersamp % 128);
/* statusline(0,dispstr); what does this do?*/
strncpy(Source_File_Name,ImageFileName,
strlen(ImageFileName)-10);
Source_File_Name[strlen(ImageFileName)-10] = '\0';
if (fmidr)
strcat(Source_File_Name,"FF");
else if (c1midr)
strcat(Source_File_Name,"C1F");
else if (c2midr)
strcat(Source_File_Name,"C2F");
else if (c3midr)
strcat(Source_File_Name,"C3F");
/* MJM's original code */
strcpy(dispstr,Source_File_Name);
sprintf(Source_File_Name,"%s%02d.LBL",dispstr,tile);
Sampling_Factor = 8;
}
fprintf(fp,"file %s \n",Source_File_Name);
sl = Sampling_Factor*centerline-dispnl/2; if (sl < 1) sl = 1;
ss = Sampling_Factor*centersamp-dispns/2; if (ss < 1) ss = 1;
fprintf(fp,"disp sl %d ss %d\n",sl,ss);
centerline = Sampling_Factor*centerline;
centersamp = Sampling_Factor*centersamp;
fclose(fp); /* Close batch file and then execute it */
strcpy(CommandString,"BATCH ");
strcat(CommandString,BrowseName);
ClearDisplay(0);
DoBatch();
}
}
return(0);
}
strcpy(status,"");
if (auto_stretch_flag >= 0 && bitsperpix == 8 && !buffer_flag)
{
if (Histogram > 0L)
{
lseek(FCB[0].handle,Histogram,SEEK_SET);
read(FCB[0].handle,(char *)histbuf,1024);
total_samples = (long)IMCB[0].nl * (long)IMCB[0].ns;
}
else
{
if ((tempbuf = malloc(IMCB[0].ns)) == NULL)
{
FatalError( "Not enough memory for pixel buffer.\n");
return(1);
}
for (i = 1; i < IMCB[0].nl; i+=10)
{
if (ImageLocation == VIRTUAL_FILE)
ReadLine (0, tempbuf, i, 1, IMCB[0].ns, status);
else
ReadXMSLine (tempbuf, i, 1, IMCB[0].ns);
if (BadStatus(status))
{
free (tempbuf);
return(1);
}
for (samp = 1; samp < IMCB[0].ns; samp+=10)
{
(histbuf[tempbuf[samp]])++;
}
}
total_samples = ((long)IMCB[0].nl * (long)IMCB[0].ns)/100L;
free(tempbuf);
}
if (auto_amount > 49) auto_amount = 49;
limit = ((long)total_samples*(long)auto_amount)/100L;
count = 0L;
for (i=0;i<256 && count < limit;i++) count += histbuf[i];
DNlow = i;
count = 0L;
for (i=255;i>0 && count < limit;i--) count += histbuf[i];
DNhigh = i;
if (DNlow >= DNhigh) DNhigh = DNlow + 1;
sprintf (dispstr, "DN low : %5d DN high : %5d ",
DNlow, DNhigh);
StatusLine(2,dispstr);
}
else if (auto_stretch_flag >= 0)
StatusLine(0,"AUTOset only works on 8-bit images.");
if (subsample < 1) subsample = 1;
if (zoom < 1) zoom = 1;
if (subsample > 1) zoom = 1;
if ( (leftflag > -1) || (rightflag > -1) ||
(upflag > -1) || (downflag > -1) )
{
sl = lastsl;
ss = lastss;
if (leftflag > -1)
ss -= left;
if (rightflag > -1)
ss += right;
if (upflag > -1)
sl -= up;
if (downflag > -1)
sl += down;
}
if (sl < 1)
sl = 1;
else if (sl > nl)
sl = nl;
if (ss < 1)
ss = 1;
else if (ss > ns)
ss = ns;
/* CENTer command stuff */
if ( (centerflag >= 0) && (centerline > 0) )
{
nldd = zoom*nld / subsample;
nsdd = zoom*nsd / subsample;
if (nldd+sldd-1 > dispnl)
nldd = dispnl-sldd+1;
if (nsdd+ssdd-1 > dispns)
nsdd = dispns-ssdd+1;
nld = subsample*nldd / zoom;
nsd = subsample*nsdd / zoom;
sl = centerline - (nld / 2);
if (sl < 1) sl = 1;
ss = centersamp - (nsd / 2);
if (ss < 1) ss = 1;
CursorLine = zoom*(centerline - sl) / subsample + sldd;
CursorSample = zoom*(centersamp - ss) / subsample + ssdd;
}
lastsl = sl;
lastss = ss;
if (nld+sl-1 > nl)
nld = nl-sl+1;
if (nsd+ss-1 > ns)
nsd = ns-ss+1;
nldd = zoom*nld / subsample;
nsdd = zoom*nsd / subsample;
if (nldd+sldd-1 > dispnl)
nldd = dispnl-sldd+1;
if (nsdd+ssdd-1 > dispns)
nsdd = dispns-ssdd+1;
nld = subsample*nldd / zoom;
nsd = subsample*nsdd / zoom;
nldd = zoom*nld / subsample;
nsdd = zoom*nsd / subsample;
if (bitsperpix <= 8)
bytesneeded = zoom*nsd;
else if (bitsperpix == 32)
bytesneeded = 4*zoom*nsd;
else
bytesneeded = 2*zoom*nsd;
if ((tempbuf = malloc(bytesneeded)) == NULL)
{
FatalError( "Not enough memory for pixel buffer.\n");
}
if (bitsperpix >= 8)
{
bitshift = Round(log(((double)DNhigh-DNlow+1)/numDN) /0.693147);
NoScale = (DNlow==0) && (DNhigh==(1 << bitsperpix)-1);
}
else
bitshift = bitsperpix - Round(log((double)numDN)/0.693147);
/* Loop through image line by line */
strcpy(status,"");
if (zoom == 1)
{
line = sl;
Image_Line = sldd; /* Remember where image is displayed */
Image_Sample = ssdd; /* Ron Baalke - 05/91 */
Image_Height = nldd;
Image_Length = nsdd;
Image_Zoom = zoom;
Image_Subsample = subsample;
Image_Line1 = Image_Line; /* Make another copy for the WINDOW command */
Image_Sample1 = Image_Sample; /* Ron Baalke - 09/18/91 */
Image_Height1 = Image_Height;
Image_Length1 = Image_Length;
Image_Zoom1 = Image_Zoom;
Image_Subsample1 = Image_Subsample;
for (i = sldd; i < nldd+sldd; i++)
{
if (flipflag == 1)
j = nldd+sldd - i;
else
j = i;
if (buffer_flag)
GetBuffer(index, tempbuf, line, ss, nsd);
else if (ImageLocation == VIRTUAL_FILE)
ReadLine (0, tempbuf, line, ss, nsd, status);
else
ReadXMSLine (tempbuf, line, ss, nsd);
if (BadStatus(status))
{
free (tempbuf);
return(1);
}
FormatLine (tempbuf, nsdd, nsd, bitshift, NoScale, status);
if (BadStatus(status))
{
free (tempbuf);
return(1);
}
DisplayLine (tempbuf, j, ssdd, nsdd);
if (RefreshLines >= j)
PutRefresh (tempbuf, j, ssdd, nsdd);
line += subsample;
if (kbhit()) /* abort disp if keypressed mdm 2/19/88*/
{
if ((ch = getch()) == 0) ch = 0x80 | getch();
if (ch != 17) /* don't abort if cntl q */
{
i=nldd+sldd;
abort_disp = 1;
}
}
}
free(tempbuf);
}
else
{
i = sldd;
Image_Line = sldd; /* Remember where image is displayed */
Image_Sample = ssdd; /* Ron Baalke - 05/91 */
Image_Height = nld * zoom;
Image_Length = nsdd;
Image_Zoom = zoom;
Image_Subsample = subsample;
Image_Line1 = Image_Line; /* Make another copy for the WINDOW command */
Image_Sample1 = Image_Sample; /* Ron Baalke - 09/18/91 */
Image_Height1 = Image_Height;
Image_Length1 = Image_Length;
Image_Zoom1 = Image_Zoom;
Image_Subsample1 = Image_Subsample;
for (line = sl; line < nld+sl; line++)
{
if (flipflag == 1)
j = nld*zoom - i;
else
j = i;
if (buffer_flag)
GetBuffer(index, tempbuf, line, ss, nsd);
else if (ImageLocation == VIRTUAL_FILE)
ReadLine (0, tempbuf, line, ss, nsd, status);
else
ReadXMSLine (tempbuf, line, ss, nsd);
if (BadStatus(status))
{
free (tempbuf);
return(1);
}
FormatLine (tempbuf, nsdd, nsd, bitshift, NoScale, status);
if (BadStatus(status))
{
free (tempbuf);
return(1);
}
for (k = 1; k <= zoom; k++)
{
DisplayLine (tempbuf, j, ssdd, nsdd);
if (RefreshLines >= j)
PutRefresh (tempbuf, j, ssdd, nsdd);
i++; j++;
if (kbhit()) /* abort disp if keypressed mdm 2/19/88*/
{
if ((ch = getch()) == 0) ch = 0x80 | getch();
if (ch != 17) /* don't abort if cntl q */
{
line = nld+sl;
k=zoom;
abort_disp = 1;
}
}
}
}
free(tempbuf);
}
if (buffer_flag)
{
nl = nl_save;
ns = ns_save;
DNhigh = high_save;
DNlow = low_save;
nld = nld_save; nsd = nsd_save; sl = sl_save; ss = ss_save;
nldd = nldd_save; nsdd = nsdd_save; sldd = sldd_save; ssdd = ssdd_save;
centerline = center1_save;
centersamp = center2_save;
bitsperpix = bits_save;
}
/* If noprompt_flag is raised, then pause until a key is hit by the
user. This will delay the command line prompt from being delayed */
if (noprompt_flag == 1)
i = getch();
/* If SelectFlag is raised indicating the SELECT option has been
selected in the BROWSE command, then prompt user
Ron Baalke 11/04/90 */
if (SelectFlag)
{
StatusLine(1,"Select this image (Y=Yes N=No (default) Q=Quit) ?");
i = getch();
if ((i == 89) || (i == 121)) /* 'Y' or 'y' */
fprintf(SelectFile,"%s\n",ImageFileName);
else if ((i == 81) || (i == 113) || (i==27)) /* 'Q', 'q' or ESC */
abort_disp = 1;
}
}