Copy link to clipboard
Copied
Hi EveryOne!
Sorry for the same question again.
Is this possible to get transparency status on TIFF image using javascript? (or) is this possible via bridge script?
Whether it is checked ON/OFF( true / false ).
Any help or suggestion much appreciated.
-yajiv
I suspect this may be a fairly inefficient method, but you could give it a try:
// based on code by paul riggott;
// 2017, use it at your own risk;
var file = File.openDialog("Please select tif file.","tif File:*.tif");
main(file);
function main(file){
file.open("r");
file.encoding = 'BINARY';
filestring = file.read();
file.close();
if (filestring.indexOf("Transparency") != -1) {alert ("has transparency")}
};
Copy link to clipboard
Copied
I suspect this may be a fairly inefficient method, but you could give it a try:
// based on code by paul riggott;
// 2017, use it at your own risk;
var file = File.openDialog("Please select tif file.","tif File:*.tif");
main(file);
function main(file){
file.open("r");
file.encoding = 'BINARY';
filestring = file.read();
file.close();
if (filestring.indexOf("Transparency") != -1) {alert ("has transparency")}
};
Copy link to clipboard
Copied
I just wrote this code snippet which hopefully parses the TIFF file more reliably... Use it at your own risk...
// "TIFF - Wikipedia"
// <https://en.wikipedia.org/wiki/TIFF>
// "TIFF™ Revision 6.0 Final — June 3, 1992"
//
function main ()
{
function readInt (file, byteCount, bigEndian)
{
var bytes = file.read (byteCount);
var intValue = 0;
for (var index = 0; index < byteCount; index++)
{
intValue = (intValue << 8) + bytes.charCodeAt (bigEndian ? index : byteCount - index - 1);
}
return intValue;
}
//
function isTiffFile (f)
{
return (f.type === 'TIFF') || f.name.match (/\.tiff?$/i);
}
var tiffFilter =
(File.fs === "Macintosh") ?
function (f) { return (f instanceof Folder) || isTiffFile (f); } :
"TIFF Files:*.tif,All Files:*.*";
var tiffFile = File.openDialog ("Open TIFF file:", tiffFilter);
if (tiffFile)
{
var readonly = tiffFile.readonly; // Let's work around an odd Photoshop CS bug after a file selection dialog !!
if (tiffFile.open ('r'))
{
tiffFile.encoding = 'BINARY';
var byteOrder = tiffFile.read (2);
switch (byteOrder)
{
case 'II': // IBM PC
case 'MM': // Macintosh
var bigEndian = (byteOrder === 'MM');
var magicNumber = readInt (tiffFile, 2, bigEndian);
if (magicNumber === 42) // Arbitrary but carefully chosen number...
{
var hasTransparency = false;
var dirOffset = readInt (tiffFile, 4, bigEndian);
do
{
tiffFile.seek (dirOffset);
var dirCount = readInt (tiffFile, 2, bigEndian);
for (var dirIndex = 0; dirIndex < dirCount; dirIndex++)
{
var entryTag = readInt (tiffFile, 2, bigEndian);
var entryType = readInt (tiffFile, 2, bigEndian);
var entryCount = readInt (tiffFile, 4, bigEndian);
var entryValueOrOffset = readInt (tiffFile, 4, bigEndian);
if (entryTag === 338) // ExtraSamples
{
if (entryType === 3) // Short
{
var values = [ ];
var entryTypeSize = 2;
var entryValueSize = entryCount * entryTypeSize;
var saveOffset = tiffFile.tell ();
if (entryValueSize > 4)
{
tiffFile.seek (entryValueOrOffset);
for (var valueIndex = 0; valueIndex < entryCount; valueIndex++)
{
values.push (readInt (tiffFile, entryTypeSize, bigEndian));
}
}
else
{
tiffFile.seek (-4, 1); // Rewind 4 bytes
for (var valueIndex = 0; valueIndex < entryCount; valueIndex++)
{
values.push (readInt (tiffFile, entryTypeSize, bigEndian));
}
}
tiffFile.seek (saveOffset);
for (valueIndex = 0; valueIndex < values.length; valueIndex++)
{
if (values[valueIndex] === 1) // Associated alpha data (with pre-multiplied color)
{
hasTransparency = true;
}
}
}
else
{
throw new Error ("Invalid entry type (3 expected):\n" + entryType);
}
}
}
dirOffset = readInt (tiffFile, 4, bigEndian);
}
while (dirOffset);
alert ("Transparency:\n" + hasTransparency);
}
else
{
alert ("Unknown magic number:\n" + magicNumber);
}
break;
default:
alert ("Unknown byte order:\n" + byteOrder);
break;
}
tiffFile.close ();
}
else
{
alert ("Cannot open TIFF file.");
}
}
}
//
main ();
//
[Edit]: Case of extra standard alpha channels is now correctly taken into account.
HTH,
--Mikaeru
Copy link to clipboard
Copied
Thank you Chris,
Thank you Mikaeru69,
I have learned new thing from both of your knowledge.
I have question on mind, Same as is it possible to get Layer compression whether its Zip or RLE.
I tried but I am not able to find the solution. Any help or suggestion much appreciated.
-yajiv
Copy link to clipboard
Copied
One way is to look at the metadata.
#target photoshop;
main();
function main(){
var inputFolder = Folder.selectDialog ("Please select folder to process...");
if(inputFolder == null) return;
var myFiles = inputFolder.getFiles(/\.(tif)$/i);
for(var f = 0;f < myFiles.length;f++){
$.writeln(decodeURI(myFiles
.name) + " " + getTifCompession(myFiles )); }
};
function getTifCompession(file){
if (ExternalObject.AdobeXMPScript == undefined) ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
var xmpf = new XMPFile(file.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_READ);
var xmp = xmpf.getXMP();
try{
var comp = xmp.getProperty( XMPConst.NS_TIFF, "Compression" ).toString();
var result = '';
switch( Number(comp) ){
case 1: result = "none"; break;
case 5: result = "lzw"; break;
case 7: result = "jpg"; break;
case 8: result = "zip"; break;
default: result = "unknown";
}
return result;
}catch(e){return undefined;}
};
Copy link to clipboard
Copied
Hi SuperMerlin,
Thank you for the prompt response. The above coding is working fine and get the Image compression value (LZW, NONE,ZIP,JPEG).
However, I am asking about the Layer Compression whether ZIP or RLE.
-yajiv
Copy link to clipboard
Copied
Sorry I didn't pay attention!
I can't see a way of getting the information, I tried with a TIFF viewer TIFF Tag Viewer, Free Download
but that didn't show the required field. I wonder if Mikaeru69 knows ?
Copy link to clipboard
Copied
I just did a few investigations:
- When Layer Compression is either 'RLE' or 'ZIP', the tag 37724 gets defined (will appear in the variable entryTag in the above code).
- When Layer Compression is 'Discard', the tag 33723 is defined instead.
- According to the page TIFF tags, the tag 37724 is ImageSourceData​, privately used by Adobe Photoshop. It is described in the document Photoshop TIFF:
Adobe Photoshop specific image source data tag. This section has the layer and mask information found in a typical layered Photoshop file.
Field: ImageSourceData
Tag: 37724 (935C.H)
Type: UNDEFINED
Count: number of bytes for section
The section starts with a character string of "Adobe Photoshop Document Data Block" including the null termination character.
The remainder of the count, after subtracting the length of the above mentioned string, is a series of tagged data types in the following format:
- 4 bytes Signature ‘8BIM’
- 4 bytes Type various types (see below)
- 4 bytes Length length in bytes, variable for each type, padded to a 4 byte offset
The various types are mentioned here with further documentation in the Photoshop File Formats.pdf.
The available types are:
- ‘Layr’ Layer Data
- ‘LMsk’ User Mask Same as Global layer mask info table
- ‘Patt’ Pattern
- ‘Anno’ Annotations
- Now, all you have to do is to consult and understand the above-mentioned document Adobe Photoshop File Formats Specification to access the relevant information... BTW, there is also a Photoshop-specific TIFF Tags section listing all the specific tags.
Good luck!
--Mikaeru
Copy link to clipboard
Copied
Hi Mikaeru,
Thank you so much for your information about TIFF tags. Now I have a clear outline on the TIFF tags and function.
-yajiv
Copy link to clipboard
Copied
A Bridge script here: