PSD and TIFF transparent color vs alpha channel

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: PSD and TIFF transparent color vs alpha channel

Post by Drarakel »

fmw42 wrote:Thus the unnassociated.
You mean "associated"? test1.tif has "Associated alpha".

And again: test1.tif has a real alpha layer! You can see the RGBA values even in a hex editor.
SamplesPerPixel in test1.tif is 4, BitsPerSample is "8 8 8 8", and since it's RGB, there has to be a description for the fourth channel in ExtraSamples ("associated").
'TIFFTransparentBackgroundNoAlphas.tif' has none of this. It has only three channels. No need for an ExtraSamples tag. (And as I said, I would suspect, that this tag doesn't make any difference for ImageMagick. It's probably only reported in the verbose info if it's found.)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PSD and TIFF transparent color vs alpha channel

Post by fmw42 »

Drarakel wrote:
fmw42 wrote:Thus the unnassociated.
You mean "associated"? test1.tif has "Associated alpha".
No, when I look at the verbose info, I see

tiff:alpha: unassociated
Drarakel wrote:And again: test1.tif has a real alpha layer! You can see the RGBA values even in a hex editor.
I created it in PS with only background transparency, not an alpha layer. But IM cannot distinquish both at the same time. So it takes the background transparency and put it in IM's alpha channel. That is why you see the alpha values.
Drarakel wrote:SamplesPerPixel in test1.tif is 4, BitsPerSample is "8 8 8 8", and since it's RGB, there has to be a description for the fourth channel in ExtraSamples ("associated").
'TIFFTransparentBackgroundNoAlphas.tif' has none of this. It has only three channels. No need for an ExtraSamples tag. (And as I said, I would suspect, that this tag doesn't make any difference for ImageMagick. It's probably only reported in the verbose info if it's found.)
I don't know why TIFFTransparentBackgroundNoAlphas.tif does not have the transparency as it shows in Photoshop. I assumed it was because the tiff:alpha: unassociated tag was not there, so IM detected no transparency. That was why I created test1.tif, the same way, I assumed, the first was created, but just with an older version of PS. But I am not sure how TIFFTransparentBackgroundNoAlphas.tif was really created. I have to talk to my client. That was why I made the comment about PS possibly changing the way it reported background transparency.
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: PSD and TIFF transparent color vs alpha channel

Post by Drarakel »

fmw42 wrote:No, when I look at the verbose info, I see

tiff:alpha: unassociated
Drarakel wrote:but identify shows it as "unassociated", even when it's "associated"
Use ExifTool - or an older IM version - to identify that correctly.
fmw42 wrote:I created it in PS with only background transparency, not an alpha layer.
But your PS obviously stored it with an alpha layer.
I will try to convince you once more: :wink:

Code: Select all

convert test1.tif -crop 10x1+86+86 +repage txt:-
# ImageMagick pixel enumeration: 10,1,255,rgba
0,0: (162,144,134,255)  #A29086  rgba(162,144,134,1)
1,0: (164,139,131,255)  #A48B83  rgba(164,139,131,1)
2,0: (168,152,150,255)  #A89896  rgba(168,152,150,1)
3,0: (169,152,142,255)  #A9988E  rgba(169,152,142,1)
4,0: (166,147,136,255)  #A69388  rgba(166,147,136,1)
5,0: (156,129,122,255)  #9C817A  rgba(156,129,122,1)
6,0: (144,120,111,255)  #90786F  rgba(144,120,111,1)
7,0: (136,111,105,255)  #886F69  rgba(136,111,105,1)
8,0: (142,118,113,255)  #8E7671  rgba(142,118,113,1)
9,0: (142,122,103,255)  #8E7A67  rgba(142,122,103,1)
Hex values in 'test1.tif' at offset 0x1DF7E:

Code: Select all

A2 90 86 FF A4 8B 83 FF A8 98 96 FF A9 98 8E FF A6 93 88 FF 9C 81 7A FF 90 78 6F FF 88 6F 69 FF 8E 76 71 FF 8E 7A 67 FF
(By the way: I now think that there is a difference in the handling of the alpha layer in IM - depending on the value "associated" or "unassociated". With "associated" alpha, the values of the 'normal' channels are usually stored with premultiplied alpha, so IM has to convert the pixel values to get the values without alpha - with small rounding errors. And when changing it in IM from "unassociated" to "associated" - or vice versa - the values in the 'normal' channels get converted, too. Well, all this doesn't make a difference if there's only binary transparency. :))

There are probably several possibilities for transparency that can't get used in ImageMagick. There could be several separate alpha layers (IM supports only the first alpha layer - doesn't it?). Or there can be additional data in proprietary tags - like ImageSourceData. I have TIF files where the ImageSourceData is bigger than the image itself. (Can't do anything with that additional data - as I don't have Photoshop.)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PSD and TIFF transparent color vs alpha channel

