icon-loader: add a new loader for XPM icons, used by many legacy X11 apps

Adapted from gdk-pixbuf, see the original at:

  https://gitlab.gnome.org/GNOME/gdk-pixbuf/-/blob/master/gdk-pixbuf/io-xpm.c

rgb.txt is from X.org, see:

  https://gitlab.freedesktop.org/xorg/app/rgb/-/blob/master/rgb.txt

Differences from the gdk-pixbuf version:

- GdkPixbuf replaced with struct lab_data_buffer
- Progressive and in-memory loading code removed
- Two functions that had separate BSD copyright rewritten
- Stores colors as ARGB32 earlier in the decoding process for efficiency
- Limited to 1024x1024 px and 1024 colors to prevent abuse
- Uses struct buf to build strings, in place of manual g_new/g_realloc
- Uses xzalloc/xznew_n for other memory allocations
- Uses g_strlcpy in place of banned strcpy/strncpy/strncat
- Uses standard C types (int, bool, etc.) in place of the GLib ones
- Follows labwc coding style (whitespace, braces, letter case, etc.)
- Et cetera ...

v2: add Perl fixes from @domo141
This commit is contained in:
John Lindgren 2024-09-30 03:41:25 -04:00
parent 887ec70ba4
commit c9e0a6e125
8 changed files with 2880 additions and 2 deletions

80
src/img/gen-color-table.pl Executable file
View file

@ -0,0 +1,80 @@
#!/usr/bin/perl -w
# Generates xpm-color-table.h from X11's rgb.txt
# Adapted from gdk-pixbuf (LGPL-2.0-or-later)
if (@ARGV != 1) {
die "Usage: gen-color-table.pl rgb.txt > xpm-color-table.h\n";
}
open IN, '<', $ARGV[0] or die "Cannot open $ARGV[0]: $!\n";
@colors = ();
while (<IN>) {
next if /^!/;
if (!/^\s*([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+(.*\S)\s*$/) {
die "Cannot parse line $_";
}
push @colors, [$1, $2, $3, $4];
}
close IN or die "close IN failed: $!\n";
@colors = sort { lc($a->[3]) cmp lc($b->[3]) } @colors;
$offset = 0;
$date = gmtime;
print <<EOT;
/* SPDX-License-Identifier: LGPL-2.0-or-later */
/* xpm-color-table.h: Generated by gen-color-table.pl from rgb.txt
*
* Date: $date
*
* Do not edit.
*/
static const char color_names[] =
EOT
for $color (@colors) {
$name = $color->[3];
if ($offset != 0) {
print qq(\n);
}
print qq( "$name\\0");
$color->[4] = $offset;
$offset += length($name) + 1;
}
print ";\n\n";
print <<EOT;
struct xcolor_entry {
uint16_t name_offset;
uint8_t red;
uint8_t green;
uint8_t blue;
};
static const struct xcolor_entry xcolors[] = {
EOT
$i = 0;
for $color (@colors) {
$red = $color->[0];
$green = $color->[1];
$blue = $color->[2];
$offset = $color->[4];
if ($i != 0) {
print ",\n";
}
print " { $offset, $red, $green, $blue }";
$i++;
}
print "\n};\n";