File: MyLucyRichardsonView.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 (847 lines) | stat: -rw-r--r-- 25,203 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
//
//  Lynkeos
//  $Id$
//
//  Created by Jean-Etienne LAMIAUD on Sat Nov 3 2007.
//  Copyright (c) 2007-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.
//

#include "MyDocument.h"
#include "LynkeosImageBufferAdditions.h"
#include "ProcessingUtilities.h"
#include "MyLucyRichardsonView.h"

NSString * const K_PSF_KIND_KEY = @"psf kind";
NSString * const K_RADIUS_KEY = @"radius";
NSString * const K_SEL_RECTANGLE_KEY = @"selection";
NSString * const K_PSF_URL_KEY = @"psf url";

/*!
 * @abstract Update the PSF to any change
 * @discussion In image mode, the _psf field in params contains the read image
 *    (unswapped).
 * @param params The Lucy Richardson parameters
 * @param item The item which will be processed
 */
static void updatePSF( MyLucyRichardsonViewParameters *params,
                       id <LynkeosProcessableItem> item )
{
   if( item == nil || params == nil )
   {
      NSLog(@"Unexpected PSF update without item to process" );
      return;
   }

   LynkeosImageBuffer *buf = nil;
   LynkeosIntegerSize s = [item imageSize];
   LynkeosIntegerRect sel;

   if ( params->_psfKind == ImageFilePSF )
      buf = params->_psf;

   else
   {
      // Get rid of the previous PSF
      if ( params->_psf != nil )
         [params->_psf release];
   }
   params->_psf = [[LynkeosFourierBuffer alloc] initWithNumberOfPlanes:1
                                                            width:s.width
                                                           height:s.height
                                                         withGoal:FOR_DIRECT];

   switch( params->_psfKind )
   {
      case GaussianPSF:
         MakeGaussian((REAL*const*const)[params->_psf colorPlanes], s.width, s.height,
                      1, params->_psf->_padw,
                      params->_gaussianRadius );
         break;
      case SelectionPSF:
         // Retrieve the PSF sample, if any
         sel = params->_selection;
         if ( sel.size.width > 0 && sel.size.height > 0 )
         {
            sel.origin.y = s.height - sel.origin.y - sel.size.height;
            [item getImageSample:&buf inRect:sel];
         }
         break;
      case ImageFilePSF:
         break;
      default:
         NSLog( @"Invalid kind of PSF" );
         break;
   }

   if ( buf != nil )
   {
      // Calculate the barycenter of the sample
      // and the mean value of the sample perimeter
      int c, x, y, mx, my;
      double sx, sy, mpv, nb;
      u_long npv;
      double vmin, vmax, threshold;
      sx = 0.0; sy = 0.0; nb = 0.0;
      [buf getMinLevel:&vmin maxLevel:&vmax];
      threshold = (vmax-vmin)*0.707;
      mpv = 0.0; npv = 0;
      for( c = 0; c < buf->_nPlanes; c++ )
      {
         for( y = 0; y < buf->_h; y++ )
         {
            for( x = 0; x < buf->_w; x++ )
            {
               REAL v = colorValue(buf,x,y,c);
               if ( (v-vmin) >= threshold )
               {
                  sx += (double)x*(v-vmin);
                  sy += (double)y*(v-vmin);
                  nb += v;
               }
               if ( x == 0 || y == 0
                    || x == buf->_w -1 || y == buf->_h -1 )
               {
                  mpv += v;
                  npv++;
               }
            }
         }
      }
      mx = (u_short)(sx/nb + 0.5);
      my = (u_short)(sy/nb + 0.5);
      mpv /= (double)npv;

      // Build the new PSF (transpose the quadrants around the barycenter)
      for( y = 0; y < s.height; y++ )
      {
         int yp;
         if ( y < (s.height+1)/2 && y < buf->_h - my )
            yp = y + my;
         else if ( y >= (s.height+1)/2 && y >= s.height - my )
            yp = y - s.height + my;
         else
            yp = -1;

         for( x = 0; x < s.width; x++ )
         {
            REAL v = 0;
            int xp;

            if ( x < (s.width+1)/2 && x < (buf->_w - mx) )
               xp = x + mx;
            else if ( x >= (s.width+1)/2 && x >= s.width - mx )
               xp = x - s.width + mx;
            else
               xp = -1;

            if ( xp >= 0 && yp >= 0 )
            {
               for( c = 0; c < buf->_nPlanes; c++ )
                  v += colorValue(buf,xp,yp,c) - mpv;
               v /= (REAL)buf->_nPlanes;
               if ( v < 0.0 )
                  v = 0.0;
            }
            else
               v = 0.0;
            colorValue(params->_psf,x,y,0) = v;
         }
      }
   }
}

/*!
 * @abstract Swap quadrants of the PSF
 * @discussion The swap is not symmetrical for odd sizes, hence the choice
 *    between back and forth swap.
 * @param psf The PSF to swap
 * @param back Whether to swap from the original image or back to it
 */
static void swapPsf( LynkeosImageBuffer *psf, BOOL back )
{
   const u_short w = psf->_w, h = psf->_h;
   REAL *buf;
   u_short x, y, c;

   buf = (REAL*)calloc( (w > h ? w : h), sizeof(REAL) );

   for( c = 0; c < psf->_nPlanes; c++ )
   {
      // Swap lines
      for( y = 0 ; y < h; y++ )
      {
         for( x = 0 ; x < w; x++ )
         {
            if ( back )
            {
               if ( x < w/2 )
                  buf[x+(w+1)/2] = colorValue(psf,x,y,c);
               else
                  buf[x-w/2] = colorValue(psf,x,y,c);
            }
            else
            {
               if ( x < (w+1)/2 )
                  buf[x+w/2] = colorValue(psf,x,y,c);
               else
                  buf[x-(w+1)/2] = colorValue(psf,x,y,c);
            }
         }
         for( x = 0; x < w; x++ )
            colorValue(psf,x,y,c) = buf[x];
      }

      // Swap colums
      for( x = 0 ; x < w; x++ )
      {
         for( y = 0 ; y < h; y++ )
         {
            if ( back )
            {
               if ( y < h/2 )
                  buf[y+(h+1)/2] = colorValue(psf,x,y,c);
               else
                  buf[y-h/2] = colorValue(psf,x,y,c);
            }
            else
            {
               if ( y < (h+1)/2 )
                  buf[y+h/2] = colorValue(psf,x,y,c);
               else
                  buf[y-(h+1)/2] = colorValue(psf,x,y,c);
            }
         }
         for( y = 0; y < h; y++ )
            colorValue(psf,x,y,c) = buf[y];
      }
   }

   free( buf );
}

NSMutableDictionary *monitorDictionary = nil;

/*!
 * @abstract Lightweight object for monitoring image size change
 * @discussion When the image size change, the LucyRichardson parameters need to
 *    be adjusted (even if the view was not opened).
 * @ingroup Processing
 */
@interface MyLucyRichardsonMonitor : NSObject
{
   NSObject <LynkeosViewDocument>      *_document;  //!< Our document
   LynkeosIntegerSize                   _imageSize; //!< PSF image size
}

/*!
 * @abstract Process the notification of a new document creation
 * @discussion It will be used to create a monitor object for the document.
 * @param notif The notification
 */
+ (void) documentDidOpen:(NSNotification*)notif;
/*!
 * @abstract Process the notification of document closing
 * @param notif The notification
 */
+ (void) documentWillClose:(NSNotification*)notif;

/*!
 * @abstract Dedicated initializer
 * @param document The document to monitor
 * @result Initialized monitor object
 */
- (id) initWithDocument:(NSObject <LynkeosViewDocument>*)document;
/*!
 * @abstract Process a change in the document (only PSF change is of interest)
 * @param notif The notification
 */
- (void) documentChange:(NSNotification*)notif;
@end

@implementation MyLucyRichardsonMonitor
+ (void) documentDidOpen:(NSNotification*)notif
{
   MyDocument *document = [notif object];

   // Create a monitor object for this document
   [monitorDictionary setObject:
       [[[MyLucyRichardsonMonitor alloc] initWithDocument:document] autorelease]
                         forKey:[NSData dataWithBytes:&document
                                               length:sizeof(id)]];
}

