Last active
September 14, 2022 02:30
-
-
Save lag945/783eaf72d1b896fcc2e1ac62af3da528 to your computer and use it in GitHub Desktop.
C++ exmaple convert svg to png with resvg library.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stb_image_write.h> | |
#include <resvg.h> | |
int main() | |
{ | |
resvg_options* opt = resvg_options_create(); | |
resvg_options_load_system_fonts(opt); | |
resvg_render_tree* tree; | |
int err = resvg_parse_tree_from_file("bboxes.svg", opt, &tree); | |
resvg_options_destroy(opt); | |
if (err == RESVG_OK) | |
{ | |
resvg_size size = resvg_get_image_size(tree); | |
int width = (int)size.width; | |
int height = (int)size.height; | |
resvg_fit_to fit_to = { RESVG_FIT_TO_TYPE_ORIGINAL, 1 }; | |
char* pData = new char[width * height * 4]; | |
memset(pData, 0, width * height * 4);//default alhpa=0 and color=black | |
resvg_render(tree, fit_to, resvg_transform_identity(), width, height, (char*)pData);//RGBA | |
err = stbi_write_png("bboxes.svg.png", width, height, 4, pData, width * 4); | |
delete[] pData; | |
} | |
else | |
{ | |
std::cout << "Error id: " << err << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment