Oolite
Loading...
Searching...
No Matches
OOPixMapChannelOperations.m File Reference
Include dependency graph for OOPixMapChannelOperations.m:

Go to the source code of this file.

Functions

static void ExtractChannel_4 (OOPixMap *ioPixMap, uint8_t channelIndex)
static void ToRGBA_1 (OOPixMap srcPx, OOPixMap dstPx)
static void ToRGBA_2 (OOPixMap srcPx, OOPixMap dstPx)
static void ModulateUniform_4 (OOPixMap pixMap, uint16_t f0, uint16_t f1, uint16_t f2, uint16_t f3)
static void ModulatePixMap_4 (OOPixMap mainPx, OOPixMap otherPx)
static void AddPixMap_4 (OOPixMap mainPx, OOPixMap otherPx)
BOOL OOExtractPixMapChannel (OOPixMap *ioPixMap, uint8_t channelIndex, BOOL compactWhenDone)
BOOL OOPixMapToRGBA (OOPixMap *ioPixMap)
BOOL OOPixMapModulateUniform (OOPixMap *ioPixMap, float f0, float f1, float f2, float f3)
BOOL OOPixMapModulatePixMap (OOPixMap *ioDstPixMap, OOPixMap otherPixMap)
BOOL OOPixMapAddPixMap (OOPixMap *ioDstPixMap, OOPixMap otherPixMap)

Function Documentation

◆ AddPixMap_4()

void AddPixMap_4 ( OOPixMap mainPx,
OOPixMap otherPx )
static

Definition at line 294 of file OOPixMapChannelOperations.m.

295{
296 uint32_t *dst, *other;
297 uint32_t px;
298 uint32_t m02, m13;
299 uint32_t o02, o13;
300 uint32_t xCount, y;
301
302 for (y = 0; y < mainPx.height; y++) {
303 dst = (uint32_t*)((char*)mainPx.pixels + y * mainPx.rowBytes);
304 other = (uint32_t*)((char*)otherPx.pixels + y * otherPx.rowBytes);
305 xCount = mainPx.width;
306
307 do {
308 px = *dst;
309 m02 = (px & 0xFF00FF00) >> 8;
310 m13 = px & 0x00FF00FF;
311
312 px = *other;
313 o02 = (px & 0xFF00FF00) >> 8;
314 o13 = px & 0x00FF00FF;
315
316 /* Saturated adds, two components at a time.
317 By masking out the overflow bits of each component,
318 multiplying them by 0xFF and shifting right one byte, we get a
319 mask that's oxFF for components that overflowed and 0x00 for
320 components that did not, without any conditionals.
321 */
322 m02 += o02;
323 m02 |= ((m02 & 0x01000100) * 0xFF) >> 8;
324 m13 += o13;
325 m13 |= ((m13 & 0x01000100) * 0xFF) >> 8;
326
327 *dst++ = ((m02 << 8) & 0xFF00FF00) | (m13 & 0x00FF00FF);
328 other++;
329 } while (--xCount);
330 }
331}
float y
OOPixMapDimension height
Definition OOPixMap.h:45
size_t rowBytes
Definition OOPixMap.h:47
void * pixels
Definition OOPixMap.h:44
OOPixMapDimension width
Definition OOPixMap.h:45

References OOPixMap::height, OOPixMap::pixels, OOPixMap::rowBytes, OOPixMap::width, and y.

Referenced by OOPixMapAddPixMap().

Here is the caller graph for this function:

◆ ExtractChannel_4()

void ExtractChannel_4 ( OOPixMap * ioPixMap,
uint8_t channelIndex )
static

Definition at line 56 of file OOPixMapChannelOperations.m.

57{
58 NSCParameterAssert(ioPixMap != NULL);
59
60 uint32_t* src;
61 uint8_t* dst;
62 uint8_t shift;
63 uint32_t xCount, y;
64
65 dst = ioPixMap->pixels;
66 shift = 8 * channelIndex;
67
68 for (y = 0; y < ioPixMap->height; y++) {
69 src = (uint32_t*)((char*)ioPixMap->pixels + y * ioPixMap->rowBytes);
70 xCount = ioPixMap->width;
71
72 do {
73 *dst++ = (*src++ >> shift) & 0xFF;
74 } while (--xCount);
75 }
76}

References OOPixMap::height, OOPixMap::pixels, OOPixMap::rowBytes, OOPixMap::width, and y.

Referenced by OOExtractPixMapChannel().

Here is the caller graph for this function:

◆ ModulatePixMap_4()

void ModulatePixMap_4 ( OOPixMap mainPx,
OOPixMap otherPx )
static

Definition at line 236 of file OOPixMapChannelOperations.m.

