volmetrics

annotate 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
rev   line source
elene@5 1 #include <ctype.h>
elene@5 2 #include <errno.h>
elene@4 3 #include <stdio.h>
elene@5 4 #include <string.h>
elene@5 5
elene@5 6 #include <string>
elene@5 7 #include <vector>
elene@4 8
elene@3 9 #include "volume.h"
elene@4 10
elene@5 11 static char *strip_whitespaces(char *buf);
elene@5 12
elene@4 13 Volume::Volume()
elene@4 14 {
elene@4 15 width = height = 0;
elene@5 16 zaspect = 1;
elene@4 17 }
elene@4 18
elene@4 19 bool Volume::load_volume(const char *fname)
elene@4 20 {
elene@5 21 FILE *fp = fopen(fname, "r");
elene@5 22 if(!fp) {
elene@5 23 fprintf(stderr, "Failed to open file: %s: %s\n", fname, strerror(errno));
elene@5 24 return false;
elene@5 25 }
elene@5 26
elene@5 27 char buf[512];
elene@5 28 if(!fgets(buf, sizeof buf, fp)) {
elene@5 29 fprintf(stderr, "Empty file: %s.\n", fname);
elene@5 30 return false;
elene@5 31 }
elene@5 32 if(strstr(buf, "VOLUME") != buf) {
elene@5 33 fprintf(stderr, "Invalid volume file format: %s\n", fname);
elene@5 34 return false;
elene@5 35 }
elene@5 36
elene@5 37 bool reading_slices = false;
elene@5 38 std::vector<std::string> img_fnames;
elene@5 39
elene@5 40 while(fgets(buf, sizeof buf, fp)) {
elene@5 41 char *line = strip_whitespaces(buf);
elene@5 42 if(!line || *line == 0 || *line == '#')
elene@5 43 continue;
elene@5 44
elene@5 45 if(reading_slices == false) {
elene@5 46 if(strcmp(line, "SLICES") == 0)
elene@5 47 reading_slices = true;
elene@5 48
elene@5 49 sscanf(line, "zaspect = %f", &zaspect);
elene@5 50 }
elene@5 51 else {
elene@5 52 img_fnames.push_back(line);
elene@5 53 }
elene@5 54 }
elene@5 55
elene@5 56 fclose(fp);
elene@5 57
elene@5 58 for(size_t i=0; i<img_fnames.size(); i++) {
elene@5 59 Image img;
elene@5 60 if(img.load(img_fnames[i].c_str())) {
elene@5 61 push_slice(std::move(img));
elene@5 62 }
elene@5 63 else {
elene@5 64 fprintf(stderr, "Failed to load slice: %s\n", img_fnames[i].c_str());
elene@5 65 }
elene@5 66 }
elene@5 67
elene@4 68 return true;
elene@4 69 }
elene@4 70
elene@4 71 bool Volume::push_slice(Image &&slice)
elene@4 72 {
elene@4 73 if(slices.empty()) {
elene@4 74 width = slice.get_width();
elene@4 75 height = slice.get_height();
elene@4 76 }
elene@4 77 else {
elene@4 78 if(width != slice.get_width() || height != slice.get_height()) {
elene@4 79 fprintf(stderr, "failed to load slice no: %d\n", (int)slices.size() + 1);
elene@4 80 return false;
elene@4 81 }
elene@4 82 }
elene@4 83
elene@4 84 slices.push_back(slice);
elene@4 85 return true;
elene@4 86 }
elene@5 87
elene@5 88 void Volume::draw()
elene@5 89 {
elene@5 90 }
elene@5 91
elene@5 92 static char *strip_whitespaces(char *buf)
elene@5 93 {
elene@5 94 while(*buf && isspace(*buf))
elene@5 95 buf++;
elene@5 96
elene@5 97 if(!*buf)
elene@5 98 return 0;
elene@5 99
elene@5 100 char *end = buf + strlen(buf) - 1;
elene@5 101 while(end > buf && isspace(*end))
elene@5 102 end--;
elene@5 103 end[1] = 0;
elene@5 104 return buf;
elene@5 105 }