Beispiel #1
0
/*---------------------------------------------------------------
 Routine : load_file_cb
 Purpose : This routine performs the action of reading the
 selected file to obtain the required data. The format of the
 file, and the type of the data is only known to the registered
 action procedure.

 If the filename presented is NULL, the file selection dialog
 is posted to allow the selection of a filename.

 If any errors occur the relevent error dialogs are posted. 
---------------------------------------------------------------*/
static void load_file_cb(fstruct *client_data, int called_from)
{

	if ( client_data->filename == NULL ) {
		select_filename_and_act_cb(client_data);
	} else {
		if ( !file_exists( client_data->filename ) ) {
			/* file given, does not exist */

			file_open_error( client_data, called_from );

		} else {

			if ( (*(client_data->action_procedure))(
					client_data->data_type, client_data->filename) ==
				FILE_ERROR ) 
			{
				file_open_error( client_data, called_from );
			}

			end_thread_cb(client_data);

		} /* end if file exists */
	}
} /* load_file_cb */
Beispiel #2
0
void write_analyze_image(const char *filename, unsigned char *im, int nx, int ny, int nz, float dx, float dy, float dz, int v)
{
	FILE *fp;
	char hdrfile[1024];
	char imgfile[1024];
	unsigned char min, max;
	int nv;

	struct dsr hdr;

	nv = nx*ny*nz;

	get_analyze_file_names(filename, hdrfile, imgfile);

	if(v) printf("\nWriting %s ...\n",imgfile);

	create_analyze_hdr(&hdr, nx, ny, nz, 2, dx, dy, dz);

	minmax(im, nv, min, max);
	hdr.dime.glmin=min;
	hdr.dime.glmax=max;

   fp=fopen(imgfile,"w");
   if(fp==NULL) file_open_error(imgfile);
   fwrite(im,sizeof(unsigned char),nv,fp);
   fclose(fp);

   fp=fopen(hdrfile,"w");
   if(fp==NULL) file_open_error(hdrfile);
   fwrite(&hdr,sizeof(struct dsr),1,fp);
   fclose(fp);
}
Beispiel #3
0
boost::shared_ptr<FILE>
file_open( char const * name, char const * mode )
    {
    if( FILE * f=fopen(name,mode) )
        return boost::shared_ptr<FILE>(f,fclose);
    else
        throw file_open_error() <<
			boost::errinfo_file_name(name) <<
            clib_failure("fopen",errno);
    }
Beispiel #4
0
void read_image(char *file, char *im, int n)
{
   FILE *fp;

   if(im==NULL) return;
	
   fp = fopen(file,"r");
   if(fp==NULL) file_open_error(file);
   fread(im,1,n,fp);
   fclose(fp);
}
Beispiel #5
0
void open_files(char *name)
{
    char name1[200],name2[200],name4[200];
    int oflag;

    oflag=O_CREAT | O_TRUNC | O_WRONLY;

    if(NO_OUTPUT_FILE)return;

    if(CALC_VOLUME_ONLY)return;

    strcpy(Gfilename,name);
    strcpy(name4,name);
    strcat(name4,"_1st.poly");

    if(DIFF_LEVEL_FILES)return;

    strcpy(name1,name);
    strcat(name1,".poly");
    strcpy(name2,name);
    strcat(name2,"_untiled.poly");

    strcpy(poly_file_name, name1);

#ifdef i860
    if((Tiled_fd=gopen(name1,oflag,M_LOG,0644))==-1) file_open_error(name1);
    if((Untiled_fd=gopen(name2,oflag,M_LOG,0644))==-1)file_open_error(name2);
#else

    Tiled_fd = fopen(name1,"w");
    if (Tiled_fd == 0) file_open_error(name1);

    Untiled_fd = fopen(name1,"w");
    if (Untiled_fd == 0) file_open_error(name2);

    if(STORE_1ST_PASS)
    {
        if((Debug_fd = fopen(name4, "w"))==0) file_open_error(name1);
    }
#endif
}
Beispiel #6
0
boost::shared_ptr<FILE>
open_file( char const * file, char const * mode )
	{
	if( FILE * f=fopen(file,mode) )
		return boost::shared_ptr<FILE>(f,fclose);
	else
		BOOST_THROW_EXCEPTION(
			file_open_error() <<
			boost::errinfo_api_function("fopen") <<
			boost::errinfo_errno(errno) <<
			boost::errinfo_file_name(file) <<
			boost::errinfo_file_open_mode(mode) );
	}