237{
238 uint32_t *dst, *other;
239 uint32_t px;
240 uint16_t m0, m1, m2, m3;
241 uint16_t o0, o1, o2, o3;
242 uint32_t xCount, y;
243
244 for (y = 0; y < mainPx.height; y++) {
245 dst = (uint32_t*)((char*)mainPx.pixels + y * mainPx.rowBytes);
246 other = (uint32_t*)((char*)otherPx.pixels + y * otherPx.rowBytes);
247 xCount = mainPx.width;
248
249 do {
250 px = *dst;
251 m0 = (px >> 24) & 0xFF;
252 m1 = (px >> 16) & 0xFF;
253 m2 = (px >> 8) & 0xFF;
254 m3 = px & 0xFF;
255
256 px = *other;
257 o0 = (px >> 24) & 0xFF;
258 o1 = (px >> 16) & 0xFF;
259 o2 = (px >> 8) & 0xFF;
260 o3 = px & 0xFF;
261
262 /* Unlike in ModulateUniform(), neither side here goes to 256, so
263 we have to divide by 255 rather than shifting. However, the
264 compiler should be able to optimize this to a multiplication
265 by a magic number.
266 */
267 m0 = (m0 * o0) / 255;
268 m1 = (m1 * o1) / 255;
269 m2 = (m2 * o2) / 255;
270 m3 = (m3 * o3) / 255;
271
272 *dst++ = (m0 << 24) | (m1 << 16) | (m2 << 8) | m3;
273 other++;
274 } while (--xCount);
275 }
276}

References OOPixMap::height, OOPixMap::pixels, OOPixMap::rowBytes, OOPixMap::width, and y.

Referenced by OOPixMapModulatePixMap().

Here is the caller graph for this function:

◆ ModulateUniform_4()

void ModulateUniform_4 ( OOPixMap pixMap,
uint16_t f0,
uint16_t f1,
uint16_t f2,
uint16_t f3 )
static

Definition at line 178 of file OOPixMapChannelOperations.m.

179{
180 NSCParameterAssert(OOPixMapBytesPerPixel(pixMap) == 4);
181
182 uint32_t* curr;
183 uint32_t px;
184 uint32_t p0, p1, p2, p3;
185 uint32_t xCount, y;
186
187 for (y = 0; y < pixMap.height; y++) {
188 curr = (uint32_t*)((char*)pixMap.pixels + y * pixMap.rowBytes);
189 xCount = pixMap.width;
190
191 do {
192 px = *curr;
193
194 /* Principle of operation:
195 Each pixel component is in the range 0..0xFF.
196 Each constant factor component is in the range 0..0x100.
197 Multiplying them therefore gives us a result in the range
198 0x0000..0xFF00. The bottom byte is discarded by shifting
199 and masking.
200 */
201
202 p0 = px & 0xFF000000;
203 p0 = ((p0 >> 8) * f0) & 0xFF000000;
204
205 p1 = px & 0x00FF0000;
206 p1 = ((p1 * f1) >> 8) & 0x00FF0000;
207
208 p2 = px & 0x0000FF00;
209 p2 = ((p2 * f2) >> 8) & 0x0000FF00;
210
211 p3 = px & 0x000000FF;
212 p3 = ((p3 * f3) >> 8) & 0x000000FF;
213
214 px = p0 | p1 | p2 | p3;
215 *curr++ = px;
216 } while (--xCount);
217 }
218}
OOINLINE unsigned short OOPixMapBytesPerPixel(OOPixMap pixMap)
Definition OOPixMap.h:118

References OOPixMap::height, OOPixMapBytesPerPixel(), OOPixMap::pixels, OOPixMap::rowBytes, OOPixMap::width, and y.

Referenced by OOPixMapModulateUniform().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OOExtractPixMapChannel()

BOOL OOExtractPixMapChannel ( OOPixMap * ioPixMap,
uint8_t channelIndex,
BOOL compactWhenDone )

Definition at line 38 of file OOPixMapChannelOperations.m.

39{
40 if (EXPECT_NOT(ioPixMap == NULL || !OOIsValidPixMap(*ioPixMap) || ioPixMap->format != kOOPixMapRGBA || channelIndex > 3)) {
41 return NO;
42 }
43
44 ExtractChannel_4(ioPixMap, channelIndex);
45
46 ioPixMap->format = kOOPixMapGrayscale;
47 ioPixMap->rowBytes = ioPixMap->width;
48
49 if (compactWhenDone) {
50 OOCompactPixMap(ioPixMap);
51 }
52
53 return YES;
54}
#define EXPECT_NOT(x)
static void ExtractChannel_4(OOPixMap *ioPixMap, uint8_t channelIndex)
@ kOOPixMapGrayscale
Definition OOPixMap.h:38
@ kOOPixMapRGBA
Definition OOPixMap.h:40
BOOL OOIsValidPixMap(OOPixMap pixMap)
Definition OOPixMap.m:39
OOINLINE void OOCompactPixMap(OOPixMap *ioPixMap)
Definition OOPixMap.h:91
OOPixMapFormat format
Definition OOPixMap.h:46

