File: MyChromaticLevels.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 (505 lines) | stat: -rw-r--r-- 16,065 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
//
//  Lynkeos
//  $Id$
//
//  Created by Jean-Etienne LAMIAUD on Wed Apr 23 2008.
//  Copyright (c) 2008-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 <math.h>

#include "SMDoubleSlider.h"

#include "MyChromaticLevels.h"

#if GNUSTEP
#define NSTickMarkPositionBelow NSTickMarkBelow
#endif

static NSString *commonPlanesNames[3];

static const double DefaultLogGammaMax = 1.0-M_LN2/M_LN10;

// To allow a double slider in a NSMatrix
@interface NSMatrix(DoubleSlider)
- (void)updateBoundControllerHiValue:(double)val;
- (void)updateBoundControllerLoValue:(double)val;
@end

@implementation NSMatrix(DoubleSlider)
- (void)updateBoundControllerHiValue:(double)val
{
}
- (void)updateBoundControllerLoValue:(double)val
{
}
@end

@interface MyChromaticLevelsView(Private)
- (void) setMatrix:(NSMatrix*)matrix rows:(int)rows ;
- (void) updateMatrices ;
- (void) updateLevels ;
- (void) itemChanged:(NSNotification*)notif ;
- (void) itemModified:(NSNotification*)notif ;
@end

@implementation MyChromaticLevelsView(Private)
- (void) setMatrix:(NSMatrix*)matrix rows:(int)rows
{
   u_short i;
   for( i = 0; i < rows; i++ )
   {
      // Add the cells, contrary to the double slider prototype, every other
      // slider is a single slider for gamma
      if ( (i%2) != 0 && matrix == _levelsSliders )
      {
         NSSliderCell *proto = [matrix prototype];
         NSSliderCell *slider =
            [[[NSSliderCell alloc] init] autorelease];
         [slider setControlSize:[proto controlSize]];
         [slider setNumberOfTickMarks:7];
         [slider setTickMarkPosition:NSTickMarkPositionBelow];
         [slider setMaxValue:DefaultLogGammaMax];
         [slider setMinValue:-DefaultLogGammaMax];
         [matrix addRowWithCells:[NSArray arrayWithObject:slider]];
      }
      else if ( (i%2) != 0
                && (matrix == _whiteTextFields || matrix == _whiteSteppers) )
      {
         [matrix addRowWithCells:
                   [NSArray arrayWithObject:
                       [[[NSCell alloc] initTextCell:@""] autorelease]]];
      }
      else
         [matrix addRow];
   }

   [matrix sizeToCells];
}

- (void) updateMatrices
{
   NSSize formerSize, newSize, containerSize;
   u_short i;
   int num = 1;

   if ( _item != nil )
   {
      num = [_item numberOfPlanes];
      // Small hack: there is no need for plane levels on monochrome images
      if (num == 1 )
         num = 0;
   }
   num = (num+1)*2;

   formerSize = [_levelsNames frame].size;

   // Set the new number of rows
   [self setMatrix:_planeNames rows:num/2];
   [self setMatrix:_levelsNames rows:num];
   [self setMatrix:_blackGammaSteppers rows:num];
   [self setMatrix:_blackGammaTextFields rows:num];
   [self setMatrix:_whiteTextFields rows:num];
   [self setMatrix:_whiteSteppers rows:num];
   [self setMatrix:_levelsSliders rows:num];

   // Set the planes names and cells characteristics
   double levelsStep = [_levelStep doubleValue],
          gammaStep = [_gammaStep doubleValue];
   for( i = 0; i < num/2; i++ )
   {
      NSString *name = nil;
      if ( i == 0 )
         name = @"Global";
      else if ( i < 4 )
         name = commonPlanesNames[i-1];
      else
         name = [NSString stringWithFormat:@"Plane%d",i-1];

      [[_planeNames cellAtRow:i column:0] setStringValue:name];
      [[_levelsNames cellAtRow:i*2 column:0] setStringValue:
                  NSLocalizedString(@"LevelsNames",@"Name of \"levels\" line")];
      [[_levelsNames cellAtRow:i*2+1 column:0] setStringValue:@"Gamma"];

      [[_blackGammaSteppers cellAtRow:2*i column:0] setIncrement:levelsStep];
      [[_whiteSteppers cellAtRow:2*i column:0] setIncrement:levelsStep];
      [[_blackGammaSteppers cellAtRow:2*i+1 column:0] setIncrement:gammaStep];
   }

   // Update the controls values
   [self updateLevels];

   // Adjust view size
   newSize = [_levelsNames frame].size;
   containerSize = [_panel frame].size;
   containerSize.height += newSize.height - formerSize.height;
   [_panel setFrameSize:containerSize];
   [_panel setNeedsDisplay:YES];
}

