Skip to main content

[JAI-IMAGEIO] White Image

1 reply [Last post]
Anonymous

This is my code to convert a 16-bit image to 8-bit, and I am getting a
white image back, can someone point out the error?

// get image width and height
int width = pImg.getWidth();
int height = pImg.getHeight();
// which are the max and min of the image?
ParameterBlock pbMaxMin = new ParameterBlock();
pbMaxMin.addSource(pImg);
RenderedOp extrema = JAI.create("extrema", pbMaxMin);
// must get the extrema of all bands
double[] allMins = (double[])extrema.getProperty("minimum");
double[] allMaxs = (double[])extrema.getProperty("maximum");
// assume that the image is single band
double min = allMins[0];
double max = allMaxs[0];
// rescale the image with the parameters
double[] multiplyByThis = new double[1];
multiplyByThis[0] = 255./(max - min);
double[] addThis = new double[1];
addThis[0] = min;
// now we can rescale the pixels gray levels
ParameterBlock pbRescale = new ParameterBlock();
pbRescale.add(multiplyByThis);
pbRescale.add(addThis);
pbRescale.addSource(pImg);
PlanarImage surrogateImage = (PlanarImage)JAI.create("Rescale",
pbRescale);
// let's convert the data type for displaying
ParameterBlock pbConvert = new ParameterBlock();
pbConvert.addSource(surrogateImage);
pbConvert.add(DataBuffer.TYPE_BYTE);
surrogateImage = JAI.create("format", pbConvert);

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Brian Burkhalter

I don't know about your code but I know that this code works for sure:

double[] min = (double[])extrema.getProperty("minimum");
double[] max = (double[])extrema.getProperty("maximum");

int numBands = img.getSampleModel().getNumBands();
double[] gain = new double[numBands];
double[] bias = new double[numBands];
for(int b = 0; b < numBands; b++) {
if(min[b] == max[b]) {
gain[b] = 0.0;
bias[b] = min[b] == 0.0 ? 0.0 : 255.0;
} else {
gain[b] = 255.0/(max[b] - min[b]);
bias[b] = -gain[b]*min[b];
}
}

RenderedOp rescaled =
JAI.create("format",
JAI.create("rescale", img, gain, bias),
DataBuffer.TYPE_BYTE);

Brian

On Thu, 25 May 2006, George Yang wrote:

> This is my code to convert a 16-bit image to 8-bit, and I am getting a white
> image back, can someone point out the error?
>
> // get image width and height
> int width = pImg.getWidth();
> int height = pImg.getHeight();
> // which are the max and min of the image?
> ParameterBlock pbMaxMin = new ParameterBlock();
> pbMaxMin.addSource(pImg);
> RenderedOp extrema = JAI.create("extrema", pbMaxMin);
> // must get the extrema of all bands
> double[] allMins = (double[])extrema.getProperty("minimum");
> double[] allMaxs = (double[])extrema.getProperty("maximum");
> // assume that the image is single band
> double min = allMins[0];
> double max = allMaxs[0];
> // rescale the image with the parameters
> double[] multiplyByThis = new double[1];
> multiplyByThis[0] = 255./(max - min);
> double[] addThis = new double[1];
> addThis[0] = min;
> // now we can rescale the pixels gray levels
> ParameterBlock pbRescale = new ParameterBlock();
> pbRescale.add(multiplyByThis);
> pbRescale.add(addThis);
> pbRescale.addSource(pImg);
> PlanarImage surrogateImage = (PlanarImage)JAI.create("Rescale",
> pbRescale);
> // let's convert the data type for displaying
> ParameterBlock pbConvert = new ParameterBlock();
> pbConvert.addSource(surrogateImage);
> pbConvert.add(DataBuffer.TYPE_BYTE);
> surrogateImage = JAI.create("format", pbConvert);
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>
>

----------------
Brian Burkhalter
Java Media, Imaging, and Graphics
Sun Microsystems, Inc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net