~ubuntu-branches/ubuntu/trusty/plee-the-bear/trusty-proposed

« back to all changes in this revision

Viewing changes to bear-factory/bear-editor/src/bf/code/image_pool.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Julien Jorge, Julien Jorge
  • Date: 2010-11-17 20:13:34 UTC
  • mfrom: (6.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20101117201334-o4dp7uq437to7oxb
Tags: 0.5.1-1
[ Julien Jorge ]
* New upstream release (Closes: #565062, #546514).
* Add armhf architecture (Closes: #604689).
* Remove patches integrated upstream: rpath-editors.diff, rpath-game.diff,
  editors-menu-section.diff.
* Bump the Standard-Version, no changes.
* Update my email address.
* Set build dependency of libclaw to 1.6.0.
* Add the missing ${misc:Depends} in debian/control.
* List gettext translations in bear-factory.install and plee-the-bear.install.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
    Bear Engine - Editor library
3
3
 
4
 
    Copyright (C) 2005-2009 Julien Jorge, Sebastien Angibaud
 
4
    Copyright (C) 2005-2010 Julien Jorge, Sebastien Angibaud
5
5
 
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"
30
30
 
31
31
#include "bf/path_configuration.hpp"
 
32
#include "bf/scan_dir.hpp"
32
33
#include "bf/wx_facilities.hpp"
33
34
 
34
35
#include <claw/assert.hpp>
35
36
#include <claw/logger.hpp>
36
37
 
 
38
#include <wx/image.h>
 
39
 
 
40
/*----------------------------------------------------------------------------*/
 
41
/**
 
42
 * \brief Constructor.
 
43
 * \param t The map in which we store the thumbnails.
 
44
 * \param r The directory from which the search started.
 
45
 */
 
46
bf::image_pool::load_thumb_func::load_thumb_func
 
47
( image_map& t, const std::string& r )
 
48
  : thumb(t), m_root(r)
 
49
{
 
50
 
 
51
} // image_pool::load_thumb_func::load_thumb_func()
 
52
 
 
53
/*----------------------------------------------------------------------------*/
 
54
/**
 
55
 * \brief Load the thumbnail of an image.
 
56
 * \param path The path to the image.
 
57
 */
 
58
void bf::image_pool::load_thumb_func::operator()( const std::string& path )
 
59
{
 
60
  wxBitmap img = load(path);
 
61
  const wxString image_name( std_to_wx_string( path.substr(m_root.size()) ) );
 
62
 
 
63
  thumb[image_name] = img;
 
64
} // image_pool::load_thumb_func::operator()()
 
65
 
 
66
/*----------------------------------------------------------------------------*/
 
67
/**
 
68
 * \brief Load the thumbnail of an image.
 
69
 * \param path The path to the image.
 
70
 */
 
71
wxBitmap bf::image_pool::load_thumb_func::load( const std::string& path )
 
72
{
 
73
  wxImage img( std_to_wx_string(path) );
 
74
 
 
75
  if ( (img.GetWidth() > s_thumb_size.x)
 
76
       || (img.GetHeight() > s_thumb_size.y) )
 
77
    {
 
78
      int w, h;
 
79
 
 
80
      if( img.GetWidth() > img.GetHeight() )
 
81
        {
 
82
          w = s_thumb_size.x;
 
83
          h = (img.GetHeight() * w) / img.GetWidth();
 
84
        }
 
85
      else
 
86
        {
 
87
          h = s_thumb_size.y;
 
88
          w = (img.GetWidth() * h) / img.GetHeight();
 
89
        }
 
90
 
 
91
      img.Rescale(w, h);
 
92
    }
 
93
 
 
94
  return wxBitmap(img);
 
95
} // image_pool::load_thumb_func::load()
 
96
 
 
97
 
 
98
 
 
99
 
37
100
/*----------------------------------------------------------------------------*/
38
101
const wxSize bf::image_pool::s_thumb_size( 150, 150 );
39
102
 
50
113
 
51
114
/*----------------------------------------------------------------------------*/
52
115
/**
 
116
 * \brief Remove all thumbnails and images.
 
117
 */
 
118
void bf::image_pool::clear()
 
119
{
 
120
  m_image.clear();
 
121
  m_thumbnail.clear();
 
122
} // image_pool::clear()
 
123
 
 
124
/*----------------------------------------------------------------------------*/
 
125
/**
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.
55
128
 */
56
129
void bf::image_pool::scan_directory( const std::string& dir_path )
57
130
{
58
 
  boost::filesystem::path dir(dir_path, boost::filesystem::native);
59
 
 
60
 
  if ( boost::filesystem::exists(dir) )
61
 
    {
62
 
      m_image.clear();
63
 
      m_thumbnail.clear();
64
 
 
65
 
      std::string path( dir_path );
66
 
      if ( path[path.size() - 1] != '/' )
67
 
        path += '/';
68
 
 
69
 
      scan_sub_directory( path, dir );
70
 
    }
 
131
  std::vector<std::string> ext(4);
 
132
  ext[0] = ".png";
 
133
  ext[1] = ".jpg";
 
134
  ext[2] = ".tga";
 
135
  ext[3] = ".bmp";
 
136
 
 
137
  std::string root( dir_path );
 
138
  if ( !root.empty() )
 
139
    if ( root[root.size() - 1] != '/' )
 
140
      root += '/';
 
141
 
 
142
  load_thumb_func f(m_thumbnail, root);
 
143
  scan_dir<load_thumb_func> scan;
 
144
 
 
145
  scan( root, f, ext.begin(), ext.end() );
71
146
} // image_pool::scan_directory()
72
147
 
73
148
/*----------------------------------------------------------------------------*/
108
183
 */
109
184
bf::image_pool::const_iterator bf::image_pool::begin() const
110
185
{
111
 
  return const_iterator( m_image.begin() );
 
186
  return const_iterator( m_thumbnail.begin() );
112
187
} // image_pool::begin()
113
188
 
114
189
/*----------------------------------------------------------------------------*/
117
192
 */
118
193
bf::image_pool::const_iterator bf::image_pool::end() const
119
194
{
120
 
  return const_iterator( m_image.end() );
 
195
  return const_iterator( m_thumbnail.end() );
121
196
} // image_pool::end()
122
197
 
123
198
/*----------------------------------------------------------------------------*/
129
204
void bf::image_pool::add_image
130
205
( const std::string& root, const std::string& file_path )
131
206
{
132
 
  wxBitmap thumb = load_thumb( file_path );
 
207
  wxBitmap thumb = load_thumb_func::load( file_path );
133
208
 
134
209
  if ( thumb.IsOk() )
135
210
    {
143
218
 
144
219
/*----------------------------------------------------------------------------*/
145
220
/**
146
 
 * \brief Load the thumbnail of an image.
147
 
 * \param path The path to the image to load.
148
 
 */
149
 
wxBitmap bf::image_pool::load_thumb( const std::string& path ) const
150
 
{
151
 
  wxImage img( std_to_wx_string(path) );
152
 
 
153
 
  if ( (img.GetWidth() > s_thumb_size.x)
154
 
       || (img.GetHeight() > s_thumb_size.y) )
155
 
    {
156
 
      int w, h;
157
 
 
158
 
      if( img.GetWidth() > img.GetHeight() )
159
 
        {
160
 
          w = s_thumb_size.x;
161
 
          h = (img.GetHeight() * w) / img.GetWidth();
162
 
        }
163
 
      else
164
 
        {
165
 
          h = s_thumb_size.y;
166
 
          w = (img.GetWidth() * h) / img.GetHeight();
167
 
        }
168
 
 
169
 
      img.Rescale(w, h);
170
 
    }
171
 
 
172
 
  return wxBitmap(img);
173
 
} // image_pool::load_thumb()
174
 
 
175
 
/*----------------------------------------------------------------------------*/
176
 
/**
177
221
 * \brief Load and store the data of an image.
178
222
 * \param name The name of the image to load.
179
223
 */
184
228
  if ( path_configuration::get_instance().expand_file_name(std_name, 1) )
185
229
    {
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);
188
232
 
189
233
      m_image[name] = wxBitmap( wxImage(std_to_wx_string(std_name)) );
190
234
    }
194
238
      m_thumbnail[name] = wxBitmap();
195
239
    }
196
240
} // image_pool::load_image_data()
197
 
 
198
 
/*----------------------------------------------------------------------------*/
199
 
/**
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.
203
 
 */
204
 
void bf::image_pool::scan_sub_directory
205
 
( const std::string& root, const boost::filesystem::path& dir )
206
 
{
207
 
  boost::filesystem::directory_iterator it(dir);
208
 
  boost::filesystem::directory_iterator eit;
209
 
 
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()
216
 
 
217
 
/*----------------------------------------------------------------------------*/
218
 
/**
219
 
 * \brief Tell if a path correspond to a supported extension.
220
 
 * \param path The path to check.
221
 
 */
222
 
bool bf::image_pool::supported_extension( const std::string& path ) const
223
 
{
224
 
  static const char* ext[] = { ".png", ".jpg", ".tga", ".bmp", NULL };
225
 
  bool result = false;
226
 
 
227
 
  for (const char** it=ext; !result && (*it != NULL); ++it)
228
 
    {
229
 
      const std::string cext(*it);
230
 
      result = ( path.rfind(cext) == path.size() - cext.size() );
231
 
    }
232
 
 
233
 
  return result;
234
 
} // image_pool::supported_extension()