volmetrics

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