File: MyImageListItem.m

package info (click to toggle)
lynkeos.app 3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,740 kB
  • sloc: objc: 40,528; ansic: 811; cpp: 150; sh: 68; makefile: 27
file content (1284 lines) | stat: -rw-r--r-- 41,784 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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
//
//  Lynkeos
//  $Id$
//
//  Created by Jean-Etienne LAMIAUD on Tue Sep 30, 2003.
//  Copyright (c) 2003-2023. 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.
//

#import <AppKit/NSCell.h>

#include "LynkeosImageBufferAdditions.h"
#include "MyPluginsController.h"
#include "ProcessStackManager.h"
#include "MyImageListItem.h"
#include "LynkeosFourierBuffer.h"
#include "LynkeosInterpolator.h"

// V1 Compatibility includes
#ifndef NO_FILE_FORMAT_COMPATIBILITY_CODE
#include "LynkeosBasicAlignResult.h"
#include "MyImageAligner.h"
#include "MyImageAnalyzer.h"
#endif

#include "LynkeosColumnDescriptor.h"

static NSString * const K_URL_KEY	= @"url";
static NSString * const K_SELECTED_KEY	= @"selected";
static NSString * const K_INDEX_KEY	= @"index";
static NSString * const K_LONG_INDEX_KEY	= @"indexL";
static NSString * const K_IMAGES_KEY	= @"images";

NSString * const myImageListItemRef = @"MyImageListItem";
NSString * const myImageListItemDarkFrame = @"darkFrame";
NSString * const myImageListItemFlatField = @"flatField";

// V1 compatibility keys
#ifndef NO_FILE_FORMAT_COMPATIBILITY_CODE
static NSString * const K_MODBLACK_KEY  = @"black";
static NSString * const K_MODWHITE_KEY  = @"white";
static NSString * const K_GAMMA_CORRECTION_KEY = @"gamma";
#endif

// A bad hack for relative URL resolution (until I find a better solution)
extern NSString *basePath;

/*!
 * @abstract Phases of the parallel processing for the lock condition
 */
typedef enum
{
   ProcessInited,
   ProcessStarted,
   ProcessEnded
} ParallelImageProcessState_t;

/*!
 * @abstract Record of data needed for parallelized interpolation
 */
@interface ParallelInterpolationArgs : NSObject
{
@public
   Class                      interpolatorClass; //!< The interpolator class to use
   NSObject <LynkeosProcessableItem> *sourceItem; //!< The item from which to interpolate
   LynkeosImageBuffer *buffer;         //!< Operation result
   LynkeosIntegerRect          rect;
   NSAffineTransformStruct     transform;
   NSPoint                    *offsets;
   u_short                     y;              //!< Current line
   NSConditionLock            *lock;           //!< Exclusive access to this object
   u_short                     startedThreads; //!< Total number of started threads
   u_short                     livingThreads;  //!< Number of still living threads
}
@end

/*!
 * @category MyImageListItem(private)
 * @abstract Internal methods
 */
@interface MyImageListItem(private)
//! Common part of standard and decoder initializers
- (void) setURL:(NSURL*)url ;
//! Initializer for movie items
- (id) initWithParent:(MyImageListItem*)parent withIndex:(u_long)index ;
//! Set selection state with tri state option
- (void) setSelectionState :(int)state;
//! Update the container selection state according to the children
- (void) childrenSelectionChanged ;
//! Initialize the name of this item
- (void) setName :(NSString*)name ;

/*!
 * @method getDarkFrame
 * @abstract Shortcut to speed up calibration frames retrieval
 * @result The dark frame for this item
 */
- (LynkeosImageBuffer*) getDarkFrame ;

/*!
 * @method getFlatField
 * @abstract Shortcut to speed up calibration frames retrieval
 * @result The flat field for this item
 */
- (LynkeosImageBuffer*) getFlatField ;
@end

/** Comparison function for sorting readers (highest priority first) */
static NSComparisonResult compareReaders( id obj1, id obj2, void *ctx )
{
   int p1 = ((LynkeosReaderRegistry*)obj1)->priority;
   int p2 = ((LynkeosReaderRegistry*)obj2)->priority;

   if ( p1 == p2 )
      return( NSOrderedSame );
   else if ( p1 > p2 )
      return( NSOrderedAscending );

   return( NSOrderedDescending );
}

@implementation ParallelInterpolationArgs
@end

@implementation MyImageListItem(private)

- (void) setURL:(NSURL*)url
{
   if ( url != nil)
   {
      MyPluginsController *plugins = [MyPluginsController defaultPluginController];
      NSDictionary *myImageFileTypes = [plugins getImageReaders];
      NSDictionary *myMovieFileTypes = [plugins getMovieReaders];

      _itemURL = [url retain];

      _itemName = [[NSFileManager defaultManager] displayNameAtPath: [_itemURL path]];
      [_itemName retain];

      // Find the reader class which declares this file type, 
      // and accepts to open this file
      NSMutableArray *readers = [NSMutableArray array];
      NSEnumerator *list;
      LynkeosReaderRegistry *item;
      NSString *ext = [[[url path] pathExtension] lowercaseString];

      [readers addObjectsFromArray:[myMovieFileTypes objectForKey:ext]];
#if !defined GNUSTEP
      [readers addObjectsFromArray:[myMovieFileTypes objectForKey: NSHFSTypeOfFile([url path])]];
      NSString *uti;
      if ([url getResourceValue:&uti forKey:NSURLTypeIdentifierKey error:nil])
         [readers addObjectsFromArray:[myMovieFileTypes objectForKey: uti]];
      else
         uti = nil;
#endif
      [readers addObjectsFromArray:[myImageFileTypes objectForKey:ext]];
#if !defined GNUSTEP
      [readers addObjectsFromArray:[myImageFileTypes objectForKey: NSHFSTypeOfFile([url path])]];
      if (uti != nil)
         [readers addObjectsFromArray:[myImageFileTypes objectForKey: uti]];
#endif

      [readers sortUsingFunction:compareReaders context:NULL];

      // Try the readers until one accepts
      list = [readers objectEnumerator];
      while( (item = [list nextObject]) != nil )
      {
         _reader = [[item->reader alloc] initWithURL:url];
         if ( _reader != nil )
         {
            if ( [item->reader conformsToProtocol: @protocol(LynkeosCustomFileReader)] )
               _isCustomReader = YES;
            else
               _isCustomReader = NO;

            // Found it !
            break;
         }
      }

      if ( _reader != nil )
      {
         // Try to cache some characteristics
         _nPlanes = [_reader numberOfPlanes];
         [_reader imageWidth:&_size.width height:&_size.height];
      }
      else
         // Bad luck
         NSLog( @"Unable to create item for %@", [url absoluteString] );
   }
}

- (id) initWithParent:(MyImageListItem*)parent withIndex:(u_long)index
{
   if ( (self = [self init]) != nil )
   {
      _parent = parent;
      _reader = [parent->_reader retain];
      _itemName = [parent->_itemName retain];
      _index = index;
      _size = parent->_size;
      _nPlanes = parent->_nPlanes;
      [self  setParametersParent:parent->_parameters];
   }

   return( self );
}

- (void) setSelectionState :(int)state 
{
   _selection_state = state;
}

- (void) childrenSelectionChanged
{
   NSEnumerator* list = [_childList objectEnumerator];
   id item;
   unsigned int selection_count;

   // Recount the selection
   selection_count = 0;
   while ( (item = [list nextObject]) != nil )
      if ( [item getSelectionState] > 0 )
         selection_count ++;

   // Update the state
   if ( selection_count == 0 )
      [self setSelectionState:NSOffState];
   else if ( selection_count == [_childList count] )
      [self setSelectionState:NSOnState];
   else
      [self setSelectionState:NSMixedState];
}

- (void) setName :(NSString*)name
{
   if ( _itemName != nil )
      [_itemName release];
   _itemName = name;
   [_itemName retain];
}

- (LynkeosImageBuffer*) getDarkFrame
{
   if ( _dark != nil )
      return( _dark );

   // Search for a cached value in containers
   _dark = [_parent getDarkFrame];

   if ( _dark == nil )
      // Well, do it the expensive way
      _dark = (LynkeosImageBuffer*)[self getProcessingParameterWithRef:
                                                        myImageListItemDarkFrame
                                                             forProcessing:nil];

   if ( _dark != nil )
      [_dark retain];

   return( _dark );
}

- (void) one_thread_interpolate:(id)argument
{
   ParallelInterpolationArgs * const args = argument;
   id <LynkeosInterpolator> interpolator;
   u_short ourY = 0;

   // Count up on entry
   if ( _processStrategy == ParallelizedStrategy )
   {
      [args->lock lock];
      if ( args->startedThreads < numberOfCpus )
         args->startedThreads++;
      else
         NSLog( @"Too much thread start in one_thread_process_image" );
      args->livingThreads++;
      if ( args->startedThreads == numberOfCpus )
         [args->lock unlockWithCondition: ProcessStarted];
      else
         [args->lock unlock];
   }

//   NSLog(@"Interpolator started for %@ in rect %d,%d,%dx%d with transform\n\t[%.6f, %.6f; %.6f, %.6f; %.1f, %.1f]",
//         _itemName,
//         args->rect.origin.x, args->rect.origin.y, args->rect.size.width, args->rect.size.height,
//         args->transform.m11, args->transform.m12,
//         args->transform.m21, args->transform.m22,
//         args->transform.tX, args->transform.tY);
   interpolator = [[[args->interpolatorClass alloc] initWithItem:self
                                                          inRect:args->rect
                                              withNumberOfPlanes:args->buffer->_nPlanes
                                                    withTranform:args->transform
                                                     withOffsets:args->offsets
                                                  withParameters:nil]
                   autorelease];

   // Process by sharing lines with other threads
   for(;;)
   {
      ourY = args->y;
      if ( ourY >= args->buffer->_h )
         break;
      if ( __sync_bool_compare_and_swap(&(args->y), ourY, ourY + 1) )
      {
         u_short x, c;
         for ( c = 0; c < args->buffer->_nPlanes; c++ )
            for ( x = 0; x < args->buffer->_w; x += sizeof(REALVECT)/sizeof(REAL) )
            {
               colorVector(args->buffer, x, ourY, c)
                  = [interpolator interpolateVectInPLane:c atX:x atY:ourY];
            }
      }
   }

   // Count down on exit
   if ( _processStrategy == ParallelizedStrategy )
   {
      [args->lock lockWhenCondition:ProcessStarted];
      if ( args->livingThreads > 0 )
         args->livingThreads--;
      else
         NSLog( @"Too much thread end in one_thread_process_image" );

      if ( args->livingThreads == 0 )
         [args->lock unlockWithCondition:ProcessEnded];
      else
         [args->lock unlock];
   }
}

- (LynkeosImageBuffer*) getFlatField
{
   if ( _flat != nil )
      return( _flat );

   // Search for a cached value in containers
   _flat = [_parent getFlatField];
   if ( _flat == nil )
      // Well, do it the expensive way
      _flat = (LynkeosImageBuffer*)[self
                          getProcessingParameterWithRef:myImageListItemFlatField
                                          forProcessing:nil];

   if ( _flat != nil )
      [_flat retain];

   return( _flat );
}
@end

@implementation MyImageListItem

+ (void) initialize
{
   // Register some of our properties as displayable in a column
   [[LynkeosColumnDescriptor defaultColumnDescriptor] registerColumn:@"select"
                                                     forProcess:myImageListItemRef
                                                      parameter:myImageListItemRef
                                                          field:@"selectionState"
                                                         format:nil];
   [[LynkeosColumnDescriptor defaultColumnDescriptor] registerColumn:@"name"
                                                     forProcess:myImageListItemRef
                                                      parameter:myImageListItemRef
                                                          field:@"name"
                                                         format:nil];
   [[LynkeosColumnDescriptor defaultColumnDescriptor] registerColumn:@"index"
                                                     forProcess:myImageListItemRef
                                                      parameter:myImageListItemRef
                                                          field:@"index"
                                                         format:nil];
}

- (id) init
{
   if ( (self = [super init]) != nil )
   {
      _reader = nil;
      _isCustomReader = NO;
      _itemURL = nil;
      _itemName = nil;
      _childList = nil;

      _index = NSNotFound;
      _parent = nil;
      _selection_state = 1;

      _flat = nil;
      _dark = nil;
   }

   return( self );
}

- (id) initWithURL :(NSURL*)url
{
   if ( (self = [self init]) != nil )
   {
      [self setURL:url];

      // Abort if there is no reader found
      if ( _reader == nil )
      {
         [self release];
         self = nil;
         return( self );
      }

      if ( [_reader conformsToProtocol:@protocol(LynkeosImageFileReader)] )
      {
         // Nothing more to do
      }

      else if ( [_reader conformsToProtocol:@protocol(LynkeosMovieFileReader)] )
      {
         // Create children for the movie images
         const u_long childrenNb = [_reader numberOfFrames];
         u_long i;

         _childList = [[NSMutableArray arrayWithCapacity:childrenNb] retain];
         for( i = 0; i < childrenNb; i++ )
            [_childList addObject:[[[MyImageListItem alloc] initWithParent:self
                                                                 withIndex:i]
                autorelease]];
      }
      else
         NSAssert( NO, @"Invalid file reader selected" );
   }

   return( self );
}

- (void) dealloc
{
   [_reader release];
   [_itemURL release];
   [_itemName release];
   [_childList release];
   if ( _dark != nil )
      [_dark release];
   if ( _flat != nil )
      [_flat release];

   [super dealloc];
}

// Coding
- (void)encodeWithCoder:(NSCoder *)encoder
{
   if ( _itemURL != nil )
   {
      // Try to resolve item path against document path
      NSURL *itemRelativeURL;

      if ( basePath != nil )
      {
         NSArray *itemPath = [[_itemURL path] pathComponents];
         NSArray *docPath = [basePath pathComponents];
         NSMutableString *relativePath = [NSMutableString string];
         NSString *itemComp, *docComp;
         NSUInteger i, j, nItems = [itemPath count], nDoc = [docPath count];

         // Scan the common part
         for ( i = 0; i < nDoc && i < nItems; i++ )
         {
            docComp = [docPath objectAtIndex:i];
            itemComp = [itemPath objectAtIndex:i];
            if ( ![itemComp isEqualToString:docComp] )
               break;
         }

         NSAssert2( i > 0, @"Doc or item URL to encode is not absolute\n%@\n%@",
                    _itemURL, basePath );

         if ( i > 1 )
         {
            // Go back from the doc to the divergence point
            for ( j = i; j < nDoc; j++ )
               [relativePath appendString:@"../"];

            // Append the item's remaining components (including file name)
            for ( ; i < nItems; i++ )
            {
               [relativePath appendString:[itemPath objectAtIndex:i]];
               if ( i < (nItems-1) )
                  [relativePath appendString:@"/"];
            }

            NSURL *baseURL = [NSURL fileURLWithPath:basePath];
            NSString *relURL =
            [relativePath stringByAddingPercentEncodingWithAllowedCharacters:
                             [NSCharacterSet URLPathAllowedCharacterSet]];
            itemRelativeURL = [NSURL URLWithString:relURL
                                      relativeToURL:baseURL];
         }
         else
            // The paths diverge immediately after "/", better be absolute
            itemRelativeURL = _itemURL;
      }
      else
         itemRelativeURL = _itemURL;

      [encoder encodeObject:itemRelativeURL forKey:K_URL_KEY];
   }
   if ( _childList != nil )
      [encoder encodeObject:_childList forKey:K_IMAGES_KEY];
   else
   {
      [encoder encodeInt64:_index forKey:K_LONG_INDEX_KEY];
      [encoder encodeBool:(_selection_state == NSOnState) 
                   forKey:K_SELECTED_KEY];
   }

   [super encodeWithCoder:encoder];
}

- (id)initWithCoder:(NSCoder *)decoder	// This is also an initialization
{
   if ( (self = [super initWithCoder:decoder]) != nil )
   {
      // Try absolute and doc relative URL resolution
      NSFileManager *fManager = [NSFileManager defaultManager];
      NSURL *itemURL = [decoder decodeObjectForKey:K_URL_KEY];
      if ( itemURL != nil && basePath != nil && ![fManager fileExistsAtPath:[itemURL path]] )
      {
         NSURL *relURL = [NSURL URLWithString:[itemURL relativeString]
                                relativeToURL:[NSURL fileURLWithPath:basePath]];
         if ( [fManager fileExistsAtPath:[relURL path]] )
            itemURL = relURL;
      }

      [self setURL:itemURL];

      _childList = [[decoder decodeObjectForKey:K_IMAGES_KEY] retain];
      if ( [decoder containsValueForKey:K_LONG_INDEX_KEY] )
         // V3 : OS X 10.8 64 bits index
         _index = [decoder decodeInt64ForKey:K_LONG_INDEX_KEY];

      else if ( [decoder containsValueForKey:K_INDEX_KEY] )
      {
         // V2 : 32 bits index
         int i = [decoder decodeInt32ForKey:K_INDEX_KEY];
         _index = (i >= 0 ? i : NSNotFound);
      }
      if ( _childList == nil )
         [self setSelected: [decoder decodeBoolForKey:K_SELECTED_KEY]];

      // V1 compatibility code
#ifndef NO_FILE_FORMAT_COMPATIBILITY_CODE
      // Compatibility code for version < V2.2

      // Try to get the black and white levels, only if the item is processed
      if ( [self getProcessingParameterWithRef:K_PROCESS_STACK_REF
                                 forProcessing:nil] != nil
           && [decoder containsValueForKey:K_MODBLACK_KEY]
           && [decoder containsValueForKey:K_MODWHITE_KEY] )
      {
         double vmin, vmax;
         u_short c;
         _black = (double*)malloc( sizeof(double)*(_nPlanes+1) );
         _white = (double*)malloc( sizeof(double)*(_nPlanes+1) );
         _gamma = (double*)malloc( sizeof(double)*(_nPlanes+1) );
         _black[_nPlanes] = [decoder decodeDoubleForKey:K_MODBLACK_KEY];
         _white[_nPlanes] = [decoder decodeDoubleForKey:K_MODWHITE_KEY];
         _gamma[_nPlanes] = [decoder decodeDoubleForKey:K_GAMMA_CORRECTION_KEY];
         if ( _gamma[_nPlanes] == 0.0 )
            _gamma[_nPlanes] = 1.0;

         [(LynkeosImageBuffer*)[self getImage] getMinLevel:&vmin
                                                          maxLevel:&vmax];
         for( c = 0; c < _nPlanes; c++ )
         {
            _black[c] = vmin;
            _white[c] = vmax;
            _gamma[c] = 1.0;
         }
      }
      // End of compatibility code
#endif

      // Fill the missing data in the children
      if ( _childList != nil )
      {
         NSEnumerator *children = [_childList objectEnumerator];
         MyImageListItem *item;

         while ( (item = [children nextObject]) != nil )
         {
            item->_parent = self;
            [item setParametersParent:_parameters];
            // When the reader is not found, we go on, and the document will
            // alert the user and delete bad items
            if ( _reader != nil )
               item->_reader = [_reader retain];
            item->_itemName = [_itemName retain];
            item->_size = _size;
            item->_nPlanes = _nPlanes;
         }

         // Refresh the parent (myself) selection state
         [self childrenSelectionChanged];
      }
   }

   return( self );
}

- (void) setMode:(ListMode_t)mode
{
   // Propagate the mode to the custom reader, if any
   if ( _isCustomReader )
   {
      [(id <LynkeosCustomFileReader>)_reader setMode:mode];
      // The mode can drive some image characteristics
      _nPlanes = [_reader numberOfPlanes];
      [_reader imageWidth:&_size.width height:&_size.height];
   }
}

// Accessors
- (NSURL*) getURL { return( _itemURL ); }

- (u_long) numberOfChildren
{
   return( _childList == nil ? 0 : [_childList count] );
}

- (int) getSelectionState { return( _selection_state ); }
- (NSNumber*) selectionState
{
   return( [NSNumber numberWithInt:_selection_state] );
}

- (NSString*)name { return( _itemName ); }

