| 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 |
* Protypes for rle functions. |
| 22 |
* |
| 23 |
*/ |
| 24 |
|
| 25 |
#pragma once |
| 26 |
|
| 27 |
#include "pstypes.h" |
| 28 |
#include "gr.h" |
| 29 |
|
| 30 |
#include <cstdint> |
| 31 |
#include "dxxsconf.h" |
| 32 |
#include "dsx-ns.h" |
| 33 |
#include "compiler-poison.h" |
| 34 |
#include <iterator> |
| 35 |
|
| 36 |
struct rle_position_t |
| 37 |
{ |
| 38 |
const uint8_t *src; |
| 39 |
uint8_t *dst; |
| 40 |
}; |
| 41 |
|
| 42 |
static inline const uint8_t *end(const grs_bitmap &b) |
| 43 |
{ |
| 44 |
return &b.bm_data[b.bm_h * b.bm_w]; |
| 45 |
} |
| 46 |
|
| 47 |
static inline uint8_t *end(grs_bitmap &b) |
| 48 |
{ |
| 49 |
return &b.get_bitmap_data()[b.bm_h * b.bm_w]; |
| 50 |
} |
| 51 |
|
| 52 |
static inline rle_position_t rle_end(const auto &src, auto &dst) |
| 53 |
{ |
| 54 |
return {end(src), end(dst)}; |
| 55 |
} |
| 56 |
|
| 57 |
namespace dcx { |
| 58 |
uint8_t *gr_rle_decode(const uint8_t *sb, uint8_t *db, rle_position_t e); |
| 59 |
void gr_bitmap_rle_compress(grs_bitmap &bmp); |
| 60 |
#if !DXX_USE_OGL |
| 61 |
void gr_rle_expand_scanline_masked(uint8_t *dest, const uint8_t *src, uint_fast32_t x1, uint_fast32_t x2); |
| 62 |
#endif |
| 63 |
void gr_rle_expand_scanline(uint8_t *dest, const uint8_t *src, uint_fast32_t x1, uint_fast32_t x2); |
| 64 |
grs_bitmap *_rle_expand_texture(const grs_bitmap &bmp); |
| 65 |
|
| 66 |
[[nodiscard]] |
| 67 |
static inline const grs_bitmap *rle_expand_texture(const grs_bitmap &bmp) |
| 68 |
{ |
| 69 |
if (bmp.get_flag_mask(BM_FLAG_RLE)) |
| 70 |
return _rle_expand_texture(bmp); |
| 71 |
return &bmp; |
| 72 |
} |
| 73 |
|
| 74 |
void rle_cache_close(); |
| 75 |
void rle_cache_flush(); |
| 76 |
void rle_swap_0_255(grs_bitmap &bmp); |
| 77 |
void rle_remap(grs_bitmap &bmp, const std::array<color_palette_index, 256> &colormap); |
| 78 |
#if !DXX_USE_OGL |
| 79 |
#define gr_rle_expand_scanline_generic(C,D,DX,DY,S,X1,X2) gr_rle_expand_scanline_generic(D,DX,DY,S,X1,X2) |
| 80 |
void gr_rle_expand_scanline_generic(grs_canvas &, grs_bitmap &dest, int dx, int dy, const ubyte *src, int x1, int x2 ); |
| 81 |
#endif |
| 82 |
|
| 83 |
class bm_rle_expand_range |
| 84 |
{ |
| 85 |
uint8_t *iter_dbits; |
| 86 |
uint8_t *const end_dbits; |
| 87 |
public: |
| 88 |
bm_rle_expand_range(uint8_t *const i, uint8_t *const e) : |
| 89 |
iter_dbits(i), end_dbits(e) |
| 90 |
{ |
| 91 |
} |
| 92 |
template <std::size_t N> |
| 93 |
bm_rle_expand_range(std::array<uint8_t, N> &a) : |
| 94 |
iter_dbits(a.data()), end_dbits(std::next(iter_dbits, N)) |
| 95 |
{ |
| 96 |
} |
| 97 |
uint8_t *get_begin_dbits() const |
| 98 |
{ |
| 99 |
return iter_dbits; |
| 100 |
} |
| 101 |
uint8_t *get_end_dbits() const |
| 102 |
{ |
| 103 |
return end_dbits; |
| 104 |
} |
| 105 |
void consume_dbits(const unsigned w) |
| 106 |
{ |
| 107 |
iter_dbits += w; |
| 108 |
} |
| 109 |
}; |
| 110 |
|
| 111 |
class bm_rle_src_stride |
| 112 |
{ |
| 113 |
/* Width of an individual element of the table of row lengths. This |
| 114 |
* is sizeof(uint8_t) if the bitmap is not RLE_BIG, or is |
| 115 |
* sizeof(uint16_t) otherwise. |
| 116 |
*/ |
| 117 |
const unsigned src_bit_stride_size; |
| 118 |
/* Bitmask used for filtering the table load. To minimize |
| 119 |
* branching, the code always loads two bytes from the table of row |
| 120 |
* lengths. If the bitmap is not RLE_BIG, then `src_bit_load_mask` |
| 121 |
* will be 0xff and will be used to mask out the high byte from the |
| 122 |
* load. Otherwise, `src_bit_load_mask` will be 0xffff and the mask |
| 123 |
* operation will leave the loaded value unchanged. |
| 124 |
*/ |
| 125 |
const unsigned src_bit_load_mask; |
| 126 |
protected: |
| 127 |
/* Pointer to the table of row lengths. The table is uint8_t[] if |
| 128 |
* the bitmap is not RLE_BIG, or is uint16_t[] otherwise. The table |
| 129 |
* is not required to be aligned. |
| 130 |
*/ |
| 131 |
const uint8_t *ptr_src_bit_lengths; |
| 132 |
/* Pointer to the table of RLE-encoded bitmap elements. |
| 133 |
*/ |
| 134 |
const uint8_t *src_bits; |
| 135 |
public: |
| 136 |
bm_rle_src_stride(const grs_bitmap &src, const unsigned rle_big) : |
| 137 |
/* Jump threading should collapse the separate ?: uses */ |
| 138 |
src_bit_stride_size{rle_big ? unsigned{sizeof(uint16_t)} : unsigned{sizeof(uint8_t)}}, |
| 139 |
src_bit_load_mask{rle_big ? 0xffffu : 0xffu}, |
| 140 |
ptr_src_bit_lengths{&src.bm_data[4]}, |
| 141 |
src_bits{&ptr_src_bit_lengths[rle_big ? src.bm_h * 2 : src.bm_h]} |
| 142 |
{ |
| 143 |
} |
| 144 |
void advance_src_bits(); |
| 145 |
}; |
| 146 |
|
| 147 |
class bm_rle_expand : bm_rle_src_stride |
| 148 |
{ |
| 149 |
/* Pointer to the first byte that is not part of the table of row |
| 150 |
* lengths. When `ptr_src_bit_lengths` == `end_src_bit_lengths`, no |
| 151 |
* further rows are available. |
| 152 |
*/ |
| 153 |
const uint8_t *const end_src_bit_lengths; |
| 154 |
/* Pointer to the first byte that is not part of the bitmap data. |
| 155 |
* When `end_src_bm` == `src_bits`, no further bitmap elements are |
| 156 |
* available. |
| 157 |
*/ |
| 158 |
const uint8_t *const end_src_bm; |
| 159 |
public: |
| 160 |
enum step_result |
| 161 |
{ |
| 162 |
/* A row decoded successfully. Act on the decoded buffer as |
| 163 |
* necessary, then call this class again. |
| 164 |
* |
| 165 |
* This result is returned even if the returned row is the last |
| 166 |
* row. The first call after the last row will return |
| 167 |
* src_exhausted, which is the trigger to stop calling the |
| 168 |
* class. |
| 169 |
*/ |
| 170 |
again, |
| 171 |
/* The source was exhausted before any work was done. No data |
| 172 |
* is available. No further calls should be made with this |
| 173 |
* source. |
| 174 |
*/ |
| 175 |
src_exhausted, |
| 176 |
/* The destination was exhausted before decoding completed. The |
| 177 |
* source is left at the beginning of the row which failed to |
| 178 |
* decode. The caller may try again with a larger destination |
| 179 |
* buffer or may abandon the attempt. Further calls with the |
| 180 |
* same source and destination will continue to return |
| 181 |
* `dst_exhausted`. |
| 182 |
*/ |
| 183 |
dst_exhausted, |
| 184 |
}; |
| 185 |
bm_rle_expand(const grs_bitmap &src) : |
| 186 |
bm_rle_src_stride{src, src.get_flag_mask(BM_FLAG_RLE_BIG)}, |
| 187 |
end_src_bit_lengths{src_bits}, |
| 188 |
end_src_bm{end(src)} |
| 189 |
{ |
| 190 |
} |
| 191 |
/* Decode one row of the bitmap, then return control to the |
| 192 |
* caller. If the return value is `again`, then the caller |
| 193 |
* should perform any per-row processing, then call step() |
| 194 |
* again. If the return value is not `again`, then the |
| 195 |
* destination buffer is undefined and the caller should not |
| 196 |
* access it or call step(). |
| 197 |
* |
| 198 |
* Use `const auto &` to ensure that t is only modified by the caller |
| 199 |
* and that the caller does not accidentally provide an |
| 200 |
* implementation of `get_begin_dbits` that moves the |
| 201 |
* destination pointer. |
| 202 |
*/ |
| 203 |
step_result step(const auto &t) |
| 204 |
{ |
| 205 |
/* Poison the memory first, so that it is undefined even if |
| 206 |
* the source is exhausted. |
| 207 |
*/ |
| 208 |
const auto b = t.get_begin_dbits(); |
| 209 |
const auto e = t.get_end_dbits(); |
| 210 |
DXX_MAKE_MEM_UNDEFINED(std::span(b, e)); |
| 211 |
/* Check for source exhaustion, so that empty bitmaps are |
| 212 |
* not read at all. This allows callers to treat |
| 213 |
* src_exhausted as a definitive end-of-record with no data |
| 214 |
* available. |
| 215 |
*/ |
| 216 |
if (ptr_src_bit_lengths == end_src_bit_lengths) |
| 217 |
return src_exhausted; |
| 218 |
return step_internal(b, e); |
| 219 |
} |
| 220 |
/* Decode until source or destination is exhausted. The |
| 221 |
* destination position is updated automatically as each row is |
| 222 |
* decoded. There is no opportunity for callers to perform |
| 223 |
* per-row processing. Callers should call step() directly if |
| 224 |
* per-row processing is required. |
| 225 |
* |
| 226 |
* Use `auto &&` since some callers may not care about the state of `t` |
| 227 |
* afterward; `auto &&` lets them pass an anonymous temporary. |
| 228 |
*/ |
| 229 |
bool loop(const unsigned bm_w, auto &&t) |
| 230 |
{ |
| 231 |
for (;;) |
| 232 |
{ |
| 233 |
switch (step(t)) |
| 234 |
{ |
| 235 |
case again: |
| 236 |
/* Step succeeded. Notify `t` to update its |
| 237 |
* dbits position, then loop around. |
| 238 |
*/ |
| 239 |
t.consume_dbits(bm_w); |
| 240 |
break; |
| 241 |
case src_exhausted: |
| 242 |
/* Success: source buffer exhausted and no error |
| 243 |
* conditions detected. |
| 244 |
*/ |
| 245 |
return true; |
| 246 |
case dst_exhausted: |
| 247 |
/* Failure: destination buffer too small to hold |
| 248 |
* expanded source. |
| 249 |
*/ |
| 250 |
return false; |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
private: |
| 255 |
step_result step_internal(uint8_t *begin_dbits, uint8_t *end_dbits); |
| 256 |
}; |
| 257 |
|
| 258 |
} |