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 version 3 as
13 * published by the Free Software Foundation.
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.
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/>.
25 #include "labelled_image_pool_file.h"
27 LabelledImagePoolFile::LabelledImagePoolFile(char *file_name) {
28 _stream = new ifstream(file_name);
31 cerr << "Can not open image pool " << file_name << " for reading." << endl;
35 cout << "Opening image pool " << file_name << " ... ";
40 int nb_image_max = 1024;
44 streampos *tmp_positions = new streampos[nb_image_max];
46 // This looks slightly ugly to me
48 _stream->seekg(0, ios::end);
49 streampos end = _stream->tellg();
50 _stream->seekg(0, ios::beg);
52 while(_stream->tellg() < end) {
53 grow(&nb_image_max, _nb_images, &tmp_positions, 2);
54 tmp_positions[_nb_images] = _stream->tellg();
56 nb_targets += dummy.nb_targets();
60 _images = new LabelledImage *[_nb_images];
61 _image_stream_positions = new streampos[_nb_images];
62 _image_nb_refs = new int[_nb_images];
64 for(int i = 0; i < _nb_images; i++) {
66 _image_stream_positions[i] = tmp_positions[i];
67 _image_nb_refs[i] = 0;
70 delete[] tmp_positions;
72 cout << "done." << endl;
73 cout << "It contains " << _nb_images << " images and " << nb_targets << " targets." << endl;
76 LabelledImagePoolFile::~LabelledImagePoolFile() {
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;
84 delete[] _image_stream_positions;
85 delete[] _image_nb_refs;
89 int LabelledImagePoolFile::nb_images() {
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);
99 _image_nb_refs[n_image]++;
100 return _images[n_image];
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];