home *** CD-ROM | disk | FTP | other *** search
- #include <stdlib.h>
- #include <stdio.h>
- #include <conio.h>
- #include <graphics.h>
- #include <io.h>
- #include <malloc.h>
- #include <string.h>
- #include <math.h>
-
-
-
- void main(int argc, char **argv)
- {
- int i,j,k,l;
- FILE *datafile;
- FILE *lutfile;
- unsigned char LUT[256][3];
- unsigned char *buffer;
- char **Images;
- int filesize;
- int slicesize;
- int numslices;
- int imsize;
- int grdriver, grmode, grerror;
- int startx, starty;
- int histogram[256];
- float avg;
- short disp[256];
-
-
-
- /* Ensure proper syntax */
-
- if(argc != 4)
- {
- printf("Usage:: Display datafile size LUTfile.\n\n");
- exit(1);
- }
-
-
- /* Check for valid size */
-
- slicesize = atoi(argv[2]);
- if((slicesize != 128) && (slicesize != 256))
- {
- printf("Resolution %d not supported.\n",slicesize);
- exit(3);
- }
-
-
- /* open input files */
-
- datafile = fopen(argv[1], "rb");
- if(datafile == NULL)
- {
- printf("Can not open file: %s.\n\n", argv[1]);
- exit(2);
- }
-
- lutfile = fopen(argv[3], "r");
- if(lutfile == NULL)
- {
- printf("Can not open file: %s.\n\n", argv[3]);
- exit(4);
- }
-
-
- /* Load the LUT structure */
-
- for(i=0; i<256; i++)
- for(j=0;j<3;j++)
- {
- fscanf(lutfile, "%d", &k);
- LUT[i][j] = (unsigned char)k;
- }
-
- fclose(lutfile);
-
-
- /* Determine the number of slices in the volume */
-
- filesize = filelength( fileno(datafile) );
- numslices = filesize / slicesize / slicesize;
-
-
- /* check graphics hardware */
-
- grdriver = DETECTX;
- grmode = 0;
- detectgraph(&grdriver, &grmode);
- switch(grdriver)
- {
- case VESA256:
- case ATI256:
- case COMPAQ:
- case TSENG3256:
- case TSENG4256:
- case GENOA5:
- case GENOA6:
- case OAK:
- case PARADIS256:
- case TECMAR:
- case TRIDENT256:
- case VIDEO7:
- case VIDEO7II:
- break;
-
- default:
- printf("Unable to detect 256 color graphics adapter.\n");
- exit(5);
- break;
- }
-
-
- grmode = 1;
- initgraph(&grdriver, &grmode, "");
- grerror = graphresult();
- if(grerror)
- {
- closegraph();
- printf("Error %d initializing graphics mode.\n", grerror);
- exit(6);
- }
-
-
- /* Allocate memory for the image caches */
-
- Images = (char **)malloc( numslices * sizeof(char *));
- if(Images == NULL)
- {
- closegraph();
- printf("Error allocating memory.\n");
- exit(7);
- }
-
- for(i=0;i<numslices; i++)
- {
- Images[i] = (char *)malloc(
- imagesize(0,0, slicesize-1,slicesize-1));
- if(Images[i] == NULL)
- {
- closegraph();
- printf("Error allocating memory.\n");
- exit(7);
- }
- }
-
-
- /* Allocate memory for the disk buffer. */
-
- buffer = (unsigned char *)malloc( slicesize*slicesize );
- if(buffer == NULL)
- {
- closegraph();
- printf("Error allocating memory for buffer.\n\n");
- exit(7);
- }
-
-
- /* Program the hardware palette */
-
- for(i=0; i<256; i++)
- setrgbpalette(i, LUT[i][0], LUT[i][1], LUT[i][2]);
-
-
- /* Clear the histogram */
-
- for(i=0;i<256;i++)
- histogram[i] = 0;
-
-
- /* Display the initial images while building the cache */
- /* and the histogram */
-
- switch(slicesize)
- {
- case 256:
- startx = 40;
- starty = 240 - slicesize/2;
- break;
-
- case 128:
- startx = 85;
- starty = 240 - slicesize/2;
- break;
- }
-
- for(i=0; i<numslices; i++)
- {
- fread(buffer, slicesize*slicesize, 1, datafile);
-
- for(j=0; j<slicesize; j++)
- for(k=0; k<slicesize; k++)
- {
- histogram[ buffer[j*slicesize + k]] ++;
- putpixel(startx+k, starty+j, buffer[j*slicesize + k]);
- }
-
- getimage(startx, starty, startx+slicesize-1,
- starty+slicesize-1, (void *)Images[i]);
- }
-
- fclose(datafile);
- free( (void *)buffer);
-
-
- /* get the average histogram value */
-
- avg = 0.0;
- j = 0;
-
- for(i=0; i<256; i++)
- {
- avg += (float)histogram[i];
- if(histogram[i] != 0)
- j++;
- }
-
- avg = avg/(float)j;
-
-
- /* setup display array */
-
- for(i=0; i<256; i++)
- {
- k = (int)floor( (double)(histogram[i] / avg * 200.0));
- if(k > 400)
- k = 400;
- disp[i] = k;
- }
-
- /* Display the histogram. */
-
- for(i=0; i<256; i++)
- {
- setcolor(i);
- switch(slicesize)
- {
- case 256:
- line(336+i, 440, 336+i, 440 - disp[i]);
- break;
-
- case 128:
- line(298+i, 440, 298+i, 440 - disp[i]);
- break;
- }
- }
-
- while(!kbhit())
- {
- for(i=0;i<numslices;i++)
- putimage(startx, starty, (void *)Images[i], COPY_PUT);
- }
-
-
- getch();
- closegraph();
-
- for(i=0;i<numslices;i++)
- free( (void *)Images[i]);
-
- free( (void *)Images);
-
- }
-