+ (void) documentWillClose:(NSNotification*)notif
{
   id <LynkeosViewDocument> document = [notif object];

   // Delete the monitor object
   [monitorDictionary removeObjectForKey:[NSData dataWithBytes:&document
                                                        length:sizeof(id)]];
}

- (id) initWithDocument:(NSObject <LynkeosViewDocument>*)document
{
   if ( (self = [self init]) != nil )
   {
      _document = document;

      _imageSize = [[document imageList] imageSize];

      NSNotificationCenter *notif = [NSNotificationCenter defaultCenter];

      // Register for item change notifications
      [notif addObserver:self
                selector:@selector(documentChange:)
                    name: LynkeosItemChangedNotification
                  object:_document];
   }

   return( self );
}

- (void) dealloc
{
   // Unregister for all notifications
   [[NSNotificationCenter defaultCenter] removeObserver:self];

   [super dealloc];
}

- (void) documentChange:(NSNotification*)notif
{
   // Check if the image size has changed
   MyImageList *item = [[notif userInfo] objectForKey:LynkeosUserInfoItem];
   if ( item == [_document imageList] )
   {
      LynkeosIntegerSize newSize = [item imageSize];
      if ( newSize.width != _imageSize.width
          || newSize.height != _imageSize.height )
      {
         // Update the Lucy Richardson parameters, if any
         NSArray *stack = (NSArray*)[item getProcessingParameterWithRef:
                                                             K_PROCESS_STACK_REF
                                                          forProcessing:nil
                                                                   goUp:NO];
         if ( stack != nil )
         {
            NSEnumerator *processings = [stack objectEnumerator];
            id processing;
            while( (processing = [processings nextObject]) != nil )
            {
               if ( [processing isKindOfClass:
                                       [MyLucyRichardsonViewParameters class]] )
               {
                  MyLucyRichardsonViewParameters *param = processing;
                  if ( param->_psfKind == ImageFilePSF )
                     // Re-swap quadrants before adjusting the PSF
                     swapPsf( param->_psf, YES );
                  updatePSF(param, item);
               }
            }
         }
         _imageSize = newSize;
      }
   }
}
@end

@interface MyLucyRichardsonView(Private)
- (void) validateControls ;
- (void) zoomChange:(NSNotification*)notif ;
- (void) itemChange:(NSNotification*)notif ;
- (void) processStarted:(NSNotification*)notif ;
- (void) processEnded:(NSNotification*)notif ;
- (void) selectionRectChange:(NSNotification*)notif ;
- (void) displayPSF ;
@end

@implementation MyLucyRichardsonViewParameters
- (id) init
{
   if ( (self = [super init]) != nil )
   {
      _psfKind = GaussianPSF;
      _gaussianRadius = 2.5;
      _selection = LynkeosMakeIntegerRect(0,0,0,0);
      _psfURL = nil;
   }

   return( self );
}

- (void) dealloc
{
   if ( _psfURL != nil )
      [_psfURL release];
   [super dealloc];
}

- (void)encodeWithCoder:(NSCoder *)encoder
{
   [super encodeWithCoder:encoder];
   [encoder encodeInt:_psfKind forKey:K_PSF_KIND_KEY];
   [encoder encodeDouble:_gaussianRadius forKey:K_RADIUS_KEY];
   [encoder encodeRect:NSRectFromIntegerRect(_selection)
                forKey:K_SEL_RECTANGLE_KEY];
   [encoder encodeObject:_psfURL forKey:K_PSF_URL_KEY];
}

- (id) initWithCoder:(NSCoder *)decoder
{
   if ( (self = [super initWithCoder:decoder]) != nil )
   {
      _psfKind = [decoder decodeIntForKey:K_PSF_KIND_KEY];
      _gaussianRadius = [decoder decodeDoubleForKey:K_RADIUS_KEY];
      _selection = LynkeosIntegerRectFromNSRect(
                                [decoder decodeRectForKey:K_SEL_RECTANGLE_KEY]);
      _psfURL = [[decoder decodeObjectForKey:K_PSF_URL_KEY] retain];
   }

   return( self );
}
@end

