Debian Codegraph @

bitmap/NewBitmap.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Descent Developers
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 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19
20 #include "NewBitmap.h"
21 #include "pserror.h"
22
23 #include <stb_image_write.h>
24
25 NewBitmap::NewBitmap(uint32_t w, uint32_t h, PixelDataFormat format, bool flippedY)
26 : _w(w), _h(h), _format(format), _flippedY(flippedY), _data(allocateData(w, h, format)) {
27 ASSERT(_data.get());
28 }
29
30 std::unique_ptr<uint8_t[]> NewBitmap::allocateData(uint32_t w, uint32_t h, PixelDataFormat format) {
31 switch (format) {
32 case PixelDataFormat::RGBA32: return std::make_unique<uint8_t[]>(w * h * 4);
33 default: return nullptr;
34 }
35 }
36
37 uint32_t NewBitmap::getNumComponents() const {
38 switch (_format) {
39 case PixelDataFormat::RGBA32: return 4;
40 default: return 0;
41 }
42 }
43
44 uint32_t NewBitmap::getStride() const {
45 switch (_format) {
46 case PixelDataFormat::RGBA32: return _w * 4;
47 default: return 0;
48 }
49 }
50
51 bool NewBitmap::saveAsPNG(const char *filePath) {
52 // TODO: Support more formats? stb_write supports at least RGB and RGBA
53 ASSERT(_format == PixelDataFormat::RGBA32);
54
55 stbi_flip_vertically_on_write(_flippedY);
56 return stbi_write_png(filePath, _w, _h, getNumComponents(), _data.get(), getStride()) == 0;
57 }