1 //
2 // Copyright (c) 2013 Mikko Mononen memon@inside.org
3 //
4 // This software is provided 'as-is', without any express or implied
5 // warranty. In no event will the authors be held liable for any damages
6 // arising from the use of this software.
7 // Permission is granted to anyone to use this software for any purpose,
8 // including commercial applications, and to alter it and redistribute it
9 // freely, subject to the following restrictions:
10 // 1. The origin of this software must not be misrepresented; you must not
11 // claim that you wrote the original software. If you use this software
12 // in a product, an acknowledgment in the product documentation would be
13 // appreciated but is not required.
14 // 2. Altered source versions must be plainly marked as such, and must not be
15 // misrepresented as being the original software.
16 // 3. This notice may not be removed or altered from any source distribution.
17 //
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <float.h>
22 #define STB_IMAGE_WRITE_IMPLEMENTATION
23 #include "stb_image_write.h"
24 #define NANOSVG_IMPLEMENTATION
25 #include "nanosvg.h"
26 #define NANOSVGRAST_IMPLEMENTATION
27 #include "nanosvgrast.h"
28
main()29 int main()
30 {
31 NSVGimage *image = NULL;
32 NSVGrasterizer *rast = NULL;
33 unsigned char* img = NULL;
34 int w, h;
35 const char* filename = "../example/23.svg";
36
37 printf("parsing %s\n", filename);
38 image = nsvgParseFromFile(filename, "px", 96.0f);
39 if (image == NULL) {
40 printf("Could not open SVG image.\n");
41 goto error;
42 }
43 w = (int)image->width;
44 h = (int)image->height;
45
46 rast = nsvgCreateRasterizer();
47 if (rast == NULL) {
48 printf("Could not init rasterizer.\n");
49 goto error;
50 }
51
52 img = malloc(w*h*4);
53 if (img == NULL) {
54 printf("Could not alloc image buffer.\n");
55 goto error;
56 }
57
58 printf("rasterizing image %d x %d\n", w, h);
59 nsvgRasterize(rast, image, 0,0,1, img, w, h, w*4);
60
61 printf("writing svg.png\n");
62 stbi_write_png("svg.png", w, h, 4, img, w*4);
63
64 error:
65 nsvgDeleteRasterizer(rast);
66 nsvgDelete(image);
67
68 return 0;
69 }
70