Issue in AutoLocalThreshold 'Contrast'

Hi @gabriel
during porting the local threshold methods to clij I stumbled over an issue in the Contrast version.

In the implementation the following constructions are used:

Math.abs((int)(a&0xff - b&0xff))

This leads to errors.

Here is an example code to show this:

byte a = (byte) 100;
byte b = (byte) 128;

int r1 = Math.abs((int)(a&0xff - b&0xff));
int r2 = Math.abs((int)(a&0xff) - (int)(b&0xff));

System.out.println("a : " + (int)(a&0xff) );
System.out.println("b : " + (int)(b&0xff) );
System.out.println("r1 : " + r1 );
System.out.println("r2 : " + r2 );

a : 100
b : 128
r1 : 100
r2 : 28

I think

Math.abs((int)(a&0xff - b&0xff))

has to be replaced by something like

Math.abs((int)(a&0xff) - (int)(b&0xff))

Can you please check this?

This issue also concerns the fiji code in Auto_Local_Threshold/ at c955dc18cff58ac61df82f3f001799f7ffaec5cb · fiji/Auto_Local_Threshold · GitHub

I’m not sure who maintains this code, @ctrueden ?

( cc @haesleinhuepf )


Hi Peter, Thanks for spotting this.
I will have a look, there might be some other places where this might happen as well.


Hi Peter, I confirm the bug you found. I also spotted some other occurrences where there could be an issue with a byte overflow (specifically the Bernsen and Median methods). However after fixing those I cannot see any differences in the output using a the AuPbSn40 and the blobs testing images. This seems to have affected only the Contrast method as far as I have been able to test.
Thank you again for spotting this. I will update the plugin shortly.


Hi Gabriel,
the code in Bernsen and Median was like

(int)( (a&0xff) - (b&0xff) )

and therefore no error had occurred.

1 Like

Hi Peter,
Yes, that is right, although the mid_gray result in the Bernsen method could potentially overflow if the min and maximum in the local set added to more than 255?
I fixed that one too.

The new jar file for ImageJ can be downloaded from:

I will try to update the Fiji version (if I can manage to do it in github :man_facepalming:). In the mean time, the above can be used in Fiji if the two Auto_Threshold and Auto_Local_Thresholjar files are removed from the /plugins folder (or uninstalled via the updater).
Again, thanks for spotting these issues.


Right, I managed to fork the plugin, make the changes, committed the changes and generated a pull request. Sorry I do not know what else I need to do now.
I am confronted with a Travis page that among other things says “Waiting to start” and asking me to sign in… and among other things is showing a clock icon and “Waiting to start”
Somebody here must know what is the next step?

1 Like

Maybe @imagejan can help us here? :upside_down_face:

Update: the yellow page went green and the pull request says “passed”.
It sounds like being successful without knowing what I am doing…

1 Like

Hi Gabriel, just to be clear, there is no potential overflow in Bernsen and Median.
The problem occured only in Contrast mode.

This is because in Bernsen and Median the expression a&0xff where bracketed. This forces a type conversion.

Here is an example:

byte a = (byte) 150;
byte b = (byte) 245;

int r1 = (int)(a&0xff - b&0xff);;
int r2 = (int)(a&0xff + b&0xff);
int r3 = ((int)(a&0xff) - (int)(b&0xff));
int r4 = ((int)(a&0xff) + (int)(b&0xff));
int r5 = ((a&0xff) - (b&0xff));
int r6 = ((a&0xff) + (b&0xff));

System.out.println("a : " + (int)(a&0xff) );
System.out.println("b : " + (int)(b&0xff) );
System.out.println("r1 : " + r1 );
System.out.println("r2 : " + r2 );
System.out.println("r3 : " + r3 );
System.out.println("r4 : " + r4 );
System.out.println("r5 : " + r5 );
System.out.println("r6 : " + r6 );

The output of this is:

a : 150
b : 245
r1 : 2
r2 : 148
r3 : -95
r4 : 395
r5 : -95
r6 : 395

Only as an all-clear: Bernsen and Median were not affected by the problem.

1 Like

Hi Peter,
Thank you for the clarification.
Maybe you meant b&0xff in r1 and r2?

1 Like

Thank you - edit my post