-
Notifications
You must be signed in to change notification settings - Fork 74
/
UCalculate.p
2141 lines (1363 loc) · 41.3 KB
/
UCalculate.p
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
{Photoshop version 1.0.1, file: UCalculate.p
Computer History Museum, www.computerhistory.org
This material is (C)Copyright 1990 Adobe Systems Inc.
It may not be distributed to third parties.
It is licensed for non-commercial use according to
www.computerhistory.org/softwarelicense/photoshop/ }
UNIT UCalculate;
INTERFACE
USES
{$LOAD MacIntf.LOAD}
MemTypes, QuickDraw, OSIntf, ToolIntf, PackIntf,
{$LOAD UMacApp.LOAD}
UObject, UList, UMacApp,
{$LOAD UPhotoshop.LOAD}
PaletteMgr, UConstants, UVMemory, UPhotoshop,
{$LOAD}
UDialog, UBWDialog, UCommands, UProgress;
TYPE
TChannelSelector = OBJECT (TDialogItem)
fProtoView : TImageView;
fProtoDocument: TImageDocument;
fCanCreate : BOOLEAN;
fAllowMask : BOOLEAN;
fPreferMask: BOOLEAN;
fPreferRGB : BOOLEAN;
fSource: ARRAY [1..3] OF TChannelSelector;
fPickedDocument: TImageDocument;
fPickedChannel : INTEGER;
fMenu1: MenuHandle;
fMenu2: MenuHandle;
fPopUpMenu1: TPopUpMenu;
fPopUpMenu2: TPopUpMenu;
fNewChannelPick : INTEGER;
fOldChannelsPick: INTEGER;
fRGBChannelsPick: INTEGER;
fMaskChannelPick: INTEGER;
PROCEDURE TChannelSelector.IChannelSelector
(itsDialog: TBWDialog;
itsItemNumber: INTEGER;
prototype: TImageView;
canCreate: BOOLEAN;
allowMask: BOOLEAN;
preferMask: BOOLEAN;
preferRGB: BOOLEAN;
source1: TChannelSelector;
source2: TChannelSelector;
source3: TChannelSelector);
PROCEDURE TChannelSelector.Free; OVERRIDE;
PROCEDURE TChannelSelector.ForAllSameSizeDocs
(PROCEDURE DoIt (doc: TImageDocument));
PROCEDURE TChannelSelector.BuildMenu2 (VAR pick: INTEGER);
FUNCTION TChannelSelector.ItemSelected
(anItem: INTEGER;
VAR handledIt: BOOLEAN;
VAR doneWithDialog: BOOLEAN): TCommand; OVERRIDE;
PROCEDURE TChannelSelector.Validate
(VAR succeeded: BOOLEAN); OVERRIDE;
END;
TCalculateCommand = OBJECT (TBufferCommand)
fDstDocument: TImageDocument;
fDstChannel : INTEGER;
PROCEDURE TCalculateCommand.ICalculateCommand (view: TImageView);
PROCEDURE TCalculateCommand.GetOptions;
FUNCTION TCalculateCommand.ValidDestination (RGB: BOOLEAN): BOOLEAN;
PROCEDURE TCalculateCommand.DoCalculation (band: INTEGER);
FUNCTION TCalculateCommand.BandArray (doc: TImageDocument;
channel: INTEGER;
band: INTEGER): TVMArray;
PROCEDURE TCalculateCommand.CopyToBuffer (doc: TImageDocument;
channel: INTEGER;
band: INTEGER);
PROCEDURE TCalculateCommand.DoIt; OVERRIDE;
PROCEDURE TCalculateCommand.UndoIt; OVERRIDE;
PROCEDURE TCalculateCommand.RedoIt; OVERRIDE;
END;
TDuplicateChannel = OBJECT (TCalculateCommand)
fInvert: BOOLEAN;
fSrcDocument: TImageDocument;
fSrcChannel : INTEGER;
PROCEDURE TDuplicateChannel.GetOptions; OVERRIDE;
PROCEDURE TDuplicateChannel.DoCalculation (band: INTEGER); OVERRIDE;
END;
TConstantChannel = OBJECT (TCalculateCommand)
fConstant: INTEGER;
PROCEDURE TConstantChannel.GetOptions; OVERRIDE;
PROCEDURE TConstantChannel.DoCalculation (band: INTEGER); OVERRIDE;
END;
TCompositeChannels = OBJECT (TCalculateCommand)
fForeDocument: TImageDocument;
fForeChannel : INTEGER;
fMaskDocument: TImageDocument;
fMaskChannel : INTEGER;
fBackDocument: TImageDocument;
fBackChannel : INTEGER;
PROCEDURE TCompositeChannels.GetOptions; OVERRIDE;
PROCEDURE TCompositeChannels.DoCalculation (band: INTEGER); OVERRIDE;
END;
TBinaryCalculation = OBJECT (TCalculateCommand)
fSrc1Document: TImageDocument;
fSrc1Channel : INTEGER;
fSrc2Document: TImageDocument;
fSrc2Channel : INTEGER;
PROCEDURE TBinaryCalculation.PrepareCalculation;
PROCEDURE TBinaryCalculation.DoBinaryCalculation (srcPtr: Ptr;
dstPtr: Ptr;
count: INTEGER);
PROCEDURE TBinaryCalculation.DoCalculation (band: INTEGER); OVERRIDE;
END;
TBlendChannels = OBJECT (TBinaryCalculation)
fPercent: INTEGER;
fMap1: TLookUpTable;
fMap2: TLookUpTable;
PROCEDURE TBlendChannels.GetOptions; OVERRIDE;
PROCEDURE TBlendChannels.PrepareCalculation; OVERRIDE;
PROCEDURE TBlendChannels.DoBinaryCalculation
(srcPtr: Ptr; dstPtr: Ptr; count: INTEGER); OVERRIDE;
END;
TSOffsetBinary = OBJECT (TBinaryCalculation)
fIndex: INTEGER;
fScale: INTEGER;
fOffset: INTEGER;
PROCEDURE TSOffsetBinary.ISOffsetBinary (view: TImageView;
index: INTEGER);
PROCEDURE TSOffsetBinary.GetOptions; OVERRIDE;
END;
TAddChannels = OBJECT (TSOffsetBinary)
PROCEDURE TAddChannels.DoBinaryCalculation
(srcPtr: Ptr; dstPtr: Ptr; count: INTEGER); OVERRIDE;
END;
TSubtractChannels = OBJECT (TSOffsetBinary)
PROCEDURE TSubtractChannels.DoBinaryCalculation
(srcPtr: Ptr; dstPtr: Ptr; count: INTEGER); OVERRIDE;
END;
TSimpleBinary = OBJECT (TBinaryCalculation)
fIndex: INTEGER;
PROCEDURE TSimpleBinary.ISimpleBinary (view: TImageView;
index: INTEGER);
PROCEDURE TSimpleBinary.GetOptions; OVERRIDE;
END;
TMultiplyChannels = OBJECT (TSimpleBinary)
PROCEDURE TMultiplyChannels.DoBinaryCalculation
(srcPtr: Ptr; dstPtr: Ptr; count: INTEGER); OVERRIDE;
END;
TLighterChannel = OBJECT (TSimpleBinary)
PROCEDURE TLighterChannel.DoBinaryCalculation
(srcPtr: Ptr; dstPtr: Ptr; count: INTEGER); OVERRIDE;
END;
TDarkerChannel = OBJECT (TSimpleBinary)
PROCEDURE TDarkerChannel.DoBinaryCalculation
(srcPtr: Ptr; dstPtr: Ptr; count: INTEGER); OVERRIDE;
END;
TDiffOfChannels = OBJECT (TSimpleBinary)
PROCEDURE TDiffOfChannels.DoBinaryCalculation
(srcPtr: Ptr; dstPtr: Ptr; count: INTEGER); OVERRIDE;
END;
TScreenChannels = OBJECT (TSimpleBinary)
PROCEDURE TScreenChannels.DoBinaryCalculation
(srcPtr: Ptr; dstPtr: Ptr; count: INTEGER); OVERRIDE;
END;
PROCEDURE InitCalculate;
FUNCTION DoDuplicateCommand (view: TImageView): TCommand;
FUNCTION DoConstantCommand (view: TImageView): TCommand;
FUNCTION DoCompositeCommand (view: TImageView): TCommand;
FUNCTION DoBlendCommand (view: TImageView): TCommand;
FUNCTION DoSubtractCommand (view: TImageView): TCommand;
FUNCTION DoAddCommand (view: TImageView): TCommand;
FUNCTION DoMultiplyCommand (view: TImageView): TCommand;
FUNCTION DoLighterCommand (view: TImageView): TCommand;
FUNCTION DoDarkerCommand (view: TImageView): TCommand;
FUNCTION DoDifferenceCommand (view: TImageView): TCommand;
FUNCTION DoScreenCommand (view: TImageView): TCommand;
IMPLEMENTATION
{$I UAssembly.a.inc}
{$I UCalculate.a.inc}
{$I USelect.p.inc}
VAR
gLastBlend: INTEGER;
gLastConstant: INTEGER;
gLastScale: ARRAY [1..2] OF INTEGER;
gLastOffset: ARRAY [1..2] OF INTEGER;
{*****************************************************************************}
{$S AInit}
PROCEDURE InitCalculate;
BEGIN
gLastBlend := 50;
gLastConstant := 0;
gLastScale [1] := 1000;
gLastOffset [1] := 0;
gLastScale [2] := 1000;
gLastOffset [2] := 0
END;
{*****************************************************************************}
{$S ADoCalculate}
PROCEDURE TChannelSelector.IChannelSelector (itsDialog: TBWDialog;
itsItemNumber: INTEGER;
prototype: TImageView;
canCreate: BOOLEAN;
allowMask: BOOLEAN;
preferMask: BOOLEAN;
preferRGB: BOOLEAN;
source1: TChannelSelector;
source2: TChannelSelector;
source3: TChannelSelector);
CONST
kBaseMenuID = 10000;
VAR
s: Str255;
pick: INTEGER;
PROCEDURE AddDocToMenu (doc: TImageDocument);
VAR
item: INTEGER;
BEGIN
AppendMenu (fMenu1, 'Dummy');
item := CountMItems (fMenu1);
s := doc.fTitle;
IF s[1] = '-' THEN s[1] := CHR ($D0);
SetItem (fMenu1, item, s);
IF doc = fProtoDocument THEN pick := item
END;
BEGIN
fProtoView := prototype;
fProtoDocument := TImageDocument (prototype.fDocument);
fCanCreate := canCreate;
fAllowMask := allowMask;
fPreferMask := preferMask;
fPreferRGB := preferRGB;
fSource [1] := source1;
fSource [2] := source2;
fSource [3] := source3;
fMenu1 := NIL;
fMenu2 := NIL;
IDialogItem (itsItemNumber, itsDialog, FALSE);
fMenu1 := NewMenu (kBaseMenuID + itsItemNumber, '');
ForAllSameSizeDocs (AddDocToMenu);
IF fCanCreate THEN
BEGIN
fProtoDocument.ChannelName (kDummyChannel, s);
AppendMenu (fMenu1, s);
pick := CountMItems (fMenu1);
fPickedDocument := NIL
END
ELSE
fPickedDocument := fProtoDocument;
fPopUpMenu1 := itsDialog.DefinePopUpMenu
(itsItemNumber, itsItemNumber + 1, fMenu1, pick);
BuildMenu2 (pick);
fPopUpMenu2 := itsDialog.DefinePopUpMenu
(itsItemNumber + 2, itsItemNumber + 3, fMenu2, pick)
END;
{*****************************************************************************}
{$S ADoCalculate}
PROCEDURE TChannelSelector.Free; OVERRIDE;
BEGIN
IF fMenu1 <> NIL THEN DisposeMenu (fMenu1);
IF fMenu2 <> NIL THEN DisposeMenu (fMenu2);
INHERITED Free
END;
{*****************************************************************************}
{$S ADoCalculate}
PROCEDURE TChannelSelector.ForAllSameSizeDocs
(PROCEDURE DoIt (doc: TImageDocument));
PROCEDURE TestIt (doc: TImageDocument);
BEGIN
IF (doc.fDepth = 8) AND
(doc.fMode <> IndexedColorMode) AND
(doc.fRows = fProtoDocument.fRows) AND
(doc.fCols = fProtoDocument.fCols) THEN DoIt (doc)
END;
BEGIN
gApplication.ForAllDocumentsDo (TestIt)
END;
{*****************************************************************************}
{$S ADoCalculate}
PROCEDURE TChannelSelector.BuildMenu2 (VAR pick: INTEGER);
CONST
kBaseMenuID = 11000;
VAR
s: Str255;
which: INTEGER;
channel: INTEGER;
preferRGB: BOOLEAN;
succeeded: BOOLEAN;
BEGIN
pick := 0;
fNewChannelPick := 0;
fOldChannelsPick := 0;
fRGBChannelsPick := 0;
fMaskChannelPick := 0;
fMenu2 := NewMenu (kBaseMenuID + fItemNumber, '');
IF ((fPickedDocument <> NIL) &
(fPickedDocument.fMode = RGBColorMode)) THEN
BEGIN
fProtoDocument.ChannelName (kRGBChannels, s);
AppendMenu (fMenu2, s);
fRGBChannelsPick := 1;
preferRGB := fPreferRGB;
FOR which := 1 TO 3 DO
IF NOT preferRGB AND (fSource [which] <> NIL) THEN
BEGIN
fSource [which] . Validate (succeeded);
IF fSource [which] . fPickedChannel = kRGBChannels THEN
preferRGB := TRUE
END;
IF preferRGB THEN pick := fRGBChannelsPick
END;
IF fPickedDocument <> NIL THEN
BEGIN
fOldChannelsPick := CountMItems (fMenu2) + 1;
FOR channel := 0 TO fPickedDocument.fChannels - 1 DO
BEGIN
fPickedDocument.ChannelName (channel, s);
AppendMenu (fMenu2, s)
END
END;
IF fCanCreate AND ((fPickedDocument = NIL) |
(fPickedDocument.fChannels < kMaxChannels)) THEN
BEGIN
fProtoDocument.ChannelName (fProtoDocument.fChannels, s);
AppendMenu (fMenu2, s);
fNewChannelPick := CountMItems (fMenu2);
IF pick = 0 THEN pick := fNewChannelPick
END;
IF fAllowMask AND (fPickedDocument <> NIL) THEN
IF fCanCreate OR NOT EmptyRect (fPickedDocument.fSelectionRect) THEN
BEGIN
fPickedDocument.ChannelName (kMaskChannel, s);
AppendMenu (fMenu2, s);
fMaskChannelPick := CountMItems (fMenu2);
IF fPreferMask THEN pick := fMaskChannelPick
END;
IF pick = 0 THEN
IF fPreferMask THEN
pick := fOldChannelsPick + fPickedDocument.fChannels - 1
ELSE IF fPickedDocument = fProtoDocument THEN
pick := fOldChannelsPick + Max (0, fProtoView.fChannel)
ELSE
pick := fOldChannelsPick
END;
{*****************************************************************************}
{$S ADoCalculate}
FUNCTION TChannelSelector.ItemSelected
(anItem: INTEGER;
VAR handledIt: BOOLEAN;
VAR doneWithDialog: BOOLEAN): TCommand; OVERRIDE;
VAR
pick: INTEGER;
PROCEDURE DecodePick (doc: TImageDocument);
BEGIN
pick := pick + 1;
IF pick = fPopUpMenu1.fPick THEN
fPickedDocument := doc
END;
BEGIN
ItemSelected := gNoChanges;
IF anItem = fItemNumber + 1 THEN
BEGIN
pick := 0;
fPickedDocument := NIL;
ForAllSameSizeDocs (DecodePick);
DisposeMenu (fMenu2);
BuildMenu2 (pick);
fPopUpMenu2.SetMenu (fMenu2, pick)
END
END;
{*****************************************************************************}
{$S ADoCalculate}
PROCEDURE TChannelSelector.Validate (VAR succeeded: BOOLEAN); OVERRIDE;
BEGIN
succeeded := TRUE;
fPickedChannel := fPopUpMenu2.fPick - fOldChannelsPick;
IF fPopUpMenu2.fPick = fNewChannelPick THEN
IF fPickedDocument <> NIL THEN
fPickedChannel := fPickedDocument.fChannels
ELSE
fPickedChannel := 0;
IF fPopUpMenu2.fPick = fRGBChannelsPick THEN
fPickedChannel := kRGBChannels;
IF fPopUpMenu2.fPick = fMaskChannelPick THEN
fPickedChannel := kMaskChannel
END;
{*****************************************************************************}
{$S ADoCalculate}
PROCEDURE TCalculateCommand.ICalculateCommand (view: TImageView);
VAR
fi: FailInfo;
PROCEDURE CleanUp (error: INTEGER; message: LONGINT);
BEGIN
Free
END;
BEGIN
IBufferCommand (cCalculation, view);
CatchFailures (fi, CleanUp);
GetOptions;
Success (fi)
END;
{*****************************************************************************}
{$S ADoCalculate}
PROCEDURE TCalculateCommand.GetOptions;
BEGIN
{$IFC qDebug}
ProgramBreak ('Need to override GetOptions')
{$ENDC}
END;
{*****************************************************************************}
{$S ADoCalculate}
FUNCTION TCalculateCommand.ValidDestination (RGB: BOOLEAN): BOOLEAN;
CONST
kNeedColorID = 911;
kNeedSingleID = 912;
BEGIN
IF (fDstDocument = NIL) AND RGB THEN fDstChannel := kRGBChannels;
IF RGB <> (fDstChannel = kRGBChannels) THEN
BEGIN
ValidDestination := FALSE;
IF RGB THEN
BWNotice (kNeedColorID, TRUE)
ELSE
BWNotice (kNeedSingleID, TRUE)
END
ELSE
ValidDestination := TRUE
END;
{*****************************************************************************}
{$S ADoCalculate}
PROCEDURE TCalculateCommand.DoCalculation (band: INTEGER);
BEGIN
{$IFC qDebug}
ProgramBreak ('Need to override DoCalculation')
{$ENDC}
END;
{*****************************************************************************}
{$S ADoCalculate}
FUNCTION TCalculateCommand.BandArray (doc: TImageDocument;
channel: INTEGER;
band: INTEGER): TVMArray;
BEGIN
IF channel = kRGBChannels THEN
BandArray := doc.fData [band]
ELSE
BandArray := doc.fData [channel]
END;
{*****************************************************************************}
{$S ADoCalculate}
PROCEDURE TCalculateCommand.CopyToBuffer (doc: TImageDocument;
channel: INTEGER;
band: INTEGER);
VAR
srcArray: TVMArray;
BEGIN
MoveHands (TRUE);
IF channel = kMaskChannel THEN
CopyAlphaChannel (doc, fBuffer [band])
ELSE
BEGIN
srcArray := BandArray (doc, channel, band);
srcArray.MoveArray (fBuffer [band]);
END;
MoveHands (TRUE)
END;
{*****************************************************************************}
{$S ADoCalculate}
PROCEDURE SelectAlphaChannel (doc: TImageDocument; buffer: TVMArray);
VAR
r: Rect;
old: Rect;
fi: FailInfo;
gray: INTEGER;
mask: TVMArray;
hist: THistogram;
view: TImageView;
PROCEDURE CleanUp (error: INTEGER; message: LONGINT);
BEGIN
FreeObject (mask)
END;
BEGIN
MoveHands (TRUE);
buffer.FindBounds (r);
IF EmptyRect (r) THEN
Failure (errNoPixels, msgCannotSelect);
MoveHands (TRUE);
mask := buffer.CopyRect (r, 1);
CatchFailures (fi, CleanUp);
MoveHands (TRUE);
mask.HistBytes (hist);
IF hist [255] = mask.fBlockCount * ORD4 (mask.fLogicalSize) THEN
BEGIN
mask.Free;
mask := NIL
END
ELSE
BEGIN
gray := 255;
WHILE hist [gray] = 0 DO
gray := gray - 1;
IF gray < 128 THEN
Failure (errNoPixels, msgCannotSelect)
END;
MoveHands (TRUE);
old := doc.fSelectionRect;
view := NIL;
IF MEMBER (gTarget, TImageView) THEN
IF TImageView (gTarget) . fDocument = doc THEN
view := TImageView (gTarget);
IF doc.fSelectionMask = NIL THEN
BEGIN
IF view <> NIL THEN view.UpdateImageArea (gZeroRect, FALSE);
doc.DeSelect (TRUE)
END
ELSE
BEGIN
doc.DeSelect (FALSE);
IF view <> NIL THEN view.UpdateImageArea (old, FALSE)
END;
Success (fi);
doc.Select (r, mask)
END;
{*****************************************************************************}
{$S ADoCalculate}
PROCEDURE TCalculateCommand.DoIt; OVERRIDE;
VAR
s: Str255;
fi: FailInfo;
band: INTEGER;
bands: INTEGER;
aVMArray: TVMArray;
dstView: TImageView;
PROCEDURE CleanUp1 (error: INTEGER; message: LONGINT);
BEGIN
FinishProgress
END;
PROCEDURE CleanUp2 (error: INTEGER; message: LONGINT);
BEGIN
fDstDocument.Free
END;
PROCEDURE FindView (view: TImageView);
BEGIN
IF dstView = NIL THEN
IF fDstChannel = view.fChannel THEN
dstView := view
END;
PROCEDURE DoUpdateTitle (view: TImageView);
BEGIN
view.UpdateWindowTitle
END;
BEGIN
MoveHands (TRUE);
IF fDstChannel = kRGBChannels THEN
bands := 3
ELSE
bands := 1;
FOR band := 0 TO bands - 1 DO
BEGIN
aVMArray := NewVMArray (fDoc.fRows, fDoc.fCols, bands - band);
fBuffer [band] := aVMArray
END;
CommandProgress (fCmdNumber);
CatchFailures (fi, CleanUp1);
FOR band := 0 TO bands - 1 DO
BEGIN
MoveHands (TRUE);
StartTask (1 / (bands - band));
DoCalculation (band);
FinishTask
END;
Success (fi);
CleanUp1 (0, 0);
IF fDstDocument = NIL THEN
BEGIN
fCanUndo := FALSE;
fDstDocument := TImageDocument
(gApplication.DoMakeDocument (cCalculation));
CatchFailures (fi, CleanUp2);
fDstDocument.fRows := fDoc.fRows;
fDstDocument.fCols := fDoc.fCols;
fDstDocument.fChannels := bands;
FOR band := 0 TO bands - 1 DO
BEGIN
fDstDocument.fData [band] := fBuffer [band];
fBuffer [band] := NIL
END;
fDstDocument.DefaultMode;
fDstDocument.fStyleInfo := fDoc.fStyleInfo;
fDstDocument.UntitledName (s);
fDstDocument.SetTitle (s);
fDstDocument.DoMakeViews (kForDisplay);
fDstDocument.DoMakeWindows;
gApplication.AddDocument (fDstDocument);
fDstDocument.ShowWindows;
Success (fi)
END
ELSE
BEGIN
dstView := NIL;
fDstDocument.fViewList.Each (FindView);
IF dstView = NIL THEN
IF fDstDocument = fDoc THEN
dstView := fView
ELSE
dstView := TImageView (fDstDocument.fViewList.First);
IF fDstChannel = kMaskChannel THEN
BEGIN
fCanUndo := FALSE;
fCausesChange := FALSE;
SelectAlphaChannel (fDstDocument, fBuffer [0])
END
ELSE IF fDstChannel = fDstDocument.fChannels THEN
BEGIN
fCanUndo := FALSE;
dstView.fChannel := fDstChannel;
fDstDocument.fChannels := fDstChannel + 1;
fDstDocument.fData [fDstChannel] := fBuffer [0];
fBuffer [0] := NIL;
IF fDstDocument.fMode = MonochromeMode THEN
BEGIN
fDstDocument.fMode := MultichannelMode;
fDstDocument.fViewList.Each (DoUpdateTitle)
END
ELSE
dstView.UpdateWindowTitle;
fDstDocument.UpdateStatus;
dstView.ReDither (TRUE)
END
ELSE
BEGIN
IF dstView.fChannel <> fDstChannel THEN
BEGIN
dstView.fChannel := fDstChannel;
dstView.UpdateWindowTitle;
dstView.ReDither (TRUE)
END;
UndoIt
END;
SelectWindow (dstView.fWindow.fWmgrWindow)
END;
fChangedDocument := fDstDocument
END;
{*****************************************************************************}
{$S ADoCalculate}
PROCEDURE TCalculateCommand.UndoIt; OVERRIDE;
VAR
channel: INTEGER;
saveArray: TVMArray;
PROCEDURE RedrawView (view: TImageView);
BEGIN
IF (view.fChannel = fDstChannel) OR