Skip to main content

[JAI] Convert to 4-bit grayscale BMP

1 reply [Last post]
Anonymous

I'm trying to convert images to 4-bit grayscale BMP.

I first convert to greyscale:

public static BufferedImage renderToGreyThumb(Image srcImage, int
thumbWidth, int thumbHeight, int bitDepth)
{
BufferedImage thumbImage = null;
try
{
// Determine thumbnail size and scale ration from
WIDTH and HEIGHT
double thumbRatio = (double)thumbWidth /
(double)thumbHeight;
int imageWidth = srcImage.getWidth(null);
int imageHeight = srcImage.getHeight(null);
double imageRatio = (double)imageWidth /
(double)imageHeight;
if (thumbRatio < imageRatio) {
thumbHeight = (int)(thumbWidth /
imageRatio);
} else {
thumbWidth = (int)(thumbHeight *
imageRatio);
}

// Draw original image to thumbnail image object and
// Scale it to the new size on-the-fly (drawImage is
quite powerful)

// Create a 4-bit grayscale color model.
ColorSpace cs =
ColorSpace.getInstance(ColorSpace.CS_GRAY);
int[] nBits = {bitDepth};
ComponentColorModel colorModel = new
ComponentColorModel(cs, nBits, false, true,

Transparency.OPAQUE,

DataBuffer.TYPE_USHORT);
WritableRaster raster =
colorModel.createCompatibleWritableRaster(thumbWidth, thumbHeight);
thumbImage = new BufferedImage(colorModel, raster,
false, null);

// Render the source image into the thumb image...
Graphics2D graphics2D = thumbImage.createGraphics();

graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.
VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(srcImage, 0, 0, thumbWidth,
thumbHeight, null);
}
catch (Exception e)
{
System.err.println("Error rendering to greyscale: "
+ e.toString());
}
return thumbImage;
}

and then write to a bmp;

public static void writeBMPImage(BufferedImage thumbImage, String
outFileName)
{
try
{
//OutputStream os = new
FileOutputStream(outFileName);
//BMPEncodeParam param2 = new BMPEncodeParam();
//ImageEncoder enc =
BMPCodec.createImageEncoder("BMP", os, param2);
//enc.encode(thumbImage);
//os.close();

JAI.create("filestore",thumbImage,outFileName,"bmp");
}
catch (Exception e)
{
System.err.println("Error writing BMP file: " +
outFileName + "\n" + e.toString());
}

}

Is this the right approach? It looks to me like I always get 8-bit images.

Regards,

Dan Donahue
www.enfora.com
ddonahue@enfora.com
972-633-4468

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

Reply viewing options

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

Looks like the BMP ImageEncoder provided in com.sun.media.jai.codec gets the
bits per sample from the SampleModel not the ColorModel.

If you are just starting out here you should use the Image I/O API defined in
javax.imageio. As of Java 5 core J2SE includes a BMP reader-writer here. If
you have to use J2SE 1.4, then you could use JAI-Image I/O Tools in
conjunction with it to obtain a BMP plug-in.

Brian

On Thu, 30 Jun 2005, Dan Donahue wrote:

> I'm trying to convert images to 4-bit grayscale BMP.
>
> I first convert to greyscale:
>
> public static BufferedImage renderToGreyThumb(Image srcImage, int
> thumbWidth, int thumbHeight, int bitDepth)
> {
> BufferedImage thumbImage = null;
> try
> {
> // Determine thumbnail size and scale ration from
> WIDTH and HEIGHT
> double thumbRatio = (double)thumbWidth /
> (double)thumbHeight;
> int imageWidth = srcImage.getWidth(null);
> int imageHeight = srcImage.getHeight(null);
> double imageRatio = (double)imageWidth /
> (double)imageHeight;
> if (thumbRatio < imageRatio) {
> thumbHeight = (int)(thumbWidth /
> imageRatio);
> } else {
> thumbWidth = (int)(thumbHeight *
> imageRatio);
> }
>
> // Draw original image to thumbnail image object and
> // Scale it to the new size on-the-fly (drawImage is
> quite powerful)
>
> // Create a 4-bit grayscale color model.
> ColorSpace cs =
> ColorSpace.getInstance(ColorSpace.CS_GRAY);
> int[] nBits = {bitDepth};
> ComponentColorModel colorModel = new
> ComponentColorModel(cs, nBits, false, true,
>
> Transparency.OPAQUE,
>
> DataBuffer.TYPE_USHORT);
> WritableRaster raster =
> colorModel.createCompatibleWritableRaster(thumbWidth, thumbHeight);
> thumbImage = new BufferedImage(colorModel, raster,
> false, null);
>
> // Render the source image into the thumb image...
> Graphics2D graphics2D = thumbImage.createGraphics();
>
> graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.
> VALUE_INTERPOLATION_BILINEAR);
> graphics2D.drawImage(srcImage, 0, 0, thumbWidth,
> thumbHeight, null);
> }
> catch (Exception e)
> {
> System.err.println("Error rendering to greyscale: "
> + e.toString());
> }
> return thumbImage;
> }
>
>
> and then write to a bmp;
>
> public static void writeBMPImage(BufferedImage thumbImage, String
> outFileName)
> {
> try
> {
> //OutputStream os = new
> FileOutputStream(outFileName);
> //BMPEncodeParam param2 = new BMPEncodeParam();
> //ImageEncoder enc =
> BMPCodec.createImageEncoder("BMP", os, param2);
> //enc.encode(thumbImage);
> //os.close();
>
> JAI.create("filestore",thumbImage,outFileName,"bmp");
> }
> catch (Exception e)
> {
> System.err.println("Error writing BMP file: " +
> outFileName + "\n" + e.toString());
> }
>
> }
>
>
> Is this the right approach? It looks to me like I always get 8-bit images.
>
>
> Regards,
>
> Dan Donahue
> www.enfora.com
> ddonahue@enfora.com
> 972-633-4468
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
> For additional commands, e-mail: interest-help@jai.dev.java.net
>
>

----------------
Brian Burkhalter
Advanced Development/Media & Entertainment
Market Development Engineering
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.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net