Exemple #1
0
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()
Exemple #2
0
 def test_buffers(self):
     from java.nio import ByteBuffer, ByteOrder
     if sys.version_info.major > 2:
         b = ByteBuffer.allocateDirect(16).order(ByteOrder.nativeOrder())
         v = memoryview(b)
         v[0] = 7
         self.assertEqual(v[0], b.get(0))
         s = b.asShortBuffer()
         v = memoryview(s)
         self.assertEqual(v[0], s.get(0))
         i = b.asIntBuffer()
         v = memoryview(i)
         self.assertEqual(v[0], i.get(0))
         l = b.asLongBuffer()
         v = memoryview(l)
         self.assertEqual(v[0], l.get(0))
         f = b.asFloatBuffer()
         v = memoryview(f)
         v[0] = -100
         self.assertEqual(v[0], f.get(0))
         d = b.asDoubleBuffer()
         v = memoryview(d)
         v[0] = -100
         self.assertEqual(v[0], d.get(0))
     try:
         # memoryview only supports native order so numpy is required for the other one.
         from numpy import asarray
         for order in (ByteOrder.LITTLE_ENDIAN, ByteOrder.BIG_ENDIAN):
             b = ByteBuffer.allocateDirect(16).order(order)
             v = asarray(b)
             v[0] = 7
             self.assertEqual(v[0], b.get(0))
             s = b.asShortBuffer()
             v = asarray(s)
             self.assertEqual(v[0], s.get(0))
             i = b.asIntBuffer()
             v = asarray(i)
             self.assertEqual(v[0], i.get(0))
             l = b.asLongBuffer()
             v = asarray(l)
             self.assertEqual(v[0], l.get(0))
             f = b.asFloatBuffer()
             v = asarray(f)
             v[0] = -100
             self.assertEqual(v[0], f.get(0))
             d = b.asDoubleBuffer()
             v = asarray(d)
             v[0] = -100
             self.assertEqual(v[0], d.get(0))
     except ImportError:
         pass  # not built with numpy
Exemple #3
0
 def test_buffer_no_array(self):
     """
     Directly tests StreamIO with and without a backing array and an
     InputStream that returns early.
     """
     size = 10000
     without_array = ByteBuffer.allocateDirect(size)
     self.assertFalse(without_array.hasArray())
     with_array = ByteBuffer.allocate(size)
     self.assertTrue(with_array.hasArray())
     bbs = [with_array, without_array]
     for bb in bbs:
         iis = InfiniteInputStream()
         io = StreamIO(iis, True)
         self.assertEqual(io.readinto(bb), size)
Exemple #4
0
 def test_buffer_no_array(self):
     """
     Directly tests StreamIO with and without a backing array and an
     InputStream that returns early.
     """
     size = 10000
     without_array = ByteBuffer.allocateDirect(size)
     self.assertFalse(without_array.hasArray())
     with_array = ByteBuffer.allocate(size)
     self.assertTrue(with_array.hasArray())
     bbs = [with_array, without_array]
     for bb in bbs:
         iis = InfiniteInputStream()
         io = StreamIO(iis, True)
         self.assertEqual(io.readinto(bb), size)
Exemple #5
0
    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()
Exemple #6
0
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 extractZip(zip, dest):
    "extract zip archive to dest directory"
    
    logger.info("Begin extracting:" + zip + " --> " +dest)
    mkdir_p(dest)
    zipfile = ZipFile(zip)

    entries = zipfile.entries()
    while entries.hasMoreElements():
        entry = entries.nextElement()

        if entry.isDirectory():
            mkdir_p(os.path.join(dest, entry.name))
        else:
            newFile = File(dest, entry.name)
            mkdir_p(newFile.parent)
            zis = zipfile.getInputStream(entry)
            fos = FileOutputStream(newFile)
            nread = 0
            buffer = ByteBuffer.allocate(1024)
            while True:
                nread = zis.read(buffer.array(), 0, 1024)
                if nread <= 0:
                        break
                fos.write(buffer.array(), 0, nread)

            fos.close()
            zis.close()

    logger.info("End extracting:" + str(zip) + " --> " + str(dest))
Exemple #8
0
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 extractZip(zip, dest):
    "extract zip archive to dest directory"

    logger.info("Begin extracting:" + zip + " --> " + dest)
    mkdir_p(dest)
    zipfile = ZipFile(zip)

    entries = zipfile.entries()
    while entries.hasMoreElements():
        entry = entries.nextElement()

        if entry.isDirectory():
            mkdir_p(os.path.join(dest, entry.name))
        else:
            newFile = File(dest, entry.name)
            mkdir_p(newFile.parent)
            zis = zipfile.getInputStream(entry)
            fos = FileOutputStream(newFile)
            nread = 0
            buffer = ByteBuffer.allocate(1024)
            while True:
                nread = zis.read(buffer.array(), 0, 1024)
                if nread <= 0:
                    break
                fos.write(buffer.array(), 0, nread)

            fos.close()
            zis.close()

    logger.info("End extracting:" + str(zip) + " --> " + str(dest))
Exemple #10
0
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()
Exemple #11
0
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()
Exemple #12
0
 def asBytes(ID, entry):
     b = ByteBuffer.allocate(12)  # (2 + 2 + 4 + 4)
     b.putShort(ID)  # TagId
     b.putShort(entry[0])  # DataType
     b.putInt(1)  # DataCount
     # DataOffset can contain the data when it fits in 4 bytes, but left-justified: must shift leftwards
     b.putInt(entry[2] << ((4 - entry[1]) * 8))
     return b.array()
Exemple #13
0
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 send_light_status(self):
     buffer = ByteBuffer.allocate(12)
     buffer.put(START)
     buffer.put(SET_LIGHT_STATUS)
     buffer.putLong(self.arduino.light_time)
     buffer.put(1 if self.arduino.light_status else 0)
     buffer.put(END)
     buf = buffer.array();
     self.output_stream.write(buf)
Exemple #15
0
    def testDirectNative(self):
        from java.nio import ByteBuffer, ByteOrder

        buffer = ByteBuffer.allocateDirect(48).order(ByteOrder.nativeOrder())
        self.assertIntDirect(buffer)
        self.assertIntDirect(buffer.asShortBuffer())
        self.assertIntDirect(buffer.asIntBuffer())
        self.assertIntDirect(buffer.asLongBuffer())
        self.assertFloatDirect(buffer.asFloatBuffer())
        self.assertFloatDirect(buffer.asDoubleBuffer())
Exemple #16
0
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)))
Exemple #17
0
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)))
Exemple #18
0
    def testDirectNative(self):
        from java.nio import ByteBuffer, ByteOrder

        buffer = ByteBuffer.allocateDirect(48).order(ByteOrder.nativeOrder())
        self.assertIntDirect(buffer)
        self.assertIntDirect(buffer.asShortBuffer())
        self.assertIntDirect(buffer.asIntBuffer())
        self.assertIntDirect(buffer.asLongBuffer())
        self.assertFloatDirect(buffer.asFloatBuffer())
        self.assertFloatDirect(buffer.asDoubleBuffer())
 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')
Exemple #20
0
    def testDirect(self):
        from java.nio import ByteBuffer

        buffer = ByteBuffer.allocateDirect(48)
        self.assertIntDirect(buffer)
        self.assertIntDirect(buffer.asShortBuffer())
        self.assertIntDirect(buffer.asIntBuffer())
        self.assertIntDirect(buffer.asLongBuffer())
        self.assertFloatDirect(buffer.asFloatBuffer())
        self.assertFloatDirect(buffer.asDoubleBuffer())
        with self.assertRaises(ValueError):
            self.createNdarrayFromBuffer(buffer.asCharBuffer())
Exemple #21
0
    def testDirect(self):
        from java.nio import ByteBuffer

        buffer = ByteBuffer.allocateDirect(48)
        self.assertIntDirect(buffer)
        self.assertIntDirect(buffer.asShortBuffer())
        self.assertIntDirect(buffer.asIntBuffer())
        self.assertIntDirect(buffer.asLongBuffer())
        self.assertFloatDirect(buffer.asFloatBuffer())
        self.assertFloatDirect(buffer.asDoubleBuffer())
        with self.assertRaises(ValueError):
            self.createNdarrayFromBuffer(buffer.asCharBuffer())
Exemple #22
0
def _create_byte_buffer(data):
    """
    Converts data into a ByteBuffer. This is useful for interfacing with the Gb
    class.
    """
    buf = ByteBuffer.allocateDirect(len(data))
    if isinstance(data[0], int):
        for byte in data:
            buf.put(byte)
    else:
        for byte in data:
            buf.put(ord(byte))
    return buf
Exemple #23
0
class Grid(object):
    """ generated source for class Grid """
    buffer_ = ByteBuffer()
    dataType = DataType()
    origin = []
    shape = []

    def __init__(self, buffer_, dataType, origin, shape):
        """ generated source for method __init__ """
        self.buffer_ = buffer_
        self.dataType = dataType
        self.origin = origin
        self.shape = shape
        size = dataType.getSize()
        i = 0
        while i < shape.length:
            size *= shape[i]
            i += 1
        if buffer_.remaining() != size:
            raise IllegalArgumentException("data length and shape mismatch")
        if origin.length != shape.length:
            raise IllegalArgumentException(
                "the length of origin and shape mismatch")

    def getDataSize(self):
        """ generated source for method getDataSize """
        return self.buffer_.remaining()

    def getDataAsByteArray(self):
        """ generated source for method getDataAsByteArray """
        data = [None] * getDataSize()
        self.buffer_.duplicate().get(data)
        return data

    def getOrigin(self):
        """ generated source for method getOrigin """
        return self.origin

    def getShape(self):
        """ generated source for method getShape """
        return self.shape

    def getDataType(self):
        """ generated source for method getDataType """
        return self.dataType

    def toArray(self):
        """ generated source for method toArray """
        array = Array.factory(self.dataType, self.shape,
                              self.buffer_.duplicate())
        return array
Exemple #24
0
    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())
Exemple #27
0
    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()
Exemple #28
0
    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 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)
Exemple #30
0
    def str2num(self, size, f):

        i = 0
        n = 0

        buf = ByteBuffer.allocate(4)
        buf.order(ByteOrder.LITTLE_ENDIAN)

        read = 0
        while read < size:
            r = f.read(buf.array(), read, size - read)
            if r == -1:
                raise IOException()
            read += r

        while read < 4:
            buf.put(read, byte(0))
            read += 1

        return buf.getInt()
Exemple #31
0
    def testPassingDirect(self):
        """
        Test that when a numpy ndarray created from a java direct buffer is
        passed to java that it still uses the exact same direct buffer. The
        simplest way to test that is to copy it back to python and ensure
        modifications are visible in both numpy ndarrays
        """
        from java.nio import ByteBuffer

        buffer = ByteBuffer.allocateDirect(48)
        ndarray = self.createNdarrayFromBuffer(buffer)

        from java.util import ArrayList
        a = ArrayList()
        a.add(ndarray)
        ndarray2 = a.get(0)
        ndarray[0] = 1
        self.assertEquals(1, ndarray2[0])
        ndarray2[0] = 2
        self.assertEquals(2, ndarray[0])
Exemple #32
0
    def encode(self, input, errors='strict'):
        error_function = codecs.lookup_error(errors)
        # workaround non-BMP issues - need to get the exact count of chars, not codepoints
        input_buffer = CharBuffer.allocate(StringBuilder(input).length())
        input_buffer.put(input)
        input_buffer.rewind()
        encoder = Charset.forName(self.encoding).newEncoder()
        output_buffer = ByteBuffer.allocate(min(max(len(input) * 2, 256), 1024))
        builder = StringIO()

        while True:
            result = encoder.encode(input_buffer, output_buffer, True)
            pos = output_buffer.position()
            output_buffer.rewind()
            builder.write(output_buffer.array()[0:pos].tostring())
            if result.isUnderflow():
                break
            _process_encode_errors(self.encoding, input, result, error_function, input_buffer, builder)

        return builder.getvalue(), len(input)
Exemple #33
0
    def testPassingDirect(self):
        """
        Test that when a numpy ndarray created from a java direct buffer is
        passed to java that it still uses the exact same direct buffer. The
        simplest way to test that is to copy it back to python and ensure
        modifications are visible in both numpy ndarrays
        """
        from java.nio import ByteBuffer

        buffer = ByteBuffer.allocateDirect(48)
        ndarray = self.createNdarrayFromBuffer(buffer)

        from java.util import ArrayList
        a = ArrayList()
        a.add(ndarray)
        ndarray2 = a.get(0)
        ndarray[0] = 1
        self.assertEquals(1, ndarray2[0])
        ndarray2[0] = 2
        self.assertEquals(2, ndarray[0])
Exemple #34
0
    def encode(self, input, errors='strict'):
        error_function = codecs.lookup_error(errors)
        # workaround non-BMP issues - need to get the exact count of chars, not codepoints
        input_buffer = CharBuffer.allocate(StringBuilder(input).length())
        input_buffer.put(input)
        input_buffer.rewind()
        encoder = Charset.forName(self.encoding).newEncoder()
        output_buffer = ByteBuffer.allocate(min(max(len(input) * 2, 256), 1024))
        builder = StringIO()

        while True:
            result = encoder.encode(input_buffer, output_buffer, True)
            pos = output_buffer.position()
            output_buffer.rewind()
            builder.write(output_buffer.array()[0:pos].tostring())
            if result.isUnderflow():
                break
            _process_encode_errors(self.encoding, input, result, error_function, input_buffer, builder)

        return builder.getvalue(), len(input)
Exemple #35
0
 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]))
Exemple #36
0
    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()
Exemple #37
0
    def testDirectArgReturn(self):
        """
        Tests making a python ndarray from a java DirectNDArray.
        It should be possible to pass to java and back and still
        reference the same memory. It should also be possible to
        pass it to java methods that expect an NDArray or a primitive
        array in which case the memory is copied.
        """
        from java.nio import ByteBuffer, ByteOrder

        buffer = ByteBuffer.allocateDirect(48)
        ndarray = self.createNdarrayFromBuffer(buffer.asIntBuffer())
        ndarray2 = self.test.testDirectArgAndReturn(ndarray)
        ndarray[0] = 1
        self.assertEquals(1, ndarray2[0])
        ndarray2[0] = 2
        self.assertEquals(2, ndarray[0])

        ndarray2 = self.test.testArgAndReturn(ndarray)
        self.assertEquals(ndarray[0] + 5, ndarray2[0])

        self.assertTrue(self.test.callIntMethod(ndarray))
Exemple #38
0
    def testDirectArgReturn(self):
        """
        Tests making a python ndarray from a java DirectNDArray.
        It should be possible to pass to java and back and still
        reference the same memory. It should also be possible to
        pass it to java methods that expect an NDArray or a primitive
        array in which case the memory is copied.
        """
        from java.nio import ByteBuffer, ByteOrder

        buffer = ByteBuffer.allocateDirect(48)
        ndarray = self.createNdarrayFromBuffer(buffer.asIntBuffer())
        ndarray2 = self.test.testDirectArgAndReturn(ndarray)
        ndarray[0] = 1
        self.assertEquals(1, ndarray2[0])
        ndarray2[0] = 2
        self.assertEquals(2, ndarray[0])

        ndarray2 = self.test.testArgAndReturn(ndarray)
        self.assertEquals(ndarray[0] + 5, ndarray2[0])

        self.assertTrue(self.test.callIntMethod(ndarray))
 def process_command(self, command):
     if command == START and self.count <= 0:
         self.buffer = []
         self.first = True
     elif command == END and self.count <= 0:
         print self.buffer
         buffer = ByteBuffer.allocate(len(self.buffer))
         for b in self.buffer:
             buffer.put(b)
         buffer.flip()
         cmd = buffer.get()
         if cmd == REQUEST_LIGHT_STATUS:
             self.send_light_status()
             pass
         elif cmd == SET_LIGHT_STATUS:
             time = buffer.getLong()
             status = buffer.get()
             print status    
             self.arduino.switch_arduino_lights(status, time)
         elif cmd == SET_ROUGH:
             time = buffer.getLong()
             self.arduino.send_rough_data(time)
         elif cmd == SET_REALLY_ROUGH:
             time = buffer.getLong()
             self.arduino.send_really_rough_data(time)
     else:
         self.buffer.append(command)
         if self.first:
             if command == SET_LIGHT_STATUS:
                 self.count = 10
             elif command == REQUEST_LIGHT_STATUS:
                 self.count = 1
             elif command == SET_ROUGH:
                 self.count = 9
             elif command == SET_REALLY_ROUGH:
                 self.count = 9
             self.first = False
     self.count -= 1
Exemple #40
0
def iconv(file):
    print 'Converting', file
    f = File(file)
    if not f.exists():
        print file, 'does not exist'
        sys.exit(1)
    buffer = ByteBuffer.allocate(f.length() * 2)
    input = FileInputStream(f)
    input.getChannel().read(buffer)
    buffer.limit(buffer.position())
    buffer.position(0)
    if buffer.limit() != f.length():
        print file, 'could not be read completely'
        sys.exit(1)
    input.close()
    buffer = encoder.encode(decoder.decode(buffer))
    buffer.position(0)
    output = FileOutputStream(file + '.cnv')
    if output.getChannel().write(buffer) != buffer.limit():
        print file, 'could not be reencoded'
        sys.exit(1)
    output.close()
    f.delete()
    File(file + '.cnv').renameTo(f)
Exemple #41
0
def iconv(file):
	print 'Converting', file
	f = File(file)
	if not f.exists():
		print file, 'does not exist'
		sys.exit(1)
	buffer = ByteBuffer.allocate(f.length() * 2)
	input = FileInputStream(f)
	input.getChannel().read(buffer)
	buffer.limit(buffer.position())
	buffer.position(0)
	if buffer.limit() != f.length():
		print file, 'could not be read completely'
		sys.exit(1)
	input.close()
	buffer = encoder.encode(decoder.decode(buffer))
	buffer.position(0)
	output = FileOutputStream(file + '.cnv')
	if output.getChannel().write(buffer) != buffer.limit():
		print file, 'could not be reencoded'
		sys.exit(1)
	output.close()
	f.delete()
	File(file + '.cnv').renameTo(f)
Exemple #42
0
def pn_data_decode(data, encoded):
  return data.decode(ByteBuffer.wrap(array(encoded, 'b')))
Exemple #43
0
 def __init__(self, errors='strict', encoding=None):
     assert encoding
     self.encoding = encoding
     self.errors = errors
     self.encoder = Charset.forName(self.encoding).newEncoder()
     self.output_buffer = ByteBuffer.allocate(1024)
Exemple #44
0
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))
Exemple #46
0
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
Exemple #47
0
def datetime(val):
    milliseconds = long(float(val) * 1e3)
    return ByteBuffer.allocate(8).putLong(0, milliseconds)
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()
Exemple #49
0
 def toDouble(byteArray):
     return ByteBuffer.wrap(byteArray).getDouble()
Exemple #50
0
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