Libraryless. Click here for Pure Java version (9300L/52K).
1 | static GrabbableRGBBytePixels grabbableRGBBytePixels(BufferedImage img) { |
2 | Raster raster = img.getRaster(); |
3 | SampleModel _sampleModel = raster.getSampleModel(); |
4 | if (!_sampleModel instanceof PixelInterleavedSampleModel) null; |
5 | |
6 | // Check if we need a conversion to RGB (e.g. after unpacking a JPEG) |
7 | var imageModel = img.getColorModel(); |
8 | if (imageModel != ColorModel.getRGBdefault()) null; |
9 | |
10 | PixelInterleavedSampleModel sampleModel = cast _sampleModel; |
11 | DataBufferByte dataBuffer = cast raster.getDataBuffer(); |
12 | assertEquals(1, dataBuffer.getNumBanks()); |
13 | assertEquals(DataBuffer.TYPE_BYTE, dataBuffer.getDataType()); |
14 | |
15 | // Let's at this point assume the raster data is what we |
16 | // think it is... (TODO: test on unusual platforms) |
17 | |
18 | int w = img.getWidth(), h = img.getHeight(); |
19 | int pixelStride = sampleModel.getPixelStride(); |
20 | int scanlineStride = sampleModel.getScanlineStride(); |
21 | byte[] pixels = dataBuffer.getData(); |
22 | int offset = dataBuffer.getOffset(); |
23 | int translateX = raster.getSampleModelTranslateX(); |
24 | int translateY = raster.getSampleModelTranslateY(); |
25 | offset += -translateX-translateY*scanlineStride; |
26 | |
27 | ret GrabbableRGBBytePixels(pixels, w, h, offset, scanlineStride, pixelStride); |
28 | } |
Began life as a copy of #1030544
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1035057 |
Snippet name: | grabbableRGBBytePixels - try to rip RGB byte[] pixels from a BufferedImage without copying |
Eternal ID of this version: | #1035057/6 |
Text MD5: | 1549ad8d34efaafd0cf2fafc99305b7a |
Transpilation MD5: | 8802bf1f4456f1d6aeea02283dc0038d |
Author: | stefan |
Category: | javax / imaging |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-08-08 19:08:32 |
Source code size: | 1250 bytes / 28 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 171 / 269 |
Version history: | 5 change(s) |
Referenced in: | [show references] |