-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image.m
308 lines (248 loc) · 6.51 KB
/
Image.m
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#import "Image.h"
#import "Image.h"
#import "fits.h"
@implementation Image
- (id) initWithFITS:(NSString *)file
{
if (self = [super init]) {
char *cFileName;
[file retain];
[fileName release];
fileName = file;
cFileName = (char *)[file UTF8String];
NSLog(@"Attempting to read in %@",file);
data = readfloat(cFileName, &nx, &ny, &error_status);
writefloat("!/var/tmp/tmp.fits", data, nx, ny, &error_status); // Useful for debugging only
NSLog(@"Read in FITS image with size %d %d",nx,ny);
}
return self;
}
- (id) initWithValue:(float)val nx:(int)nrow ny:(int)ncol
{
int i;
if (self = [super init]) {
nx = nrow;
ny = ncol;
data = (float *) malloc(nx*ny*sizeof(float));
for(i=0;i<nx*ny;i++){
*(data+i) = val;
}
NSLog(@"Creating zero image with size %d %d",nx,ny);
}
return self;
}
- (id) initWithData:(float *)pixels nx:(int)nrow ny:(int)ncol
{
if (self = [super init]) {
nx = nrow;
ny = ncol;
data = pixels;
}
return self;
}
/*
- (Image *) duplicate
{
int i;
float *d;
d = (float *)malloc(nx*ny*sizeof(float));
for(i=0;i<nx*ny;i++)
*(d+i)=*(data+i);
return [[Image alloc] initWithData:d nx:nx ny:ny];
}
*/
- (id)copyWithZone:(NSZone *)zone
{
int i;
float *d;
d = (float *)malloc(nx*ny*sizeof(float));
for(i=0;i<nx*ny;i++)
*(d+i)=*(data+i);
return [[Image allocWithZone:zone] initWithData:d nx:nx ny:ny];
}
- (void) setValue:(float)val x:(int)x y:(int)y
{
*((float *)data + nx*y + x) = val;
}
-(float) value:(int)x :(int)y
{
return *((float *)data + nx*y + x);
}
/* These next two will blow up if an image size greater than 65K x 65K is used */
- (void) setValue:(float)val index:(long int)i
{
*((float *)data + i) = val;
}
-(float) value:(long int)index
{
return *((float *)data + index);
}
- (NSMutableArray *) row:(int) y
{
NSMutableArray *array;
NSNumber *number;
int i;
if((y < 0) || (y >= ny)) {
return(nil);
}
array = [[NSMutableArray alloc] initWithCapacity:nx];
for(i=0;i<nx;i++){
number = [[NSNumber alloc] initWithFloat:(*((float *)data + y*nx + i))];
[array addObject:number];
[number release];
}
return(array);
}
- (NSMutableArray *) column:(int) x
{
NSMutableArray *array;
NSNumber *number;
int j;
if((x < 0) || (x >= nx)) {
return(nil);
}
array = [[NSMutableArray alloc] initWithCapacity:ny];
for(j=0;j<ny;j++){
number = [[NSNumber alloc] initWithFloat:(*((float *)data + j*nx + x))];
[array addObject:number];
[number release];
}
return(array);
}
- (void) clear{
int i;
for(i=0;i<nx*ny;i++) {
*(data + i) = 0.0;
}
}
- (void) dealloc
{
//NSLog(@"Destroying %@",self);
free(data);
[super dealloc];
}
- (NSString *) description
{
NSString *result = [[NSString alloc] initWithFormat:@"image with size: %d %d",[self nx],[self ny]];
[result autorelease];
return result;
}
- (float) min
{
int i;
float pixval;
float minval=1.e30;
for(i=0;i<nx*ny;i++)
{
pixval = *((float *)data + i);
if (pixval<=minval)
minval = pixval;
}
return minval;
}
- (float) max
{
int i;
float pixval;
float maxval=-1.e30;
for(i=0;i<nx*ny;i++)
{
pixval = *((float *)data + i);
if (pixval>=maxval)
maxval = pixval;
}
return maxval;
}
- (float) total
{
int i;
float tot;
tot = 0.0;
for(i=0;i<nx*ny;i++)
tot = tot + *((float *)data + i);
return tot;
}
- (Image *) boxcar:(int)halfwidth
{
int x,y;
int i,j;
float sum;
float *temp;
float area = pow(2.0*halfwidth + 1.0,2.0);
temp = (float *)malloc(nx*ny*sizeof(float));
for(x=0;x<nx;x++){
for(y=0;y<ny;y++){
sum = 0.0;
if ((x<halfwidth) || x>(nx-halfwidth-1) || (y<halfwidth) || y>(ny-halfwidth-1)) {
// border pixels remain unchanged
sum = *(data + nx*y + x);
}
else {
for(i=x-halfwidth;i<=x+halfwidth;i++){
for(j=y-halfwidth;j<=y+halfwidth;j++){
sum += *(data + nx*j + i);
}
}
}
*(temp + nx*y + x) = sum/area;
}
}
return [[[Image alloc] initWithData:temp nx:nx ny:ny] autorelease];
}
- (void) saveFITS:(NSString *)file
{
writefloat([file UTF8String],data,nx,ny,&error_status);
}
- (NSBitmapImageRep *)createRepresentationWithMin:(float)min andMax:(float)max;
{
int i,j;
unsigned int p[1] = {128};
float temp;
NSBitmapImageRep *bitmap;
// We allocate the NSBitmapImageRep instance and initialize it.
// The initialization method has a lot of parameters because it is very versatile.
// It can handle many different arrangements of pixel data. In this case I
// have chosen an organization that corresponds to a grayscale.
//
// Finally, we let the NSBitmapImageRep instance allocate a buffer of the proper size to handle
// our image; alternatively, you could allocate your own buffer for the pixel data and pass the
// address to NSBitmapImageRep when initializing.
bitmap = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes: NULL // Let the class allocate it
pixelsWide: [self nx]
pixelsHigh: [self ny]
bitsPerSample: 8 // Each component is 8 bits (one byte)
samplesPerPixel: 1 // Number of components grayscale
hasAlpha: NO
isPlanar: NO
colorSpaceName: NSCalibratedWhiteColorSpace
bytesPerRow: 0 // 0 means: Let the class figure it out
bitsPerPixel: 0 // 0 means: Let the class figure it out
];
// Note the funny indexing below. This is because it seems GIF/TIFF/Whatever standard
// is to have the origin of the coordinate system at the top-left, and I think this is
// what the NSImageView assumes is going to be used too.
for(i=0;i<nx;i++){
for(j=0;j<ny;j++){
temp = (*((float *)data + nx*j + i)-min)/(max-min);
if (temp<=0.0)
p[0] = 0;
else if (temp>=1.0)
p[0] = 255;
else
p[0] = (int)(256*temp);
[bitmap setPixel:p atX:i y:(ny-j-1)];
}
}
// Return the bitmap autoreleased
return [bitmap autorelease];
}
//Accessor methods
//idAccessor(data,setData)
- (float *)pixelData
{
return data;
}
intAccessor(nx, setNx)
intAccessor(ny, setNy)
@end