def read2DImageROI(path, dimensions, interval, pixelType=UnsignedShortType, header=0, byte_order=ByteOrder.LITTLE_ENDIAN): """ Read a region of interest (the interval) of an image in a file. Assumes the image is written with the first dimension moving slowest. path: the file path to the image file. dimensions: a sequence of integer values e.g. [512, 512, 512] interval: two sequences of integer values defining the min and max coordinates, e.g. [[20, 0], [400, 550]] pixeltype: e.g. UnsignedShortType, FloatType header: defaults to zero, the number of bytes between the start of the file and the start of the image data. Supports only these types: UnsignedByteType, UnsignedShortType, FloatType. Returns an ArrayImg of the given type. """ ra = RandomAccessFile(path, 'r') try: width, height = dimensions minX, minY = interval[0] maxX, maxY = interval[1] roi_width, roi_height = maxX - minX + 1, maxY - minY + 1 tailX = width - roi_width - minX #print minX, minY #print maxX, maxY #print roi_width, roi_height size = roi_width * roi_height n_bytes_per_pixel = pixelType().getBitsPerPixel() / 8 #print n_bytes_per_pixel bytes = zeros(size * n_bytes_per_pixel, 'b') # Read only the 2D ROI ra.seek(header + (minY * width + minX) * n_bytes_per_pixel) for h in xrange(roi_height): ra.readFully(bytes, h * roi_width * n_bytes_per_pixel, roi_width * n_bytes_per_pixel) ra.skipBytes((tailX + minX) * n_bytes_per_pixel) # Make an image roiDims = [roi_width, roi_height] if UnsignedByteType == pixelType: return ArrayImgs.unsignedBytes(bytes, roiDims) if UnsignedShortType == pixelType: shorts = zeros(size, 'h') ByteBuffer.wrap(bytes).order(byte_order).asShortBuffer().get(shorts) return ArrayImgs.shorts(shorts, roiDims) if FloatType == pixelType: floats = zeros(size, 'f') ByteBuffer.wrap(bytes).order(byte_order).asFloatBuffer().get(floats) return ArrayImgs.floats(floats, roiDims) finally: ra.close()
def pn_data_get_uuid(data): u = data.getUUID() ba = zeros(16, 'b') bb = ByteBuffer.wrap(ba) bb.putLong(u.getMostSignificantBits()) bb.putLong(u.getLeastSignificantBits()) return ba.tostring()
def decode(self, input, final=False): error_function = codecs.lookup_error(self.errors) input_array = array('b', self.buffer + str(input)) input_buffer = ByteBuffer.wrap(input_array) builder = StringBuilder( int(self.decoder.averageCharsPerByte() * len(input))) self.output_buffer.rewind() while True: result = self.decoder.decode(input_buffer, self.output_buffer, final) pos = self.output_buffer.position() self.output_buffer.rewind() builder.append(self.output_buffer.subSequence(0, pos)) if result.isUnderflow(): if not final: # Keep around any remaining input for next call to decode self.buffer = input_array[input_buffer.position( ):input_buffer.limit()].tostring() else: _process_incomplete_decode(self.encoding, input, error_function, input_buffer, builder) break _process_decode_errors(self.encoding, input, result, error_function, input_buffer, builder) return builder.toString()
def readFloats(path, dimensions, header=0, byte_order=ByteOrder.LITTLE_ENDIAN): """ Read a file as an ArrayImg of FloatType """ size = reduce(operator.mul, dimensions) ra = RandomAccessFile(path, 'r') try: if header < 0: # Interpret from the end: useful for files with variable header lengths # such as some types of uncompressed TIFF formats header = ra.length() + header ra.skipBytes(header) bytes = zeros(size * 4, 'b') ra.read(bytes) floats = zeros(size, 'f') ByteBuffer.wrap(bytes).order(byte_order).asFloatBuffer().get(floats) return ArrayImgs.floats(floats, dimensions) finally: ra.close()
def readUnsignedShorts(path, dimensions, header=0, return_array=False, byte_order=ByteOrder.LITTLE_ENDIAN): """ Read a file as an ArrayImg of UnsignedShortType """ size = reduce(operator.mul, dimensions) ra = RandomAccessFile(path, 'r') try: if header < 0: # Interpret from the end: useful for files with variable header lengths # such as some types of uncompressed TIFF formats header = ra.length() + header ra.skipBytes(header) bytes = zeros(size * 2, 'b') ra.read(bytes) shorts = zeros(size, 'h') # h is for short ByteBuffer.wrap(bytes).order(byte_order).asShortBuffer().get(shorts) return shorts if return_array else ArrayImgs.unsignedShorts(shorts, dimensions) finally: ra.close()
def readFIBSEMdat(path, channel_index=-1, header=1024, magic_number=3555587570, asImagePlus=False, toUnsigned=True): """ Read a file from Shan Xu's FIBSEM software, where two or more channels are interleaved. Assumes channels are stored in 16-bit. path: the file path to the .dat file. channel_index: the 0-based index of the channel to parse, or -1 (default) for all. header: defaults to a length of 1024 bytes magic_number: defaults to that for version 8 of Shan Xu's .dat image file format. isSigned: defaults to True, will subtract the min value when negative. asImagePlus: return a list of ImagePlus instead of ArrayImg which is the default. """ ra = RandomAccessFile(path, 'r') try: # Check the magic number ra.seek(0) magic = ra.readInt() & 0xffffffff if magic != magic_number: msg = "magic number mismatch: v8 magic " + str(magic_number) + " != " + str(magic) + " for path:\n" + path System.out.println(msg) print msg # Continue: attempt to parse the file anyway # Read the number of channels ra.seek(32) numChannels = ra.readByte() & 0xff # a single byte as unsigned integer # Parse width and height ra.seek(100) width = ra.readInt() ra.seek(104) height = ra.readInt() # Read the whole interleaved pixel array ra.seek(header) bytes = zeros(width * height * 2 * numChannels, 'b') # 2 for 16-bit ra.read(bytes) # Parse as 16-bit array sb = ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).asShortBuffer() bytes = None finally: ra.close() # shorts = zeros(width * height * numChannels, 'h') sb.get(shorts) sb = None # Deinterleave channels and convert to unsigned short # Shockingly, these values are signed shorts, not unsigned! (for first popeye2 squid volume, December 2021) # With ASM: fast channels = DAT_handler.deinterleave(shorts, numChannels, channel_index) shorts = None # if toUnsigned: for s in channels: DAT_handler.toUnsigned(s) # With python array sampling: very slow, and not just from iterating whole array once per channel #seq = xrange(numChannels) if -1 == channel_index else [channel_index] #channels = [shorts[i::numChannels] for i in seq] if asImagePlus: return [ImagePlus(str(i), ShortProcessor(width, height, s, None)) for i, s in enumerate(channels)] else: return [ArrayImgs.unsignedShorts(s, [width, height]) for s in channels]
def write(fd, string): """write(fd, string) -> byteswritten Write a string to a file descriptor. """ from java.nio import ByteBuffer from org.python.core.util import StringUtil rawio = FileDescriptors.get(fd) return _handle_oserror(rawio.write, ByteBuffer.wrap(StringUtil.toBytes(string)))
def write_chunk(self, chunk: bytes) -> None: assert self.handle is not None chunks = binascii.unhexlify(bytes(chunk).hex()) if len(chunk) != 64: raise TransportException("Unexpected chunk size: %d" % len(chunk)) request = UsbRequest() request.initialize(self.handle, self.endpoint_out) success = request.queue(ByteBuffer.wrap(chunks)) if success: self.handle.requestWait() else: raise TransportException('android_usb send failed')
def getSpriteBitmap(spritefile, name): sprite = spritefile.getSprite(name) bitmap = Bitmap.createBitmap(sprite.width, sprite.height, Bitmap.Config.ARGB_8888) bitmap.copyPixelsFromBuffer(ByteBuffer.wrap(sprite.rgba)) if sprite.ydpi < sprite.xdpi: yscale = sprite.xdpi / sprite.ydpi bitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight() * yscale, False) return bitmap
def readFIBSEMdat(path, channel_index=-1, header=1024, magic_number=3555587570): """ Read a file from Shan Xu's FIBSEM software, where two channels are interleaved. Assumes channels are stored in 16-bit. path: the file path to the .dat file. channel_index: the 0-based index of the channel to parse, or -1 (default) for all. header: defaults to a length of 1024 bytes magic_number: defaults to that for version 8 of Shan Xu's .dat image file format. """ ra = RandomAccessFile(path, 'r') try: # Check the magic number ra.seek(0) if ra.readInt() & 0xffffffff != magic_number: print "Magic number mismatch" return None # Read the number of channels ra.seek(32) numChannels = ra.readByte() & 0xff # a single byte as unsigned integer # Parse width and height ra.seek(100) width = ra.readInt() ra.seek(104) height = ra.readInt() print numChannels, width, height # Read the whole interleaved pixel array ra.seek(header) bytes = zeros(width * height * 2 * numChannels, 'b') # 2 for 16-bit ra.read(bytes) print "read", len(bytes), "bytes" # takes ~2 seconds # Parse as 16-bit array sb = ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).asShortBuffer() shorts = zeros(width * height * numChannels, 'h') sb.get(shorts) # Deinterleave channels # With Weaver: fast channels = w.deinterleave(shorts, numChannels, channel_index) # With python array sampling: very slow, and not just from iterating whole array once per channel # seq = xrange(numChannels) if -1 == channel_index else [channel_index] #channels = [shorts[i::numChannels] for i in seq] # With clojure: extremely slow, may be using reflection unexpectedly #channels = deinterleave.invoke(shorts, numChannels) print len(channels) # Shockingly, these values are signed shorts, not unsigned! return [ArrayImgs.shorts(s, [width, height]) for s in channels] finally: ra.close()
def buildGrid2DFromBlocks(cls, plane, dataType, blocks, buffer_, pos): """ generated source for method buildGrid2DFromBlocks """ size = plane.getxRange().getSize() * plane.getyRange().getSize() * dataType.getSize() if buffer_.length - pos < size: raise IllegalArgumentException("buffer not enough") count = 0 for block in blocks: while x < Math.min(blockPlane.getxRange().getEnd(), plane.getxRange().getEnd()): while y < Math.min(blockPlane.getyRange().getEnd(), plane.getyRange().getEnd()): System.arraycopy(block.getDataAsByteArray(), posInBlock, buffer_, pos + posInData, dataType.getSize()) count += dataType.getSize() y += 1 x += 1 if count != size: raise RuntimeException("the blocks does not contain enough data") byteBuffer = ByteBuffer.wrap(buffer_, pos, size) return Grid2D(byteBuffer, dataType, plane.getOrigin(), plane.getShape())
def createAccess(bytes, bytesPerPixel): """ Return a new volatile access instance for the appropriate pixel type. Supports byte, short, float and long. """ if 1 == bytesPerPixel: # BYTE return VolatileByteArray(bytes, True) # Transform bytes into another type bb = ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN) if 2 == bytesPerPixel: # SHORT pixels = zeros(len(bytes) / 2, 's') bb.asShortBuffer().get(pixels) return VolatileShortArray(pixels, True) if 4 == bytesPerPixel: # FLOAT pixels = zeros(len(bytes) / 4, 'f') bb.asFloatBuffer().get(pixels) return VolatileFloatArray(pixels, True) if 8 == bytesPerPixel: # LONG pixels = zeros(len(bytes) / 8, 'l') bb.asLongBuffer().get(pixels) return VolatileLongArray(pixels, True)
def decode(self, input, errors='strict', final=True): error_function = codecs.lookup_error(errors) input_buffer = ByteBuffer.wrap(array('b', input)) decoder = Charset.forName(self.encoding).newDecoder() output_buffer = CharBuffer.allocate(min(max(int(len(input) / 2), 256), 1024)) builder = StringBuilder(int(decoder.averageCharsPerByte() * len(input))) while True: result = decoder.decode(input_buffer, output_buffer, False) pos = output_buffer.position() output_buffer.rewind() builder.append(output_buffer.subSequence(0, pos)) if result.isUnderflow(): if final: _process_incomplete_decode(self.encoding, input, error_function, input_buffer, builder) break _process_decode_errors(self.encoding, input, result, error_function, input_buffer, builder) return builder.toString(), input_buffer.position()
def test_byte_array(self): from java.nio import ByteBuffer import array l = [1, 2, 3, 4] bb = ByteBuffer.wrap(bytearray(l)) self.assertSequenceEqual(bb.array(), l) if sys.version_info.major > 2: bb = ByteBuffer.wrap(bytes(l)) self.assertSequenceEqual(bb.array(), l) bb = ByteBuffer.wrap(array.array('b', l)) self.assertSequenceEqual(bb.array(), l) bb = ByteBuffer.wrap(array.array('B', l)) self.assertSequenceEqual(bb.array(), l) v = memoryview(array.array('B', l)) v = v[::2] bb = ByteBuffer.wrap(v) self.assertSequenceEqual(bb.array(), v) with self.assertRaises(TypeError): ByteBuffer.wrap(array.array('f', [1, 2, 3, 4]))
def decode(self, input, final=False): error_function = codecs.lookup_error(self.errors) input_array = array('b', self.buffer + str(input)) input_buffer = ByteBuffer.wrap(input_array) builder = StringBuilder(int(self.decoder.averageCharsPerByte() * len(input))) self.output_buffer.rewind() while True: result = self.decoder.decode(input_buffer, self.output_buffer, final) pos = self.output_buffer.position() self.output_buffer.rewind() builder.append(self.output_buffer.subSequence(0, pos)) if result.isUnderflow(): if not final: # Keep around any remaining input for next call to decode self.buffer = input_array[input_buffer.position():input_buffer.limit()].tostring() else: _process_incomplete_decode(self.encoding, input, error_function, input_buffer, builder) break _process_decode_errors(self.encoding, input, result, error_function, input_buffer, builder) return builder.toString()
def pn_data_decode(data, encoded): return data.decode(ByteBuffer.wrap(array(encoded, 'b')))
def decode(self, encoded): return self._data.decode(ByteBuffer.wrap(encoded))
def pn_data_put_uuid(data, u): bb = ByteBuffer.wrap(array(u, 'b')) first = bb.getLong() second = bb.getLong() data.putUUID(JUUID(first, second)) return 0
def read_TIFF_plane(ra, tags, handler=None): """ ra: RandomAccessFile tags: dicctionary of TIFF tags for the IFD of the plane to parse. handler: defaults to Nobe; a function that reads the image data and returns an array. For compressed image planes, takes advantage of the ij.io.ImageReader class which contains methods that ought to be static (no side effects) and therefore demand a dummy constructor just to invoke them. """ if handler: return handler(ra, tags) # Values of the "compressed" tag field (when present): # 1: "uncompressed", # 32773: "packbits", # 5: "LZW", # 6: "JPEG", width, height = tags["width"], tags["height"] bitDepth = tags["bitDepth"] n_bytes = int(ceil(width * height * (float(bitDepth) / 8))) # supports bitDepth < 8 compression = tags.get("compression", 1) bytes = None if 32773 == compression: bytes = unpackBits(ra, tags, use_imagereader=True) elif 5 == compression: bytes = ImageReader(FileInfo()).lzwUncompress( getIFDImageBytes(ra, tags), tags["width"] * tags["height"]) elif 32946 == compression or 8 == compression: bytes = ImageReader(FileInfo()).zipUncompress( getIFDImageBytes(ra, tags), tags["width"] * tags["height"]) elif 1 == compression: bytes = zeros(n_bytes, 'b') index = 0 for strip_offset, strip_length in zip(tags["StripOffsets"], tags["StripByteCounts"]): ra.seek(strip_offset) ra.read(bytes, index, strip_length) elif 6 == compression: print "Unsupported compression: JPEG" raise Exception("Can't handle JPEG compression of TIFF image planes") else: raise Exception("Can't deal with compression type " + str(compression)) # If read as bytes, parse to the appropriate primitive type if 8 == bitDepth: return bytes bb = ByteBuffer.wrap(bytes) if not tags["bigEndian"]: bb = bb.order(ByteOrder.BIG_ENDIAN) if 16 == bitDepth: pixels = zeros(width * height, 'h') bb.asShortBuffer().get(pixels) return pixels if 32 == bitDepth: pixels = zeros(width * height, 'f') bb.asFloatBuffer().get(pixels) return pixels if bitDepth < 8: # Support for 1-bit, 2-bit, 3-bit, ... 12-bit, etc. images pixels = zeros(int(ceil(width * height * bitDepth / 64.0)), 'l') bb.asLongBuffer().get(pixels) # Reverse bits from left to right to right to left for ImgLib2 LongArray pixels = array(imap(Long.reverse, pixels), 'l') return pixels
def toDouble(byteArray): return ByteBuffer.wrap(byteArray).getDouble()
try: ra = RandomAccessFile(filepath, 'r') ra.skipBytes(headerSize + slice_offset) stack = ImageStack(width, height) slice_n_bytes = width * height * (bitDepth / 8) # ASSUMES images aren't RGB or ARGB image_type = { 8: ('b', None, ByteProcessor), 16: ('h', "asShortBuffer", ShortProcessor), 32: ('f', "asFloatBuffer", FloatProcessor) } pixel_type, convertMethod, constructor = image_type[bitDepth] for i in xrange(num_slices): bytes = zeros(slice_n_bytes, 'b') # an empty byte[] array ra.read(bytes) bb = ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN) if convertMethod: # if not 8-bit pixels = zeros(width * height, pixel_type) # an empty short[] or float[] array getattr(bb, convertMethod).get( pixels) # e.g. bb.asShortBuffer().get(pixels) else: pixels = bytes stack.addSlice(constructor(width, height, pixels, None)) ra.skipBytes(slice_n_bytes) # imp = ImagePlus( os.path.split(filepath)[1] + " from slice %i" % slice_index, stack) imp.show() finally: ra.close()