Implement Gnome thumbnailer

This commit is contained in:
Maximilian Mader 2019-05-04 17:59:27 +00:00
parent 8420fb7364
commit 8b158ac877
Signed by: Max
GPG Key ID: F71D56A3151C4FB3
9 changed files with 10895 additions and 19 deletions

View File

@ -0,0 +1,96 @@
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <assert.h>
#include <Core/gb.h>
#include "get_image_for_rom.h"
#define LENGTH 60 * 10
struct local_data {
unsigned long frames;
bool running;
};
static char *async_input_callback(GB_gameboy_t *gb)
{
return NULL;
}
static void log_callback(GB_gameboy_t *gb, const char *string, GB_log_attributes attributes)
{
}
static void vblank(GB_gameboy_t *gb)
{
struct local_data *local_data = (struct local_data *)GB_get_user_data(gb);
if (local_data->frames == LENGTH) {
local_data->running = false;
}
else if (local_data->frames == LENGTH - 1) {
GB_set_rendering_disabled(gb, false);
}
local_data->frames++;
}
static uint32_t rgb_encode(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b)
{
return (b << 16) | (g << 8) | (r) | 0xFF000000;
}
int get_image_for_rom_common(GB_gameboy_t gb, const char *filename, uint32_t *output, uint8_t *cgb_flag) {
GB_set_vblank_callback(&gb, (GB_vblank_callback_t) vblank);
GB_set_pixels_output(&gb, output);
GB_set_rgb_encode_callback(&gb, rgb_encode);
GB_set_async_input_callback(&gb, async_input_callback);
GB_set_log_callback(&gb, log_callback);
if (GB_load_rom(&gb, filename)) {
GB_free(&gb);
return 1;
}
/* Run emulation */
struct local_data local_data = {0,};
GB_set_user_data(&gb, &local_data);
local_data.running = true;
local_data.frames = 0;
GB_set_rendering_disabled(&gb, true);
GB_set_turbo_mode(&gb, true, true);
*cgb_flag = GB_read_memory(&gb, 0x143) & 0xC0;
while (local_data.running) {
GB_run(&gb);
}
GB_free(&gb);
return 0;
}
int get_image_for_rom_alt(const char *filename, const unsigned char *buffer, size_t size, uint32_t *output, uint8_t *cgb_flag)
{
GB_gameboy_t gb;
GB_init(&gb, GB_MODEL_CGB_E);
GB_load_boot_rom_from_buffer(&gb, buffer, size);
return get_image_for_rom_common(gb, filename, output, cgb_flag);
}
int get_image_for_rom(const char *filename, const char *boot_path, uint32_t *output, uint8_t *cgb_flag)
{
GB_gameboy_t gb;
GB_init(&gb, GB_MODEL_CGB_E);
if (GB_load_boot_rom(&gb, boot_path)) {
GB_free(&gb);
return 1;
}
return get_image_for_rom_common(gb, filename, output, cgb_flag);
}

View File

@ -0,0 +1,10 @@
#ifndef get_image_for_rom_h
#define get_image_for_rom_h
#include <stdint.h>
// typedef bool (*cancel_callback_t)(void*);
int get_image_for_rom_alt(const char *filename, const unsigned char *buffer, size_t size, uint32_t *output, uint8_t *cgb_flag);
int get_image_for_rom(const char *filename, const char *boot_path, uint32_t *output, uint8_t *cgb_flag);
#endif

5992
GnomeThumbnailer/lodepng.c Normal file

File diff suppressed because it is too large Load Diff

1930
GnomeThumbnailer/lodepng.h Normal file

File diff suppressed because it is too large Load Diff

187
GnomeThumbnailer/main.c Normal file
View File

