Please help to complete beginner in programming.

I work with binary stack of 2 pictures and here is the plugin I use:

import ij.*;
import ij.plugin.filter.PlugInFilter;
import ij.process.*;
import java.awt.*;

    protected ImageStack stack;

    public int setup(String arg, ImagePlus imp) {
        stack =imp.getStack();
        return DOES_8G+STACK_REQUIRED;

    public void run(ImageProcessor ip) {
        BinaryProcessor bp
            = new BinaryProcessor((ByteProcessor)ip);
        byte[] pixels;
        int dimension = stack.getWidth()*stack.getHeight();
        int[] sum = new int[dimension];
        for (int i=1;i<=stack.getSize();i++) {
            pixels = (byte[]) stack.getPixels(i);
            for (int j=0;j<dimension;j++) {
                sum[j] += pixels[j];
        byte[] average = new byte[dimension];
        for (int j=0;j<dimension;j++) {
            average[j] = (byte) ((sum[j]/stack.getSize()) & 0xff);

As a result I see black pixels what are common for 2 pictures only. The rest of the picture is white.
But when & 0xFF is added after sum[j] += pixels[j], the plugin does averaging and I see gray also.

Please, answer in simpliest way as I scarcely understand how -128-+127 is converted in 0-255.

What values mean white, gray and black in java?

Thanks in advance...

5 Years
Discussion Span
Last Post by JamesCherrill

Your code uses classes that are not in the standard Java API, so we have no way of knowing what the method calls do, or what the types of any return values are. That makes it near impossible to comment on the code.

See http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html for a list of the common formats for storing image pixels in Java. You will see its quite a long list, and we do not know which of those (if any) your classes use.

for &0xff see

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.