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. //
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. //
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/>. //
15 // Written by Francois Fleuret, (C) IDIAP //
16 // Contact <francois.fleuret@idiap.ch> for comments & bug reports //
17 ///////////////////////////////////////////////////////////////////////////
19 #include "labelled_image_pool_file.h"
21 LabelledImagePoolFile::LabelledImagePoolFile(char *file_name) {
22 _stream = new ifstream(file_name);
25 cerr << "Can not open image pool " << file_name << " for reading." << endl;
29 cout << "Opening image pool " << file_name << " ... ";
34 int nb_image_max = 1024;
38 streampos *tmp_positions = new streampos[nb_image_max];
40 // This looks slightly ugly to me
42 _stream->seekg(0, ios::end);
43 streampos end = _stream->tellg();
44 _stream->seekg(0, ios::beg);
46 while(_stream->tellg() < end) {
47 grow(&nb_image_max, _nb_images, &tmp_positions, 2);
48 tmp_positions[_nb_images] = _stream->tellg();
50 nb_targets += dummy.nb_targets();
54 _images = new LabelledImage *[_nb_images];
55 _image_stream_positions = new streampos[_nb_images];
56 _image_nb_refs = new int[_nb_images];
58 for(int i = 0; i < _nb_images; i++) {
60 _image_stream_positions[i] = tmp_positions[i];
61 _image_nb_refs[i] = 0;
64 delete[] tmp_positions;
66 cout << "done." << endl;
67 cout << "It contains " << _nb_images << " images and " << nb_targets << " targets." << endl;
70 LabelledImagePoolFile::~LabelledImagePoolFile() {
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;
78 delete[] _image_stream_positions;
79 delete[] _image_nb_refs;
83 int LabelledImagePoolFile::nb_images() {
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);
93 _image_nb_refs[n_image]++;
94 return _images[n_image];
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];