File: contrastzoom.pro

package info (click to toggle)
coyote 2019.01.29-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,316 kB
  • sloc: python: 184; makefile: 14; sh: 13
file content (1184 lines) | stat: -rw-r--r-- 44,643 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
;+
; NAME:
;       CONTRASTZOOM
;
; PURPOSE:
;
;       The purpose of this program is to demonstrate how to
;       zoom an image "in place" and how to window and level
;       (set "contrast and brightness") an image using object
;       graphics functionality. The exercise involves using
;       multiple views in an object graphics scene, and being
;       able to interact with different views in different ways.
;
; AUTHOR:
;
;       FANNING SOFTWARE CONSULTING
;       David Fanning, Ph.D.
;       1645 Sheely Drive
;       Fort Collins, CO 80526 USA
;       Phone: 970-221-0438
;       E-mail: david@idlcoyote.com
;       Coyote's Guide to IDL Programming: http://www.idlcoyote.com
;
; CATEGORY:
;
;       Widgets, Object Graphics.
;
; CALLING SEQUENCE:
;
;       ContrastZoom, image
;
; REQUIRED INPUTS:
;
;       None. The image "mr_knee.dcm" from the examples/data directory
;       is used if no data is supplied in call.
;
; OPTIONAL INPUTS
;
;       image: A 2D image array of any data type.
;
; OPTIONAL KEYWORD PARAMETERS:
;
;       COLORTABLE: The number of a color table to use as the image palette.
;       Color table 0 (grayscale) is used as a default.

