int ModelBase::load_model(const char* file_name) {
    const char *dot = strrchr(file_name, '.');
	const char supported_ext[2][10] = {".raw", ".pvm"};
	if (!dot || (strcmp(dot, supported_ext[0]) != 0 && strcmp(dot, supported_ext[1]) != 0)) {
		Logger::log("File error: Unsupported file extension (.raw|.pvm allowed)\n");
		return 1;
	}
	unsigned char *loaded_data;
	unsigned int width, height, depth, size, components = 1;
	if (strcmp(dot, supported_ext[1]) == 0) {
		Logger::log("Reading PVM file...\n");
		float scale_x, scale_y, scale_z;
		unsigned char *description, *courtesy, *parameters, *comment;
		loaded_data = readPVMvolume(file_name, &width, &height, &depth, 
			&components, &scale_x, &scale_y, &scale_z, &description, &courtesy, &parameters, &comment);
		if (loaded_data == NULL) { 
			Logger::log("Error: File not found: %s\n", file_name);
			return 1;
		}
		Logger::log("PVM file volume metadata:\nDimensions: width = %d height = %d depth = %d components = %d\n",
			width, height, depth, components);
		if (scale_x!=1.0f || scale_y!=1.0f || scale_z!=1.0f)
			printf("Real dimensions: %g %g %g\n", scale_x, scale_y, scale_z);
		if (description!=NULL)
			printf("Object description:\n%s\n",description);
		if (courtesy!=NULL)
			printf("Courtesy information:\n%s\n",courtesy);
		if (parameters!=NULL)
			printf("Scan parameters:\n%s\n",parameters);
		if (comment!=NULL)
			printf("Additonal comments:\n%s\n",comment);
		printf("\n");
		size = width * height * depth;				
		//float max_scale = MAXIMUM(scale_x, MAXIMUM(scale_y, scale_z));	// scaling - if used indexing in model is: (pos.x*(1/bound.x)) 
		//volume.bound = (-1) * make_float3(scale_x / max_scale, scale_y / max_scale, scale_z / max_scale);
	}
	if (strcmp(dot, supported_ext[0]) == 0) {
		Logger::log("Reading RAW file...\n");
		loaded_data = readRAWfile(file_name, &size);
		if (loaded_data == NULL) { 
			Logger::log("Error: File not found: %s\n", file_name);
			return 1;
		}
		printf("Enter RAW file volume dimensions (width, height, depth): ");   
		scanf("%d %d %d",&width, &height, &depth);
		if (width * height * depth != size) {
			printf("Enter RAW file volume components (bytes per voxel): ");   
			scanf("%d", &components);
			if (width * height * depth * components != size) {
				Logger::log("Error: Incorrect RAW file volume parameters\n");
				free(loaded_data);
				return 1;
			}
		}
	}
	if (components > 2) {
		Logger::log("Error: Unsupported number of components (1|2 allowed)\n");
		free(loaded_data);
		return 1;
	}
	if (components == 2) {
		Logger::log("Quantizing 16 bit volume to 8 bit using a non-linear mapping...\n");
		loaded_data = quantize(loaded_data, width, height, depth);
	}
	if (volume.data != NULL)
		free(volume.data);
	volume.dims = make_ushort3(width, height, depth);
	volume.size = size;
	volume.data = loaded_data;
	strcpy(ModelBase::file_name, file_name);
	Logger::log("File loaded: %s, volume size: %.2f MB\n\n", file_name, volume.size / (float)(1024 * 1024));
	compute_histogram();

	return 0;
}
Beispiel #2
0
// read a possibly compressed PNM image
unsigned char *readPNMimage(const char *filename,unsigned int *width,unsigned int *height,unsigned int *components)
   {
   const int maxstr=100;

   char str[maxstr];

   unsigned char *data,*ptr1,*ptr2;
   size_t bytes;

   int pnmtype,maxval;
   unsigned char *image;

   if ((data=readDDSfile(filename,&bytes))==NULL)
      if ((data=readRAWfile(filename,&bytes))==NULL) return(NULL);

   if (bytes<4) return(NULL);

   memcpy(str,data,3);
   str[3]='\0';

   if (sscanf(str,"P%1d\n",&pnmtype)!=1) return(NULL);

   ptr1=data+3;
   while (*ptr1=='\n' || *ptr1=='#')
      {
      while (*ptr1=='\n')
         if (++ptr1>=data+bytes) ERRORMSG();
      while (*ptr1=='#')
         if (++ptr1>=data+bytes) ERRORMSG();
         else
            while (*ptr1!='\n')
               if (++ptr1>=data+bytes) ERRORMSG();
      }

   ptr2=ptr1;
   while (*ptr2!='\n' && *ptr2!=' ')
      if (++ptr2>=data+bytes) ERRORMSG();
   if (++ptr2>=data+bytes) ERRORMSG();
   while (*ptr2!='\n' && *ptr2!=' ')
      if (++ptr2>=data+bytes) ERRORMSG();
   if (++ptr2>=data+bytes) ERRORMSG();
   while (*ptr2!='\n' && *ptr2!=' ')
      if (++ptr2>=data+bytes) ERRORMSG();
   if (++ptr2>=data+bytes) ERRORMSG();

   if (ptr2-ptr1>=maxstr) ERRORMSG();
   memcpy(str,ptr1,ptr2-ptr1);
   str[ptr2-ptr1]='\0';

   if (sscanf(str,"%u %u\n%d\n",width,height,&maxval)!=3) ERRORMSG();

   if (*width<1 || *height<1) ERRORMSG();

   if (pnmtype==5 && maxval==255) *components=1;
   else if (pnmtype==5 && (maxval==32767 || maxval==65535)) *components=2;
   else if (pnmtype==6 && maxval==255) *components=3;
   else ERRORMSG();

   if ((image=(unsigned char *)malloc((*width)*(*height)*(*components)))==NULL) ERRORMSG();
   if (data+bytes!=ptr2+(*width)*(*height)*(*components)) ERRORMSG();

   if (DDS_ISINTEL)
      if (*components==2) swapshort(ptr2,(*width)*(*height));

   memcpy(image,ptr2,(*width)*(*height)*(*components));
   free(data);

   return(image);
   }