add cleaner resize for svg images

This commit is contained in:
TangoCash
2021-10-27 21:30:09 +02:00
committed by Thilo Graf
parent 445304a2ec
commit 2cc8464941
2 changed files with 66 additions and 0 deletions

View File

@@ -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);

View File

@@ -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;