- (NSNumber*) index
{
   if ( _index == NSNotFound )
      return( nil );
   else
      return( [NSNumber numberWithUnsignedInteger:_index] );
}

- (MyImageListItem*) getParent { return( _parent ); }

- (id <LynkeosFileReader>) getReader { return(_reader ); }

- (MyImageListItem*) getChildAtIndex:(u_long)index
{
   NSAssert( _childList != nil, @"getChildAtIndex called on a leaf item" );
   return( [_childList objectAtIndex:index] );
}

- (NSUInteger) indexOfItem:(MyImageListItem*)item
{
   NSAssert( _childList != nil, @"indexOfItem called on a leaf item" );
   return( [_childList indexOfObject:item] );
}

- (void) addChild:(MyImageListItem*)item
{
   NSEnumerator *iter = [_childList objectEnumerator];
   MyImageListItem *child;
   u_long itemIndex = item->_index;
   int arrayIndex = 0;

   // Look for the first image whose index is after this one
   while ( (child = [iter nextObject]) != nil && child->_index < itemIndex )
      arrayIndex++;

   // There shall not be two images for the same movie frame
   NSAssert( child == nil || child->_index != itemIndex,
            @"Add a preexisting frame in MyImageListItem" );

   // And insert this one before it
   [_childList insertObject:item atIndex:arrayIndex];

   // Connect the parameters chain
   [item setParametersParent:_parameters];

   [self childrenSelectionChanged];
}

- (void) deleteChild:(MyImageListItem*)item
{
   NSAssert( [_childList containsObject:item], 
            @"Cannot delete a nonexistent child!" );
   [_childList removeObject:item];
   [self childrenSelectionChanged];
}

- (void) setSelected :(BOOL)value
{
   _selection_state = value ? NSOnState : NSOffState;

   if ( _childList != nil )
   {
      NSEnumerator* list = [_childList objectEnumerator];
      id item;

      // Propagate that state on all the images
      while ( (item = [list nextObject]) != nil )
         [item setSelected:value];
   }
   else
   {
      MyImageListItem *parent = [self getParent];

      if ( parent != nil )
         [parent childrenSelectionChanged];
   }

   // Notify for some change
   [_parameters notifyItemModification:self];
}

- (void) setParametersParent :(LynkeosProcessingParameterMgr*)parent;
{
   if ( _parameters->_parent != nil )
      [_parameters->_parent release];
   _parameters->_parent = [parent retain];

   // Now that the parameters are connected, get the calibration frames
   _dark = (LynkeosImageBuffer*)[self getProcessingParameterWithRef:
                                                        myImageListItemDarkFrame
                                                          forProcessing:nil];
   if ( _dark != nil )
      [_dark retain];
   _flat = (LynkeosImageBuffer*)[self getProcessingParameterWithRef:
                                                        myImageListItemFlatField
                                                          forProcessing:nil];
   if ( _flat != nil )
      [_flat retain];

   if ( _isCustomReader )
   {
      [(id <LynkeosCustomFileReader>)_reader setDarkFrame:_dark];
      [(id <LynkeosCustomFileReader>)_reader setFlatField:_flat];
   }
}

#pragma mark = LynkeosProcessableItem protocol
- (u_short) numberOfPlanes
{
   if ( _nPlanes == 0 )
      _nPlanes = [_reader numberOfPlanes];

   return( _nPlanes );
}

- (LynkeosIntegerSize) imageSize
{
   if ( _size.width == 0 && _size.height == 0 )
      [_reader imageWidth:&_size.width height:&_size.height];

   return( _size );
}

- (LynkeosImageBuffer*) getImage
{
   LynkeosImageBuffer* image = [super getImage];

   if ( image == nil )
   {
      LynkeosIntegerRect r = { {0.0,0.0}, _size };
      [self getImageSample:&image inRect:r];
   }

   return( image );
}

- (void) setOperatorsStrategy:(ImageOperatorsStrategy_t)strategy
{
   _processStrategy = strategy;
}

- (BOOL) hasImage
{
   if ( _processedImage != nil || _processedSpectrum != nil )
      return(YES);
   else
      return ( _childList == nil );
}

- (BOOL) supportsTiling
{
   if ( _processedImage != nil || _processedSpectrum != nil )
      return [super supportsTiling];
   else
      return NO;
}

#if !GNUSTEP
- (CGImageRef) getImageTileInRect:(CGRect)rect
{
   CGImageRef image = NULL;
   NSImage *nsImg = nil;

   if ( _processedImage != nil || _processedSpectrum != nil )
      image = [super getImageTileInRect:rect];

   else if ( _childList != nil )
      // No image at movie level
      ;
   else if ( _index == NSNotFound )
      // Image file
      nsImg = [_reader getNSImage];
   else
      // Movie image
      nsImg = [_reader getNSImageAtIndex:_index];

   if (nsImg != nil)
   {
      image = [nsImg CGImageForProposedRect:&rect context:NULL hints:nil];
      CGImageRetain(image);
   }

   return( image );
}
#else
- (NSImage*) getNSImage
{
   NSImage *image = nil;

   if ( _processedImage != nil || _processedSpectrum != nil )
      image = [super getNSImage];

   else if ( _childList != nil )
      // No image at movie level
      ;
   else if ( _index == NSNotFound )
      // Image file
      image = [_reader getNSImage];
   else
      // Movie image
      image = [_reader getNSImageAtIndex:_index];

   return( image );
}
#endif

- (void) getImageSample:(LynkeosImageBuffer**)buffer
                 inRect:(LynkeosIntegerRect)rect
{
   if ( _processedImage != nil || _processedSpectrum != nil )
      [super getImageSample:buffer inRect:rect];

   else
   {
      LynkeosImageBuffer* data = nil;
      LynkeosIntegerRect wRect;
      REAL * const * planes;
      u_short x, y, c;
      LynkeosImageBuffer* flat = [self getFlatField];
      LynkeosImageBuffer* dark = [self getDarkFrame];

      // No image sample should be retrieved at movie level
      NSAssert( _childList == nil, @"getImageSample called at movie level" );

      // Create an image buffer if needed
      if ( *buffer == nil )
         *buffer = [LynkeosImageBuffer imageBufferWithNumberOfPlanes: [_reader numberOfPlanes]
                                                                       width: rect.size.width
                                                                      height: rect.size.height];
      else
         // Otherwise, reset its remanent characteristics
         [*buffer resetMinMax];

      NSAssert( (*buffer)->_w == rect.size.width && (*buffer)->_h == rect.size.height,
                @"Sample size inconsistency" );

      // There is no transformation, do not use an interpolator

      // Intersect the rectangle with the image
      wRect = IntersectIntegerRect(rect, LynkeosMakeIntegerRect(0,0,_size.width,_size.height) );

      // Fill with black outside the image
      if ( wRect.size.width != rect.size.width
          || wRect.size.height < rect.size.height )
      {
         for( c = 0; c < (*buffer)->_nPlanes; c++ )
         {
            // Upper margin
            for( y = 0;
                y < wRect.origin.y-rect.origin.y && y < (*buffer)->_h;
                y++ )
               for( x = 0; x < (*buffer)->_w; x++ )
                  colorValue(*buffer,x,y,c) = 0.0;
            if ( wRect.size.width != rect.size.width )
            {
               for( ; y < wRect.origin.y+wRect.size.height-rect.origin.y
                   && y < (*buffer)->_h; y++ )
               {
                  // Left margin
                  for( x = 0;
                      x < wRect.origin.x-rect.origin.x && x < (*buffer)->_w;
                      x++ )
                     colorValue(*buffer,x,y,c) = 0.0;
                  // Right margin
                  for( x = wRect.origin.x+wRect.size.width-rect.origin.x;
                      x < (*buffer)->_w;
                      x++ )
                     colorValue(*buffer,x,y,c) = 0.0;
               }
            }
            // Bottom margin
            for( ; y < (*buffer)->_h; y++ )
               for( x = 0; x < (*buffer)->_w; x++ )
                  colorValue(*buffer,x,y,c) = 0.0;
         }
      }

      // If, for some reason, the rectangle is outside the image, it's over now
      if ( wRect.size.width == 0 || wRect.size.height == 0 )
         return;

      // Fake the planes origin for the reader or the conversion to fill the
      // intersection (the fake image is said to have rect.size.height to keep
      // the planes aligned, but we fill only wRect.size.height)
      LynkeosImageBuffer *transBuf
         = [LynkeosImageBuffer imageBufferWithData:&colorValue(*buffer,
                                                                       wRect.origin.x - rect.origin.x,
                                                                       wRect.origin.y - rect.origin.y,
                                                                       0)
                                                      copy:NO freeWhenDone:NO
                                            numberOfPlanes:(*buffer)->_nPlanes
                                                     width:wRect.size.width
                                               paddedWidth:(*buffer)->_padw
                                                    height:(*buffer)->_h];
      planes = [transBuf colorPlanes];

      // Try to get the data from a custom image if we can calibrate in it
      NSAffineTransformStruct t = {1.0, 0.0, 0.0, 1.0, 0.0, 0.0};
      NSPoint offsets[3] = {{0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}};
      if ( _isCustomReader )
         data = [self getCustomImageSampleinRect:wRect withTransform:t withOffsets:offsets];

      // And read as a last (but common ;o) resort
      if ( data == nil )
      {
         REAL * const *readPlanes;
         short readPlanesNb = [_reader numberOfPlanes];

         if ( ( (dark == nil && flat == nil) ||
               (wRect.size.width == rect.size.width
                && wRect.size.height == rect.size.height) )
             && readPlanesNb == transBuf->_nPlanes )
         {
            // Optimisation : as the planearity is the same as the reader (and
            // calibration frames), we can read directly in the buffer and spare
            // the conversion.
            // But to calibrate, we also need the sample to be fully inside the
            // image.
            data = transBuf;
            readPlanes = planes;
         }

         else
         {
            // We need a temporary buffer to read in
            data = [LynkeosImageBuffer imageBufferWithNumberOfPlanes:readPlanesNb
                                                               width:wRect.size.width
                                                              height:wRect.size.height];
            readPlanes = [(LynkeosImageBuffer*)data colorPlanes];
         }

         if ( _index == NSNotFound )
            // Image file
            [_reader getImageSample:(REAL*const*const)readPlanes
                         withPlanes:((LynkeosImageBuffer*)data)->_nPlanes
                                atX:wRect.origin.x Y:wRect.origin.y
                                  W:wRect.size.width H:wRect.size.height
                          lineWidth:((LynkeosImageBuffer*)data)->_padw];
         else
            // Movie image
            [_reader getImageSample:(REAL*const*const)readPlanes
                            atIndex:_index
                         withPlanes:((LynkeosImageBuffer*)data)->_nPlanes
                                atX:wRect.origin.x Y:wRect.origin.y
                                  W:wRect.size.width H:wRect.size.height
                          lineWidth:((LynkeosImageBuffer*)data)->_padw];
      }

      NSAssert( data != nil, @"Failed to read a sample" );

      if ( dark != nil || flat != nil )
         [data calibrateWithDarkFrame:dark flatField:flat
                                  atX:wRect.origin.x Y:wRect.origin.y];

      if ( data != transBuf )
         [data convertToPlanar:(REAL*const*const)planes
                    withPlanes:transBuf->_nPlanes
                     lineWidth:transBuf->_padw];
   }
}

