2018-08-15 23:01:54 +08:00
|
|
|
#include "tiny_xpm.h"
|
2018-02-06 00:29:40 +08:00
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
2018-02-06 00:29:40 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2018-02-13 17:10:18 +08:00
|
|
|
#include "log.h"
|
|
|
|
|
2018-02-06 00:29:40 +08:00
|
|
|
struct index {
|
|
|
|
char c;
|
2019-03-03 06:52:22 +08:00
|
|
|
uint32_t color;
|
2018-02-06 00:29:40 +08:00
|
|
|
};
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
static bool
|
|
|
|
find_color(struct index *index, int len, char c, uint32_t *color) {
|
2018-02-06 00:29:40 +08:00
|
|
|
// there are typically very few color, so it's ok to iterate over the array
|
|
|
|
for (int i = 0; i < len; ++i) {
|
|
|
|
if (index[i].c == c) {
|
|
|
|
*color = index[i].color;
|
2019-03-03 06:52:22 +08:00
|
|
|
return true;
|
2018-02-06 00:29:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*color = 0;
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2018-02-06 00:29:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// We encounter some problems with SDL2_image on MSYS2 (Windows),
|
|
|
|
// so here is our own XPM parsing not to depend on SDL_image.
|
|
|
|
//
|
|
|
|
// We do not hardcode the binary image to keep some flexibility to replace the
|
|
|
|
// icon easily (just by replacing icon.xpm).
|
|
|
|
//
|
|
|
|
// Parameter is not "const char *" because XPM formats are generally stored in a
|
|
|
|
// (non-const) "char *"
|
2019-03-03 03:09:56 +08:00
|
|
|
SDL_Surface *
|
|
|
|
read_xpm(char *xpm[]) {
|
2018-02-07 18:47:34 +08:00
|
|
|
#if SDL_ASSERT_LEVEL >= 2
|
|
|
|
// patch the XPM to change the icon color in debug mode
|
|
|
|
xpm[2] = ". c #CC00CC";
|
|
|
|
#endif
|
|
|
|
|
2018-02-06 00:29:40 +08:00
|
|
|
char *endptr;
|
|
|
|
// *** No error handling, assume the XPM source is valid ***
|
|
|
|
// (it's in our source repo)
|
|
|
|
// Assertions are only checked in debug
|
2018-08-12 21:27:58 +08:00
|
|
|
int width = strtol(xpm[0], &endptr, 10);
|
|
|
|
int height = strtol(endptr + 1, &endptr, 10);
|
|
|
|
int colors = strtol(endptr + 1, &endptr, 10);
|
|
|
|
int chars = strtol(endptr + 1, &endptr, 10);
|
2018-02-06 00:29:40 +08:00
|
|
|
|
|
|
|
// sanity checks
|
2018-08-12 21:27:58 +08:00
|
|
|
SDL_assert(0 <= width && width < 256);
|
|
|
|
SDL_assert(0 <= height && height < 256);
|
|
|
|
SDL_assert(0 <= colors && colors < 256);
|
2018-02-06 00:29:40 +08:00
|
|
|
SDL_assert(chars == 1); // this implementation does not support more
|
|
|
|
|
|
|
|
// init index
|
|
|
|
struct index index[colors];
|
|
|
|
for (int i = 0; i < colors; ++i) {
|
|
|
|
const char *line = xpm[1+i];
|
|
|
|
index[i].c = line[0];
|
|
|
|
SDL_assert(line[1] == '\t');
|
|
|
|
SDL_assert(line[2] == 'c');
|
|
|
|
SDL_assert(line[3] == ' ');
|
|
|
|
if (line[4] == '#') {
|
|
|
|
index[i].color = 0xff000000 | strtol(&line[5], &endptr, 0x10);
|
|
|
|
SDL_assert(*endptr == '\0');
|
|
|
|
} else {
|
|
|
|
SDL_assert(!strcmp("None", &line[4]));
|
|
|
|
index[i].color = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse image
|
2019-03-03 06:52:22 +08:00
|
|
|
uint32_t *pixels = SDL_malloc(4 * width * height);
|
2018-02-06 00:29:40 +08:00
|
|
|
if (!pixels) {
|
2018-02-13 17:10:18 +08:00
|
|
|
LOGE("Could not allocate icon memory");
|
2018-02-06 00:29:40 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (int y = 0; y < height; ++y) {
|
|
|
|
const char *line = xpm[1 + colors + y];
|
|
|
|
for (int x = 0; x < width; ++x) {
|
|
|
|
char c = line[x];
|
2019-03-03 06:52:22 +08:00
|
|
|
uint32_t color;
|
|
|
|
bool color_found = find_color(index, colors, c, &color);
|
2018-02-06 00:29:40 +08:00
|
|
|
SDL_assert(color_found);
|
|
|
|
pixels[y * width + x] = color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
|
2019-03-03 06:52:22 +08:00
|
|
|
uint32_t amask = 0x000000ff;
|
|
|
|
uint32_t rmask = 0x0000ff00;
|
|
|
|
uint32_t gmask = 0x00ff0000;
|
|
|
|
uint32_t bmask = 0xff000000;
|
2018-02-06 00:29:40 +08:00
|
|
|
#else // little endian, like x86
|
2019-03-03 06:52:22 +08:00
|
|
|
uint32_t amask = 0xff000000;
|
|
|
|
uint32_t rmask = 0x00ff0000;
|
|
|
|
uint32_t gmask = 0x0000ff00;
|
|
|
|
uint32_t bmask = 0x000000ff;
|
2018-02-06 00:29:40 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
SDL_Surface *surface = SDL_CreateRGBSurfaceFrom(pixels,
|
|
|
|
width, height,
|
|
|
|
32, 4 * width,
|
|
|
|
rmask, gmask, bmask, amask);
|
|
|
|
// make the surface own the raw pixels
|
|
|
|
surface->flags &= ~SDL_PREALLOC;
|
|
|
|
return surface;
|
|
|
|
}
|