-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
168 lines (148 loc) · 4.44 KB
/
main.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <windows.h>
#include <stdio.h>
#include <string>
using namespace std;
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment (lib, "gdiplus.lib")
static void
printStatusError(Status status) {
switch (status) {
case GenericError:
fprintf(stderr, "Error: GenericError\n");
break;
case InvalidParameter:
fprintf(stderr, "Error: InvalidParameter\n");
break;
case OutOfMemory:
fprintf(stderr, "Error: OutOfMemory\n");
break;
case ObjectBusy:
fprintf(stderr, "Error: ObjectBusy\n");
break;
case InsufficientBuffer:
fprintf(stderr, "Error: InsufficientBuffer\n");
break;
case NotImplemented:
fprintf(stderr, "Error: NotImplemented\n");
break;
case Win32Error:
fprintf(stderr, "Error: Win32Error\n");
break;
case WrongState:
fprintf(stderr, "Error: WrongState\n");
break;
case Aborted:
fprintf(stderr, "Error: Aborted\n");
break;
case FileNotFound:
fprintf(stderr, "Error: FileNotFound\n");
break;
case ValueOverflow:
fprintf(stderr, "Error: ValueOverflow\n");
break;
case AccessDenied:
fprintf(stderr, "Error: AccessDenied\n");
break;
case UnknownImageFormat:
fprintf(stderr, "Error: UnknownImageFormat\n");
break;
case FontFamilyNotFound:
fprintf(stderr, "Error: FontFamilyNotFound\n");
break;
case FontStyleNotFound:
fprintf(stderr, "Error: FontStyleNotFound\n");
break;
case NotTrueTypeFont:
fprintf(stderr, "Error: NotTrueTypeFont\n");
break;
case UnsupportedGdiplusVersion:
fprintf(stderr, "Error: UnsupportedGdiplusVersion\n");
break;
case GdiplusNotInitialized:
fprintf(stderr, "Error: GdiplusNotInitialized\n");
break;
case PropertyNotFound:
fprintf(stderr, "Error: PropertyNotFound\n");
break;
case PropertyNotSupported:
fprintf(stderr, "Error: PropertyNotSupported\n");
break;
default:
fprintf(stderr, "Error: Unknown Error\n");
}
}
static unsigned int
usage() {
fprintf(stderr, "\nUsage:\n wmf2png.exe INPUTFILE.WMF OUTPUTFILE.PNG\n");
return 1;
}
static wchar_t *
char2wchar(const char * text) {
const size_t size = strlen(text) + 1;
wchar_t *wText = new wchar_t[size];
size_t convertedChars = 0;
mbstowcs_s(&convertedChars, wText, size, text, _TRUNCATE);
return wText;
}
static int
GetEncoderClsid(const wchar_t * format, CLSID* pClsid) {
unsigned int num = 0;
unsigned int size = 0;
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if (size == 0) {
fprintf(stderr, "Fatal: GetImageEncodersSize failed\n");
return -1;
}
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if (pImageCodecInfo == NULL) {
fprintf(stderr, "Fatal: malloc failed\n");
return -1;
}
GetImageEncoders(num, size, pImageCodecInfo);
for (unsigned int i = 0; i < num; ++i) {
if (wcscmp(pImageCodecInfo[i].MimeType, format) == 0) {
*pClsid = pImageCodecInfo[i].Clsid;
free(pImageCodecInfo);
return i;
}
}
free(pImageCodecInfo);
fprintf(stderr, "Fatal: Unable to find encoder for format %ws\n", format);
return -1;
}
int
main(int argc, char ** argv) {
GdiplusStartupInput gdiplusStartupInput;
unsigned long gdiplusToken;
CLSID encoderClsid;
Status status;
int getEncoderResult;
int exitCode = 0;
if (argc < 3) {
fprintf(stderr, "Error: You must supply an input WMF file and an output PNG file.\n");
return usage();
}
status = GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
if (status != Ok) {
fprintf(stderr, "GdiplusStartup failed\n");
printStatusError(status);
return 1;
}
getEncoderResult = GetEncoderClsid(L"image/png", &encoderClsid);
if (getEncoderResult >= 0) {
Metafile input(char2wchar(argv[1]));
status = input.Save(char2wchar(argv[2]), &encoderClsid, NULL);
if (status != Ok) {
fprintf(stderr, "Failed to save transcoded PNG file\n");
printStatusError(status);
exitCode = 2;
}
} else {
fprintf(stderr, "Failed to transcode WMF file\n");
exitCode = 3;
}
GdiplusShutdown(gdiplusToken);
return exitCode;
}