Beispiel #7
0
// If is empty, i.e. orient="", that the orientation is read from imfile header
// otherwise, the orien overrides the header information
void standard_PIL_transformation(const char *imfile, const char *lmfile, char *orient, int verbose, float *TPIL)
{
   char modelfile[1024]="";

   DIM dim;
   float ac[4]={0.0, 0.0, 0.0, 1.0};
   float pc[4]={0.0, 0.0, 0.0, 1.0};
   float rp[4]={0.0, 0.0, 0.0, 1.0};

   //////////////////////////////////////////////////////////////////////////////////
   // Detect the MSP plane 
   //////////////////////////////////////////////////////////////////////////////////
   float Tmsp[16]; // takes the image to PIL orientation -- no AC/PC alignment

   findMSP(imfile,orient,lmfile,Tmsp,verbose,dim);

   ///////////////////////////////////////////////////////////////////////////////////////////////
   // AC/PC detection 
   ///////////////////////////////////////////////////////////////////////////////////////////////

   opt_AC=opt_PC=opt_RP=YES;  // by default find these automatically

   if( lmfile[0]!='\0' )
   {
      FILE *fp;
      
      fp = fopen(lmfile,"r");
      if(fp==NULL) file_open_error(lmfile);
      fscanf(fp,"%f %f %f\n", &ac[0], &ac[1], &ac[2]); ac[3]=1;
      fscanf(fp,"%f %f %f\n", &pc[0], &pc[1], &pc[2]); pc[3]=1;
      fscanf(fp,"%f %f %f\n", &rp[0], &rp[1], &rp[2]); rp[3]=1;
      fclose(fp);

      opt_AC=opt_PC=opt_RP=NO;  // Do not find AC, PC, RP
   }

   opt_MSP=NO;
   detect_AC_PC_MSP(imfile,orient,modelfile,ac,pc,rp,Tmsp,verbose,0);
   //restore to default state, this is important for subsequent calls to detect_AC_PC_MSP()
   opt_MSP=YES; 

   // convert the AC/PC from (i,j,k) in original space to (x,y,z) in PIL space
   orig_ijk_to_pil_xyz(Tmsp, dim, ac, pc);

   // if opt_CENTER_AC=NO, puts FOV center at halfway btw AC and PC to match $ARTHOME/PILbrain.nii
   // if opt_CENTER_AC=YES, makes AC the FOV center
   ACPCtransform(TPIL, Tmsp, ac, pc, opt_CENTER_AC);

   return;
}
Beispiel #8
0
FILE* openATiled(int level)
{
    char name[230],exten[10];
    FILE* fd;

    sprintf(exten,"L%d",level);
    strcpy(name,Gfilename);
    strcat(name,exten);
    strcat(name,".poly");

    fd = fopen(name, "w");
    if (fd == 0) file_open_error(name);
    return fd;
}
Beispiel #9
0
void read_analyze_hdr(struct dsr *hdr, char *filename)
{
   FILE *fp;
   int n;

   fp=fopen(filename,"r");
   if(fp==NULL) file_open_error(filename);

   n=(int)fread(hdr,sizeof(struct dsr),1,fp);

   if(n!=1) 
   {
      printf("error: reading from file %s !\n",filename);
      fclose(fp);
      exit(0);
   }

   fclose(fp);
}
Beispiel #10
0
char *read_image(char *file, int n)
{
	FILE *fp;
	char *im;
	int nread;

	im =(char *)calloc(n,1);

	if(im==NULL) return(NULL);
	
	fp = fopen(file,"r");
    if(fp==NULL) file_open_error(file);
	nread=(int)fread(im,1,n,fp);
	fclose(fp);

	if(nread!=n) { free(im); return(NULL);}

	return(im);
}
void getNiftiImageOrientation(const char *filename, char *orientation)
{
   FILE *fp;
   nifti_1_header hdr;
   int swapflg=0;

   orientation[0]='\0';

   // ensure that the specified image has either a .hdr or a .nii extension
   if( !checkNiftiFileExtension(filename) )
   {
      errorMessage("The image filename must have a `.hdr' or `.nii' extension.");
   }

   fp = fopen(filename,"r");

   if(fp==NULL)
   {
      file_open_error(filename);
   }

   if( fread(&hdr, sizeof(nifti_1_header), 1, fp) != 1 )
   {
      errorMessage("I have trouble reading the specified image file.");
   }

   fclose(fp);

   // looks like ANALYZE 7.5, cannot determine orientation
   if( hdr.magic[0]!='n' ||  (hdr.magic[1]!='+' && hdr.magic[1]!='i') ||  hdr.magic[2]!='1')
   {
      printf("\nWarning: Could not determine %s image orientation ...\n",filename);
      return;
   }

   // if dim[0] is outside range 1..7, then the header information
   // needs to be byte swapped appropriately
   if(hdr.dim[0]<1 || hdr.dim[0]>7) 
   {
      swapflg=1;
   }

   // Here I am only byte swapping the header fields relevant for determining the image orientation
   if(swapflg)
   {
      swapByteOrder( (char *)&(hdr.qform_code), sizeof(short));
      swapByteOrder( (char *)&(hdr.sform_code), sizeof(short));
      swapByteOrder( (char *)&(hdr.quatern_b), sizeof(float));
      swapByteOrder( (char *)&(hdr.quatern_c), sizeof(float));
      swapByteOrder( (char *)&(hdr.quatern_d), sizeof(float));
      swapByteOrder( (char *)&(hdr.qoffset_x), sizeof(float));
      swapByteOrder( (char *)&(hdr.qoffset_y), sizeof(float));
      swapByteOrder( (char *)&(hdr.qoffset_z), sizeof(float));
      for(int i=0; i<4; i++)
      {
         swapByteOrder( (char *)&(hdr.srow_x[i]), sizeof(float));
         swapByteOrder( (char *)&(hdr.srow_y[i]), sizeof(float));
         swapByteOrder( (char *)&(hdr.srow_z[i]), sizeof(float));
      }
   }

   if(hdr.qform_code == 0 && hdr.sform_code == 0) 
   {
      printf("\nWarning: Could not determine %s image orientation ...\n",filename);
      printf("\nWarning: The header of this \"NIFTI\" file does not contain orientation information.\n");
      return;
   }

   if(hdr.qform_code > 0 )
   {
      mat44 R;

      R = nifti_quatern_to_mat44(hdr.quatern_b, hdr.quatern_c, hdr.quatern_d, hdr.qoffset_x, hdr.qoffset_y, 
      hdr.qoffset_z, hdr.pixdim[1], hdr.pixdim[2], hdr.pixdim[3], hdr.pixdim[0]);

      orientation[0] = directionCode(R.m[0][0],R.m[1][0],R.m[2][0]);
      orientation[1] = directionCode(R.m[0][1],R.m[1][1],R.m[2][1]);
      orientation[2] = directionCode(R.m[0][2],R.m[1][2],R.m[2][2]);
      orientation[3] = '\0';
   }
   else
   {
      orientation[0] = directionCode(hdr.srow_x[0],hdr.srow_y[0],hdr.srow_z[0]);
      orientation[1] = directionCode(hdr.srow_x[1],hdr.srow_y[1],hdr.srow_z[1]);
      orientation[2] = directionCode(hdr.srow_x[2],hdr.srow_y[2],hdr.srow_z[2]);
      orientation[3] = '\0';
   }

   return;
}
Beispiel #12
0
int main(int argc, char * argv[])
{
    int * hits;
    int period;
    int i;
    int length = 0;
    int bufsize;
    char * buffer;
    int maxkey = KAPPA_DEF_MAXKEY;
    float hitrate;
    float threshold = KAPPA_DEF_THRESHOLD;
    int z;
    extern int opterr;
    extern char *optarg;
    int errflg = 0;
    
    opterr = 0;
    while ((z = getopt(argc, argv, "m:t:i:o")) != EOF) {
	switch ((char)z) {
	case 'i':
	    if (freopen(optarg, "r", stdin) == NULL) {
		file_open_error();
	    }
	    break;
	case 'o':
	    if (freopen(optarg, "w", stdout) == NULL) {
		file_open_error();
	    }
	    break;
	case 'm':
	    maxkey = atoi(optarg);
	    break;
	case 't':
	    threshold = atof(optarg);
	    break;
	case '?':
	    errflg = TRUE;
	}
    }
    if (errflg) {
	usage(KAPPA_USAGE);
    }
    
    hits = (int *)malloc((maxkey + 1) * sizeof(int));
    bufsize = (maxkey > 63) ? maxkey + 1 : 64;
    buffer = (char *)malloc(bufsize * sizeof(char));
    if (hits == NULL || buffer == NULL) {
	memory_error();
    }
    
    for (i = 0; i <= maxkey; i++) {
	hits[i] = 0;
    }

    while ((z = getchar()) != EOF) {
	buffer[length % bufsize] = (char)z;
	for (period = 1; period <= maxkey; period++) {
	    if (length >= period) {
		if (buffer[length % bufsize] ==
		    buffer[(length - period) % bufsize]) {
		    hits[period]++;
		}
	    }
	}
	length++;
    } 

    printf("Offset\t Hits\t  %% Hits\n");
    for (period = 1; period <= maxkey; period++) {
	hitrate = (float)hits[period] * 100 / (float)(length - (period));
	printf("%4d\t%5d\t%10.6f", period, hits[period], hitrate);
	if (hitrate > threshold) {
	    printf("  <===\n");
	} else {
	    printf("\n");
	}
    }	
    printf("Total letters checked: %d\n", length);
    return(0);
}
Beispiel #13
0
// lmfile (landmarks file) - This is a text file with format:
// i1 j1 k1
// i2 j2 k2
// i3 j3 k3
// where 
// (i1, j1, k1) are the coordinates of the AC
// (i2, j2, k2) are the coordinates of the PC, and
// (i3, j3, k3) are the coordinates of the RP.
// subfile (subject file) - 3D T1W volume of type short in NIFTI format
// TPIL - output 4x4 rigid-body transformation matrix that would transform
// subfile into a standardized PIL orientation
void new_PIL_transform(const char *subfile,const char *lmfile,char *orient,float *TPIL, int SAVE_MRX_FLAG)
{
   float Qavg[3]; // average of rows of Q
   float Pavg[3]; // average of rows of P
   float TPIL0[16]; // transforms the original image to MSP/AC-PC aligned PIL orientation
   int n; // number of landmarks
   float *LM;  // (3 x n) matrix of detected landmarks
   float *invT;
   char filename[1024];
   SHORTIM subimPIL; 
   nifti_1_header PILbraincloud_hdr;
   // subfile without the directory structure and extension
   char subfile_prefix[1024]; 
   char imagedir[1024]; 
   char modelfile[1024]="";

   if( niftiFilename(subfile_prefix, subfile)==0 ) exit(0);
   getDirectoryName(subfile, imagedir);

   getARTHOME();

   // initial TPIL0 using old PIL transformation
   char opt_CENTER_AC_old;   
   opt_CENTER_AC_old = opt_CENTER_AC; // record opt_CENTER_AC
   opt_CENTER_AC = NO; // temporarily set opt_CENTER_AC to NO
   standard_PIL_transformation(subfile, lmfile, orient, 0, TPIL0);
   opt_CENTER_AC = opt_CENTER_AC_old; // restore opt_CENTER_AC

   /////////////////////////////////////////////////////////
   // Read input volume from subfile
   SHORTIM subim; 
   nifti_1_header subim_hdr;  

   subim.v = (short *)read_nifti_image(subfile, &subim_hdr);

   if(subim.v==NULL)
   {
      printf("Error reading %s, aborting ...\n", subfile);
      exit(1);
   }

   set_dim(subim, subim_hdr);
   /////////////////////////////////////////////////////////

   /////////////////////////////////////////////////////////
   // Reslice subim to PIL space 
   sprintf(filename,"%s/PILbrain.nii",ARTHOME);
   PILbraincloud_hdr=read_NIFTI_hdr(filename);

   set_dim(subimPIL, PILbraincloud_hdr);
   invT = inv4(TPIL0);
   resliceImage(subim, subimPIL, invT, LIN);
   free(invT);

   // Detect 8 landmarks on the MSP on the subimPIL volume
   sprintf(modelfile,"%s/orion.mdl",ARTHOME);

   LM=detect_landmarks(subimPIL, modelfile, n);

   convert_to_xyz(LM, n, subimPIL);
 
   // This block outputs the locations of the detected landmarks 
   // in (i,j,k) coordinates of the native space of the input volume
   if(opt_txt)
   {
      FILE *fp;
      float landmark[4];
      invT = inv4(TPIL0);
      sprintf(filename,"%s/%s_orion.txt",imagedir,subfile_prefix);
      fp=fopen(filename,"w");
      if(fp==NULL) file_open_error(filename);
      for(int i=0; i<n; i++)
      {
         landmark[0]=LM[0*n + i];
         landmark[1]=LM[1*n + i];
         landmark[2]=LM[2*n + i];
         landmark[3]=1;
         multi(invT,4,4,landmark,4,1,landmark);
         convert_to_ijk(landmark, 1, subim);
         fprintf(fp,"%5.1f %5.1f %5.1f\n",landmark[0], landmark[1], landmark[2]);
      }
      fclose(fp);
      free(invT);
   }

   float *P;
   float *Q; // Image using Q insted of P' in Eq. (1) of Arun et al. 1987
   float TLM[16];

   Q =  (float *)calloc(3*n,sizeof(float));
   P =  (float *)calloc(3*n,sizeof(float));
   for(int i=0; i<3*n; i++) P[i]=LM[i];

   delete subimPIL.v;
   /////////////////////////////////////////////////////////

   // read Q
   {
      FILE *fp;
      int r, r2;
      int cm[3];

      fp=fopen(modelfile, "r");
      if(fp==NULL) file_open_error(modelfile);

      fread(&n, sizeof(int), 1, fp);
      fread(&r, sizeof(int), 1, fp);
      fread(&r2, sizeof(int), 1, fp);
      SPH refsph(r);

      for(int i=0; i<n; i++)
      {
         fread(cm, sizeof(int), 3, fp);
         fread(refsph.v, sizeof(float), refsph.n, fp);
         Q[0*n + i]=cm[0];
         Q[1*n + i]=cm[1];
         Q[2*n + i]=cm[2];
      }

      fclose(fp);

      convert_to_xyz(Q, n, subimPIL);
   }

   Procrustes(Q, Qavg, n, P, Pavg, TLM);

   multi(TLM,4,4,TPIL0,4,4,TPIL);

   // create the *LM.ppm image
   if(opt_ppm || opt_png)
   {
      int *lmx, *lmy;
      float lm[4]; 

      invT = inv4(TPIL);
      resliceImage(subim, subimPIL, invT, LIN);
      free(invT);

      lmx = (int *)calloc(n,sizeof(int));
      lmy = (int *)calloc(n,sizeof(int));

      for(int i=0; i<n; i++)
      {
         lm[0] = P[i] + Pavg[0];
         lm[1] = P[n+i] + Pavg[1];
         lm[2] = P[2*n+i] + Pavg[2];
         lm[3] = 1.0;

         multi(TLM,4,4,lm,4,1,lm);

         convert_to_ijk(lm, 1, subimPIL);

         lmx[i]=(int)( lm[0] + 0.5 );
         lmy[i]=(int)( lm[1] + 0.5 );
      }

      sprintf(filename,"%s/%s_orion.ppm",imagedir, subfile_prefix);
      mspPPM(subimPIL, lmx, lmy, n, filename);

      delete lmx;
      delete lmy;
      delete subimPIL.v;
   }

  if(opt_CENTER_AC)
  {
    float ac[4]; 
    float Ttmp[16];

    ac[0] = P[1] + Pavg[0];  // landmark #1 is AC
    ac[1] = P[n+1] + Pavg[1];
    ac[2] = P[2*n+1] + Pavg[2];
    ac[3] = 1.0;

    multi(TLM,4,4,ac,4,1,ac);

    Ttmp[0]=1.0;  Ttmp[1]=0.0;  Ttmp[2]=0.0;  Ttmp[3]=-ac[0];  // makes ac the center of the FOV
    Ttmp[4]=0.0;  Ttmp[5]=1.0;  Ttmp[6]=0.0;  Ttmp[7]=-ac[1];  // ac[1] should be equal to pc[1]
    Ttmp[8]=0.0;  Ttmp[9]=0.0;  Ttmp[10]=1.0; Ttmp[11]=0;
    Ttmp[12]=0.0; Ttmp[13]=0.0; Ttmp[14]=0.0; Ttmp[15]=1.0;

    multi(Ttmp,4,4,TPIL,4,4,TPIL);
  }

  // save the PIL transformation in <subfile_prefix>_PIL.mrx
  if(SAVE_MRX_FLAG == 1)
  {
    FILE *fp;
    sprintf(filename,"%s/%s_PIL.mrx",imagedir, subfile_prefix);
    fp=fopen(filename,"w");
    if(fp==NULL) file_open_error(filename);
    printMatrix(TPIL,4,4,"",fp);
    fclose(fp);
  }

   {
      float ssd1=0.0;
      float ssd3=0.0;
      float x[4], y[4];
      
      x[3]=y[3]=1.0;

      for(int i=0; i<n; i++)
      {
         x[0] = Q[i] + Qavg[0];
         x[1] = Q[i+n] + Qavg[1];
         x[2] = Q[i+2*n] + Qavg[2];

         y[0] = P[i]+Pavg[0];
         y[1] = P[i+n]+Pavg[1];
         y[2] = P[i+2*n]+Pavg[2];

         ssd1 += (x[0]-y[0])*(x[0]-y[0]); 
         ssd1 += (x[1]-y[1])*(x[1]-y[1]); 
         ssd1 += (x[2]-y[2])*(x[2]-y[2]); 

         multi(TLM,4,4,y,4,1,y);
         ssd3 += (x[0]-y[0])*(x[0]-y[0]); 
         ssd3 += (x[1]-y[1])*(x[1]-y[1]); 
         ssd3 += (x[2]-y[2])*(x[2]-y[2]); 
      }
      //if(opt_v) printf("SSD (MSP + AC/PC transformation) = %f\n",ssd1);
      //if(opt_v) printf("SSD (MSP + AC/PC + LM transformation) = %f\n",ssd3);
   }

   delete subim.v;
   free(P);
   free(Q);
}
Beispiel #14
0
// This function makes a ppm file from the MSP and displays 'nl' detected landmarks.
//im: the input image 
//lmx and lmy: are pointers to nl size arrays including the (x,y) for the nl landmarks
//ppmfile: the output file name
void mspPPM(SHORTIM im, int *ii, int *jj, int nl, const char *ppmfile)
{
   int colourflag;
//   unsigned char colour[3]={0xFF,0xFF,0x00};
//   unsigned char colour[3]={0x00,0xFF,0x00};
   unsigned char colour[3]={0xFF,0xFF,0xFF};
   unsigned char *imgTemp;
   FILE *fp;

   int kk=im.nz/2;
   
   /*size of the mark*/
   int d=4; 

   float temp;

   imgTemp=(unsigned char *)calloc(im.nv,sizeof(unsigned char));

   int low, high;
   setLowHigh(im.v+kk*im.np, im.nx*im.ny, &low, &high, 1.0);

   for(int i=0;i<im.nv;i++)
   {
      if( im.v[i] >= high ) temp=255.0;
      else temp=im.v[i]*255.0/high;

      imgTemp[i]=(unsigned char)temp;
   }

   fp=fopen(ppmfile,"w");
   if(fp==NULL) file_open_error(ppmfile);

   /*Write the header part of the PPM file*/
   fprintf(fp,"P6\n");
   fprintf(fp,"%d %d\n", im.nx, im.ny);
   fprintf(fp,"255\n");


   for (int j=0;j<im.ny;j++)
   {
      for(int i=0;i<im.nx;i++)
      {
         for(int m=0;m<nl;m++)
         {
//uncomment to save in different colours
//            if(m==3) { colour[0]=255; colour[1]=0; colour[2]=0; } 
//            else if(m==1) { colour[0]=0; colour[1]=255; colour[2]=0; } 
//            else if(m==2) { colour[0]=0; colour[1]=0; colour[2]=255; } 
//            else if(m==0) { colour[0]=0; colour[1]=255; colour[2]=191; } 
//            else if(m==4) { colour[0]=0; colour[1]=128; colour[2]=255; } 
//            else if(m==5) { colour[0]=255; colour[1]=0; colour[2]=255; } 
//            else if(m==6) { colour[0]=255; colour[1]=128; colour[2]=0; } 
//            else if(m==7) { colour[0]=255; colour[1]=255; colour[2]=0; } 
            
            if( (i==ii[m] && jj[m]-d<j && j<jj[m]+d) || (j==jj[m] && ii[m]-d<i && i<ii[m]+d) )
            {
               fwrite(colour,1,3,fp);
               colourflag=1;
            }
         }
         if(colourflag==0) 
         {
            fwrite(imgTemp+im.np*kk+im.nx*j+i,1,1,fp);
            fwrite(imgTemp+im.np*kk+im.nx*j+i,1,1,fp);
            fwrite(imgTemp+im.np*kk+im.nx*j+i,1,1,fp);
         }
         colourflag=0;
      }
   }

   fclose(fp);

  if(opt_png)
  {
    char *pngfilename;
    char *cmnd;
    int L;

    L = strlen(ppmfile);
    pngfilename = (char *)calloc(L,sizeof(char));
    cmnd = (char *)calloc(2*L+128,sizeof(char));  // 128 is plenty :)
    stpcpy(pngfilename, ppmfile);
    pngfilename[L-1]='g';
    pngfilename[L-2]='n';
    pngfilename[L-3]='p';

    sprintf(cmnd,"pnmtopng %s > %s",ppmfile,pngfilename); 
    if(opt_png) system(cmnd);

    free(pngfilename);
    free(cmnd);
  }

  if(opt_ppm==NO) remove(ppmfile);

  delete imgTemp;
}