Mana
Loading...
Searching...
No Matches
imagewriter.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
23
24#include "log.h"
25
26#include <png.h>
27#include <SDL.h>
28#include <string>
29
30bool ImageWriter::writePNG(SDL_Surface *surface, const std::string &filename)
31{
32 // TODO Maybe someone can make this look nice?
33
34 png_structp png_ptr;
35 png_infop info_ptr;
36 png_bytep *row_pointers;
37 int colortype;
38
39 if (SDL_MUSTLOCK(surface))
40 SDL_LockSurface(surface);
41
42 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
43 if (!png_ptr)
44 {
45 Log::info("Had trouble creating png_structp");
46 return false;
47 }
48
49 info_ptr = png_create_info_struct(png_ptr);
50 if (!info_ptr)
51 {
52 png_destroy_write_struct(&png_ptr, (png_infopp)nullptr);
53 Log::info("Could not create png_info");
54 return false;
55 }
56
57 if (setjmp(png_jmpbuf(png_ptr)))
58 {
59 png_destroy_write_struct(&png_ptr, (png_infopp)nullptr);
60 Log::info("problem writing to %s", filename.c_str());
61 return false;
62 }
63
64 FILE *fp = fopen(filename.c_str(), "wb");
65 if (!fp)
66 {
67 Log::info("could not open file %s for writing", filename.c_str());
68 return false;
69 }
70
71 png_init_io(png_ptr, fp);
72
73 colortype = (surface->format->BitsPerPixel == 24) ?
74 PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGB_ALPHA;
75
76 png_set_IHDR(png_ptr, info_ptr, surface->w, surface->h, 8, colortype,
77 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
78
79 png_write_info(png_ptr, info_ptr);
80
81 png_set_packing(png_ptr);
82
83 row_pointers = new png_bytep[surface->h];
84 if (!row_pointers)
85 {
86 Log::info("Had trouble converting surface to row pointers");
87 fclose(fp);
88 return false;
89 }
90
91 for (int i = 0; i < surface->h; i++)
92 {
93 row_pointers[i] = (png_bytep)(Uint8 *)surface->pixels + i * surface->pitch;
94 }
95
96 png_write_image(png_ptr, row_pointers);
97 png_write_end(png_ptr, info_ptr);
98
99 fclose(fp);
100
101 delete [] row_pointers;
102
103 png_destroy_write_struct(&png_ptr, (png_infopp)nullptr);
104
105 if (SDL_MUSTLOCK(surface))
106 SDL_UnlockSurface(surface);
107
108 return true;
109}
static bool writePNG(SDL_Surface *surface, const std::string &filename)
void info(const char *log_text,...) LOG_PRINTF_ATTR