References EXPECT_NOT, ExtractChannel_4(), OOPixMap::format, kOOPixMapGrayscale, kOOPixMapRGBA, OOCompactPixMap(), OOIsValidPixMap(), OOPixMap::rowBytes, and OOPixMap::width.

Referenced by OOTextureLoader(OOPrivate)::applySettings, and OOCombinedEmissionMapGenerator::loadTexture.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OOPixMapAddPixMap()

BOOL OOPixMapAddPixMap ( OOPixMap * ioDstPixMap,
OOPixMap otherPixMap )

Definition at line 278 of file OOPixMapChannelOperations.m.

279{
280 if (EXPECT_NOT(ioDstPixMap == NULL || !OOIsValidPixMap(*ioDstPixMap)))
281 return NO;
282 if (EXPECT_NOT(!OOIsValidPixMap(otherPixMap) || otherPixMap.format != kOOPixMapRGBA))
283 return NO;
284 if (EXPECT_NOT(!OOPixMapToRGBA(ioDstPixMap)))
285 return NO;
286 if (EXPECT_NOT(ioDstPixMap->width != otherPixMap.width || ioDstPixMap->height != otherPixMap.height))
287 return NO;
288
289 AddPixMap_4(*ioDstPixMap, otherPixMap);
290
291 return YES;
292}
static void AddPixMap_4(OOPixMap mainPx, OOPixMap otherPx)
BOOL OOPixMapToRGBA(OOPixMap *ioPixMap)

References AddPixMap_4(), EXPECT_NOT, OOPixMap::format, OOPixMap::height, kOOPixMapRGBA, OOIsValidPixMap(), OOPixMapToRGBA(), and OOPixMap::width.

Referenced by OOCombinedEmissionMapGenerator::loadTexture.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OOPixMapModulatePixMap()

BOOL OOPixMapModulatePixMap ( OOPixMap * ioDstPixMap,
OOPixMap otherPixMap )

Definition at line 220 of file OOPixMapChannelOperations.m.

221{
222 if (EXPECT_NOT(ioDstPixMap == NULL || !OOIsValidPixMap(*ioDstPixMap)))
223 return NO;
224 if (EXPECT_NOT(!OOIsValidPixMap(otherPixMap) || otherPixMap.format != kOOPixMapRGBA))
225 return NO;
226 if (EXPECT_NOT(!OOPixMapToRGBA(ioDstPixMap)))
227 return NO;
228 if (EXPECT_NOT(ioDstPixMap->width != otherPixMap.width || ioDstPixMap->height != otherPixMap.height))
229 return NO;
230
231 ModulatePixMap_4(*ioDstPixMap, otherPixMap);
232
233 return YES;
234}
static void ModulatePixMap_4(OOPixMap mainPx, OOPixMap otherPx)

References EXPECT_NOT, OOPixMap::format, OOPixMap::height, kOOPixMapRGBA, ModulatePixMap_4(), OOIsValidPixMap(), OOPixMapToRGBA(), and OOPixMap::width.

Referenced by OOCombinedEmissionMapGenerator::loadTexture.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OOPixMapModulateUniform()

BOOL OOPixMapModulateUniform ( OOPixMap * ioPixMap,
float f0,
float f1,
float f2,
float f3 )

Definition at line 166 of file OOPixMapChannelOperations.m.

167{
168 if (EXPECT_NOT(ioPixMap == NULL || !OOIsValidPixMap(*ioPixMap)))
169 return NO;
170 if (EXPECT_NOT(!OOPixMapToRGBA(ioPixMap)))
171 return NO;
172
173 ModulateUniform_4(*ioPixMap, f0 * 256.0f, f1 * 256.0f, f2 * 256.0f, f3 * 256.0f);
174
175 return YES;
176}
static void ModulateUniform_4(OOPixMap pixMap, uint16_t f0, uint16_t f1, uint16_t f2, uint16_t f3)

References EXPECT_NOT, ModulateUniform_4(), OOIsValidPixMap(), and OOPixMapToRGBA().

Referenced by OOCombinedEmissionMapGenerator::loadTexture.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OOPixMapToRGBA()

BOOL OOPixMapToRGBA ( OOPixMap * ioPixMap)

Definition at line 78 of file OOPixMapChannelOperations.m.

