Exemplo n.º 1
0
def read_3D_java(sizeF1, sizeF2, sizeF3, filename="ser"):
    """
    Reads in a Bruker 3D fid
    
    sizeF1 x sizeF2 is the number of fid
    sizeF3 is the number of data-points in the fid
    uses java low level file access
    """
    from java.io import RandomAccessFile
    #    from nmrtec.util.IO import SwapByte
    import jarray

    # read binary
    dim(3)
    chsize(int(sizeF1), int(sizeF2), int(sizeF3))
    f = RandomAccessFile(filename, "r")
    for i1 in range(sizeF1):
        print(i1, end=' ')
        for i2 in range(sizeF2):
            for i3 in range(sizeF3):
                x = f.readInt()
                #               x = SwapByte.swap(x)       # uncoment if swapbyte is required
                setval(i1 + 1, i2 + 1, i3 + 1, float(x))  # copy to 3D buffer
            if ((sizeF3 % 256) != 0
                ):  # pb with Bruker files, where size is always a 256 multiple
                for i3 in range(((sizeF3 / 256) + 1) * 256 - sizeF3):
                    x = f.readInt()
Exemplo n.º 2
0
def parse_TIFF_IFDs(filepath):
    """ Returns a generator of dictionaries of tags for each IFD in the TIFF file,
      as defined by the 'parseIFD' function above. """
    ra = RandomAccessFile(filepath, 'r')
    try:
        # TIFF file format can have metadata at the end after the images, so the above approach can fail
        # TIFF file header is 8-bytes long:
        # (See: http://paulbourke.net/dataformats/tiff/tiff_summary.pdf )
        #
        # Bytes 1 and 2: identifier. Either the value 4949h (II) or 4D4Dh (MM),
        #                            meaning little-endian and big-endian, respectively.
        # All data encountered past the first two bytes in the file obey
        # the byte-ordering scheme indicated by the identifier field.
        b1, b2 = ra.read(), ra.read()  # as two java int, each one byte sized
        bigEndian = chr(b1) == 'M'
        parseNextInt = parseNextIntBigEndian if bigEndian else parseNextIntLittleEndian
        # Bytes 3 and 4: Version: Always 42
        ra.skipBytes(2)
        # Bytes 5,6,7,8: IFDOffset: offset to first image file directory (IFD), the metadata entry for the first image.
        nextIFDoffset = parseNextInt(ra, 4)  # offset to first IFD
        while nextIFDoffset != 0:
            ra.seek(nextIFDoffset)
            tags, nextIFDoffset = parseIFD(ra, parseNextInt)
            tags["bigEndian"] = bigEndian
            yield tags
    finally:
        ra.close()
Exemplo n.º 3
0
 def get(self, index):
   IFD = self.IFDs[index]
   ra = RandomAccessFile(self.filepath, 'r')
   try:
     cell_position = [0, 0, index]
     pixels = read_TIFF_plane(ra, IFD) # a native array
     access = self.types[IFD["bitDepth"]][0] # e.g. ByteArray, FloatArray ...
     return Cell(self.cell_dimensions, cell_position, access(pixels))
   finally:
     ra.close()
Exemplo n.º 4
0
def updateCmdForDeltaScanning(commandLine, Framework):
    originalScanFileFolderPath = CollectorsParameters.PROBE_MGR_INVENTORY_XMLENRICHER_FILES_FOLDER + XmlEnricherConstants.ORIGINAL_FOLDER_NAME
    originalScanFile = File(originalScanFileFolderPath, InventoryUtils.generateScanFileName(Framework))
    if originalScanFile.exists():
        scan = None
        try:
            try:
                buffer = jarray.zeros(0x24, 'b')
                fileSize = originalScanFile.length()
                if fileSize > 0x24:
                    scan = RandomAccessFile(originalScanFile, "r")
                    scan.readFully(buffer)
                    if (buffer[0] == 0x1F) and ((buffer[1] & 0xFF) == 0x8B) and (buffer[2] == 0x08):
                        scan.seek(fileSize - 8)
                        scan.readFully(buffer, 0, 8)
                        crc32 = getInt(buffer, 0)
                        size = getInt(buffer, 4)
                        deltaParams = ' -oldscanid:' + str(crc32) + ' -oldscansize:' + str(size) + ' '
                        index = String(commandLine).indexOf(ENTERPRISE_MODE) + String(ENTERPRISE_MODE).length()
                        commandLine = commandLine[0:index] + deltaParams + commandLine[index + 1:]
                        logger.debug('Scanner execution command updated to ', commandLine)
            except:
                logger.debugException("Failed to calculate CRC32 and size of zipped scan file " + originalScanFile.getAbsolutePath())
        finally:
            if scan is not None:
                try:
                    scan.close()
                except:
                    pass
    return commandLine
Exemplo n.º 5
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()
Exemplo n.º 6
0
 def get(self, index):
     ra = None
     try:
         # Read cell origin and dimensions for cell at index
         cellMin = zeros(3, 'l')  # long, 3 dimensions
         cellDims = zeros(3, 'i')  # integer, 3 dimensions
         grid.getCellDimensions(index, cellMin, cellDims)
         # Unpack Cell origin (in pixel coordinates)
         x, y, z = cellMin
         # Unpack Cell dimensions: at margins, may be smaller than cell_width, cell_height
         width, height, _ = cellDims  # ignore depth: it's 1
         # Read cell from file into a byte array
         ra = RandomAccessFile(filepaths[z], 'r')
         read_width = width * bytesPerPixel
         bytes = zeros(read_width * height, 'b')
         # Initial offset to the Cell origin
         offset = (section_width * y + x) * bytesPerPixel
         n_read = 0
         n_pixels = width * height
         # Read line by line
         while n_read < n_pixels:
             ra.seek(offset)
             ra.read(bytes, n_read, read_width)
             n_read += read_width
             offset += section_width * bytesPerPixel
         # Create a new Cell of the right pixel type
         return Cell(cellDims, cellMin, createAccess(bytes, bytesPerPixel))
     except:
         print sys.exc_info()
     finally:
         if ra:
             ra.close()
Exemplo n.º 7
0
 def get(self, index):
     """ Assumes:
  - uncompressed image
  - one sample per pixel (one channel only)
 """
     IFD = self.IFDs[index]
     ra = RandomAccessFile(self.filepath, 'r')
     try:
         cell_dimensions = [IFD["width"], IFD["height"], 1]
         cell_position = [0, 0, index]
         pixels = read_TIFF_plane(ra, IFD)
         return Cell(cell_dimensions, cell_position,
                     self.types[pixels.typecode](pixels))
     finally:
         ra.close()
Exemplo n.º 8
0
def readBinaryMaskImg(filepath, width, height, depth, header_size):
  ra = RandomAccessFile(filepath, 'r')
  try:
    ra.skipBytes(header_size)
    bytes = zeros(width * height * depth, 'b')
    ra.read(bytes)
    return ArrayImgs.unsignedBytes(bytes, [width, height, depth])
  finally:
    ra.close()
Exemplo n.º 9
0
def readUnsignedBytes(path, dimensions, header=0):
    """ Read a file as an ArrayImg of UnsignedShortType """
    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(reduce(operator.mul, dimensions), 'b')
        ra.read(bytes)
        return ArrayImgs.unsignedBytes(bytes, dimensions)
    finally:
        ra.close()
Exemplo n.º 10
0
    def getSprite(self, name):

        f = RandomAccessFile(self.file, "r")

        sprite = self.sprites[name]
        if not sprite.decoded:
            self.read_details(f, sprite)

        return sprite
Exemplo n.º 11
0
    def __init__(self, fileToPrint):
        self.fileName = fileToPrint
        self.today = SimpleDateFormat("yyyy.MM.dd HH:mm").format(Date())

        self.textFont = Font("Monospaced", Font.PLAIN, 12)

        # We use a RandomAccessFile because Java likes to seek around and
        # print pages multiple times.
        self.raf = RandomAccessFile(self.fileName, "r")

        # These keep track of what pages we've found so far,
        # and where in the file the pages start.
        # Page 0's starts out filled, and the following pages' are filled
        # at the end of its predecessor.
        self.pagePointers = []
        self.pageAfterEnd = None

        # Set the pointer for page 0.
        self.pagePointers.append(self.raf.getFilePointer())
