The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
create_images.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 create the test images for the unit tests.
18  */
19 
20 #include "filesystem.hpp"
21 #include "tools/exploder_utils.hpp"
22 #include "tests/test_sdl_utils.hpp"
23 
24 #include <SDL_image.h>
25 
26 #include "utils/functional.hpp"
27 
28 #include <iostream>
29 
30 static void
32 {
33  std::cerr <<
34 "Usage:\n"
35 "test_images -h\n"
36 "test_images DIR\n"
37 "Helper program to create images for the unit tests.\n"
38 "\n"
39 "The DIR is the output directory, will be created if doesn't exist.\n"
40 "OPTIONS:\n"
41 "-h, --help Show this help and terminate the program.\n"
42 ;
43 }
44 
45 static surface
47 {
48  surface result = SDL_CreateRGBSurface(
49  SDL_SWSURFACE
50  , 1024
51  , 1024
52  , 32
53  , 0xFF0000
54  , 0xFF00
55  , 0xFF
56  , 0);
57 
58  surface_lock lock(result);
59 
60  Uint32 *pixels = lock.pixels();
61 
62  for(Uint32 r = 0; r < 256; r += 4) {
63  for(Uint32 g = 0; g < 256; g += 2) {
64  for(Uint32 b = 0; b < 256; b += 2, ++pixels) {
65 
66  *pixels = (0xFF << 24) | (r << 16) | (g << 8) | b;
67 
68  }
69  }
70  }
71 
72  save_image(result, filename);
73 
74  return result;
75 }
76 
77 static void
79  const surface& dst
80  , const std::string root
81  , const Uint8 amount
82  , const Uint32 color)
83 {
84  const std::string filename = blend_get_filename(root, amount, color);
85 
86  save_image(dst, filename);
87 }
88 
89 static void
91 {
93  src
94  , std::bind(&create_image_blend_functor, _1, root, _2, _3));
95 }
96 
97 typedef void (*tfunctor) (const surface&, const std::string&);
98 typedef std::pair<std::string, tfunctor> tcreator;
99 
100 static const tcreator creators[] =
101 {
102  std::make_pair("/blend/", &create_image_blend)
103 };
104 
105 int
106 main(int argc, char* argv[])
107 {
108  if(argc != 2) {
109  show_usage();
110  return EXIT_FAILURE;
111  }
112 
113  const std::string root = argv[1];
114 
115  if(root == "-h" || root == "--help") {
116  show_usage();
117  return EXIT_SUCCESS;
118  }
119 
120  if(!filesystem::is_directory(root)) {
121  if(filesystem::file_exists(root)) {
122  std::cerr << "";
123  return EXIT_FAILURE;
124  }
125  if(!filesystem::make_directory(root)) {
126  std::cerr << "";
127  return EXIT_FAILURE;
128  }
129  }
130 
131  for(const tcreator& creator : creators) {
132  if(!filesystem::make_directory(root + creator.first)) {
133  std::cerr << "";
134  return EXIT_FAILURE;
135  }
136  }
137 
138  try {
139  const surface base_image = create_image_base(root + "/base.png");
140 
141  for(const tcreator& creator : creators) {
142  creator.second(base_image, root + creator.first);
143  }
144 
145  } catch(exploder_failure& err) {
146  std::cerr << "Error: Failed with error »" << err.message << "«.\n";
147  return EXIT_FAILURE;
148  }
149 
150  return EXIT_SUCCESS;
151 }
152 
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: glew.h:1222
GLvoid **typedef void(GLAPIENTRY *PFNGLGETVERTEXATTRIBDVPROC)(GLuint
Definition: glew.h:1806
std::pair< std::string, tfunctor > tcreator
void blend_image(const surface &src, tblend_functor functor)
static surface create_image_base(const std::string &filename)
std::string message
GLboolean GLboolean g
Definition: glew.h:7319
GLenum src
Definition: glew.h:2392
bool save_image(const locator &i_locator, const std::string &filename)
Definition: image.cpp:1249
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
GLuint64EXT * result
Definition: glew.h:10727
GLenum GLenum dst
Definition: glew.h:2392
bool is_directory(const std::string &fname)
Returns true if the given file is a directory.
Helper class for pinning SDL surfaces into memory.
Definition: utils.hpp:439
GLuint color
Definition: glew.h:5801
const std::string &parameters float amount
Definition: filter.cpp:132
logger & err()
Definition: log.cpp:79
bool make_directory(const std::string &dirname)
Declarations for File-IO.
GLdouble GLdouble GLdouble r
Definition: glew.h:1374
std::string blend_get_filename(std::string root, const Uint8 amount, const Uint32 color)
static const tcreator creators[]
static void create_image_blend(const surface &src, const std::string &root)
static void show_usage()
static void create_image_blend_functor(const surface &dst, const std::string root, const Uint8 amount, const Uint32 color)
Uint32 * pixels()
Definition: utils.hpp:444
int main(int argc, char *argv[])
void(* tfunctor)(const surface &, const std::string &)
bool file_exists(const std::string &name)
Returns true if a file or directory with such name already exists.
GLsizei const GLcharARB ** string
Definition: glew.h:4503