automatic commit
[folded-ctf.git] / labelled_image_pool_file.cc
1
2 ///////////////////////////////////////////////////////////////////////////
3 // This program is free software: you can redistribute it and/or modify  //
4 // it under the terms of the version 3 of the GNU General Public License //
5 // as published by the Free Software Foundation.                         //
6 //                                                                       //
7 // This program is distributed in the hope that it will be useful, but   //
8 // WITHOUT ANY WARRANTY; without even the implied warranty of            //
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      //
10 // General Public License for more details.                              //
11 //                                                                       //
12 // You should have received a copy of the GNU General Public License     //
13 // along with this program. If not, see <http://www.gnu.org/licenses/>.  //
14 //                                                                       //
15 // Written by Francois Fleuret, (C) IDIAP                                //
16 // Contact <francois.fleuret@idiap.ch> for comments & bug reports        //
17 ///////////////////////////////////////////////////////////////////////////
18
19 #include "labelled_image_pool_file.h"
20
21 LabelledImagePoolFile::LabelledImagePoolFile(char *file_name) {
22   _stream = new ifstream(file_name);
23
24   if(_stream->fail()) {
25     cerr << "Can not open image pool " << file_name << " for reading." << endl;
26     exit(1);
27   }
28
29   cout << "Opening image pool " << file_name << " ... ";
30   cout.flush();
31
32   LabelledImage dummy;
33
34   int nb_image_max = 1024;
35   int nb_targets = 0;
36
37   _nb_images = 0;
38   streampos *tmp_positions = new streampos[nb_image_max];
39
40   // This looks slightly ugly to me
41
42   _stream->seekg(0, ios::end);
43   streampos end = _stream->tellg();
44   _stream->seekg(0, ios::beg);
45
46   while(_stream->tellg() < end) {
47     grow(&nb_image_max, _nb_images, &tmp_positions, 2);
48     tmp_positions[_nb_images] = _stream->tellg();
49     dummy.read(_stream);
50     nb_targets += dummy.nb_targets();
51     _nb_images++;
52   }
53
54   _images = new LabelledImage *[_nb_images];
55   _image_stream_positions = new streampos[_nb_images];
56   _image_nb_refs = new int[_nb_images];
57
58   for(int i = 0; i < _nb_images; i++) {
59     _images[i] = 0;
60     _image_stream_positions[i] = tmp_positions[i];
61     _image_nb_refs[i] = 0;
62   }
63
64   delete[] tmp_positions;
65
66   cout << "done." << endl;
67   cout << "It contains " << _nb_images << " images and " << nb_targets << " targets." << endl;
68 }
69
70 LabelledImagePoolFile::~LabelledImagePoolFile() {
71 #ifdef DEBUG
72   for(int i = 0; i < _nb_images; i++) if(_image_nb_refs[i] > 0) {
73     cerr << "Destroying a pool while images are grabbed." << endl;
74     abort();
75   }
76 #endif
77   delete[] _images;
78   delete[] _image_stream_positions;
79   delete[] _image_nb_refs;
80   delete _stream;
81 }
82
83 int LabelledImagePoolFile::nb_images() {
84   return _nb_images;
85 }
86
87 LabelledImage *LabelledImagePoolFile::grab_image(int n_image) {
88   if(_image_nb_refs[n_image] == 0) {
89     _stream->seekg(_image_stream_positions[n_image]);
90     _images[n_image] = new LabelledImage();
91     _images[n_image]->read(_stream);
92   }
93   _image_nb_refs[n_image]++;
94   return _images[n_image];
95 }
96
97 void LabelledImagePoolFile::release_image(int n_image) {
98   ASSERT(_image_nb_refs[n_image] > 0);
99   _image_nb_refs[n_image]--;
100   if(_image_nb_refs[n_image] <= 0) delete _images[n_image];
101 }