4

In my app I use initWithContentsOfURL to load various types of image (JPEG, TIFF, PNG, GIF, etc) into an image, and then into an OpenGL texture.

The only type that loads an image with an alpha channel is png. (in the list above, only PNG and TIFF can contain alpha data.) If I try to load a .tiff image, it gets loaded without an alpha channel (the image's image rep reports alpha=NO, and it reports bitsPerPixel of 24.

I can edit an image with alpha in PS, save it as a PNG and a TFF, and the PNG loads in my program with alpha but the TIFF does not. Further, I can open the TIFF image in PS and confirm that it does have alpha data.

What am I missing here? Why are my TIFF images not loading with an alpha channel? And is there another appkit call I can make that WILL load my TIFF without dropping the alpha channel on the floor?

EDIT:

Since posting this question I've found that some 4-channel TIFFs load with alpha data and some do not. I have not yet figured out what workflow results in the different results.

This file loads with an alpha channel in Photoshop, but not if you load it in Cocoa using -[[NSImage alloc] initWithContentsOfURL]:

Image "Red Julia Seahorse crop"

A similar image that also has an alpha channel DOES load with alpha using the above Cocoa call:

Image "Transparent Seahorses"

Duncan C
  • 128,072
  • 22
  • 173
  • 272

2 Answers2

2

I just tried (OSX 10.9.4) and the image loaded complete with graduated transparency. The code I used is trivial:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
    NSURL *imageURL = [NSURL URLWithString: @"file:/Users/john/Desktop/test.tiff"];
    NSImage *image = [[NSImage alloc] initWithContentsOfURL: imageURL];
    self.imageView.image = image;
}

I created a TIFF using layers and without layers (two tests). Both worked. I tried a couple of different NSImageView backgrounds to verify the graduated transparency was genuine. (If you choose different Image View border styles in IB the background colour changes too).

I used Photoshop 12 (CS5) to create the images, and manually checked the 'Save Transparency' checkbox in the TIFF Options dialog when saving.

Hopefully something here helps you home in on your issue. From my tests it all works as expected.

John Cook
  • 117
  • 3
  • John, Thanks for responding. I am using exactly the same code you posted to load my TIFFs (`-[[NSImage alloc] initWithContentsOfURL]`). Since posting my question I have found some 4-channel TIFFS that do load with alpha. I edited my question to include sample images that do and do not work correctly. Very strange... – Duncan C Nov 21 '14 at 14:28
1

The issue is definitely with how the images were saved, most likely the TIFF options used.

Using your two images, the "Red Julia Seahorse Crop" image did not display with transparency, but the "Transparent Seahorses" displayed correctly with transparency.

I opened the "Red Julia Seahorse Crop" in Photoshop and re-saved the image (unchanged), but made sure the "Save Transparency" check box was selected in the "TIFF Options" dialog box. Once saved, that image now showed transparency correctly in the application.

screen shot TIFF options screen shot output from application

picciano
  • 22,341
  • 9
  • 69
  • 82
  • The "Save Transparency" setting in PS does indeed seem to make the difference. The really odd thing is that the image I posted displays with alpha if you load it in PS. Plus, I would swear that TIFFs I generated programmatically were not loading with alpha in Cocoa. I can't repeat it now though. – Duncan C Nov 21 '14 at 16:14
  • From what I can tell, the transparency is saved in the TIFF file in either case, but that the "Save Transparency" option sets a "flag" of sorts in the file that tells the consuming application whether to honor that transparency. I am probably butchering that explanation, but I think you know what I mean. – picciano Nov 21 '14 at 18:43