- (void) updateLevels
{
   double b, w, g, vmin, vmax, gmax = DefaultLogGammaMax;
   BOOL enabled = (_item != nil && [_item getMinLevel:&vmin maxLevel:&vmax]);
   u_short nPlanes;
   SMDoubleSlider *slider;
   u_short i;

   if ( enabled )
   {
      nPlanes = [_item numberOfPlanes];
      BOOL isLevel;

      // Small hack: there is no need for plane levels on monochrome images
      if ( nPlanes == 1 )
         nPlanes = 0;

      // First, get the min and max values
      for( i = 0; i <= nPlanes; i++ )
      {
         // And set the controls values to the levels
         if ( i == 0 )
            isLevel = [_item getBlackLevel:&b whiteLevel:&w gamma:&g];
         else
            isLevel=[_item getBlackLevel:&b whiteLevel:&w gamma:&g forPlane:i-1];

         if ( isLevel )
         {
            if ( b < vmin )
               vmin = b;
            if ( w > vmax )
               vmax = w;
            g = -log(g)/M_LN10;
            if ( g > gmax )
               gmax = g;
            else if ( g < -gmax )
               gmax = -g;
         }
      }

      for( i = 0; i <= nPlanes; i++ )
      {
        // And set the controls values to the levels
         if ( i == 0 )
            isLevel = [_item getBlackLevel:&b whiteLevel:&w gamma:&g];
         else
            isLevel=[_item getBlackLevel:&b whiteLevel:&w gamma:&g forPlane:i-1];

         // Update all sliders bounds
         [[_blackGammaSteppers cellAtRow:2*i column:0] setMaxValue:vmax];
         [[_blackGammaSteppers cellAtRow:2*i column:0] setMinValue:vmin];
         [[_levelsSliders cellAtRow:2*i column:0] setMaxValue:vmax];
         [[_levelsSliders cellAtRow:2*i column:0] setMinValue:vmin];
         [[_whiteSteppers cellAtRow:2*i column:0] setMaxValue:vmax];
         [[_whiteSteppers cellAtRow:2*i column:0] setMinValue:vmin];
         [[_levelsSliders cellAtRow:2*i+1 column:0] setMaxValue:gmax];
         [[_levelsSliders cellAtRow:2*i+1 column:0] setMinValue:-gmax];
         [[_blackGammaSteppers cellAtRow:2*i+1 column:0] setMaxValue:
                                                              exp(M_LN10*gmax)];
         [[_blackGammaSteppers cellAtRow:2*i+1 column:0] setMinValue:
                                                             exp(-M_LN10*gmax)];

         if ( isLevel )
         {
            // Levels line
            [[_blackGammaTextFields cellAtRow:i*2 column:0] setDoubleValue:b];
            [[_blackGammaSteppers cellAtRow:i*2 column:0] setDoubleValue:b];
            slider = (SMDoubleSlider*)[_levelsSliders cellAtRow:i*2 column:0];
            [slider setDoubleLoValue:b];
            [slider setDoubleHiValue:w];
            if ( b < vmin )
               vmin = b;
            if ( w > vmax )
               vmax = b;
            [[_whiteTextFields cellAtRow:i*2 column:0] setDoubleValue:w];
            [[_whiteSteppers cellAtRow:i*2 column:0] setDoubleValue:w];
            // Gamma line
            [[_blackGammaTextFields cellAtRow:i*2+1 column:0] setDoubleValue:g];
            [[_blackGammaSteppers cellAtRow:i*2+1 column:0] setDoubleValue:g];
            g = -log(g)/M_LN10;
            [[_levelsSliders cellAtRow:i*2+1 column:0] setDoubleValue:g];
         }
         else
         {
            [[_blackGammaTextFields cellAtRow:i*2 column:0] setStringValue:@""];
            [[_whiteTextFields cellAtRow:i*2 column:0] setStringValue:@""];
            [[_blackGammaTextFields cellAtRow:i*2+1 column:0] setStringValue:@""];
         }

         [[_blackGammaTextFields cellAtRow:i*2 column:0] setEnabled:isLevel];
         [[_blackGammaSteppers cellAtRow:i*2 column:0] setEnabled:isLevel];
         [[_levelsSliders cellAtRow:i*2 column:0] setEnabled:isLevel];
         [[_whiteTextFields cellAtRow:i*2 column:0] setEnabled:isLevel];
         [[_whiteSteppers cellAtRow:i*2 column:0] setEnabled:isLevel];
         [[_blackGammaTextFields cellAtRow:i*2+1 column:0] setEnabled:isLevel];
         [[_blackGammaSteppers cellAtRow:i*2+1 column:0] setEnabled:isLevel];
         [[_levelsSliders cellAtRow:i*2+1 column:0] setEnabled:isLevel];
      }
   }
   else
   {
      nPlanes = (_item == nil ? 0 : [_item numberOfPlanes]);
      if ( nPlanes == 1 )
         nPlanes = 0;
      for( i = 0; i <= nPlanes; i++ )
      {
         [[_blackGammaTextFields cellAtRow:i*2 column:0] setStringValue:@""];
         [[_whiteTextFields cellAtRow:i*2 column:0] setStringValue:@""];
         [[_blackGammaTextFields cellAtRow:i*2+1 column:0] setStringValue:@""];
      }
   }

   [_blackGammaTextFields setEnabled:enabled];
   [_blackGammaSteppers setEnabled:enabled];
   [_whiteTextFields setEnabled:enabled];
   [_whiteSteppers setEnabled:enabled];
   [_levelsSliders setEnabled:enabled];
}

