-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.c
91 lines (68 loc) · 1.83 KB
/
image.c
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
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <propidl.h>
#include <gdiplus.h>
#include <assert.h>
#include "thetisskinmaker.h"
CLSID g_pngEncoder = {0};
HBITMAP CreateBitmapFromPath(LPWSTR path)
{
GdiplusStartupInput startup = {0};
GpBitmap *gdiBmp;
ULONG_PTR token;
HBITMAP bitmap = NULL;
int ret = 0;
startup.GdiplusVersion = 1;
ret = GdiplusStartup(&token, &startup, NULL);
assert(ret == 0 && "GDI+ failed to initialize. This should not happen.");
GdipCreateBitmapFromFile(path, &gdiBmp);
if(gdiBmp)
{
GdipCreateHBITMAPFromBitmap(gdiBmp, &bitmap, 0x00FFFFFF);
GdipDisposeImage((GpImage *) gdiBmp);
}
GdiplusShutdown(token);
return bitmap;
}
static CLSID
GetEncoderForFormat(LPWSTR format)
{
ImageCodecInfo *info;
UINT num = 0, size = 0;
GdipGetImageEncodersSize(&num, &size);
if(!size)
return CLSID_NULL;
info = malloc(size);
assert(info != NULL);
GdipGetImageEncoders(num, size, info);
for(int i = 0; i < num; i++)
{
if(wcscmp(info[i].MimeType, format) == 0)
return info[i].Clsid;
}
return CLSID_NULL;
}
int SaveBitmapToFile(HBITMAP bitmap, LPWSTR path)
{
GdiplusStartupInput startup = {0};
ULONG_PTR token;
GpBitmap *gdiBmp;
int ret = 0;
startup.GdiplusVersion = 1;
ret = GdiplusStartup(&token, &startup, NULL);
/* you better work if we already loaded the image */
assert(ret == 0);
ret = GdipCreateBitmapFromHBITMAP(bitmap, NULL, &gdiBmp);
if(ret != 0)
return ret;
if(IsEqualCLSID(&g_pngEncoder, &CLSID_NULL))
/* we will always have to use PNG */
g_pngEncoder = GetEncoderForFormat(L"image/png");
assert(!IsEqualCLSID(&g_pngEncoder, &CLSID_NULL));
ret = GdipSaveImageToFile(gdiBmp, path, &g_pngEncoder, NULL);
if(ret)
return ret;
GdipDisposeImage((GpImage *) gdiBmp);
GdiplusShutdown(token);
return 0;
}