Advertisement
Guest User

DM-Script to import FEI or ZEISS TIFF Images with calibr.

a guest
Mar 10th, 2016
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.33 KB | None | 0 0
  1.     // Auxilliary method for stream-reading of values
  2.     number ReadValueOfType(object fStream, string type, number byteOrder)
  3.     {
  4.         number val = 0
  5.         TagGroup tg = NewTagGroup()
  6.         if ( type == "bool" )
  7.         {
  8.             tg.TagGroupSetTagAsBoolean( type, 0 )
  9.             tg.TagGroupReadTagDataFromStream( type, fstream, byteOrder )
  10.             tg.TagGroupGetTagAsBoolean( type, val )
  11.         }
  12.         else if ( type == "uint16" )
  13.         {
  14.             tg.TagGroupSetTagAsUInt16( type, 0 )
  15.             tg.TagGroupReadTagDataFromStream( type, fstream, byteOrder )
  16.             tg.TagGroupGetTagAsUInt16( type, val )
  17.         }
  18.         else if ( type == "uint32" )
  19.         {
  20.             tg.TagGroupSetTagAsUInt32( type, 0 )
  21.             tg.TagGroupReadTagDataFromStream( type, fstream, byteOrder )
  22.             tg.TagGroupGetTagAsUInt32( type, val )
  23.         }
  24.         else Throw("Invalid read-type:"+type)
  25.         return val
  26.     }
  27.        
  28.     string ExtractTextFromTiff( string path )
  29.     {
  30.         string txt
  31.         if ( !DoesFileExist(path) ) Throw("File not found.\n"+path)
  32.        
  33.         // Open Stream
  34.         number fileID = OpenFileForReading( path )
  35.         object fStream = NewStreamFromFileReference(fileID,1)
  36.        
  37.         // Read data byte order. (1 = big Endian, 2= little Endian for Gatan)
  38.         number val
  39.         number byteOrder = 0
  40.         val = fStream.ReadValueOfType( "uint16", byteOrder )
  41.         byteOrder = ( 0x4949 == val  ) ? 2 : ( 0x4D4D == val ? 1 : 0 )
  42.         //Result("\n TIFF endian:"+byteOrder)
  43.        
  44.         // Verify TIFF image
  45.         val = fStream.ReadValueOfType( "uint16", byteOrder )
  46.         if ( val != 42 ) Throw( "Not a valid TIFF image" )
  47.        
  48.         // Browse all directories
  49.         number offset = fStream.ReadValueOfType( "uint32", byteOrder )
  50.        
  51.         while( 0 != offset )
  52.         {
  53.             fStream.StreamSetPos( 0, offset ) // Start of IFD
  54.             number nEntries = fStream.ReadValueOfType( "uint16", byteOrder )
  55.             for ( number e=0;e<nEntries;e++)
  56.             {
  57.                 number tag        = fStream.ReadValueOfType( "uint16", byteOrder )
  58.                 number typ        = fStream.ReadValueOfType( "uint16", byteOrder )
  59.                 number count      = fStream.ReadValueOfType( "uint32", byteOrder )
  60.                 number dataOffset = fStream.ReadValueOfType( "uint32", byteOrder )
  61.                 //Result("\n entry # "+e+": ID["+tag+"]\ttyp="+typ+"\tcount="+count+"\t offset @ "+dataOffset)
  62.                 if ( 2 == typ ) // ASCII
  63.                 {
  64.                     number currentPos = fStream.StreamGetPos()
  65.                     fStream.StreamSetPos( 0, dataOffset )
  66.                     string textField = fStream.StreamReadAsText( 0, count )
  67.                     txt+=textField
  68.                     fStream.StreamSetPos( 0, currentPos )
  69.                 }  
  70.             }
  71.             offset = fStream.ReadValueOfType( "uint32", byteOrder ) // this is 0000 for the last directory according to spec
  72.         }
  73.        
  74.         return txt
  75.     }
  76.    
  77.     String TruncWhiteSpaceBeforeAndAfter( string input )
  78.     {
  79.         string work = input
  80.         if ( len(work) == 0 ) return ""
  81.         while ( " " == left(work,1) )
  82.         {
  83.             work = right( work, len(work) - 1 )
  84.             if ( len(work) == 0 ) return ""
  85.         }
  86.         while ( " " == right(work,1) )
  87.         {
  88.             work = left( work, len(work) - 1 )
  89.             if ( len(work) == 0 ) return ""
  90.         }
  91.         return work
  92.     }
  93.  
  94.  
  95.     // INPUT:  String with line-wise information
  96.     // OUTPUT: TagGroup
  97.     // Assumptions:  
  98.     //  - Groups are specified in a line in the format:             [GroupName]
  99.     //  - The string contains information line-wise in the format:  KeyName=Vale
  100.     TagGroup CreateTagsFromString( string input )
  101.     {
  102.         TagGroup tg = NewTagGroup()
  103.         string work = input
  104.        
  105.         string eoL          = "\n"
  106.         string GroupLeadIn  = "["
  107.         string GroupLeadOut = "]"
  108.         string keyToValueSep= "="
  109.         string groupName = ""
  110.        
  111.         number pos = find(work,eoL )
  112.         while( -1 != pos )
  113.         {
  114.             string line = left(work,pos)
  115.             work = right(work,len(work)-pos-len(eoL))
  116.             number leadIn  = find(line,GroupLeadIn)
  117.             number leadOut = find(line,GroupLeadOut)
  118.             number sep = find(line,keyToValueSep)
  119.             if ( ( -1 < leadIn ) && ( -1 < leadOut ) && ( leadIn < leadOut ) ) // Is it a new group? "[GROUPNAME]"
  120.             {
  121.                 groupName = mid(line,leadIn+len(GroupLeadIn),leadOut-leadIn-len(GroupLeadOut))
  122.                 groupName = TruncWhiteSpaceBeforeAndAfter(groupName)
  123.             }
  124.             else if( -1 < sep )                                                 // Is it a value? "KEY=VALUE" ?
  125.             {
  126.                 string key  = left(line,sep)
  127.                 string value= right(line,len(line)-sep-len(keyToValueSep))
  128.                 key   = TruncWhiteSpaceBeforeAndAfter(key)
  129.                 value = TruncWhiteSpaceBeforeAndAfter(value)
  130.                 string tagPath = groupName + ( "" == groupName ? "" : ":" ) + key
  131.                 tg.TagGroupSetTagAsString( tagPath, value )
  132.             }
  133.             pos = find(work,eoL)       
  134.         }
  135.         return tg
  136.     }
  137.  
  138.     void ImportTIFFWithTags()
  139.     {
  140.         string path = GetApplicationDirectory("open_save",0)
  141.         if (!OpenDialog(NULL,"Select TIFF file",path, path)) exit(0)
  142.        
  143.         string extractedText = ExtractTextFromTiff(path)
  144.         /*
  145.         if ( TwoButtonDialog("Show extracted text?","Yes","No") )
  146.             result(extractedtext)
  147.         */
  148.        
  149.         tagGroup infoAsTags = CreateTagsFromString(extractedText )
  150.         /*
  151.         if ( TwoButtonDialog("Output tagstructure?","Yes","No") )
  152.             infoAsTags.TagGroupOpenBrowserWindow(path,0)
  153.         */
  154.        
  155.         // Import data and add info-tags
  156.         image imported := OpenImage(path)
  157.         imported.ImageGetTagGroup().TagGroupSetTagAsTagGroup("TIFF Tags",infoAsTags)
  158.         imported.ShowImage()
  159.  
  160.         // Calibrate image, if info is found
  161.         // It seems FEI stores this value as [m] in the tags PixelHeight and PixelWidth
  162.         // while ZEISS images contain the size of the FOV in the tags "Height" and "Width" as string including unit
  163.         number scaleX = 0
  164.         number scaleY = 0
  165.         string unitX
  166.         string UnitY
  167.         string hStr
  168.         string wStr
  169.         if ( imported.GetNumberNote( "TIFF Tags:Scan:PixelWidth", scaleX ) )
  170.         {
  171.             unitX = "nm"
  172.             scaleX *= 1e9
  173.         }
  174.         if ( imported.GetNumberNote( "TIFF Tags:Scan:PixelHeight", scaleY ) )
  175.         {
  176.             unitY = "nm"
  177.             scaleY *= 1e9
  178.         }
  179.         if ( imported.GetStringNote( "TIFF Tags:Width", wStr ) )
  180.         {
  181.             number pos = find( wStr, " " )
  182.             if ( -1 < pos )
  183.             {
  184.                 scaleX = val( left(wStr,pos) )
  185.                 scaleX /= imported.ImageGetDimensionSize(0)
  186.                 unitX  = right( wStr, len(wStr)-pos-1 )
  187.             }
  188.         }
  189.         if ( imported.GetStringNote( "TIFF Tags:Height", hStr ) )
  190.         {
  191.             number pos = find( hStr, " " )
  192.             if ( -1 < pos )
  193.             {
  194.                 scaleY = val( left(hStr,pos) )
  195.                 scaleY /= imported.ImageGetDimensionSize(1)
  196.                 unitY  = right( hStr, len(hStr)-pos-1 )
  197.             }
  198.         }
  199.         if ( 0 < scaleX )
  200.         {
  201.             imported.ImageSetDimensionScale(0,scaleX)
  202.             imported.ImageSetDimensionUnitString(0,unitX)
  203.         }
  204.         if ( 0 < scaleY )
  205.         {
  206.             imported.ImageSetDimensionScale(1,scaleY)
  207.             imported.ImageSetDimensionUnitString(1,unitY)
  208.         }
  209.     }
  210.     ImportTIFFWithTags()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement