def main():
    imp = IJ.getFilePath("Select DCIMG file")
    if not imp:
        return
    root, ext = os.path.splitext(imp)
    if ext.lower() != '.dcimg':
        cFrame = PlugInFrame('ERR DLG')
        MessageDialog(cFrame, 'ERROR', 'Expected extension .dcimg')
        return

    #Lets start
    fID = open(imp, 'rb')

    hdr_bytes = read_header_bytes(fID)
    hdr = parse_header_bytes(fID, hdr_bytes)

    metadataStr = beginMetadata()
    for key, value in hdr.iteritems():
        metadataStr += addMetadataEntry(key, str(value))

    metadataStr += endMetadata()
    metadataDlg = HTMLDialog("DCIMG metadata", metadataStr, 0)
    size = metadataDlg.getSize()
    if size.width < 300:
        size.width = 300
    if size.height < 500:
        size.height = 500
    metadataDlg.setSize(size)

    finfo = FileInfo()
    finfo.fileName = imp
    #finfo.width = hdr['xsize_req']
    finfo.width = hdr['xsize']
    finfo.height = hdr['ysize']
    finfo.nImages = hdr['nframes']
    finfo.offset = 232
    finfo.fileType = hdr['bitdepth'] / 8 - 1  #Ugh
    finfo.intelByteOrder = 1
    #finfo.gapBetweenImages = int(hdr['bytes_per_img']*(1-float(hdr['xsize_req'])/float(hdr['xsize'])))
    finfo.gapBetweenImages = 0
    finfo.fileFormat = 1
    finfo.samplesPerPixel = 1
    finfo.displayRanges = None
    finfo.lutSize = 0
    finfo.whiteIsZero = 0
    vs = VirtualStack()
    finfo.virtualStack = vs
    FileInfoVirtualStack(finfo)
示例#2
0
文件: io.py 项目: mwinding/scripts
def unpackBits2(bytes_packedbits, tags, use_imagereader=False):
    # Decompress a packBits-compressed image, returns an array of n_bytes.
    # ra: a RandomAccessFile with the pointer at the right place to start reading.
    # PackBits actually packages bytes: a byte-wise RLE most efficient at encoding runs of bytes
    # 3 types of data packets:
    # 1. two-byte encoded run packet:
    #     - first byte is the number of bytes in the run.
    #       Ranges from -127 to -1, meaning +1: from 2 to 128 (-count + 1)
    #     - second byte value of each byte in the run.
    # 2. literal run packet: stores 1 to 128 bytes literally without compression.
    #     - first byte is the number of bytes in the run.
    #       Ranges from 0 to 127, indicating 1 to 128 values (count + 1)
    #     - then the sequence of literal bytes
    # 3. no-op packet: never used, value -128.
    # See documentation: http://paulbourke.net/dataformats/tiff/tiff_summary.pdf
    # (note documentation PDF has its details flipped when it comes to the ranges for literal runs and packed runs)
    # See also: ij.io.ImageReader.packBitsUncompress (a public method without side effects)

    n_bytes = tags["width"] * tags["height"]

    if use_imagereader:
        return ImageReader(FileInfo()).packBitsUncompress(
            bytes_packedbits, n_bytes)

    bytes = zeros(n_bytes, 'b')
    try:
        indexP = 0  # packed
        indexU = 0  # unpacked
        while indexU < n_bytes:
            count = bytes_packedbits[indexP]
            if count >= 0:
                # Literal run
                System.arraycopy(bytes_packedbits, indexP + 1, bytes, indexU,
                                 count + 1)
                indexP += count + 2  # one extra for the 'count' byte
                indexU += count + 1
            else:
                # Packed run
                Arrays.fill(bytes, indexU, indexU - count + 1,
                            bytes_packedbits[indexP + 1])
                indexP += 2
                indexU += -count + 1
    except:
        print sys.exc_info()
    finally:
        return bytes
示例#3
0
文件: io.py 项目: mwinding/scripts
def unpackBits(ra, tags, use_imagereader=False):
    # Decompress a packBits-compressed image, write into bytes array starting at indexU.
    # ra: a RandomAccessFile with the pointer at the right place to start reading.
    # PackBits actually packages bytes: a byte-wise RLE most efficient at encoding runs of bytes
    # 3 types of data packets:
    # 1. two-byte encoded run packet:
    #     - first byte is the number of bytes in the run.
    #       Ranges from -127 to -1, meaning +1: from 2 to 128 (-count + 1)
    #     - second byte value of each byte in the run.
    # 2. literal run packet: stores 1 to 128 bytes literally without compression.
    #     - first byte is the number of bytes in the run.
    #       Ranges from 0 to 127, indicating 1 to 128 values (count + 1)
    #     - then the sequence of literal bytes
    # 3. no-op packet: never used, value -128.
    # See documentation: http://paulbourke.net/dataformats/tiff/tiff_summary.pdf
    # (note documentation PDF has its details flipped when it comes to the ranges for literal runs and packed runs)
    # See also: ij.io.ImageReader.packBitsUncompress (a public method without side effects)

    if use_imagereader:
        return ImageReader(FileInfo()).packBitsUncompress(
            getIFDImageBytes(ra, tags), tags["width"] * tags["height"])

    try:
        bytes = zeros(tags["width"] * tags["height"], 'b')
        indexU = 0  # index over unpacked bytes
        for strip_offset, strip_length in zip(tags["StripOffsets"],
                                              tags["StripByteCounts"]):
            ra.seek(strip_offset)
            indexP = 0
            while indexP < strip_length and indexU < len(bytes):
                count = ra.readByte()
                if count >= 0:
                    # Literal run
                    ra.read(bytes, indexU, count + 1)
                    indexP += count + 2  # one extra for the count byte
                    indexU += count + 1
                else:
                    # Packed run
                    Arrays.fill(bytes, indexU, indexU - count + 1,
                                ra.readByte())
                    indexP += 2
                    indexU += -count + 1
    except:
        print sys.exc_info()
    finally:
        return bytes
    cs = ChannelSeparator()
    cs.setId(filepath)
    bfvs = BFVirtualStack(filepath, cs, False, False, False)
    stack = ImageStack(width, height)
    for index in xrange(slice_index, slice_index + num_slices):
        stack.addSlice(bfvs.getProcessor(index))
    title = os.path.split(filepath)[1] + " from slice %i" % slice_index
    imp = ImagePlus(title, stack)
    imp.show()
finally:
    cs.close()

# Approach 3: using low-level ImageJ libraries
from ij.io import FileInfo, FileOpener

fi = FileInfo()
fi.width = width
fi.height = height
fi.offset = headerSize + slice_offset
# ASSUMES images aren't ARGB, which would also have 32 as bit depth
# (There are other types: see FileInfo javadoc)
fi.fileType = {
    8: FileInfo.GRAY8,
    16: FileInfo.GRAY16_UNSIGNED,
    24: FileInfo.RGB,
    32: FileInfo.GRAY32_UNSIGNED
}[bitDepth]
fi.samplesPerPixel = 1
fi.nImages = num_slices
directory, filename = os.path.split(filepath)
fi.directory = directory
示例#5
0
文件: io.py 项目: mwinding/scripts
def read_TIFF_plane(ra, tags, handler=None):
    """ ra: RandomAccessFile
      tags: dicctionary of TIFF tags for the IFD of the plane to parse.
      handler: defaults to Nobe; a function that reads the image data and returns an array.

      For compressed image planes, takes advantage of the ij.io.ImageReader class
      which contains methods that ought to be static (no side effects) and therefore
      demand a dummy constructor just to invoke them.
  """
    if handler:
        return handler(ra, tags)
    # Values of the "compressed" tag field (when present):
    #     1: "uncompressed",
    # 32773: "packbits",
    #     5: "LZW",
    #     6: "JPEG",
    width, height = tags["width"], tags["height"]
    bitDepth = tags["bitDepth"]
    n_bytes = int(ceil(width * height *
                       (float(bitDepth) / 8)))  # supports bitDepth < 8
    compression = tags.get("compression", 1)
    bytes = None

    if 32773 == compression:
        bytes = unpackBits(ra, tags, use_imagereader=True)
    elif 5 == compression:
        bytes = ImageReader(FileInfo()).lzwUncompress(
            getIFDImageBytes(ra, tags), tags["width"] * tags["height"])
    elif 32946 == compression or 8 == compression:
        bytes = ImageReader(FileInfo()).zipUncompress(
            getIFDImageBytes(ra, tags), tags["width"] * tags["height"])
    elif 1 == compression:
        bytes = zeros(n_bytes, 'b')
        index = 0
        for strip_offset, strip_length in zip(tags["StripOffsets"],
                                              tags["StripByteCounts"]):
            ra.seek(strip_offset)
            ra.read(bytes, index, strip_length)
    elif 6 == compression:
        print "Unsupported compression: JPEG"
        raise Exception("Can't handle JPEG compression of TIFF image planes")
    else:
        raise Exception("Can't deal with compression type " + str(compression))

    # If read as bytes, parse to the appropriate primitive type
    if 8 == bitDepth:
        return bytes
    bb = ByteBuffer.wrap(bytes)
    if not tags["bigEndian"]:
        bb = bb.order(ByteOrder.BIG_ENDIAN)
    if 16 == bitDepth:
        pixels = zeros(width * height, 'h')
        bb.asShortBuffer().get(pixels)
        return pixels
    if 32 == bitDepth:
        pixels = zeros(width * height, 'f')
        bb.asFloatBuffer().get(pixels)
        return pixels
    if bitDepth < 8:
        # Support for 1-bit, 2-bit, 3-bit, ... 12-bit, etc. images
        pixels = zeros(int(ceil(width * height * bitDepth / 64.0)), 'l')
        bb.asLongBuffer().get(pixels)
        # Reverse bits from left to right to right to left for ImgLib2 LongArray
        pixels = array(imap(Long.reverse, pixels), 'l')
        return pixels
示例#6
0
metadataStr += addMetadataEntry('nImages', str(frames))
metadataStr += addMetadataEntry('offset', str(offset+64))
metadataStr += addMetadataEntry('fileType', str(fileType))
metadataStr += addMetadataEntry('gapBetweenImages', str(gap+64))
if(metadataInconsistency > 0):
  metadataStr += addMetadataEntry('Inconsistent METADATA', str(metadataInconsistency))
metadataStr += endMetadata()
metadataDlg = HTMLDialog("HIS parameters", metadataStr, 0)
size = metadataDlg.getSize()
if size.width < 300:
  size.width = 300
if size.height < 300:
  size.height = 300
metadataDlg.setSize(size)

finfo = FileInfo()
finfo.fileName = imp
finfo.width = width
finfo.height = height
finfo.nImages = frames
finfo.offset = offset+64
finfo.fileType = fileType-1
finfo.intelByteOrder = 1
finfo.gapBetweenImages = gap+64
finfo.fileFormat = 1
finfo.samplesPerPixel = 1
finfo.displayRanges = None
finfo.lutSize = 0
finfo.whiteIsZero = 0
vs = VirtualStack()
finfo.virtualStack = vs