Oolite
Loading...
Searching...
No Matches
OOPixMap.m
Go to the documentation of this file.
1/*
2
3OOPixMap.c
4
5
6Copyright (C) 2010-2013 Jens Ayton
7
8Permission is hereby granted, free of charge, to any person obtaining a copy
9of this software and associated documentation files (the "Software"), to deal
10in the Software without restriction, including without limitation the rights
11to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12copies of the Software, and to permit persons to whom the Software is
13furnished to do so, subject to the following conditions:
14
15The above copyright notice and this permission notice shall be included in all
16copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24SOFTWARE.
25
26*/
27
28#import "OOPixMap.h"
29
31 .pixels = NULL,
32 .width = 0,
33 .height = 0,
34 .format = kOOPixMapInvalidFormat,
35 .rowBytes = 0,
36 .bufferSize = 0
37};
38
40{
41 return pixMap.pixels != NULL && pixMap.width > 0 && pixMap.height > 0 && OOIsValidPixMapFormat(pixMap.format) && pixMap.rowBytes >= pixMap.width * OOPixMapBytesPerPixelForFormat(pixMap.format) && pixMap.bufferSize >= pixMap.rowBytes * pixMap.height;
42}
43
44OOPixMap OOMakePixMap(void* pixels, OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format, size_t rowBytes, size_t bufferSize)
45{
46 if (rowBytes == 0)
47 rowBytes = width * OOPixMapBytesPerPixelForFormat(format);
48 if (bufferSize == 0)
49 bufferSize = rowBytes * height;
50
51 OOPixMap result = {
52 .pixels = pixels,
53 .width = width,
54 .height = height,
55 .format = format,
56 .rowBytes = rowBytes,
57 .bufferSize = bufferSize
58 };
59
60 if (OOIsValidPixMap(result))
61 return result;
62 else
63 return kOONullPixMap;
64}
65
66OOPixMap OOAllocatePixMap(OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format, size_t rowBytes, size_t bufferSize)
67{
68 // Create pixmap struct with dummy pixel pointer to test validity.
69 OOPixMap pixMap = OOMakePixMap((void*)-1, width, height, format, rowBytes, bufferSize);
70 if (EXPECT_NOT(!OOIsValidPixMap(pixMap)))
71 return kOONullPixMap;
72
73 pixMap.pixels = malloc(pixMap.bufferSize);
74 if (EXPECT_NOT(pixMap.pixels == NULL))
75 return kOONullPixMap;
76
77 return pixMap;
78}
79
80void OOFreePixMap(OOPixMap* ioPixMap)
81{
82 if (EXPECT_NOT(ioPixMap == NULL))
83 return;
84
85 free(ioPixMap->pixels);
86 *ioPixMap = kOONullPixMap;
87}
88
89OOPixMap OODuplicatePixMap(OOPixMap srcPixMap, size_t desiredSize)
90{
91 if (EXPECT_NOT(!OOIsValidPixMap(srcPixMap)))
92 return kOONullPixMap;
93
94 size_t minSize = OOMinimumPixMapBufferSize(srcPixMap);
95 if (desiredSize < minSize)
96 desiredSize = minSize;
97
98 OOPixMap result = OOAllocatePixMap(srcPixMap.width, srcPixMap.width, srcPixMap.format, srcPixMap.rowBytes, desiredSize);
99 if (EXPECT_NOT(!OOIsValidPixMap(result)))
100 return kOONullPixMap;
101
102 memcpy(result.pixels, srcPixMap.pixels, minSize);
103 return result;
104}
105
106BOOL OOResizePixMap(OOPixMap* ioPixMap, size_t desiredSize)
107{
108 if (EXPECT_NOT(ioPixMap == NULL || !OOIsValidPixMap(*ioPixMap)))
109 return NO;
110 if (desiredSize == ioPixMap->bufferSize)
111 return YES;
112 if (desiredSize < OOMinimumPixMapBufferSize(*ioPixMap))
113 return NO;
114
115 void* newPixels = realloc(ioPixMap->pixels, desiredSize);
116 if (newPixels != NULL) {
117 ioPixMap->pixels = newPixels;
118 ioPixMap->bufferSize = desiredSize;
119 return YES;
120 } else {
121 return NO;
122 }
123}
124
125BOOL OOExpandPixMap(OOPixMap* ioPixMap, size_t desiredSize)
126{
127 if (EXPECT_NOT(ioPixMap == NULL || !OOIsValidPixMap(*ioPixMap)))
128 return NO;
129 if (desiredSize <= ioPixMap->bufferSize)
130 return YES;
131
132 return OOResizePixMap(ioPixMap, desiredSize);
133}
134
135#ifndef NDEBUG
136
137#import "MyOpenGLView.h"
138#import "Universe.h"
139
140void OODumpPixMap(OOPixMap pixMap, NSString* name)
141{
142 if (!OOIsValidPixMap(pixMap))
143 return;
144
145 MyOpenGLView* gameView = [UNIVERSE gameView];
146
147 switch (pixMap.format) {
148
150 break;
151
153 [gameView dumpGrayToFileNamed:name
154 bytes:pixMap.pixels
155 width:pixMap.width
156 height:pixMap.height
157 rowBytes:pixMap.rowBytes];
158 break;
159
161 [gameView dumpGrayAlphaToFileNamed:name
162 bytes:pixMap.pixels
163 width:pixMap.width
164 height:pixMap.height
165 rowBytes:pixMap.rowBytes];
166 break;
167
168 case kOOPixMapRGBA:
169 [gameView dumpRGBAToRGBFileNamed:[name stringByAppendingString:@" rgb"]
170 andGrayFileNamed:[name stringByAppendingString:@" alpha"]
171 bytes:pixMap.pixels
172 width:pixMap.width
173 height:pixMap.height
174 rowBytes:pixMap.rowBytes];
175 break;
176 }
177}
178#endif
179
181{
182 switch (format) {
184 return NO;
187 case kOOPixMapRGBA:
188 return YES;
189 }
190
191 return NO;
192}
193
194#ifndef NDEBUG
196{
197 switch (format) {
199 return 0;
201 return 1;
203 return 2;
204 case kOOPixMapRGBA:
205 return 4;
206 }
207
208 return -1;
209}
210#endif
211
213{
214 switch (format) {
216 return @"invalid";
218 return @"grayscale";
220 return @"grayscale+alpha";
221 case kOOPixMapRGBA:
222 return @"RGBA";
223 }
224
225 return [NSString stringWithFormat:@"invalid<%i>", (int)format];
226}
227
229{
230 switch (format) {
232 return NO;
234 return NO;
236 return YES;
237 case kOOPixMapRGBA:
238 return YES;
239 }
240
241 return NO;
242}
#define EXPECT_NOT(x)
uint32_t OOPixMapDimension
Definition OOPixMap.h:32
OOPixMapFormat
Definition OOPixMap.h:36
@ kOOPixMapInvalidFormat
Definition OOPixMap.h:37
@ kOOPixMapGrayscale
Definition OOPixMap.h:38
@ kOOPixMapRGBA
Definition OOPixMap.h:40
@ kOOPixMapGrayscaleAlpha
Definition OOPixMap.h:39
OOINLINE size_t OOMinimumPixMapBufferSize(OOPixMap pixMap)
Definition OOPixMap.h:55
const OOPixMap kOONullPixMap
Definition OOPixMap.m:30
void OOFreePixMap(OOPixMap *ioPixMap)
Definition OOPixMap.m:80
NSString * OOPixMapFormatName(OOPixMapFormat format)
Definition OOPixMap.m:212
BOOL OOPixMapFormatHasAlpha(OOPixMapFormat format)
Definition OOPixMap.m:228
void OODumpPixMap(OOPixMap pixMap, NSString *name)
Definition OOPixMap.m:140
BOOL OOIsValidPixMapFormat(OOPixMapFormat format)
Definition OOPixMap.m:180
OOPixMap OOAllocatePixMap(OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format, size_t rowBytes, size_t bufferSize)
Definition OOPixMap.m:66
unsigned short OOPixMapBytesPerPixelForFormat(OOPixMapFormat format)
Definition OOPixMap.m:195
OOPixMap OOMakePixMap(void *pixels, OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format, size_t rowBytes, size_t bufferSize)
Definition OOPixMap.m:44
BOOL OOResizePixMap(OOPixMap *ioPixMap, size_t desiredSize)
Definition OOPixMap.m:106
BOOL OOIsValidPixMap(OOPixMap pixMap)
Definition OOPixMap.m:39
BOOL OOExpandPixMap(OOPixMap *ioPixMap, size_t desiredSize)
Definition OOPixMap.m:125
OOPixMap OODuplicatePixMap(OOPixMap srcPixMap, size_t desiredSize)
Definition OOPixMap.m:89
void dumpRGBAToRGBFileNamed:andGrayFileNamed:bytes:width:height:rowBytes:(NSString *rgbName,[andGrayFileNamed] NSString *grayName,[bytes] uint8_t *bytes,[width] NSUInteger width,[height] NSUInteger height,[rowBytes] NSUInteger rowBytes)
void dumpGrayAlphaToFileNamed:bytes:width:height:rowBytes:(NSString *name,[bytes] uint8_t *bytes,[width] NSUInteger width,[height] NSUInteger height,[rowBytes] NSUInteger rowBytes)
void dumpGrayToFileNamed:bytes:width:height:rowBytes:(NSString *name,[bytes] uint8_t *bytes,[width] NSUInteger width,[height] NSUInteger height,[rowBytes] NSUInteger rowBytes)
OOPixMapDimension height
Definition OOPixMap.h:45
size_t bufferSize
Definition OOPixMap.h:48
size_t rowBytes
Definition OOPixMap.h:47
void * pixels
Definition OOPixMap.h:44
OOPixMapDimension width
Definition OOPixMap.h:45
OOPixMapFormat format
Definition OOPixMap.h:46