- (void) itemChanged:(NSNotification*)notif
{
   // Forget about the previous item
   if ( _item != nil )
      [_item release];
   _item = nil;

   // Get the new item
   LynkeosImageProcessingParameter *fakeParam;
   [_window getItemToProcess:&_item andParameter:&fakeParam forView:self];
   if ( _item != nil )
      [_item retain];

   // And display the new levels and the item
   [self updateMatrices];
   [_imageView displayItem:_item];
}

- (void) itemModified:(NSNotification*)notif
{
   id item = [[notif userInfo] objectForKey:LynkeosUserInfoItem];
   if ( item == _item )
      [self updateLevels];
}
@end

@implementation MyChromaticLevelsView

+ (void) initialize
{
   commonPlanesNames[0] = NSLocalizedString(@"RedPlane", @"Red plane name");
   commonPlanesNames[1] = NSLocalizedString(@"GreenPlane", @"Green plane name");
   commonPlanesNames[2] = NSLocalizedString(@"BluePlane", @"Blue plane name");
}

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

+ (ProcessingViewKind_t) processingViewKindForConfig:(id <NSObject>)config
{ return( OtherProcessingKind ); }

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

+ (void) getProcessingTitle:(NSString**)title
                  toolTitle:(NSString**)toolTitle
                        key:(NSString**)key
                       icon:(NSImage**)icon
                        tip:(NSString**)tip
                  forConfig:(id <NSObject>)config
{
   *title = NSLocalizedString(@"ChromaLevelsMenu",
                              @"Chromatic Levels menu title");
   *toolTitle = NSLocalizedString(@"ChromaLevelsTool",
                                  @"Chromatic Levels tool title");
   *key = @"";
   *icon = [NSImage imageNamed:@"ChromaticLevels"];
   *tip = NSLocalizedString(@"ChromaLevelsTip",@"Chromatic Levels tooltip");
}