;       GROUP_LEADER: The group leader for this program. When the group leader
;       is destroyed, this program will be destroyed.
;
; COMMON BLOCKS:
;
;       None.
;
; SIDE EFFECTS:
;
;       None.
;
; RESTRICTIONS:
;
;       None. The Coyote Library program VCOLORBAR is included.
;
; EXAMPLE:
;
;       To use this program with your 8-bit image data and a red-temperature
;       color scale, type:
;
;          IDL> ContrastZoom, image, Colortable=3
;
; NOTES:
;
;       The left image is used to "zoom" into a portion of the image.
;       The aspect ratio of the sub-image is always preserved. To see
;       the entire image, click and release the mouse button in this
;       window.
;
;       The center image is used to adjust the contrast and brightness
;       (sometimes called the "window" and "level" of the image. Click and
;       drag the mouse vertically to set contrast. Click and drag the mouse
;       horizontally to set brightness. To return to original values (25%
;       contrast and 75% brightness), click and release in the center image.
;
;       The color bars shows the image values of the image.
;
; MODIFICATION HISTORY:
;
;       Written by David Fanning, 18 November 2001.
;       Added second colorbar to show the relationship of the clamped
;          colors to the overall image values. 19 November 2001. DWF.
;       Changed FSC_Normalize to cgNormalize to reflect new name. 6 Feb 2013. DWF.
;-
;
;******************************************************************************************;
;  Copyright (c) 2008, by Fanning Software Consulting, Inc.                                ;
;  All rights reserved.                                                                    ;
;                                                                                          ;
;  Redistribution and use in source and binary forms, with or without                      ;
;  modification, are permitted provided that the following conditions are met:             ;
;                                                                                          ;
;      * Redistributions of source code must retain the above copyright                    ;
;        notice, this list of conditions and the following disclaimer.                     ;
;      * Redistributions in binary form must reproduce the above copyright                 ;
;        notice, this list of conditions and the following disclaimer in the               ;
;        documentation and/or other materials provided with the distribution.              ;
;      * Neither the name of Fanning Software Consulting, Inc. nor the names of its        ;
;        contributors may be used to endorse or promote products derived from this         ;
;        software without specific prior written permission.                               ;
;                                                                                          ;
;  THIS SOFTWARE IS PROVIDED BY FANNING SOFTWARE CONSULTING, INC. ''AS IS'' AND ANY        ;
;  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES    ;
;  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT     ;
;  SHALL FANNING SOFTWARE CONSULTING, INC. BE LIABLE FOR ANY DIRECT, INDIRECT,             ;
;  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED    ;
;  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;         ;
;  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND             ;
;  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT              ;
;  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS           ;
;  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                            ;
;******************************************************************************************;
PRO ContrastZoom_VColorBar::Clamp, datarange

; This method clamps the data to a particular data range.

self->GetProperty, Range=currentRange

thisclamp = Bytscl(datarange, Max=currentRange[1], Min=currentRange[0])
bar = BytScl(Replicate(1B,10) # Bindgen(self.ncolors), Min=thisclamp[0], Max=thisclamp[1])
self.thisImage->SetProperty, Data=bar
END
;-------------------------------------------------------------------------



FUNCTION ContrastZoom_VColorBar::INIT, Position=position, $
    NColors=ncolors, Title=title, Palette=palette, $
    Major=major, Minor=minor, Range=range, Color=color, $
    _Extra=extra, Name=name

   ; Catch possible errors.

Catch, theError
IF theError NE 0 THEN BEGIN
   Catch, /Cancel
   ok = Dialog_Message(!Error_State.Msg)
   Message, !Error_State.Msg, /Informational
   RETURN, 0
ENDIF

   ; Initialize model superclass.

IF (self->IDLgrModel::Init(_EXTRA=extra) NE 1) THEN RETURN, 0

    ; Define default values for keywords, if necessary.

IF N_Elements(name) EQ 0 THEN name=''
IF N_Elements(color) EQ 0 THEN self.color = [255,255,255] $
   ELSE self.color = color
thisFont = Obj_New('IDLgrFont', 'Helvetica', Size=8.0)
self.thisFont = thisFont
IF N_Elements(title) EQ 0 THEN title=''

thisTitle = Obj_New('IDLgrText', title, Color=self.color, $
    Font=thisFont, Recompute_Dimensions=2, /Enable_Formatting)

IF N_Elements(ncolors) EQ 0 THEN self.ncolors = 256 $
   ELSE self.ncolors = ncolors
IF N_Elements(palette) EQ 0 THEN BEGIN
    red = (green = (blue = BIndGen(self.ncolors)))
    self.palette = Obj_New('IDLgrPalette', red, green, blue)
ENDIF ELSE self.palette = palette
IF N_Elements(range) EQ 0 THEN self.range = [0, self.ncolors] $
   ELSE self.range = range
IF N_Elements(major) EQ 0 THEN self.major = 5 $
   ELSE self.major = major
IF N_Elements(minor) EQ 0 THEN self.minor = 4 $
   ELSE self.minor = minor
IF N_Elements(position) EQ 0 THEN self.position = [0.90, 0.10, 0.95, 0.90] $
   ELSE self.position = position

    ; Create the colorbar image. Get its size.

bar = REPLICATE(1B,10) # BINDGEN(self.ncolors)
s = SIZE(bar, /Dimensions)
xsize = s[0]
ysize = s[1]

    ; Create the colorbar image object. Add palette to it.

thisImage = Obj_New('IDLgrImage', bar, Palette=self.palette)
xs = cgNormalize([0,xsize], Position=[0,1.])
ys = cgNormalize([0,ysize], Position=[0,1.])
thisImage->SetProperty, XCoord_Conv=xs, YCoord_Conv=ys

   ; Create a polygon object. Add the image as a texture map. We do
   ; this so the image can rotate in 3D space.

thisPolygon = Obj_New('IDLgrPolygon', [0, 1, 1, 0], [0, 0, 1, 1], [0,0,0,0], $
   Texture_Map=thisImage, Texture_Coord = [[0,0], [1,0], [1,1], [0,1]], color=[255,255,255])

    ; Scale the polygon into the correct position.

xs = cgNormalize([0,1], Position=[self.position(0), self.position(2)])
ys = cgNormalize([0,1], Position=[self.position(1), self.position(3)])
thispolygon->SetProperty, XCoord_Conv=xs, YCoord_Conv=ys

    ; Create scale factors to position the axes.

longScale = cgNormalize(self.range, Position=[self.position(1), self.position(3)])
shortScale = cgNormalize([0,1], Position=[self.position(0), self.position(2)])

    ; Create the colorbar axes. 1000 indicates this location ignored.

shortAxis1 = Obj_New("IDLgrAxis", 0, Color=self.color, Ticklen=0.025, $
    Major=1, Range=[0,1], /NoText, /Exact, XCoord_Conv=shortScale,  $
    Location=[1000, self.position(1), 0.001])
shortAxis2 = Obj_New("IDLgrAxis", 0, Color=self.color, Ticklen=0.025, $0.001
    Major=1, Range=[0,1], /NoText, /Exact, XCoord_Conv=shortScale,  $
    Location=[1000, self.position(3), 0.001], TickDir=1)

textAxis = Obj_New("IDLgrAxis", 1, Color=self.color, Ticklen=0.025, $
    Major=self.major, Minor=self.minor, Title=thisTitle, Range=self.range, /Exact, $
    YCoord_Conv=longScale, Location=[self.position(0), 1000, 0.001])
textAxis->GetProperty, TickText=thisText
thisText->SetProperty, Font=self.thisFont, Recompute_Dimensions=2

longAxis2 = Obj_New("IDLgrAxis", 1, Color=self.color, /NoText, Ticklen=0.025, $
    Major=self.major, Minor=self.minor, Range=self.range, TickDir=1, $
    YCoord_Conv=longScale, Location=[self.position(2), 1000, 0.001], /Exact)

    ; Add the parts to the colorbar model.

self->Add, shortAxis1
self->Add, shortAxis2
self->Add, textAxis
self->Add, longAxis2
self->Add, thisPolygon

   ; Assign the name.

self->IDLgrModel::SetProperty, Name=name, Select_Target=1

    ; Create a container object and put the objects into it.

thisContainer = Obj_New('IDL_Container')
thisContainer->Add, thisFont
thisContainer->Add, thisImage
thisContainer->Add, thisText
thisContainer->Add, thisTitle
thisContainer->Add, self.palette
thisContainer->Add, textAxis
thisContainer->Add, shortAxis1
thisContainer->Add, shortAxis2
thisContainer->Add, longAxis2

    ; Update the SELF structure.

self.thisImage = thisImage
self.thisFont = thisFont
self.thisText = thisText
self.textAxis = textAxis
self.shortAxis1 = shortAxis1
self.shortAxis2 = shortAxis2
self.longAxis2 = longAxis2
self.thisContainer = thisContainer
self.thisTitle = thisTitle

RETURN, 1
END
;-------------------------------------------------------------------------



PRO ContrastZoom_VColorBar::Cleanup

    ; Lifecycle method to clean itself up.

Obj_Destroy, self.thisContainer
self->IDLgrMODEL::Cleanup
END
;-------------------------------------------------------------------------



PRO ContrastZoom_VColorBar::GetProperty, Position=position, Text=text, $
    Title=title, Palette=palette, Major=major, Minor=minor, $
    Range=range, Color=color, Name=name, $
    Transform=transform, _Ref_Extra=extra

    ; Get the properties of the colorbar.

IF Arg_Present(position) THEN position = self.position
IF Arg_Present(text) THEN text = self.thisText
IF Arg_Present(title) THEN self.thisTitle->GetProperty, Strings=title
IF Arg_Present(palette) THEN palette = self.palette
IF Arg_Present(major) THEN major = self.major
IF Arg_Present(minor) THEN minor = self.minor
IF Arg_Present(range) THEN range = self.range
IF Arg_Present(color) THEN color = self.color
IF Arg_Present(name) THEN self->IDLgrMODEL::GetProperty, Name=name
IF Arg_Present(transform) THEN self->IDLgrMODEL::GetProperty, Transform=transform
IF Arg_Present(extra) THEN self->IDLgrMODEL::GetProperty, _Extra=extra

END
;-------------------------------------------------------------------------



PRO ContrastZoom_VColorBar::SetProperty, Position=position, $
    Title=title, Palette=palette, Major=major, Minor=minor, $
    Range=range, Color=color, Name=name, Transform=transform, _Extra=extra

    ; Set properties of the colorbar.

IF N_Elements(position) NE 0 THEN BEGIN
    self.position = position

        ; Find the size of the image.

    self.thisImage->GetProperty, Data=image
    s = Size(image)
    xsize = s(1)
    ysize = s(2)
    xs = cgNormalize([0,xsize], Position=[position(0), position(2)])
    ys = cgNormalize([0,ysize], Position=[position(1), position(3)])
    self.thisImage->SetProperty, XCoord_Conv=xs, YCoord_Conv=ys

        ; Create new scale factors to position the axes.

    longScale = cgNormalize(self.range, $
       Position=[self.position(1), self.position(3)])
    shortScale = cgNormalize([0,1], $
       Position=[self.position(0), self.position(2)])

        ; Position the axes. 1000 indicates this position ignored.

    self.textaxis->SetProperty, YCoord_Conv=longScale, $
       Location=[self.position(0), 1000, 0]
    self.longaxis2->SetProperty, YCoord_Conv=longScale, $
       Location=[self.position(2), 1000, 0]
    self.shortAxis1->SetProperty, XCoord_Conv=shortScale, $
       Location=[1000, self.position(1), 0]
    self.shortAxis2->SetProperty, XCoord_Conv=shortScale, $
       Location=[1000, self.position(3), 0]

ENDIF
IF N_Elements(title) NE 0 THEN self.thisTitle->SetProperty, Strings=title
IF N_Elements(transform) NE 0 THEN self->IDLgrMODEL::SetProperty, Transform=transform
IF N_Elements(palette) NE 0 THEN BEGIN
    self.palette = palette
    self.thisImage->SetProperty, Palette=palette
ENDIF
IF N_Elements(major) NE 0 THEN BEGIN
    self.major = major
    self.textAxis->SetProperty, Major=major
    self.longAxis2->SetProperty, Major=major
END
IF N_Elements(minor) NE 0 THEN BEGIN
    self.minor = minor
    self.textAxis->SetProperty, Minor=minor
    self.longAxis2->SetProperty, Minor=minor
END
IF N_Elements(range) NE 0 THEN BEGIN
    self.range = range
    longScale = cgNormalize(range, $
       Position=[self.position(1), self.position(3)])
    self.textAxis->SetProperty, Range=range, YCoord_Conv=longScale
    self.longAxis2->SetProperty, Range=range, YCoord_Conv=longScale
ENDIF
IF N_Elements(color) NE 0 THEN BEGIN
    self.color = color
    self.textAxis->SetProperty, Color=color
    self.longAxis2->SetProperty, Color=color
    self.shortAxis1->SetProperty, Color=color
    self.shortAxis2->SetProperty, Color=color
    self.thisText->SetProperty, Color=color
ENDIF
IF N_Elements(name) NE 0 THEN self->IDLgrMODEL::SetProperty, Name=name
IF N_Elements(extra) NE 0 THEN self->IDLgrMODEL::SetProperty, _Extra=extra
END
;-------------------------------------------------------------------------



PRO ContrastZoom_VColorBar__Define

; For details on how this colorbar object works, see the VCOLORBAR__DEFINE.PRO
; program in the Coyote Library.
;
;   http://www.idlcoyote.com/programs/vcolorbar__define.pro

colorbar = { ContrastZoom_VColorBar, $
             INHERITS IDLgrMODEL, $      ; Inherits the Model Object.
             Position:FltArr(4), $       ; The position of the colorbar.
             Palette:Obj_New(), $        ; The colorbar palette.
             thisImage:Obj_New(), $      ; The colorbar image.
             imageModel:Obj_New(), $     ; The colorbar image model.
             thisContainer:Obj_New(), $  ; Container for cleaning up.
             thisFont:Obj_New(), $       ; The annotation font object.
             thisText:Obj_New(), $       ; The bar annotation text object.
             thisTitle: Obj_New(), $     ; The title of the colorbar.
             textAxis:Obj_New(), $       ; The axis containing annotation.
             shortAxis1:Obj_New(), $     ; A short axis.
             shortAxis2:Obj_New(), $     ; A second short axis.
             longAxis2:Obj_New(), $      ; The other long axis.
             NColors:0, $                ; The number of colors in the bar.
             Major:0, $                  ; Number of major axis intervals.
             Minor:0, $                  ; Number of minor axis intervals.
             Color:BytArr(3), $          ; Color of axes and annotation.
             Range:FltArr(2) }           ; The range of the colorbar axis.

END
;-------------------------------------------------------------------------



FUNCTION ContrastZoom_Aspect, aspectRatio, MARGIN=margin, WindowAspect=wAspectRatio

; This function calculates the correct aspect ratios for positioning
; objects in windows.

ON_ERROR, 2

   ; Check for aspect ratio parameter and possibilities.

IF N_PARAMS() EQ 0 THEN aspectRatio = 1.0

IF aspectRatio EQ 0 THEN BEGIN
   MESSAGE, 'Aspect Ratio of 0. Changing to 1...', /Informational
   aspectRatio = 1.0
ENDIF

s = SIZE(aspectRatio)
IF s(s(0)+1) NE 4 THEN $
   MESSAGE, 'Aspect Ratio is not a FLOAT. Take care...', /Informational

   ; Check for margins.

IF N_ELEMENTS(margin) EQ 0 THEN margin = 0.15

   ; Error checking.

IF margin LT 0 OR margin GE 0.5 THEN $
   MESSAGE, 'The MARGIN keyword value must be between 0.0 and 0.5.'

   ; Calculate the aspect ratio of the current window.

IF N_Elements(wAspectRatio) EQ 0 THEN wAspectRatio = FLOAT(!D.Y_VSIZE) / !D.X_VSIZE

   ; Calculate normalized positions in window.

IF (aspectRatio LE wAspectRatio) THEN BEGIN
   xstart = margin
   ystart = 0.5 - (0.5 - margin) * (aspectRatio / wAspectRatio)
   xend = 1.0 - margin
   yend = 0.5 + (0.5 - margin) * (aspectRatio / wAspectRatio)
ENDIF ELSE BEGIN
   xstart = 0.5 - (0.5 - margin) * (wAspectRatio / aspectRatio)
   ystart = margin
   xend = 0.5 + (0.5 - margin) * (wAspectRatio / aspectRatio)
   yend = 1.0 - margin
ENDELSE

position = [xstart, ystart, xend, yend]

RETURN, position
END
;-------------------------------------------------------------------------



PRO ContrastZoom_Resize, event

; This event handler responds to resize events.

Widget_Control, event.top, Get_UValue=info, /No_Copy

    ; Resize the draw widget. This is the proper way to do this
    ; in object graphics, but it does not always work in UNIX
    ; versions of IDL. If it doesn't work for you, comment the
    ; first line out and try the second. The second line is more
    ; portable, but not exactly the proper "object" way. :-(

info.theWindow->SetProperty, Dimensions=[event.x, event.y-20]
;Widget_Control, info.drawID, Draw_XSize=event.x, Draw_YSize=event.y-20


   ; Update the aspect ratios and re-position the images
   ; in the window.

sz = Size(*info.subimage, /Dimensions)
imageAspect = Float(sz[1]) / sz[0]
info.theWindow->GetProperty, Dimensions=dims
windowAspect = (450./info.window_ysize * dims[1]) / (300./info.window_xsize * dims[0])
pos = ContrastZoom_Aspect(imageAspect, WindowAspect=windowAspect, Margin=0)
info.zoomImage->GetProperty, XRange=xrange, YRange=yrange
xs = cgNormalize(xrange, Position=[pos(0), pos(2)])
ys = cgNormalize(yrange, Position=[pos(1), pos(3)])
info.zoomImage->SetProperty, XCoord_Conv=xs, YCoord_Conv=ys
info.theBox->SetProperty, XCoord_Conv=xs, YCoord_Conv=ys

sc = Size(*info.image, /Dimensions)
imageAspect = Float(sc[1]) / sc[0]
info.theWindow->GetProperty, Dimensions=dims
windowAspect = (450./info.window_ysize * dims[1]) / (300./info.window_xsize * dims[0])
pos = ContrastZoom_Aspect(imageAspect, WindowAspect=windowAspect, Margin=0)
info.contrastImage->GetProperty, XRange=xrange, YRange=yrange
xs = cgNormalize(xrange, Position=[pos(0), pos(2)])
ys = cgNormalize(yrange, Position=[pos(1), pos(3)])
info.contrastImage->SetProperty, XCoord_Conv=xs, YCoord_Conv=ys

   ; Draw the scene.

info.theWindow->Draw, info.theScene
Widget_Control, event.top, Set_UValue=info, /No_Copy
END ;-------------------------------------------------------------------------



PRO ContrastZoom_DistinguishEvents, event

; This event handler responds to all draw widget events.

   ; Error handling.

Catch, theError
IF theError NE 0 THEN BEGIN
   Catch, /Cancel
   ok = cgErrorMsg(/Traceback)
   Widget_Control, event.top, Set_UValue=info, /No_Copy
   RETURN
ENDIF

   ; Get the info structure.

Widget_Control, event.top, Get_UValue=info, /No_Copy

   ; Which view object are we dealing with. Find out by getting
   ; the UVALUE from the view obect?

thisView = info.theWindow->Select(info.theScene, [event.x, event.y], Dimensions=[1,1])
thisView = thisView[0]
IF Obj_Valid(thisView) EQ 0 THEN BEGIN
   possibleEventTypes = [ 'DOWN', 'UP', 'MOTION', 'SCROLL', 'EXPOSE' ]
   thisEvent = possibleEventTypes(event.type)
   IF thisEvent EQ 'EXPOSE' THEN info.theWindow->Draw, info.theScene
   Widget_Control, event.top, Set_UValue=info, /No_Copy
   RETURN
ENDIF
thisView->GetProperty, UValue=selectWindow

   ; What happens depends upon which view the event comes from.

CASE selectWindow OF

   'ZOOMWINDOW': BEGIN ; You are trying to zoom into a region in the left-hand image.

         ; Make sure ContrastWindow events don't show up here.

      IF info.currentMode EQ 'CONTRASTWINDOW' THEN BEGIN
         Widget_Control, event.top, Set_UValue=info, /No_Copy
         RETURN
      ENDIF

         ; Find the point in the coordinates of the image in the window.

      hit = info.theWindow->Pickdata(thisView, info.zoomImage, [event.x, event.y], xyz)
      xpt = Floor(xyz[0])
      ypt = Floor(xyz[1])

      possibleEventTypes = [ 'DOWN', 'UP', 'MOTION', 'SCROLL', 'EXPOSE' ]
      thisEvent = possibleEventTypes(event.type)

      CASE thisEvent OF

         'DOWN': BEGIN

               ; While the coordinates are in the image coordinate system, it is
               ; possible that they are *outside* the actual coordinates of the
               ; image. Make sure they don't exceed the size of the image.
               ; What you do with the point depends on what kind of event it is.

            IF xpt LT 0 OR xpt GT (info.zxsize-1) THEN BEGIN
               Widget_Control, event.top, Set_UValue=info, /No_Copy
               RETURN
            ENDIF
            IF ypt LT 0 OR ypt GT (info.zysize-1) THEN BEGIN
               Widget_Control, event.top, Set_UValue=info, /No_Copy
               RETURN
            ENDIF

               ; Set the static corners of the box to current
               ; cursor location. See the current mode so dragging
               ; outside the image can't cause problems.

            info.xs = xpt
            info.ys = ypt
            info.currentMode = "ZOOMWINDOW"

               ; Change the event handler for the draw widget and turn MOTION
               ; events ON.

            Widget_Control, event.id, Draw_Motion_Events=1

               ; Initialize and hide the polyline object.

            box = FltArr(2,5)
            box[0,*] = Replicate(info.xs, 5)
            box[1,*] = Replicate(info.ys, 5)
            info.theBox->SetProperty, Data=box, Hide=0

            END ; of DOWN event

        'UP': BEGIN

              ; It is possible to get an UP event without a previous DOWN event. (For
              ; example, the user starts the box outside the draw widget window.) If this
              ; occurs, info.xs and info.ys will be negative. Check for this and return,
              ; if necessary.

           IF info.xs EQ -1 OR info.ys EQ -1 THEN BEGIN
              Widget_Control, event.top, Set_UValue=info, /No_Copy
              RETURN
           ENDIF

              ; If this is an UP event, you need to erase the zoombox, turn motion events
              ; OFF, and draw the "zoomed" plot.

              ; Turn motion events off. Set the current mode to NULL.

           Widget_Control, event.id, Draw_Motion_Events=0
           Widget_Control, event.id, /Clear_Events
           info.currentMode = ""

              ; Draw the "zoomed" image. Start by getting the LAST zoom
              ; box outline. These are indices into image array.

              xpt = 0 > xpt < (info.zxsize)
              ypt = 0 > ypt < (info.zysize)

              x = [info.xs, xpt]
              y = [info.ys, ypt]

                 ; If the static point and the dynamic point are the same,
                 ; zoom all the way out.

           IF Abs(info.xs - xpt) LT 2 AND Abs(info.ys-ypt) LT 2 THEN BEGIN
              s = Size(*info.image, /Dimensions)
              info.zxsize = s[0]
              info.zysize = s[1]
              contrast = info.contrast
              brightness = info.brightness
              level = (1-brightness/100.)*(info.maxVal - info.minVal) + info.minVal
              width = (1-contrast/100.)*(info.maxVal - info.minVal)

                 ; Calculate display minimum and maximum.

              displayMax = (level + (width / 2))
              displayMin = (level - (width / 2))

              IF displayMax GT info.maxval THEN BEGIN
                 difference = Abs(displayMax - info.maxval)
                 displayMax = displayMax - difference
                 displayMin = displayMin - difference
              ENDIF
              IF displayMin LT info.minval THEN BEGIN
                 difference = Abs(info.minval - displayMin)
                 displayMin = displayMin + difference
                 displayMax = displayMax + difference
              ENDIF

                     ; Display the image.

              info.zoomImage->SetProperty, Data=BytScl(*info.image, Min=info.minval > $
                  displayMin, Max=displayMax < info.maxval), Dimensions=s
              *info.subimage = *info.image
              imageAspect = Float(s[1]) / s[0]
              info.theWindow->GetProperty, Dimensions=dims
              windowAspect = (450./info.window_ysize * dims[1]) / (300./info.window_xsize * dims[0])
              pos = ContrastZoom_Aspect(imageAspect, WindowAspect=windowAspect, Margin=0)
              info.zoomImage->GetProperty, XRange=xrange, YRange=yrange
              xs = cgNormalize(xrange, Position=[pos(0), pos(2)])
              ys = cgNormalize(yrange, Position=[pos(1), pos(3)])

              info.zoomImage->SetProperty, XCoord_Conv=xs, YCoord_Conv=ys
              info.theBox->SetProperty, Hide=1, XCoord_Conv=xs, YCoord_Conv=ys
              info.theWindow->Draw, info.theScene
              info.xs = 0
              info.ys = 0
              info.xd = s[0]
              info.yd = s[1]
              Widget_Control, event.top, Set_UValue=info, /No_Copy
              RETURN
           ENDIF

              ; Make sure the x and y values are ordered as [min, max].

           IF info.xs GT xpt THEN x = [xpt, info.xs]
           IF info.ys GT ypt THEN y = [ypt, info.ys]

              ; Subset the image.


           *info.subimage = (*info.subimage)(x[0]:x[1] < (info.zxsize-1), y[0]:y[1] < (info.zysize-1))
           zoomedImage = *info.subimage

              ; Update the zoomed image data and draw it.

           s = Size(zoomedImage, /Dimensions)
           info.zxsize = s[0]
           info.zysize = s[1]
           contrast = info.contrast
           brightness = info.brightness
           level = (1-brightness/100.)*(info.maxVal - info.minVal) + info.minVal
           width = (1-contrast/100.)*(info.maxVal - info.minVal)

              ; Calculate display minimum and maximum.

           displayMax = (level + (width / 2))
           displayMin = (level - (width / 2))

           IF displayMax GT info.maxval THEN BEGIN
              difference = Abs(displayMax - info.maxval)
              displayMax = displayMax - difference
              displayMin = displayMin - difference
           ENDIF
           IF displayMin LT info.minval THEN BEGIN
              difference = Abs(info.minval - displayMin)
              displayMin = displayMin + difference
              displayMax = displayMax + difference
           ENDIF

              ; Display the image after positioning it appropriately
              ; in the window so that it maintains the aspect ratio of the
              ; sub-sampled image. Apply the positioning to the image and
              ; to the zoom box.

           info.zoomImage->SetProperty, Data=BytScl(*info.subimage, Min=info.minval > $
               displayMin, Max=displayMax < info.maxval), Dimensions=s
           imageAspect = Float(s[1]) / s[0]
           info.theWindow->GetProperty, Dimensions=dims
           windowAspect = (450./info.window_ysize * dims[1]) / (300./info.window_xsize * dims[0])
           pos = ContrastZoom_Aspect(imageAspect, WindowAspect=windowAspect, Margin=0)
           info.zoomImage->GetProperty, XRange=xrange, YRange=yrange
           xs = cgNormalize(xrange, Position=[pos(0), pos(2)])
           ys = cgNormalize(yrange, Position=[pos(1), pos(3)])
           info.zoomImage->SetProperty, XCoord_Conv=xs, YCoord_Conv=ys
           info.theBox->SetProperty, Hide=1, XCoord_Conv=xs, YCoord_Conv=ys

              ; Clear any motion events that may have occurred.

           Widget_Control, event.id, /Clear_Events

              ; Set the static values to negative values.

           info.xs = -1
           info.ys = -1

           END ; of UP event

         'MOTION': BEGIN

               ; Get the dynamic corner of the box.

             info.xd = 0 > xpt < (info.zxsize)
             info.yd = 0 > ypt < (info.zysize)

               ; Re-configure the box coordinates.

            box = FltArr(2,5)
            box[0,*] = [info.xs, info.xd, info.xd, info.xs, info.xs]
            box[1,*] = [info.ys, info.ys, info.yd, info.yd, info.ys]

               ; Draw the new zoom box.

           info.theBox->SetProperty, Data=box

            END ; of MOTION event.

         ELSE:

     ENDCASE

  END ; of ZOOMWINDOW processing.

  'CONTRASTWINDOW': BEGIN ; You are trying to window and level the image.

        ; Make sure no zooming event sneak in here by accident.

     IF info.currentMode EQ 'ZOOMWINDOW' THEN BEGIN
        Widget_Control, event.top, Set_UValue=info, /No_Copy
        RETURN
     ENDIF


        ; Find the point in the coordinates of the image in the window.

     hit = info.theWindow->Pickdata(thisView, info.zoomImage, [event.x, event.y], xyz)
     xpt = Floor(xyz[0])
     ypt = Floor(xyz[1])

        ; What happens next depends on the type of event this is.

     possibleEventTypes = [ 'DOWN', 'UP', 'MOTION', 'SCROLL', 'EXPOSE' ]
     thisEvent = possibleEventTypes(event.type)

     CASE thisEvent OF

     'DOWN': BEGIN

           ; Set the initial (x,y) point. Turn motion events on.

        info.x1 = xpt
        info.y1 = ypt
        Widget_Control, info.drawID, Draw_Motion_Events=1
        info.currentMode = "CONTRASTWINDOW"
        END

      'UP': BEGIN

            ; Turn motion events off. Clear any motion events that might have queued.
            ; Reset the current mode.

         Widget_Control, info.drawID, Draw_Motion_Events=0
         Widget_Control, info.drawID, /Clear_Events
         info.currentMode = ""

            ; If the static point and the dynamic point are the same,
            ; reset the level and width.

         IF Abs(info.x1 - xpt) LT 2 AND Abs(info.y1-ypt) LT 2 THEN BEGIN
            contrast = 0
            brightness = 100
            level = (1-brightness/100.)*(info.maxVal - info.minVal) + info.minVal
            width = (1-contrast/100.)*(info.maxVal - info.minVal)

             ; Calculate display minimum and maximum.

            displayMax = (level + (width / 2))
            displayMin = (level - (width / 2))

            IF displayMax GT info.maxval THEN BEGIN
               difference = Abs(displayMax - info.maxval)
               displayMax = displayMax - difference
               displayMin = displayMin - difference
            ENDIF
            IF displayMin LT info.minval THEN BEGIN
               difference = Abs(info.minval - displayMin)
               displayMin = displayMin + difference
              displayMax = displayMax + difference
             ENDIF

             ; Display the image.

            info.contrastImage->SetProperty, Data=BytScl(*info.image, Min=info.minval > $
               displayMin, Max=displayMax < info.maxval)
            info.zoomImage->SetProperty, Data=BytScl(*info.subimage, Min=info.minval > $
               displayMin, Max=displayMax < info.maxval)
            info.colorbar->Clamp, [info.minval > displayMin, displayMax < info.maxval]
            info.colorbar2->SetProperty, Range=[info.minval > displayMin, displayMax < info.maxval]

               ; Update the current contrast and brightness values.

            info.contrast = contrast
            info.brightness = brightness
            info.theWindow->Draw, info.theScene
            Widget_Control, event.top, Set_UValue=info, /No_Copy
            RETURN

           ENDIF

               ; Calculate new contrast, brightness, level, and width parameters.

           contrast = 0 > ((info.y1 - ypt) * info.cstep + info.contrast) < 99
           brightness = 0 > ((info.x1 - xpt) * info.bstep + info.brightness) < 100
           level = (1-brightness/100.)*(info.maxVal - info.minVal) + info.minVal
           width = (1-contrast/100.)*(info.maxVal - info.minVal)

               ; Calculate display minimum and maximum.

           displayMax = (level + (width / 2))
           displayMin = (level - (width / 2))

           IF displayMax GT info.maxval THEN BEGIN
              difference = Abs(displayMax - info.maxval)
              displayMax = displayMax - difference
              displayMin = displayMin - difference
           ENDIF
           IF displayMin LT info.minval THEN BEGIN
              difference = Abs(info.minval - displayMin)
              displayMin = displayMin + difference
              displayMax = displayMax + difference
           ENDIF

              ; Display the image.

           info.contrastImage->SetProperty, Data=BytScl(*info.image, Min=info.minval > $
               displayMin, Max=displayMax < info.maxval)
           info.zoomImage->SetProperty, Data=BytScl(*info.subimage, Min=info.minval > $
               displayMin, Max=displayMax < info.maxval)
           info.colorbar->Clamp, [info.minval > displayMin, displayMax < info.maxval]
           info.colorbar2->SetProperty, Range=[info.minval > displayMin, displayMax < info.maxval]

              ; Update the current contrast and brightness values.

           info.contrast = contrast
           info.brightness = brightness
           END

         'MOTION': BEGIN

               ; Calculate new contrast, brightness, level, and width parameters.
               ; Restrict the width to 5 percent of the image range.

            contrast = 0 > ((info.y1 - ypt) * info.cstep + info.contrast) < 99
            brightness = 0 > ((info.x1 - xpt) * info.bstep + info.brightness) < 100
            level = (1-brightness/100.)*(info.maxVal - info.minVal) + info.minVal
            width = (1-contrast/100.)*(info.maxVal - info.minVal)

               ; Calculate display minimum and maximum.

            displayMax = (level + (width / 2))
            displayMin = (level - (width / 2))

            IF displayMax GT info.maxval THEN BEGIN
               difference = Abs(displayMax - info.maxval)
               displayMax = displayMax - difference
               displayMin = displayMin - difference
            ENDIF
            IF displayMin LT info.minval THEN BEGIN
               difference = Abs(info.minval - displayMin)
               displayMin = displayMin + difference
               displayMax = displayMax + difference
            ENDIF

               ; Display the image.

            info.contrastImage->SetProperty, Data=BytScl(*info.image, Min=info.minval > $
               displayMin, Max=displayMax < info.maxval)
            info.zoomImage->SetProperty, Data=BytScl(*info.subimage, Min=info.minval > $
               displayMin, Max=displayMax < info.maxval)
            info.colorbar->Clamp, [info.minval > displayMin, displayMax < info.maxval]
            info.colorbar2->SetProperty, Range=[info.minval > displayMin, displayMax < info.maxval]

            END

         ELSE:

         ENDCASE

      END ; of ContrastWindow processing.

   ELSE:


ENDCASE

   ; Draw the scene.

info.theWindow->Draw, info.theScene

   ; Store the info structure.

Widget_Control, event.top, Set_UValue=info, /No_Copy
END;-------------------------------------------------------------------------------------



PRO ContrastZoom_Cleanup, tlb

   ; This is the clean-up procedure for the program.

Widget_Control, tlb, Get_UValue=info, /No_Copy
IF N_Elements(info) EQ 0 THEN RETURN

Obj_Destroy, info.theContainer
Ptr_Free, info.image
Ptr_Free, info.subimage

END;-------------------------------------------------------------------------------------


PRO ContrastZoom, image, Colortable=colortable, Group_Leader=group_leader

On_Error, 2

   ; Find an image, if needed.

IF N_Elements(image) EQ 0 THEN BEGIN
   filename = Filepath(Subdir=['examples','data'], 'rbcells.jpg')
   Read_JPEG, filename, image
   image = Reverse(image,2) ; Image is upside down.
ENDIF

   ; Only 2D images can be used.

IF Size(image, /N_Dimensions) NE 2 THEN Message, 'Image must be 2D. Returning...'

   ; Get a colortable if needed.

IF N_Elements(colortable) EQ 0 THEN colortable = 0 ELSE colortable = 0 > colortable < 41

   ; Create a color palette and load the colortable.

thePalette = Obj_New('IDLgrPalette')
thePalette->LoadCT, colortable

   ; Create a zoom box for zoom rubberbanding.

theBox = Obj_New('IDLgrPolyline', Hide=1, Color=[255,255,255])

   ; Create contrast and zoom image objects.

dims = Size(image, /Dimensions)
zxsize = dims[0]
zysize = dims[1]
zoomImage = Obj_New('IDLgrImage', image, Palette=thePalette, Dimensions=dims)
contrastImage = Obj_New('IDLgrImage', BytScl(image), Palette=thePalette, Dimensions=dims)

   ; Create a color bar.

colorbar = Obj_New('ContrastZoom_VColorBar', Palette=thePalette, Range=[Min(image), Max(image)], $
   Position=[0.7, 0.1, 0.95, 0.95], Title='Image Values')
colorbar2 = Obj_New('ContrastZoom_VColorBar', Palette=thePalette, Range=[Min(image), Max(image)], $
   Position=[0.7, 0.1, 0.95, 0.95], Title='Displayed Values', Major=8)

   ; Create the scene, views, and models for the object heirarchy. The dimensions
   ; and location are in normalized units, but device units are used to calculate
   ; the values. The numbers window_xsize and window_ysize refer to the X size and Y
   ; size of the initial draw widget that will be created later. User values will be
   ; used to identify which events are associated with which view.

window_xsize = 925
window_ysize = 500

theScene = Obj_New('IDLgrScene', Color=[125, 125, 125])
zoomView = Obj_New('IDLgrView', Color=[125, 125, 125], Viewplane_Rect=[0,0,1,1], $
   Location=[25./window_xsize, 25./window_ysize], Dimensions=[300./window_xsize, 450./window_ysize], Units=3, UValue='ZOOMWINDOW')
contrastView = Obj_New('IDLgrView', Color=[125, 125, 125], Viewplane_Rect=[0,0,1,1], $
   Location=[350./window_xsize, 25./window_ysize], Dimensions=[300./window_xsize, 450./window_ysize], Units=3, UValue='CONTRASTWINDOW')
colorbarView = Obj_New('IDLgrView', Color=[125, 125, 125], Viewplane_Rect=[-0.2,0,1.2,1], $
   Location=[675./window_xsize, 25./window_ysize], Dimensions=[75./window_xsize, 450./window_ysize], Units=3, UValue='COLORBARWINDOW')
colorbar2View = Obj_New('IDLgrView', Color=[125, 125, 125], Viewplane_Rect=[-0.2,0,1.2,1], $
   Location=[775./window_xsize, 25./window_ysize], Dimensions=[75./window_xsize, 450./window_ysize], Units=3, UValue='COLORBAR2WINDOW')

theScene->Add, zoomView
theScene->Add, contrastView
theScene->Add, colorbarView
theScene->Add, colorbar2View

zoomModel = Obj_New('IDLgrModel')
contrastModel = Obj_New('IDLgrModel')
colorbarModel = Obj_New('IDLgrModel')
colorbar2Model = Obj_New('IDLgrModel')

zoomView->Add, zoomModel
contrastView->Add, contrastModel
colorbarView->Add, colorbarModel
colorbar2View->Add, colorbar2Model

zoomModel->Add, zoomImage
zoomModel->Add, theBox
contrastModel->Add, contrastImage
colorbarModel->Add, colorbar
colorbar2Model->Add, colorbar2

   ; We need to scale the image into the view. Start by
   ; getting the image range. This is the same for both
   ; the zoom and contrast image.

zoomImage->GetProperty, XRange=xrange, YRange=yrange

    ; Calculate the aspect ratios (height/width) for the image
    ; and for the display window. Scale the images and the zoom box.

s = Size(image, /Dimensions)
imageAspect = Float(s[1]) / s[0]
windowAspect = Float(450) / 300
pos = ContrastZoom_Aspect(imageAspect, WindowAspect=windowAspect, Margin=0)
xs = cgNormalize(xrange, Position=[pos(0), pos(2)])
ys = cgNormalize(yrange, Position=[pos(1), pos(3)])
zoomImage->SetProperty, XCoord_Conv=xs, YCoord_Conv=ys
contrastImage->SetProperty, XCoord_Conv=xs, YCoord_Conv=ys
theBox->SetProperty, XCoord_Conv=xs, YCoord_Conv=ys

   ; Set up initial parameters. Contrast and brightness values
   ; go from 0 to 100. Start with 25% contrast and 75% brightness.

maxval = Max(image, Min=minVal)
contrast = 25
brightness = 75

   ; Calculate window level and width from contrast/brightness values.

level = (1-brightness/100.)*(maxVal - minVal) + minVal
width = (1-contrast/100.)*(maxVal - minVal)

   ; Calculate display minimum and maximum.

displayMax = (level + (width / 2))
displayMin = (level - (width / 2))

IF displayMax GT maxval THEN BEGIN
   difference = Abs(displayMax - maxval)
   displayMax = displayMax - difference
   displayMin = displayMin - difference
ENDIF
IF displayMin LT minval THEN BEGIN
   difference = Abs(minval - displayMin)
   displayMin = displayMin + difference
   displayMax = displayMax + difference
ENDIF

   ; Update the contrast and zoom image after changes.

contrastImage->SetProperty, Data=BytScl(image, Min=displayMin, Max=displayMax)
zoomImage->SetProperty, Data=BytScl(image, Min=displayMin, Max=displayMax)
colorbar2->SetProperty, Range=[displayMin, displayMax]

   ; Create the widgets for the program. Set software rendering for the draw
   ; widget, or rubberbanding may be slow.

tlb = Widget_Base(Title='Contrast/Zoom Object Graphics Example', /Base_Align_Center, $
   Column=1, /TLB_Size_Events)
drawID = Widget_Draw(tlb, XSize=window_xsize, YSize=window_ysize, Button_Events=1, $
   Graphics_Level=2, Event_Pro='ContrastZoom_DistinguishEvents', Expose_Events=1, $
   Renderer=1)
label = Widget_Label(tlb, Value='Left image allows ZOOMING. Center image allows WINDOWING. ' + $
   'Click and drag. Click and release in window restores original view.')
Widget_Control, tlb, /Realize

   ; Get the window object and draw the scene.

Widget_Control, drawID, Get_Value=theWindow
theWindow->Draw, theScene

   ; Create a container object to aid in proper object clean-up.

theContainer = Obj_New('IDL_Container')
theContainer->Add, thePalette
theContainer->Add, zoomView
theContainer->Add, contrastView
theContainer->Add, colorbarView
theContainer->Add, colorbar2View
theContainer->Add, theScene

   ; Create the info structure.

info = { zoomImage:zoomImage, $            ; The zoom image object.
         contrastImage: contrastImage, $   ; The contrast image object.
         image:Ptr_New(image), $           ; A pointer to the original image.
         subimage: Ptr_New(image), $       ; A pointer to the current image subset.
         zoomView:zoomView, $              ; The zoom view object.
         contrastView:contrastView, $      ; The contrast view object.
         colorbar:colorbar, $              ; The color bar object.
         colorbar2:colorbar2, $            ; The second color bar.
         colorbarView:colorbarView, $      ; The color bar view.
         theContainer:theContainer, $      ; The container object.
         theWindow:theWindow, $            ; The window object.
         theScene:theScene, $              ; The scene to be displayed.
         drawID:drawID, $                  ; The draw widget identifier.
         thebox: thebox, $                 ; The zoom box object.
         currentMode: "", $                ; The current mode the window is operating in.
         x1:-1L, $                         ; Locations in the window for contrast/brightness operations.
         x2:-1L, $
         y1:-1L, $
         y2:-1L, $
         zxsize:zxsize, $                  ; The X size of the zoom image.
         zysize:zysize, $                  ; The Y size of the zoom image.
         contrast:contrast, $              ; The current contrast value.
         brightness:brightness, $          ; The current brightness value.
         bstep:zxsize/512., $              ; The amount of brighness change for one pixel movement.
         cstep:zysize/512., $              ; The amount of contrast change for one pixel movement.
         maxVal:maxVal, $
         minVal:minVal, $
         window_xsize:window_xsize, $      ; The original X size of the draw widget.
         window_ysize:window_ysize, $      ; The original Y size of the draw widget.
         xs:-1L, $                         ; Locations in the window for zooming operations.
         ys:-1L, $
         xd:zxsize-1, $
         yd:zysize-1 }

Widget_Control, tlb, Set_UValue=info, /No_Copy
XManager, 'contrastzoom', tlb, /No_Block, Cleanup='ContrastZoom_Cleanup', $
   Event_Handler='Contrastzoom_Resize', Group_Leader=group_leader

END