79{
80 if (EXPECT_NOT(ioPixMap == NULL || !OOIsValidPixMap(*ioPixMap)))
81 return NO;
82 if (ioPixMap->format == kOOPixMapRGBA)
83 return YES;
84
85 OOPixMap temp = OOAllocatePixMap(ioPixMap->width, ioPixMap->height, 4, 0, 0);
86 if (EXPECT_NOT(OOIsNullPixMap(temp)))
87 return NO;
88
89 BOOL OK = NO;
90 switch (ioPixMap->format) {
92 ToRGBA_1(*ioPixMap, temp);
93 OK = YES;
94 break;
95
97 ToRGBA_2(*ioPixMap, temp);
98 OK = YES;
99 break;
100
101 case kOOPixMapRGBA:
103 OK = NO;
104 break;
105 // No default, because -Wswitch-enum is our friend.
106 }
107
108 if (OK) {
109 free(ioPixMap->pixels);
110 *ioPixMap = temp;
111 } else {
112 free(temp.pixels);
113 }
114
115 return OK;
116}
static void ToRGBA_2(OOPixMap srcPx, OOPixMap dstPx)
static void ToRGBA_1(OOPixMap srcPx, OOPixMap dstPx)
@ kOOPixMapInvalidFormat
Definition OOPixMap.h:37
@ kOOPixMapGrayscaleAlpha
Definition OOPixMap.h:39
OOPixMap OOAllocatePixMap(OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format, size_t rowBytes, size_t bufferSize)
Definition OOPixMap.m:66
OOINLINE BOOL OOIsNullPixMap(OOPixMap pixMap)
Definition OOPixMap.h:53

References EXPECT_NOT, OOPixMap::format, OOPixMap::height, kOOPixMapGrayscale, kOOPixMapGrayscaleAlpha, kOOPixMapInvalidFormat, kOOPixMapRGBA, OOAllocatePixMap(), OOIsNullPixMap(), OOIsValidPixMap(), OOPixMap::pixels, ToRGBA_1(), ToRGBA_2(), and OOPixMap::width.

Referenced by OOTextureLoader(OOPrivate)::applySettings, OOCombinedEmissionMapGenerator::loadTexture, OOPixMapAddPixMap(), OOPixMapModulatePixMap(), and OOPixMapModulateUniform().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ToRGBA_1()

void ToRGBA_1 ( OOPixMap srcPx,
OOPixMap dstPx )
static

Definition at line 118 of file OOPixMapChannelOperations.m.

119{
120 NSCParameterAssert(OOPixMapBytesPerPixel(srcPx) == 1 && dstPx.format == kOOPixMapRGBA && srcPx.width == dstPx.width && srcPx.height == dstPx.height);
121
122 uint8_t* src;
123 uint32_t* dst;
124 uint32_t xCount, y;
125
126 dst = dstPx.pixels;
127
128 for (y = 0; y < srcPx.height; y++) {
129 src = (uint8_t*)((char*)srcPx.pixels + y * srcPx.rowBytes);
130 xCount = srcPx.width;
131
132 do {
133 *dst++ = (*src++ * 0x00010101) | 0xFF000000;
134 } while (--xCount);
135 }
136}

References OOPixMap::format, OOPixMap::height, kOOPixMapRGBA, OOPixMapBytesPerPixel(), OOPixMap::pixels, OOPixMap::rowBytes, OOPixMap::width, and y.

Referenced by OOPixMapToRGBA().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ToRGBA_2()

void ToRGBA_2 ( OOPixMap srcPx,
OOPixMap dstPx )
static

Definition at line 138 of file OOPixMapChannelOperations.m.

139{
140 NSCParameterAssert(OOPixMapBytesPerPixel(srcPx) == 2 && dstPx.format == kOOPixMapRGBA && srcPx.width == dstPx.width && srcPx.height == dstPx.height);
141
142 uint16_t* src;
143 uint32_t px;
144 uint32_t* dst;
145 uint32_t xCount, y;
146
147 dst = dstPx.pixels;
148
149 for (y = 0; y < srcPx.height; y++) {
150 src = (uint16_t*)((char*)srcPx.pixels + y * srcPx.rowBytes);
151 xCount = srcPx.width;
152
153 do {
154 px = *src++;
155#if OOLITE_BIG_ENDIAN
156 *dst++ = (((px & 0xFF00) >> 8) * 0x00010101) | ((px & 0x00FF) << 24);
157#elif OOLITE_LITTLE_ENDIAN
158 *dst++ = ((px & 0x00FF) * 0x00010101) | ((px & 0xFF00) << 16);
159#else
160#error Unknown byte order.
161#endif
162 } while (--xCount);
163 }
164}

References OOPixMap::format, OOPixMap::height, kOOPixMapRGBA, OOPixMapBytesPerPixel(), OOPixMap::pixels, OOPixMap::rowBytes, OOPixMap::width, and y.

Referenced by OOPixMapToRGBA().

Here is the call graph for this function:
Here is the caller graph for this function: