{ Sunraster Reader is a macro to import standard format Sunraster files into NIH Image. There is minimal support for Color table format. Presenly only supports RGB color maps that are 768 bytes in length. Version 1.0 Doug Morris University of Illinois Biomedical Magnetic Resonance Lab dmorris@bmrl.med.uiuc.edu To Do: Support Reduced color Maps. Add 1-bit (binary) image support. } var tstatus,dtime,longx,longy,image,palette:integer; longvalue:real; procedure GetLongValue; var val1,val2,val3,val4:integer; begin val1:= GetPixel(longx,longy); val2:= GetPixel(longx+1,longy); val3:= GetPixel(longx+2,longy); val4:= GetPixel(longx+3,longy); longvalue:= val4 + val3*256 + val2*256*256 + val1*256*256*256; end; procedure TestMagicNumber; var mval1,mval2,mval3,mval4:integer; { Test for Sun Raster file Magic Number. Magic Number is 1504078485 decimal } begin longx := 0; longy := 0; mval1:= GetPixel(longx,longy); mval2:= GetPixel(longx+1,longy); mval3:= GetPixel(longx+2,longy); mval4:= GetPixel(longx+3,longy); if (mval1 <> 89 ) then begin if (mval2 <> 166) then begin if (mval3 <> 106 ) then begin if (mval4 <> 189) then begin Dispose; PutMessage('Bad Magic number. Not a Sun Rasterfile.'); Exit; end; end; end; end; end; procedure ImportSunRaster; var nmultislice,slices,count,width,height,itype,depth,nspectra,offset,i,j,n,dtype:integer; alength,type,maptype,maplength,mval1,mval2,mval3,mval4:integer; magic,magicno:real; titlestr,conpbm,str,txtstr:string; dtime,year, month,day,hour, minute,second,dayofweek:integer begin width:=256; height:=1; offset:=0; slices:=1; count :=0; dtime:=0; tstatus:=0; SetImport('8-bit'); SetCustom(width,height,offset,slices); Import(''); {Read in header as an image, prompting for file name.} titlestr := Windowtitle; txtstr := concat(titlestr,' Rasterfile Info'); { Test for Sun Raster file Magic Number. Magic Number is 1504078485 decimal. } TestMagicNumber; longx := 4; longy := 0; GetlongValue; width := longvalue; longx := 8; longy := 0; GetlongValue; height := longvalue; longx := 12; longy := 0; GetlongValue; depth := longvalue; longx := 16; longy := 0; GetlongValue; alength := longvalue; longx := 20; longy := 0; GetlongValue; type := longvalue; longx := 24; longy := 0; GetlongValue; maptype := longvalue; longx := 28; longy := 0; GetlongValue; maplength := longvalue; { List File parameters } NewTextWindow(txtstr,450,150); Writeln('Image Width: ',width:10:0); Writeln('Image Height: ',height:10:0); Writeln('Image Depth in bits: ',depth:4:0); Writeln('Image Size (bytes): ',alength:21:0); Writeln('Rasterfile type: ',type:21:0); Writeln('Color map type: ',maptype:21:0); Writeln('Color map length: ',maplength:21:0); { Write out the type of data} if (type = 0 ) then begin Writeln('Rasterfile is RT_OLD. Raw pixrect image in 68000 byte order .'); end; if (type = 1 ) then begin Writeln('Rasterfile is RT_STANDARD. Raw pixrect image in 68000 byte order.'); end; if (type > 1) then begin SelectWindow(titlestr); Dispose; end; if (type = 2 ) then begin Writeln('Rasterfile is RT_BYTE_ENCODED. Run-length compression of bytes .'); Writeln('RT_BYTE_ENCODED format not supported'); PutMessage('RT_BYTE_ENCODED format not supported.'); Exit; end; if (type = 3 ) then begin Writeln('Rasterfile is RT_FORMAT_RGB. XRGB or RGB instead of XBGR or BGR '); Writeln('RT_FORMAT_RGB format not supported'); PutMessage('RT_FORMAT_RGB format not supported'); Exit; end; if (type = 4 ) then begin Writeln('Rasterfile is RT_FORMAT_TIFF. tiff <-> standard rasterfile '); Writeln('RT_FORMAT_TIFF format not supported'); PutMessage('RT_FORMAT_TIFF format not supported'); Exit; end; if (type = 5 ) then begin Writeln('Rasterfile is RT_FORMAT_IFF. iff (TAAC format) <-> standard rasterfile '); Writeln('RT_FORMAT_IFF format not supported'); PutMessage('RT_FORMAT_IFF format not supported'); Exit; end; if (maptype = 0 ) then begin Writeln('Rasterfile color map is RMT_NONE. ras_maplength is expected to be 0 '); SelectWindow(titlestr); Dispose; PutMessage('1 bit Binary rasterfile not supported'); Exit; end; if (maptype = 1 ) then begin Writeln('Rasterfile color map is RMT_EQUAL_RGB. red[maplength/3] ,green[], blue[]'); end; if (maptype = 2 ) then begin Writeln('Rasterfile color map is RMT_RAW.'); end; SelectWindow(titlestr); Dispose; { 32 byte offset for Sunraster Header and maplenght to access Image data.} offset:=32+maplength; if (type < 2) then begin SetImport('8-bit'); end; if (palette = 1) then begin SetCustom(maplength,1,32,1); Import(''); SetExport('Raw'); Export('rasterfile LUT'); SetImport('Palette'); Import('rasterfile LUT'); end; if (image = 1) then begin if (maptype = 1) then begin SetCustom(maplength,1,32,1); Import(''); SetExport('Raw'); Export('rasterfile LUT'); Dispose; end; SetCustom(width,height,offset,slices); Import(''); if (maptype = 1) then begin SetImport('Palette'); Import('rasterfile LUT'); end; end; end; macro 'Import Sun raster files'; begin image:=1; palette:=0; importSunRaster; exit; macro 'Display Sun rasterfile Info'; begin image:=0; palette:=0; importSunRaster; exit; macro 'Extract Sun rasterfile Color Map'; begin image:=0; palette:=1; importSunRaster; exit; { This macro reads and displays as decimal the 1st 100 bytes of an image. This is useful for decoding a image file header. } { macro 'Read the first 100 Bytes '; var pix,ndim,slices,width,height,itype,offset,i:integer; begin width:=100; height:=1; offset:=0; slices:=1; SetImport('8-bit'); SetCustom(width,height,offset,slices); Import(''); {Read in header as an image, prompting for file name.} NewTextWindow('Pixel Data ',260,300); for i := 0 to 100 do begin pix := GetPixel(i,0); Writeln(' Byte #',i:3:0,' Value= ',pix:7:0); end; Exit; }