Oolite 1.91.0.7645-241119-222d325
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
30
32{
33 .pixels = NULL,
34 .width = 0,
35 .height = 0,
36 .format = kOOPixMapInvalidFormat,
37 .rowBytes = 0,
38 .bufferSize = 0
39};
40
41
43{
44 return pixMap.pixels != NULL &&
45 pixMap.width > 0 &&
46 pixMap.height > 0 &&
48 pixMap.rowBytes >= pixMap.width * OOPixMapBytesPerPixelForFormat(pixMap.format) &&
49 pixMap.bufferSize >= pixMap.rowBytes * pixMap.height;
50}
51
52
53OOPixMap OOMakePixMap(void *pixels, OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format, size_t rowBytes, size_t bufferSize)
54{
55 if (rowBytes == 0) rowBytes = width * OOPixMapBytesPerPixelForFormat(format);
56 if (bufferSize == 0) bufferSize = rowBytes * height;
57
58 OOPixMap result =
59 {
60 .pixels = pixels,
61 .width = width,
62 .height = height,
63 .format = format,
64 .rowBytes = rowBytes,
65 .bufferSize = bufferSize
66 };
67
68 if (OOIsValidPixMap(result)) return result;
69 else return kOONullPixMap;
70}
71
72
73OOPixMap OOAllocatePixMap(OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format, size_t rowBytes, size_t bufferSize)
74{
75 // Create pixmap struct with dummy pixel pointer to test validity.
76 OOPixMap pixMap = OOMakePixMap((void *)-1, width, height, format, rowBytes, bufferSize);
77 if (EXPECT_NOT(!OOIsValidPixMap(pixMap))) return kOONullPixMap;
78
79 pixMap.pixels = malloc(pixMap.bufferSize);
80 if (EXPECT_NOT(pixMap.pixels == NULL)) return kOONullPixMap;
81
82 return pixMap;
83}
84
85
86void OOFreePixMap(OOPixMap *ioPixMap)
87{
88 if (EXPECT_NOT(ioPixMap == NULL)) return;
89
90 free(ioPixMap->pixels);
91 *ioPixMap = kOONullPixMap;
92}
93
94
95OOPixMap OODuplicatePixMap(OOPixMap srcPixMap, size_t desiredSize)
96{
97 if (EXPECT_NOT(!OOIsValidPixMap(srcPixMap))) return kOONullPixMap;
98
99 size_t minSize = OOMinimumPixMapBufferSize(srcPixMap);
100 if (desiredSize < minSize) desiredSize = minSize;
101
102 OOPixMap result = OOAllocatePixMap(srcPixMap.width, srcPixMap.width, srcPixMap.format, srcPixMap.rowBytes, desiredSize);
103 if (EXPECT_NOT(!OOIsValidPixMap(result))) return kOONullPixMap;
104
105 memcpy(result.pixels, srcPixMap.pixels, minSize);
106 return result;
107}
108
109
110BOOL OOResizePixMap(OOPixMap *ioPixMap, size_t desiredSize)
111{
112 if (EXPECT_NOT(ioPixMap == NULL || !OOIsValidPixMap(*ioPixMap))) return NO;
113 if (desiredSize == ioPixMap->bufferSize) return YES;
114 if (desiredSize < OOMinimumPixMapBufferSize(*ioPixMap)) return NO;
115
116 void *newPixels = realloc(ioPixMap->pixels, desiredSize);
117 if (newPixels != NULL)
118 {
119 ioPixMap->pixels = newPixels;
120 ioPixMap->bufferSize = desiredSize;
121 return YES;
122 }
123 else
124 {
125 return NO;
126 }
127}
128
129
130BOOL OOExpandPixMap(OOPixMap *ioPixMap, size_t desiredSize)
131{
132 if (EXPECT_NOT(ioPixMap == NULL || !OOIsValidPixMap(*ioPixMap))) return NO;
133 if (desiredSize <= ioPixMap->bufferSize) return YES;
134
135 return OOResizePixMap(ioPixMap, desiredSize);
136}
137
138
139#ifndef NDEBUG
140
141#import "Universe.h"
142#import "MyOpenGLView.h"
143
144
145void OODumpPixMap(OOPixMap pixMap, NSString *name)
146{
147 if (!OOIsValidPixMap(pixMap)) return;
148
149 MyOpenGLView *gameView = [UNIVERSE gameView];
150
151 switch (pixMap.format)
152 {
153
155 break;
156
158 [gameView dumpGrayToFileNamed:name
159 bytes:pixMap.pixels
160 width:pixMap.width
161 height:pixMap.height
162 rowBytes:pixMap.rowBytes];
163 break;
164
166 [gameView dumpGrayAlphaToFileNamed:name
167 bytes:pixMap.pixels
168 width:pixMap.width
169 height:pixMap.height
170 rowBytes:pixMap.rowBytes];
171 break;
172
173 case kOOPixMapRGBA:
174 [gameView dumpRGBAToRGBFileNamed:[name stringByAppendingString:@" rgb"]
175 andGrayFileNamed:[name stringByAppendingString:@" alpha"]
176 bytes:pixMap.pixels
177 width:pixMap.width
178 height:pixMap.height
179 rowBytes:pixMap.rowBytes];
180 break;
181 }
182}
183#endif
184
185
187{
188 switch (format)
189 {
190 case kOOPixMapInvalidFormat: return NO;
193 case kOOPixMapRGBA:
194 return YES;
195 }
196
197 return NO;
198}
199
200
201#ifndef NDEBUG
203{
204 switch (format)
205 {
206 case kOOPixMapInvalidFormat: return 0;
207 case kOOPixMapGrayscale: return 1;
208 case kOOPixMapGrayscaleAlpha: return 2;
209 case kOOPixMapRGBA: return 4;
210 }
211
212 return -1;
213}
214#endif
215
216
218{
219 switch (format)
220 {
221 case kOOPixMapInvalidFormat: return @"invalid";
222 case kOOPixMapGrayscale: return @"grayscale";
223 case kOOPixMapGrayscaleAlpha: return @"grayscale+alpha";
224 case kOOPixMapRGBA: return @"RGBA";
225 }
226
227 return [NSString stringWithFormat:@"invalid<%i>", (int)format];
228}
229
230
231
233{
234 switch (format)
235 {
236 case kOOPixMapInvalidFormat: return NO;
237 case kOOPixMapGrayscale: return NO;
238 case kOOPixMapGrayscaleAlpha: return YES;
239 case kOOPixMapRGBA: return YES;
240 }
241
242 return NO;
243}
#define EXPECT_NOT(x)
uint_fast32_t OOPixMapDimension
Definition OOPixMap.h:33
OOPixMapFormat
Definition OOPixMap.h:39
@ kOOPixMapInvalidFormat
Definition OOPixMap.h:40
@ kOOPixMapGrayscale
Definition OOPixMap.h:41
@ kOOPixMapRGBA
Definition OOPixMap.h:43
@ kOOPixMapGrayscaleAlpha
Definition OOPixMap.h:42
OOINLINE size_t OOMinimumPixMapBufferSize(OOPixMap pixMap)
Definition OOPixMap.h:62
void OOFreePixMap(OOPixMap *ioPixMap)
Definition OOPixMap.m:86
NSString * OOPixMapFormatName(OOPixMapFormat format)
Definition OOPixMap.m:217
BOOL OOPixMapFormatHasAlpha(OOPixMapFormat format)
Definition OOPixMap.m:232
void OODumpPixMap(OOPixMap pixMap, NSString *name)
Definition OOPixMap.m:145
BOOL OOIsValidPixMapFormat(OOPixMapFormat format)
Definition OOPixMap.m:186
const OOPixMap kOONullPixMap
Definition OOPixMap.m:31
OOPixMap OOAllocatePixMap(OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format, size_t rowBytes, size_t bufferSize)
Definition OOPixMap.m:73
unsigned short OOPixMapBytesPerPixelForFormat(OOPixMapFormat format)
Definition OOPixMap.m:202
OOPixMap OOMakePixMap(void *pixels, OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format, size_t rowBytes, size_t bufferSize)
Definition OOPixMap.m:53
BOOL OOResizePixMap(OOPixMap *ioPixMap, size_t desiredSize)
Definition OOPixMap.m:110
BOOL OOIsValidPixMap(OOPixMap pixMap)
Definition OOPixMap.m:42
BOOL OOExpandPixMap(OOPixMap *ioPixMap, size_t desiredSize)
Definition OOPixMap.m:130
OOPixMap OODuplicatePixMap(OOPixMap srcPixMap, size_t desiredSize)
Definition OOPixMap.m:95
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:50
size_t bufferSize
Definition OOPixMap.h:53
size_t rowBytes
Definition OOPixMap.h:52
void * pixels
Definition OOPixMap.h:49
OOPixMapDimension width
Definition OOPixMap.h:50
OOPixMapFormat format
Definition OOPixMap.h:51