This repository was archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImage_GDIPlus.cpp
More file actions
154 lines (136 loc) · 7.04 KB
/
Image_GDIPlus.cpp
File metadata and controls
154 lines (136 loc) · 7.04 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
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
/*******************************************************************************
*******************************************************************************
* Copyright (c) 2009-2023 ectotropic (ectotropic@gmail.com, *
* https://github.com/ectotropic) *
* *
* This program is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation, either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************
******************************************************************************/
//--------------------------------------
//
#include "CommonHeaders.h"
#include "Image_GDIPlus.h"
//--------------------------------------
//--------------------------------------
//
#include "Windows/GDIPlus/GDIPlus_Bitmap.h"
//--------------------------------------
namespace Image {
//**************************************************************************
// GDIPlusImage <static>
//**************************************************************************
GDIPlusImage::pointer GDIPlusImage::NewImage(_In_ HDC hDC,
_In_ native_coord_type width,
_In_ native_coord_type height,
_In_ const void* data) noexcept {
// BUG: This should be done globally; as a static it may cause problems
// on destruction (as this may happen when the required info no
// longer exists). No errors have occurred so far however!
static const ::Windows::GDIPlus::ScopedStartup s_ScopedGDIPlusStartup{};
try {
graphics_pointer pGraphics{ ::Gdiplus::Graphics::FromHDC(hDC) };
assert(pGraphics);
WinGDIPlusAssertStatus(pGraphics);
if (!pGraphics) { return {}; }
if (pGraphics->GetLastStatus() != ::Gdiplus::Ok) { return {}; }
pGraphics->SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic);
pointer pImage {};
{
pImage.reset(new GDIPlusImage{ width, height, std::move(pGraphics) });
pImage->m_pBitmap.reset(new ::Gdiplus::Bitmap{ width, height });
}
if (pImage->m_pBitmap->GetLastStatus() != ::Gdiplus::Ok) { return {}; }
::Windows::GDIPlus::ScopedBitmapLockBits lock_{
*pImage->m_pBitmap,
Gdiplus::ImageLockModeWrite,
PixelFormat32bppARGB
};
if (lock_) {
const auto bitmapLock{ lock_.GetData() };
std::memcpy(bitmapLock.Scan0, data,
width * height * 4);
return pImage;
}
} catch (...) {
assert(false);
}
return {};
}
//--------------------------------------------------------------------------
GDIPlusImage::pointer GDIPlusImage::NewImage(_In_ HDC hDC,
_In_ const data_type& data) noexcept {
if (!data) { return {}; }
return NewImage(hDC, data.GetWidth(),
data.GetHeight(), data.GetPixels());
}
//**************************************************************************
// GDIPlusImage
//**************************************************************************
GDIPlusImage::GDIPlusImage(native_extent_type width,
native_extent_type height,
graphics_pointer&& pGraphics) noexcept :
base_type{ static_cast<extent_type>(width), static_cast<extent_type>(height) },
m_pGraphics{ std::forward<graphics_pointer>(pGraphics) } {
WinGDIPlusAssertStatus(m_pGraphics);
}
//--------------------------------------------------------------------------
void GDIPlusImage::Destroy() noexcept {
if (m_pBitmap) { m_pBitmap.reset(); }
if (m_pGraphics) { m_pGraphics.reset(); }
base_type::Destroy();
}
//--------------------------------------------------------------------------
void GDIPlusImage::Draw(_In_ coord_type x0, _In_ coord_type y0,
_In_ coord_type x1, _In_ coord_type y1) const noexcept {
const auto dstWidth = x1 - x0;
const auto dstHeight = y1 - y0;
try {
m_pGraphics->DrawImage(m_pBitmap.get(), x0, y0, dstWidth, dstHeight);
} catch (...) {
assert(false);
}
}
//--------------------------------------------------------------------------
void GDIPlusImage::Blend(_In_ coord_type x0, _In_ coord_type y0,
_In_ coord_type x1, _In_ coord_type y1,
_In_ float alpha) const noexcept {
try {
// https://www.codeproject.com/Articles/9778/Alpha-Blending-using-GDI
// Diagonal values are R, G, B, A, w
const auto gdiAlpha{ static_cast<Gdiplus::REAL>(alpha) };
const Gdiplus::ColorMatrix matrix{
1.f, 0.f, 0.f, 0.f, 0.f,
0.f, 1.f, 0.f, 0.f, 0.f,
0.f, 0.f, 1.f, 0.f, 0.f,
0.f, 0.f, 0.f, gdiAlpha, 0.f,
0.f, 0.f, 0.f, 0.f, 1.f
};
Gdiplus::ImageAttributes ImgAttr{};
ImgAttr.SetColorMatrix(&matrix,
Gdiplus::ColorMatrixFlagsDefault,
Gdiplus::ColorAdjustTypeBitmap);
const auto dstWidth = x1 - x0;
const auto dstHeight = y1 - y0;
m_pGraphics->DrawImage(m_pBitmap.get(),
Gdiplus::Rect{ x0, y0, dstWidth, dstHeight },
0, 0,
m_iWidth, m_iHeight,
Gdiplus::UnitPixel,
&ImgAttr);
} catch (...) {
assert(false);
}
}
} // namespace Image