Post by fmw42 »

From my testing, if a PSD image has an alpha layer, then IM puts that into frame [0] as the alpha data. If it has a background transparency, then IM does not include that in frame[0], but creates a second frame [1], that is the size of the image inserted into the background. So you can look at frame [0] and [1] and compare sizes. If they are different, then you can use the virtual canvas with frame [1] to create a transparent background the size of frame [0]. That has worked well on the images I have tested from my client and from my tests. I suspect there will be other cases where it might fail as the tests so far have been fairly simple cases from my client.

However, TIFF files are not treated the same in IM. I wish they were. I am not sure if TIFFs can have an alpha layer as PSD does, since my PS CS does not allow the creation of alpha layers as far as I can tell - at least it does not label any layer with the word alpha. So I have only been able to test background transparency. IM failed to find it on my clients image for some reason I do not understand yet, but found if via the unassociated result in the verbose info of the file I created in PS CS (test1.tif), which was then converted in IM to an alpha channel.

That is the best understanding that I have so far of how IM handles transparency in PSD and TIFFs.
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: PSD and TIFF transparent color vs alpha channel

Post by Drarakel »

fmw42 wrote:So I have only been able to test background transparency. IM failed to find it on my clients image for some reason I do not understand yet, but found if via the unassociated result in the verbose info of the file I created in PS CS (test1.tif), which was then converted in IM to an alpha channel.
IM didn't have to convert anything with 'test1.tif" - it has already an alpha channel.
I'm just tryin' to prevent you from hunting phantoms... :wink:

OK, so today I tested it at a PC with Photoshop installed (CS3):
I loaded "TIFFTransparentBackgroundNoAlphas.tif" - there was transparency in PS.
I loaded a version of "TIFFTransparentBackgroundNoAlphas.tif" with deleted 'ImageSourceData' tag - no more transparency in PS.
So, I can assure you now: It IS 'ImageSourceData'.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PSD and TIFF transparent color vs alpha channel

Post by fmw42 »

IM didn't have to convert anything with 'test1.tif" - it has already an alpha channel.
I'm just tryin' to prevent you from hunting phantoms...
I still think you may misunderstand me. I did not change the alpha data, IM reformatted it (for its internal format) when it opened the file so that it converted the background transparency to an IM alpha channel.

When I created test1.tif in photoshop, I just created a background transparency and inserted an image, then saved it. There was no alpha layer. When you look at the verbose info in IM, it has an alpha channel as IM has taken the flag that says it has background transparency (as opposed to a PSD alpha layer) (where ever it finds it?) so that the background transparency is shown as alpha data in IM.

I also found that if in PS (CS), if one does not save the tif file by checking the save transparency in the tiff options save window, then IM does not find the transparency, even though PS can open the file and see the background transparency. If this is done, then the tiff:alpha unassociated tag does not show in IM. However, if one saves the file by checking the save transparency check box in PS, then IM can find the transparency and shows the tiff:alpha unassociated tag. That is why test1.tif shows transparency in IM and TIFFTransparentBackgroundNoAlphas.tif does not. Please feel free to verify this, as I would appreciate anyone confirming my conclusions.

It seems to me that PS may keep transparency info in the ImageSourceData for its own use, which explains why TIFFTransparentBackgroundNoAlphas.tiff can be opened in PS and still see the transparency, but it does not show in IM. However, IM seems to me to be using the tiff:alpha unassociated as a means of detecting transparency not the ImageSourceData.

You seem to find that if you delete the ImageSourceData then load it in PS, it does not show the transparency. That is consistent with what I think is happening, since PS can then use that to redisplay the background transparency. But I think that is not the relevant issue for IM, rather the tiff:alpha unassociated tag.

That is my best understanding (assumption) so far but I could be wrong.

But I do appreciate your help and advice. It has helped me. And I would certainly appreciate anyone verifying what I think I have deduced.