@ -0,0 +1,187 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "stb_image_resize.h"
#include "get_image_for_rom.h"
#include "lodepng.h"
extern uint8_t cgb_boot_fast_data[] asm("_binary_build_bin_BootROMs_cgb_boot_fast_bin_start");
extern uint8_t cgb_boot_fast_data_size[] asm("_binary_build_bin_BootROMs_cgb_boot_fast_bin_end");
extern uint8_t cgb_boot_fast_data_end[] asm("_binary_build_bin_BootROMs_cgb_boot_fast_bin_size");
extern uint8_t cartridge_template_data[] asm("_binary_QuickLook_CartridgeTemplate_png_start");
extern uint8_t cartridge_template_size[] asm("_binary_QuickLook_CartridgeTemplate_png_end");
extern uint8_t cartridge_template_end[] asm("_binary_QuickLook_CartridgeTemplate_png_size");
extern uint8_t color_cartridge_template_data[] asm("_binary_QuickLook_ColorCartridgeTemplate_png_start");
extern uint8_t color_cartridge_template_size[] asm("_binary_QuickLook_ColorCartridgeTemplate_png_end");
extern uint8_t color_cartridge_template_end[] asm("_binary_QuickLook_ColorCartridgeTemplate_png_size");
extern uint8_t universal_cartridge_template_data[] asm("_binary_QuickLook_UniversalCartridgeTemplate_png_start");
extern uint8_t universal_cartridge_template_size[] asm("_binary_QuickLook_UniversalCartridgeTemplate_png_end");
extern uint8_t universal_cartridge_template_end[] asm("_binary_QuickLook_UniversalCartridgeTemplate_png_size");
unsigned int alpha_blend(const unsigned int dest, const unsigned int src) {
unsigned char r1 = (src >> 0) & 0xFF;
unsigned char g1 = (src >> 8) & 0xFF;
unsigned char b1 = (src >> 16) & 0xFF;
float a1 = (float)((src >> 24) & 0xFF) / 255.0;
unsigned char r2 = (dest >> 0) & 0xFF;
unsigned char g2 = (dest >> 8) & 0xFF;
unsigned char b2 = (dest >> 16) & 0xFF;
float a2 = (float)((dest >> 24) & 0xFF) / 255.0;
unsigned char a = (a1 + (1.0 - a1) * a2) * 255.0;
if (a > 0) {
unsigned char r = (255 / a) * ((a1 * r1) + (1 - a1) * r2);
unsigned char g = (255 / a) * ((a1 * g1) + (1 - a1) * g2);
unsigned char b = (255 / a) * ((a1 * b1) + (1 - a1) * b2);
return (a << 24) | (b << 16) | (g << 8) | r;
}
return 0;
}
int main (int argc, char *argv[]) {
int size = 128;
char* input = "";
char* output = "";
for (int i = 1; i < argc; i++) {
if (strncmp(argv[i], "-s", 2) == 0) {
i += 1;
if (argc > i && sscanf(argv[i], "%d", &size) != 1) {
fprintf(stderr, "Failed to parse parameter \"%s %s\"\n", argv[i - 1], argv[i]);
}
}
else if (input[0] == '\0') {
input = argv[i];
for (int j = i + 1; j < argc; j++) {
if (strncmp(argv[j], "-s", 2) != 0) {
output = argv[j];
break;
}
else if (argc >= j + 3) {
output = argv[j + 2];
break;
}
}
}
}
if (input[0] == '\0') {
fprintf(stderr, "No input file specified\n");
return 1;
}
unsigned error;
uint32_t bitmap[160 * 144];
uint8_t cgbFlag = 0;
size_t boot_size = (size_t)((void *)cgb_boot_fast_data_size);
const unsigned char *buffer = (const unsigned char*)((void *) cgb_boot_fast_data);
if (get_image_for_rom_alt(input, buffer, boot_size, bitmap, &cgbFlag)) {
return -1;
}
const unsigned char* screen = (const unsigned char*)((void *)bitmap);
unsigned char* resized_screen = malloc(640 * 576 * 4 * sizeof(unsigned char));
if (resized_screen == NULL) {
fprintf(stderr, "Failed to allocate memory\n");
return -1;
}
error = stbir_resize_uint8(screen, 160, 140, 0, resized_screen, 640, 576, 0, 4);
if (error == 0) {
fprintf(stderr, "Failed to resize screenshot\n");
return 1;
}
unsigned char* template_data;
size_t template_size;
unsigned char* template;
unsigned template_width, template_height;
printf("cgb: %x\n", cgbFlag);
switch (cgbFlag) {
case 0xC0:
template_data = (unsigned char*)color_cartridge_template_data;
template_size = (size_t)color_cartridge_template_size;
break;
case 0x80:
template_data = (unsigned char*)universal_cartridge_template_data;
template_size = (size_t)universal_cartridge_template_size;
break;
default:
template_data = (unsigned char*)cartridge_template_data;
template_size = (size_t)cartridge_template_size;
}
error = lodepng_decode32(
&template,
&template_width,
&template_height,
template_data,
template_size
);
uint32_t* canvas = calloc(template_width * template_height, sizeof(uint32_t));
uint32_t* resized_screen32 = (uint32_t*)resized_screen;
if (canvas == NULL) {
fprintf(stderr, "Failed to allocate memory\n");
return -1;
}
for (int y = 0; y < 576; y++) {
for (int x = 0; x < 640; x++) {
canvas[x + 192 + ((y + 298) * template_height)] = resized_screen32[x + (y * 640)];
}
}
uint32_t* template32 = (uint32_t*)template;
for (int y = 0; y < template_height; y++) {
for (int x = 0; x < template_width; x++) {
canvas[x + (y * template_width)] = alpha_blend(
canvas[x + (y * template_width)],
template32[x + (y * template_width)]
);
}
}
unsigned char* final = malloc(size * size * 4 * sizeof(unsigned char));
if (final == NULL) {
fprintf(stderr, "Failed to allocate memory\n");
return -1;
}
error = stbir_resize_uint8((const unsigned char*)canvas, template_width, template_height, 0, final, size, size, 0, 4);
if (error == 0) {
fprintf(stderr, "Failed to resize screenshot\n");
return 1;
}
/*Encode the image*/
error = lodepng_encode32_file(output, (const unsigned char*)final, size, size);
if (error) {
fprintf(stderr, "Failed to encode PNG %u: %s\n", error, lodepng_error_text(error));
return error;
}
return 0;
}

