Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

41
LINES

< > BotCompany Repo | #1033438 // grabbableGrayPixels - try to rip grayscale pixels from a TYPE_BYTE_GRAY BufferedImage without copying

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (4835L/27K).

1  
scope grabbableGrayPixels.
2  
3  
import java.awt.image.DataBufferByte;
4  
5  
static Bool #succeeded;
6  
static Throwable #error;
7  
sbool #enable = true;
8  
9  
static GrabbableGrayPixels grabbableGrayPixels(BufferedImage img) {
10  
  if (img == null || !enable) null;
11  
  try {
12  
    var result = grabbableGrayPixels_impl(img);
13  
    succeeded = result != null;
14  
    ret result;
15  
  } on fail e {
16  
    error = e;
17  
    succeeded = false;
18  
  }
19  
}
20  
21  
static GrabbableGrayPixels grabbableGrayPixels_impl(BufferedImage img) {
22  
  Raster raster = img.getRaster();
23  
  SampleModel sampleModel = raster.getSampleModel();
24  
  if (!sampleModel instanceof PixelInterleavedSampleModel) null;
25  
  cast sampleModel to PixelInterleavedSampleModel;
26  
  
27  
  DataBufferByte dataBuffer = cast raster.getDataBuffer();
28  
  assertEquals(1, dataBuffer.getNumBanks());
29  
  assertEquals(DataBuffer.TYPE_BYTE, dataBuffer.getDataType());
30  
31  
  // Let's at this point assume the raster data is gray and not a palette
32  
  
33  
  int w = img.getWidth(), h = img.getHeight();
34  
  int scanlineStride = sampleModel.getScanlineStride();
35  
  byte[] pixels = dataBuffer.getData();
36  
  int offset = dataBuffer.getOffset();
37  
  int translateX = raster.getSampleModelTranslateX();
38  
  int translateY = raster.getSampleModelTranslateY();
39  
  offset += -translateX-translateY*scanlineStride;
40  
  ret GrabbableGrayPixels(pixels, w, h, offset, scanlineStride);
41  
}

Author comment

Began life as a copy of #1030544

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, ekrmjmnbrukm, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033438
Snippet name: grabbableGrayPixels - try to rip grayscale pixels from a TYPE_BYTE_GRAY BufferedImage without copying
Eternal ID of this version: #1033438/9
Text MD5: 4f97d07980c87f246d65976c2e272fa4
Transpilation MD5: 63a9b29ae64b3e1744ab882abcef435d
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-27 15:00:59
Source code size: 1381 bytes / 41 lines
Pitched / IR pitched: No / No
Views / Downloads: 213 / 277
Version history: 8 change(s)
Referenced in: [show references]