55import ij .*;
66import ij .gui .*;
77import ij .measure .*;
8- import ij .plugin .RGBStackConverter ;
8+ import ij .plugin .* ;
99
1010/** This class does stack type conversions. */
1111public class StackConverter {
@@ -57,6 +57,7 @@ public void convertToGray8() {
5757 int inc = nSlices /20 ;
5858 if (inc <1 ) inc = 1 ;
5959 LUT [] luts = composite ?((CompositeImage )imp ).getLuts ():null ;
60+ boolean scale = ImageConverter .getDoScaling ();
6061 for (int i =1 ; i <=nSlices ; i ++) {
6162 label = stack1 .getSliceLabel (1 );
6263 ip = stack1 .getProcessor (1 );
@@ -67,7 +68,6 @@ public void convertToGray8() {
6768 max = luts [index ].max ;
6869 }
6970 ip .setMinAndMax (min , max );
70- boolean scale = ImageConverter .getDoScaling ();
7171 stack2 .addSlice (label , ip .convertToByte (scale ));
7272 if ((i %inc )==0 ) {
7373 IJ .showProgress ((double )i /nSlices );
@@ -76,9 +76,14 @@ public void convertToGray8() {
7676 }
7777 imp .setStack (null , stack2 );
7878 imp .setCalibration (imp .getCalibration ()); //update calibration
79- if (imp .isComposite ()) {
80- ((CompositeImage )imp ).resetDisplayRanges ();
81- ((CompositeImage )imp ).updateAllChannelsAndDraw ();
79+ if (composite && luts !=null ) {
80+ if (scale ) {
81+ for (int i =0 ; i <luts .length ; i ++) {
82+ luts [i ].min = 0 ;
83+ luts [i ].max = 255 ;
84+ }
85+ }
86+ ((CompositeImage )imp ).setLuts (luts );
8287 }
8388 ImageConverter .record ();
8489 imp .setSlice (currentSlice );
@@ -99,7 +104,7 @@ void convertRGBToGray8() {
99104 String label ;
100105 int inc = nSlices /20 ;
101106 if (inc <1 ) inc = 1 ;
102- for (int i =1 ; i <=nSlices ; i ++) {
107+ for (int i =1 ; i <=nSlices ; i ++) {
103108 label = stack1 .getSliceLabel (1 );
104109 ip = stack1 .getProcessor (1 );
105110 stack1 .deleteSlice (1 );
@@ -116,7 +121,7 @@ void convertRGBToGray8() {
116121 IJ .showProgress (1.0 );
117122 }
118123
119- /** Converts this Stack to 16-bit grayscale. */
124+ /** Converts this Stack to 16-bit grayscale. */
120125 public void convertToGray16 () {
121126 if (type ==ImagePlus .GRAY16 )
122127 return ;
@@ -126,6 +131,9 @@ public void convertToGray16() {
126131 ImageConverter .convertAndCalibrate (imp ,"16-bit" );
127132 return ;
128133 }
134+ ImageProcessor ip = imp .getProcessor ();
135+ double min = ip .getMin ();
136+ double max = ip .getMax ();
129137 ImageStack stack1 = imp .getStack ();
130138 ImageStack stack2 = new ImageStack (width , height );
131139 int channels = imp .getNChannels ();
@@ -137,13 +145,15 @@ public void convertToGray16() {
137145 if (inc <1 ) inc = 1 ;
138146 boolean scale = type ==ImagePlus .GRAY32 && ImageConverter .getDoScaling ();
139147 ImageProcessor ip1 , ip2 ;
140- for (int i =1 ; i <=nSlices ; i ++) {
148+ for (int i =1 ; i <=nSlices ; i ++) {
141149 label = stack1 .getSliceLabel (1 );
142150 ip1 = stack1 .getProcessor (1 );
143151 if (luts !=null ) {
144- int index = ((i -1 )%channels );
145- ip1 .setMinAndMax (luts [index ].min ,luts [index ].max );
152+ int index = (i -1 )%luts .length ;
153+ min = luts [index ].min ;
154+ max = luts [index ].max ;
146155 }
156+ ip1 .setMinAndMax (min , max );
147157 ip2 = ip1 .convertToShort (scale );
148158 stack1 .deleteSlice (1 );
149159 stack2 .addSlice (label , ip2 );
@@ -154,13 +164,16 @@ public void convertToGray16() {
154164 }
155165 IJ .showProgress (1.0 );
156166 imp .setStack (null , stack2 );
157- if (scale ) {
158- for (int c =channels ; c >=1 ; c --) {
159- imp .setPosition (c ,imp .getSlice (),imp .getFrame ());
160- imp .setDisplayRange (0 ,65535 );
167+ if (imp .isComposite () && luts !=null ) {
168+ if (scale ) {
169+ for (int i =0 ; i <luts .length ; i ++) {
170+ luts [i ].min = 0 ;
171+ luts [i ].max = 65535 ;
172+ }
161173 }
174+ ((CompositeImage )imp ).setLuts (luts );
175+ imp .updateAndDraw ();
162176 }
163- ImageConverter .record ();
164177 }
165178
166179 /** Converts this Stack to 32-bit (float) grayscale. */
@@ -176,6 +189,12 @@ public void convertToGray32() {
176189 if (inc <1 ) inc = 1 ;
177190 ImageProcessor ip1 , ip2 ;
178191 Calibration cal = imp .getCalibration ();
192+ double min = imp .getDisplayRangeMin ();
193+ double max = imp .getDisplayRangeMax ();
194+ int channels = imp .getNChannels ();
195+ LUT [] luts = imp .getLuts ();
196+ if ((luts !=null && luts .length !=channels ) || cal .calibrated ())
197+ luts = null ;
179198 for (int i =1 ; i <=nSlices ; i ++) {
180199 label = stack1 .getSliceLabel (1 );
181200 ip1 = stack1 .getProcessor (1 );
@@ -195,6 +214,11 @@ public void convertToGray32() {
195214 imp .resetDisplayRange ();
196215 imp .updateAndDraw ();
197216 }
217+ if (imp .isHyperStack () && luts !=null )
218+ ((CompositeImage )imp ).setLuts (luts );
219+ else if (!cal .calibrated ())
220+ imp .setDisplayRange (min , max );
221+
198222 }
199223
200224 /** Converts the Stack to RGB. */
0 commit comments