Blender V5.0
format_svg.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
13
15#include "IMB_filetype.hh"
16#include "IMB_imbuf_types.hh"
17#include "nanosvgrast.h"
18
20 const int /*flags*/,
21 const size_t max_thumb_size,
22 ImFileColorSpace & /*r_colorspace*/,
23 size_t *r_width,
24 size_t *r_height)
25{
26 NSVGimage *image = nsvgParseFromFile(filepath, "px", 96.0f);
27
28 if (image == nullptr) {
29 return nullptr;
30 }
31
32 if (image->width == 0 || image->height == 0) {
33 nsvgDelete(image);
34 return nullptr;
35 }
36
37 int w = int(image->width);
38 int h = int(image->height);
39
40 /* Return full size of the image. */
41 *r_width = size_t(w);
42 *r_height = size_t(h);
43
44 NSVGrasterizer *rast = nsvgCreateRasterizer();
45 if (rast == nullptr) {
46 nsvgDelete(image);
47 return nullptr;
48 }
49
50 const float scale = float(max_thumb_size) / std::max(w, h);
51 const int dest_w = std::max(int(w * scale), 1);
52 const int dest_h = std::max(int(h * scale), 1);
53
54 ImBuf *ibuf = IMB_allocImBuf(dest_w, dest_h, 32, IB_byte_data);
55 if (ibuf != nullptr) {
56 nsvgRasterize(rast, image, 0, 0, scale, ibuf->byte_buffer.data, dest_w, dest_h, dest_w * 4);
57 IMB_flipy(ibuf);
58 }
59
60 nsvgDeleteRasterizer(rast);
61 nsvgDelete(image);
62
63 return ibuf;
64}
void IMB_flipy(ImBuf *ibuf)
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, unsigned char planes, unsigned int flags)
@ IB_byte_data
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
nullptr float
ImBuf * imb_load_filepath_thumbnail_svg(const char *filepath, const int, const size_t max_thumb_size, ImFileColorSpace &, size_t *r_width, size_t *r_height)
Definition format_svg.cc:19
ImBufByteBuffer byte_buffer