GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/pnglib.cpp Lines: 0 34 0.0 %
Date: 2017-11-29 Branches: 0 32 0.0 %

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