java - BufferedImage getRGB vs Raster getSample -
I am trying to do some image processing in Java. I used the ImageIO library to read and write images. I can read image pixel values in two ways (thus there may be other methods).
-
Use the getRGB method of BufferedImage:
pixel = image.getRGB (X, y);
-
Using the GetSample method of the raster:
Writer raster raster = image.getRaster ();
pixel = raster.get sample (x, y, 0);What is the difference between two approaches?
1: The first approach will always return a pixel in the int ARGF format, and the sRGB color space will be there. Despite the internal representation of the image, it means that unless the internal representation of the image is done
TYPE_INT_ARGB , some conversions have to be done. It is sometimes useful, because it predicts, but it is often as slow as an example, color space conversion is quite expensive. In addition, if the image per sample is 8 bits per sample and / or 4 pixels per one more precision, then there is accurate damage. Whether it can be acceptable or not, in case of your use. 2: The second approach can give you a pixel value, but not in all cases, as you give sample value at (x, y)) for Band 0 (the first band). For
TYPE_INT_ARGB this will be similar to pixel value. This will be an index for use in
TYPE_BYTE_INDEXED (you will need to see it to get pixel value). This will give you only blue values for TYPE_3BYTE_BGR (you'll need to add it to the band 1 and 2 samples to get the full pixel value). Etc. for other types For samples that do not represent internally as an int, a data type conversion occurs (and precision loss in rare cases). It can work for you, but I have never used too much for
getSample (...) methods.
Instead, I suggest that I look into what I believe is the fastest way to get pixel data using the
getDataElements method:
object pixel = null; // Getting Started on the first invitation of getDataElements (y) {for (x) {pixel = raster.getDataElements (x, y, pixel) to start pixel; }}
This will give you the "basic" value from the data buffer without any conversion.
Then you will need special handling for each transfer type (see the
Databuror category), and possibly a normal fallback for non-standard types.
This pixel values will have a "problem" similar to your approach 2 vs. normalized RGB value, so that you need to "convert" manually / see.
What the approach is better, as always depends on you will have to look at each use case and decide whether it is more important. Ease / simplicity, or best possible performance (or perhaps the best quality?)
Comments
Post a Comment