Forum Topic: Bitmaps in Java

(173 views • 9 replies)

This topic is 1 page long.

<< < > >>
None

Blaze

Reply To Post Reply & Quote

Posted at: 9/16/09 02:35 AM

Blaze LIGHT LEVEL 22

Sign-Up: 08/04/05

Posts: 6,989

I know, I know. Bitmaps and Java. Ugh.

It's a small project I have to do. Thing is, I'm very limited here. (Can't use any mystic functions, or the like). So I'm having some trouble... Heh.

I had a bit of a hard time at the beggining, trying to understand the source code the one who asked me for the project gave me. Thing is, it was useless (it was too simple and didn't give me any useful information)..

I know my question will probably sound stupid,, but here goes anyway.

All images being read will be 24 bit bitmap, and 640x480. Now... I have a question.

Theoretically speaking, a standard non-compressed image that's 640x480, should weigh 640x480x3, right? being 24 bits, having 3 bytes per pixel.

And effectively, it does. However, I'm having second thoughts on how to obtain the array of pixels from a bitmap... The array of a size 640x480x3 would be too huge for java to handle. Not to mention it would then take AGES to parse it into three arrays for R G and B.

I thought about doing it line by line, 640 pixels at a time.

Am I doing something wrong? I'm loading byte by byte into an array. Therefore, i assume every three bytes I load is a full pixel.

Please, This is a bit urgent.

Thanks in advance to any useful information you could provide. I've already researched a lot, and haven't gotten to a full answer.


None

CronoMan

Reply To Post Reply & Quote

Posted at: 9/16/09 05:10 AM

CronoMan EVIL LEVEL 06

Sign-Up: 07/19/04

Posts: 2,987

At 9/16/09 02:35 AM, Blaze wrote: And effectively, it does. However, I'm having second thoughts on how to obtain the array of pixels from a bitmap... The array of a size 640x480x3 would be too huge for java to handle. Not to mention it would then take AGES to parse it into three arrays for R G and B.

Use a one-dimensional array of bytes (rgbrgbrgbrgbrgb etc)
Talking of which : http://blogs.msdn.com/ericlippert/archiv e/2008/09/22/arrays-considered-somewhat-
harmful.aspx

"no sound in ass"


None

Blaze

Reply To Post Reply & Quote

Posted at: 9/16/09 02:33 PM

Blaze LIGHT LEVEL 22

Sign-Up: 08/04/05

Posts: 6,989

At 9/16/09 05:10 AM, CronoMan wrote: Use a one-dimensional array of bytes (rgbrgbrgbrgbrgb etc)
Talking of which : http://blogs.msdn.com/ericlippert/archiv e/2008/09/22/arrays-considered-somewhat-
harmful.aspx

But wouldnt that array turn to be just huge?
And assuming I did that, lets say, how would the array be formed? [R][G][B][R][G][B] etC?
I'm confused :S

Thanks for the link aswell, looks interesting. I'll read into it.


None

kiwi-kiwi

Reply To Post Reply & Quote

Posted at: 9/16/09 03:01 PM

kiwi-kiwi LIGHT LEVEL 08

Sign-Up: 03/06/09

Posts: 657

If you are so worried about the dimensions of the array, just use a linked list. It's gonna be a whole lot easier for you to use an array though.
Implementation wise either make use of Integers (AFAIK ints are on 32 bits in java so it should work) that you instantiate as MAX_VALUE then use >>, << and & using the byte values as a mask, which is kinda like bragging.
Other option is to make yourself a custom class with three properties r,g and b, make an array or linked list, set every value as you read the file and then use them.


None

Blaze

Reply To Post Reply & Quote

Posted at: 9/16/09 03:20 PM

Blaze LIGHT LEVEL 22

Sign-Up: 08/04/05

Posts: 6,989

At 9/16/09 03:01 PM, kiwi-kiwi wrote: If you are so worried about the dimensions of the array, just use a linked list. It's gonna be a whole lot easier for you to use an array though.

Hmm. I think I'm going to have to go around with that. Thanks for the link, too.

Implementation wise either make use of Integers (AFAIK ints are on 32 bits in java so it should work) that you instantiate as MAX_VALUE then use >>, << and & using the byte values as a mask, which is kinda like bragging.

I've read about it. Running a few tests with it as we speak using PixelGrabber. :D Thanks, too!

Other option is to make yourself a custom class with three properties r,g and b, make an array or linked list, set every value as you read the file and then use them.

That's pretty much what I'm aiming to do; But I need to modify the whole image itself, and I need to get a full array or linked list with all the pixels.

Thank you for your response!


None

dELtaluca

Reply To Post Reply & Quote

Posted at: 9/16/09 05:21 PM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,559

At 9/16/09 02:35 AM, Blaze wrote: The array of a size 640x480x3 would be too huge for java to handle. Not to mention it would then take AGES to parse it into three arrays for R G and B.

... sorry if I'm mistaken, but i thought Java was meant to be fast? If Java honestly can't handle that, then it's a bit of a shit language :P

My social worker says im special!

BBS Signature

None

Blaze

Reply To Post Reply & Quote

Posted at: 9/16/09 05:25 PM

Blaze LIGHT LEVEL 22

Sign-Up: 08/04/05

Posts: 6,989

At 9/16/09 05:21 PM, dELtaluca wrote: ... sorry if I'm mistaken, but i thought Java was meant to be fast? If Java honestly can't handle that, then it's a bit of a shit language :P

Oh, that was just an assumption. (: No need to be sarcastic, dELta. Hehe.

Anyway, I finally managed to do it. I ended up using a huge unidimensional array. Works nicely. I still have to hate microsoft for having bmps load bottom-top, and backwards RGB. D: But still, everythings going smooth.

Thanks guys :)


None

CronoMan

Reply To Post Reply & Quote

Posted at: 9/16/09 05:27 PM

CronoMan EVIL LEVEL 06

Sign-Up: 07/19/04

Posts: 2,987

At 9/16/09 02:33 PM, Blaze wrote: But wouldnt that array turn to be just huge?

Depends on how you define "huge"
640x480x3 turns out to be around 1MB or RAM required

And assuming I did that, lets say, how would the array be formed? [R][G][B][R][G][B] etC?
private byte[] bitmapdata = new byte[640 * 480 * 3];

public int getColor(int x, int y)
{
    int pos = 640 * y + x;
    return (bitmapdata[pos] | (bitmapdata[pos + 1] << 1) | (bitmapdata[pos + 2] << 2));
}
I'm confused :S

:P

"no sound in ass"


None

CronoMan

Reply To Post Reply & Quote

Posted at: 9/16/09 05:40 PM

CronoMan EVIL LEVEL 06

Sign-Up: 07/19/04

Posts: 2,987

At 9/16/09 05:25 PM, Blaze wrote: I still have to hate microsoft for having bmps load bottom-top, and backwards RGB. D: But still, everythings going smooth.

To shed some light on this;
BMP was one of the first image formats to surface
Originally it was monochrome (hence the name "Bit-Map"), it is stored upsidedown, because this makes sense in a mathematical grid (not for CRT monitors though)
The colors aren't stored "backwards", they are stored as BGR simply because they chose to use the names alphabetically. RGB was something that spawned from Visual Basic (believe it or not)
There is no unifying standard for what order the colors should be stored
What annoys me more about the BMP format is the things that noone knows what is for;
planes (which was meant for animation), pels (fuck knows what that is) and other useless junk
additionally, I find it frustrating that there is a formula you have to know in order to find the first scanline in the data; why isn't the bitmapdata stored just immediately after the header instead? Why is there a field in the header that tells the entire file size? Wtf?

Well, BMP is nearing 30 years, I guess I could give it some slack

"no sound in ass"


None

Blaze

Reply To Post Reply & Quote

Posted at: 9/16/09 08:15 PM

Blaze LIGHT LEVEL 22

Sign-Up: 08/04/05

Posts: 6,989

At 9/16/09 05:40 PM, CronoMan wrote: Well, BMP is nearing 30 years, I guess I could give it some slack

Haha, thanks for the info aswell. :)
But yes, I do find some of the info in the header to be quite useless nowadays.


All times are Eastern Standard Time (GMT -5) | Current Time: 06:09 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!