Libraryless. Click here for Pure Java version (3316L/19K).
1 | scope grabbableIntPixels. |
2 | |
3 | static Bool #succeeded; |
4 | sbool #enable = true; |
5 | |
6 | static GrabbableIntPixels grabbableIntPixels(BufferedImage img) { |
7 | if (img == null || !enable) null; |
8 | try { |
9 | var result = grabbableIntPixels_impl(img); |
10 | succeeded = result != null; |
11 | ret result; |
12 | } on fail { |
13 | succeeded = false; |
14 | } |
15 | } |
16 | |
17 | static GrabbableIntPixels grabbableIntPixels_impl(BufferedImage img) { |
18 | Raster raster = img.getRaster(); |
19 | SampleModel _sampleModel = raster.getSampleModel(); |
20 | if (!_sampleModel instanceof SinglePixelPackedSampleModel) null; |
21 | SinglePixelPackedSampleModel sampleModel = cast _sampleModel; |
22 | DataBufferInt dataBuffer = cast raster.getDataBuffer(); |
23 | assertEquals(1, dataBuffer.getNumBanks()); |
24 | assertEquals(DataBuffer.TYPE_INT, dataBuffer.getDataType()); |
25 | |
26 | // Let's at this point assume the raster data is what we |
27 | // think it is... (TODO: test on unusual platforms) |
28 | |
29 | int w = img.getWidth(), h = img.getHeight(); |
30 | int scanlineStride = sampleModel.getScanlineStride(); |
31 | int[] pixels = dataBuffer.getData(); |
32 | int offset = dataBuffer.getOffset(); |
33 | int translateX = raster.getSampleModelTranslateX(); |
34 | int translateY = raster.getSampleModelTranslateY(); |
35 | offset += -translateX-translateY*scanlineStride; |
36 | ret GrabbableIntPixels(pixels, w, h, offset, scanlineStride); |
37 | } |
download show line numbers debug dex old transpilations
Travelled to 5 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt
No comments. add comment
Snippet ID: | #1030544 |
Snippet name: | grabbableIntPixels - try to rip RGB int[] pixels from a BufferedImage without copying |
Eternal ID of this version: | #1030544/10 |
Text MD5: | 6403ba4eb59084aa4951892c72e191e9 |
Transpilation MD5: | 019c1c5816b3725b701cdbe448f0eebd |
Author: | stefan |
Category: | javax / imaging |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-09-08 02:26:56 |
Source code size: | 1338 bytes / 37 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 233 / 339 |
Version history: | 9 change(s) |
Referenced in: | [show references] |