File: IconMap.h

package info (click to toggle)
transcend 0.3.dfsg2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,784 kB
  • sloc: cpp: 26,886; ansic: 693; sh: 210; makefile: 99; perl: 67
file content (206 lines) | stat: -rw-r--r-- 4,126 bytes parent folder | download | duplicates (30)
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
// Jason Rohrer
// IconMap.h

/**
*
*	Simple 2d icon map
*
*
*	Created 12-12-99
*	Mods:
*		Jason Rohrer	12-13-99	Added numPixels member	
*		Jason Rohrer	3-22-2000	Added support for drawing icon maps into an icon map
*		Jason Rohrer	12-17-2000	Added include of Color.h
*/



#ifndef ICON_MAP_INCLUDED
#define ICON_MAP_INCLUDED

#include <string.h>
#include "Color.h"

class IconMap {

	
	public:
		
		IconMap( int w, int h);		// construct a map of a certain width and height
	
		IconMap( int w, int h, int *offset );
									// pass in precomputed y offsets into image map
									
		~IconMap();
		
		int wide;
		int high;
		int numPixels;
		
		int *yOffset;
		
		unsigned long *imageMap;
		
		
		// draw a solid IconMap into this IconMap
		void drawIconMap( IconMap *icon, int xPos, int yPos );
		
		// draw a transparent IconMap into this IconMap
		void drawIconMapAlpha( IconMap *icon, int xPos, int yPos );
		
		void copy( IconMap *icon );		// copy contents of another icon map into this one
										// does nothing if icon maps aren't the same size
		
	private:
		char yOffsetExternal;	// is the yOffset ptr external?
		
		float invChannelMax;
		Color utilColor;
		
	};
	
	
inline IconMap::IconMap( int w, int h ) {
	invChannelMax = 1 / 255.0;
	
	wide = w;
	high = h;
	numPixels = wide * high;
	
	imageMap = new unsigned long[ wide * high ];
	
	yOffset = new int[high];
	for( int y=0; y<high; y++ ) {		// precalc y offsets
		yOffset[y] = y * wide;
		}
	yOffsetExternal = false;
	}
	
inline IconMap::IconMap( int w, int h, int *offset ) {
	invChannelMax = 1 / 255.0;
	
	wide = w;
	high = h;
	numPixels = wide * high;
		
	imageMap = new unsigned long[ wide * high ];
	
	yOffset = offset;
	yOffsetExternal = true;
	}	

inline IconMap::~IconMap() {
	delete [] imageMap;
	
	if( !yOffsetExternal ) {
		delete [] yOffset;
		}
	}	



inline void IconMap::drawIconMap( IconMap *icon, int xPos, int yPos ) {
	
	unsigned long *image = icon->imageMap;
	int *imageYOffset = icon->yOffset;
	int imageWide = icon->wide;
	int imageHigh = icon->high;
	
	// watch for buffer bounds 
	int minY = yPos;
	if( minY < 0 ) {
		minY = 0;
		}
	int maxY = yPos + imageHigh;
	if( maxY > high ) {
		maxY = high;
		}
	int minX = xPos;
	if( minX < 0 ) {
		minX = 0;
		}
	int maxX = xPos + imageWide;
	if( maxX > wide ) {
		maxX = wide;
		}
	
	for( int y=minY; y<maxY; y++ ) {
		int imageY = y-yPos;
	
		int buffYContrib = yOffset[y];
		int imageYContrib = imageYOffset[imageY];
	
		for( int x=minX; x<maxX; x++ ) {
			int imageX = x-xPos;
			imageMap[ buffYContrib + x ] = image[ imageYContrib + imageX ];	
			}
		}
	}


inline void IconMap::drawIconMapAlpha( IconMap *icon, int xPos, int yPos ) {
	
	unsigned long *image = icon->imageMap;
	int *imageYOffset = icon->yOffset;
	int imageWide = icon->wide;
	int imageHigh = icon->high;
	
	// watch for buffer bounds 
	int minY = yPos;
	if( minY < 0 ) {
		minY = 0;
		}
	int maxY = yPos + imageHigh;
	if( maxY > high ) {
		maxY = high;
		}
	int minX = xPos;
	if( minX < 0 ) {
		minX = 0;
		}
	int maxX = xPos + imageWide;
	if( maxX > wide ) {
		maxX = wide;
		}

	for( int y=minY; y<maxY; y++ ) {
		int imageY = y-yPos;
		
		int buffYContrib = yOffset[y];
		int imageYContrib = imageYOffset[imageY];
		
		for( int x=minX; x<maxX; x++ ) {
			int imageX = x-xPos;
			// get alpha value for this icon pixel
			unsigned long argb = image[ imageYContrib + imageX ];
			float alpha = (argb >> 24) * invChannelMax;

			float oneMinusAlpha = 1-alpha;
			
			unsigned long buffARGB = imageMap[ buffYContrib + x ];
			
			argb = utilColor.getWeightedComposite( argb, alpha );
			buffARGB = utilColor.getWeightedComposite( buffARGB, oneMinusAlpha );
			
			unsigned long sum = utilColor.sumComposite( argb, buffARGB );
			
			imageMap[ buffYContrib + x ] = sum;
			}
		}
	}


inline void IconMap::copy( IconMap *icon ) {
	// make sure they are the same size
	if( numPixels != icon->numPixels ) {
		return;
		}
	
	// void * memcpy (void * dst, const void * src, size_t len);
	
	// each pixel is 4 bytes, so shift numPixels by 2
	memcpy( (void *)(imageMap), (void *)(icon->imageMap), numPixels << 2 );
	}

#endif