File diff suppressed because it is too large Load Diff

View File

@ -188,6 +188,7 @@ endif
cocoa: $(BIN)/SameBoy.app
quicklook: $(BIN)/SameBoy.qlgenerator
gnome-thumbnailer: $(BIN)/GnomeThumbnailer/SameBoy-thumbnailer
sdl: $(SDL_TARGET) $(BIN)/SDL/dmg_boot.bin $(BIN)/SDL/cgb_boot.bin $(BIN)/SDL/agb_boot.bin $(BIN)/SDL/sgb_boot.bin $(BIN)/SDL/sgb2_boot.bin $(BIN)/SDL/LICENSE $(BIN)/SDL/registers.sym $(BIN)/SDL/background.bmp $(BIN)/SDL/Shaders
bootroms: $(BIN)/BootROMs/agb_boot.bin $(BIN)/BootROMs/cgb_boot.bin $(BIN)/BootROMs/dmg_boot.bin $(BIN)/BootROMs/sgb_boot.bin $(BIN)/BootROMs/sgb2_boot.bin
tester: $(TESTER_TARGET) $(BIN)/tester/dmg_boot.bin $(BIN)/tester/cgb_boot.bin $(BIN)/tester/agb_boot.bin $(BIN)/tester/sgb_boot.bin $(BIN)/tester/sgb2_boot.bin
@ -198,6 +199,7 @@ all: cocoa sdl tester libretro
CORE_SOURCES := $(shell ls Core/*.c)
SDL_SOURCES := $(shell ls SDL/*.c) $(OPEN_DIALOG) SDL/audio/$(SDL_AUDIO_DRIVER).c
TESTER_SOURCES := $(shell ls Tester/*.c)
GNOME_THUMBNAILER_SOURCES := $(shell ls GnomeThumbnailer/*.c)
ifeq ($(PLATFORM),Darwin)
COCOA_SOURCES := $(shell ls Cocoa/*.m) $(shell ls HexFiend/*.m) $(shell ls JoyKit/*.m)
@ -211,6 +213,7 @@ endif
CORE_OBJECTS := $(patsubst %,$(OBJ)/%.o,$(CORE_SOURCES))
COCOA_OBJECTS := $(patsubst %,$(OBJ)/%.o,$(COCOA_SOURCES))
QUICKLOOK_OBJECTS := $(patsubst %,$(OBJ)/%.o,$(QUICKLOOK_SOURCES))
GNOME_THUMBNAILER_OBJECTS := $(patsubst %,$(OBJ)/%.o,$(GNOME_THUMBNAILER_SOURCES))
SDL_OBJECTS := $(patsubst %,$(OBJ)/%.o,$(SDL_SOURCES))
TESTER_OBJECTS := $(patsubst %,$(OBJ)/%.o,$(TESTER_SOURCES))
@ -315,7 +318,7 @@ $(BIN)/SameBoy.qlgenerator/Contents/MacOS/SameBoyQL: $(CORE_OBJECTS) $(QUICKLOOK
$(BIN)/SameBoy.qlgenerator/Contents/Resources/cgb_boot_fast.bin: $(BIN)/BootROMs/cgb_boot_fast.bin
-@$(MKDIR) -p $(dir $@)
cp -f $^ $@
# SDL Port
# Unix versions build only one binary
@ -326,6 +329,24 @@ ifeq ($(CONF), release)
$(STRIP) $@
endif
$(OBJ)/GnomeThumbnailer/cgb_boot_fast.o: $(BIN)/BootROMs/cgb_boot_fast.bin
-@$(MKDIR) -p $(dir $@)
# TODO: support i386 output as well
objcopy -B i386 -I binary -O elf64-x86-64 $^ $@
$(OBJ)/GnomeThumbnailer/%.o: QuickLook/%.png
-@$(MKDIR) -p $(dir $@)
# TODO: support i386 output as well
objcopy -B i386 -I binary -O elf64-x86-64 $< $@
$(BIN)/GnomeThumbnailer/SameBoy-thumbnailer: $(CORE_OBJECTS) $(GNOME_THUMBNAILER_OBJECTS) \
$(OBJ)/GnomeThumbnailer/cgb_boot_fast.o \
$(OBJ)/GnomeThumbnailer/CartridgeTemplate.o \
$(OBJ)/GnomeThumbnailer/ColorCartridgeTemplate.o \
$(OBJ)/GnomeThumbnailer/UniversalCartridgeTemplate.o
-@$(MKDIR) -p $(dir $@)
$(CC) $(CORE_OBJECTS) $(GNOME_THUMBNAILER_OBJECTS) $(OBJ)/GnomeThumbnailer/cgb_boot_fast.o $(OBJ)/GnomeThumbnailer/CartridgeTemplate.o $(OBJ)/GnomeThumbnailer/ColorCartridgeTemplate.o $(OBJ)/GnomeThumbnailer/UniversalCartridgeTemplate.o -o $@ $(LDFLAGS)
# Windows version builds two, one with a conole and one without it
$(BIN)/SDL/sameboy.exe: $(CORE_OBJECTS) $(SDL_OBJECTS) $(OBJ)/Windows/resources.o
-@$(MKDIR) -p $(dir $@)
@ -342,7 +363,7 @@ $(OBJ)/%.o: %.rc
else
$(OBJ)/%.res: %.rc
-@$(MKDIR) -p $(dir $@)
rc /fo $@ /dVERSION=\"$(VERSION)\" $^
rc /fo $@ /dVERSION=\"$(VERSION)\" $^
%.o: %.res
cvtres /OUT:"$@" $^

View File

@ -21,7 +21,7 @@ static char *async_input_callback(GB_gameboy_t *gb)
static void log_callback(GB_gameboy_t *gb, const char *string, GB_log_attributes attributes)
{
}
@ -36,7 +36,7 @@ static void vblank(GB_gameboy_t *gb)
else if (local_data->frames == LENGTH - 1) {
GB_set_rendering_disabled(gb, false);
}
local_data->frames++;
}
@ -45,15 +45,7 @@ static uint32_t rgb_encode(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b)
return (b << 16) | (g << 8) | (r) | 0xFF000000;
}
int get_image_for_rom(const char *filename, const char *boot_path, uint32_t *output, uint8_t *cgb_flag)
{
GB_gameboy_t gb;
GB_init(&gb, GB_MODEL_CGB_E);
if (GB_load_boot_rom(&gb, boot_path)) {
GB_free(&gb);
return 1;
}
int get_image_for_rom_common(GB_gameboy_t gb, const char *filename, uint32_t *output, uint8_t *cgb_flag) {
GB_set_vblank_callback(&gb, (GB_vblank_callback_t) vblank);
GB_set_pixels_output(&gb, output);
GB_set_rgb_encode_callback(&gb, rgb_encode);
@ -80,7 +72,7 @@ int get_image_for_rom(const char *filename, const char *boot_path, uint32_t *out
GB_free(&gb);
return 1;
}
/* Run emulation */
struct local_data local_data = {0,};
GB_set_user_data(&gb, &local_data);
@ -88,16 +80,34 @@ int get_image_for_rom(const char *filename, const char *boot_path, uint32_t *out
local_data.frames = 0;
GB_set_rendering_disabled(&gb, true);
GB_set_turbo_mode(&gb, true, true);
*cgb_flag = GB_read_memory(&gb, 0x143) & 0xC0;
while (local_data.running) {
GB_run(&gb);
}
GB_free(&gb);
return 0;
}
int get_image_for_rom_alt(const char *filename, const unsigned char *buffer, size_t size, uint32_t *output, uint8_t *cgb_flag)
{
GB_gameboy_t gb;
GB_init(&gb, GB_MODEL_CGB_E);
GB_load_boot_rom_from_buffer(&gb, buffer, size);
return get_image_for_rom_common(gb, filename, output, cgb_flag);
}
int get_image_for_rom(const char *filename, const char *boot_path, uint32_t *output, uint8_t *cgb_flag)
{
GB_gameboy_t gb;
GB_init(&gb, GB_MODEL_CGB_E);
if (GB_load_boot_rom(&gb, boot_path)) {
GB_free(&gb);
return 1;
}
return get_image_for_rom_common(gb, filename, output, cgb_flag);
}

View File

@ -4,7 +4,7 @@
typedef bool (*cancel_callback_t)(void*);
int get_image_for_rom_alt(const char *filename, const unsigned char *buffer, size_t size, uint32_t *output, uint8_t *cgb_flag);
int get_image_for_rom(const char *filename, const char *boot_path, uint32_t *output, uint8_t *cgb_flag);
#endif