forked from horosproject/horos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CLUTOpacityView.mm
2405 lines (2052 loc) · 68.4 KB
/
CLUTOpacityView.mm
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
/*=========================================================================
Program: OsiriX
Copyright (c) OsiriX Team
All rights reserved.
Distributed under GNU - LGPL
See http://www.osirix-viewer.com/copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
---------------------------------------------------------------------------
This file is part of the Horos Project.
Current contributors to the project include Alex Bettarini and Danny Weissman.
Horos 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, version 3 of the License.
Horos 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 Horos. If not, see <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------------
This file is part of the Horos Project.
Current contributors to the project include Alex Bettarini and Danny Weissman.
Horos 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, version 3 of the License.
Horos 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 Horos. If not, see <http://www.gnu.org/licenses/>.
=========================================================================*/
#import "CLUTOpacityView.h"
#import "BrowserController.h"
#import "Notifications.h"
@implementation CLUTOpacityView
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self)
{
backgroundColor = [[NSColor blackColor] retain];
histogramOpacity = 0.25;
histogramColor = [[NSColor lightGrayColor] retain];
pointsColor = [[NSColor blackColor] retain];
pointsBorderColor = [[NSColor blackColor] retain];
textLabelColor = [[NSColor whiteColor] retain];
selectedPointColor = [[NSColor whiteColor] retain];
curveColor = [[NSColor grayColor] retain];
HUmin = -100000.0;
HUmax = 100000.0;
curves = [[NSMutableArray array] retain];
pointColors = [[NSMutableArray array] retain];
selectedPoint.y = -1.0;
pointDiameter = 8;
lineWidth = 3;
pointBorder = 2;
zoomFactor = 1.0;
zoomFixedPoint = 0.0;
vrViewLowResolution = NO;
didResizeVRVIew = NO;
mousePositionX = 0.0;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changePointColor:) name:NSColorPanelColorDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(computeHistogram:) name:OsirixUpdateVolumeDataNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowWillCloseNotification:) name:NSWindowWillCloseNotification object:nil];
[self createContextualMenu];
undoManager = [[NSUndoManager alloc] init];
[self updateView];
}
return self;
}
- (void)windowWillCloseNotification:(NSNotification*)notification;
{
[NSObject cancelPreviousPerformRequestsWithTarget: self selector: @selector(setCLUTtoVRViewHighRes) object: nil];
if( [notification object] == vrViewWindow)
windowWillClose = YES;
}
- (void)cleanup;
{
if(curves) [curves release];
curves = [[NSMutableArray array] retain];
if(pointColors) [pointColors release];
pointColors = [[NSMutableArray array] retain];
if( histogram)
{
free( histogram);
histogram = nil;
}
didResizeVRVIew = NO;
[self updateView];
}
- (void)dealloc
{
[NSObject cancelPreviousPerformRequestsWithTarget: self];
[[self window] setAcceptsMouseMovedEvents: NO];
[NSObject cancelPreviousPerformRequestsWithTarget: self selector: @selector(setCLUTtoVRViewHighRes) object: nil];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[curves release];
curves = nil; // IMPORTANT - used in setCLUTtoVRViewHighRes to detect if the object was destroy
if(histogram) free(histogram);
[pointColors release];
[selectedPointColor release];
[contextualMenu release];
[undoManager release];
[backgroundColor release];
[histogramColor release];
[pointsColor release];
[pointsBorderColor release];
[textLabelColor release];
[selectedPointColor release];
[curveColor release];
[super dealloc];
}
#pragma mark -
#pragma mark Contextual menu
- (void)createContextualMenu;
{
contextualMenu = [[NSMenu alloc] init];
[contextualMenu addItemWithTitle:NSLocalizedString(@"New Curve", nil) action:@selector(newCurve:) keyEquivalent:@""];
[contextualMenu addItemWithTitle:NSLocalizedString(@"Send to back", nil) action:@selector(sendToBack:) keyEquivalent:@""];
[contextualMenu addItemWithTitle:NSLocalizedString(@"Remove All Curves", nil) action:@selector(removeAllCurves:) keyEquivalent:@""];
[contextualMenu addItem:[NSMenuItem separatorItem]];
[contextualMenu addItemWithTitle:NSLocalizedString(@"Save...", nil) action:@selector(chooseNameAndSave:) keyEquivalent:@""];
}
#pragma mark -
#pragma mark Histogram
- (void)setVolumePointer:(float*)ptr width:(int)width height:(int)height numberOfSlices:(int)n;
{
volumePointer = ptr;
voxelCount = width * height * n;
}
- (void)setHUmin:(float)min HUmax:(float)max;
{
if( max - min < 1)
max = min + 1;
HUmin = min;
HUmax = max;
}
- (void)callComputeHistogram
{
protectionAgainstReentry = 0;
[self computeHistogram];
}
- (void)computeHistogram;
{
if( windowWillClose)
return;
vrViewWindow = [vrView window];
vImage_Buffer buffer;
buffer.data = volumePointer;
buffer.height = 1;
buffer.width = voxelCount;
buffer.rowBytes = voxelCount * sizeof(float);
histogramSize = (int)((HUmax-HUmin)/2);
histogramSize++;
if( histogramSize < 100)
histogramSize = 100;
if(histogram) free(histogram);
histogram = (vImagePixelCount*) malloc(sizeof(vImagePixelCount) * ((histogramSize+100)*2L));
if( histogram && buffer.data)
{
vImageHistogramCalculation_PlanarF( &buffer, histogram, histogramSize, HUmin, HUmax, kvImageDoNotTile);
int i;
vImagePixelCount min = histogram[0], max = 0;
for(i=0; i<histogramSize; i++)
{
if(histogram[i]<min) min = histogram[i];
if(histogram[i]>max) max = histogram[i];
}
float temp;
for(i=0; i<histogramSize; i++)
{
temp = ((float)(histogram[i] - min) / (float)max)*10000.0;
if (temp >= 1)
histogram[i] = (vImagePixelCount)(log(temp)*1000);
else
histogram[i] = (vImagePixelCount)temp;
}
}
if( protectionAgainstReentry++ > 20)
return;
[self simplifyHistogram];
}
- (void)simplifyHistogram;
{
if(!histogram) return;
if(histogramSize==0) return;
vImagePixelCount sum = 0;
for (int i=0 ; i<histogramSize; i++)
{
sum += histogram[i];
}
if(sum<=100) return;
int maxBin = histogramSize-1;
float binWidth = (HUmax - HUmin) / histogramSize;
float newHUmax;
for (int i=histogramSize-2; i>=0; i--)
{
if(histogram[i]<=10)
{
maxBin=i;
newHUmax = HUmin+binWidth*i*1.5; // factor 1.5 is for tunning
}
else i=-1;
}
if(maxBin < histogramSize-2)
{
if(newHUmax >= HUmax) return;
[self setHUmin:HUmin HUmax:newHUmax];
[self computeHistogram];
}
}
- (void)drawBinHistogramInRect:(NSRect)rect;
{
int i, max = 0;
for(i=2; i<histogramSize; i++)
{
if(histogram[i]>max) max = histogram[i];
}
float heightFactor = (max==0)? 1 : rect.size.height / max;
NSRect *rects = (NSRect*) malloc(sizeof(NSRect) * histogramSize);
float binWidth = rect.size.width / histogramSize;
for(i=0; i<histogramSize; i++)
{
rects[i] = NSMakeRect(i * binWidth, 0, binWidth, histogram[i] * heightFactor);
}
[histogramColor set];
NSRectFillList(rects, histogramSize);
free( rects);
}
- (void)drawHistogramInRect:(NSRect)rect;
{
NSAffineTransform *transform = [self transform];
int i;
vImagePixelCount max = 0;
for(i=0; i<histogramSize; i++)
{
if(histogram[i]>max) max = histogram[i];
}
float heightFactor = (max==0)? 1.0 : 1.0 / max;
float binWidth = (HUmax - HUmin) / histogramSize;
NSBezierPath *line = [NSBezierPath bezierPath];
[line moveToPoint:[transform transformPoint:NSMakePoint(HUmin, 0.0)]];
for(i=0; i<histogramSize; i++)
{
NSPoint pt = NSMakePoint(HUmin + i * binWidth, histogram[i] * heightFactor);
NSPoint ptInView = [transform transformPoint:pt];
[line lineToPoint:ptInView];
if(mousePositionX > pt.x-1 && mousePositionX < pt.x+1)
{
NSRect dotFrame = NSMakeRect(ptInView.x-3, ptInView.y-3, 6, 6);
NSBezierPath *dot = [NSBezierPath bezierPathWithOvalInRect:dotFrame];
NSColor *cDot = [histogramColor colorWithAlphaComponent:histogramOpacity*3.0];
[cDot set];
[dot fill];
}
}
NSPoint pt = NSMakePoint(HUmax,0.0);
pt = [transform transformPoint:pt];
[line lineToPoint:pt];
[line closePath];
NSColor *c = [histogramColor colorWithAlphaComponent:histogramOpacity];
[c set];
[line fill];
c = [histogramColor colorWithAlphaComponent:histogramOpacity*2.0];
[c set];
[line setLineWidth:1.0];
[line stroke];
}
#pragma mark -
#pragma mark Curves
- (void)newCurve;
{
NSMutableArray *theNewCurve = [NSMutableArray arrayWithCapacity:4];
NSPoint pt1, pt2, pt3, pt4;
pt1 = NSMakePoint(12, 0.0);
pt2 = NSMakePoint(202, sqrt(0.027));
pt3 = NSMakePoint(404, sqrt(0.133));
pt4 = NSMakePoint(549, sqrt(0.682));
float shift = 40.0;
if(pt1.x<HUmin || pt4.x>HUmax)
{
float middle = (HUmin + HUmax)/2.0;
float length = HUmax - HUmin;
pt1.x = middle - 0.05*length;
pt2.x = middle;
pt3.x = middle + 0.05*length;
pt4.x = middle + 0.1*length;
shift = 0.01*length;
}
BOOL needsShift = NO;
NSPoint c1, c2;
for( int i = 0; i < [curves count]; i++)
{
c1 = [[[curves objectAtIndex:i] objectAtIndex:0] pointValue];
c2 = [[[curves objectAtIndex:i] lastObject] pointValue];
needsShift = (pt1.x>c1.x-shift && pt1.x<c1.x+shift) || (pt4.x>c2.x-shift && pt4.x<c2.x+shift);
if(needsShift)
{
pt1.x += shift;
pt2.x += shift;
pt3.x += shift;
pt4.x += shift;
i=-1;
needsShift = NO;
}
}
[theNewCurve addObject:[NSValue valueWithPoint:pt1]];
[theNewCurve addObject:[NSValue valueWithPoint:pt2]];
[theNewCurve addObject:[NSValue valueWithPoint:pt3]];
[theNewCurve addObject:[NSValue valueWithPoint:pt4]];
NSMutableArray *theColors = [NSMutableArray arrayWithCapacity:4];
[theColors addObject:[NSColor colorWithDeviceRed:0.0 green:0.0 blue:0.0 alpha:1.0]];
[theColors addObject:[NSColor colorWithDeviceRed:1.0 green:0.0 blue:0.0 alpha:1.0]];
[theColors addObject:[NSColor colorWithDeviceRed:1.0 green:1.0 blue:0.0 alpha:1.0]];
[theColors addObject:[NSColor colorWithDeviceRed:1.0 green:1.0 blue:1.0 alpha:1.0]];
[self addCurveAtindex:0 withPoints:theNewCurve colors:theColors];
// select the new curve
NSPoint controlPoint = [self controlPointForCurveAtIndex:0];
selectedPoint = controlPoint;
nothingChanged = NO;
clutChanged = YES;
vrViewLowResolution = NO;
[self updateView];
}
//- (void)fillCurvesInRect:(NSRect)rect;
//{
// int i, j;
//
// NSAffineTransform* transform = [self transform];
//
// for (i=[curves count]-1; i>=0; i--)
// {
// NSArray *aCurve = [curves objectAtIndex:i];
//
// // GRADIENT FILL
// NSRect smallRect;
// NSPoint p0, p1;
// NSColor *c, *c0, *c1;
// for (j=0; j<[aCurve count]-1; j++)
// {
// p0 = [transform transformPoint:[[aCurve objectAtIndex:j] pointValue]];
// p1 = [transform transformPoint:[[aCurve objectAtIndex:j+1] pointValue]];
// c0 = [[pointColors objectAtIndex:i] objectAtIndex:j];
// c1 = [[pointColors objectAtIndex:i] objectAtIndex:j+1];
// int numberOfSmallRect = p1.x - p0.x + 1;
// int n;
// for(n=0; n<numberOfSmallRect; n++)
// {
// if(p0.y<p1.y)
// smallRect = NSMakeRect(p0.x+n, 0, 2, ((numberOfSmallRect-n)*p0.y+n*p1.y)/numberOfSmallRect);
// else
// smallRect = NSMakeRect(p0.x+n-1, 0, 2, ((numberOfSmallRect-n)*p0.y+n*p1.y)/numberOfSmallRect);
//
// c = [c0 blendedColorWithFraction:(float)n/(float)numberOfSmallRect ofColor:c1];
// [c set];
// NSRectFill(smallRect);
// }
// }
// }
//}
- (void)fillCurvesInRect:(NSRect)rect;
{
NSAffineTransform* transform = [self transform];
for (int i=(long)[curves count]-1; i>=0; i--)
{
NSArray *aCurve = [curves objectAtIndex:i];
CGFloat *locations = (CGFloat*)malloc(sizeof(CGFloat)*[aCurve count]); // for NSGradient
NSBezierPath *line = [NSBezierPath bezierPath];
NSPoint p0 = [[aCurve objectAtIndex:0] pointValue];
[line moveToPoint:NSMakePoint(p0.x, 0.0)];
float minX = [[aCurve objectAtIndex:0] pointValue].x;
float maxX = [[aCurve lastObject] pointValue].x;
float d = maxX - minX;
// construct path & locations
NSPoint pt;
for (int j=0; j<[aCurve count]; j++)
{
pt = [[aCurve objectAtIndex:j] pointValue];
locations[j] = (pt.x-minX) / d;
[line lineToPoint:pt];
}
// close path
NSPoint pt_closing = [[aCurve lastObject] pointValue];
pt_closing.y = 0.0;
[line lineToPoint:pt_closing];
[line closePath];
line = [transform transformBezierPath:line];
// GRADIENT FILL
NSGradient *gradient = [[NSGradient alloc] initWithColors:[pointColors objectAtIndex:i] atLocations:locations colorSpace:[NSColorSpace genericRGBColorSpace]];
[gradient drawInBezierPath:line angle:0];
[gradient release];
free( locations);
}
}
- (void)drawCurvesInRect:(NSRect)rect;
{
int i, j;
NSAffineTransform* transform = [self transform];
for (i=(long)[curves count]-1; i>=0; i--)
{
NSArray *aCurve = [curves objectAtIndex:i];
// CONTROL POINT SELECTED?
NSPoint controlPoint = [self controlPointForCurveAtIndex:i];
BOOL controlPointSelected = NO;
if([self isAnyPointSelected])
{
if((int) selectedPoint.x==(int) controlPoint.x && (float) selectedPoint.y==(float) controlPoint.y)
{
[selectedPointColor set];
controlPointSelected = YES;
}
}
// LINE
NSBezierPath *line = [NSBezierPath bezierPath];
[line moveToPoint:[[aCurve objectAtIndex:0] pointValue]];
for (j=1; j<[aCurve count]; j++)
{
NSPoint pt = [[aCurve objectAtIndex:j] pointValue];
[line lineToPoint:pt];
}
line = [transform transformBezierPath:line];
[curveColor set];
if(controlPointSelected) [selectedPointColor set];
[line setLineWidth:lineWidth];
[line stroke];
// CONTROL POINT (DRAW)
NSRect frame = NSMakeRect(controlPoint.x-pointDiameter*0.5, controlPoint.y-pointDiameter*0.5, pointDiameter, pointDiameter);
NSBezierPath *control = [NSBezierPath bezierPathWithRect:frame];
[control setLineWidth:pointBorder];
[pointsColor set];
[control fill];
[curveColor set];
if(controlPointSelected) [selectedPointColor set];
[control stroke];
// DOTS
NSPoint selectedPointForLabel = NSMakePoint(-1.0, -1.0);
for (j=0; j<[aCurve count]; j++)
{
NSPoint pt = [[aCurve objectAtIndex:j] pointValue];
BOOL selected = NO;
if([self isAnyPointSelected])
{
if((int) selectedPoint.x==(int) pt.x && (float) selectedPoint.y==(float) pt.y)
{
selected = YES;
}
}
//border
NSPoint pt1 = [transform transformPoint:pt];
NSRect frame1 = NSMakeRect(pt1.x-pointDiameter*0.5-pointBorder, pt1.y-pointDiameter*0.5-pointBorder, pointDiameter+2*pointBorder, pointDiameter+2*pointBorder);
NSBezierPath *dot1 = [NSBezierPath bezierPathWithOvalInRect:frame1];
[pointsColor set];
[dot1 stroke];
[curveColor set];
if(selected || controlPointSelected) [selectedPointColor set];
[dot1 fill];
//inside
NSPoint pt2 = [transform transformPoint:pt];
NSRect frame = NSMakeRect(pt2.x-pointDiameter*0.5, pt2.y-pointDiameter*0.5, pointDiameter, pointDiameter);
NSBezierPath *dot = [NSBezierPath bezierPathWithOvalInRect:frame];
[pointsColor set];
[dot stroke];
NSColor *c = [[pointColors objectAtIndex:i] objectAtIndex:j];
[c set];
[dot fill];
if(selected) selectedPointForLabel = pt;
}
// LABEL FOR SELECTED POINT
if(selectedPointForLabel.y>=0.0)[self drawPointLabelAtPosition:selectedPointForLabel];
// LABEL FOR ALL POINTS
if(controlPointSelected)
{
int maxYIndex = -1;
int minYIndex = -1;
float minY = 1.0;
float maxY = 0.0;
NSPoint currentPoint;
for (j=0; j<[aCurve count]; j++)
{
currentPoint = [[aCurve objectAtIndex:j] pointValue];
if(currentPoint.y<minY)
{
minY = currentPoint.y;
minYIndex = j;
}
if(currentPoint.y>maxY)
{
maxY = currentPoint.y;
maxYIndex = j;
}
}
[self drawPointLabelAtPosition:[[aCurve objectAtIndex:0] pointValue]];
[self drawPointLabelAtPosition:[[aCurve objectAtIndex:(long)[aCurve count]-1] pointValue]];
if(minYIndex>0 && minY>0.0) [self drawPointLabelAtPosition:[[aCurve objectAtIndex:minYIndex] pointValue]];
if(maxYIndex>0) [self drawPointLabelAtPosition:[[aCurve objectAtIndex:maxYIndex] pointValue]];
}
}
}
- (void)addCurveAtindex:(int)curveIndex withPoints:(NSArray*)pointsArray colors:(NSArray*)colorsArray;
{
[[undoManager prepareWithInvocationTarget:self] deleteCurveAtIndex:curveIndex];
[curves insertObject:pointsArray atIndex:curveIndex];
[pointColors insertObject:colorsArray atIndex:curveIndex];
}
- (void)deleteCurveAtIndex:(int)curveIndex;
{
nothingChanged = NO;
clutChanged = YES;
[[undoManager prepareWithInvocationTarget:self] addCurveAtindex:curveIndex withPoints:[NSMutableArray arrayWithArray:[curves objectAtIndex:curveIndex]] colors:[NSMutableArray arrayWithArray:[pointColors objectAtIndex:curveIndex]]];
[curves removeObjectAtIndex:curveIndex];
[pointColors removeObjectAtIndex:curveIndex];
}
- (void)moveCurveAtIndex:(int)i0 toIndex:(int)i1;
{
[[undoManager prepareWithInvocationTarget:self] moveCurveAtIndex:i1 toIndex:i0];
NSMutableArray *theCurve = [curves objectAtIndex:i0];
NSMutableArray *theColors = [pointColors objectAtIndex:i0];
if(i0>i1)
{
[curves insertObject:theCurve atIndex:i1];
[pointColors insertObject:theColors atIndex:i1];
[curves removeObjectAtIndex:i0+1];
[pointColors removeObjectAtIndex:i0+1];
}
else
{
[curves insertObject:theCurve atIndex:i1+1];
[pointColors insertObject:theColors atIndex:i1+1];
[curves removeObjectAtIndex:i0];
[pointColors removeObjectAtIndex:i0];
}
}
- (void)sendToBackCurveAtIndex:(int)i;
{
if(i != (long)[curves count]-1)
{
nothingChanged = NO;
clutChanged = NO;
[self moveCurveAtIndex:i toIndex:(long)[curves count]-1];
}
}
- (void)sendToFrontCurveAtIndex:(int)i;
{
if(i != 0)
{
nothingChanged = NO;
clutChanged = NO;
[self moveCurveAtIndex:i toIndex:0];
}
}
- (int)selectedCurveIndex;
{
return selectedCurveIndex;
}
- (void)selectCurveAtIndex:(int)i;
{
if([curves count]==0) return;
NSPoint controlPoint = [self controlPointForCurveAtIndex:i];
selectedCurveIndex = i;
selectedPoint = controlPoint;
[self setCLUTtoVRView:NO];
}
- (void)setColor:(NSColor*)color forCurveAtIndex:(int)curveIndex;
{
nothingChanged = NO;
clutChanged = YES;
[[undoManager prepareWithInvocationTarget:self] setColors:[NSMutableArray arrayWithArray:[pointColors objectAtIndex:curveIndex]] forCurveAtIndex:curveIndex];
for (NSUInteger i=0; i<[[curves objectAtIndex:curveIndex] count]; i++)
{
[[pointColors objectAtIndex:curveIndex] replaceObjectAtIndex:i withObject:color];
}
}
- (void)setColors:(NSArray*)colors forCurveAtIndex:(int)curveIndex;
{
nothingChanged = NO;
clutChanged = YES;
[[undoManager prepareWithInvocationTarget:self] setColors:[NSMutableArray arrayWithArray:[pointColors objectAtIndex:curveIndex]] forCurveAtIndex:curveIndex];
for (NSUInteger i=0; i<[[curves objectAtIndex:curveIndex] count]; i++)
{
[[pointColors objectAtIndex:curveIndex] replaceObjectAtIndex:i withObject:[colors objectAtIndex:i]];
}
}
- (void)shiftCurveAtIndex:(int)curveIndex shift:(float)aShift
{
[[undoManager prepareWithInvocationTarget:self] shiftCurveAtIndex:curveIndex shift:-aShift];
NSMutableArray *theCurve = [curves objectAtIndex:curveIndex];
NSPoint pt;
for (NSUInteger i=0; i<[theCurve count]; i++)
{
pt = [[theCurve objectAtIndex:i] pointValue];
pt.y += aShift;
[theCurve replaceObjectAtIndex:i withObject:[NSValue valueWithPoint:pt]];
}
}
- (void)setCurves:(NSMutableArray*)newCurves;
{
if(curves) [curves release];
curves = [newCurves retain];
}
- (void)setPointColors:(NSMutableArray*)newPointColors;
{
if(pointColors) [pointColors release];
pointColors = [newPointColors retain];
}
#pragma mark -
#pragma mark Coordinate to NSView Transform
- (NSAffineTransform*) transform;
{
NSAffineTransform* transform = [NSAffineTransform transform];
[transform translateXBy: -HUmin*drawingRect.size.width/(HUmax-HUmin)*zoomFactor+drawingRect.origin.x yBy:0.0];
[transform scaleXBy: drawingRect.size.width/(HUmax-HUmin)*zoomFactor yBy: drawingRect.size.height];
NSAffineTransform* transform3 = [NSAffineTransform transform];
[transform3 translateXBy: -zoomFixedPoint*(zoomFactor) yBy:0.0];
[transform appendTransform: transform3];
return transform;
}
#pragma mark -
#pragma mark Global draw method
- (void)drawRect:(NSRect)rect
{
if( histogram == nil)
[self callComputeHistogram];
[backgroundColor set];
NSRectFill(rect);
sideBarRect.origin = rect.origin;
sideBarRect.size.height = rect.size.height;
sideBarRect.size.width = 30.0;
rect.origin.x += sideBarRect.size.width;
rect.size.width -= sideBarRect.size.width;
drawingRect = rect;
[self fillCurvesInRect:rect];
[self drawHistogramInRect:rect];
[self drawCurvesInRect:rect];
[self drawSideBar:sideBarRect];
}
- (void)updateView;
{
if( updateView) return; // avoid re-entry
updateView = YES;
[self setNeedsDisplay:YES];
if(clutChanged)[self setCLUTtoVRView];
clutChanged = NO;
updateView = NO;
}
#pragma mark -
#pragma mark Points
- (BOOL)selectPointAtPosition:(NSPoint)position;
{
int i, j;
for (i=0; i<[curves count]; i++)
{
NSArray *aCurve = [curves objectAtIndex:i];
for (j=0; j<[aCurve count]; j++)
{
NSPoint pt = [[aCurve objectAtIndex:j] pointValue];
NSAffineTransform* transform = [self transform];
NSPoint pt2 = [transform transformPoint:pt];
if(position.x>=pt2.x-pointDiameter && position.y>=pt2.y-pointDiameter && position.x<=pt2.x+pointDiameter && position.y<=pt2.y+pointDiameter)
{
selectedPoint = [[aCurve objectAtIndex:j] pointValue];
[[NSColorPanel sharedColorPanel] setColor:[[pointColors objectAtIndex:i] objectAtIndex:j]];
[self sendToFrontCurveAtIndex:i];
selectedCurveIndex = -1;
clutChanged = NO;
[self updateView];
[self setCLUTtoVRView:NO];
return YES;
}
}
}
[self setCLUTtoVRView:NO];
return NO;
}
- (void)unselectPoints;
{
selectedPoint.y = -1.0;
clutChanged = NO;
[self updateView];
}
- (BOOL)isAnyPointSelected;
{
return (selectedPoint.y>=0.0);
}
- (void)changePointColor:(NSNotification *)notification;
{
if([self isAnyPointSelected])
{
vrViewLowResolution = YES;
int i, j;
for (i=0; i<[curves count]; i++)
{
NSMutableArray *aCurve = [curves objectAtIndex:i];
for (j=0; j<[aCurve count]; j++)
{
NSPoint pt = [[aCurve objectAtIndex:j] pointValue];
if((int) pt.x==(int) selectedPoint.x && (float) pt.y==(float) selectedPoint.y)
{
[self setColor:[[(NSColorPanel*)[notification object] color] colorUsingColorSpaceName: NSCalibratedRGBColorSpace] forPointAtIndex:j inCurveAtIndex:i];
[self updateView];
return;
}
}
NSPoint controlPoint = [self controlPointForCurveAtIndex:i];
if((int) controlPoint.x==(int) selectedPoint.x && (float) controlPoint.y==(float) selectedPoint.y)
{
[self setColor:[[(NSColorPanel*)[notification object] color] colorUsingColorSpaceName: NSCalibratedRGBColorSpace] forCurveAtIndex:i];
[self updateView];
return;
}
}
}
}
- (void)setColor:(NSColor*)color forPointAtIndex:(int)pointIndex inCurveAtIndex:(int)curveIndex;
{
NSColor *currentColor = [[[pointColors objectAtIndex:curveIndex] objectAtIndex:pointIndex] colorUsingColorSpaceName: NSCalibratedRGBColorSpace];
NSColor *newColor = [color colorUsingColorSpaceName: NSCalibratedRGBColorSpace];
if([currentColor redComponent]!=[newColor redComponent] || [currentColor greenComponent]!=[newColor greenComponent] || [currentColor blueComponent]!=[newColor blueComponent])
{
clutChanged = YES;
nothingChanged = NO;
//vrViewLowResolution = NO;
[[undoManager prepareWithInvocationTarget:self] setColor:[[pointColors objectAtIndex:curveIndex] objectAtIndex:pointIndex] forPointAtIndex:pointIndex inCurveAtIndex:curveIndex];
[[pointColors objectAtIndex:curveIndex] replaceObjectAtIndex:pointIndex withObject:color];
}
}
- (NSPoint)legalizePoint:(NSPoint)point inCurve:(NSArray*)aCurve atIndex:(int)j;
{
if(point.y<0.0) point.y = 0.0;
if(point.y>=0.999) point.y = 0.999;
if(j>0)
if(point.x<=[[aCurve objectAtIndex:j-1] pointValue].x+10) point.x = [[aCurve objectAtIndex:j-1] pointValue].x+10;
if(j<(long)[aCurve count]-1)
if(point.x>=[[aCurve objectAtIndex:j+1] pointValue].x-10) point.x = [[aCurve objectAtIndex:j+1] pointValue].x-10;
return point;
}
- (void)drawPointLabelAtPosition:(NSPoint)pt;
{
NSMutableDictionary *attrsDictionary = [NSMutableDictionary dictionaryWithCapacity:3];
[attrsDictionary setObject:textLabelColor forKey:NSForegroundColorAttributeName];
NSAttributedString *label = [[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:NSLocalizedString(@"value : %.0f\nalpha : %1.3f", @"don't translate the 'backslash n' before 'alpha', it is a new line symbol!"), pt.x, pt.y*pt.y] attributes:attrsDictionary] autorelease];
NSAttributedString *labelValue = [[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:NSLocalizedString(@"value : %.0f", nil), pt.x] attributes:attrsDictionary] autorelease];
NSAttributedString *labelAlpha = [[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:NSLocalizedString(@"alpha : %1.3f", nil), pt.y*pt.y] attributes:attrsDictionary] autorelease];
NSAffineTransform* transform = [self transform];
NSPoint pt1 = [transform transformPoint:pt];
NSPoint labelPosition = NSMakePoint(pt1.x + pointDiameter, pt1.y + pointDiameter);
// NSRect rect = [self bounds];
NSRect rect = drawingRect;
NSRect labelBounds = [label boundingRectWithSize:rect.size options:NSStringDrawingUsesDeviceMetrics];
NSRect labelValueBounds = [labelValue boundingRectWithSize:rect.size options:NSStringDrawingUsesDeviceMetrics];
NSRect labelAlphaBounds = [labelAlpha boundingRectWithSize:rect.size options:NSStringDrawingUsesDeviceMetrics];
labelBounds.size.height *= 3.0; // because of the \n, we have 2 lines!
labelBounds.size.height += 1.0;
labelBounds.size.width = labelValueBounds.size.width;
if(labelValueBounds.size.width < labelAlphaBounds.size.width) labelBounds.size.width = labelAlphaBounds.size.width;
labelBounds.size.width += 4.0;
if(labelPosition.y+labelBounds.size.height >= rect.size.height)
{
labelPosition.y = rect.size.height - labelBounds.size.height;
}
if(labelPosition.x+labelBounds.size.width >= rect.size.width)
{
labelPosition.x = rect.size.width - labelBounds.size.width;
}
NSBezierPath *labelRect = [NSBezierPath bezierPathWithRect:NSMakeRect(labelPosition.x-2.0,labelPosition.y,labelBounds.size.width,labelBounds.size.height)];
[[[NSColor blackColor] colorWithAlphaComponent:0.5] set];
[labelRect fill];
[label drawAtPoint:labelPosition];
}
- (void)addPoint:(NSPoint)point atIndex:(int)pointIndex inCurveAtIndex:(int)curveIndex withColor:(NSColor*)color;
{
[[undoManager prepareWithInvocationTarget:self] removePointAtIndex:pointIndex inCurveAtIndex:curveIndex];
[[curves objectAtIndex:curveIndex] insertObject:[NSValue valueWithPoint:point] atIndex:pointIndex];
[[pointColors objectAtIndex:curveIndex] insertObject:color atIndex:pointIndex];
}
- (void)removePointAtIndex:(int)ip inCurveAtIndex:(int)ic;
{
NSMutableArray *theCurve = [curves objectAtIndex:ic];
if([theCurve count]<=3)
{
[self deleteCurveAtIndex:ic];
}
else if(ip==0 || ip==(long)[theCurve count]-1) return;
else
{
[[undoManager prepareWithInvocationTarget:self] addPoint:[[theCurve objectAtIndex:ip] pointValue] atIndex:ip inCurveAtIndex:ic withColor:[[pointColors objectAtIndex:ic] objectAtIndex:ip]];
[theCurve removeObjectAtIndex:ip];
[[pointColors objectAtIndex:ic] removeObjectAtIndex:ip];
}
[self unselectPoints];
[self updateView];
}
- (void)replacePointAtIndex:(int)ip inCurveAtIndex:(int)ic withPoint:(NSPoint)point;
{
[[undoManager prepareWithInvocationTarget:self] replacePointAtIndex:ip inCurveAtIndex:ic withPoint:[[[curves objectAtIndex:ic] objectAtIndex:ip] pointValue]];
[[curves objectAtIndex:ic] replaceObjectAtIndex:ip withObject:[NSValue valueWithPoint:point]];
}
#pragma mark -
#pragma mark Control Point
- (NSPoint)controlPointForCurveAtIndex:(int)i;
{
NSPoint controlPoint;
NSArray *aCurve = [curves objectAtIndex:i];
NSAffineTransform *transform = [self transform];
if([aCurve count]%2==1)
{
controlPoint.x = [[aCurve objectAtIndex:((long)[aCurve count]-1)/2] pointValue].x;
controlPoint.y = [[aCurve objectAtIndex:((long)[aCurve count]-1)/2] pointValue].y/2.0;
}
else
{
controlPoint.x = ([[aCurve objectAtIndex:[aCurve count]/2-1] pointValue].x + [[aCurve objectAtIndex:[aCurve count]/2] pointValue].x)/2.0;
controlPoint.y = ([[aCurve objectAtIndex:[aCurve count]/2-1] pointValue].y + [[aCurve objectAtIndex:[aCurve count]/2] pointValue].y)/4.0;
}
controlPoint.x = ([[aCurve lastObject] pointValue].x + [[aCurve objectAtIndex:0] pointValue].x)/2.0;
controlPoint = [transform transformPoint:controlPoint];
return controlPoint;
}
- (BOOL)selectControlPointAtPosition:(NSPoint)position;
{
NSPoint controlPoint;
for ( NSUInteger i=0; i<[curves count]; i++)
{
controlPoint = [self controlPointForCurveAtIndex:i];
if(position.x>=controlPoint.x-pointDiameter && position.y>=controlPoint.y-pointDiameter && position.x<=controlPoint.x+pointDiameter && position.y<=controlPoint.y+pointDiameter)
{
selectedPoint = controlPoint;
[self sendToFrontCurveAtIndex:i];
selectedCurveIndex = 0;