From 2cc8464941cfdc89cf50c68b778b7bf67dd01190 Mon Sep 17 00:00:00 2001 From: TangoCash Date: Wed, 27 Oct 2021 21:30:09 +0200 Subject: [PATCH] add cleaner resize for svg images --- src/driver/pictureviewer/pictureviewer.cpp | 10 ++++ src/driver/pictureviewer/svg.cpp | 56 ++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/src/driver/pictureviewer/pictureviewer.cpp b/src/driver/pictureviewer/pictureviewer.cpp index 979b4a925..da753288c 100644 --- a/src/driver/pictureviewer/pictureviewer.cpp +++ b/src/driver/pictureviewer/pictureviewer.cpp @@ -46,6 +46,7 @@ extern int fh_crw_id (const char *); #ifdef FBV_SUPPORT_SVG extern int fh_svg_getsize (const char *, int *, int *, int, int); extern int fh_svg_load (const char *, unsigned char **, int *, int *); +extern int svg_load_resize(const char *name, unsigned char **buffer, int* ox, int* oy, int* dx, int* dy); extern int fh_svg_id (const char *); #endif double CPictureViewer::m_aspect_ratio_correction; @@ -861,6 +862,15 @@ fb_pixel_t * CPictureViewer::int_getImage(const std::string & name, int *width, { dprintf(DEBUG_INFO, "[CPictureViewer] [%s - %d] resize %s to %d x %d \n", __func__, __LINE__, name.c_str(), *width, *height); if (bpp == 4) +#ifdef FBV_SUPPORT_SVG + if (name.find(".svg") == (name.length() - 4)) + { + svg_load_resize(name.c_str(), &buffer, &x, &y, width, height); + if (x != *width || y != *height) + buffer = ResizeA(buffer, x, y, *width, *height); + } + else +#endif buffer = ResizeA(buffer, x, y, *width, *height); else buffer = Resize(buffer, x, y, *width, *height, COLOR); diff --git a/src/driver/pictureviewer/svg.cpp b/src/driver/pictureviewer/svg.cpp index 5fd80a4ff..264dae6e3 100644 --- a/src/driver/pictureviewer/svg.cpp +++ b/src/driver/pictureviewer/svg.cpp @@ -108,6 +108,62 @@ error: return 0; } +int svg_load_resize(const char *name, unsigned char **buffer, int* ox, int* oy, int* dx, int* dy); + +int svg_load_resize(const char *name, unsigned char **buffer, int* ox, int* oy, int* dx, int* dy) +{ + NSVGimage *image = NULL; + NSVGrasterizer *rast = NULL; + int w, h; + + //printf("[SVG] parsing load %s\n", name); + image = nsvgParseFromFile(name, "px", 96.0f); + if (image == NULL) + { + printf("[SVG] Could not open SVG image.\n"); + goto error; + } + w = (int)image->width; + h = (int)image->height; + + rast = nsvgCreateRasterizer(); + if (rast == NULL) + { + printf("[SVG] Could not init rasterizer.\n"); + goto error; + } + + float scale,scale_w,scale_h; + scale_w = *dx/w; + scale_h = *dy/h; + + scale = std::max(scale_w,scale_h); + + w = (int)(w*scale); + h = (int)(h*scale); + + free(*buffer); + *buffer = (unsigned char*) malloc(w*h*4); + + *ox = w; + *oy = h; + + if (buffer == NULL) + { + printf("[SVG] Could not alloc image buffer.\n"); + goto error; + } + + //printf("[SVG] rasterizing image %d x %d\n", w, h); + nsvgRasterize(rast, image, 0, 0, scale, *buffer, w, h, w*4); + +error: + nsvgDeleteRasterizer(rast); + nsvgDelete(image); + + return 0; +} + int fh_svg_getsize(const char *name,int *x,int *y, int /*wanted_width*/, int /*wanted_height*/) { NSVGimage *image = NULL;