2
2
Bear Engine - Editor library
4
Copyright (C) 2005-2009 Julien Jorge, Sebastien Angibaud
4
Copyright (C) 2005-2010 Julien Jorge, Sebastien Angibaud
6
6
This program is free software; you can redistribute it and/or modify it
7
7
under the terms of the GNU General Public License as published by the
29
29
#include "bf/image_pool.hpp"
31
31
#include "bf/path_configuration.hpp"
32
#include "bf/scan_dir.hpp"
32
33
#include "bf/wx_facilities.hpp"
34
35
#include <claw/assert.hpp>
35
36
#include <claw/logger.hpp>
40
/*----------------------------------------------------------------------------*/
43
* \param t The map in which we store the thumbnails.
44
* \param r The directory from which the search started.
46
bf::image_pool::load_thumb_func::load_thumb_func
47
( image_map& t, const std::string& r )
51
} // image_pool::load_thumb_func::load_thumb_func()
53
/*----------------------------------------------------------------------------*/
55
* \brief Load the thumbnail of an image.
56
* \param path The path to the image.
58
void bf::image_pool::load_thumb_func::operator()( const std::string& path )
60
wxBitmap img = load(path);
61
const wxString image_name( std_to_wx_string( path.substr(m_root.size()) ) );
63
thumb[image_name] = img;
64
} // image_pool::load_thumb_func::operator()()
66
/*----------------------------------------------------------------------------*/
68
* \brief Load the thumbnail of an image.
69
* \param path The path to the image.
71
wxBitmap bf::image_pool::load_thumb_func::load( const std::string& path )
73
wxImage img( std_to_wx_string(path) );
75
if ( (img.GetWidth() > s_thumb_size.x)
76
|| (img.GetHeight() > s_thumb_size.y) )
80
if( img.GetWidth() > img.GetHeight() )
83
h = (img.GetHeight() * w) / img.GetWidth();
88
w = (img.GetWidth() * h) / img.GetHeight();
95
} // image_pool::load_thumb_func::load()
37
100
/*----------------------------------------------------------------------------*/
38
101
const wxSize bf::image_pool::s_thumb_size( 150, 150 );
51
114
/*----------------------------------------------------------------------------*/
116
* \brief Remove all thumbnails and images.
118
void bf::image_pool::clear()
122
} // image_pool::clear()
124
/*----------------------------------------------------------------------------*/
53
126
* \brief Read all item files from a given directory and in its subdirectories.
54
127
* \param dir_path The path to the directory to scan.
56
129
void bf::image_pool::scan_directory( const std::string& dir_path )
58
boost::filesystem::path dir(dir_path, boost::filesystem::native);
60
if ( boost::filesystem::exists(dir) )
65
std::string path( dir_path );
66
if ( path[path.size() - 1] != '/' )
69
scan_sub_directory( path, dir );
131
std::vector<std::string> ext(4);
137
std::string root( dir_path );
139
if ( root[root.size() - 1] != '/' )
142
load_thumb_func f(m_thumbnail, root);
143
scan_dir<load_thumb_func> scan;
145
scan( root, f, ext.begin(), ext.end() );
71
146
} // image_pool::scan_directory()
73
148
/*----------------------------------------------------------------------------*/
109
184
bf::image_pool::const_iterator bf::image_pool::begin() const
111
return const_iterator( m_image.begin() );
186
return const_iterator( m_thumbnail.begin() );
112
187
} // image_pool::begin()
114
189
/*----------------------------------------------------------------------------*/
118
193
bf::image_pool::const_iterator bf::image_pool::end() const
120
return const_iterator( m_image.end() );
195
return const_iterator( m_thumbnail.end() );
121
196
} // image_pool::end()
123
198
/*----------------------------------------------------------------------------*/
129
204
void bf::image_pool::add_image
130
205
( const std::string& root, const std::string& file_path )
132
wxBitmap thumb = load_thumb( file_path );
207
wxBitmap thumb = load_thumb_func::load( file_path );
134
209
if ( thumb.IsOk() )
144
219
/*----------------------------------------------------------------------------*/
146
* \brief Load the thumbnail of an image.
147
* \param path The path to the image to load.
149
wxBitmap bf::image_pool::load_thumb( const std::string& path ) const
151
wxImage img( std_to_wx_string(path) );
153
if ( (img.GetWidth() > s_thumb_size.x)
154
|| (img.GetHeight() > s_thumb_size.y) )
158
if( img.GetWidth() > img.GetHeight() )
161
h = (img.GetHeight() * w) / img.GetWidth();
166
w = (img.GetWidth() * h) / img.GetHeight();
172
return wxBitmap(img);
173
} // image_pool::load_thumb()
175
/*----------------------------------------------------------------------------*/
177
221
* \brief Load and store the data of an image.
178
222
* \param name The name of the image to load.
184
228
if ( path_configuration::get_instance().expand_file_name(std_name, 1) )
186
230
if ( m_thumbnail.find(name) == m_thumbnail.end() )
187
m_thumbnail[name] = load_thumb(std_name);
231
m_thumbnail[name] = load_thumb_func::load(std_name);
189
233
m_image[name] = wxBitmap( wxImage(std_to_wx_string(std_name)) );
194
238
m_thumbnail[name] = wxBitmap();
196
240
} // image_pool::load_image_data()
198
/*----------------------------------------------------------------------------*/
200
* \brief Read all item files from a given directory and in its subdirectories.
201
* \param root The path to the root directory where the images are searched.
202
* \param dir The path to the directory to scan.
204
void bf::image_pool::scan_sub_directory
205
( const std::string& root, const boost::filesystem::path& dir )
207
boost::filesystem::directory_iterator it(dir);
208
boost::filesystem::directory_iterator eit;
210
for ( ; it!=eit; ++it)
211
if ( boost::filesystem::is_directory(*it) )
212
scan_sub_directory( root, *it );
213
else if (supported_extension( it->string() ))
214
add_image( root, it->string() );
215
} // image_pool::scan_sub_directory()
217
/*----------------------------------------------------------------------------*/
219
* \brief Tell if a path correspond to a supported extension.
220
* \param path The path to check.
222
bool bf::image_pool::supported_extension( const std::string& path ) const
224
static const char* ext[] = { ".png", ".jpg", ".tga", ".bmp", NULL };
227
for (const char** it=ext; !result && (*it != NULL); ++it)
229
const std::string cext(*it);
230
result = ( path.rfind(cext) == path.size() - cext.size() );
234
} // image_pool::supported_extension()