The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
cutter.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2004 - 2016 by Philippe Plantier <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Standalone-Utility for images / tiles
18  */
19 
20 #include "game_config.hpp"
21 #include "exploder_composer.hpp"
22 
23 #include <SDL_image.h>
24 
25 #include <iostream>
26 
27 namespace {
28 
30  {
31  std::cerr << "usage: " << name << " [source] [dest_directory]\n";
32  }
33 }
34 
35 int main(int argc, char* argv[])
36 {
38  std::string dest_dir;
39  cutter cut;
40 
41  // Parse arguments that shouldn't require a display device
42  int arg;
43  for(arg = 1; arg != argc; ++arg) {
44  const std::string val(argv[arg]);
45  if(val.empty()) {
46  continue;
47  }
48 
49  if(val == "--help" || val == "-h") {
50  print_usage(argv[0]);
51  return 0;
52  } else if(val == "--verbose" || val == "-v") {
53  cut.set_verbose(true);
54  } else if(val == "--directory" || val == "-d" ) {
55  game_config::path = argv[++arg];
56  } else {
57  if(src.empty()) {
58  src = val;
59  } else if(dest_dir.empty()) {
60  dest_dir = val;
61  } else {
62  print_usage(argv[0]);
63  return 1;
64  }
65  }
66  }
67 
68  if(src.empty() || dest_dir.empty()) {
69  print_usage(argv[0]);
70  return 1;
71  }
72 
73  try {
74  const config conf = cut.load_config(src);
75  cut.load_masks(conf);
76 
77  const surface src_surface(make_neutral_surface(IMG_Load(src.c_str())));
78  if(src_surface == nullptr)
79  throw exploder_failure("Unable to load the source image " + src);
80 
81  const cutter::surface_map surfaces = cut.cut_surface(src_surface, conf);
82 
83  for(cutter::surface_map::const_iterator itor = surfaces.begin();
84  itor != surfaces.end(); ++itor) {
85  const cutter::mask &mask = itor->second.mask;
86 
88  itor->second.image
89  , mask.cut.w
90  , mask.cut.h);
91 
92  masked_overwrite_surface(surf, itor->second.image, mask.image,
93  mask.cut.x - mask.shift.x, mask.cut.y - mask.shift.y);
94 
95  save_image(surf, dest_dir + "/" + mask.name + ".png");
96  }
97 
98  } catch(exploder_failure& err) {
99  std::cerr << "Failed: " << err.message << "\n";
100  return 1;
101  }
102 
103  return 0;
104 }
105 
106 
void set_verbose(bool value)
static void print_usage(const char *badoption)
Definition: lua.cpp:107
surface_map cut_surface(surface surf, const config &conf)
std::string name
std::string message
GLuint const GLfloat * val
Definition: glew.h:2614
std::multimap< std::string, positioned_surface > surface_map
GLenum src
Definition: glew.h:2392
const config load_config(const std::string &filename)
bool save_image(const locator &i_locator, const std::string &filename)
Definition: image.cpp:1249
exploder_rect cut
void masked_overwrite_surface(surface dest, surface src, surface mask, int x, int y)
std::string path
GLenum GLint GLuint mask
Definition: glew.h:1813
int main(int argc, char *argv[])
Definition: cutter.cpp:35
surf
Definition: filter.cpp:143
std::map< std::string, tfilter >::iterator itor
Definition: filter.cpp:199
logger & err()
Definition: log.cpp:79
const GLvdpauSurfaceNV * surfaces
Definition: glew.h:11139
GLuint const GLchar * name
Definition: glew.h:1782
surface make_neutral_surface(const surface &surf)
Definition: utils.cpp:135
surface create_compatible_surface(const surface &surf, int width, int height)
Definition: utils.cpp:2166
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
exploder_point shift
GLsizei const GLcharARB ** string
Definition: glew.h:4503
void load_masks(const config &conf)