Exemplo n.º 12
0
def read_1D_java(size, filename="fid"):
    """
    Reads in a Bruker 1D fid

    size is the number of data-points in the fid
    uses java low level file access
    """
    from java.io import RandomAccessFile
    #    from nmrtec.util.IO import SwapByte
    import jarray

    # read binary
    dim(1)
    chsize(int(size))
    f = RandomAccessFile(filename, "r")
    for i in range(size):
        x = f.readInt()
        #        x = SwapByte.swap(x)       # uncoment if swapbyte is required
        #        print i,x
        setval(i + 1, float(x))  # copy to 1D buffer
Exemplo n.º 13
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()
Exemplo n.º 14
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()
Exemplo n.º 15
0
def updateCmdForDeltaScanning(commandLine, Framework):
    originalScanFileFolderPath = CollectorsParameters.PROBE_MGR_INVENTORY_XMLENRICHER_FILES_FOLDER + XmlEnricherConstants.ORIGINAL_FOLDER_NAME
    originalScanFile = File(originalScanFileFolderPath,
                            InventoryUtils.generateScanFileName(Framework))
    if originalScanFile.exists():
        scan = None
        try:
            try:
                buffer = jarray.zeros(0x24, 'b')
                fileSize = originalScanFile.length()
                if fileSize > 0x24:
                    scan = RandomAccessFile(originalScanFile, "r")
                    scan.readFully(buffer)
                    if (buffer[0] == 0x1F) and (
                        (buffer[1] & 0xFF) == 0x8B) and (buffer[2] == 0x08):
                        scan.seek(fileSize - 8)
                        scan.readFully(buffer, 0, 8)
                        crc32 = getInt(buffer, 0)
                        size = getInt(buffer, 4)
                        deltaParams = ' -oldscanid:' + str(
                            crc32) + ' -oldscansize:' + str(size) + ' '
                        index = String(commandLine).indexOf(
                            ENTERPRISE_MODE) + String(
                                ENTERPRISE_MODE).length()
                        commandLine = commandLine[
                            0:index] + deltaParams + commandLine[index + 1:]
                        logger.debug('Scanner execution command updated to ',
                                     commandLine)
            except:
                logger.debugException(
                    "Failed to calculate CRC32 and size of zipped scan file " +
                    originalScanFile.getAbsolutePath())
        finally:
            if scan is not None:
                try:
                    scan.close()
                except:
                    pass
    return commandLine
Exemplo n.º 16
0
    def read(self, file):

        f = RandomAccessFile(file, "r")

        # Examine the sprites
        number = self.str2num(4, f)
        offset = self.str2num(4, f) - 4
        free = self.str2num(4, f) - 4

        self.sprites = {}

        o = offset
        while offset < free:
            offset += self.read_name(f, offset)
Exemplo n.º 17
0
    def __init__(self, fileToPrint):
        self.fileName = fileToPrint
        self.today = SimpleDateFormat("yyyy.MM.dd HH:mm").format(Date())

        self.textFont = Font("Monospaced", Font.PLAIN, 12)

        # We use a RandomAccessFile because Java likes to seek around and
        # print pages multiple times.
        self.raf = RandomAccessFile(self.fileName, "r")

        # These keep track of what pages we've found so far,
        # and where in the file the pages start.
        # Page 0's starts out filled, and the following pages' are filled
        # at the end of its predecessor.
        self.pagePointers = []
        self.pageAfterEnd = None

        # Set the pointer for page 0.
        self.pagePointers.append(self.raf.getFilePointer())
Exemplo n.º 18
0
writer = JAIWriter()
writer.setFormatName("tiff")
# See doc: https://download.java.net/media/jai/javadoc/1.1.3/jai-apidocs/com/sun/media/jai/codec/TIFFEncodeParam.html
param = TIFFEncodeParam()
param.setCompression(TIFFEncodeParam.COMPRESSION_PACKBITS)
writer.setImageEncodeParam(param)
writer.write(filepath, imp)


# Parse the test file
IFDs = list(parse_TIFF_IFDs(filepath)) # the tags of each IFD

firstIFD = IFDs[0]
print firstIFD

ra = RandomAccessFile(filepath, 'r')
try:
  # Read the image plane, compressed with packbits
  bytes_packedbits = zeros(sum(firstIFD["StripByteCounts"]), 'b')
  index = 0
  for strip_offset, strip_length in zip(firstIFD["StripOffsets"], firstIFD["StripByteCounts"]):
    ra.seek(strip_offset)
    ra.read(bytes_packedbits, index, strip_length)
    index += strip_length
  print "Compressed:", bytes_packedbits
  # unpack
  bytes1 = unpackBits2(bytes_packedbits, firstIFD)
  bytes2 = unpackBits2(bytes_packedbits, firstIFD, use_imagereader=True)
  print "Decompressed jython:", bytes1
  print "Decompressed imagej:", bytes2
  # Check:
Exemplo n.º 19
0
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()
                    ra, n)  # should have the actual data, left-justified
                ra.skipBytes(
                    4 - n
                )  # if any left to skip up to 12 total for the tag entry, may skip none
            tags[name] = dataOffset
            print "tag:", name, dataType, dataCount, dataOffset
        else:
            ra.skipBytes(
                10)  # 2 were for the tagId, 12 total for each tag entry
    nextIFDoffset = parseNextInt(ra, 4)
    return tags, nextIFDoffset


if filepath.endswith("tif"):
    try:
        ra = RandomAccessFile(filepath, 'r')
        # TIFF file format can have metadata at the end after the images, so the above approach can fail
        # TIFF file header is 8-bytes long:
        # (See: http://paulbourke.net/dataformats/tiff/tiff_summary.pdf )
        #
        # Bytes 1 and 2: identifier. Either the value 4949h (II) or 4D4Dh (MM),
        #                            meaning little-ending and big-endian, respectively.
        # All data encountered past the first two bytes in the file obey
        # the byte-ordering scheme indicated by the identifier field.
        b1, b2 = ra.read(), ra.read()  # as two java int, each one byte sized
        bigEndian = chr(b1) == 'M'
        parseNextInt = parseNextIntBigEndian if bigEndian else parseNextIntLittleEndian
        # Bytes 3 and 4: Version: Always 42
        ra.skipBytes(2)
        # Bytes 5,6,7,8: IFDOffset: offset to first image file directory (IFD), the metadata entry for the first image.
        firstIFDoffset = parseNextInt(ra, 4)
Exemplo n.º 21
0
class JESPrintableDocument(JESPrintable):
    def __init__(self, fileToPrint):
        self.fileName = fileToPrint
        self.today = SimpleDateFormat("yyyy.MM.dd HH:mm").format(Date())

        self.textFont = Font("Monospaced", Font.PLAIN, 12)

        # We use a RandomAccessFile because Java likes to seek around and
        # print pages multiple times.
        self.raf = RandomAccessFile(self.fileName, "r")

        # These keep track of what pages we've found so far,
        # and where in the file the pages start.
        # Page 0's starts out filled, and the following pages' are filled
        # at the end of its predecessor.
        self.pagePointers = []
        self.pageAfterEnd = None

        # Set the pointer for page 0.
        self.pagePointers.append(self.raf.getFilePointer())

    def printPage(self, g, pform, pageIndex):
        if pageIndex < len(self.pagePointers):
            # We already know where this page starts. Seek there.
            self.raf.seek(self.pagePointers[pageIndex])
        elif pageIndex >= self.pageAfterEnd:
            # We've found the end, and this page is after it.
            return JESPrintable.NO_SUCH_PAGE
        else:
            # We haven't gotten here yet.
            # Java will probably print each page multiple times,
            # and maybe even repeat pages after their successors,
            # but it should never print page n + 1 before page n.
            raise RuntimeError("Printing pages out of order")

        # Find our dimensions.
        minX = int(pform.getImageableX() + 10)
        width = int(pform.getImageableWidth() - 10)
        maxX = int(minX + width)
        midX = int(minX + width / 2)

        minY = int(pform.getImageableY() + 12)
        height = int(pform.getImageableHeight() - 12)
        maxY = int(minY + height)

        x, y = minX, minY

        # Title lines
        g.setColor(Color.black)
        # Left: filename
        titleString = "JES: " + os.path.basename(self.fileName)
        g.drawString(titleString, x, y)
        # Center: text
        pageNoString = "Page %d" % (pageIndex + 1)
        pageNoWidth = g.getFontMetrics().stringWidth(pageNoString)
        g.drawString(pageNoString, midX - (pageNoWidth / 2), y)
        # Right: date
        dateString = self.today
        dateWidth = g.getFontMetrics().stringWidth(dateString)
        g.drawString(dateString, maxX - dateWidth, y)
        # Line below header
        g.drawLine(minX, y + 6, maxX, y + 6)

        # OK, now the text.
        g.setColor(Color.black)
        g.setFont(self.textFont)

        # Generate as many lines as will fit in imageable area.
        y += 24
        while y + 12 < maxY:
            line = self.raf.readLine()
            if line is None:
                # We've already printed the last line.
                # Don't print another page.
                self.pageAfterEnd = pageIndex + 1
                return JESPrintable.PAGE_EXISTS
            try:
                g.drawString(line, x, y)
            except:
                # TBH I'm not sure what kind of exceptions happen here.
                # (If the line's too long, it'll just print over the margin.)
                # Unprintable characters maybe?
                g.drawString(' ', x, y)
            y = y + 12

        if pageIndex + 1 == len(self.pagePointers):
            # Hey, we've discovered the pointer to the next page!
            # Let's add it to the list so the next print call works.
            self.pagePointers.append(self.raf.getFilePointer())

        return JESPrintable.PAGE_EXISTS
 def get(self, index):
     ra = None
     try:
         # Read cell origin and dimensions for cell at index
         cellMin = zeros(3, 'l')  # long[3]
         cellDims = zeros(3, 'i')  # integer[3]
         grid.getCellDimensions(index, cellMin, cellDims)
         # Unpack Cell origin (in pixel coordinates)
         x, y, z = cellMin
         # Unpack Cell dimensions: at margins, may be smaller than cell_width, cell_height
         width, height, _ = cellDims  # ignore depth: it's 1
         # Read cell from file into a byte array
         ra = RandomAccessFile(filepaths[z], 'r')
         read_width = width * bytesPerPixel
         bytes = zeros(read_width * height,
                       'b')  # will contain the entire Cell pixel data
         # Initial offset to the Cell origin
         offset = (section_width * y + x) * bytesPerPixel
         n_pixels = width * height
         if width == section_width:
             # Read whole block in one go: cell data is continuous in the file
             ra.seek(offset)
             ra.read(bytes, 0, n_pixels * bytesPerPixel)
         else:
             # Read line by line
             n_read = 0
             while n_read < n_pixels:
                 ra.seek(offset)
                 ra.read(bytes, n_read, read_width)
                 n_read += read_width  # ensure n_read advances in case file is truncated to avoid infinite loop
                 offset += section_width * bytesPerPixel
         # Create a new Cell of the right pixel type
         return Cell(cellDims, cellMin, createAccess(bytes, bytesPerPixel))
     except:
         print sys.exc_info()
     finally:
         if ra:
             ra.close()
Exemplo n.º 23
0
        try:
            if not File(sys.getPath(filename)).createNewFile():
                raise OSError(errno.EEXIST, errno.strerror(errno.EEXIST),
                              filename)
        except java.io.IOException, ioe:
            raise OSError(ioe)

    mode = '%s%s%s%s' % (reading and 'r' or '',
                         (not appending and writing) and 'w' or '',
                         (appending and (writing or updating)) and 'a' or '',
                         updating and '+' or '')

    if sync and (writing or updating):
        from java.io import FileNotFoundException, RandomAccessFile
        try:
            fchannel = RandomAccessFile(sys.getPath(filename), 'rws').getChannel()
        except FileNotFoundException, fnfe:
            if path.isdir(filename):
                raise OSError(errno.EISDIR, errno.strerror(errno.EISDIR))
            raise OSError(errno.ENOENT, errno.strerror(errno.ENOENT), filename)
        return FileIO(fchannel, mode)

    return FileIO(filename, mode)

def read(fd, buffersize):
    """read(fd, buffersize) -> string

    Read a file descriptor.
    """
    from org.python.core.util import StringUtil
    rawio = FileDescriptors.get(fd)
Exemplo n.º 24
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]
Exemplo n.º 25
0
def savePointMatches(img_filename1,
                     img_filename2,
                     pointmatches,
                     directory,
                     params,
                     coords_header=["x1", "y1", "x2", "y2"]):
    filename = basename(img_filename1) + '.' + basename(
        img_filename2) + ".pointmatches.csv"
    path = os.path.join(directory, filename)
    msg = [str(len(pointmatches))]
    ra = None
    try:
        """
    with open(path, 'w') as csvfile:
      w = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_NONNUMERIC)
      # First two rows: parameter names and values
      keys = params.keys()
      msg.append("keys: " + ",".join(map(str, keys)))
      msg.append("vals: " + ",".join(str(params[key]) for key in keys))
      #for pm in pointmatches:
      #  msg.append(", ".join(map(str, PointMatches.asRow(pm))))
      w.writerow(keys)
      w.writerow(tuple(params[key] for key in keys))
      # PointMatches header
      if 0 == len(pointmatches):
        # Can't know whether there are 2 or 3 dimensions per coordinate
        w.writerow(coords_header)
      else:
        w.writerow(PointMatches.csvHeader(next(iter(pointmatches)))) # support both lists and sets
      # One PointMatch per row
      for pm in pointmatches:
        w.writerow(PointMatches.asRow(pm))
      # Ensure it's written
      csvfile.flush()
      os.fsync(csvfile.fileno())
    """
        # DEBUG write differently, the above FAILS for ~20 out of 130,000 files
        lines = []
        keys = params.keys()
        lines.append(",".join(map(str, keys)))
        lines.append(",".join(map(str, (params[key] for key in keys))))
        header = coords_header if 0 == len(pointmatches) \
                               else PointMatches.csvHeader(next(iter(pointmatches)))
        lines.append(",".join(header))
        for pm in pointmatches:
            p1 = pm.getP1().getW()  # a double[] array
            p2 = pm.getP2().getW()  # a double[] array
            lines.append("%f,%f,%f,%f" % (p1[0], p1[1], p2[0], p2[1]))
        body = "\n".join(lines)
        ra = RandomAccessFile(path, 'rw')
        ra.writeBytes(body)
        ra.getFD().sync()  # ensure it's written
    except:
        syncPrintQ("Failed to save pointmatches at %s\n%s" %
                   (path, "\n".join(msg)))
        printException()
        if os.path.exists(path):
            os.remove(path)
    finally:
        if ra is not None:
            ra.close()
Exemplo n.º 26
0
for z in xrange(img.dimension(2)):
    radius = img.dimension(0) * 0.5 / (z + 1)
    circle = GeomMasks.openSphere(center, radius)
    # Works, explicit iteration of every pixel
    #for t in Regions.sample(circle, Views.hyperSlice(img, 2, z)):
    #  t.setOne()
    # Works: about twice as fast -- measured with: from time import time .... t0 = time(); ... t1 = time()
    deque(imap(BitType.setOne,
               Regions.sample(circle, Views.hyperSlice(img, 2, z))),
          maxlen=0)

# Write as TIFF file
filepath = os.path.join(
    tempfile.gettempdir(),
    "bit-img-" + datetime.now().strftime("%Y-%m-%d-%H:%M:%S") + ".tif")
ra = RandomAccessFile(filepath, 'rw')
try:
    # Header: big endian (4D, 4D) or (M, M), magic number (42, 42), and offset of 9 bytes to first IFD
    # Note:
    # bin(int("4D", 16))[2:].zfill(8) == '01001101'
    # int("4D", 16) == 77
    ra.write(array([77, 77, 0, 42, 0, 0, 0, 8], 'b'))
    # A tmp plane img to copy into it each 2D slice for saving later as the pixel data of each IFD
    plane_img = ArrayImgs.bits([img.dimension(0), img.dimension(1)])
    plane_array = plane_img.update(None).getCurrentStorageArray()  # a long[]
    bb = ByteBuffer.allocate(len(plane_array) *
                             8)  # each long primitive has 8 bytes
    # Each IFD and image data
    # Use either short (3) or int (4) for the DataType, so that ImageJ's ij.io.TiffDecoder can read it
    tags = {
        256: (4, 4, img.dimension(0)),  # signed int (32-bit), 4 bytes, width
Exemplo n.º 27
0
class JESPrintableDocument(JESPrintable):
    def __init__(self, fileToPrint):
        self.fileName = fileToPrint
        self.today = SimpleDateFormat("yyyy.MM.dd HH:mm").format(Date())

        self.textFont = Font("Monospaced", Font.PLAIN, 12)

        # We use a RandomAccessFile because Java likes to seek around and
        # print pages multiple times.
        self.raf = RandomAccessFile(self.fileName, "r")

        # These keep track of what pages we've found so far,
        # and where in the file the pages start.
        # Page 0's starts out filled, and the following pages' are filled
        # at the end of its predecessor.
        self.pagePointers = []
        self.pageAfterEnd = None

        # Set the pointer for page 0.
        self.pagePointers.append(self.raf.getFilePointer())

    def printPage(self, g, pform, pageIndex):
        if pageIndex < len(self.pagePointers):
            # We already know where this page starts. Seek there.
            self.raf.seek(self.pagePointers[pageIndex])
        elif pageIndex >= self.pageAfterEnd:
            # We've found the end, and this page is after it.
            return JESPrintable.NO_SUCH_PAGE
        else:
            # We haven't gotten here yet.
            # Java will probably print each page multiple times,
            # and maybe even repeat pages after their successors,
            # but it should never print page n + 1 before page n.
            raise RuntimeError("Printing pages out of order")

        # Find our dimensions.
        minX = int(pform.getImageableX() + 10)
        width = int(pform.getImageableWidth() - 10)
        maxX = int(minX + width)
        midX = int(minX + width / 2)

        minY = int(pform.getImageableY() + 12)
        height = int(pform.getImageableHeight() - 12)
        maxY = int(minY + height)

        x, y = minX, minY

        # Title lines
        g.setColor(Color.black)
        # Left: filename
        titleString = "JES: " + os.path.basename(self.fileName)
        g.drawString(titleString, x, y)
        # Center: text
        pageNoString = "Page %d" % (pageIndex + 1)
        pageNoWidth = g.getFontMetrics().stringWidth(pageNoString)
        g.drawString(pageNoString, midX - (pageNoWidth / 2), y)
        # Right: date
        dateString = self.today
        dateWidth = g.getFontMetrics().stringWidth(dateString)
        g.drawString(dateString, maxX - dateWidth, y)
        # Line below header
        g.drawLine(minX, y + 6, maxX, y + 6)

        # OK, now the text.
        g.setColor(Color.black)
        g.setFont(self.textFont)

        # Generate as many lines as will fit in imageable area.
        y += 24
        while y + 12 < maxY:
            line = self.raf.readLine()
            if line is None:
                # We've already printed the last line.
                # Don't print another page.
                self.pageAfterEnd = pageIndex + 1
                return JESPrintable.PAGE_EXISTS
            try:
                g.drawString(line, x, y)
            except:
                # TBH I'm not sure what kind of exceptions happen here.
                # (If the line's too long, it'll just print over the margin.)
                # Unprintable characters maybe?
                g.drawString(' ', x, y)
            y = y + 12

        if pageIndex + 1 == len(self.pagePointers):
            # Hey, we've discovered the pointer to the next page!
            # Let's add it to the list so the next print call works.
            self.pagePointers.append(self.raf.getFilePointer())

        return JESPrintable.PAGE_EXISTS