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