forked from sammycage/lunasvg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg2png.cpp
69 lines (51 loc) · 1.67 KB
/
svg2png.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#include <iostream>
#include <sstream>
#include <lunasvg.h>
using namespace lunasvg;
int help()
{
std::cout << "Usage: \n"
" svg2png [filename] [resolution] [bgColor]\n\n"
"Examples: \n"
" $ svg2png input.svg\n"
" $ svg2png input.svg 512x512\n"
" $ svg2png input.svg 512x512 0xff00ffff\n\n";
return 1;
}
bool setup(int argc, char** argv, std::string& filename, std::uint32_t& width, std::uint32_t& height, std::uint32_t& bgColor)
{
if(argc > 1) filename.assign(argv[1]);
if(argc > 2) {
std::stringstream ss;
ss << argv[2];
ss >> width;
if(ss.fail() || ss.get() != 'x')
return false;
ss >> height;
}
if(argc > 3) {
std::stringstream ss;
ss << std::hex << argv[3];
ss >> std::hex >> bgColor;
}
return argc > 1;
}
int main(int argc, char** argv)
{
std::string filename;
std::uint32_t width = 0, height = 0;
std::uint32_t bgColor = 0x00000000;
if(!setup(argc, argv, filename, width, height, bgColor)) return help();
auto document = Document::loadFromFile(filename);
if(!document) return help();
auto bitmap = document->renderToBitmap(width, height, bgColor);
if(!bitmap.valid()) return help();
bitmap.convertToRGBA();
auto basename = filename.substr(filename.find_last_of("/\\") + 1);
basename.append(".png");
stbi_write_png(basename.c_str(), bitmap.width(), bitmap.height(), 4, bitmap.data(), 0);
std::cout << "Generated PNG file : " << basename << std::endl;
return 0;
}