-1
  1. I have a full red image I made using MS Paint (red = 255, blue = 0, green = 0)
  2. I read that image into a File object file
  3. Then I extracted the bytes using Files.readAllBytes(file.toPath()) into a byte array byteArray
  4. Now my expectation is that :

    a) byteArray[0], when converted to bitstream, should be all 1

    b) byteArray[1], when converted to bitstream, should be all 0

    c) byteArray[2], when converted to bitstream, should be all 0

because, as I understand, the pixels values are stored in the order RGB with 8 bits for each color.

When I run my code, I don't get expected outcome. byteArray[0] is all 1 alright, but the other 2 aren't 0s.

Where am I going wrong?

Edit

As requested, I'm including image size, saved format and code used to read it.

Size = 1920p x 1080p

Format = JPG

Code:

        File file = new File("image_path.jpg");
        byte byteArray[]= new byte[(int) file.length()];
        try {
            byteArray = Files.readAllBytes(file.toPath());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int bits[] = new int[8];
        for(int j=0; j<8; j++)
        {
            bits[j] = (b[0] & (1 << j))==0 ? 0:1 ;
            //System.out.println("bitsb :"+bitsb[j]);
        }

Update

Unfortunately I am unable to make use of other questions containing ImageIO library functions. I'm here partly trying to understand how the image itself is stored, and how I can write my own logic for retrieving and manipulating the image files.

halfer
  • 19,824
  • 17
  • 99
  • 186
sonofel
  • 112
  • 3
  • 8
  • 1
    You did not specify what format you saved the image in. All image files have headers, and the way they arrange actual pixel data is different. Specify the exact image format and size (some formats have line padding!) and show your code for reading the file! – Lev M. May 09 '20 at 14:44
  • @LevM. , I have edited the question – sonofel May 09 '20 at 14:53
  • Does this answer your question? [Get color of each pixel of an image using BufferedImages](https://stackoverflow.com/questions/22391353/get-color-of-each-pixel-of-an-image-using-bufferedimages) – Progman May 09 '20 at 15:04

1 Answers1

3

JPEG is a complex image format.

It does not hold the raw image pixel data, but instead has a header, optional metadata and compressed image data.

The algorithm to decompress it to raw pixel values is quite complex, but there are libraries that will do the work for you.

Here is a short tutorial: https://docs.oracle.com/javase/tutorial/2d/images/loadimage.html

Here is the documentation of the BufferedImage class which will hold the image data: https://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html

You will need to use one of the getRGB functions to access the raw pixel data. Make sure to check that your image is in 24 bit color format, if you want each color component to take 1 byte exactly!

JPEG supports other formats such as 32 and 16 bits!

Alternatively, save your image as 24 bit uncompressed BMP. The file will be much larger, but reading it is much simpler so you don't have to use a library. Just skip the header, then read raw bytes.

An even simpler image format to work with would be PBM/PPM.

Lev M.
  • 6,088
  • 1
  • 10
  • 23