/************************************************************************/
/*                                                                      */
/*         Program: segment.c                                           */
/*         Purpose: to segment a 3-D volume into 3 phases               */
/*                     porosity						*/
/*		       hydration products				*/
/*                     unhydrated material				*/
/*         Input file: 3-D image, one character per voxel		*/
/*         Output file: 3-D image, one integer per line per voxel	*/
/*         Programmer: Dale P. Bentz                                    */
/*                     National Institute of Standards and Technology   */
/*                     100 Bureau Drive Stop 8621                       */
/*                     Gaithersburg, MD 20899-8621                      */
/*                     Phone: (301) 975-5865                            */
/*                     Fax: (301) 990-6891                              */
/*                     E-mail: dale.bentz@nist.gov                      */
/*                                                                      */
/*         Date: 2001                                                   */
/*                                                                      */
/***********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* XSIZE, YSIZE, and ZSIZE specify the size of the 3-D image in the */
/* x, y, and z directions, respectively (should be set by the user */
/* before compiling the program) */
#define XSIZE 300
#define YSIZE 300
#define ZSIZE 300
/* define phase IDs for porosity, anhydrous material, and hydration products */
/* Note it is assumed that the porosity is the darkest phase in the 3-D image */
/* the anhydrous material is the brightest and the hydration products are */
/* intermediate in brightness */
#define POROSITY 0
#define ANHYDROUS 1
#define PRODUCTS 2

main(){
	FILE *infile,*outfile;
	char filein[80],fileout[80];
	int ix,iy,iz,valout;
        int th0,th1;
        long int ct1=0,ct2=0,ct3=0;   /* counters for the three phases */
	unsigned char valin;

        printf("This program will segment a greylevel (0-255) image \n");
        printf("into 3 phases as specified by two threshold levels \n");
        printf("Phase 1- greylevel < limit1 \n");
        printf("Phase 2- limit1 <= greylevel < limit2 \n");
        printf("Phase 3- greylevel >= limit2 \n");
        printf("Enter the 2 threshold values (limit1 limit2: range of 0-255) \n");
        scanf("%d %d",&th0,&th1);

	printf("Enter name of 3-D image file for input \n");
	scanf("%s",filein);
	printf("%s \n",filein);
	printf("Enter name of file for output of segemented 3-D image \n");
	scanf("%s",fileout);
	printf("%s \n",fileout);

	infile=fopen(filein,"rb");
	outfile=fopen(fileout,"w");

/* Simply read in the 3-D image voxel by voxel and perform segementation */
	for(iz=0;iz<ZSIZE;iz++){
	for(iy=0;iy<YSIZE;iy++){
	for(ix=0;ix<XSIZE;ix++){

		fscanf(infile,"%c",&valin);
		valout=valin;
                if(valout<th0){
			valout=POROSITY;
                        ct1+=1;
		}
		else if (valout<th1){
			valout=PRODUCTS;
                        ct2+=1;
                }
                else{
			valout=ANHYDROUS;
                        ct3+=1;
		}
		fprintf(outfile,"%d\n",valout);
	}
	}
	}

        printf("count for phase1 (porosity) is %ld\n",ct1);
        printf("count for phase2 (products) is %ld\n",ct2);
        printf("count for phase3 (anhydrous) is %ld\n",ct3);
	fclose(infile);
	fclose(outfile);
}