Debian Codegraph @

common/2d/bitmap.cpp

raw
1 /*
2 * Portions of this file are copyright Rebirth contributors and licensed as
3 * described in COPYING.txt.
4 * Portions of this file are copyright Parallax Software and licensed
5 * according to the Parallax license below.
6 * See COPYING.txt for license details.
7
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20 /*
21 *
22 * Graphical routines for manipulating grs_bitmaps.
23 *
24 */
25
26 #include <algorithm>
27 #include <stdexcept>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include "u_mem.h"
31 #include "gr.h"
32 #include "dxxerror.h"
33 #if DXX_USE_OGL
34 #include "ogl_init.h"
35 #endif
36 #include "bitmap.h"
37 #include <memory>
38
39 namespace dcx {
40 namespace {
41
42 // Allocated a bitmap and makes its data be raw_data that is already somewhere.
43 static grs_bitmap_ptr gr_create_bitmap_raw(uint16_t w, uint16_t h, RAIIdmem<uint8_t[]> raw_data);
44
45 [[noreturn]]
46 static void report_overflow(const std::array<char, 124> &buf, const int written)
47 {
48 throw std::runtime_error(std::string{std::data(buf), written > 0 ? std::min<unsigned>(std::size(buf), written) : 0});
49 }
50
51 [[noreturn]]
52 static void report_offset_overflow(const char dimension, const uint16_t displacement, const uint16_t parent_displacement)
53 {
54 std::array<char, 124> buf;
55 report_overflow(buf, {std::snprintf(std::data(buf), std::size(buf), "offset overflow: %c displacement %hu + parent %hu exceeds uint16_t", dimension, displacement, parent_displacement)});
56 }
57
58 [[noreturn]]
59 static void report_dimension_overflow(const char dimension, const uint16_t offset, const uint16_t parent_dimension)
60 {
61 std::array<char, 124> buf;
62 report_overflow(buf, {std::snprintf(std::data(buf), std::size(buf), "offset beyond parent dimension: %c offset %hu > parent %hu", dimension, offset, parent_dimension)});
63 }
64
65 }
66
67 void gr_set_bitmap_data(grs_bitmap &bm, const uint8_t *data)
68 {
69 #if DXX_USE_OGL
70 ogl_freebmtexture(bm);
71 #endif
72 bm.bm_data = data;
73 }
74
75 grs_bitmap_ptr gr_create_bitmap(uint16_t w, uint16_t h )
76 {
77 RAIIdmem<uint8_t[]> d;
78 MALLOC(d, uint8_t[], MAX_BMP_SIZE(w, h));
79 return gr_create_bitmap_raw(w, h, std::move(d));
80 }
81
82 namespace {
83
84 grs_bitmap_ptr gr_create_bitmap_raw(const uint16_t w, const uint16_t h, RAIIdmem<uint8_t[]> raw_data)
85 {
86 auto n = std::make_unique<grs_main_bitmap>();
87 gr_init_main_bitmap(*n.get(), bm_mode::linear, 0, 0, w, h, w, std::move(raw_data));
88 return n;
89 }
90
91 }
92
93 // TODO: virtualize
94 void gr_init_bitmap(grs_bitmap &bm, const bm_mode mode, const uint16_t x, const uint16_t y, const uint16_t w, const uint16_t h, const uint16_t bytesperline, color_palette_index *const mdata) noexcept
95 {
96 bm.bm_x = x;
97 bm.bm_y = y;
98 bm.bm_w = w;
99 bm.bm_h = h;
100 bm.set_flags(0);
101 bm.set_type(mode);
102 bm.bm_rowsize = bytesperline;
103
104 bm.bm_mdata = mdata;
105 #if DXX_USE_OGL
106 bm.bm_parent = nullptr;
107 bm.gltexture = nullptr;
108 #endif
109 }
110
111 void gr_init_main_bitmap(grs_main_bitmap &bm, const bm_mode mode, const uint16_t x, const uint16_t y, const uint16_t w, const uint16_t h, const uint16_t bytesperline, RAIIdmem<uint8_t[]> data)
112 {
113 bm.reset();
114 gr_init_bitmap(bm, mode, x, y, w, h, bytesperline, data.get());
115 data.release();
116 }
117
118 void gr_init_bitmap_alloc(grs_main_bitmap &bm, const bm_mode mode, const uint16_t x, const uint16_t y, const uint16_t w, const uint16_t h, const uint16_t bytesperline)
119 {
120 RAIIdmem<uint8_t[]> d;
121 MALLOC(d, uint8_t[], MAX_BMP_SIZE(w, h));
122 gr_init_main_bitmap(bm, mode, x, y, w, h, bytesperline, std::move(d));
123 }
124
125 grs_main_bitmap::grs_main_bitmap()
126 {
127 static_cast<grs_bitmap &>(*this) = {};
128 }
129
130 grs_subbitmap_ptr gr_create_sub_bitmap(grs_bitmap &bm, uint16_t x, uint16_t y, uint16_t w, uint16_t h)
131 {
132 auto n = std::make_unique<grs_subbitmap>();
133 gr_init_sub_bitmap(*n.get(), bm, {x}, {y}, {w}, {h});
134 return n;
135 }
136
137 void gr_free_bitmap_data (grs_bitmap &bm) // TODO: virtulize
138 {
139 if (auto &d = bm.bm_mdata)
140 d_free(d);
141 #if DXX_USE_OGL
142 ogl_freebmtexture(bm);
143 #endif
144 }
145
146 void gr_init_sub_bitmap(grs_bitmap &bm, grs_bitmap &bmParent, const uint16_t x, const uint16_t y, const uint16_t w, const uint16_t h) // TODO: virtualize
147 {
148 const uint32_t subx{unsigned{x} + bmParent.bm_x};
149 const uint32_t suby{unsigned{y} + bmParent.bm_y};
150 if (!std::in_range<uint16_t>(subx))
151 report_offset_overflow('x', {x}, {bmParent.bm_x});
152 if (!std::in_range<uint16_t>(suby))
153 report_offset_overflow('y', {y}, {bmParent.bm_y});
154 if (std::cmp_greater(x, bmParent.bm_w))
155 report_dimension_overflow('x', {x}, {bmParent.bm_w});
156 if (std::cmp_greater(y, bmParent.bm_h))
157 report_dimension_overflow('y', {y}, {bmParent.bm_h});
158 bm.bm_x = static_cast<uint16_t>(subx);
159 bm.bm_y = static_cast<uint16_t>(suby);
160 bm.bm_w = std::min<uint16_t>(w, bmParent.bm_w - x);
161 bm.bm_h = std::min<uint16_t>(h, bmParent.bm_h - y);
162 bm.set_flags(bmParent.get_flags());
163 bm.set_type(bmParent.get_type());
164 bm.bm_rowsize = bmParent.bm_rowsize;
165
166 #if DXX_USE_OGL
167 bm.gltexture = bmParent.gltexture;
168 #endif
169 bm.bm_parent = &bmParent;
170 bm.bm_data = &bmParent.bm_data[static_cast<uint32_t>((y*bmParent.bm_rowsize)+x)];
171 }
172
173 void decode_data(const std::span<color_palette_index> data, const std::array<color_palette_index, 256> &colormap, std::bitset<256> &used)
174 {
175 const auto a = [&](uint8_t mapped) {
176 return used[mapped] = true, colormap[mapped];
177 };
178 std::transform(data.begin(), data.end(), data.begin(), a);
179 }
180
181 namespace {
182
183 static void gr_set_super_transparent(grs_bitmap &bm, bool bOpaque)
184 {
185 bm.set_flag_mask(!bOpaque, BM_FLAG_SUPER_TRANSPARENT);
186 }
187
188 }
189
190 std::array<color_palette_index, 256> build_colormap_good(const palette_array_t &palette)
191 {
192 std::array<color_palette_index, 256> colormap;
193 const auto a = [](const rgb_t &p) {
194 return gr_find_closest_color(p.r, p.g, p.b);
195 };
196 std::ranges::transform(palette, colormap.begin(), a);
197 return colormap;
198 }
199
200 void gr_remap_bitmap_good(grs_bitmap &bmp, const palette_array_t &palette, uint_fast32_t transparent_color, uint_fast32_t super_transparent_color)
201 {
202 auto colormap{build_colormap_good(palette)};
203
204 if (super_transparent_color < colormap.size())
205 colormap[super_transparent_color] = color_palette_index{254};
206
207 if (transparent_color < colormap.size())
208 colormap[transparent_color] = TRANSPARENCY_COLOR;
209
210 std::bitset<256> freq{};
211 if (bmp.bm_w == bmp.bm_rowsize)
212 decode_data(std::span(bmp.get_bitmap_data(), bmp.bm_w * bmp.bm_h), colormap, freq);
213 else {
214 auto p = bmp.get_bitmap_data();
215 for (uint_fast32_t y = bmp.bm_h; y--; p += bmp.bm_rowsize)
216 decode_data(std::span{p, bmp.bm_w}, colormap, freq);
217 }
218
219 if (transparent_color < freq.size() && freq[transparent_color])
220 gr_set_transparent(bmp, 1);
221
222 if (super_transparent_color < freq.size() && freq[super_transparent_color])
223 gr_set_super_transparent(bmp, 1);
224 }
225
226 }