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