Exemple #1
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()
Exemple #2
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()
Exemple #3
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
Exemple #4
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]
Exemple #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()
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()
Exemple #7
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
 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()
Exemple #9
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
Exemple #10
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
Exemple #11
0
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:
  if 0 == sum(a - b for a, b in zip(source_bytes, bytes1)):
    print "Image decompressed successfully by jython."
  if 0 == sum(a - b for a, b in zip(source_bytes, bytes2)):
    print "Image decompressed successfully by imagej."
finally:
  ra.close()