+314
-342
@@ -75,7 +75,7 @@ std::span<T> read(std::span<std::byte> & buffer, size_t count)
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::byte> load_svg(const std::filesystem::path & filename, int size)
|
std::vector<wivrn::icon> try_load_svg(const std::filesystem::path & filename, int size)
|
||||||
{
|
{
|
||||||
// TODO check if aspect ratio needs to be handled, dpi
|
// TODO check if aspect ratio needs to be handled, dpi
|
||||||
|
|
||||||
@@ -111,6 +111,8 @@ std::vector<std::byte> load_svg(const std::filesystem::path & filename, int size
|
|||||||
GFile_ptr file{g_file_new_for_path(filename.c_str())};
|
GFile_ptr file{g_file_new_for_path(filename.c_str())};
|
||||||
|
|
||||||
RsvgHandle_ptr handle{rsvg_handle_new_from_gfile_sync(file.get(), RSVG_HANDLE_FLAGS_NONE, nullptr, &error)};
|
RsvgHandle_ptr handle{rsvg_handle_new_from_gfile_sync(file.get(), RSVG_HANDLE_FLAGS_NONE, nullptr, &error)};
|
||||||
|
if (!handle)
|
||||||
|
throw std::runtime_error{std::string{"Could not open: "} + error->message};
|
||||||
|
|
||||||
// Create a Cairo image surface and a rendering context for it
|
// Create a Cairo image surface and a rendering context for it
|
||||||
cairo_surface_t_ptr surface{cairo_image_surface_create(CAIRO_FORMAT_ARGB32, size, size)};
|
cairo_surface_t_ptr surface{cairo_image_surface_create(CAIRO_FORMAT_ARGB32, size, size)};
|
||||||
@@ -145,58 +147,49 @@ std::vector<std::byte> load_svg(const std::filesystem::path & filename, int size
|
|||||||
if (auto status = cairo_surface_write_to_png_stream(surface.get(), cb, &png); status != CAIRO_STATUS_SUCCESS)
|
if (auto status = cairo_surface_write_to_png_stream(surface.get(), cb, &png); status != CAIRO_STATUS_SUCCESS)
|
||||||
throw std::runtime_error{std::string{"Could not write output: "} + cairo_status_to_string(status)};
|
throw std::runtime_error{std::string{"Could not write output: "} + cairo_status_to_string(status)};
|
||||||
|
|
||||||
return png;
|
return {{size, size, 32, std::move(png)}};
|
||||||
}
|
}
|
||||||
|
|
||||||
// See https://gitlab.gnome.org/GNOME/gimp/-/tree/master/plug-ins/file-ico?ref_type=heads
|
std::vector<wivrn::icon> try_load_ico(const std::filesystem::path & filename)
|
||||||
|
|
||||||
struct ico_file_entry
|
|
||||||
{
|
|
||||||
int width; // Width of icon in pixels
|
|
||||||
int height; // Height of icon in pixels
|
|
||||||
int bpp; // 1, 4, 8, 16, 24 or 32 bits per pixel
|
|
||||||
std::span<std::byte> data;
|
|
||||||
};
|
|
||||||
|
|
||||||
constexpr int rowstride(int width, int bpp)
|
|
||||||
{
|
|
||||||
int words_per_line = (width * bpp + 31) / 32;
|
|
||||||
return words_per_line * 4;
|
|
||||||
}
|
|
||||||
static_assert(rowstride(1, 1) == 4);
|
|
||||||
static_assert(rowstride(8, 1) == 4);
|
|
||||||
static_assert(rowstride(16, 1) == 4);
|
|
||||||
static_assert(rowstride(32, 1) == 4);
|
|
||||||
static_assert(rowstride(33, 1) == 8);
|
|
||||||
|
|
||||||
static_assert(rowstride(1, 4) == 4);
|
|
||||||
static_assert(rowstride(8, 4) == 4);
|
|
||||||
static_assert(rowstride(16, 4) == 8);
|
|
||||||
static_assert(rowstride(32, 4) == 16);
|
|
||||||
static_assert(rowstride(33, 4) == 20);
|
|
||||||
|
|
||||||
static_assert(rowstride(1, 8) == 4);
|
|
||||||
static_assert(rowstride(8, 8) == 8);
|
|
||||||
static_assert(rowstride(16, 8) == 16);
|
|
||||||
static_assert(rowstride(32, 8) == 32);
|
|
||||||
static_assert(rowstride(33, 8) == 36);
|
|
||||||
|
|
||||||
static_assert(rowstride(1, 24) == 4);
|
|
||||||
static_assert(rowstride(8, 24) == 24);
|
|
||||||
static_assert(rowstride(16, 24) == 48);
|
|
||||||
static_assert(rowstride(32, 24) == 96);
|
|
||||||
static_assert(rowstride(33, 24) == 100);
|
|
||||||
|
|
||||||
static_assert(rowstride(1, 32) == 4);
|
|
||||||
static_assert(rowstride(8, 32) == 32);
|
|
||||||
static_assert(rowstride(16, 32) == 64);
|
|
||||||
static_assert(rowstride(32, 32) == 128);
|
|
||||||
static_assert(rowstride(33, 32) == 132);
|
|
||||||
|
|
||||||
std::vector<std::byte> load_ico(const std::filesystem::path & filename, int size, int index = -1)
|
|
||||||
{
|
{
|
||||||
const uint32_t ICO_PNG_MAGIC = 0x474e5089;
|
const uint32_t ICO_PNG_MAGIC = 0x474e5089;
|
||||||
|
|
||||||
|
// See https://gitlab.gnome.org/GNOME/gimp/-/tree/master/plug-ins/file-ico?ref_type=heads
|
||||||
|
auto rowstride = [](int width, int bpp) {
|
||||||
|
int words_per_line = (width * bpp + 31) / 32;
|
||||||
|
return words_per_line * 4;
|
||||||
|
};
|
||||||
|
|
||||||
|
static_assert(rowstride(1, 1) == 4);
|
||||||
|
static_assert(rowstride(8, 1) == 4);
|
||||||
|
static_assert(rowstride(16, 1) == 4);
|
||||||
|
static_assert(rowstride(32, 1) == 4);
|
||||||
|
static_assert(rowstride(33, 1) == 8);
|
||||||
|
|
||||||
|
static_assert(rowstride(1, 4) == 4);
|
||||||
|
static_assert(rowstride(8, 4) == 4);
|
||||||
|
static_assert(rowstride(16, 4) == 8);
|
||||||
|
static_assert(rowstride(32, 4) == 16);
|
||||||
|
static_assert(rowstride(33, 4) == 20);
|
||||||
|
|
||||||
|
static_assert(rowstride(1, 8) == 4);
|
||||||
|
static_assert(rowstride(8, 8) == 8);
|
||||||
|
static_assert(rowstride(16, 8) == 16);
|
||||||
|
static_assert(rowstride(32, 8) == 32);
|
||||||
|
static_assert(rowstride(33, 8) == 36);
|
||||||
|
|
||||||
|
static_assert(rowstride(1, 24) == 4);
|
||||||
|
static_assert(rowstride(8, 24) == 24);
|
||||||
|
static_assert(rowstride(16, 24) == 48);
|
||||||
|
static_assert(rowstride(32, 24) == 96);
|
||||||
|
static_assert(rowstride(33, 24) == 100);
|
||||||
|
|
||||||
|
static_assert(rowstride(1, 32) == 4);
|
||||||
|
static_assert(rowstride(8, 32) == 32);
|
||||||
|
static_assert(rowstride(16, 32) == 64);
|
||||||
|
static_assert(rowstride(32, 32) == 128);
|
||||||
|
static_assert(rowstride(33, 32) == 132);
|
||||||
|
|
||||||
auto ico = load_file(filename);
|
auto ico = load_file(filename);
|
||||||
|
|
||||||
std::span header{ico};
|
std::span header{ico};
|
||||||
@@ -208,338 +201,287 @@ std::vector<std::byte> load_ico(const std::filesystem::path & filename, int size
|
|||||||
if (reserved != 0 or resource_type != 1)
|
if (reserved != 0 or resource_type != 1)
|
||||||
throw std::runtime_error{"Cannot read icon: " + filename.native()};
|
throw std::runtime_error{"Cannot read icon: " + filename.native()};
|
||||||
|
|
||||||
std::vector<ico_file_entry> entries;
|
std::vector<wivrn::icon> icons;
|
||||||
int max_bpp = 0;
|
|
||||||
|
|
||||||
for (int i = 0; i < icon_count; i++)
|
for (int i = 0; i < icon_count; i++)
|
||||||
{
|
{
|
||||||
ico_file_entry entry;
|
auto width = read<uint8_t>(header);
|
||||||
entry.width = read<uint8_t>(header);
|
auto height = read<uint8_t>(header);
|
||||||
entry.height = read<uint8_t>(header);
|
|
||||||
auto num_colors = read<uint8_t>(header);
|
auto num_colors = read<uint8_t>(header);
|
||||||
auto reserved = read<uint8_t>(header); // reserved
|
auto reserved = read<uint8_t>(header); // reserved
|
||||||
auto planes = read<uint16_t>(header);
|
auto planes = read<uint16_t>(header);
|
||||||
entry.bpp = read<uint16_t>(header);
|
auto bpp = read<uint16_t>(header);
|
||||||
auto size = read<uint32_t>(header);
|
auto size = read<uint32_t>(header);
|
||||||
auto offset = read<uint32_t>(header);
|
auto offset = read<uint32_t>(header);
|
||||||
|
|
||||||
if (offset + size > ico.size())
|
if (offset + size > ico.size())
|
||||||
throw std::runtime_error{"Cannot read icon: " + filename.native()};
|
throw std::runtime_error{"Cannot read icon: " + filename.native()};
|
||||||
|
|
||||||
entry.data = std::span{ico}.subspan(offset, size);
|
auto data = std::span{ico}.subspan(offset, size);
|
||||||
if (entry.width == 0)
|
|
||||||
entry.width = 256;
|
|
||||||
if (entry.height == 0)
|
|
||||||
entry.height = 256;
|
|
||||||
if (reserved != 0)
|
if (reserved != 0)
|
||||||
throw std::runtime_error{"Cannot read icon: " + filename.native()};
|
throw std::runtime_error{"Cannot read icon: " + filename.native()};
|
||||||
if (planes != 0 and planes != 1)
|
if (planes != 0 and planes != 1)
|
||||||
throw std::runtime_error{"Cannot read icon: " + filename.native()};
|
throw std::runtime_error{"Cannot read icon: " + filename.native()};
|
||||||
|
|
||||||
entries.push_back(entry);
|
auto magic = read<uint32_t>(data);
|
||||||
|
|
||||||
max_bpp = std::max(max_bpp, entry.bpp);
|
if (magic == ICO_PNG_MAGIC)
|
||||||
}
|
|
||||||
|
|
||||||
const ico_file_entry * best_entry = index >= 0 ? &entries.at(index) : [&]() {
|
|
||||||
const ico_file_entry * best_entry = nullptr;
|
|
||||||
|
|
||||||
// Look for the smallest multiple of the requested size
|
|
||||||
int criterion = std::numeric_limits<int>::max();
|
|
||||||
|
|
||||||
for (const auto & entry: entries)
|
|
||||||
{
|
{
|
||||||
if (entry.bpp != max_bpp)
|
// PNG file
|
||||||
continue;
|
std::vector<std::byte> png_data;
|
||||||
|
|
||||||
if (entry.width % size != 0 or entry.height % size != 0)
|
// The PNG magic has already been consumed, put it back
|
||||||
continue;
|
png_data.insert(png_data.end(), data.begin() - 4, data.end());
|
||||||
|
|
||||||
if (std::min(entry.width, entry.height) / size < criterion)
|
// Check if the PNG is valid
|
||||||
|
std::vector<uint8_t> pixels;
|
||||||
|
png_image png_ref{
|
||||||
|
.version = PNG_IMAGE_VERSION,
|
||||||
|
.format = PNG_FORMAT_BGRA,
|
||||||
|
};
|
||||||
|
if (not png_image_begin_read_from_memory(&png_ref, png_data.data(), png_data.size()))
|
||||||
|
continue;
|
||||||
|
// throw std::runtime_error{"Cannot read icon: " + filename.native()};
|
||||||
|
|
||||||
|
pixels.resize(PNG_IMAGE_SIZE(png_ref));
|
||||||
|
|
||||||
|
if (!png_image_finish_read(&png_ref, nullptr, pixels.data(), 0, nullptr))
|
||||||
{
|
{
|
||||||
criterion = std::min(entry.width, entry.height);
|
png_image_free(&png_ref);
|
||||||
best_entry = &entry;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (best_entry)
|
|
||||||
return best_entry;
|
|
||||||
|
|
||||||
// No multiple found: pick the largest icon
|
|
||||||
criterion = 0;
|
|
||||||
|
|
||||||
for (const auto & entry: entries)
|
|
||||||
{
|
|
||||||
if (entry.bpp != max_bpp)
|
|
||||||
continue;
|
continue;
|
||||||
|
// throw std::runtime_error{"Cannot read icon: " + filename.native()};
|
||||||
if (entry.width * entry.height > criterion)
|
|
||||||
{
|
|
||||||
criterion = entry.width * entry.height;
|
|
||||||
best_entry = &entry;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return best_entry;
|
|
||||||
}();
|
|
||||||
|
|
||||||
if (not best_entry)
|
// TODO: get bit depth from PNG instead of trusting the ico header
|
||||||
return {};
|
icons.emplace_back(png_ref.width, png_ref.height, bpp, std::move(png_data));
|
||||||
|
|
||||||
auto data = best_entry->data;
|
|
||||||
auto magic = read<uint32_t>(data);
|
|
||||||
|
|
||||||
if (magic == ICO_PNG_MAGIC)
|
|
||||||
{
|
|
||||||
// PNG file
|
|
||||||
std::vector<std::byte> png_data;
|
|
||||||
|
|
||||||
// The PNG magic has already been consumed, put it back
|
|
||||||
png_data.insert(png_data.end(), data.begin() - 4, data.end());
|
|
||||||
|
|
||||||
// Check if the PNG is valid
|
|
||||||
std::vector<uint8_t> pixels;
|
|
||||||
png_image png_ref{
|
|
||||||
.version = PNG_IMAGE_VERSION,
|
|
||||||
.format = PNG_FORMAT_BGRA,
|
|
||||||
};
|
|
||||||
if (not png_image_begin_read_from_memory(&png_ref, png_data.data(), png_data.size()))
|
|
||||||
throw std::runtime_error{"Cannot read icon: " + filename.native()};
|
|
||||||
|
|
||||||
pixels.resize(PNG_IMAGE_SIZE(png_ref));
|
|
||||||
|
|
||||||
if (!png_image_finish_read(&png_ref, nullptr, pixels.data(), 0, nullptr))
|
|
||||||
{
|
|
||||||
png_image_free(&png_ref);
|
png_image_free(&png_ref);
|
||||||
throw std::runtime_error{"Cannot read icon: " + filename.native()};
|
|
||||||
}
|
}
|
||||||
png_image_free(&png_ref);
|
else if (magic == 40)
|
||||||
|
|
||||||
return png_data;
|
|
||||||
}
|
|
||||||
else if (magic == 40)
|
|
||||||
{
|
|
||||||
auto width = read<uint32_t>(data); // Width of image in pixels
|
|
||||||
auto height = read<uint32_t>(data); // Height of image in pixels
|
|
||||||
auto planes = read<uint16_t>(data); // Must be 1
|
|
||||||
auto bpp = read<uint16_t>(data); // 1, 4, 8, 16, 24, 32
|
|
||||||
auto compression = read<uint32_t>(data); // Must be 0 for icons
|
|
||||||
auto image_size = read<uint32_t>(data); // Size of image (without this header)
|
|
||||||
auto x_res = read<uint32_t>(data);
|
|
||||||
auto y_res = read<uint32_t>(data);
|
|
||||||
auto used_colors = read<uint32_t>(data);
|
|
||||||
auto important_colors = read<uint32_t>(data);
|
|
||||||
|
|
||||||
if (planes != 1 or compression != 0)
|
|
||||||
return {};
|
|
||||||
|
|
||||||
if (bpp != 1 and
|
|
||||||
bpp != 4 and
|
|
||||||
bpp != 8 and
|
|
||||||
bpp != 16 and
|
|
||||||
bpp != 24 and
|
|
||||||
bpp != 32)
|
|
||||||
return {};
|
|
||||||
|
|
||||||
auto w = width;
|
|
||||||
auto h = height / 2;
|
|
||||||
|
|
||||||
std::span<uint8_t> palette; // BGRX
|
|
||||||
|
|
||||||
if (bpp <= 16)
|
|
||||||
{
|
{
|
||||||
// Load the palette
|
auto width = read<uint32_t>(data); // Width of image in pixels
|
||||||
if (used_colors == 0)
|
auto height = read<uint32_t>(data); // Height of image in pixels
|
||||||
used_colors = 1 << bpp;
|
auto planes = read<uint16_t>(data); // Must be 1
|
||||||
|
auto bpp = read<uint16_t>(data); // 1, 4, 8, 16, 24, 32
|
||||||
|
auto compression = read<uint32_t>(data); // Must be 0 for icons
|
||||||
|
auto image_size = read<uint32_t>(data); // Size of image (without this header)
|
||||||
|
auto x_res = read<uint32_t>(data);
|
||||||
|
auto y_res = read<uint32_t>(data);
|
||||||
|
auto used_colors = read<uint32_t>(data);
|
||||||
|
auto important_colors = read<uint32_t>(data);
|
||||||
|
|
||||||
palette = read<uint8_t>(data, used_colors * 4);
|
if (planes != 1 or compression != 0)
|
||||||
}
|
return {};
|
||||||
|
|
||||||
size_t xor_stride = rowstride(w, bpp);
|
if (bpp != 1 and
|
||||||
size_t and_stride = rowstride(w, 1);
|
bpp != 4 and
|
||||||
size_t dst_stride = 4 * w;
|
bpp != 8 and
|
||||||
|
bpp != 16 and
|
||||||
|
bpp != 24 and
|
||||||
|
bpp != 32)
|
||||||
|
return {};
|
||||||
|
|
||||||
std::span<uint8_t> xor_map = read<uint8_t>(data, xor_stride * h);
|
auto w = width;
|
||||||
std::span<uint8_t> and_map = read<uint8_t>(data, and_stride * h);
|
auto h = height / 2;
|
||||||
|
|
||||||
std::vector<uint8_t> dest_buffer;
|
std::span<uint8_t> palette; // BGRX
|
||||||
dest_buffer.resize(h * dst_stride);
|
|
||||||
|
|
||||||
switch (bpp)
|
if (bpp <= 16)
|
||||||
{
|
{
|
||||||
case 1:
|
// Load the palette
|
||||||
for (int y = 0; y < h; y++)
|
if (used_colors == 0)
|
||||||
{
|
used_colors = 1 << bpp;
|
||||||
uint8_t * src_xor_row = xor_map.data() + xor_stride * y;
|
|
||||||
uint8_t * src_and_row = and_map.data() + and_stride * y;
|
|
||||||
uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y);
|
|
||||||
|
|
||||||
for (int x = 0, i = 0; x < w; x++)
|
palette = read<uint8_t>(data, used_colors * 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t xor_stride = rowstride(w, bpp);
|
||||||
|
size_t and_stride = rowstride(w, 1);
|
||||||
|
size_t dst_stride = 4 * w;
|
||||||
|
|
||||||
|
std::span<uint8_t> xor_map = read<uint8_t>(data, xor_stride * h);
|
||||||
|
std::span<uint8_t> and_map = read<uint8_t>(data, and_stride * h);
|
||||||
|
|
||||||
|
std::vector<uint8_t> dest_buffer;
|
||||||
|
dest_buffer.resize(h * dst_stride);
|
||||||
|
|
||||||
|
switch (bpp)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
for (int y = 0; y < h; y++)
|
||||||
{
|
{
|
||||||
int colour = (src_xor_row[x / 8] >> (7 - (x % 8))) & 0x1;
|
uint8_t * src_xor_row = xor_map.data() + xor_stride * y;
|
||||||
if (colour >= used_colors)
|
uint8_t * src_and_row = and_map.data() + and_stride * y;
|
||||||
return {};
|
uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y);
|
||||||
|
|
||||||
uint8_t b = palette[4 * colour + 0]; // Blue
|
for (int x = 0, i = 0; x < w; x++)
|
||||||
uint8_t g = palette[4 * colour + 1]; // Green
|
{
|
||||||
uint8_t r = palette[4 * colour + 2]; // Red
|
int colour = (src_xor_row[x / 8] >> (7 - (x % 8))) & 0x1;
|
||||||
|
if (colour >= used_colors)
|
||||||
|
return {};
|
||||||
|
|
||||||
// Get alpha from AND mask (0: opaque => 0xff, 1: transparent => 0)
|
uint8_t b = palette[4 * colour + 0]; // Blue
|
||||||
uint8_t a = src_and_row[x / 8] & (1 << (7 - x % 8)) ? 0 : 0xff;
|
uint8_t g = palette[4 * colour + 1]; // Green
|
||||||
|
uint8_t r = palette[4 * colour + 2]; // Red
|
||||||
|
|
||||||
*dst_row++ = b;
|
// Get alpha from AND mask (0: opaque => 0xff, 1: transparent => 0)
|
||||||
*dst_row++ = g;
|
uint8_t a = src_and_row[x / 8] & (1 << (7 - x % 8)) ? 0 : 0xff;
|
||||||
*dst_row++ = r;
|
|
||||||
*dst_row++ = a;
|
*dst_row++ = b;
|
||||||
|
*dst_row++ = g;
|
||||||
|
*dst_row++ = r;
|
||||||
|
*dst_row++ = a;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
break;
|
||||||
break;
|
|
||||||
|
|
||||||
case 4:
|
case 4:
|
||||||
for (int y = 0; y < h; y++)
|
for (int y = 0; y < h; y++)
|
||||||
{
|
|
||||||
uint8_t * src_xor_row = xor_map.data() + xor_stride * y;
|
|
||||||
uint8_t * src_and_row = and_map.data() + and_stride * y;
|
|
||||||
uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y);
|
|
||||||
|
|
||||||
for (int x = 0, i = 0; x < w; x++)
|
|
||||||
{
|
{
|
||||||
int colour = (src_xor_row[x / 2] >> (4 - 4 * (x % 2))) & 0xf;
|
uint8_t * src_xor_row = xor_map.data() + xor_stride * y;
|
||||||
if (colour >= used_colors)
|
uint8_t * src_and_row = and_map.data() + and_stride * y;
|
||||||
return {};
|
uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y);
|
||||||
|
|
||||||
uint8_t b = palette[4 * colour + 0]; // Blue
|
for (int x = 0, i = 0; x < w; x++)
|
||||||
uint8_t g = palette[4 * colour + 1]; // Green
|
{
|
||||||
uint8_t r = palette[4 * colour + 2]; // Red
|
int colour = (src_xor_row[x / 2] >> (4 - 4 * (x % 2))) & 0xf;
|
||||||
|
if (colour >= used_colors)
|
||||||
|
return {};
|
||||||
|
|
||||||
// Get alpha from AND mask (0: opaque => 0xff, 1: transparent => 0)
|
uint8_t b = palette[4 * colour + 0]; // Blue
|
||||||
uint8_t a = src_and_row[x / 8] & (1 << (7 - x % 8)) ? 0 : 0xff;
|
uint8_t g = palette[4 * colour + 1]; // Green
|
||||||
|
uint8_t r = palette[4 * colour + 2]; // Red
|
||||||
|
|
||||||
*dst_row++ = b;
|
// Get alpha from AND mask (0: opaque => 0xff, 1: transparent => 0)
|
||||||
*dst_row++ = g;
|
uint8_t a = src_and_row[x / 8] & (1 << (7 - x % 8)) ? 0 : 0xff;
|
||||||
*dst_row++ = r;
|
|
||||||
*dst_row++ = a;
|
*dst_row++ = b;
|
||||||
|
*dst_row++ = g;
|
||||||
|
*dst_row++ = r;
|
||||||
|
*dst_row++ = a;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
break;
|
||||||
break;
|
|
||||||
|
|
||||||
case 8:
|
case 8:
|
||||||
for (int y = 0; y < h; y++)
|
for (int y = 0; y < h; y++)
|
||||||
{
|
|
||||||
uint8_t * src_xor_row = xor_map.data() + xor_stride * y;
|
|
||||||
uint8_t * src_and_row = and_map.data() + and_stride * y;
|
|
||||||
uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y);
|
|
||||||
|
|
||||||
for (int x = 0, i = 0; x < w; x++)
|
|
||||||
{
|
{
|
||||||
int colour = *src_xor_row++;
|
uint8_t * src_xor_row = xor_map.data() + xor_stride * y;
|
||||||
if (colour >= used_colors)
|
uint8_t * src_and_row = and_map.data() + and_stride * y;
|
||||||
return {};
|
uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y);
|
||||||
|
|
||||||
uint8_t b = palette[4 * colour + 0]; // Blue
|
for (int x = 0, i = 0; x < w; x++)
|
||||||
uint8_t g = palette[4 * colour + 1]; // Green
|
{
|
||||||
uint8_t r = palette[4 * colour + 2]; // Red
|
int colour = *src_xor_row++;
|
||||||
|
if (colour >= used_colors)
|
||||||
|
return {};
|
||||||
|
|
||||||
// Get alpha from AND mask (0: opaque => 0xff, 1: transparent => 0)
|
uint8_t b = palette[4 * colour + 0]; // Blue
|
||||||
uint8_t a = src_and_row[x / 8] & (1 << (7 - x % 8)) ? 0 : 0xff;
|
uint8_t g = palette[4 * colour + 1]; // Green
|
||||||
|
uint8_t r = palette[4 * colour + 2]; // Red
|
||||||
|
|
||||||
*dst_row++ = b;
|
// Get alpha from AND mask (0: opaque => 0xff, 1: transparent => 0)
|
||||||
*dst_row++ = g;
|
uint8_t a = src_and_row[x / 8] & (1 << (7 - x % 8)) ? 0 : 0xff;
|
||||||
*dst_row++ = r;
|
|
||||||
*dst_row++ = a;
|
*dst_row++ = b;
|
||||||
|
*dst_row++ = g;
|
||||||
|
*dst_row++ = r;
|
||||||
|
*dst_row++ = a;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
break;
|
||||||
break;
|
|
||||||
|
|
||||||
case 16:
|
case 16:
|
||||||
for (int y = 0; y < h; y++)
|
for (int y = 0; y < h; y++)
|
||||||
{
|
|
||||||
uint8_t * src_xor_row = xor_map.data() + xor_stride * y;
|
|
||||||
uint8_t * src_and_row = and_map.data() + and_stride * y;
|
|
||||||
uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y);
|
|
||||||
|
|
||||||
for (int x = 0, i = 0; x < w; x++)
|
|
||||||
{
|
{
|
||||||
int colour = (src_xor_row[1] << 8) | src_xor_row[0];
|
uint8_t * src_xor_row = xor_map.data() + xor_stride * y;
|
||||||
src_xor_row += 2;
|
uint8_t * src_and_row = and_map.data() + and_stride * y;
|
||||||
|
uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y);
|
||||||
|
|
||||||
if (colour >= used_colors)
|
for (int x = 0, i = 0; x < w; x++)
|
||||||
return {};
|
{
|
||||||
|
int colour = (src_xor_row[1] << 8) | src_xor_row[0];
|
||||||
|
src_xor_row += 2;
|
||||||
|
|
||||||
uint8_t b = palette[4 * colour + 0]; // Blue
|
if (colour >= used_colors)
|
||||||
uint8_t g = palette[4 * colour + 1]; // Green
|
return {};
|
||||||
uint8_t r = palette[4 * colour + 2]; // Red
|
|
||||||
|
|
||||||
// Get alpha from AND mask (0: opaque => 0xff, 1: transparent => 0)
|
uint8_t b = palette[4 * colour + 0]; // Blue
|
||||||
uint8_t a = src_and_row[x / 8] & (1 << (7 - x % 8)) ? 0 : 0xff;
|
uint8_t g = palette[4 * colour + 1]; // Green
|
||||||
|
uint8_t r = palette[4 * colour + 2]; // Red
|
||||||
|
|
||||||
*dst_row++ = b;
|
// Get alpha from AND mask (0: opaque => 0xff, 1: transparent => 0)
|
||||||
*dst_row++ = g;
|
uint8_t a = src_and_row[x / 8] & (1 << (7 - x % 8)) ? 0 : 0xff;
|
||||||
*dst_row++ = r;
|
|
||||||
*dst_row++ = a;
|
*dst_row++ = b;
|
||||||
|
*dst_row++ = g;
|
||||||
|
*dst_row++ = r;
|
||||||
|
*dst_row++ = a;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
break;
|
||||||
break;
|
|
||||||
|
|
||||||
case 24:
|
case 24:
|
||||||
for (int y = 0; y < h; y++)
|
for (int y = 0; y < h; y++)
|
||||||
{
|
|
||||||
uint8_t * src_xor_row = xor_map.data() + xor_stride * y;
|
|
||||||
uint8_t * src_and_row = and_map.data() + and_stride * y;
|
|
||||||
uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y);
|
|
||||||
|
|
||||||
for (int x = 0, i = 0; x < w; x++)
|
|
||||||
{
|
{
|
||||||
uint8_t b = *src_xor_row++; // Blue
|
uint8_t * src_xor_row = xor_map.data() + xor_stride * y;
|
||||||
uint8_t g = *src_xor_row++; // Green
|
uint8_t * src_and_row = and_map.data() + and_stride * y;
|
||||||
uint8_t r = *src_xor_row++; // Red
|
uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y);
|
||||||
|
|
||||||
// Get alpha from AND mask (0: opaque => 0xff, 1: transparent => 0)
|
for (int x = 0, i = 0; x < w; x++)
|
||||||
uint8_t a = src_and_row[x / 8] & (1 << (7 - x % 8)) ? 0 : 0xff;
|
{
|
||||||
|
uint8_t b = *src_xor_row++; // Blue
|
||||||
|
uint8_t g = *src_xor_row++; // Green
|
||||||
|
uint8_t r = *src_xor_row++; // Red
|
||||||
|
|
||||||
*dst_row++ = b;
|
// Get alpha from AND mask (0: opaque => 0xff, 1: transparent => 0)
|
||||||
*dst_row++ = g;
|
uint8_t a = src_and_row[x / 8] & (1 << (7 - x % 8)) ? 0 : 0xff;
|
||||||
*dst_row++ = r;
|
|
||||||
*dst_row++ = a;
|
*dst_row++ = b;
|
||||||
|
*dst_row++ = g;
|
||||||
|
*dst_row++ = r;
|
||||||
|
*dst_row++ = a;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
break;
|
||||||
break;
|
|
||||||
|
|
||||||
case 32:
|
case 32:
|
||||||
for (int y = 0; y < h; y++)
|
for (int y = 0; y < h; y++)
|
||||||
{
|
{
|
||||||
uint8_t * src_xor_row = (uint8_t *)xor_map.data() + xor_stride * y;
|
uint8_t * src_xor_row = (uint8_t *)xor_map.data() + xor_stride * y;
|
||||||
uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y);
|
uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y);
|
||||||
|
|
||||||
memcpy(dst_row, src_xor_row, dst_stride);
|
memcpy(dst_row, src_xor_row, dst_stride);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
png_image png{
|
png_image png{
|
||||||
.version = PNG_IMAGE_VERSION,
|
.version = PNG_IMAGE_VERSION,
|
||||||
.width = w,
|
.width = w,
|
||||||
.height = h,
|
.height = h,
|
||||||
.format = PNG_FORMAT_BGRA,
|
.format = PNG_FORMAT_BGRA,
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<std::byte> png_data{65536};
|
std::vector<std::byte> png_data{65536};
|
||||||
png_alloc_size_t png_size = png_data.size();
|
png_alloc_size_t png_size = png_data.size();
|
||||||
png_image_write_to_memory(&png, png_data.data(), &png_size, false /* convert_to_8_bit*/, dest_buffer.data(), w * 4, nullptr);
|
|
||||||
|
|
||||||
if (png_size > png_data.size())
|
|
||||||
{
|
|
||||||
png_data.resize(png_size);
|
|
||||||
png_image_write_to_memory(&png, png_data.data(), &png_size, false /* convert_to_8_bit*/, dest_buffer.data(), w * 4, nullptr);
|
png_image_write_to_memory(&png, png_data.data(), &png_size, false /* convert_to_8_bit*/, dest_buffer.data(), w * 4, nullptr);
|
||||||
}
|
|
||||||
else
|
|
||||||
png_data.resize(png_size);
|
|
||||||
|
|
||||||
return png_data;
|
if (png_size > png_data.size())
|
||||||
|
{
|
||||||
|
png_data.resize(png_size);
|
||||||
|
png_image_write_to_memory(&png, png_data.data(), &png_size, false /* convert_to_8_bit*/, dest_buffer.data(), w * 4, nullptr);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
png_data.resize(png_size);
|
||||||
|
|
||||||
|
icons.emplace_back(w, h, bpp, std::move(png_data));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw std::runtime_error{"Wrong magic number in " + filename.native()};
|
return icons;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::byte> load_zip(const std::filesystem::path & filename, int size)
|
std::vector<wivrn::icon> try_load_zip(const std::filesystem::path & filename)
|
||||||
{
|
{
|
||||||
std::unique_ptr<archive, decltype(&archive_read_free)> zip{archive_read_new(), &archive_read_free};
|
std::unique_ptr<archive, decltype(&archive_read_free)> zip{archive_read_new(), &archive_read_free};
|
||||||
|
|
||||||
@@ -550,14 +492,10 @@ std::vector<std::byte> load_zip(const std::filesystem::path & filename, int size
|
|||||||
throw std::runtime_error{"Cannot open " + filename.native() + ": " + archive_error_string(zip.get())};
|
throw std::runtime_error{"Cannot open " + filename.native() + ": " + archive_error_string(zip.get())};
|
||||||
|
|
||||||
archive_entry * entry;
|
archive_entry * entry;
|
||||||
int width = 0;
|
std::vector<wivrn::icon> icons;
|
||||||
int height = 0;
|
|
||||||
std::vector<std::byte> png;
|
|
||||||
|
|
||||||
while (archive_read_next_header(zip.get(), &entry) == ARCHIVE_OK)
|
while (archive_read_next_header(zip.get(), &entry) == ARCHIVE_OK)
|
||||||
{
|
{
|
||||||
printf("%s\n", archive_entry_pathname(entry));
|
|
||||||
|
|
||||||
std::vector<std::byte> buffer;
|
std::vector<std::byte> buffer;
|
||||||
buffer.resize(archive_entry_size(entry));
|
buffer.resize(archive_entry_size(entry));
|
||||||
|
|
||||||
@@ -577,10 +515,6 @@ std::vector<std::byte> load_zip(const std::filesystem::path & filename, int size
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keep the largest icon only
|
|
||||||
if (png_ref.width * png_ref.height < width * height)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
pixels.resize(PNG_IMAGE_SIZE(png_ref));
|
pixels.resize(PNG_IMAGE_SIZE(png_ref));
|
||||||
|
|
||||||
if (!png_image_finish_read(&png_ref, nullptr, pixels.data(), 0, nullptr))
|
if (!png_image_finish_read(&png_ref, nullptr, pixels.data(), 0, nullptr))
|
||||||
@@ -589,48 +523,86 @@ std::vector<std::byte> load_zip(const std::filesystem::path & filename, int size
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// PNG read successfully: keep it until we find a better one
|
// PNG read successfully
|
||||||
width = png_ref.width;
|
icons.emplace_back(png_ref.width, png_ref.height, 32, std::move(buffer)); // TODO: check how to get bit depth
|
||||||
height = png_ref.height;
|
|
||||||
png = std::move(buffer);
|
|
||||||
|
|
||||||
png_image_free(&png_ref);
|
png_image_free(&png_ref);
|
||||||
}
|
}
|
||||||
|
|
||||||
return png;
|
return icons;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unordered_map<std::filesystem::path, std::vector<std::byte>> icon_cache;
|
std::vector<wivrn::icon> try_load_png(const std::filesystem::path & filename)
|
||||||
|
{
|
||||||
|
auto png_data = load_file(filename);
|
||||||
|
std::vector<wivrn::icon> icons;
|
||||||
|
|
||||||
|
// Try reading the PNG
|
||||||
|
std::vector<uint8_t> pixels;
|
||||||
|
png_image png_ref{
|
||||||
|
.version = PNG_IMAGE_VERSION,
|
||||||
|
.format = PNG_FORMAT_BGRA,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (not png_image_begin_read_from_memory(&png_ref, png_data.data(), png_data.size()))
|
||||||
|
{
|
||||||
|
png_image_free(&png_ref);
|
||||||
|
throw std::runtime_error("");
|
||||||
|
}
|
||||||
|
|
||||||
|
pixels.resize(PNG_IMAGE_SIZE(png_ref));
|
||||||
|
|
||||||
|
if (!png_image_finish_read(&png_ref, nullptr, pixels.data(), 0, nullptr))
|
||||||
|
{
|
||||||
|
png_image_free(&png_ref);
|
||||||
|
throw std::runtime_error("");
|
||||||
|
}
|
||||||
|
|
||||||
|
// PNG read successfully
|
||||||
|
icons.emplace_back(png_ref.width, png_ref.height, 32, std::move(png_data)); // TODO: check how to get bit depth
|
||||||
|
|
||||||
|
png_image_free(&png_ref);
|
||||||
|
|
||||||
|
return icons;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unordered_map<std::filesystem::path, std::vector<wivrn::icon>> icon_cache;
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
const std::vector<std::byte> & wivrn::load_icon(const std::filesystem::path & filename, int size)
|
const std::vector<wivrn::icon> & wivrn::load_icon(const std::filesystem::path & filename)
|
||||||
{
|
{
|
||||||
auto it = icon_cache.find(filename);
|
auto it = icon_cache.find(filename);
|
||||||
if (it != icon_cache.end() and not it->second.empty())
|
if (it != icon_cache.end() and not it->second.empty())
|
||||||
return it->second;
|
return it->second;
|
||||||
|
|
||||||
std::vector<std::byte> icon;
|
try
|
||||||
|
{
|
||||||
|
return icon_cache.emplace(filename, try_load_svg(filename, 256)).first->second;
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{}
|
||||||
|
|
||||||
if (filename.extension() == ".svg")
|
try
|
||||||
{
|
{
|
||||||
icon = load_svg(filename, size);
|
return icon_cache.emplace(filename, try_load_ico(filename)).first->second;
|
||||||
}
|
|
||||||
else if (filename.extension() == ".ico")
|
|
||||||
{
|
|
||||||
// Steam icon (Windows)
|
|
||||||
icon = load_ico(filename, size);
|
|
||||||
}
|
|
||||||
else if (filename.extension() == ".zip")
|
|
||||||
{
|
|
||||||
// Steam icon (Linux)
|
|
||||||
icon = load_zip(filename, size);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// TODO read arbitrary format / rewrite as png
|
|
||||||
icon = load_file(filename);
|
|
||||||
}
|
}
|
||||||
|
catch (...)
|
||||||
|
{}
|
||||||
|
|
||||||
return icon_cache.emplace(filename, std::move(icon)).first->second;
|
try
|
||||||
|
{
|
||||||
|
return icon_cache.emplace(filename, try_load_zip(filename)).first->second;
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return icon_cache.emplace(filename, try_load_png(filename)).first->second;
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{}
|
||||||
|
|
||||||
|
throw std::runtime_error{"Cannot load icon: " + filename.native()};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,5 +23,13 @@
|
|||||||
|
|
||||||
namespace wivrn
|
namespace wivrn
|
||||||
{
|
{
|
||||||
const std::vector<std::byte> & load_icon(const std::filesystem::path & filename, int size);
|
struct icon
|
||||||
}
|
{
|
||||||
|
int width;
|
||||||
|
int height;
|
||||||
|
int bpp;
|
||||||
|
std::vector<std::byte> png_data;
|
||||||
|
};
|
||||||
|
|
||||||
|
const std::vector<icon> & load_icon(const std::filesystem::path & filename);
|
||||||
|
} // namespace wivrn
|
||||||
|
|||||||
@@ -759,11 +759,22 @@ void wivrn_session::operator()(from_headset::get_application_list && request)
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
auto icon = load_icon(*app.icon_path, 256);
|
const auto & icons = load_icon(*app.icon_path);
|
||||||
|
|
||||||
|
if (icons.empty())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
const auto & largest_icon = std::ranges::max(icons, [](const wivrn::icon & a, const wivrn::icon & b) {
|
||||||
|
if (a.bpp < b.bpp)
|
||||||
|
return true;
|
||||||
|
if (a.bpp > b.bpp)
|
||||||
|
return false;
|
||||||
|
return a.width * a.height < b.width * b.height;
|
||||||
|
});
|
||||||
|
|
||||||
send_control(to_headset::application_icon{
|
send_control(to_headset::application_icon{
|
||||||
.id = id,
|
.id = id,
|
||||||
.image = std::move(icon),
|
.image = largest_icon.png_data,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch (std::exception & e)
|
catch (std::exception & e)
|
||||||
|
|||||||
Reference in New Issue
Block a user