Example #1
0
    def LoadData(self):
        #load colormap from file
	import os
        
	myfile = open("ct_head.rawiv", "rb" )
        print(("myfile: ", myfile))
        if not myfile:
            print("Error: could not open file: ct_head.rawiv")
        else:
            #the header
            header=myfile.read(68)
            # unpack header following rawiv format,big endian
            h = unpack('>6f5I6f',header)
            width=int(h[8])
            height=int(h[9])
            depth =int(h[10])

            # load the data
            l = myfile.read(width*height*depth)
            self.data = Numeric.fromstring(l, Numeric.UnsignedInt8,
                                           (width*height*depth) )
            self.size = (width, height, depth)
            print(("size: ", self.size))
            #self.data = Numeric.reshape(self.data,(width,height,depth))
            myfile.close()
        
  	myfile = open("colormap.map", "rb" )
        if not myfile:
            print("Error: Could not open file: colormap.map")
        else:
            l = myfile.read(256*4)
            self.byte_map =Numeric.fromstring(l,
                                              Numeric.UnsignedInt8, (256*4)) 
            self.byte_map = Numeric.reshape(self.byte_map,(256,4))
            myfile.close()
Example #2
0
    def LoadData(self):
        #load colormap from file
        import os

        myfile = open("ct_head.rawiv", "rb")
        print(("myfile: ", myfile))
        if not myfile:
            print("Error: could not open file: ct_head.rawiv")
        else:
            #the header
            header = myfile.read(68)
            # unpack header following rawiv format,big endian
            h = unpack('>6f5I6f', header)
            width = int(h[8])
            height = int(h[9])
            depth = int(h[10])

            # load the data
            l = myfile.read(width * height * depth)
            self.data = Numeric.fromstring(l, Numeric.UnsignedInt8,
                                           (width * height * depth))
            self.size = (width, height, depth)
            print(("size: ", self.size))
            #self.data = Numeric.reshape(self.data,(width,height,depth))
            myfile.close()

        myfile = open("colormap.map", "rb")
        if not myfile:
            print("Error: Could not open file: colormap.map")
        else:
            l = myfile.read(256 * 4)
            self.byte_map = Numeric.fromstring(l, Numeric.UnsignedInt8,
                                               (256 * 4))
            self.byte_map = Numeric.reshape(self.byte_map, (256, 4))
            myfile.close()
Example #3
0
 def __call__(image):
     from gamera.plugins import _string_io
     pixel_type = image.data.pixel_type
     shape = (image.nrows, image.ncols)
     typecode = _typecodes[pixel_type]
     if pixel_type == RGB:
         shape += (3,)
     array = n.fromstring(_string_io._to_raw_string(image), typecode)
     return n.resize(array, shape)
Example #4
0
    def LoadVolume(self, file):
        """ Get the data from binary file,rawiv format"""

        #load colormap from file
        myfile = open(file, "rb")
        #the header
        header = myfile.read(68)
        # unpack header following rawiv format,big endian
        h = unpack('>6f5I6f', header)
        width = int(h[8])
        height = int(h[9])
        depth = int(h[10])

        # load the data
        l = myfile.read(width * height * depth)
        self.dataArr = Numeric.fromstring(l, Numeric.UnsignedInt8,
                                          (width * height * depth))
        self.volumeSize = (width, height, depth)
        #self.dataArr = Numeric.reshape(self.dataArr,(width,height,depth))
        myfile.close()

        if self.dataArr == None:
            print " you need to load a volume data"
            return
        if self.firstLoaded:
            if self.viewer:
                rootObject = self.viewer.rootObject
                self.minBB = (-0.5, -0.5, -0.5)
                self.maxBB = (0.5, 0.5, 0.5)
                self.viewer.NormalizeCurrentObject()
                self.firstLoaded = 0
        status = self.volume.uploadColorMappedData(self.dataArr, width, height,
                                                   depth)
        if status != 1:
            raise RuntimeError(
                "uploadColorMappedData() in LoadVoulume failed. Status %d" %
                status)
        #print "status uploadColorMappedData: ", status
        self.dataArr = Numeric.reshape(self.dataArr, (depth, height, width))
        if self.byte_map == None:
            self.grayRamp()
