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).

scope grabbableGrayPixels.

import java.awt.image.DataBufferByte;

static Bool #succeeded;
static Throwable #error;
sbool #enable = true;

static GrabbableGrayPixels grabbableGrayPixels(BufferedImage img) {
  if (img == null || !enable) null;
  try {
    var result = grabbableGrayPixels_impl(img);
    succeeded = result != null;
    ret result;
  } on fail e {
    error = e;
    succeeded = false;
  }
}

static GrabbableGrayPixels grabbableGrayPixels_impl(BufferedImage img) {
  Raster raster = img.getRaster();
  SampleModel sampleModel = raster.getSampleModel();
  if (!sampleModel instanceof PixelInterleavedSampleModel) null;
  cast sampleModel to PixelInterleavedSampleModel;
  
  DataBufferByte dataBuffer = cast raster.getDataBuffer();
  assertEquals(1, dataBuffer.getNumBanks());
  assertEquals(DataBuffer.TYPE_BYTE, dataBuffer.getDataType());

  // Let's at this point assume the raster data is gray and not a palette
  
  int w = img.getWidth(), h = img.getHeight();
  int scanlineStride = sampleModel.getScanlineStride();
  byte[] pixels = dataBuffer.getData();
  int offset = dataBuffer.getOffset();
  int translateX = raster.getSampleModelTranslateX();
  int translateY = raster.getSampleModelTranslateY();
  offset += -translateX-translateY*scanlineStride;
  ret GrabbableGrayPixels(pixels, w, h, offset, scanlineStride);
}

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: 212 / 276
Version history: 8 change(s)
Referenced in: [show references]