@implementation MyLucyRichardsonView(Private)
- (void) validateControls
{
   NSInteger kind = [_psfPopup indexOfSelectedItem];

   [_iterationText setEnabled:(_item != nil && !_isProcessing)];
   [_iterationStepper setEnabled:(_item != nil && !_isProcessing)];
   [_psfPopup setEnabled:(_item != nil && !_isProcessing)];
   [_radiusText setEnabled:(_item != nil && !_isProcessing)];
   [_radiusSlider setEnabled:(_item != nil && !_isProcessing)];
   [_gaussBox setHidden:(kind != GaussianPSF)];
   [_fileBox setHidden:(kind != ImageFilePSF)];
   [_saveButton setEnabled:(_item != nil && _params->_psf != nil
                            && !_isProcessing)];
   [_loadButton setEnabled:(_item != nil && !_isProcessing)];
   if ( kind == SelectionPSF )
   {
      [_imageView removeAllSelections];
      [_imageView setSelectionMode:SingleSelection];
      [_imageView setSelection:_params->_selection
                     resizable:!_isProcessing movable:!_isProcessing];
   }
   else
   {
      [_imageView removeAllSelections];
      [_imageView setSelectionMode:NoSelection];
   }
   [_startButton setEnabled:(_item != nil && _params->_numberOfIteration != 0
                             && _params->_psf != nil && !_isProcessing)];
}

- (void) zoomChange:(NSNotification*)notif
{
   [self displayPSF];
}

- (void) itemChange:(NSNotification*)notif
{
   // Update item and parameters
   if ( _params != nil )
   {
      _params->_delegate = nil;
      [_params release];
   }
   [_window getItemToProcess:&_item andParameter:&_params forView:self];

   if ( _item != nil )
   {
      if ( _params == nil )
         // Create some new parameters
         _params = [[MyLucyRichardsonViewParameters alloc] init];
      else
         [_params retain];
      _params->_delegate = self;

      [_iterationText setIntValue:_params->_numberOfIteration];
      [_iterationStepper setIntValue:_params->_numberOfIteration];
      [_psfPopup selectItemAtIndex:_params->_psfKind];
      [_logRadius setDoubleValue:_params->_gaussianRadius];

      if ( _params->_psf == nil )
         updatePSF( _params, _item );
   }
   else
   {
      [_iterationText setStringValue:@""];
      [_psfPopup selectItemAtIndex:-1];
      [_radiusText setStringValue:@""];
   }

   // Display the image and PSF
   [_imageView displayItem:_item];
   [self displayPSF];
   [self validateControls];
}

- (void) processStarted:(NSNotification*)notif
{
   _isProcessing = YES;
   _currentIteration = 0;

   // Disable the controls
   [self validateControls];
}

- (void) processEnded:(NSNotification*)notif
{
   _isProcessing = NO;

   [_counterText setStringValue:@""];

   // Redisplay the image
   [_imageView displayItem:_item];
   // Reenable the controls
   [self validateControls];
}

- (void) selectionRectChange:(NSNotification*)notif
{
   NSAssert( _params->_psfKind == SelectionPSF, @"Unexpected selection" );
   _params->_selection = [_imageView getSelection];
   updatePSF( _params, _item);
   [self displayPSF];
}

- (void) displayPSF
{
   NSImage *image = nil;

   if ( _params !=nil && _params->_psf != nil )
   {
      double zoom = [_realImageView getZoom];
      NSSize s = [_psfImage frame].size;
      const int w = (int)s.width/zoom, h = (int)s.height/zoom;

      // Create a NSImage for the image view
      image = [[[NSImage alloc] initWithSize:s] autorelease];

      // Create a bitmap from the PSF center
      NSBitmapImageRep *bmap =
         [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
                                                  pixelsWide:w
                                                  pixelsHigh:h
                                               bitsPerSample:8
                                             samplesPerPixel:
                                                         _params->_psf->_nPlanes
                                                    hasAlpha:NO
                                                    isPlanar:YES
                                              colorSpaceName:
                                                (_params->_psf->_nPlanes == 1 ?
                                                 NSCalibratedWhiteColorSpace :
                                                 NSCalibratedRGBColorSpace)
                                                 bytesPerRow:w
                                                bitsPerPixel:8] autorelease];
      double vmin, vmax, scale;
      u_short c, x, y;
      unsigned char *planes[5];

      [_params->_psf getMinLevel:&vmin maxLevel:&vmax];
      scale = 255.9/(vmax-vmin);

      [bmap getBitmapDataPlanes:planes];
      for( c = 0; c < _params->_psf->_nPlanes; c++ )
      {
         for( y = 0; y < h; y++ )
         {
            unsigned short yp;

            if ( y < h/2 )
               yp = _params->_psf->_h - h/2 + y;
            else
               yp = y - h/2;

            for( x = 0; x < w; x++ )
            {
               unsigned short xp;

               if ( x < w/2 )
                  xp = _params->_psf->_w - w/2 + x;
               else
                  xp = x - w/2;

               planes[c][y*w+x] = (int)(scale*(colorValue(_params->_psf,xp,yp,c)
                                               - vmin));
            }
         }
      }

      [image addRepresentation:bmap];
   }

   // Set the image in the view
   [_psfImage setImage:image];
}
@end

