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: 232 / 336
Version history: 9 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1030563 - grabbableIntPixels_ComponentSampleModel - try to rip int[] pixels from a BufferedImage without copying [dev.]
#1033307 - grabbableIntPixels_fastOrSlow
#1033438 - grabbableGrayPixels - try to rip grayscale pixels from a TYPE_BYTE_GRAY BufferedImage without copying
#1035057 - grabbableRGBBytePixels - try to rip RGB byte[] pixels from a BufferedImage without copying