You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.9KB

  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. #include <stdio.h>
  19. #include <string.h>
  20. #include <float.h>
  21. #define STB_IMAGE_WRITE_IMPLEMENTATION
  22. #include "stb_image_write.h"
  23. #define NANOSVG_IMPLEMENTATION
  24. #include "nanosvg.h"
  25. #define NANOSVGRAST_IMPLEMENTATION
  26. #include "nanosvgrast.h"
  27. int main()
  28. {
  29. NSVGimage *image = NULL;
  30. NSVGrasterizer *rast = NULL;
  31. unsigned char* img = NULL;
  32. int w, h;
  33. const char* filename = "../example/23.svg";
  34. printf("parsing %s\n", filename);
  35. image = nsvgParseFromFile(filename, "px", 96.0f);
  36. if (image == NULL) {
  37. printf("Could not open SVG image.\n");
  38. goto error;
  39. }
  40. w = (int)image->width;
  41. h = (int)image->height;
  42. rast = nsvgCreateRasterizer();
  43. if (rast == NULL) {
  44. printf("Could not init rasterizer.\n");
  45. goto error;
  46. }
  47. img = malloc(w*h*4);
  48. if (img == NULL) {
  49. printf("Could not alloc image buffer.\n");
  50. goto error;
  51. }
  52. printf("rasterizing image %d x %d\n", w, h);
  53. nsvgRasterize(rast, image, 0,0,1, img, w, h, w*4);
  54. printf("writing svg.png\n");
  55. stbi_write_png("svg.png", w, h, 4, img, w*4);
  56. error:
  57. nsvgDeleteRasterizer(rast);
  58. nsvgDelete(image);
  59. return 0;
  60. }