Let imageviewer toggle background color

This commit is contained in:
Nigel Tao committed 2020-01-24 12:00:20 +11:00
1 parent 731350f8f9
commit d595980f75
1 file changed
+28 -5
+28 -5
View File
@@ -65,10 +65,17 @@ The Escape key quits.
// X11 limits its image dimensions to uint16_t. // X11 limits its image dimensions to uint16_t.
#define MAX_DIMENSION 65535 #define MAX_DIMENSION 65535
#define NUM_BACKGROUND_COLORS 3
#define SRC_BUFFER_SIZE (64 * 1024) #define SRC_BUFFER_SIZE (64 * 1024)
// Global variable names start with a "g_" prefix. // Global variable names start with a "g_" prefix.
wuffs_base__color_u32_argb_premul g_background_colors[NUM_BACKGROUND_COLORS] = {
0xFF000000,
0xFFFFFFFF,
0xFFA9009A,
};
FILE* g_file = NULL; FILE* g_file = NULL;
const char* g_filename = NULL; const char* g_filename = NULL;
uint32_t g_width = 0; uint32_t g_width = 0;
@@ -80,7 +87,7 @@ uint8_t g_src_buffer[SRC_BUFFER_SIZE] = {0};
wuffs_base__io_buffer g_src = {0}; wuffs_base__io_buffer g_src = {0};
wuffs_base__image_config g_image_config = {0}; wuffs_base__image_config g_image_config = {0};
wuffs_base__image_decoder* g_image_decoder = NULL; wuffs_base__image_decoder* g_image_decoder = NULL;
wuffs_base__color_u32_argb_premul g_background_color = 0xFFA9009A; uint32_t g_background_color_index = 0;
union { union {
wuffs_gif__decoder gif; wuffs_gif__decoder gif;
@@ -217,8 +224,10 @@ bool load_image_config() {
} }
{ {
uint8_t* ptr = (uint8_t*)p; uint8_t* ptr = (uint8_t*)p;
wuffs_base__color_u32_argb_premul color =
g_background_colors[g_background_color_index];
for (size_t i = 0; i < num_pixels; i++) { for (size_t i = 0; i < num_pixels; i++) {
store_u32le(ptr, g_background_color); store_u32le(ptr, color);
ptr += 4; ptr += 4;
} }
} }
@@ -409,6 +418,7 @@ int main(int argc, char** argv) {
while (true) { while (true) {
xcb_generic_event_t* event = xcb_wait_for_event(c); xcb_generic_event_t* event = xcb_wait_for_event(c);
bool reload = false;
switch (event->response_type & 0x7F) { switch (event->response_type & 0x7F) {
case XCB_EXPOSE: { case XCB_EXPOSE: {
@@ -429,6 +439,7 @@ int main(int argc, char** argv) {
switch (i) { switch (i) {
case XK_Escape: case XK_Escape:
return 0; return 0;
case ' ': case ' ':
case XK_BackSpace: case XK_BackSpace:
case XK_Return: case XK_Return:
@@ -441,9 +452,15 @@ int main(int argc, char** argv) {
} else if (arg == argc) { } else if (arg == argc) {
arg = 1; arg = 1;
} }
loaded = load(c, s, w, g, argv[arg]); reload = true;
xcb_clear_area(c, 1, w, 0, 0, 0xFFFF, 0xFFFF); break;
xcb_flush(c);
case ',':
case '.':
g_background_color_index +=
(i == ',') ? (NUM_BACKGROUND_COLORS - 1) : 1;
g_background_color_index %= NUM_BACKGROUND_COLORS;
reload = true;
break; break;
} }
} }
@@ -460,6 +477,12 @@ int main(int argc, char** argv) {
} }
free(event); free(event);
if (reload) {
loaded = load(c, s, w, g, argv[arg]);
xcb_clear_area(c, 1, w, 0, 0, 0xFFFF, 0xFFFF);
xcb_flush(c);
}
} }
return 0; return 0;
} }