I have a byte array representing a BMP image in Windows format, and I would like my library to present it in a Java application as BufferedImagewithout copying pixel data.
The main problem is that all implementations Rasterin the JDK store image pixels in order from top to bottom, from left to right, while BMP pixel data is stored from bottom to top, from left to right. If this is not compensated, the resulting image will be flipped vertically.
The most obvious βsolutionβ is to set the property SampleModel scanlineStrideto a negative value and change the range offsets (or array offsets DataBuffer) to point to the upper left pixel, that is, the first pixel of the last row in the array. Unfortunately, this does not work, because all constructors SampleModelthrow an exception if a negative argument is given scanlineStride.
I am currently working on this, forcing the field scanlineStrideto use reflection with a negative value, but I would like to make it a cleaner and more portable way, if possible. for example, is there any other way to trick Rasteror SampleModelto arrange the pixels in order from bottom to top, but without breaking encapsulation? Or is there a library somewhere that will wrap Rasterand SampleModelpresent rows of pixels in reverse order?
I would prefer to avoid the following approaches:
- Copy the entire image (for performance reasons). The code must process hundreds of large (> = 1 Mpixels) images per second, and although the entire image should be accessible to the application, it will usually only have access to the tiny (but complex) to predict) part of the image.)
DataBuffer ( , "" , scanline/pixel.)Raster / SampleModel (- , ( , Sun JDK), SampleModel, BottomUpSampleModel .)