home *** CD-ROM | disk | FTP | other *** search
- #include <stdlib.h>
- #include <stdio.h>
- #include <conio.h>
- #include <io.h>
- #include <math.h>
-
-
-
- #define BUFFERSIZE 128*128
-
-
- double rnd(double val);
-
-
- void main(int argc, char **argv)
- {
- int i,j,k;
- FILE *in, *out;
- int win, lev;
- int filesize;
- short inbuffer[BUFFERSIZE];
- unsigned char outbuffer[BUFFERSIZE];
- int bpf;
- float temp;
-
-
- /* Ensure correct syntax */
-
- if(argc != 5)
- {
- printf("Usage:: WinLev infile outfile win lev.\n\n");
- exit(1);
- }
-
-
- /* Setup input and output files */
-
- in = fopen(argv[1], "rb");
- if(in == NULL)
- {
- printf("Unable to open file %s.\n\n", argv[1]);
- exit(2);
- }
-
- out = fopen(argv[2], "wb");
- if(out == NULL)
- {
- printf("Error creating / opening file %s.\n\n", argv[2]);
- exit(2);
- }
-
-
- /* Verify window and level values */
-
- win = atoi(argv[3]);
- lev = atoi(argv[4]);
-
- if((win < 0) || (win > 4096))
- {
- printf("Window not within allowable range(1 - 4096).\n\n");
- exit(3);
- }
-
- if(((lev-win/2) < 0) || ((lev+win/2) > 4096))
- {
- printf("Level puts window out of range(0 - 4095).\n\n");
- exit(4);
- }
-
-
- /* Calculate number of buffers per file */
-
- filesize = filelength( fileno(in));
- bpf = filesize / sizeof(inbuffer);
-
-
- /* Processing loop */
-
- for(i=0; i<bpf; i++)
- {
-
- fread(inbuffer, sizeof(inbuffer), 1, in);
-
- for(j=0; j<BUFFERSIZE; j++)
- {
- temp = inbuffer[j] - (lev-win/2);
- temp = temp*255.0/(float)win;
- if(temp <= 0)
- outbuffer[j] = 0;
- if(temp >= 255)
- outbuffer[j] = 255;
- else
- outbuffer[j] = (unsigned char)rnd((double)temp);
- }
-
- fwrite(outbuffer, sizeof(outbuffer), 1, out);
- }
-
- fclose(in);
- fclose(out);
-
- }
-
-
- double rnd(double val)
- {
- double tempval;
-
- tempval = floor(val);
- if((val - tempval) > 0.5)
- return(tempval + 1.0);
- else return(tempval);
- }
-