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); }