If you know of a tool that will allow one to be able to read the ImageSourceData as text rather than binary, I would appreciate knowing about it, as deducing all this is not as satisfactory as knowing from the PSD information. That way, perhaps one could even tell that TIFFTransparentBackgroundNoAlphas.tif actually has PS transparency, even though IM thinks otherwise.

Fred
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: PSD and TIFF transparent color vs alpha channel

Post by Drarakel »

I would say, that our positions don't differ that much. There are only differences in small details. You're repeating some small errors over and over again (sorry!) - but I see no point in refering to these anymore.. (Of course, there could be misunderstandings. Also, don't forget that English is not my mother tongue.)

Perhaps I should ask: What's your goal in this thread? (Apart from understanding the TIFF structures a bit better.)
If you want to use the existing ImageSourceData (without the usage of Photoshop) - well, that's probably not possible. (Or what did you mean with "ImageSourceData as text rather than binary"?) But personally, I don't have real experience in that area. We would have to find a TIFF/Photoshop expert to have clarity there.

You can find a small description of the main ImageSourceData fields in the document here:
http://partners.adobe.com/public/develo ... toshop.pdf
And some of the descriptions of the "Photoshop File Formats" can perhaps be used, too. The latter document can be found here, for example:
http://forums.adobe.com/message/2924224
But decoding 'ImageSourceData' that way surely would be.. a tough job. :)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PSD and TIFF transparent color vs alpha channel

Post by fmw42 »

I apologize if we seem at odds about some small details. I certainly am not an expert on file formats and especially PSD and TIFF. I do appreciate your help and advice. And your English writing is very good. I would not know it was not your native language.
Perhaps I should ask: What's your goal in this thread? (Apart from understanding the TIFF structures a bit better.)
The original issue was that a client asked me to write a script to have IM create preview jpg images that looked like what one sees in PS when one displays an image which has background transparency with or without an alpha channel. He thought that this would be rather easy. However, PS only displays the background transparency and does not show by default the alpha channel. My problem was that when I opened the various PSD examples in IM and tried to flatten them over a background checkerboard (to simulate what one sees in PS), then that did not usually work. This appears to be because in IM, layer [0] includes the alpha layer from the PSD and not the background transparency.

So I set about to do some testing to see what seems to be happening when IM gets the file with respect to what information it uses to detect background transparency. With PSD files, for the most part with my clients examples, I found that I could ignore the first IM layer [0] and flatten all the other layers over a checkerboard and that seemed to work reasonably well (at least for all his examples and few others I created in my PS CS), because in those layers, IM only retains the inserted image, but keeps the virtual canvas information. When there was only one IM layer, I could flatten it over the checkerboard (after turning off -alpha in IM so as to delete any PSD alpha layer) and that seemed to work. All of this is rather not very satisfying as I am only deducing what I think is happening and not using the same information that PS does.

However, when I looked at the TIFF examples, I found that IM did not make other layers as it did for the PSD files, but seemed to rely upon the tiff:alpha unassociated tag as explained before. So I could use that to determine if IM detects transparency (so I know whether to turn on or off IM -alpha before compositing against a checkerboard) because in that case IM takes the background transparency and puts it in the one layer it has. If the TIFF file is not saved with the save transparency checkbox checked, then IM does not recognize the transparency and says it is an opaque image; whereas PS can open the same file and shows background transparency. Again this is not very satisfying..

Thus PS seems to use something else in both PSD and TIFF files, which may be the ImageSourceData. So my goal was to try to get at that, once you pointed it out. But I have not been able to read that data even using exiftools. But perhaps I am not using the right arguments as I don't know that tool very well. Whether that will help me or not is still an open question.

I hope this helps explain my motivations and what I have done without being too verbose and thus confusing.

If you or anyone else has any good ideas how to proceed, I would be grateful for any suggestions. Other than that, I am left with my deductions and script as I have developed it to this point as I am not sure I can do anything further. As you say, it probably needs the help from some PS expert or someone who knows the PSD and TIFF formats much better than I. The client can decide if what I have works to his satisfaction or not. But it is better than what he had by simply by converting in IM the PSD and TIFF layer [0] to jpg.

As you said, this exercise has certainly made me more aware of the difficulties of working with PSD and TIFF files, but has provided some useful information at least to me.


Fred
Post Reply