#update crop box
        self.crop.updateData()
        nx, ny, nz = self.volumeSize
        self.cropBox.setVolSize((nx, ny, nz))
        self.cropBox.xmin = 0
        self.cropBox.xmax = nx
        self.cropBox.ymin = 0
        self.cropBox.ymax = ny
        self.cropBox.zmin = 0
        self.cropBox.zmax = nz
        self.cropBox.update()
        for c in self.onaddVolume_list:
            c.OnAddVolumeToViewer()
    def LoadVolume(self,file):
        """ Get the data from binary file,rawiv format"""
	
	#load colormap from file
	myfile = open(file, "rb" )
	#the header
	header=myfile.read(68)
	# unpack header following rawiv format,big endian
	h = unpack('>6f5I6f',header)
	width=int(h[8])
	height=int(h[9])
	depth =int(h[10])
	
	# load the data
	l = myfile.read(width*height*depth)
	self.dataArr = Numeric.fromstring(l, Numeric.UnsignedInt8,
					  (width*height*depth))
	self.volumeSize = (width,height,depth)
	#self.dataArr = Numeric.reshape(self.dataArr,(width,height,depth))
	myfile.close()
			
	if self.dataArr == None:
	    print " you need to load a volume data"
            return
        if self.firstLoaded:
	    if self.viewer:
	        rootObject = self.viewer.rootObject
		self.minBB=(-0.5, -0.5, -0.5)
		self.maxBB=(0.5, 0.5, 0.5)
		self.viewer.NormalizeCurrentObject()
		self.firstLoaded = 0
	status = self.volume.uploadColorMappedData(self.dataArr,width,height,depth)
	if status !=1:
	    raise RuntimeError("uploadColorMappedData() in LoadVoulume failed. Status %d"% status)
            #print "status uploadColorMappedData: ", status
	self.dataArr = Numeric.reshape(self.dataArr,
				       (depth,height,width))
	if self.byte_map == None:
	    self.grayRamp()
	#update crop box
	self.crop.updateData()
	nx,ny,nz = self.volumeSize
	self.cropBox.setVolSize((nx,ny,nz))
	self.cropBox.xmin = 0
	self.cropBox.xmax = nx
	self.cropBox.ymin = 0
	self.cropBox.ymax = ny
	self.cropBox.zmin = 0
	self.cropBox.zmax = nz
	self.cropBox.update()
	for c in self.onaddVolume_list:
	    c.OnAddVolumeToViewer()
Example #6
0
    def LoadColorMap(self, file):
        """ Get the colormap data from binaries files"""

        myfile = open(file, "rb")
        l = myfile.read(256 * 4)
        self.byte_map = Numeric.fromstring(l, (256 * 4), Numeric.UnsignedInt8)
        self.byte_map = Numeric.reshape(self.byte_map, (256, 4))
        myfile.close()
        if self.byte_map == None:
            print " you need to load a color map"
            return
        else:
            self.volume.uploadColorMap(self.byte_map)
    def LoadColorMap(self,file):
        """ Get the colormap data from binaries files"""
		
	myfile = open(file, "rb" )
	l = myfile.read(256*4)
	self.byte_map =Numeric.fromstring(l,(256*4),
					  Numeric.UnsignedInt8) 
	self.byte_map = Numeric.reshape(self.byte_map,(256,4))
	myfile.close()
	if self.byte_map == None:
	    print " you need to load a color map"
	    return
        else:
	    self.volume.uploadColorMap(self.byte_map)
 def setImage(self, image, width=None, height=None, mode=None):
     self.width = None
     self.height = None
     self.imarray = None
     if image is None:
         return
     elif isinstance(image, Image.Image):
         self.width = image.size[0]
         self.height = image.size[1]
         im = image.transpose(Image.FLIP_TOP_BOTTOM)
         self.imarray = Numeric.fromstring(im.tostring(), 'B')
         self.mode = image.mode
     elif isinstance(image,Numeric.ArrayType):
         if mode == 'RGB':
             lenmode = 3
         elif mode in ['L','P']:
             lenmode = 1
         assert image.dtype.char=='b'
         if len(image.shape)==3:
             self.width = image.shape[0]
             self.height = image.shape[1]
             self.imarray = Numeric.reshape( image, (-1,))
         elif len(image.shape)==1:
             self.width = width
             self.height = height
             self.imarray = image
             self.imarray.shape = (self.width, self,height)
             self.numimarray = numarray.array(self.imarray)
             self.imarray = Numeric.reshape( image, (-1,))
         else:
             raise RuntimeError, "bad shape for image array"
         assert len(self.imarray)==self.width*self.height*lenmode
         self.mode = mode
     else:
         print 'Not surported yet'
     
     self.Configure()
     self.tkRedraw()
Example #9
0
    def setImage(self, image, width=None, height=None, mode=None):
        self.width = None
        self.height = None
        self.imarray = None
        if image is None:
            return
        elif isinstance(image, Image.Image):
            self.width = image.size[0]
            self.height = image.size[1]
            im = image.transpose(Image.FLIP_TOP_BOTTOM)
            self.imarray = Numeric.fromstring(im.tostring(), 'B')
            self.mode = image.mode
        elif isinstance(image, Numeric.ArrayType):
            if mode == 'RGB':
                lenmode = 3
            elif mode in ['L', 'P']:
                lenmode = 1
            assert image.dtype.char == 'b'
            if len(image.shape) == 3:
                self.width = image.shape[0]
                self.height = image.shape[1]
                self.imarray = Numeric.reshape(image, (-1, ))
            elif len(image.shape) == 1:
                self.width = width
                self.height = height
                self.imarray = image
                self.imarray.shape = (self.width, self, height)
                self.numimarray = numarray.array(self.imarray)
                self.imarray = Numeric.reshape(image, (-1, ))
            else:
                raise RuntimeError, "bad shape for image array"
            assert len(self.imarray) == self.width * self.height * lenmode
            self.mode = mode
        else:
            print 'Not surported yet'

        self.Configure()
        self.tkRedraw()
Example #10
0
def readbinary(filename,length):
    file = open(filename,'r')
    buffer = Numeric.fromstring(file.read(length*8),'double')
    file.close()
    # then reshape the buffer if needed
    return buffer