@implementation MyLucyRichardsonView
+ (void) initialize
{
   // Register the monitor class for document notifications
   NSNotificationCenter *notif = [NSNotificationCenter defaultCenter];

   monitorDictionary = [[NSMutableDictionary alloc] initWithCapacity:1];

   [notif addObserver:[MyLucyRichardsonMonitor class]
             selector:@selector(documentDidOpen:)
                 name:LynkeosDocumentDidOpenNotification
               object:nil];
   [notif addObserver:[MyLucyRichardsonMonitor class]
             selector:@selector(documentWillClose:)
                 name:LynkeosDocumentWillCloseNotification
               object:nil];
}

+ (BOOL) isStandardProcessingViewController { return(YES); }

+ (ProcessingViewKind_t) processingViewKindForConfig:(id <NSObject>)config
{
   NSAssert( config == nil, @"Lucy/Richardson does not support configuration" );
   return(ImageProcessingKind);
}

+ (BOOL) isViewControllingProcess:(Class)processingClass
                       withConfig:(id <NSObject>*)config
{
   *config = nil;
   return( processingClass == [MyLucyRichardson class] );
}

+ (void) getProcessingTitle:(NSString**)title
                  toolTitle:(NSString**)toolTitle
                        key:(NSString**)key
                       icon:(NSImage**)icon
                        tip:(NSString**)tip
                  forConfig:(id <NSObject>)config
{
   NSAssert( config == nil, @"Lucy/Richardson does not support configuration" );
   *title = NSLocalizedString(@"LucyRichardson",@"Lucy/Richardson menu");
   *toolTitle = NSLocalizedString(@"LucyRichardsonTool",
                                  @"Lucy/Richardson tool");
   *key = @"r";
   *icon = [NSImage imageNamed:@"LucyRichardson"];
   *tip = NSLocalizedString(@"LucyRichardsonTip",@"Lucy/Richardson tooltip");;
}

+ (unsigned int) allowedDisplaysForConfig:(id <NSObject>)config
{
   NSAssert( config == nil, @"Lucy/Richardson does not support configuration" );
   return( BottomTab|BottomTab_NoList|SeparateView|SeparateView_NoList );
}

- (id) init
{
   if ( (self = [super init]) != nil )
   {
      _document = nil;
      _window = nil;
      _imageView = nil;

      _item = nil;
      _params = nil;
      _isProcessing = NO;

      [[NSBundle mainBundle] loadNibNamed:@"MyLucyRichardson" owner:self topLevelObjects:&_nibObjects];
      [_nibObjects retain];
   }

   return( self );
}

- (void) dealloc
{
   if ( _params != nil )
   {
      _params->_delegate = nil;
      [_params release];
      [_logRadius release];
   }

   [_nibObjects release];
   [super dealloc];
}

- (id) initWithWindowController: (id <LynkeosWindowController>)window
                       document: (id <LynkeosViewDocument>)document
                  configuration: (id <NSObject>)config
{
   NSAssert( config == nil, @"Lucy/Richardson does not support configuration" );

   if ( (self = [self init]) != nil )
   {
      _document = document;
      _window = window;
      _imageView = [_window getImageView];
      _realImageView = [_window getRealImageView];
      _logRadius = [[LynkeosLogFields alloc] initWithSlider:_radiusSlider
                                          andTextField:_radiusText];

   }

   return( self );
}

