5 Replies Latest reply on Mar 22, 2010 9:39 PM by Sashikala Balasubramanian

    TIFF file with LZW compression

    Sashikala Balasubramanian Level 1

      Is there any method to check whether the TIFF file has been saved with LZW compression and IBM PC.

       

      regards,

      Sashi

        • 1. Re: TIFF file with LZW compression
          Michael L Hale Level 5

          The first two bytes of a tiff file will tell you the byte order. "II" or 4949 hex for ibm and "MM" or 4D4D hex for mac.

           

          Exiftools might be the eaiest way to determine the compression type.

          1 person found this helpful
          • 2. Re: TIFF file with LZW compression
            Sashikala Balasubramanian Level 1

            Thanks for your reply. Is Exiftools a third party software to check the compression?


            Is there any method in Photoshop scripting to check the compression.

             

            regards,

            Sashi

            • 3. Re: TIFF file with LZW compression
              Sashikala Balasubramanian Level 1

              Is there any method to check whether the tiff file is saved with LWZ compression.

               

              regards,

              Sashi

              • 4. Re: TIFF file with LZW compression
                Michael L Hale Level 5

                I still think that calling exiftools from your script would be the best way to read tiff data. I don't know of anyway to determine the compression used from a Photoshop script without reading the data from the tiff file someway.

                 

                But if you don't want to use exiftools below is a script that I adapted from a simple tiff reader I wrote several years ago. With my limited testing it does seems to read the compression used by Photoshop.

                 

                // =================================================================
                // Robert Stucky's hashtable
                // =================================================================
                Hashtable = function( caseSensitive ) {
                this.caseSensitive = caseSensitive || false;
                this.keyList = new Array( 0 );
                this.keyIndex = {};
                this.processedKeyList = new Array( 0 );
                this.count = 0;
                }
                Hashtable.prototype._getKey = function( name ) {
                return this.caseSensitive ? new String( "htx%_" + name ) : new String( "htx%_" + name ).toLowerCase();
                }
                Hashtable.prototype.clone = function() {
                var t = new Hashtable( this.caseSensitive );
                for ( var i = 0; i < this.keyList.length; i++ ) {
                t.putValue( this.keyList[ i ], this.getValue( this.keyList[ i ] ) );
                }
                return t;
                }
                Hashtable.prototype.merge = function( ht ) {
                for ( var i = 0; i < ht.keyList.length; i++ ) {
                this.putValue( ht.keyList[ i ], ht.getValue( ht.keyList[ i ] ) );
                }
                }
                Hashtable.prototype.isUniqueKey = function( key ) {
                return this.keyIndex[ key ] == undefined;
                }
                Hashtable.prototype.putValue = function( name, value ) {
                if ( ( value instanceof Boolean ) || ( typeof value == "boolean" ) ) {
                value = value ? "true" : "false";
                }
                var key = this._getKey( name );
                this[ key ] = value;
                if ( this.isUniqueKey( key ) ) {
                this.keyIndex[ key ] = this.keyList.length;
                this.keyList.push( name );
                this.count++;
                } else {
                this.keyList[ this.keyIndex[ key ] ] = name;
                }
                }
                Hashtable.prototype.keys = function() {
                return this.keyList;
                }
                Hashtable.prototype.getValue = function( name ) {
                var val = this[ this._getKey( name ) ];
                if ( ( val == "true" ) || ( val == "false" ) ) {
                val = eval( val );
                }
                return val;
                }
                Hashtable.prototype.getCount = function() {
                return this.count;
                }
                Hashtable.prototype.remove = function( name ) {
                if ( this.getValue( name ) ) {
                var key = this._getKey( name );
                var idx = this.keyIndex[ key ];
                this.keyIndex[ key ] = undefined;
                this[ key ] = undefined;
                this.keyList.splice( idx, 1 );
                this.count--;
                }
                }
                Hashtable.prototype.toString = function() {
                return "[object Hashtable] " + this.keyList;
                }
                // ========================================================================
                // These File functions are apapted from functions
                // in Xbytor's Stream.js http://www.ps-scripts.com/bb/viewtopic.php?t=369
                // ET is the byte order. Use 'BE' for Mac. The default byte order is Intel.
                // Function names come from Tiff file format data types.
                // ========================================================================
                File.prototype.readByte = function() {//aka unsigned byte
                  return this.read(1).charCodeAt(0);
                };
                
                File.prototype.readSByte = function(){//signed byte -128 to 127
                var sb = this.readByte();
                  if(sb>0x7F){
                    sb= 0xFFFFFF00^sb;
                  };
                };
                 
                File.prototype.readShort = function(ET) { //aka unsigned short, word = 2 bytes
                  var self = this;
                  var b1 = this.readByte();
                  var b2 = this.readByte();
                  if(ET == "BE"){
                   return (b1 << 8) + b2;
                  }else{
                     return (b2 << 8) + b1;
                     };
                };
                
                File.prototype.readSShort = function(ET){//signed short -32768 to 32767 = 2 bytes
                  var ss = this.readShort();
                   if(ss>0x7FFF){
                      ss = 0xFFFF0000^ss;
                   };
                  return ss;
                };
                
                File.prototype.readLong = function(ET) {//aka unsigned long = 4 bytes
                  var self = this;
                  var s1 = self.readShort(ET);
                  var s2 = self.readShort(ET);
                  if(ET == "BE"){
                     return (s1 << 16) + s2;
                  }else{
                     return (s2 << 16) + s1;
                  }
                };
                
                File.prototype.readSLong = function(ET){//signed long -2147483648 to 2147483647 = 4 bytes
                   var sl = this.readLong();
                   if(sl>0x7FFFFFFF){
                      sl = 0x00000000^sl;
                   };
                };
                //==============================================================================================
                // This gets all the tags in a tif file and stores them for later
                // Tests for valid Tiff format
                // getTifTags(File Object)
                //==============================================================================================
                function getTifTags(fileRef){
                var tags = new Hashtable();
                var tag = 0;
                var dataType = 0
                var dataLength = 0
                var tagData = 0
                var pointer = 0;
                
                fileRef.open( "r" );
                fileRef.encoding  = 'BINARY'
                var ET = fileRef.readShort();// First two bytes of tif file denotes encoding
                if(ET != 0x4949 && ET != 0x4D4D ){// Needs to be "II" or "MM" to be vaild
                   alert("Not a valid tiff file");
                   return;
                }else{
                   if(ET == 0x4D4D){
                      ET = 'BE';
                   };
                };
                if(fileRef.readShort(ET) != 42){// Next two byte must = 0x002A to be vaild
                   alert('Not a valid tiff file');
                   return;
                };
                pointer = fileRef.readLong(ET);//get offset to first IFD
                fileRef.seek(pointer,0);//move to IFD pointed to
                var numberOfTags = fileRef.readShort(ET);
                var tagsToRead = numberOfTags;
                tags.putValue('filename',fileRef.name);
                tags.putValue('Number of tags',numberOfTags);// for sanity check against tagDeatails.getCount
                while(tagsToRead >= 1 ){
                   var tagDetails = new Hashtable();
                   tag = fileRef.readShort(ET);
                   tagDetails.putValue('dataType',fileRef.readShort(ET));
                   tagDetails.putValue('dataLength',fileRef.readLong(ET));
                   tagDetails.putValue('tagData',fileRef.readLong(ET));
                   tags.putValue(tag,tagDetails);
                   tagsToRead = tagsToRead - 1;
                };//end search
                fileRef.close;
                return tags;
                };
                // =============================================================
                // throwError from Xbytor
                // =============================================================
                throwError = function(e) {
                  throw e;
                };
                
                // =============================================================
                // End of Functions
                // =============================================================
                
                
                var fileRef = File.openDialog("Select an Tiff File to Read", "TIFF Files: *.tif,*.TIF");
                if(fileRef == null){throwError('');};// User knows they canceled dialog
                
                var fileTags = getTifTags(fileRef);
                if(fileTags == undefined){throwError('Something went wrong');};
                
                var compressionTag = fileTags.getValue(259);// tag number for file compression
                if(compressionTag == undefined){throwError('Tag not found');};
                 
                var compressionValue = compressionTag.getValue('tagData');
                if( compressionValue == undefined )
                {throwError('Data not found');};
                switch( compressionValue ){
                     case 1:
                          var compressionType = "none";
                          break;
                     case 5:
                          var compressionType = "lzw";
                          break;
                     case 7:
                          var compressionType = "jpg";
                          break;
                     case 8:
                          var compressionType = "zip";
                          break;
                     default: var compressionType = "unknown";
                }
                alert( compressionType );
                
                1 person found this helpful
                • 5. Re: TIFF file with LZW compression
                  Sashikala Balasubramanian Level 1

                  Thank you for such a prompt reply. I will try this.

                   

                  regards,

                  Sashi