summaryrefslogtreecommitdiff
path: root/plugins/NewStory/src/TxDIB.h
blob: 2b8212d228b0f54a7b519c13c3147cfddee7f2a3 (plain)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#pragma once

#include <Uxtheme.h>
#include <math.h>
#include <vector>
#include <memory>
#include <algorithm>

#define JPEG_QUALITY_SUPER		0
#define JPEG_QUALITY_GOOD		1
#define JPEG_QUALITY_NORMAL		2
#define JPEG_QUALITY_AVERAGE	3
#define JPEG_QUALITY_BAD		4

class CTxDIB
{
	LPRGBQUAD	m_bits;
	int			m_width;
	int			m_height;
	BYTE		m_maxAlpha;
public:
	CTxDIB(void);
	CTxDIB(LPCWSTR fileName);
	CTxDIB(const CTxDIB& val);
	virtual ~CTxDIB(void);

	void operator=(const CTxDIB& val);

	BOOL	load(LPCWSTR fileName);
	BOOL	load(HRSRC hRes, HMODULE hModule = NULL);
	BOOL	load(LPBYTE data, DWORD size);
	BOOL	savePNG(LPCWSTR fileName, int dpi = 96);
	BOOL	saveJPG(LPCWSTR fileName, int quality = JPEG_QUALITY_GOOD, int dpi = 96);
	BOOL	saveBMP(LPCWSTR fileName);

	BOOL	destroy();
	BOOL	draw(HDC hdc, int x, int y, int cx = -1, long cy = -1);
	BOOL	draw(HDC hdc, LPCRECT rcDraw);
	BOOL	createFromHBITMAP(HBITMAP bmp);
	BOOL	createFromHICON(HICON ico, BOOL fixAlpha); /* default fixAlpha=TRUE, for shell icons set is to FALSE */

	HBITMAP createBitmap( HDC hdc = NULL );
	void	setTransColor(COLORREF clr);
	void	resample( int newWidth, int newHeight, CTxDIB* dst = NULL );
	void	tile(HDC hdc, LPRECT rcDraw, LPRECT rcClip = NULL);
	int		getWidth();
	int		getHeight();
	void	crop(int left, int top, int right, int bottom, CTxDIB* dst = NULL);
	void	crop(LPCRECT rcCrop, CTxDIB* dst = NULL);
	BOOL	isValid();
	void	setMaxAlpha(BYTE alpha);
	BYTE	getMaxAlpha();
	void	colorize(COLORREF clr);
	BOOL	calcAlpha(CTxDIB* imgWhite, CTxDIB* imgBlack);
	LPRGBQUAD	getBits() { return m_bits; }
	void	PreMulRGBA(RGBQUAD& color);
	void	rotateLeft(CTxDIB* dst = NULL);
	void	rotateRight(CTxDIB* dst = NULL);
	void	_copy( LPRGBQUAD newBits, int newWidth, int newHeight, BOOL copyBits = FALSE );
	BOOL	attach( LPVOID dib );
	void	PreMultiplyWithAlpha();
private:
	void	OverflowCoordinates(float &x, float &y);
	RGBQUAD GetPixelColorWithOverflow(long x, long y);
	RGBQUAD GetAreaColorInterpolated(float const xc, float const yc, float const w, float const h);
	void	AddAveragingCont(RGBQUAD const &color, float const surf, float &rr, float &gg, float &bb, float &aa);
	RGBQUAD GetPixelColorInterpolated(float x,float y);
	void	resample2( int newWidth, int newHeight, CTxDIB* dst = NULL );
	bool	QIShrink( int newWidth, int newHeight, CTxDIB* dst = NULL );

	void	_copy( const CTxDIB& val );
};

class CTxDibSet
{
	typedef std::vector<CTxDIB*>	imgCols;
	
	std::vector<imgCols>	m_items;
	int						m_width;
	int						m_height;
	int						m_cols;
	int						m_rows;
public:
	CTxDibSet(CTxDIB* img, int rows, int cols);
	~CTxDibSet();
	CTxDIB* get(int col, int row) { return m_items[row][col]; }


	int width()		{ return m_width;	}
	int height()	{ return m_height;	}
	int rows()		{ return m_rows;	}
	int cols()		{ return m_cols;	}
};

class CTxSkinDIB
{
	CTxDIB	m_dibLeftTop;
	CTxDIB	m_dibTop;
	CTxDIB	m_dibRightTop;

	CTxDIB	m_dibLeftCenter;
	CTxDIB	m_dibCenter;
	CTxDIB	m_dibRightCenter;

	CTxDIB	m_dibLeftBottom;
	CTxDIB	m_dibBottom;
	CTxDIB	m_dibRightBottom;

	MARGINS	m_margins;
	BOOL	m_tileX;
	BOOL	m_tileY;
public:
	CTxSkinDIB();
	virtual ~CTxSkinDIB();

	BOOL load(LPCWSTR fileName, MARGINS* mg, BOOL tileX, BOOL tileY);
	BOOL load(CTxDIB* dib, MARGINS* mg, BOOL tileX, BOOL tileY);

	void draw(HDC hdc, LPRECT rcDraw, LPRECT rcClip);
};

//***bd*** simple floating point point
class CTxDibPoint2
{
public:
  CTxDibPoint2();
  CTxDibPoint2(float const x_, float const y_);
  CTxDibPoint2(CTxDibPoint2 const &p);

  float Distance(CTxDibPoint2 const p2);
  float Distance(float const x_, float const y_);

  float x,y;
};

