This repository has been archived by the owner on Dec 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImage.h
89 lines (76 loc) · 2.03 KB
/
Image.h
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
#ifndef IMAGE_H
#define IMAGE_H
#include "includes.h"
class ImageBuilder {
private:
IWICImagingFactory& imagingFactory;
IWICBitmapDecoder* decoder = nullptr;
IWICBitmapFrameDecode* src = nullptr;
IWICFormatConverter* converter = nullptr;
IWICBitmapScaler* scaler = nullptr;
IWICBitmapSource* res = nullptr;
public:
ImageBuilder(LPCWSTR uri, IWICImagingFactory* imf): imagingFactory(*imf) {
HRESULT hr;
hr = imagingFactory.CreateDecoderFromFilename(
uri, nullptr, GENERIC_READ,
WICDecodeMetadataCacheOnLoad, &decoder
);
CHECK;
hr = decoder->GetFrame(0, &src);
CHECK;
res = src;
}
ImageBuilder& convert(WICPixelFormatGUID format = GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherType dType = WICBitmapDitherTypeNone,
WICBitmapPaletteType pType = WICBitmapPaletteTypeMedianCut) {
WICPixelFormatGUID pf;
res->GetPixelFormat(&pf);
if (pf != format) {
HRESULT hr;
hr = imagingFactory.CreateFormatConverter(&converter);
CHECK;
hr = converter->Initialize(res, format, dType, nullptr, 0.f, pType);
CHECK;
res = converter;
}
return *this;
}
ImageBuilder& scale(UINT dWidth, UINT dHeight = 0,
WICBitmapInterpolationMode iMode = WICBitmapInterpolationModeLinear) {
UINT w, h;
res->GetSize(&w, &h);
if ((dWidth != w || dHeight != h) && (dWidth != 0 || dHeight != 0)) {
if (dWidth == 0)
dWidth = w * dHeight / h;
else if (dHeight == 0)
dHeight = h * dWidth / w;
HRESULT hr;
hr = imagingFactory.CreateBitmapScaler(&scaler);
CHECK;
hr = scaler->Initialize(res, dWidth, dHeight, iMode);
CHECK;
res = scaler;
}
return *this;
}
ID2D1Bitmap* to(ID2D1RenderTarget* renderTarget) {
ID2D1Bitmap* bmp;
HRESULT hr = renderTarget->CreateBitmapFromWicBitmap(res, &bmp);
CHECK;
return bmp;
}
ID2D1Bitmap* to(ID2D1RenderTarget& renderTarget) {
ID2D1Bitmap* bmp;
HRESULT hr = renderTarget.CreateBitmapFromWicBitmap(res, &bmp);
CHECK;
return bmp;
}
~ImageBuilder() {
release(decoder);
release(src);
release(converter);
release(scaler);
}
};
#endif // !IMAGE_H