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