Blender V5.3
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
19namespace blender {
20
22 const ImBufFlags /*flags*/,
23 const size_t max_thumb_size,
24 ImFileColorSpace & /*r_colorspace*/,
25 size_t *r_width,
26 size_t *r_height)
27{
28 NSVGimage *image = nsvgParseFromFile(filepath, "px", 96.0f);
29
30 if (image == nullptr) {
31 return nullptr;
32 }
33
34 if (image->width == 0 || image->height == 0) {
35 nsvgDelete(image);
36 return nullptr;
37 }
38
39 int w = int(image->width);
40 int h = int(image->height);
41
42 /* Return full size of the image. */
43 *r_width = size_t(w);
44 *r_height = size_t(h);
45
46 NSVGrasterizer *rast = nsvgCreateRasterizer();
47 if (rast == nullptr) {
48 nsvgDelete(image);
49 return nullptr;
50 }
51
52 const float scale = float(max_thumb_size) / std::max(w, h);
53 const int dest_w = std::max(int(w * scale), 1);
54 const int dest_h = std::max(int(h * scale), 1);
55
56 ImBuf *ibuf = IMB_allocImBuf(dest_w, dest_h, ImBufFlags::ByteData);
57 if (ibuf != nullptr) {
58 nsvgRasterize(
59 rast, image, 0, 0, scale, ibuf->byte_data_for_write(), dest_w, dest_h, dest_w * 4);
60 IMB_flipy(ibuf);
61 }
62
63 nsvgDeleteRasterizer(rast);
64 nsvgDelete(image);
65
66 return ibuf;
67}
68
69} // namespace blender
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
float3x3 scale(float3x3 mat, float2 scale)
BMesh Iterators.
void IMB_flipy(ImBuf *ibuf)
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
ImBuf * imb_load_filepath_thumbnail_svg(const char *filepath, const ImBufFlags, const size_t max_thumb_size, ImFileColorSpace &, size_t *r_width, size_t *r_height)
Definition format_svg.cc:21
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60