Example #11
0
    def Set(self, check=1, **kw):
	"""Set various parameters for texture objects:
- image=im  set an image as the texture to be used. im can be either a Numeric
array of bytes with shape (n x m x d) where d can be either 3 or 4.
Im can also be a PIL Image object. If thes image is not of type 'RGB' or
'RGBA' it will be converted to 'RGB' if possible, else ... error !

"The set command will also pad the image to make sure that width and height
are powers of 2." (has been tested for Image but not for Numeric array yet - guillaume)
"""
        if __debug__:
            if check:
                apply( checkKeywords, (self.name,self.keywords), kw)

	val = getkw(kw, 'enable')
	if not val is None:
	    assert val in (viewerConst.YES, viewerConst.NO), "enable can only be YES or NO"
	    self.enabled = val

	val = getkw(kw, 'wrapS')
	if not val is None:
	    assert val in (GL.GL_REPEAT, GL.GL_CLAMP)
	    self.wrap[0] = val

	val = getkw(kw, 'wrapT')
	if not val is None:
	    assert val in (GL.GL_REPEAT, GL.GL_CLAMP)
	    self.wrap[1] = val

	val = getkw(kw, 'magFilter')
	if not val is None:
	    assert val in (GL.GL_NEAREST, GL.GL_LINEAR)
	    self.magFilter = val

	val = getkw(kw, 'minFilter')
	if not val is None:
	    assert val in (GL.GL_NEAREST, GL.GL_LINEAR)
	    self.minFilter = val

	val = getkw(kw, 'genModS')
	if not val is None:
	    assert val in (GL.GL_OBJECT_LINEAR, GL.GL_EYE_LINEAR, GL.GL_SPHERE_MAP)
	    self.genMod[0] = val

	val = getkw(kw, 'genModT')
	if not val is None:
	    assert val in (GL.GL_OBJECT_LINEAR, GL.GL_EYE_LINEAR, GL.GL_SPHERE_MAP)
	    self.genMod[1] = val

	val = getkw(kw, 'genPlaneS')
	if not val is None:
	    assert val in (GL.GL_OBJECT_PLANE, GL.GL_EYE_PLANE)
	    self.genPlane[0] = val

	val = getkw(kw, 'genPlaneT')
	if not val is None:
	    assert val in (GL.GL_OBJECT_PLANE, GL.GL_EYE_PLANE)
	    self.genPlane[1] = val

	val = getkw(kw, 'planeS')
	if not val is None:
	    assert len(val)==4 and type(val[0])==type(0.0), "Plane has to be 4Float vector"
	    self.plane[0] = val

	val = getkw(kw, 'planeT')
	if not val is None:
	    assert len(val)==4 and type(val[0])==type(0.0), "Plane has to be 4Float vector"
	    self.plane[1] = val

	val = getkw(kw, 'level')
	if not val is None:
	    assert type(val)==type(0) and val >=0
	    self.level = val

	val = getkw(kw, 'auto')
	if not val is None:
	    assert val in (viewerConst.YES,viewerConst.NO), "auto can only be YES or NO"
	    self.auto = val

	val = getkw(kw, 'envMode')
	if not val is None:
	    assert val in (GL.GL_MODULATE, GL.GL_REPLACE, GL.GL_DECAL, GL.GL_BLEND ), "envMode can only be GL_MODULATE, GL_DECAL, or GL_BLEND"
	    self.envMode = val
	val = getkw(kw, 'envColor')
	if val:
	    col = OneColor(val)
	    if col: self.envColor = col

	b = getkw(kw, 'border')
	f = getkw(kw, 'format')
	im = getkw(kw, 'image')
	if im is not None:

            if isinstance(im, Image.Image):
                lImInstaceImage = True
            else:
                lImInstaceImage = False

            if lImInstaceImage is True:
                width = im.size[0]
                height = im.size[1]
            else:
                height = im.shape[0]
                width = im.shape[1]

            # find smallest power of 2 larger than image size
            dim1 = 1
            dim2 = 1
            while dim1 < width:
                dim1 = dim1 << 1
            while dim2 < height:
                dim2 = dim2 << 1
            self.resizeRatio = ( width / float(dim1),
                                 height / float(dim2) )

            if os.name != 'nt': #sys.platform != 'win32':
                lMaxTextureSize = GL.glGetInteger(GL.GL_MAX_TEXTURE_SIZE)
                #print "width", width, height, dim1, dim2, lMaxTextureSize
                if (dim1 > lMaxTextureSize) or (dim2 > lMaxTextureSize):
                    warnings.warn('texture map too big for this implementation of opengl %d'%lMaxTextureSize)

            if lImInstaceImage is True:
                if im.mode !='RGB' and im.mode !='RGBA':
                    im = im.convert('RGB')
                im = im.transpose(Image.FLIP_TOP_BOTTOM)
                imstr = im.tostring()
                imarr = Numeric.fromstring( imstr, 'B')
                if im.mode=='RGB':
                    imarr.shape = (height, width, 3)
                elif im.mode=='RGBA':
                    imarr.shape = (height, width, 4)
                im = imarr
                
            if (dim1 != width) or (dim2 != height):
                if len(im.shape) == 3:
                    newArray = Numeric.zeros( (dim2, dim1, len(im[0][0]) ) )
                else:
                    newArray = Numeric.zeros( (dim2, dim1 ) )
                for i in range(height):
                    for j in range(width):    
                        newArray[i][j] = im[i][j]
                im = newArray.astype('B')

	    if b:
		assert type(b)==type(0)
	    else: b = 0
	    self.border = b

	    if f:
		assert f in (GL.GL_RGB, GL.GL_RGBA), "format can only be GL_RGB or GL_RGBA"

	    assert type(im).__name__ == 'ndarray'
	    assert im.dtype.char == 'B'
	    if im.shape[-1] == 3:
		if f and f != GL.GL_RGB: raise ValueError("bad image format")
	        self.format = GL.GL_RGB
	    elif im.shape[-1] == 4:
		if f and f != GL.GL_RGBA: raise ValueError("bad image format")
	        self.format = GL.GL_RGBA

                for o in self.objects:
                    o.transparent=1
                    o.inheritMaterial=0
                    if self.viewer:
                        self.viewer.objectsNeedingRedo[o] = None
                    
	    l = len(im.shape)
	    if l==2:
		w=im.shape[0] - 2*b
		q, r  = divmod(math.log(w)/math.log(2), 1)
		if r != 0.0:
		    raise ValueError("Image width must be 2**m +2b")
		self.dim = GL.GL_TEXTURE_1D
		self.image = im
		self.width = im.shape[0]

	    elif l==3:
		w=im.shape[0] - 2*b
		q, r  = divmod(math.log(w)/math.log(2), 1)
		if r != 0.0:
		    raise ValueError("Image width must be 2**m +2b")
		h=im.shape[1] -2*b
		q, r  = divmod(math.log(h)/math.log(2), 1)
		if r != 0.0:
		    raise ValueError("Image height must be 2**m +2b")
		self.dim = GL.GL_TEXTURE_2D
		self.image = im
		self.width = im.shape[1]
		self.height = im.shape[0]

	    else:
		raise ValueError("Bad shape for image")

        if self.viewer:
            self.viewer.deleteOpenglList()
            self.viewer.Redraw()