| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
|
| 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 |
} |