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

37
LINES

< > BotCompany Repo | #1030544 // grabbableIntPixels - try to rip RGB int[] pixels from a BufferedImage without copying

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

Libraryless. Click here for Pure Java version (3316L/19K).

scope grabbableIntPixels.

static Bool #succeeded;
sbool #enable = true;

static GrabbableIntPixels grabbableIntPixels(BufferedImage img) {
  if (img == null || !enable) null;
  try {
    var result = grabbableIntPixels_impl(img);
    succeeded = result != null;
    ret result;
  } on fail {
    succeeded = false;
  }
}

static GrabbableIntPixels grabbableIntPixels_impl(BufferedImage img) {
  Raster raster = img.getRaster();
  SampleModel _sampleModel = raster.getSampleModel();
  if (!_sampleModel instanceof SinglePixelPackedSampleModel) null;
  SinglePixelPackedSampleModel sampleModel = cast _sampleModel;
  DataBufferInt dataBuffer = cast raster.getDataBuffer();
  assertEquals(1, dataBuffer.getNumBanks());
  assertEquals(DataBuffer.TYPE_INT, dataBuffer.getDataType());

  // Let's at this point assume the raster data is what we
  // think it is... (TODO: test on unusual platforms)
  
  int w = img.getWidth(), h = img.getHeight();
  int scanlineStride = sampleModel.getScanlineStride();
  int[] pixels = dataBuffer.getData();
  int offset = dataBuffer.getOffset();
  int translateX = raster.getSampleModelTranslateX();
  int translateY = raster.getSampleModelTranslateY();
  offset += -translateX-translateY*scanlineStride;
  ret GrabbableIntPixels(pixels, w, h, offset, scanlineStride);
}

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: 231 / 335
Version history: 9 change(s)
Referenced in: [show references]