Debian Codegraph @

common/2d/bitblt.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-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20 /*
21 *
22 * Routines for bitblt's.
23 *
24 */
25
26 #include <algorithm>
27 #include <utility>
28 #include <string.h>
29 #include "u_mem.h"
30 #include "gr.h"
31 #include "rle.h"
32 #include "dxxerror.h"
33 #include "byteutil.h"
34 #if DXX_USE_OGL
35 #include "ogl_init.h"
36 #endif
37
38 #include "compiler-range_for.h"
39 #include "d_range.h"
40 #include <array>
41
42 namespace dcx {
43
44 namespace {
45
46 static void gr_bm_ubitblt00_rle(unsigned w, unsigned h, int dx, int dy, int sx, int sy, const grs_bitmap &src, grs_bitmap &dest);
47 #if !DXX_USE_OGL
48 static void gr_bm_ubitblt00m_rle(unsigned w, unsigned h, int dx, int dy, int sx, int sy, const grs_bitmap &src, grs_bitmap &dest);
49 static void gr_bm_ubitblt0x_rle(grs_canvas &dest, unsigned w, unsigned h, int dx, int dy, int sx, int sy, const grs_bitmap &src);
50 #endif
51
52 #define gr_linear_movsd(S,D,L) memcpy(D,S,L)
53
54 #if !DXX_USE_OGL
55 static void gr_linear_rep_movsdm(uint8_t *const dest, const uint8_t *const src, const uint_fast32_t num_pixels)
56 {
57 auto predicate = [&](uint8_t s, uint8_t d) {
58 return s == 255 ? d : s;
59 };
60 std::transform(src, src + num_pixels, dest, dest, predicate);
61 }
62 #endif
63
64 static void gr_for_each_bitmap_line(grs_canvas &canvas, const unsigned x, const unsigned y, const grs_bitmap &bm, auto &&f)
65 {
66 const size_t src_width = bm.bm_w;
67 const uintptr_t src_rowsize = bm.bm_rowsize;
68 const uintptr_t dest_rowsize = canvas.cv_bitmap.bm_rowsize;
69 auto dest = &(canvas.cv_bitmap.get_bitmap_data()[ dest_rowsize*y+x ]);
70 auto src = bm.get_bitmap_data();
71 for (uint_fast32_t y1 = bm.bm_h; y1 --;)
72 {
73 f(dest, src, src_width);
74 src += src_rowsize;
75 dest+= dest_rowsize;
76 }
77 }
78
79 static void gr_ubitmap00(grs_canvas &canvas, const unsigned x, const unsigned y, const grs_bitmap &bm)
80 {
81 gr_for_each_bitmap_line(canvas, x, y, bm, memcpy);
82 }
83
84 #if !DXX_USE_OGL
85 static void gr_ubitmap00m(grs_canvas &canvas, const unsigned x, const unsigned y, const grs_bitmap &bm)
86 {
87 gr_for_each_bitmap_line(canvas, x, y, bm, gr_linear_rep_movsdm);
88 }
89 #endif
90
91 static inline void gr_for_each_bitmap_byte(grs_canvas &canvas, const uint_fast32_t bx, const uint_fast32_t by, const grs_bitmap &bm, auto &&f)
92 {
93 auto src = bm.bm_data;
94 const auto ey = by + bm.bm_h;
95 const auto ex = bx + bm.bm_w;
96 range_for (const auto iy, xrange(by, ey))
97 range_for (const auto ix, xrange(bx, ex))
98 f(canvas, src++, ix, iy);
99 }
100
101 static void gr_ubitmap012(grs_canvas &canvas, const unsigned x, const unsigned y, const grs_bitmap &bm)
102 {
103 const auto a = [](grs_canvas &cv, const color_palette_index *const src, const uint_fast32_t px, const uint_fast32_t py) {
104 const auto color = *src;
105 gr_upixel(cv.cv_bitmap, px, py, color);
106 };
107 gr_for_each_bitmap_byte(canvas, x, y, bm, a);
108 }
109
110 #if !DXX_USE_OGL
111 static void gr_ubitmap012m(grs_canvas &canvas, const unsigned x, const unsigned y, const grs_bitmap &bm)
112 {
113 const auto a = [](grs_canvas &cv, const color_palette_index *const src, const uint_fast32_t px, const uint_fast32_t py) {
114 const uint8_t c = *src;
115 if (c != 255)
116 {
117 gr_upixel(cv.cv_bitmap, px, py, c);
118 }
119 };
120 gr_for_each_bitmap_byte(canvas, x, y, bm, a);
121 }
122 #endif
123
124 static void gr_ubitmapGENERIC(grs_canvas &canvas, const unsigned x, const unsigned y, const grs_bitmap &bm)
125 {
126 const uint_fast32_t bm_h = bm.bm_h;
127 const uint_fast32_t bm_w = bm.bm_w;
128 range_for (const uint_fast32_t y1, xrange(bm_h))
129 {
130 range_for (const uint_fast32_t x1, xrange(bm_w))
131 {
132 const auto color = gr_gpixel(bm, x1, y1);
133 gr_upixel(canvas.cv_bitmap, x + x1, y + y1, color);
134 }
135 }
136 }
137
138 #if !DXX_USE_OGL
139 static void gr_ubitmapGENERICm(grs_canvas &canvas, const unsigned x, const unsigned y, const grs_bitmap &bm)
140 {
141 const uint_fast32_t bm_h = bm.bm_h;
142 const uint_fast32_t bm_w = bm.bm_w;
143 range_for (const uint_fast32_t y1, xrange(bm_h))
144 {
145 range_for (const uint_fast32_t x1, xrange(bm_w))
146 {
147 const auto c = gr_gpixel(bm,x1,y1);
148 if (c != TRANSPARENCY_COLOR)
149 {
150 gr_upixel(canvas.cv_bitmap, x + x1, y + y1, c);
151 }
152 }
153 }
154 }
155 #endif
156
157 }
158
159 void gr_ubitmap(grs_canvas &canvas, grs_bitmap &bm)
160 {
161 const unsigned x = 0;
162 const unsigned y = 0;
163
164 const auto source = bm.get_type();
165 const auto dest = canvas.cv_bitmap.get_type();
166
167 if (source==bm_mode::linear) {
168 switch( dest )
169 {
170 case bm_mode::linear:
171 if (bm.get_flag_mask(BM_FLAG_RLE))
172 gr_bm_ubitblt00_rle(bm.bm_w, bm.bm_h, x, y, 0, 0, bm, canvas.cv_bitmap);
173 else
174 gr_ubitmap00(canvas, x, y, bm);
175 return;
176 #if DXX_USE_OGL
177 case bm_mode::ogl:
178 ogl_ubitmapm_cs(canvas, x, y, opengl_bitmap_use_dst_canvas, opengl_bitmap_use_dst_canvas, bm, ogl_colors::white);
179 return;
180 #endif
181 default:
182 gr_ubitmap012(canvas, x, y, bm);
183 return;
184 }
185 } else {
186 gr_ubitmapGENERIC(canvas, x, y, bm);
187 }
188 }
189
190 #if !DXX_USE_OGL
191 void gr_ubitmapm(grs_canvas &canvas, const unsigned x, const unsigned y, grs_bitmap &bm)
192 {
193 const auto source = bm.get_type();
194 if (source==bm_mode::linear) {
195 switch(canvas.cv_bitmap.get_type())
196 {
197 case bm_mode::linear:
198 if (bm.get_flag_mask(BM_FLAG_RLE))
199 gr_bm_ubitblt00m_rle(bm.bm_w, bm.bm_h, x, y, 0, 0, bm, canvas.cv_bitmap);
200 else
201 gr_ubitmap00m(canvas, x, y, bm);
202 return;
203 default:
204 gr_ubitmap012m(canvas, x, y, bm);
205 return;
206 }
207 } else {
208 gr_ubitmapGENERICm(canvas, x, y, bm);
209 }
210 }
211
212 namespace {
213
214 // From Linear to Linear
215 static void gr_bm_ubitblt00(const unsigned w, const unsigned h, const unsigned dx, const unsigned dy, const unsigned sx, const unsigned sy, const grs_bitmap &src, grs_bitmap &dest)
216 {
217 //int src_bm_rowsize_2, dest_bm_rowsize_2;
218 auto sbits = &src.get_bitmap_data()[(src.bm_rowsize * sy) + sx];
219 auto dbits = &dest.get_bitmap_data()[(dest.bm_rowsize * dy) + dx];
220 const auto dstep = dest.bm_rowsize;
221 // No interlacing, copy the whole buffer.
222 for (uint_fast32_t i = h; i --;)
223 {
224 gr_linear_movsd( sbits, dbits, w );
225 //memcpy(dbits, sbits, w);
226 sbits += src.bm_rowsize;
227 dbits += dstep;
228 }
229 }
230
231 // From Linear to Linear Masked
232 static void gr_bm_ubitblt00m(const unsigned w, const uint_fast32_t h, const unsigned dx, const unsigned dy, const unsigned sx, const unsigned sy, const grs_bitmap &src, grs_bitmap &dest)
233 {
234 //int src_bm_rowsize_2, dest_bm_rowsize_2;
235 auto sbits = &src.get_bitmap_data()[(src.bm_rowsize * sy) + sx];
236 auto dbits = &dest.get_bitmap_data()[(dest.bm_rowsize * dy) + dx];
237
238 // No interlacing, copy the whole buffer.
239
240 {
241 for (auto i = h; i; --i)
242 {
243 gr_linear_rep_movsdm(dbits, sbits, w);
244 sbits += src.bm_rowsize;
245 dbits += dest.bm_rowsize;
246 }
247 }
248 }
249
250 }
251
252 void gr_bm_ubitblt(grs_canvas &canvas, const unsigned w, const unsigned h, const int dx, const int dy, const int sx, const int sy, const grs_bitmap &src)
253 {
254 auto &dest = canvas.cv_bitmap;
255 if (src.get_type() == bm_mode::linear && dest.get_type() == bm_mode::linear)
256 {
257 if (src.get_flag_mask(BM_FLAG_RLE))
258 gr_bm_ubitblt00_rle( w, h, dx, dy, sx, sy, src, dest );
259 else
260 gr_bm_ubitblt00( w, h, dx, dy, sx, sy, src, dest );
261 return;
262 }
263
264 if (src.get_flag_mask(BM_FLAG_RLE) && src.get_type() == bm_mode::linear)
265 {
266 gr_bm_ubitblt0x_rle(canvas, w, h, dx, dy, sx, sy, src);
267 return;
268 }
269
270 range_for (const uint_fast32_t y1, xrange(h))
271 range_for (const uint_fast32_t x1, xrange(w))
272 gr_bm_pixel(canvas, dest, dx + x1, dy + y1, gr_gpixel(src, sx + x1, sy + y1));
273 }
274 #endif
275
276 // Clipped bitmap ...
277 void gr_bitmap(grs_canvas &canvas, const unsigned x, const unsigned y, grs_bitmap &bm)
278 {
279 int dx1=x, dx2=x+bm.bm_w-1;
280 int dy1=y, dy2=y+bm.bm_h-1;
281
282 if (dx1 >= canvas.cv_bitmap.bm_w || dx2 < 0)
283 return;
284 if (dy1 >= canvas.cv_bitmap.bm_h || dy2 < 0)
285 return;
286 // Draw bitmap bm[x,y] into (dx1,dy1)-(dx2,dy2)
287 #if DXX_USE_OGL
288 ogl_ubitmapm_cs(canvas, x, y, opengl_bitmap_use_src_bitmap, opengl_bitmap_use_src_bitmap, bm, ogl_colors::white);
289 #else
290 int sx{0}, sy = 0;
291 if ( dx1 < 0 )
292 {
293 sx = -dx1;
294 dx1 = 0;
295 }
296 if ( dy1 < 0 )
297 {
298 sy = -dy1;
299 dy1 = 0;
300 }
301 if (dx2 >= canvas.cv_bitmap.bm_w)
302 dx2 = canvas.cv_bitmap.bm_w - 1;
303 if (dy2 >= canvas.cv_bitmap.bm_h)
304 dy2 = canvas.cv_bitmap.bm_h - 1;
305
306 gr_bm_ubitblt(canvas, dx2 - dx1 + 1, dy2 - dy1 + 1, dx1, dy1, sx, sy, bm);
307 #endif
308 }
309
310 #if !DXX_USE_OGL
311 void gr_bitmapm(grs_canvas &canvas, const unsigned x, const unsigned y, const grs_bitmap &bm)
312 {
313 int dx1=x, dx2=x+bm.bm_w-1;
314 int dy1=y, dy2=y+bm.bm_h-1;
315 int sx{0}, sy=0;
316
317 if (dx1 >= canvas.cv_bitmap.bm_w || dx2 < 0)
318 return;
319 if (dy1 >= canvas.cv_bitmap.bm_h || dy2 < 0)
320 return;
321 if ( dx1 < 0 ) { sx = -dx1; dx1 = 0; }
322 if ( dy1 < 0 ) { sy = -dy1; dy1 = 0; }
323 if (dx2 >= canvas.cv_bitmap.bm_w)
324 dx2 = canvas.cv_bitmap.bm_w - 1;
325 if (dy2 >= canvas.cv_bitmap.bm_h)
326 dy2 = canvas.cv_bitmap.bm_h - 1;
327
328 // Draw bitmap bm[x,y] into (dx1,dy1)-(dx2,dy2)
329
330 if (bm.get_type() == bm_mode::linear && canvas.cv_bitmap.get_type() == bm_mode::linear)
331 {
332 if (bm.get_flag_mask(BM_FLAG_RLE))
333 gr_bm_ubitblt00m_rle(dx2 - dx1 + 1, dy2 - dy1 + 1, dx1, dy1, sx, sy, bm, canvas.cv_bitmap );
334 else
335 gr_bm_ubitblt00m(dx2 - dx1 + 1, dy2 - dy1 + 1, dx1, dy1, sx, sy, bm, canvas.cv_bitmap );
336 return;
337 }
338 gr_bm_ubitbltm(canvas, dx2 - dx1 + 1, dy2 - dy1 + 1, dx1, dy1, sx, sy, bm);
339 }
340
341 void gr_bm_ubitbltm(grs_canvas &canvas, const unsigned w, const unsigned h, const unsigned dx, const unsigned dy, const unsigned sx, const unsigned sy, const grs_bitmap &src)
342 {
343 ubyte c;
344
345 auto &dest = canvas.cv_bitmap;
346 range_for (const uint_fast32_t y1, xrange(h))
347 range_for (const uint_fast32_t x1, xrange(w))
348 if ((c=gr_gpixel(src,sx+x1,sy+y1))!=255)
349 gr_bm_pixel(canvas, dest, dx + x1, dy + y1, c);
350 }
351 #endif
352
353 namespace {
354
355 class bm_rle_window : bm_rle_src_stride
356 {
357 public:
358 bm_rle_window(const grs_bitmap &src) :
359 bm_rle_src_stride{src, src.get_flag_mask(BM_FLAG_RLE_BIG)}
360 {
361 }
362 void skip_upper_rows(uint_fast32_t);
363 uint8_t *init(uint_fast32_t dx, uint_fast32_t dy, uint_fast32_t sy, grs_bitmap &dest);
364 void apply(uint_fast32_t w, uint_fast32_t h, uint_fast32_t sx, uint8_t *dbits, uint_fast32_t bm_rowsize, auto &&f);
365 #if !DXX_USE_OGL
366 using bm_rle_src_stride::src_bits;
367 using bm_rle_src_stride::advance_src_bits;
368 #endif
369 };
370
371 void bm_rle_window::skip_upper_rows(const uint_fast32_t sy)
372 {
373 for (uint_fast32_t i = sy; i; --i)
374 advance_src_bits();
375 }
376
377 uint8_t *bm_rle_window::init(const uint_fast32_t dx, const uint_fast32_t dy, const uint_fast32_t sy, grs_bitmap &dest)
378 {
379 skip_upper_rows(sy);
380 return &dest.get_bitmap_data()[(dest.bm_rowsize * dy) + dx];
381 }
382
383 void bm_rle_window::apply(const uint_fast32_t w, const uint_fast32_t h, const uint_fast32_t sx, uint8_t *dbits, const uint_fast32_t bm_rowsize, auto &&f)
384 {
385 // No interlacing, copy the whole buffer.
386 for (uint_fast32_t i = h; i; --i)
387 {
388 f(std::exchange(dbits, dbits + bm_rowsize), src_bits, sx, w);
389 advance_src_bits();
390 }
391 }
392
393 static void gr_bm_ubitblt00_rle(const unsigned w, const unsigned h, const int dx, const int dy, const int sx, const int sy, const grs_bitmap &src, grs_bitmap &dest)
394 {
395 bm_rle_window bw(src);
396 bw.apply(sx + w - 1, h, sx, bw.init(dx, dy, sy, dest), dest.bm_rowsize, gr_rle_expand_scanline);
397 }
398
399 #if !DXX_USE_OGL
400 static void gr_bm_ubitblt00m_rle(const unsigned w, const unsigned h, const int dx, const int dy, const int sx, const int sy, const grs_bitmap &src, grs_bitmap &dest)
401 {
402 bm_rle_window bw(src);
403 bw.apply(sx + w - 1, h, sx, bw.init(dx, dy, sy, dest), dest.bm_rowsize, gr_rle_expand_scanline_masked);
404 }
405
406 // in rle.c
407
408 static void gr_bm_ubitblt0x_rle(grs_canvas &canvas, const unsigned w, const unsigned h, const int dx, const int dy, const int sx, const int sy, const grs_bitmap &src)
409 {
410 bm_rle_window bw(src);
411 bw.skip_upper_rows(sy);
412 range_for (const uint_fast32_t y1, xrange(h))
413 {
414 const auto sbits = bw.src_bits;
415 gr_rle_expand_scanline_generic(canvas, canvas.cv_bitmap, dx, dy + y1, sbits, sx, sx + w-1);
416 bw.advance_src_bits();
417 }
418 }
419 #endif
420
421 // rescalling bitmaps, 10/14/99 Jan Bobrowski [email protected]
422
423 static void scale_line(const uint8_t *in, uint8_t *out, const uint_fast32_t ilen, const uint_fast32_t olen)
424 {
425 uint_fast32_t a = olen / ilen, b = olen % ilen, c = 0;
426 for (uint8_t *const end = out + olen; out != end;)
427 {
428 uint_fast32_t i = a;
429 c += b;
430 if(c >= ilen) {
431 c -= ilen;
432 ++i;
433 }
434 auto e = out + i;
435 std::fill(std::exchange(out, e), e, *in++);
436 }
437 }
438
439 static void gr_bitmap_scale_to(const grs_bitmap &src, grs_bitmap &dst)
440 {
441 auto s = src.get_bitmap_data();
442 auto d = dst.get_bitmap_data();
443 int h = src.bm_h;
444 int a = dst.bm_h/h, b = dst.bm_h%h;
445 int c{0}, i;
446
447 for (uint_fast32_t y = src.bm_h; y; --y) {
448 i = a;
449 c += b;
450 if(c >= h) {
451 c -= h;
452 goto inside;
453 }
454 while(--i>=0) {
455 inside:
456 scale_line(s, d, src.bm_w, dst.bm_w);
457 d += dst.bm_rowsize;
458 }
459 s += src.bm_rowsize;
460 }
461 }
462
463 }
464
465 void show_fullscr(grs_canvas &canvas, grs_bitmap &bm)
466 {
467 auto &scr = canvas.cv_bitmap;
468 #if DXX_USE_OGL
469 if (bm.get_type() == bm_mode::linear && scr.get_type() == bm_mode::ogl &&
470 bm.bm_w <= grd_curscreen->get_screen_width() && bm.bm_h <= grd_curscreen->get_screen_height()) // only scale with OGL if bitmap is not bigger than screen size
471 {
472 ogl_ubitmapm_cs(canvas, 0, 0, opengl_bitmap_use_dst_canvas, opengl_bitmap_use_dst_canvas, bm, ogl_colors::white);//use opengl to scale, faster and saves ram. -MPM
473 return;
474 }
475 #endif
476 if (scr.get_type() != bm_mode::linear)
477 {
478 grs_bitmap_ptr p = gr_create_bitmap(scr.bm_w, scr.bm_h);
479 auto &tmp = *p.get();
480 gr_bitmap_scale_to(bm, tmp);
481 gr_bitmap(canvas, 0, 0, tmp);
482 return;
483 }
484 gr_bitmap_scale_to(bm, scr);
485 }
486
487 // Find transparent area in bitmap
488 void gr_bitblt_find_transparent_area(const grs_bitmap &bm, unsigned &minx, unsigned &miny, unsigned &maxx, unsigned &maxy)
489 {
490 using std::advance;
491 using std::min;
492 using std::max;
493
494 if (!bm.get_flag_mask(BM_FLAG_TRANSPARENT))
495 return;
496
497 minx = bm.bm_w - 1;
498 maxx = 0;
499 miny = bm.bm_h - 1;
500 maxy = 0;
501
502 unsigned i{0}, count = 0;
503 auto check = [&](unsigned x, unsigned y, const color_palette_index c) {
504 if (c == TRANSPARENCY_COLOR) { // don't look for transparancy color here.
505 count++;
506 minx = min(x, minx);
507 miny = min(y, miny);
508 maxx = max(x, maxx);
509 maxy = max(y, maxy);
510 }
511 };
512 // decode the bitmap
513 const uint_fast32_t bm_h = bm.bm_h;
514 const uint_fast32_t bm_w = bm.bm_w;
515 if (bm.get_flag_mask(BM_FLAG_RLE))
516 {
517 bm_rle_expand expander(bm);
518 for (uint_fast32_t y = 0;; ++y)
519 {
520 std::array<uint8_t, 4096> buf;
521 if (expander.step(bm_rle_expand_range(buf)) != bm_rle_expand::again)
522 break;
523 range_for (const uint_fast32_t x, xrange(bm_w))
524 check(x, y, buf[x]);
525 }
526 }
527 else
528 {
529 range_for (const uint_fast32_t y, xrange(bm_h))
530 range_for (const uint_fast32_t x, xrange(bm_w))
531 check(x, y, bm.bm_data[i++]);
532 }
533 Assert (count);
534 }
535
536 }