home *** CD-ROM | disk | FTP | other *** search
/ PC Graphics Unleashed / PC Graphics Unleashed.iso / ch20 / source / display.c next >
Encoding:
C/C++ Source or Header  |  1994-09-28  |  4.8 KB  |  265 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <graphics.h>
  5. #include <io.h>
  6. #include <malloc.h>
  7. #include <string.h>
  8. #include <math.h>
  9.  
  10.  
  11.  
  12. void main(int argc, char **argv)
  13. {
  14. int i,j,k,l;
  15. FILE *datafile;
  16. FILE *lutfile;
  17. unsigned char LUT[256][3];
  18. unsigned char *buffer;
  19. char **Images;
  20. int filesize;
  21. int slicesize;           
  22. int numslices;
  23. int imsize;
  24. int grdriver, grmode, grerror;
  25. int startx, starty;
  26. int histogram[256];
  27. float avg;
  28. short disp[256];
  29.  
  30.  
  31.  
  32.   /* Ensure proper syntax */
  33.   
  34.   if(argc != 4)
  35.     {
  36.     printf("Usage::  Display datafile size LUTfile.\n\n");
  37.     exit(1);
  38.     }
  39.  
  40.  
  41.   /* Check for valid size */
  42.  
  43.   slicesize = atoi(argv[2]);
  44.   if((slicesize != 128) && (slicesize != 256))
  45.     {
  46.     printf("Resolution %d not supported.\n",slicesize);
  47.     exit(3);
  48.     }
  49.  
  50.   
  51.   /* open input files */
  52.   
  53.   datafile = fopen(argv[1], "rb");
  54.   if(datafile == NULL)
  55.     {
  56.     printf("Can not open file: %s.\n\n", argv[1]);
  57.     exit(2);
  58.     }
  59.  
  60.   lutfile = fopen(argv[3], "r");
  61.   if(lutfile == NULL)
  62.     {
  63.     printf("Can not open file: %s.\n\n", argv[3]);
  64.     exit(4);
  65.     }
  66.  
  67.   
  68.   /* Load the LUT structure */
  69.  
  70.   for(i=0; i<256; i++)
  71.     for(j=0;j<3;j++)
  72.       { 
  73.       fscanf(lutfile, "%d", &k);
  74.       LUT[i][j] = (unsigned char)k;
  75.       }
  76.  
  77.   fclose(lutfile);
  78.  
  79.   
  80.   /* Determine the number of slices in the volume */
  81.  
  82.   filesize = filelength( fileno(datafile) );
  83.   numslices = filesize / slicesize / slicesize;
  84.  
  85.   
  86.   /* check graphics hardware */
  87.  
  88.   grdriver = DETECTX;
  89.   grmode = 0;
  90.   detectgraph(&grdriver, &grmode);
  91.   switch(grdriver)
  92.     {
  93.     case VESA256:
  94.     case ATI256:
  95.     case COMPAQ:
  96.     case TSENG3256:
  97.     case TSENG4256:
  98.     case GENOA5:
  99.     case GENOA6:
  100.     case OAK:
  101.     case PARADIS256:
  102.     case TECMAR:
  103.     case TRIDENT256:
  104.     case VIDEO7:
  105.     case VIDEO7II:
  106.       break;
  107.  
  108.     default:
  109.       printf("Unable to detect 256 color graphics adapter.\n");
  110.       exit(5);
  111.       break;
  112.     }
  113.   
  114.   
  115.   grmode = 1;
  116.   initgraph(&grdriver, &grmode, "");
  117.   grerror = graphresult();
  118.   if(grerror)
  119.     {
  120.     closegraph();
  121.     printf("Error %d initializing graphics mode.\n", grerror);
  122.     exit(6);
  123.     }
  124.  
  125.   
  126.   /* Allocate memory for the image caches */
  127.   
  128.   Images = (char **)malloc( numslices * sizeof(char *));
  129.   if(Images == NULL)
  130.     {
  131.     closegraph();
  132.     printf("Error allocating memory.\n");
  133.     exit(7);
  134.     }
  135.   
  136.   for(i=0;i<numslices; i++)  
  137.     {
  138.     Images[i] = (char *)malloc( 
  139.                   imagesize(0,0, slicesize-1,slicesize-1));
  140.     if(Images[i] == NULL)
  141.       {
  142.       closegraph();
  143.       printf("Error allocating memory.\n");
  144.       exit(7);
  145.       }
  146.     }
  147.   
  148.   
  149.   /* Allocate memory for the disk buffer. */
  150.  
  151.   buffer = (unsigned char *)malloc( slicesize*slicesize );
  152.   if(buffer == NULL)
  153.     {
  154.     closegraph();
  155.     printf("Error allocating memory for buffer.\n\n");
  156.     exit(7);
  157.     }
  158.  
  159.   
  160.   /* Program the hardware palette */
  161.   
  162.   for(i=0; i<256; i++)
  163.     setrgbpalette(i, LUT[i][0], LUT[i][1], LUT[i][2]);
  164.   
  165.   
  166.   /* Clear the histogram */
  167.  
  168.   for(i=0;i<256;i++)
  169.     histogram[i] = 0;
  170.   
  171.   
  172.   /* Display the initial images while building the cache */
  173.   /*  and the histogram */
  174.  
  175.   switch(slicesize)
  176.     {
  177.     case 256:
  178.       startx = 40;
  179.       starty = 240 - slicesize/2;
  180.       break;
  181.  
  182.     case 128:
  183.       startx = 85;
  184.       starty = 240 - slicesize/2;
  185.       break;
  186.     }
  187.  
  188.   for(i=0; i<numslices; i++)
  189.     {
  190.     fread(buffer, slicesize*slicesize, 1, datafile);
  191.  
  192.     for(j=0; j<slicesize; j++)
  193.       for(k=0; k<slicesize; k++) 
  194.         {
  195.         histogram[ buffer[j*slicesize + k]] ++;
  196.         putpixel(startx+k, starty+j, buffer[j*slicesize + k]);
  197.         }
  198.  
  199.     getimage(startx, starty, startx+slicesize-1, 
  200.              starty+slicesize-1, (void *)Images[i]); 
  201.     }
  202.  
  203.   fclose(datafile);
  204.   free( (void *)buffer);
  205.   
  206.  
  207.   /* get the average histogram value */
  208.  
  209.   avg = 0.0;
  210.   j = 0;
  211.  
  212.   for(i=0; i<256; i++)
  213.     {
  214.     avg += (float)histogram[i];
  215.     if(histogram[i] != 0)
  216.       j++;
  217.     }
  218.  
  219.   avg = avg/(float)j;
  220.  
  221.  
  222.   /* setup display array */
  223.  
  224.   for(i=0; i<256; i++)
  225.     {
  226.     k = (int)floor( (double)(histogram[i] / avg * 200.0));
  227.     if(k > 400)
  228.       k = 400;
  229.     disp[i] = k;
  230.     }
  231.  
  232.   /* Display the histogram. */
  233.  
  234.   for(i=0; i<256; i++)
  235.     {
  236.     setcolor(i);
  237.     switch(slicesize)
  238.       {
  239.       case 256:
  240.         line(336+i, 440, 336+i, 440 - disp[i]);
  241.         break;
  242.  
  243.       case 128:
  244.         line(298+i, 440, 298+i, 440 - disp[i]);
  245.         break;
  246.       }
  247.     }  
  248.  
  249.   while(!kbhit())
  250.     {
  251.     for(i=0;i<numslices;i++)
  252.       putimage(startx, starty, (void *)Images[i], COPY_PUT);
  253.     }
  254.  
  255.  
  256.   getch();
  257.   closegraph();
  258.  
  259.   for(i=0;i<numslices;i++)
  260.     free( (void *)Images[i]);
  261.  
  262.   free( (void *)Images);
  263.  
  264. }
  265.