2 * folded-ctf is an implementation of the folded hierarchy of
3 * classifiers for object detection, developed by Francois Fleuret
6 * Copyright (c) 2008 Idiap Research Institute, http://www.idiap.ch/
7 * Written by Francois Fleuret <francois.fleuret@idiap.ch>
9 * This file is part of folded-ctf.
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.
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.
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/>.
26 #include "labelled_image_pool_file.h"
28 LabelledImagePoolFile::LabelledImagePoolFile(char *file_name) {
29 _stream = new ifstream(file_name);
32 cerr << "Can not open image pool " << file_name << " for reading." << endl;
36 cout << "Opening image pool " << file_name << " ... ";
41 int nb_image_max = 1024;
45 streampos *tmp_positions = new streampos[nb_image_max];
47 // This looks slightly ugly to me
49 _stream->seekg(0, ios::end);
50 streampos end = _stream->tellg();
51 _stream->seekg(0, ios::beg);
53 while(_stream->tellg() < end) {
54 grow(&nb_image_max, _nb_images, &tmp_positions, 2);
55 tmp_positions[_nb_images] = _stream->tellg();
57 nb_targets += dummy.nb_targets();
61 _images = new LabelledImage *[_nb_images];
62 _image_stream_positions = new streampos[_nb_images];
63 _image_nb_refs = new int[_nb_images];
65 for(int i = 0; i < _nb_images; i++) {
67 _image_stream_positions[i] = tmp_positions[i];
68 _image_nb_refs[i] = 0;
71 delete[] tmp_positions;
73 cout << "done." << endl;
74 cout << "It contains " << _nb_images << " images and " << nb_targets << " targets." << endl;
77 LabelledImagePoolFile::~LabelledImagePoolFile() {
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;
85 delete[] _image_stream_positions;
86 delete[] _image_nb_refs;
90 int LabelledImagePoolFile::nb_images() {
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);
100 _image_nb_refs[n_image]++;
101 return _images[n_image];
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];