2006-03-20 17:12:09 +00:00
|
|
|
/*
|
2014-06-01 17:55:23 +00:00
|
|
|
* Portions of this file are copyright Rebirth contributors and licensed as
|
|
|
|
* described in COPYING.txt.
|
|
|
|
* Portions of this file are copyright Parallax Software and licensed
|
|
|
|
* according to the Parallax license below.
|
|
|
|
* See COPYING.txt for license details.
|
|
|
|
|
2006-03-20 17:12:09 +00:00
|
|
|
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
|
|
|
|
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
|
|
|
|
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
|
|
|
|
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
|
|
|
|
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
|
|
|
|
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
|
|
|
|
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
|
|
|
|
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
|
|
|
|
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
|
|
|
|
COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Graphical routines for manipulating grs_bitmaps.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-11-30 22:09:21 +00:00
|
|
|
#include <algorithm>
|
2014-11-12 03:43:44 +00:00
|
|
|
#include <stdexcept>
|
2006-03-20 17:12:09 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "u_mem.h"
|
|
|
|
#include "gr.h"
|
2012-07-07 18:35:06 +00:00
|
|
|
#include "dxxerror.h"
|
2016-09-24 18:06:11 +00:00
|
|
|
#if DXX_USE_OGL
|
2006-03-20 17:12:09 +00:00
|
|
|
#include "ogl_init.h"
|
|
|
|
#endif
|
2012-11-02 17:37:54 +00:00
|
|
|
#include "bitmap.h"
|
2020-05-02 21:18:43 +00:00
|
|
|
#include <memory>
|
2015-01-17 18:31:40 +00:00
|
|
|
|
2015-12-13 18:00:49 +00:00
|
|
|
namespace dcx {
|
2022-07-09 13:39:29 +00:00
|
|
|
namespace {
|
2015-12-05 22:57:23 +00:00
|
|
|
|
2014-11-15 03:31:41 +00:00
|
|
|
// Allocated a bitmap and makes its data be raw_data that is already somewhere.
|
2019-02-02 18:36:39 +00:00
|
|
|
static grs_bitmap_ptr gr_create_bitmap_raw(uint16_t w, uint16_t h, RAIIdmem<uint8_t[]> raw_data);
|
2014-11-15 03:31:41 +00:00
|
|
|
|
2022-07-09 13:39:29 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 04:02:28 +00:00
|
|
|
void gr_set_bitmap_data(grs_bitmap &bm, const uint8_t *data)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2016-09-24 18:06:11 +00:00
|
|
|
#if DXX_USE_OGL
|
2014-11-30 22:09:20 +00:00
|
|
|
ogl_freebmtexture(bm);
|
2006-03-20 17:12:09 +00:00
|
|
|
#endif
|
2014-11-30 22:09:20 +00:00
|
|
|
bm.bm_data = data;
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
2014-11-15 03:31:41 +00:00
|
|
|
grs_bitmap_ptr gr_create_bitmap(uint16_t w, uint16_t h )
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2019-02-02 18:36:39 +00:00
|
|
|
RAIIdmem<uint8_t[]> d;
|
2021-06-28 03:37:48 +00:00
|
|
|
MALLOC(d, uint8_t[], MAX_BMP_SIZE(w, h));
|
2019-02-02 18:36:39 +00:00
|
|
|
return gr_create_bitmap_raw(w, h, std::move(d));
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
2022-07-09 13:39:29 +00:00
|
|
|
namespace {
|
|
|
|
|
2019-02-02 18:36:39 +00:00
|
|
|
grs_bitmap_ptr gr_create_bitmap_raw(const uint16_t w, const uint16_t h, RAIIdmem<uint8_t[]> raw_data)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2020-05-02 21:18:42 +00:00
|
|
|
auto n = std::make_unique<grs_main_bitmap>();
|
2019-02-02 18:36:39 +00:00
|
|
|
gr_init_main_bitmap(*n.get(), bm_mode::linear, 0, 0, w, h, w, std::move(raw_data));
|
2012-11-18 18:21:50 +00:00
|
|
|
return n;
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
2022-07-09 13:39:29 +00:00
|
|
|
}
|
|
|
|
|
2019-02-02 18:36:39 +00:00
|
|
|
// TODO: virtualize
|
2021-06-12 21:07:32 +00:00
|
|
|
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
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2014-11-30 22:09:20 +00:00
|
|
|
bm.bm_x = x;
|
|
|
|
bm.bm_y = y;
|
|
|
|
bm.bm_w = w;
|
|
|
|
bm.bm_h = h;
|
2017-01-15 00:03:13 +00:00
|
|
|
bm.set_flags(0);
|
2015-07-25 23:10:47 +00:00
|
|
|
bm.set_type(mode);
|
2014-11-30 22:09:20 +00:00
|
|
|
bm.bm_rowsize = bytesperline;
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2021-06-12 21:07:32 +00:00
|
|
|
bm.bm_mdata = mdata;
|
2016-09-24 18:06:11 +00:00
|
|
|
#if DXX_USE_OGL
|
2014-11-30 22:09:20 +00:00
|
|
|
bm.bm_parent = nullptr;
|
|
|
|
bm.gltexture = nullptr;
|
2006-03-20 17:12:09 +00:00
|
|
|
#endif
|
2019-02-02 18:36:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2019-07-07 22:00:02 +00:00
|
|
|
bm.reset();
|
2019-02-02 18:36:39 +00:00
|
|
|
gr_init_bitmap(bm, mode, x, y, w, h, bytesperline, data.get());
|
|
|
|
data.release();
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
2016-10-29 23:16:15 +00:00
|
|
|
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)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2019-02-02 18:36:39 +00:00
|
|
|
RAIIdmem<uint8_t[]> d;
|
2021-06-28 03:37:48 +00:00
|
|
|
MALLOC(d, uint8_t[], MAX_BMP_SIZE(w, h));
|
2019-02-02 18:36:39 +00:00
|
|
|
gr_init_main_bitmap(bm, mode, x, y, w, h, bytesperline, std::move(d));
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
2017-02-11 21:42:31 +00:00
|
|
|
grs_main_bitmap::grs_main_bitmap()
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2018-07-14 17:23:15 +00:00
|
|
|
static_cast<grs_bitmap &>(*this) = {};
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
2014-11-30 22:09:21 +00:00
|
|
|
grs_subbitmap_ptr gr_create_sub_bitmap(grs_bitmap &bm, uint16_t x, uint16_t y, uint16_t w, uint16_t h)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2020-05-02 21:18:42 +00:00
|
|
|
auto n = std::make_unique<grs_subbitmap>();
|
2014-11-30 22:09:21 +00:00
|
|
|
gr_init_sub_bitmap(*n.get(), bm, x, y, w, h);
|
2012-11-18 18:21:50 +00:00
|
|
|
return n;
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
2014-11-30 22:09:20 +00:00
|
|
|
void gr_free_bitmap_data (grs_bitmap &bm) // TODO: virtulize
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2017-02-08 23:34:41 +00:00
|
|
|
if (auto &d = bm.bm_mdata)
|
|
|
|
d_free(d);
|
2016-09-24 18:06:11 +00:00
|
|
|
#if DXX_USE_OGL
|
2014-11-30 22:09:20 +00:00
|
|
|
ogl_freebmtexture(bm);
|
2006-03-20 17:12:09 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-11-30 22:09:21 +00:00
|
|
|
void gr_init_sub_bitmap (grs_bitmap &bm, grs_bitmap &bmParent, uint16_t x, uint16_t y, uint16_t w, uint16_t h ) // TODO: virtualize
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2014-11-30 22:09:21 +00:00
|
|
|
uint32_t subx = x + bmParent.bm_x;
|
|
|
|
uint32_t suby = y + bmParent.bm_y;
|
|
|
|
if (subx != (bm.bm_x = static_cast<uint16_t>(subx)) ||
|
|
|
|
suby != (bm.bm_y = static_cast<uint16_t>(suby)))
|
2014-11-12 03:43:44 +00:00
|
|
|
throw std::overflow_error("offset overflow");
|
2021-02-25 03:53:49 +00:00
|
|
|
if (x > bmParent.bm_w ||
|
|
|
|
y > bmParent.bm_h)
|
|
|
|
throw std::overflow_error("offset beyond parent dimensions");
|
2021-12-01 03:08:27 +00:00
|
|
|
bm.bm_w = std::min<uint16_t>(w, bmParent.bm_w - x);
|
|
|
|
bm.bm_h = std::min<uint16_t>(h, bmParent.bm_h - y);
|
2017-01-15 00:03:13 +00:00
|
|
|
bm.set_flags(bmParent.get_flags());
|
2015-07-25 23:10:47 +00:00
|
|
|
bm.set_type(bmParent.get_type());
|
2014-11-30 22:09:21 +00:00
|
|
|
bm.bm_rowsize = bmParent.bm_rowsize;
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2016-09-24 18:06:11 +00:00
|
|
|
#if DXX_USE_OGL
|
2014-11-30 22:09:21 +00:00
|
|
|
bm.gltexture = bmParent.gltexture;
|
2006-03-20 17:12:09 +00:00
|
|
|
#endif
|
2014-11-30 22:09:21 +00:00
|
|
|
bm.bm_parent = &bmParent;
|
2016-07-06 01:54:24 +00:00
|
|
|
bm.bm_data = &bmParent.bm_data[static_cast<uint32_t>((y*bmParent.bm_rowsize)+x)];
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
2022-09-24 17:47:52 +00:00
|
|
|
void decode_data(const std::span<color_palette_index> data, const std::array<color_palette_index, 256> &colormap, std::bitset<256> &used)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2014-11-30 22:09:21 +00:00
|
|
|
const auto a = [&](uint8_t mapped) {
|
2018-03-08 04:21:18 +00:00
|
|
|
return used[mapped] = true, colormap[mapped];
|
2014-11-30 22:09:21 +00:00
|
|
|
};
|
2022-09-24 17:47:52 +00:00
|
|
|
std::transform(data.begin(), data.end(), data.begin(), a);
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
2022-07-09 13:39:29 +00:00
|
|
|
namespace {
|
|
|
|
|
2014-11-30 22:09:21 +00:00
|
|
|
static void gr_set_super_transparent(grs_bitmap &bm, bool bOpaque)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2017-01-15 00:03:13 +00:00
|
|
|
bm.set_flag_mask(!bOpaque, BM_FLAG_SUPER_TRANSPARENT);
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
2022-07-09 13:39:29 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 02:31:04 +00:00
|
|
|
void build_colormap_good(const palette_array_t &palette, std::array<color_palette_index, 256> &colormap)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2015-12-04 03:36:32 +00:00
|
|
|
const auto a = [](const rgb_t &p) {
|
|
|
|
return gr_find_closest_color(p.r, p.g, p.b);
|
|
|
|
};
|
|
|
|
std::transform(palette.begin(), palette.end(), colormap.begin(), a);
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
2015-01-25 05:32:45 +00:00
|
|
|
void gr_remap_bitmap_good(grs_bitmap &bmp, palette_array_t &palette, uint_fast32_t transparent_color, uint_fast32_t super_transparent_color)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2020-07-16 02:31:04 +00:00
|
|
|
std::array<color_palette_index, 256> colormap;
|
2018-03-08 04:21:18 +00:00
|
|
|
build_colormap_good(palette, colormap);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2015-01-25 05:32:45 +00:00
|
|
|
if (super_transparent_color < colormap.size())
|
2020-07-16 02:31:04 +00:00
|
|
|
colormap[super_transparent_color] = color_palette_index{254};
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2015-01-25 05:32:45 +00:00
|
|
|
if (transparent_color < colormap.size())
|
2006-03-20 17:12:09 +00:00
|
|
|
colormap[transparent_color] = TRANSPARENCY_COLOR;
|
|
|
|
|
2020-07-16 02:31:04 +00:00
|
|
|
std::bitset<256> freq{};
|
2015-01-25 05:32:45 +00:00
|
|
|
if (bmp.bm_w == bmp.bm_rowsize)
|
2022-09-24 17:47:52 +00:00
|
|
|
decode_data(std::span(bmp.get_bitmap_data(), bmp.bm_w * bmp.bm_h), colormap, freq);
|
2006-03-20 17:12:09 +00:00
|
|
|
else {
|
2015-01-25 05:32:45 +00:00
|
|
|
auto p = bmp.get_bitmap_data();
|
2015-01-25 05:32:45 +00:00
|
|
|
for (uint_fast32_t y = bmp.bm_h; y--; p += bmp.bm_rowsize)
|
2022-09-24 17:47:52 +00:00
|
|
|
decode_data(std::span{p, bmp.bm_w}, colormap, freq);
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
2015-01-25 05:32:45 +00:00
|
|
|
if (transparent_color < freq.size() && freq[transparent_color])
|
2015-01-25 05:32:45 +00:00
|
|
|
gr_set_transparent(bmp, 1);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2015-01-25 05:32:45 +00:00
|
|
|
if (super_transparent_color < freq.size() && freq[super_transparent_color])
|
2015-01-25 05:32:45 +00:00
|
|
|
gr_set_super_transparent(bmp, 1);
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
2015-12-05 22:57:23 +00:00
|
|
|
|
|
|
|
}
|