- (void) getImageSample:(LynkeosImageBuffer**)buffer
                 inRect:(LynkeosIntegerRect)rect
          withTransform:(const NSAffineTransformStruct)transform
            withOffsets:(const NSPoint *)offsets
{
   int i;
   Class interpolatorClass = [LynkeosInterpolatorManager interpolatorWithScaling:UseTransform
                                                                       transform:transform];
   NSAssert(interpolatorClass != nil, @"Could not find an interpolator");


   if ( *buffer == nil )
      *buffer = [LynkeosImageBuffer imageBufferWithNumberOfPlanes: [_reader numberOfPlanes]
                                                                    width: rect.size.width
                                                                   height: rect.size.height];
   else
      // Otherwise, reset its remanent characteristics
      [*buffer resetMinMax];

   NSAssert( *buffer != nil, @"No buffer to extract a sample");
   NSAssert( (*buffer)->_w == rect.size.width
            && (*buffer)->_h == rect.size.height,
            @"Sample size inconsistency" );

   // No need to calibrate here, the interpolator will use getImageSample:inRect:, which calibrates
   ParallelInterpolationArgs *args = [[[ParallelInterpolationArgs alloc] init] autorelease];
   args->interpolatorClass = interpolatorClass;
   args->sourceItem = self;
   args->buffer = *buffer;
   args->rect = rect;
   args->transform = transform;
   args->offsets = (NSPoint*)malloc(sizeof(NSPoint)*(*buffer)->_nPlanes);
      for (i = 0; i < (*buffer)->_nPlanes; i++)
      {
         args->offsets[i] = (offsets != NULL ? offsets[i] : NSMakePoint(0.0, 0.0));
      }
   args->y = 0;
   args->lock = nil;
   args->startedThreads = 0;
   args->livingThreads = 0;

   // When parallelization is required
   if (_processStrategy == ParallelizedStrategy)
   {
      args->lock = [[NSConditionLock alloc] initWithCondition:ProcessInited];

      // Start a thread for each "other processor"
      for( i =  1; i < numberOfCpus; i++ )
         [NSThread detachNewThreadSelector:@selector(one_thread_interpolate:)
                                  toTarget:self
                                withObject:args];
   }

   // Do our part of the job
   [self one_thread_interpolate:args];

   // Finally, wait or all threads completion, if any
   if (_processStrategy == ParallelizedStrategy)
   {
      [args->lock lockWhenCondition:ProcessEnded];
      [args->lock unlock];
      [args->lock release];
   }
   free(args->offsets);
}

- (LynkeosImageBuffer*) getCustomImageSampleinRect:(LynkeosIntegerRect)rect
                                         withTransform:(const NSAffineTransformStruct)transform
                                           withOffsets:(const NSPoint *)offsets
{
   LynkeosImageBuffer* customImage = nil;

   if ([[_reader class] conformsToProtocol:@protocol(LynkeosCustomFileReader)])
   {
      NSPoint *lOffsets = (NSPoint*)malloc(sizeof(NSPoint)*_nPlanes);
      u_short p;
      for (p = 0; p < _nPlanes; p++)
         lOffsets[p] = (offsets != NULL ? offsets[p] : NSMakePoint(0.0, 0.0));

      if ( _index == NSNotFound )
         // Image file
         customImage = [(id <LynkeosCustomImageFileReader>)_reader
                                    getCustomImageSampleAtX:rect.origin.x
                                                          Y:rect.origin.y
                                                          W:rect.size.width
                                                          H:rect.size.height
                                              withTransform:transform
                                                withOffsets:lOffsets];
      else
         // Movie image
         customImage = [(id <LynkeosCustomMovieFileReader>)_reader
                                getCustomImageSampleAtIndex:_index
                                                        atX:rect.origin.x
                                                          Y:rect.origin.y
                                                          W:rect.size.width
                                                          H:rect.size.height
                                              withTransform:transform
                                                withOffsets:lOffsets];

      free(lOffsets);
   }

   return( customImage );
}


- (void) setImage:(LynkeosImageBuffer*)buffer
{
   [super setImage:buffer];

   if ( _processedImage == nil )
   {
      [_reader imageWidth:&_size.width height:&_size.height];
      _nPlanes = [_reader numberOfPlanes];
   }
}

- (void) setOriginalImage:(LynkeosImageBuffer*)buffer
{
   NSLog( @"Impossible to set the original image on MyImageListItem" );
}

- (void) revertToOriginal
{
   // Easy ! The original image always comes from the reader
   [self setImage:nil];
}

- (BOOL) isProcessed
{
   return( ![self isOriginal] );
}

- (BOOL) getBlackLevel:(double*)black whiteLevel:(double*)white
                 gamma:(double*)gamma
{
   if ( _processedImage != nil || _processedSpectrum != nil )
      return( [super getBlackLevel:black whiteLevel:white gamma:gamma] );

   else
   {
      [_reader getMinLevel:black maxLevel:white];
      *gamma = 1.0;
   }

   return( YES );
}

- (BOOL) getBlackLevel:(double*)black whiteLevel:(double*)white
                 gamma:(double*)gamma  forPlane:(u_short)plane
{
   if ( _processedImage != nil || _processedSpectrum != nil )
      return( [super getBlackLevel:black whiteLevel:white gamma:gamma
                          forPlane:plane] );

   else
   {
      [_reader getMinLevel:black maxLevel:white];
      *gamma = 1.0;
   }

   return( YES );
}

- (void) setBlackLevel:(double)black whiteLevel:(double)white
                  gamma:(double)gamma
{
   // Forget the reader and start a processed image
   if ( _processedImage == nil && _processedSpectrum == nil )
      [self setImage:[self getImage]];
   [super setBlackLevel:black whiteLevel:white gamma:gamma];
}

- (void) setBlackLevel:(double)black whiteLevel:(double)white
                 gamma:(double)gamma forPlane:(u_short)plane
{
   // Forget the reader and start a processed image
   [self setImage:[self getImage]];
   [super setBlackLevel:black whiteLevel:white gamma:gamma forPlane:plane];
}

- (BOOL) getMinLevel:(double*)vmin maxLevel:(double*)vmax
{
   if ( _processedImage != nil || _processedSpectrum != nil )
      return( [super getMinLevel:vmin maxLevel:vmax] );

   else
      [_reader getMinLevel:vmin maxLevel:vmax];

   return( YES );
}

- (BOOL) getMinLevel:(double*)vmin maxLevel:(double*)vmax
            forPlane:(u_short)plane
{
   if ( _processedImage != nil || _processedSpectrum != nil )
      return( [super getMinLevel:vmin maxLevel:vmax forPlane:plane] );

   else
      [_reader getMinLevel:vmin maxLevel:vmax];

   return( YES );
}

- (id <LynkeosProcessingParameter>) getProcessingParameterWithRef:(NSString*)ref 
                                             forProcessing:(NSString*)processing
{
   return( [self getProcessingParameterWithRef:ref forProcessing:processing
                                          goUp:YES] );
}

- (id <LynkeosProcessingParameter>) getProcessingParameterWithRef:(NSString*)ref 
                                             forProcessing:(NSString*)processing
                                                             goUp:(BOOL)goUp
{
   // Present ourselves as a parameter for displaying some fields in the GUI
   if ( [processing isEqual:myImageListItemRef] )
      return( self );
   else
      return( [_parameters getProcessingParameterWithRef:ref
                                           forProcessing:processing goUp:goUp] );
}

- (void) setProcessingParameter:(id <LynkeosProcessingParameter>)parameter
                        withRef:(NSString*)ref 
                  forProcessing:(NSString*)processing
{
   [_parameters setProcessingParameter:parameter withRef:ref 
                         forProcessing:processing];

   // Handle the shortcut for calibration frames
   if ( processing == nil )
   {
      if ( [ref isEqual:myImageListItemDarkFrame] )
      {
         if ( _dark != nil )
            [_dark release];
         _dark = (LynkeosImageBuffer*)[parameter retain];
         if ( _isCustomReader )
            [(id <LynkeosCustomFileReader>)_reader setDarkFrame:_dark];
      }
      else if ( [ref isEqual:myImageListItemFlatField] )
      {
         if ( _flat != nil )
            [_flat release];
         _flat = (LynkeosImageBuffer*)[parameter retain];
         if ( _isCustomReader )
            [(id <LynkeosCustomFileReader>)_reader setFlatField:_flat];
      }
   }


   // Notify of the change
   [_parameters notifyItemModification:self];
}

- (NSDictionary*) getMetaData
{
   return([_reader getMetaData]);
}

+ (id) imageListItemWithURL :(NSURL*)url
{
   return( [[[self alloc] initWithURL:url] autorelease] );
}

+ (NSArray*) imageListItemFileTypes
{
   MyPluginsController *plugins = [MyPluginsController defaultPluginController];
   return( [[[plugins getImageReaders] allKeys] arrayByAddingObjectsFromArray:
                                          [[plugins getMovieReaders] allKeys]]);
}

@end