//and simple rectangle
class CTxDibRect2
{
public:
  CTxDibRect2();
  CTxDibRect2(float const x1_, float const y1_, float const x2_, float const y2_);
  CTxDibRect2(CTxDibPoint2 const &bl, CTxDibPoint2 const &tr);
  CTxDibRect2(CTxDibRect2 const &p);

  float Surface() const;
  CTxDibRect2 CrossSection(CTxDibRect2 const &r2);
  CTxDibPoint2 Center();
  float Width();
  float Height();

  CTxDibPoint2 botLeft;
  CTxDibPoint2 topRight;
};


inline RGBQUAD CTxDIB::GetPixelColorWithOverflow(long x, long y)
{
	if (!(0 <= y && y < m_height && 0 <= x &&  x < m_width))
	{
		x = std::max((int) x, 0); 
		x = std::min((int) x, m_width - 1);
		y = std::max((int) y, 0);
		y = std::min((int) y, m_height - 1);
	}
	return m_bits[y * m_width + x];
}

inline void CTxDIB::OverflowCoordinates( float &x, float &y )
{
	if (x >= 0 && x < m_width && y >= 0 && y < m_height) 
	{
		return;
	}
	x = std::max(x, 0.0f);
	x = std::min(x, (float) m_width - 1);
	y = std::max(y, 0.0f);
	y = std::min(y, (float) m_height - 1);
}

inline void	CTxDIB::AddAveragingCont(RGBQUAD const &color, float const surf, float &rr, float &gg, float &bb, float &aa)
{
	rr += color.rgbRed		* surf;
	gg += color.rgbGreen	* surf;
	bb += color.rgbBlue		* surf;
	aa += color.rgbReserved	* surf;
}


inline void CTxDIB::PreMulRGBA(RGBQUAD& color)
{
	color.rgbRed		= (color.rgbRed		* color.rgbReserved) / 255;
	color.rgbGreen		= (color.rgbGreen	* color.rgbReserved) / 255;
	color.rgbBlue		= (color.rgbBlue	* color.rgbReserved) / 255;
}

inline int CTxDIB::getWidth()	
{ 
	return m_width;  
}

inline int CTxDIB::getHeight()	
{ 
	return m_height; 
}


inline void CTxDIB::crop(LPCRECT rcCrop, CTxDIB* dst /*= NULL*/) 
{ 
	crop(rcCrop->left, rcCrop->top, rcCrop->right, rcCrop->bottom, dst); 
}

inline BOOL CTxDIB::isValid()	
{ 
	return m_bits ? TRUE : FALSE; 
}

inline void CTxDIB::setMaxAlpha(BYTE alpha)
{ 
	m_maxAlpha = alpha; 
}

inline BYTE CTxDIB::getMaxAlpha()
{ 
	return m_maxAlpha;
}

inline BOOL CTxDIB::draw(HDC hdc, LPCRECT rcDraw)
{ 
	return draw(hdc, rcDraw->left, rcDraw->top, rcDraw->right - rcDraw->left, rcDraw->bottom - rcDraw->top); 
}

inline void CTxDIB::operator=(const CTxDIB& val) 
{ 
	_copy(val); 
}


inline CTxDibPoint2::CTxDibPoint2()
{
  x=y=0.0f;
}

inline CTxDibPoint2::CTxDibPoint2(float const x_, float const y_)
{
  x=x_;
  y=y_;
}

inline CTxDibPoint2::CTxDibPoint2(CTxDibPoint2 const &p)
{
  x=p.x;
  y=p.y;
}

inline float CTxDibPoint2::Distance(CTxDibPoint2 const p2)
{
  return (float)sqrt((x-p2.x)*(x-p2.x)+(y-p2.y)*(y-p2.y));
}

inline float CTxDibPoint2::Distance(float const x_, float const y_)
{
  return (float)sqrt((x-x_)*(x-x_)+(y-y_)*(y-y_));
}

inline CTxDibRect2::CTxDibRect2()
{
}

inline CTxDibRect2::CTxDibRect2(float const x1_, float const y1_, float const x2_, float const y2_)
{
  botLeft.x=x1_;
  botLeft.y=y1_;
  topRight.x=x2_;
  topRight.y=y2_;
}

inline CTxDibRect2::CTxDibRect2(CTxDibRect2 const &p)
{
  botLeft=p.botLeft;
  topRight=p.topRight;
}

inline float CTxDibRect2::Surface() const
/*
 * Returns the surface of rectangle.
 */
{
  return (topRight.x-botLeft.x)*(topRight.y-botLeft.y);
}

inline CTxDibRect2 CTxDibRect2::CrossSection(CTxDibRect2 const &r2)
{
  CTxDibRect2 cs;
  cs.botLeft.x =std::max(botLeft.x, r2.botLeft.x);
  cs.botLeft.y = std::max(botLeft.y, r2.botLeft.y);
  cs.topRight.x = std::min(topRight.x, r2.topRight.x);
  cs.topRight.y = std::min(topRight.y, r2.topRight.y);
  if (cs.botLeft.x<=cs.topRight.x && cs.botLeft.y<=cs.topRight.y) {
    return cs;
  } else {
    return CTxDibRect2(0,0,0,0);
  }//if
}

inline CTxDibPoint2 CTxDibRect2::Center()
{
  return CTxDibPoint2((topRight.x+botLeft.x)/2.0f, (topRight.y+botLeft.y)/2.0f);
}

inline float CTxDibRect2::Width()
{
  return topRight.x-botLeft.x;
}

inline float CTxDibRect2::Height()
{
  return topRight.y-botLeft.y;
}