summaryrefslogtreecommitdiff
path: root/plugins/Popup/src/bitmap_funcs.h
blob: 8a66c30f85ef6f40b3def8c9a6eed268cdbdbf93 (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
/*
Popup Plus plugin for Miranda IM

Copyright	© 2002 Luca Santarelli,
			© 2004-2007 Victor Pavlychko
			© 2010 MPK
			© 2010 Merlin_de

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

===============================================================================

File name      : $HeadURL: http://svn.miranda.im/mainrepo/popup/trunk/src/bitmap_funcs.h $
Revision       : $Revision: 1610 $
Last change on : $Date: 2010-06-23 00:55:13 +0300 (Ср, 23 июн 2010) $
Last change by : $Author: Merlin_de $

===============================================================================
*/

#ifndef __bitmap_funcs_h__
#define __bitmap_funcs_h__

// This should make bitmap manipulations much easier...
class MyBitmap
{
public:
	typedef unsigned long COLOR32;
	static inline COLOR32 RGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a = 0xff)
	{
		return (a << 24) | (r << 16) | (g << 8) | b;
	};

private:
	HBITMAP hBmpSave, hBmp;
	HDC dcBmp;
	COLOR32 *bits;
	COLOR32 *bitsSave;
	int width, height;

	void free();

	bool loadFromFile_pixel(const char *fn, const char *fnAlpha = 0);
	bool loadFromFile_gradient(const char *fn, const char *fnAlpha = 0);
	bool loadFromFile_png(const char *fn, const char *fnAlpha = 0);
	bool loadFromFile_default(const char *fn, const char *fnAlpha = 0);
	void premultipleChannels();

public:
	MyBitmap();
	MyBitmap(int w, int h);
	MyBitmap(const char *fn, const char *fnAlpha = 0);
	~MyBitmap();
	void allocate(int w, int h);

	bool loadFromFile(const char *fn, const char *fnAlpha = 0);

	int getWidth() { return width; }
	int getHeight() { return height; }

	HDC getDC() { return dcBmp; }
	HBITMAP getBitmap() { return hBmp; }

	void makeOpaque();
	void makeOpaqueRect(int x1, int y1, int x2, int y2);
	void makeOpaqueRect(RECT rc) { makeOpaqueRect(rc.left, rc.top, rc.right, rc.bottom);  }

	void saveAlpha(int x = 0, int y = 0, int w = 0, int h = 0);
	void restoreAlpha(int x = 0, int y = 0, int w = 0, int h = 0);

	void DrawBits(COLOR32 *inbits, int inw, int inh, int x, int y, int w, int h);
	void BlendBits(COLOR32 *inbits, int inw, int inh, int x, int y, int w, int h);

	void DrawNoAlpha(MyBitmap *bmp, int x, int y, int w, int h);

	void Blend(MyBitmap *bmp, int x, int y, int w, int h);
	void Draw(MyBitmap *bmp, int x, int y, int w, int h);

	void BlendColorized(MyBitmap *bmp, int x, int y, int w, int h, COLOR32 color);
	void DrawColorized(MyBitmap *bmp, int x, int y, int w, int h, COLOR32 color);

	void BlendPart(MyBitmap *bmp, int xin, int yin, int win, int hin, int x, int y, int w, int h);
	void BlendPartColorized(MyBitmap *bmp, int xin, int yin, int win, int hin, int x, int y, int w, int h, COLOR32 color);
	void DrawPart(MyBitmap *bmp, int xin, int yin, int win, int hin, int x, int y, int w, int h);
//	void DrawPartNoAlpha(MyBitmap *bmp, int x, int y, int w, int h);
//	void DrawPartColorized(MyBitmap *bmp, int x, int y, int w, int h, COLOR32 color);

	void DrawIcon(HICON hic, int x, int y, int w = 0, int h = 0);
	void Draw_TextA(char *str, int x, int y);
	void Draw_TextW(WCHAR *str, int x, int y);

	__forceinline COLOR32 *getBits() { return bits; }
	__forceinline COLOR32 *getRow(int row) { return bits + row * width; }
	__forceinline COLOR32 *operator[] (int row) { return bits + row * width; }

	static __forceinline COLOR32 rgba(COLOR32 r, COLOR32 g, COLOR32 b, COLOR32 a)
	{
		return ((a & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
	}
	static __forceinline COLOR32 getr(COLOR32 c)
	{
		return (c >> 16) & 0xff;
	}
	static __forceinline COLOR32 getg(COLOR32 c)
	{
		return (c >> 8) & 0xff;
	}
	static __forceinline COLOR32 getb(COLOR32 c)
	{
		return c & 0xff;
	}
	static __forceinline COLOR32 geta(COLOR32 c)
	{
		return (c >> 24) & 0xff;
	}

	HRGN buildOpaqueRgn(int level = 64, bool opaque = true);
};

#endif // __bitmap_funcs_h__