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

28
LINES

< > BotCompany Repo | #1035057 // grabbableRGBBytePixels - try to rip RGB byte[] pixels from a BufferedImage without copying

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

Libraryless. Click here for Pure Java version (9300L/52K).

static GrabbableRGBBytePixels grabbableRGBBytePixels(BufferedImage img) {
  Raster raster = img.getRaster();
  SampleModel _sampleModel = raster.getSampleModel();
  if (!_sampleModel instanceof PixelInterleavedSampleModel) null;
  
  // Check if we need a conversion to RGB (e.g. after unpacking a JPEG)
  var imageModel = img.getColorModel();
  if (imageModel != ColorModel.getRGBdefault()) null;

  PixelInterleavedSampleModel sampleModel = cast _sampleModel;
  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 what we
  // think it is... (TODO: test on unusual platforms)
  
  int w = img.getWidth(), h = img.getHeight();
  int pixelStride = sampleModel.getPixelStride();
  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 GrabbableRGBBytePixels(pixels, w, h, offset, scanlineStride, pixelStride);
}

Author comment

Began life as a copy of #1030544

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1035057
Snippet name: grabbableRGBBytePixels - try to rip RGB byte[] pixels from a BufferedImage without copying
Eternal ID of this version: #1035057/6
Text MD5: 1549ad8d34efaafd0cf2fafc99305b7a
Transpilation MD5: 8802bf1f4456f1d6aeea02283dc0038d
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-08-08 19:08:32
Source code size: 1250 bytes / 28 lines
Pitched / IR pitched: No / No
Views / Downloads: 170 / 268
Version history: 5 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)