Reputation: 62
As I'm developing some image processing app in JAVA, I wonder which way is the fastest and less memory consuming for storing an image. So far I figured out I can do it by storing all loaded data in int[] array (of width*height length, each entry represents 32-bit RGBA color), in BufferedImage object, or in Image object. Mainly purpose of application is to transform 2D images and to perform specific structures (let's say edges, corners, etc.) detection algorithm.
Thanks in advance for every reply/comment!
Upvotes: 2
Views: 1909
Reputation: 54649
I started this as a comment, but it became longer and longer - maybe it qualifies as an answer.
BufferedImage
is extending Image
. So the main choice is between a BufferedImage
and a raw int[]
array.
Concerning the memory overhead of a BufferedImage
over an int[]
array: This should become neglibible for "reasonably large" images, because a BufferedImage
(with TYPE_INT_*
) does not store much more than an int[]
array with the actual data internally. (Particularly, this int[]
array is stored as the data of a DataBufferInt
in the Raster
).
Concerning the performance, there may be a noticable or even significant difference between accessing raw array elements and accessing the pixels of a BufferedImage
. BufferedImage
s are fast, particularly for drawing, but when it comes to computations on pixel values, nothing beats a plain, raw int[]
array.
However, this sheds only little light at the broad spectrum of questions that may influence the final decision. For example, when you say that you want to do things like edge detection, you might notice that this functionality is already available for BufferedImage
, as shown in the Image Processing and Enhancement section of the Java2D Imaging Tech Notes. You won't be able to implement an edge detection by convolution with a sobel filter on a plain int[]
array with just 4 (four) lines of code.
BTW: The tech notes in general provide some background information, and are definitely worth a look in order to make a profound decision here.
There is also an answer on the GameDev stackexchange site that covers some of of the aspects that are relevant here, maybe you'll find it interesting.
Upvotes: 1