Java Convert PPM Byte array to JPG image - java

Currently working on Image manipulation in Java
I have the byte array(PPM) of size 921600 (640*480*3)
byte[] image; // PPM
BufferedImage image = ImageIO.read(new ByteArrayInputStream(image));
image is null.
Tried with ImageMagic and JAI libraries. But it does not help me.
Is it possible to get the RGB components from byte array and convert it to JPG file.
Any help is appreciated.

Below is the Code (which will convert PPM(byte array to Buffered image and you can save buffered image to the file)
// Method Call
BufferedImage image = ppm(width, height, 255, byte[]);
//Method Definition
static public BufferedImage ppm(int width, int height, int maxcolval, byte[] data){
if(maxcolval<256){
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
int r,g,b,k=0,pixel;
if(maxcolval==255){ // don't scale
for(int y=0;y<height;y++){
for(int x=0;(x<width)&&((k+3)<data.length);x++){
r=data[k++] & 0xFF;
g=data[k++] & 0xFF;
b=data[k++] & 0xFF;
pixel=0xFF000000+(r<<16)+(g<<8)+b;
image.setRGB(x,y,pixel);
}
}
}
else{
for(int y=0;y<height;y++){
for(int x=0;(x<width)&&((k+3)<data.length);x++){
r=data[k++] & 0xFF;r=((r*255)+(maxcolval>>1))/maxcolval; // scale to 0..255 range
g=data[k++] & 0xFF;g=((g*255)+(maxcolval>>1))/maxcolval;
b=data[k++] & 0xFF;b=((b*255)+(maxcolval>>1))/maxcolval;
pixel=0xFF000000+(r<<16)+(g<<8)+b;
image.setRGB(x,y,pixel);
}
}
}
return image;
}
else{
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
int r,g,b,k=0,pixel;
for(int y=0;y<height;y++){
for(int x=0;(x<width)&&((k+6)<data.length);x++){
r=(data[k++] & 0xFF)|((data[k++] & 0xFF)<<8);r=((r*255)+(maxcolval>>1))/maxcolval; // scale to 0..255 range
g=(data[k++] & 0xFF)|((data[k++] & 0xFF)<<8);g=((g*255)+(maxcolval>>1))/maxcolval;
b=(data[k++] & 0xFF)|((data[k++] & 0xFF)<<8);b=((b*255)+(maxcolval>>1))/maxcolval;
pixel=0xFF000000+(r<<16)+(g<<8)+b;
image.setRGB(x,y,pixel);
}
}
return image;
}
}

You can use a WritableRaster to set the pixels in the image for you:
For a grayscale image, you will have a byte array like this:
byte[] arr = new byte[width * height];
To make an image, use:
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
img.getRaster().setDataElements(0, 0, width, height, arr);
For a color image, you will have an array like this:
byte[] arr = new byte[width * height * 3];
So, to make an image, use:
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
img.getRaster().setDataElements(0, 0, width, height, arr);
You may need to mess this the TYPE in the first line. See here to choose which type your image is.

Related

Importing grayscale image to bufferedimage

In CSV semicolon file I have saved grayscale values of pixels from my image. I want to import this values into my program as BufferedImage class object. I can but result image is more faded.
There are steps:
public static BufferedImage loadImageFromCSV(File file) throws Exception {
BufferedImage bufferedImage = null;
//load CSV file data to memory
List<String> linesFromCsvFile = CSVUtils.parseCSV(file);
//calculate width and height
int width = getWidthFromCSVLines(linesFromCsvFile);
int height = getHeightFromCSVLines(linesFromCsvFile);
//get values form CSV lines
Integer[][] pixels = asTwoDimensionalArray(linesFromCsvFile);
//generate image
bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
for (int y=0; y<height; ++y) {
for (int x=0; x<width; ++x) {
int rgb = pixels[y][x] | (pixels[y][x] << 8) | (pixels[y][x] << 16);
bufferedImage.setRGB(x, y, rgb );
}
}
return bufferedImage;
}
Below on the left side grayscale image and on the right side imported image to my program:
CSV file looks ok: https://filebin.net/9mp7nifqu045b48e
Please help

Convert 8bit Grayscale image byte array to a BufferedImage

I have a byte array containing data of the raw grayscale 8bit image, which I need to convert to a BufferedImage. I've tried doing:
BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes));
However, the resulting image object is null which means I'm doing something wrong here.
What's the correct way of making such a conversion?
There are two good ways to do this, depending on your use case.
Either create a new, gray image, and copy the data into it. This will keep the image "managed", which may lead to better rendering performance (ie. on screen). But it will need twice as much memory, and copy the data from your input to the image.
The other, is to create the gray image directly "around" your existing pixel data. This will be faster, and use almost no extra heap, as it avoids copying the pixel data. But the image will not be managed (as the backing array is exposed and mutable).
Both options are demonstrated below:
int w = 640;
int h = 480;
byte[] imageBytes = new byte[w * h];
// 1 Keeps the image "managed" at the expense of twice the memory + a large array copy
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
image.getRaster().setDataElements(0, 0, w, h, imageBytes);
System.out.println("image: " + image);
// 2 Faster, and uses less memory, but will make the image "unmanaged"
ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_GRAY), false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
WritableRaster raster = Raster.createInterleavedRaster(new DataBufferByte(imageBytes, imageBytes.length), w, h, w, 1, new int[]{0}, null);
BufferedImage image2 = new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null);
System.out.println("image2: " + image2);
If the image data isn't in linear gray color space, one could use an IndexColorModel to map the input into whatever range you want:
// Alternate, using IndexColorModel, if your input isn't in linear gray color space
int[] cmap = new int[256]; // TODO: Add ARGB packed colors here...
IndexColorModel icm = new IndexColorModel(8, 256, cmap, 0, false, -1, DataBuffer.TYPE_BYTE);
// As 1
BufferedImage image3 = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_INDEXED, icm);
image3.getRaster().setDataElements(0, 0, w, h, imageBytes);
System.out.println("image3: " + image3);
// As 2
BufferedImage image4 = new BufferedImage(icm, raster, cm.isAlphaPremultiplied(), null);
System.out.println("image4: " + image4);
I've managed to did the conversion for the 640x480 resolution the following way:
BufferedImage image = new BufferedImage(640,480,BufferedImage.TYPE_BYTE_INDEXED);
int i = 0;
for(int y = 0; y < 480; y++)
{
for(int x = 0; x < 640; x++)
{
int g = imageBytes[i++] & 0xFF;
image.setRGB(x,y,new Color(g,g,g).getRGB());
}
}
EDIT: removed useless code (thanks to Marco13)
Java
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
image.getRaster().setDataElements(0, 0, width, height, array));
Kotlin
val image = BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY)
image.raster.setDataElements(0, 0, width, height, byteArray )

Why isn't my method for removing this color working?

I'm trying to make a Mario game clone, and right now, in my constructor, I have a method that is supposed to make a certain color transparent instead of the current pinkish (R: 255, G: 0, B: 254). According to Photoshop, the hex value is ff00fe. My method is:
public Mario(){
this.state = MarioState.SMALL;
this.x = 54;
this.y = 806;
URL spriteAtLoc = getClass().getResource("sprites/Mario/SmallStandFaceRight.bmp");
try{
sprite = ImageIO.read(spriteAtLoc);
int width = sprite.getWidth();
int height = sprite.getHeight();
int[] pixels = new int[width * height];
sprite.getRGB(0, 0, width, height, pixels, 0, width);
for (int i = 0; i < pixels.length; i++) {
if (pixels[i] == 0xFFff00fe) {
pixels[i] = 0x00ff00fe; //this is supposed to set alpha value to 0 and make the target color transparent
}
}
} catch(IOException e){
System.out.println("sprite not found");
e.printStackTrace();
}
}
it runs and compiles, but sprite comes out exactly the same when I render it. (edit: perhaps of note I do not have super.paintComponent(g) in my paintComponent(g) method. The sprites are .bmps.
You are only retrieving the pixels using BufferedImage.getRGB. That returns a copy of the data in a certain area of the BufferedImage.
Any change you make to the int[] returned is not automatically reflected back into the image.
To update the image, you need to call BufferedImage.setRGB after you change the int[]:
sprite.setRGB(0, 0, width, height, pixels, 0, width);
Another change you should probably make (and this involves a little guesswork as I don't have your bmp to test with) - the BufferedImage returned by ImageIO.read may have type BufferedImage.TYPE_INT_RGB - meaning that it doesn't have an alpha channel. You can verify by printing sprite.getType(), if that prints 1 it's TYPE_INT_RGB without an alpha channel.
To get an alpha channel, create a new BufferedImage of the right size and then set the converted int[] on that image, then use the new image from then on:
BufferedImage newSprite = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
newSprite.setRGB(0, 0, width, height, pixels, 0, width);
sprite = newSprite; // Swap the old sprite for the new one with an alpha channel
BMP images don't provide an alpha channel, you have to set it manually (as you do in your code)...
when you check your pixel to have a certain color you have to check without alpha (BMP has no alpha it's always 0x0).
if (pixels[i] == 0x00ff00fe) { //THIS is the color WITHOUT alpha
pixels[i] = 0xFFff00fe; //set alpha to 0xFF to make this pixel transparent
}
so in short: you did all right but mixed it up a bit ^^
This works:
private BufferedImage switchColors(BufferedImage img) {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
// top left pixel is presumed to be BG color
int rgb = img.getRGB(0, 0);
for (int xx=0; xx<w; xx++) {
for (int yy=0; yy<h; yy++) {
int rgb2 = img.getRGB(xx, yy);
if (rgb2!=rgb) {
bi.setRGB(xx, yy, rgb2);
}
}
}
return bi;
}

Int Pixel Array To Image

I am trying to take an int array of pixels from a Bitmap on Android and use the int array to make a buffered image. But I'm having some problems. I am able to get the bitmap from android with no problem:
Bitmap b = BitmapFactory.decodeByteArray(data, 0, data.length);
b = Bitmap.createScaledBitmap(b, (int)(b.getWidth() * .3125), (int)(b.getHeight() * .44444444444), false);
int[] arr = new int[b.getHeight() * b.getWidth()];
b.getPixels(arr, 0, b.getWidth(), 0, 0, b.getWidth(), b.getHeight());
I scale the bitmap down and then get the pixels and put them into the int array. Then I send them over a network to the PC where I try to recreate it to a BufferedImage (networking isn't important). Here is the code I use to convert the pixel array to a BufferedImage.
int width = 600;
int height = 479;
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
bi.setRGB(0, 0, width, height, arr, 0, width);
g2d.drawImage(bi, 10, 50, null);
The variable "arr" is the byte array of pixels. Converting to a BufferedImage gives no error and shows the following image:
Obviously this isn't the picture I am trying to get. When I print out 5 of the pixels in the array (just for testing) I get the following and this is the format of the pixels:
-15528956
-15200766
-13558523
-11718123
-12243954
-13294582

Wrong brightness converting image to grayscale in Java

I'm converting a image to gray scale in Java with the following code:
BufferedImage originalImage = ImageIO.read(new File("/home/david/input.bmp"));
BufferedImage grayImage = new BufferedImage(originalImage.getWidth()
, originalImage.getHeight()
, BufferedImage.TYPE_BYTE_GRAY);
ColorSpace gray = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp colorConvert = new ColorConvertOp(gray, null);
colorConvert.filter(originalImage, grayImage);
ImageIO.write(grayImage, "bmp", new File("/home/david/output_java.bmp"));
That seems to work, but the problem is that the output image is very different from the gray scale image generated by gimp (see examples below).
Can I control someway how is the image generated?
How I can make the result more similar to the gimp result?
Original image:
Gray scale image generated in Java:
Gray scale image generated in Gimp (Image -> Mode -> Grayscale):
BTW: I have a bunch of images coming from ffmpeg (with gray option) and they are like Gimp images so because of that I want my image in that way.
Finally I've wrote GrayscaleFilter class implementing BufferedImageOp interface.
I've followed this really good guide about Java image processing.
This is the relevant code fragment:
public class GrayscaleFilter extends AbstractFilter
{
public final static double[] METHOD_AVERAGE = {1.0/3.0, 1.0/3.0, 1.0/3.0};
public final static double[] METHOD_GIMP_LUMINOSITY = {0.21, 0.71, 0.07};
public GrayscaleFilter(final double[] rgb)
{
this(rgb[0], rgb[1], rgb[2]);
}
public BufferedImage filter(BufferedImage src, BufferedImage dest)
{
if (src.getType() == BufferedImage.TYPE_BYTE_GRAY)
{
dest = src;
return dest;
}
if (dest == null)
dest = createCompatibleDestImage(src, null);
final int width = src.getWidth();
final int height = src.getHeight();
int[] inPixels = new int[width * height];
GraphicsUtilities.getPixels(src, 0, 0, width, height, inPixels);
byte[] outPixels = doFilter(inPixels);
GraphicsUtilities.setPixels(dest, 0, 0, width, height, outPixels);
return dest;
}
private byte[] doFilter(int[] inputPixels)
{
int red, green, blue;
int i = 0;
byte[] outPixels = new byte[inputPixels.length];
for(int pixel : inputPixels)
{
// Obtengo valores originales
red = (pixel >> 16) & 0xFF;
green = (pixel >> 8) & 0xFF;
blue = pixel & 0xFF;
// Calculo valores nuevos
outPixels[i++] = (byte)(
red * red_part +
green * green_part +
blue * blue_part
);
}
return outPixels;
}
public BufferedImage createCompatibleDestImage(BufferedImage src, ColorModel destCM)
{
return new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
}
}
Find out the conversion formula used by Gimp. It probably takes some human color perception into account, while the Java implementation is mathematical (R+G+B)/ 3.

Categories

Resources