-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage-win.cpp
More file actions
109 lines (95 loc) · 3.12 KB
/
Image-win.cpp
File metadata and controls
109 lines (95 loc) · 3.12 KB
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
#include "ConsoleGraphics.h"
#include <windows.h>
#include <gdiplus.h>
static bool bGdiplusStarted = false;
static void InitGDIPlus() {
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
// Initialize GDI+.
if (!bGdiplusStarted)
Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
bGdiplusStarted = true;
}
static int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
Gdiplus::ImageCodecInfo* pImageCodecInfo = NULL;
Gdiplus::GetImageEncodersSize(&num, &size);
if(size == 0)
return -1; // Failure
pImageCodecInfo = (Gdiplus::ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL)
return -1; // Failure
GetImageEncoders(num, size, pImageCodecInfo);
for (UINT j = 0; j < num; ++j)
{
if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j; // Success
}
}
free(pImageCodecInfo);
return -1; // Failure
}
static void toW(const char *string, WCHAR *dest, int destSize) {
MultiByteToWideChar(CP_ACP, 0, string, -1, dest, destSize);
}
Image::Image(const char *file, Pixel transparentColor) : width(0), height(0), pixelData(0), associatedPalette(0) {
WCHAR wfilename[1024];
// Load an image via GDI+
InitGDIPlus();
toW(file, wfilename, sizeof(wfilename));
Gdiplus::Bitmap image(wfilename);
if (image.GetWidth() == 0) {
fprintf(stderr, "Image %s could not be loaded\n", file);
throw "Image could not be loaded";
}
width = image.GetWidth();
height = image.GetHeight();
pixelData = new Pixel[height * width];
Gdiplus::Rect rt(0, 0, width, height);
Gdiplus::BitmapData data;
image.LockBits(&rt, Gdiplus::ImageLockModeRead, PixelFormat32bppARGB, &data);
uint32_t *ptr = (uint32_t*) data.Scan0;
for (unsigned j = 0; j < height; j++) {
for (unsigned i = 0; i < width; i++) {
if (ptr[i] == transparentColor.value)
pixelData[j * width + i] = 0;
else
pixelData[j * width + i] = ptr[i];
}
ptr = (uint32_t*) ((uint8_t*) ptr + data.Stride);
}
image.UnlockBits(&data);
}
void Image::writeToFile(const char *filename) const {
CLSID pngClsid;
WCHAR wfilename[1024];
Gdiplus::Bitmap bmp(width, height);
Gdiplus::Rect rt(0, 0, width, height);
Gdiplus::BitmapData data;
// Copy pixels
bmp.LockBits(&rt, Gdiplus::ImageLockModeWrite, PixelFormat32bppARGB, &data);
uint32_t *ptr = (uint32_t*) data.Scan0;
for (unsigned j = 0; j < height; j++) {
for (unsigned i = 0; i < width; i++) {
uint32_t pixel = pixelData[j * width + i].value;
if (associatedPalette) {
// Indexed
ptr[i] = associatedPalette->getColor(pixel).value;
} else {
// Normal
ptr[i] = pixel;
}
}
ptr = (uint32_t*) ((uint8_t*) ptr + data.Stride);
}
bmp.UnlockBits(&data);
// Save
toW(filename, wfilename, sizeof(wfilename));
GetEncoderClsid(L"image/png", &pngClsid);
bmp.Save(wfilename, &pngClsid, NULL);
}