graphics: make jpg/png support seamless with load_sprite

This commit is contained in:
K. Lange 2020-04-23 18:44:07 +09:00
parent 4725c3f585
commit c68cd7e1e8
7 changed files with 58 additions and 25 deletions

View File

@ -32,7 +32,6 @@
#include <toaru/list.h>
#include <toaru/sdf.h>
#include <toaru/button.h>
#include <toaru/jpeg.h>
#define APPLICATION_TITLE "File Browser"
#define SCROLL_AMOUNT 120
@ -1060,7 +1059,7 @@ static void draw_background(int width, int height) {
}
}
load_sprite_jpg(wallpaper, wallpaper_path);
load_sprite(wallpaper, wallpaper_path);
if (free_it) {
free(wallpaper_path);

View File

@ -25,7 +25,6 @@
#include <toaru/auth.h>
#include <toaru/confreader.h>
#include <toaru/sdf.h>
#include <toaru/jpeg.h>
#include <toaru/trace.h>
#define TRACE_APP_NAME "glogin-provider"
@ -295,7 +294,7 @@ redo_everything:
TRACE("Loading wallpaper...");
{
sprite_t * wallpaper = malloc(sizeof(sprite_t));
load_sprite_jpg(wallpaper, WALLPAPER);
load_sprite(wallpaper, WALLPAPER);
float x = (float)width / (float)wallpaper->width;
float y = (float)height / (float)wallpaper->height;

View File

@ -25,8 +25,6 @@
#include <toaru/graphics.h>
#include <toaru/decorations.h>
#include <toaru/menu.h>
#include <toaru/jpeg.h>
#include <toaru/png.h>
/* Pointer to graphics memory */
static yutani_t * yctx;
@ -151,14 +149,7 @@ int main(int argc, char * argv[]) {
decor_width = bounds.width;
decor_height = bounds.height;
int status;
if (strstr(argv[optind],".jpg")) {
status = load_sprite_jpg(&img, argv[optind]);
} else if (strstr(argv[optind],".png")) {
status = load_sprite_png(&img, argv[optind]);
} else {
status = load_sprite(&img, argv[optind]);
}
int status = load_sprite(&img, argv[optind]);
if (status) {
fprintf(stderr, "%s: failed to open image %s\n", argv[0], argv[optind]);
return 1;

View File

@ -13,7 +13,6 @@
#include <toaru/sdf.h>
#include <toaru/menu.h>
#include <toaru/button.h>
#include <toaru/jpeg.h>
#include <toaru/list.h>
#include <sys/utsname.h>
@ -157,8 +156,8 @@ void set_hilight(struct TTKButton * button, int hilight) {
void load_wallpaper(void) {
if (wallpaper.bitmap) free(wallpaper.bitmap);
wallpaper.bitmap = NULL;
/* load JPG */
load_sprite_jpg(&wallpaper, wallpaper_path);
/* load wallpaper */
load_sprite(&wallpaper, wallpaper_path);
/* Ensures we render correctly when scaling */
wallpaper.alpha = ALPHA_EMBEDDED;
}

View File

@ -75,6 +75,7 @@ extern void blur_context_box(gfx_context_t * _src, int radius);
extern void sprite_free(sprite_t * sprite);
extern int load_sprite(sprite_t * sprite, char * filename);
extern int load_sprite_bmp(sprite_t * sprite, char * filename);
//extern int load_sprite_png(sprite_t * sprite, char * file);
extern void draw_sprite(gfx_context_t * ctx, sprite_t * sprite, int32_t x, int32_t y);
extern void draw_line(gfx_context_t * ctx, int32_t x0, int32_t x1, int32_t y0, int32_t y1, uint32_t color);

View File

@ -11,6 +11,7 @@
#include <stdio.h>
#include <math.h>
#include <fcntl.h>
#include <dlfcn.h>
#include <sys/ioctl.h>
@ -395,7 +396,40 @@ void blur_context_box(gfx_context_t * _src, int radius) {
_box_blur_vertical(_src,radius);
}
static int (*load_sprite_jpg)(sprite_t *, char *) = NULL;
static int (*load_sprite_png)(sprite_t *, char *) = NULL;
/**
* TODO: This should probably use some config file or plugin path
* for better discovery; we could rename these libraries and
* not have applications / other libraries depend on them
* directly and instead go through libtoaru_graphics.
*/
__attribute__((constructor)) static void _load_format_libraries() {
void * _lib_jpeg = dlopen("libtoaru_jpeg.so", 0);
if (_lib_jpeg) load_sprite_jpg = dlsym(_lib_jpeg, "load_sprite_jpg");
void * _lib_png = dlopen("libtoaru_png.so", 0);
if (_lib_png) load_sprite_png = dlsym(_lib_png, "load_sprite_png");
}
static char * extension_from_filename(char * filename) {
char * ext = strrchr(filename, '.');
if (ext && *ext == '.') return ext + 1;
return "";
}
int load_sprite(sprite_t * sprite, char * filename) {
char * ext = extension_from_filename(filename);
if (!strcmp(ext,"png")) return load_sprite_png(sprite, filename);
if (!strcmp(ext,"jpg") || !strcmp(ext,"jpeg")) return load_sprite_jpg(sprite, filename);
/* Fall back to bitmap */
return load_sprite_bmp(sprite, filename);
}
int load_sprite_bmp(sprite_t * sprite, char * filename) {
/* Open the requested binary */
FILE * image = fopen(filename, "r");

View File

@ -37,6 +37,12 @@ static char * icon_directories_48[] = {
NULL
};
static char * prefixes[] = {
"png",
"bmp",
NULL
};
__attribute__((constructor))
static void _init_caches(void) {
icon_cache_16 = hashmap_create(10);
@ -73,14 +79,18 @@ static sprite_t * icon_get_int(const char * name, hashmap_t * icon_cache, char *
char path[100];
while (icon_directories[i]) {
/* Check each path... */
sprintf(path, "%s/%s.bmp", icon_directories[i], name);
if (access(path, R_OK) == 0) {
/* And if we find one, cache it */
icon = malloc(sizeof(sprite_t));
load_sprite(icon, path);
icon->alpha = ALPHA_EMBEDDED;
hashmap_set(icon_cache, (void*)name, icon);
return icon;
char ** prefix = prefixes;
while (*prefix) {
sprintf(path, "%s/%s.%s", icon_directories[i], name, *prefix);
if (access(path, R_OK) == 0) {
/* And if we find one, cache it */
icon = malloc(sizeof(sprite_t));
load_sprite(icon, path);
icon->alpha = ALPHA_EMBEDDED;
hashmap_set(icon_cache, (void*)name, icon);
return icon;
}
prefix++;
}
i++;
}