volmetrics

diff src/volume.cc @ 5:92c163c939be

Volume::load
author Eleni Maria Stea <elene.mst@gmail.com>
date Fri, 17 Jan 2014 23:45:56 +0200
parents 1fbbe10c8e08
children e6485ef45e6e
line diff
     1.1 --- a/src/volume.cc	Sat Jan 11 23:49:58 2014 +0200
     1.2 +++ b/src/volume.cc	Fri Jan 17 23:45:56 2014 +0200
     1.3 @@ -1,14 +1,70 @@
     1.4 +#include <ctype.h>
     1.5 +#include <errno.h>
     1.6  #include <stdio.h>
     1.7 +#include <string.h>
     1.8 +
     1.9 +#include <string>
    1.10 +#include <vector>
    1.11  
    1.12  #include "volume.h"
    1.13  
    1.14 +static char *strip_whitespaces(char *buf);
    1.15 +
    1.16  Volume::Volume()
    1.17  {
    1.18  	width = height = 0;
    1.19 +	zaspect = 1;
    1.20  }
    1.21  
    1.22  bool Volume::load_volume(const char *fname)
    1.23  {
    1.24 +	FILE *fp = fopen(fname, "r");
    1.25 +	if(!fp) {
    1.26 +		fprintf(stderr, "Failed to open file: %s: %s\n", fname, strerror(errno));
    1.27 +		return false;
    1.28 +	}
    1.29 +
    1.30 +	char buf[512];
    1.31 +	if(!fgets(buf, sizeof buf, fp)) {
    1.32 +		fprintf(stderr, "Empty file: %s.\n", fname);
    1.33 +		return false;
    1.34 +	}
    1.35 +	if(strstr(buf, "VOLUME") != buf) {
    1.36 +		fprintf(stderr, "Invalid volume file format: %s\n", fname);
    1.37 +		return false;
    1.38 +	}
    1.39 +
    1.40 +	bool reading_slices = false;
    1.41 +	std::vector<std::string> img_fnames;
    1.42 +
    1.43 +	while(fgets(buf, sizeof buf, fp)) {
    1.44 +		char *line = strip_whitespaces(buf);
    1.45 +		if(!line || *line == 0 || *line == '#')
    1.46 +			continue;
    1.47 +
    1.48 +		if(reading_slices == false) {
    1.49 +			if(strcmp(line, "SLICES") == 0)
    1.50 +				reading_slices = true;
    1.51 +
    1.52 +			sscanf(line, "zaspect = %f", &zaspect);
    1.53 +		}
    1.54 +		else {
    1.55 +			img_fnames.push_back(line);
    1.56 +		}
    1.57 +	}
    1.58 +
    1.59 +	fclose(fp);
    1.60 +
    1.61 +	for(size_t i=0; i<img_fnames.size(); i++) {
    1.62 +		Image img;
    1.63 +		if(img.load(img_fnames[i].c_str())) {
    1.64 +			push_slice(std::move(img));
    1.65 +		}
    1.66 +		else {
    1.67 +			fprintf(stderr, "Failed to load slice: %s\n", img_fnames[i].c_str());
    1.68 +		}
    1.69 +	}
    1.70 +
    1.71  	return true;
    1.72  }
    1.73  
    1.74 @@ -28,3 +84,22 @@
    1.75  	slices.push_back(slice);
    1.76  	return true;
    1.77  }
    1.78 +
    1.79 +void Volume::draw()
    1.80 +{
    1.81 +}
    1.82 +
    1.83 +static char *strip_whitespaces(char *buf)
    1.84 +{
    1.85 +	while(*buf && isspace(*buf))
    1.86 +		buf++;
    1.87 +
    1.88 +	if(!*buf)
    1.89 +		return 0;
    1.90 +
    1.91 +	char *end = buf + strlen(buf) - 1;
    1.92 +	while(end > buf && isspace(*end))
    1.93 +		end--;
    1.94 +	end[1] = 0;
    1.95 +	return buf;
    1.96 +}