Merge lp:~widelands-dev/widelands/fix_screenshots into lp:widelands

Proposed by Tino
Status: Merged
Merged at revision: 7287
Proposed branch: lp:~widelands-dev/widelands/fix_screenshots
Merge into: lp:widelands
Diff against target: 129 lines (+30/-19)
4 files modified
src/graphic/graphic.cc (+2/-2)
src/graphic/image_io.cc (+25/-15)
src/graphic/image_io.h (+2/-1)
src/map_io/map_saver.cc (+1/-1)
To merge this branch: bzr merge lp:~widelands-dev/widelands/fix_screenshots
Reviewer Review Type Date Requested Status
SirVer Approve
Review via email: mp+243225@code.launchpad.net

Description of the change

Ok, next try.
Perhaps someone with more c++ knowledge could check/implement:
- the if/else condition in those 2 nested for clauses for every pixel is not nice
- can we move the RGBAColor declaration also out of the nested fors?

To post a comment you must log in.
Revision history for this message
SirVer (sirver) wrote :

Excellent! Thanks for fixing.

review: Approve
Revision history for this message
SirVer (sirver) wrote :

Oh, I had some inline comments, please consider them before merging.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/graphic/graphic.cc'
2--- src/graphic/graphic.cc 2014-11-24 07:10:03 +0000
3+++ src/graphic/graphic.cc 2014-11-30 12:59:06 +0000
4@@ -300,7 +300,7 @@
5 * @param sw a StreamWrite where the png is written to
6 */
7 void Graphic::save_png(const Image* image, StreamWrite * sw) const {
8- save_surface_to_png(image->texture(), sw);
9+ save_surface_to_png(image->texture(), sw, COLOR_TYPE::RGBA);
10 }
11
12 uint32_t Graphic::new_maptexture(const std::vector<std::string>& texture_files, const uint32_t frametime)
13@@ -326,7 +326,7 @@
14 {
15 log("Save screenshot to %s\n", fname.c_str());
16 StreamWrite * sw = g_fs->open_stream_write(fname);
17- save_surface_to_png(screen_.get(), sw);
18+ save_surface_to_png(screen_.get(), sw, COLOR_TYPE::RGB);
19 delete sw;
20 }
21
22
23=== modified file 'src/graphic/image_io.cc'
24--- src/graphic/image_io.cc 2014-11-28 18:41:13 +0000
25+++ src/graphic/image_io.cc 2014-11-30 12:59:06 +0000
26@@ -71,7 +71,7 @@
27 return sdlsurf;
28 }
29
30-bool save_surface_to_png(Surface* surface, StreamWrite* sw) {
31+bool save_surface_to_png(Surface* surface, StreamWrite* sw, COLOR_TYPE color_type) {
32 png_structp png_ptr = png_create_write_struct(
33 PNG_LIBPNG_VER_STRING, static_cast<png_voidp>(nullptr), nullptr, nullptr);
34
35@@ -104,7 +104,7 @@
36 surface->width(),
37 surface->height(),
38 8,
39- PNG_COLOR_TYPE_RGB,
40+ (color_type == COLOR_TYPE::RGB) ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGBA,
41 PNG_INTERLACE_NONE,
42 PNG_COMPRESSION_TYPE_DEFAULT,
43 PNG_FILTER_TYPE_DEFAULT);
44@@ -114,7 +114,7 @@
45 {
46 const uint16_t surf_w = surface->width();
47 const uint16_t surf_h = surface->height();
48- const uint32_t row_size = 3 * surf_w;
49+ const uint32_t row_size = (color_type == COLOR_TYPE::RGB) ? 3 * surf_w : 4 * surf_w;
50
51 std::unique_ptr<png_byte[]> row(new png_byte[row_size]);
52
53@@ -123,21 +123,31 @@
54 surface->lock(Surface::Lock_Normal);
55
56 // Write each row
57- for (uint32_t y = 0; y < surf_h; ++y) {
58- for (uint32_t x = 0; x < surf_w; ++x) {
59- RGBAColor color;
60- color.set(fmt, surface->get_pixel(x, y));
61- row[3 * x] = color.r;
62- row[3 * x + 1] = color.g;
63- row[3 * x + 2] = color.b;
64- }
65-
66- png_write_row(png_ptr, row.get());
67+ RGBAColor color;
68+ if (color_type == COLOR_TYPE::RGB) {
69+ for (uint32_t y = 0; y < surf_h; ++y) {
70+ for (uint32_t x = 0; x < surf_w; ++x) {
71+ color.set(fmt, surface->get_pixel(x, y));
72+ row[3 * x] = color.r;
73+ row[3 * x + 1] = color.g;
74+ row[3 * x + 2] = color.b;
75+ }
76+ png_write_row(png_ptr, row.get());
77+ }
78+ } else {
79+ for (uint32_t y = 0; y < surf_h; ++y) {
80+ for (uint32_t x = 0; x < surf_w; ++x) {
81+ color.set(fmt, surface->get_pixel(x, y));
82+ row[4 * x] = color.r;
83+ row[4 * x + 1] = color.g;
84+ row[4 * x + 2] = color.b;
85+ row[4 * x + 3] = color.a;
86+ }
87+ png_write_row(png_ptr, row.get());
88+ }
89 }
90-
91 surface->unlock(Surface::Unlock_NoChange);
92 }
93-
94 // End write
95 png_write_end(png_ptr, info_ptr);
96 png_destroy_write_struct(&png_ptr, &info_ptr);
97
98=== modified file 'src/graphic/image_io.h'
99--- src/graphic/image_io.h 2014-11-24 07:10:03 +0000
100+++ src/graphic/image_io.h 2014-11-30 12:59:06 +0000
101@@ -29,6 +29,7 @@
102 class StreamWrite;
103 class Surface;
104 struct SDL_Surface;
105+enum class COLOR_TYPE {RGB, RGBA};
106
107 class ImageNotFound : public WException {
108 public:
109@@ -50,6 +51,6 @@
110 SDL_Surface* load_image_as_sdl_surface(const std::string& fn, FileSystem* fs = nullptr);
111
112 /// Saves the 'surface' to 'sw' as a PNG.
113-bool save_surface_to_png(Surface* surface, StreamWrite* sw);
114+bool save_surface_to_png(Surface* surface, StreamWrite* sw, COLOR_TYPE color_type);
115
116 #endif // end of include guard: WL_GRAPHIC_IMAGE_IO_H
117
118=== modified file 'src/map_io/map_saver.cc'
119--- src/map_io/map_saver.cc 2014-11-24 07:10:03 +0000
120+++ src/map_io/map_saver.cc 2014-11-30 12:59:06 +0000
121@@ -220,7 +220,7 @@
122 std::unique_ptr<Texture> minimap(
123 draw_minimap(m_egbase, nullptr, Point(0, 0), MiniMapLayer::Terrain));
124 FileWrite fw;
125- save_surface_to_png(minimap.get(), &fw);
126+ save_surface_to_png(minimap.get(), &fw, COLOR_TYPE::RGBA);
127 fw.write(m_fs, "minimap.png");
128 }
129 }

Subscribers

People subscribed via source and target branches

to status/vote changes: