Beispiel #1
0
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
Beispiel #2
0
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
Beispiel #4
0
def read_TIFF_plane(ra, tags, handler=None):
    """ ra: RandomAccessFile
      tags: dicctionary of TIFF tags for the IFD of the plane to parse.
      handler: defaults to Nobe; a function that reads the image data and returns an array.

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

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

    # If read as bytes, parse to the appropriate primitive type
    if 8 == bitDepth:
        return bytes
    bb = ByteBuffer.wrap(bytes)
    if not tags["bigEndian"]:
        bb = bb.order(ByteOrder.BIG_ENDIAN)
    if 16 == bitDepth:
        pixels = zeros(width * height, 'h')
        bb.asShortBuffer().get(pixels)
        return pixels
    if 32 == bitDepth:
        pixels = zeros(width * height, 'f')
        bb.asFloatBuffer().get(pixels)
        return pixels
    if bitDepth < 8:
        # Support for 1-bit, 2-bit, 3-bit, ... 12-bit, etc. images
        pixels = zeros(int(ceil(width * height * bitDepth / 64.0)), 'l')
        bb.asLongBuffer().get(pixels)
        # Reverse bits from left to right to right to left for ImgLib2 LongArray
        pixels = array(imap(Long.reverse, pixels), 'l')
        return pixels
Beispiel #5
0
def open_Octopus_file():

	# set up a file info structure
	fi = FileInfo()
	fi.fileFormat = fi.RAW
	fi.fileType=FileInfo.GRAY16_UNSIGNED
	fi.intelByteOrder = True
	fi.nImages = 1

	op = OpenDialog("Choose Octopus .dth file...", "")
	if not op.getDirectory(): return False

	# get the file extension
	file_extension = re.search('(\.[a-z][a-z][a-z])', op.getFileName()).group(1)
	
	if file_extension != ".dth":
		dlg = GenericDialog("Warning")
		dlg.addMessage("Please select an octopus .dth file")
		dlg.showDialog()
		return False

	# now strip the filename into a stem and index
	file_parse = re.match('([a-zA-z0-9_]*_)([0-9]+)\.dth', op.getFileName())
	file_stem = file_parse.group(1)
	file_index = int( file_parse.group(2) )

	# ok now we need to parse the header info
	header = get_Octopus_header(op.getDirectory(), file_stem, file_index)
	fi.nImages  = len(header['N'])

	# check to see whether we have a bit depth, if not, assume 16-bit
	if 'Bit_Depth' in header:
		print header['Bit_Depth']
		bit_depth = int(header['Bit_Depth'][0])
		if bit_depth == 8: fi.fileType = FileInfo.GRAY8
	else:
		bit_depth = 16

	# will assume that all files have the same size
	fi.width = int( header['W'][0] )
	fi.height = int( header['H'][0] )
	file_timestamp = strftime("%a, %d %b %Y %H:%M:%S", gmtime(float(header['Time'][0])) )
	

	# make a new imagestack to store the data
	stack = ImageStack(fi.width, fi.height)

	# finally, we need to make a list of files to import as sometimes we have
	# non contiguous file numbers
	try:
		files = os.listdir(op.getDirectory())
	except IOError:
		raise IOError( "No files exist in directory: " + op.getDirectory())

	filenums = []
	for f in files:
		# strip off the stem, and get the number
		targetfile = re.match(file_stem+'([0-9]+)\.dth', f)
		# only take thosefiles which match the formatting requirements
		if targetfile:
			filenums.append( int(targetfile.group(1)) )

	# sort the file numbers
	sorted_filenums = sorted(filenums)

	# make a file stats string
	file_stats_str = file_stem + '\n' + str(fi.width) +'x' + str(fi.height) + 'x' + \
		str(len(sorted_filenums)) +' ('+str(bit_depth)+'-bit)\n' + file_timestamp


	# now open a dialog to let the user set options
	dlg = GenericDialog("Load Octopus Stream (v"+__version__+")")
	dlg.addMessage(file_stats_str)
	dlg.addStringField("Title: ", file_stem)
	dlg.addNumericField("Start: ", 1, 0);
	dlg.addNumericField("End: ", len(sorted_filenums), 0)
	dlg.addCheckbox("Open headers", True)
	dlg.addCheckbox("Contiguous stream?", False)
	dlg.addCheckbox("8-bit unsigned", bit_depth==8)
	dlg.showDialog()

	# if we cancel the dialog, exit here
	if dlg.wasCanceled():
		return

	# set some params
	file_title = dlg.getNextString()
	file_start = dlg.getNextNumber()
	file_end = dlg.getNextNumber()
	DISPLAY_HEADER = bool( dlg.getNextBoolean() )

	# check the ranges
	if file_start > file_end: 
		file_start, file_end = file_end, file_start
	if file_start < 1: 
		file_start = 1
	if file_end > len(sorted_filenums): 
		file_end = len(sorted_filenums) 

	# now set these to the actual file numbers in the stream
	file_start = sorted_filenums[int(file_start)-1]
	file_end = sorted_filenums[int(file_end)-1]

	files_to_open = [n for n in sorted_filenums if n>=file_start and n<=file_end]

	# if we've got too many, truncate the list
	if (len(files_to_open) * fi.nImages * fi.width * fi.height) > (MAX_FRAMES_TO_IMPORT*512*512):
		dlg = GenericDialog("Warning")
		dlg.addMessage("This may use a lot of memory. Continue?")
		dlg.showDialog()
		if dlg.wasCanceled(): return False

	IJ.log( "Opening file: " + op.getDirectory() + op.getFileName() )
	IJ.log( file_stats_str + "\nFile range: " + str(files_to_open[0]) + \
		"-" + str(files_to_open[-1]) +"\n" )

	# make a results table for the metadata
	# NOTE: horrible looping at the moment, but works
	if DISPLAY_HEADER:
		rt = ResultsTable()

	# ok now we can put the files together into the stack
	for i in files_to_open:

		# open the original .dat file and get the stack
		fi.fileName = get_Octopus_filename( op.getDirectory(), file_stem, i)
		
		if os.path.isfile( fi.fileName ):
			fo = FileOpener(fi)
			imp = fo.open(False).getStack() 
	
			# put the slices into the stack
			for im_slice in xrange( imp.getSize() ):
				ip = imp.getProcessor( im_slice+1 )
				if bit_depth == 8:
					bi = ip.getBufferedImage()
				else:
					bi = ip.get16BitBufferedImage() 
				stack.addSlice( file_title,  ip )


			if DISPLAY_HEADER:
				header = get_Octopus_header(op.getDirectory(), file_stem, i)
				for n in xrange(len(header['N'])):
					rt.incrementCounter()
					for k in header.keys():
						rt.addValue(k, parse_header( header[k][n] ) )

		else:
			break

	# done!
	output = ImagePlus('Octopus ('+file_stem+')', stack)
	output.show()

	if DISPLAY_HEADER:
		rt.show("Octopus header metadata")

	return True
Beispiel #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
from net.imglib2.view import Views

from net.imglib2.img.array import ArrayImgs
from net.imglib2 import KDTree, RealPoint, Cursor, RandomAccessible
from net.imglib2.type.numeric.integer import UnsignedByteType
from net.imglib2.neighborsearch import NearestNeighborSearchOnKDTree
from net.imglib2.util import Intervals
from fiji.scripting import Weaver
from java.util import ArrayList
from net.imglib2.img import Img

# Load 128x128x128 binary mask
# with zero for background and 1 for the mask

fi = FileInfo()
fi.offset = 1024  # header size in bytes
fi.width = 128
fi.height = 128
fi.nImages = 128

baseDir = "/home/albert/lab/scripts/data/cim.mcgill.ca-shape-benchmark/"

bird = IL.wrap(Raw.open(baseDir + "/birdsIm/b21.im", fi))
airplane = IL.wrap(Raw.open(baseDir + "/airplanesIm/b14.im", fi))

# Rotate bird
# Starts with posterior view
# Rotate 180 degrees around Y axis
# Set to dorsal up: 180 degrees
birdY90 = Views.rotate(bird, 2, 0)  # 90
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)