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 //
16 // (C) Idiap Research Institute //
18 // Contact <francois.fleuret@idiap.ch> for comments & bug reports //
19 ///////////////////////////////////////////////////////////////////////////
21 #include "labelled_image_pool_file.h"
23 LabelledImagePoolFile::LabelledImagePoolFile(char *file_name) {
24 _stream = new ifstream(file_name);
27 cerr << "Can not open image pool " << file_name << " for reading." << endl;
31 cout << "Opening image pool " << file_name << " ... ";
36 int nb_image_max = 1024;
40 streampos *tmp_positions = new streampos[nb_image_max];
42 // This looks slightly ugly to me
44 _stream->seekg(0, ios::end);
45 streampos end = _stream->tellg();
46 _stream->seekg(0, ios::beg);
48 while(_stream->tellg() < end) {
49 grow(&nb_image_max, _nb_images, &tmp_positions, 2);
50 tmp_positions[_nb_images] = _stream->tellg();
52 nb_targets += dummy.nb_targets();
56 _images = new LabelledImage *[_nb_images];
57 _image_stream_positions = new streampos[_nb_images];
58 _image_nb_refs = new int[_nb_images];
60 for(int i = 0; i < _nb_images; i++) {
62 _image_stream_positions[i] = tmp_positions[i];
63 _image_nb_refs[i] = 0;
66 delete[] tmp_positions;
68 cout << "done." << endl;
69 cout << "It contains " << _nb_images << " images and " << nb_targets << " targets." << endl;
72 LabelledImagePoolFile::~LabelledImagePoolFile() {
74 for(int i = 0; i < _nb_images; i++) if(_image_nb_refs[i] > 0) {
75 cerr << "Destroying a pool while images are grabbed." << endl;
80 delete[] _image_stream_positions;
81 delete[] _image_nb_refs;
85 int LabelledImagePoolFile::nb_images() {
89 LabelledImage *LabelledImagePoolFile::grab_image(int n_image) {
90 if(_image_nb_refs[n_image] == 0) {
91 _stream->seekg(_image_stream_positions[n_image]);
92 _images[n_image] = new LabelledImage();
93 _images[n_image]->read(_stream);
95 _image_nb_refs[n_image]++;
96 return _images[n_image];
99 void LabelledImagePoolFile::release_image(int n_image) {
100 ASSERT(_image_nb_refs[n_image] > 0);
101 _image_nb_refs[n_image]--;
102 if(_image_nb_refs[n_image] <= 0) delete _images[n_image];