home *** CD-ROM | disk | FTP | other *** search
/ PC Graphics Unleashed / PC Graphics Unleashed.iso / ch20 / source / shrinkmr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-09  |  1.9 KB  |  91 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <io.h>
  4.  
  5. #define INSIZE  256
  6. #define OUTSIZE 128
  7. #define BPV     2       /* Bytes per voxel */
  8.  
  9.  
  10. void main(int argc, char **argv)
  11. {
  12. int i,j,k,l,m;
  13. int Rfactor = INSIZE / OUTSIZE;         
  14. FILE *in, *out;
  15. short inframe[INSIZE][INSIZE];
  16. short outframe[OUTSIZE][OUTSIZE];
  17. int filesize, numslices;
  18. int temp;
  19.  
  20.  
  21.     /* Ensure proper syntax */
  22.     
  23.     if(argc != 3)
  24.       {
  25.       printf("\nUsage:: shrinkMR infile outfile\n\n");
  26.       exit(1);
  27.       }
  28.  
  29.  
  30.     /* Open the files */
  31.     
  32.     in = fopen(argv[1], "rb");
  33.     if(in == NULL)
  34.       {
  35.       printf("\nCould not open file %s.\n\n", argv[1]);
  36.       exit(2);
  37.       }
  38.  
  39.     out = fopen(argv[2], "wb");
  40.     if(out == NULL)
  41.       {
  42.       printf("\nCould not create file %s.\n\n", argv[2]);
  43.       exit(3);
  44.       }
  45.     
  46.     
  47.     /* Determine number of slices in the volume */
  48.     
  49.     filesize = filelength( fileno(in) );
  50.     numslices = filesize / INSIZE / INSIZE / BPV;
  51.     if(numslices * INSIZE * INSIZE * BPV != filesize)
  52.       {
  53.       printf("\nThe size of the input file is incorrect.\n\n");
  54.       exit(4);
  55.       }
  56.     
  57.     
  58.     /* Processing Loop */
  59.  
  60.     for(i=0; i<numslices; i++)
  61.       {
  62.       fread(inframe, INSIZE * INSIZE, BPV, in);
  63.       printf("Processing slice %d/%d.\n", i+1, numslices);
  64.  
  65.       
  66.       /* Mask out upper nibble */
  67.       
  68.       for(j=0; j<INSIZE; j++)
  69.         for(k=0; k<INSIZE; k++)
  70.           inframe[j][k] &= 0x0FFF;
  71.  
  72.       
  73.       /* Size reduction by averaging */
  74.  
  75.       for(j=0; j<OUTSIZE; j++)
  76.         for(k=0; k<OUTSIZE; k++)
  77.           {
  78.           temp = 0;
  79.           for(l=0; l<Rfactor; l++)
  80.             for(m=0; m<Rfactor; m++)  
  81.               temp += inframe[j*Rfactor+l][k*Rfactor+m]; 
  82.           outframe[j][k] = temp / Rfactor / Rfactor;
  83.           }
  84.  
  85.       fwrite(outframe, OUTSIZE * OUTSIZE, BPV, out);
  86.       }
  87.  
  88.     fclose(in);
  89.     fclose(out);
  90. }
  91.