- (NSView*) getProcessingView { return( _panel ); }

- (void) setActiveView:(BOOL)active
{
   NSNotificationCenter *notifCenter = [NSNotificationCenter defaultCenter];

   if ( active )
   {
      // Authorize (or not) some selections
      [_window setListSelectionAuthorization:NO];
      [_window setDataModeSelectionAuthorization:YES];
      [_window setItemSelectionAuthorization:YES];
      [_window setItemEditionAuthorization:NO];
      [_imageView removeAllSelections];

      // Register for notifications
      [notifCenter addObserver:self
                      selector:@selector(itemChange:)
                          name: LynkeosHilightedItemDidChangeNotification
                        object:_window];
      [notifCenter addObserver:self
                      selector:@selector(processStarted:)
                          name: LynkeosProcessStartedNotification
                        object:_document];
      [notifCenter addObserver:self
                      selector:@selector(processEnded:)
                          name: LynkeosProcessStackEndedNotification
                        object:_document];
      [notifCenter addObserver:self
                      selector:@selector(itemChange:)
                          name: LynkeosDataModeChangeNotification
                        object:_document];
      [notifCenter addObserver:self
                      selector:@selector(selectionRectChange:)
                          name:LynkeosImageViewSelectionRectDidChangeNotification
                        object:_imageView];
      [notifCenter addObserver:self
                      selector:@selector(zoomChange:)
                          name:LynkeosImageViewZoomDidChangeNotification
                        object:_imageView];

      // Synchronize the display
      [self itemChange:nil];
   }
   else
   {
      // Release the parameters
      if ( _params != nil )
      {
         _params->_delegate = nil;
         [_params release];
         _params = nil;
      }

      // Stop receiving notifications
      [notifCenter removeObserver:self];
   }
}

- (LynkeosProcessingViewFrame_t) preferredDisplay { return( BottomTab ); }

- (id <LynkeosProcessingParameter>) getCurrentParameters
{
   return( _params );
}

- (Class) processingClass
{
   return( [MyLucyRichardson class] );
}

- (oneway void) iterationEnded
{
   if ( [_progressButton state] == NSOnState )
      [_imageView displayItem:_item];
   _currentIteration++;
   [_counterText setIntValue:_currentIteration];
}

- (IBAction) iterationAction:(id)sender
{
   _params->_numberOfIteration = [sender intValue];

   // Reconcile controls
   if ( sender == _iterationStepper )
      [_iterationText setIntValue:_params->_numberOfIteration];
   else if ( sender == _iterationText )
      [_iterationStepper setIntValue:_params->_numberOfIteration];

   [self validateControls];
}

- (IBAction) psfTypeAction:(id)sender
{
   NSInteger newKind = [sender indexOfSelectedItem];

   if ( newKind != _params->_psfKind )
   {
      _params->_psfKind = (PsfKind_t)newKind;

      if ( newKind != ImageFilePSF )
      {
         updatePSF( _params, _item);
         [self displayPSF];
      }
   }

   [self validateControls];
}

- (IBAction) radiusAction:(id)sender
{
   _params->_gaussianRadius = [_logRadius valueFrom:sender];

   updatePSF( _params, _item);
   [self displayPSF];
}

- (IBAction) loadAction:(id)sender
{
   _params->_psf = [_window loadImage];
   updatePSF( _params, _item);
   [self displayPSF];
}

- (IBAction) saveAction:(id)sender
{
   double b[2], w[2], g[2] = {1.0,1.0};

   LynkeosImageBuffer *buf = [[_params->_psf copy] autorelease];
   // Transpose the quadrants for saving (0,0 at center)
   swapPsf(buf,YES);
   // And save
   [_params->_psf getMinLevel:&b[0] maxLevel:&w[0]];
   b[1] = b[0];
   w[1] = w[0];
   [_window saveImage:buf withBlack:b white:w gamma:g metadata:nil];
}

- (IBAction) startProcess:(id)sender
{
   _currentIteration = 0;
   [_counterText setIntValue:0];
   [_document startProcess:[MyLucyRichardson class]
                   forItem:_item parameters:_params];
}
@end