File: MyCalibrationLock.m

package info (click to toggle)
lynkeos.app 3.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 15,740 kB
  • sloc: objc: 36,412; ansic: 684; cpp: 148; sh: 68; makefile: 21
file content (257 lines) | stat: -rw-r--r-- 7,651 bytes parent folder | download
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
//
//  Lynkeos
//  $Id: MyCalibrationLock.m 565 2014-01-25 20:13:23Z j-etienne $
//
//  Created by Jean-Etienne LAMIAUD on Mon Aug 16 2004.
//  Copyright (c) 2004-2014. Jean-Etienne LAMIAUD
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//

#include "MyCalibrationLock.h"

@interface MyCalibrationLock(Private)
- (void) initCalibrationData:(CalibrationData_t*)data;
- (void) removeReader:(id <LynkeosFileReader>)reader
             fromData:(CalibrationData_t*)data;
- (BOOL) addCalibrationReader:(id <LynkeosFileReader>)reader
                      forMode:(ListMode_t)mode;
- (BOOL) checkImageReader:(id <LynkeosFileReader>)reader
                  forMode:(ListMode_t)mode;
@end

@implementation MyCalibrationLock(Private)
- (void) initCalibrationData:(CalibrationData_t*)data
{
   data->list = [[NSMutableArray array] retain];
   data->size = LynkeosMakeIntegerSize(0,0);
   data->nPlanes = 0;
}

- (void) removeReader:(id <LynkeosFileReader>)reader
             fromData:(CalibrationData_t*)data
{
   [data->list removeObjectIdenticalTo:reader];

   if ( [data->list count] == 0 )
   {
      // No more frames, reset the "geometry"
      data->size = LynkeosMakeIntegerSize(0,0);
      data->nPlanes = 0;
   }
}

- (BOOL) addCalibrationReader:(id <LynkeosFileReader>)reader
                      forMode:(ListMode_t)mode
{
   NSEnumerator *iter = [_image.list objectEnumerator];
   CalibrationData_t *calData
      = (mode == DarkFrameMode ? &_darkFrame : &_flatField);
   const BOOL firstCalibration = ([calData->list count] == 0);
   id <LynkeosFileReader> imageReader;
   LynkeosIntegerSize calSize;
   u_short np;

   [reader imageWidth:&calSize.width height:&calSize.height];
   np = [reader numberOfPlanes];

   // If there are already some calibration frames, verify "geometrical
   // compatibility"
   if ( !firstCalibration
        && ( calSize.width != calData->size.width
            || calSize.height != calData->size.height
            || np != calData->nPlanes) )
      return( NO );

   // Check against every image
   while ( (imageReader = [iter nextObject]) != nil )
   {
      if ( [[imageReader class] conformsToProtocol:
                                   @protocol(LynkeosCustomFileReader)] )
      {
         if ( ![(id <LynkeosCustomFileReader>)imageReader
                                         canBeCalibratedBy:reader asMode:mode] )
            return( NO );
      }

      // If there are not any calibration images yet, verify "geometrical
      // compatibility" with each image
      else if ( firstCalibration )
      {
         LynkeosIntegerSize imageSize;

         [imageReader imageWidth:&imageSize.width height:&imageSize.height];

         if ( imageSize.width != calSize.width
             || imageSize.height != calSize.height
             || [imageReader numberOfPlanes] != np )
            return( NO );
      }
   }

   if ( firstCalibration )
   {
      // First calibration image
      calData->size = calSize;
      calData->nPlanes = np;
   }

   [calData->list addObject:reader];

   return( YES );
}

- (BOOL) checkImageReader:(id <LynkeosFileReader>)reader
                  forMode:(ListMode_t)mode
{
   CalibrationData_t *calData
      = (mode == DarkFrameMode ? &_darkFrame : &_flatField);
   LynkeosIntegerSize imageSize;
   u_short np;

   [reader imageWidth:&imageSize.width height:&imageSize.height];
   np = [reader numberOfPlanes];

   // Nothing to check if there are no calibration frames
   if ( [calData->list count] != 0 )
   {
      NSEnumerator *iter = [calData->list objectEnumerator];
      id <LynkeosFileReader> calibrationReader;
      BOOL customImage = [[reader class] conformsToProtocol:
                                            @protocol(LynkeosCustomFileReader)];

      if ( customImage )
      {
         // Check "custom compatibility" against every calibration frame
         while ( (calibrationReader = [iter nextObject]) != nil )
         {
            if ( ![(id <LynkeosCustomFileReader>)reader
                                             canBeCalibratedBy:calibrationReader
                                                        asMode:mode] )
               return( NO );
         }
      }
       // Otherwise verify "geometrical compatibility"
      else if ( imageSize.width != calData->size.width ||
                imageSize.height != calData->size.height ||
                np != calData->nPlanes )
         return( NO );
      

   }

   return( YES );
}
@end

@implementation MyCalibrationLock

/*!
 * @discussion A dark frame item can be added if it is able to calibrate all
 *   the images in the "calibrable" list..
 */
- (BOOL) addDarkFrameItem :(MyImageListItem*)item ;
{
   return( [self addCalibrationReader:[item getReader]
                              forMode:DarkFrameMode] );
}

/*!
 * @discussion A flat field item can be added if it is able to calibrate all
 *   the images in the "calibrable" list..
 */
- (BOOL) addFlatFieldItem :(MyImageListItem*)item
{
   return( [self addCalibrationReader:[item getReader]
                              forMode:FlatFieldMode] );
}

/*!
 * @discussion An image item can be added if it can be calibrated by all 
 *   the images in the "calibration" lists..
 */
- (BOOL) addImageItem :(MyImageListItem*)item 
{
   const BOOL firstImage = ([_image.list count] == 0);
   id <LynkeosFileReader> imageReader = [item getReader];
   LynkeosIntegerSize imageSize;
   u_short np;

   [imageReader imageWidth:&imageSize.width height:&imageSize.height];
   np = [imageReader numberOfPlanes];

   // Check geometry consistency (if calibration frames are used)
   if ( !firstImage
        && ([_darkFrame.list count] != 0 || [_flatField.list count] != 0)
        && (imageSize.width != _image.size.width
            || imageSize.height != _image.size.height
            || np != _image.nPlanes) )
      return( NO );

   if ( ! [self checkImageReader:imageReader forMode:DarkFrameMode] )
      return( NO );
   if ( ! [self checkImageReader:imageReader forMode:FlatFieldMode] )
      return( NO );

   if ( firstImage )
   {
      _image.size = imageSize;
      _image.nPlanes = np;
   }
   [_image.list addObject:imageReader];

   return( YES );
}

- (void) removeItem :(MyImageListItem*)item ;
{
   id <LynkeosFileReader> reader = [item getReader];

   // Try to remove from each of the lists, it will succeed in one only
   [self removeReader:reader fromData:&_darkFrame];
   [self removeReader:reader fromData:&_flatField];
   [self removeReader:reader fromData:&_image];
}

// Constructors
- (id) init
{
   self = [super init];

   if ( self != nil )
   {
      [self initCalibrationData:&_darkFrame];
      [self initCalibrationData:&_flatField];
      [self initCalibrationData:&_image];
   }

   return( self );
}

- (void) dealloc
{
   [_darkFrame.list release];
   [_flatField.list release];
   [_image.list release];

   [super dealloc];
}

+ (id) calibrationLock
{
   return( [[[self alloc] init] autorelease] );
}

@end