+ (unsigned int) allowedDisplaysForConfig:(id <NSObject>)config
{
   return( BottomTab|BottomTab_NoList|SeparateView|SeparateView_NoList );
}

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

      _item = nil;

      [[NSBundle mainBundle] loadNibNamed:@"MyChromaticLevels" owner:self topLevelObjects:&_nibObjects];
      [_nibObjects retain];
      [_gammaStep addItemWithObjectValue:[NSNumber numberWithDouble:1.0]];
      [_gammaStep addItemWithObjectValue:[NSNumber numberWithDouble:0.1]];
      [_gammaStep addItemWithObjectValue:[NSNumber numberWithDouble:0.01]];
      [_gammaStep setDoubleValue:0.1];
   }

   return( self );
}

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

   if ( (self = [self init]) != nil )
   {
      _document = document;
      _window = window;
      _imageView = [_window getImageView];
   }

   return( self );
}

- (void) dealloc
{
   if ( _item != nil )
      [_item release];
   [_nibObjects release];

   [super dealloc];
}

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

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

- (Class) processingClass { return( nil ); }

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

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

      // Register for notifications
      [notifCenter addObserver:self selector:@selector(itemChanged:)
                          name: LynkeosHilightedItemDidChangeNotification
                        object:_window];
      [notifCenter addObserver:self selector:@selector(itemModified:)
                          name: LynkeosItemChangedNotification
                        object:_document];

      // Synchronize the display
      [self itemChanged:nil];
   }
   else
   {
      // Release the item data
      if ( _item != nil )
         [_item release];
      _item = nil;

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

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

- (IBAction) changeLevel:(id)sender
{
   NSCell *cell = [sender selectedCell];
   double b, w, g, v;
   NSInteger row, col, plane;

   [sender getRow:&row column:&col ofCell:cell];
   plane = row/2;

   // Get the text fields values
   b = [[_blackGammaTextFields cellAtRow:plane*2 column:0] doubleValue];
   w = [[_whiteTextFields cellAtRow:plane*2 column:0] doubleValue];
   g = [[_blackGammaTextFields cellAtRow:plane*2+1 column:0] doubleValue];

   // And modify needed ones if the sender was not a text field
   v = [[sender cellAtRow:row column:0] doubleValue];
   if ( sender == _levelsSliders )
   {
      if ( (row%2) == 0 )
      {
         b = [(SMDoubleSlider*)[sender cellAtRow:row column:0] doubleLoValue];
         w = [(SMDoubleSlider*)[sender cellAtRow:row column:0] doubleHiValue];
      }
      else
         // Gamma
         g = exp(-M_LN10*v);
   }
   else if ( sender == _blackGammaSteppers )
   {
      if ( (row%2) == 0 )
         b = v;
      else
         g  = v;
   }
   else if ( sender == _whiteSteppers )
      w = v;

   if ( b > w )
      b = w;

   // Update the item, the notification will reconcile all control
   if ( plane  == 0 )
      [_item setBlackLevel:b whiteLevel:w gamma:g];
   else
      [_item setBlackLevel:b whiteLevel:w gamma:g forPlane:plane-1];
   // And redisplay it
   [_imageView updateImage];
}

- (IBAction) changeLevelStep:(id)sender
{
   const double levelsStep = [_levelStep doubleValue];
   const u_short nPlanes = [_item numberOfPlanes];
   u_short i;

   for( i = 0; i <= nPlanes; i++ )
   {
      [[_blackGammaSteppers cellAtRow:2*i column:0] setIncrement:levelsStep];
      [[_whiteSteppers cellAtRow:2*i column:0] setIncrement:levelsStep];
   }
}

- (IBAction) changeGammaStep:(id)sender
{
   const double gammaStep = [_gammaStep doubleValue];
   const u_short nPlanes = [_item numberOfPlanes];
   u_short i;

   for( i = 0; i <= nPlanes; i++ )
   {
      [[_blackGammaSteppers cellAtRow:2*i+1 column:0] setIncrement:gammaStep];
   }
}
@end