Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nanosvg utility binary #158

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 32 additions & 28 deletions example/example2.c → example/nanosvg.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,44 +26,48 @@
#define NANOSVGRAST_IMPLEMENTATION
#include "nanosvgrast.h"

int main()
int main(int argc, char * argv[])
{
NSVGimage *image = NULL;
NSVGrasterizer *rast = NULL;
unsigned char* img = NULL;
int w, h;
const char* filename = "../example/23.svg";
const char* filename;
char outfile[4096];

printf("parsing %s\n", filename);
image = nsvgParseFromFile(filename, "px", 96.0f);
if (image == NULL) {
printf("Could not open SVG image.\n");
goto error;
}
w = (int)image->width;
h = (int)image->height;
for (int argi = 1; argi < argc; argi++) {
filename = argv[argi];
printf("parsing %s\n", filename);
image = nsvgParseFromFile(filename, "px", 96.0f);
if (image == NULL) {
printf("Could not open SVG image.\n");
goto error;
}
w = (int)image->width;
h = (int)image->height;

rast = nsvgCreateRasterizer();
if (rast == NULL) {
printf("Could not init rasterizer.\n");
goto error;
}
rast = nsvgCreateRasterizer();
if (rast == NULL) {
printf("Could not init rasterizer for '%s'.\n", filename);
goto error;
}

img = malloc(w*h*4);
if (img == NULL) {
printf("Could not alloc image buffer.\n");
goto error;
}
img = malloc(w*h*4);
if (img == NULL) {
printf("Could not alloc image buffer for '%s'.\n", filename);
goto error;
}

printf("rasterizing image %d x %d\n", w, h);
nsvgRasterize(rast, image, 0,0,1, img, w, h, w*4);
printf("rasterizing image %d x %d\n", w, h);
nsvgRasterize(rast, image, 0,0,1, img, w, h, w*4);
snprintf(outfile, sizeof(outfile), "%s.png", filename);
printf("writing %s\n", outfile);
stbi_write_png(outfile, w, h, 4, img, w*4);

printf("writing svg.png\n");
stbi_write_png("svg.png", w, h, 4, img, w*4);

error:
nsvgDeleteRasterizer(rast);
nsvgDelete(image);
error:
nsvgDeleteRasterizer(rast);
nsvgDelete(image);
}

return 0;
}
4 changes: 2 additions & 2 deletions premake4.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ solution "nanosvg"
defines { "NDEBUG" }
flags { "Optimize", "ExtraWarnings"}

project "example2"
project "nanosvg"
kind "ConsoleApp"
language "C++"
files { "example/example2.c", "example/*.h", "src/*.h" }
files { "example/nanosvg.c", "example/*.h", "src/*.h" }
includedirs { "example", "src" }
targetdir("build")

Expand Down