The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
wesmage.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2016 by Mark de Wever <[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  * Tool to test the image conversion functions.
18  */
19 
20 #include "tools/exploder_utils.hpp"
21 #include "wesmage/exit.hpp"
22 #include "wesmage/filter.hpp"
23 #include "wesmage/options.hpp"
24 
25 #include <SDL_image.h>
26 
27 #include <ctime>
28 #include <iostream>
29 
30 static clock_t
32 {
33  clock_t begin = std::clock();
34  clock_t end = std::clock();
35 
36  while(begin == (end = std::clock())) {
37  /* DO NOTHING */
38  }
39  std::cout << "Clock resolution "
40  << end - begin
41  << " ticks, using " << CLOCKS_PER_SEC << " ticks/second.\n"
42  << "This give resolution of about "
43  <<static_cast<double>(end - begin) / CLOCKS_PER_SEC
44  << " seconds.\n";
45 
46  /* IO might be slow so wait until the next value. */
47  begin = std::clock();
48  while(begin == (end = std::clock())) {
49  /* DO NOTHING */
50  }
51 
52  return begin;
53 }
54 
55 int
56 main(int argc, char* argv[])
57 {
58  try {
59  const toptions& options = toptions::parse(argc, argv);
60 
62  IMG_Load(options.input_filename.c_str())));
63 
64  if(!surf) {
65  std::cerr << "Error: Failed to load input file »"
66  << options.input_filename
67  << "«.\n";
68 
69  return EXIT_FAILURE;
70  }
71 
72  std::vector<surface> surfaces;
73  if(options.count != 1) {
74  for(int i = 1; i < options.count; ++i) {
75  // make_neutral_surface make a deep-copy of the image.
76  surfaces.push_back(make_neutral_surface(surf));
77  }
78  }
79  surfaces.push_back(surf);
80 
81  const clock_t begin = options.time ? get_begin_time() : 0;
82 
83  for(int i = 0; i < options.count; ++i) {
84  for(const std::string& filter : options.filters) {
85  filter_apply(surfaces[i], filter);
86  }
87  }
88 
89  if(options.time) {
90  const clock_t end = std::clock();
91  std::cout << "Applying the filters took "
92  << end - begin
93  << " ticks, "
94  << static_cast<double>(end - begin) / CLOCKS_PER_SEC
95  << " seconds.\n";
96  }
97 
98  if(!options.output_filename.empty()) {
99  save_image(surfaces[0], options.output_filename);
100  }
101 
102  } catch(const texit& exit) {
103  return exit.status;
104  } catch(exploder_failure& err) {
105  std::cerr << "Error: Failed with error »" << err.message << "«.\n";
106  return EXIT_FAILURE;
107  }
108 
109  return EXIT_SUCCESS;
110 }
A singleton class containing the parsed command line parameters.
Definition: options.hpp:29
std::string message
bool time
Display the time that applying the filters took.
Definition: options.hpp:72
std::string output_filename
The filename of the output file.
Definition: options.hpp:66
bool save_image(const locator &i_locator, const std::string &filename)
Definition: image.cpp:1249
const config & options()
GLuint GLuint end
Definition: glew.h:1221
int main(int argc, char *argv[])
Definition: wesmage.cpp:56
std::string input_filename
The filename of the input file.
Definition: options.hpp:63
surf
Definition: filter.cpp:143
This exception when throw should terminate the application.
Definition: sdl2.cpp:35
logger & err()
Definition: log.cpp:79
const GLvdpauSurfaceNV * surfaces
Definition: glew.h:11139
size_t i
Definition: function.cpp:1057
Exit exception.
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glew.h:3448
surface make_neutral_surface(const surface &surf)
Definition: utils.cpp:135
Command line parameters for wesmage.
Filters for wesmage.
int status
The exit status for the application.
Definition: exit.hpp:34
static const toptions & parse(int argc, char *argv[])
Parses the command line.
Definition: options.cpp:157
int count
The number of times the filter has to be applied.
Definition: options.hpp:79
void filter_apply(surface &surf, const std::string &filter)
static clock_t get_begin_time()
Definition: wesmage.cpp:31
GLsizei const GLcharARB ** string
Definition: glew.h:4503
std::vector< std::string > filters
The filters to apply to the input file.
Definition: options.hpp:69