NSImage initWithContentsOfURL missing alpha channel when loading TIFFs - macos

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"

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.

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.

Related

photoshop saving image without white background

i can't remove the white background when saving as png or jpeg.
tried everything what suggested me on the web, but still get the diminsions i set up as whitebackground.
How can i save image without the background?
see image below for settings.
enter image description here
if you want to remove all the white from the picture, better go to color range and sample white. layer via cut and delete the layer. and save it in png as #cybernatic.nomad said, supports transparency.
Make sure you have created your image on a new layer and delete (or hide) the first white layer. I just created a simple text on a new layer with a transparent background and then loaded the image back into Photoshop to confirm that it works properly. As per previous comment I use .png format to preserve transparency.

Set Colour Of Vector Image In Xcode 6+

I saw one of the Apple Videos mention that you can colour images via the code. All my searches on how to do this came up blank.
If I have a black vector image (pdf) saved inside Images.xcassets, how can I colour that image at run time?
Ideally it would be something simple like [UIImage setVectorColor:UIColorBlue] but I'm sure there could be more to it!
You have to set all vectors image as Template Image on Render options in the xassets. (http://i.stack.imgur.com/oTuDC.png)
After, you can set the color in the uiimageview which contain your image with method :
[imageView setTintColor:[UIColor redColor]];

Resizing a png image without losing tranparency

I am trying to save a png file in database using C#.
Bitmap thumbnail = new Bitmap(file.InputStream);
thumbnail = ImageUtilities.ResizeImage(thumbnail, Convert.ToInt32(width),Convert.ToInt32(height)); ImageUtilities.SaveJpeg(path,thumbnail,Convert.ToInt32(Resources.AppConstants.ExtractThumbnailQuality);
The image gets saved in database, but with a black background around the image. In short, it loses its transparency.
If I skip the resizing of image, instead of black background, a white background appears around the image.
Can anyone suggest, how to preserve the tranparency of the image
You are saving the resized image as JPEG. JPEG does not support transparency. Save it as PNG instead

Convert StdPicture Transparency Colour to White

I am using a CodeJock ImageManager component to hold a variety of images. I want to put one of these images into a FlexGrid Cell.
The Images I have are Png format and have transparent backgrounds so when I load the image into the grid like so:
Grid.Cell(flexcpPicture, 123, 4) = _
ImageManagerControl.Icons.GetImage(ImageNum, 16).CreatePicture(xtpImageNormal)
the background which in the original image was transparent is now black:
(the same happens if I load the image into a PictureBox using the above method)
According to the documentation CreatePicture returns an StdPicture object, is there some way I can convert this image (using BitBlt perhaps) so that the black is white? I'm not sure if this even possible?
I only need to do this with about three images so if I have to load them into an ImageList or something else first that would be ok.
I'm not sure if this helps at all, but I've been playing around with the PictureBox. I put two picture boxes on a form, put a bmp file (this only works with bmp files, so it might not be helpful to you), and did this:
Picture2.PaintPicture Picture1.Picture, 0, 0, opcode:=vbNotSrcCopy
Which successfully inverted the bitmap. Here are the RasterOp constants: http://msdn.microsoft.com/en-us/library/aa243035(v=vs.60).aspx

How to read the original alpha channel from PNG in J2ME?

I'm writing a simple J2ME game that uses PNG images with 8-bit alpha channel. Problem: not all hardware supports full alpha transparency rendering. However, since my game is pretty static in nature (at the beginning, "sprites" are layed out onto background image, based on current screen size, and that's about it), I thought it would be possible to prerender those transparent images directly onto background during game initialization and use that later in game. I can't prerender them in Photoshop as their positions are not known in advance.
But, it seems there is no way to read the original alpha channel on devices that do not support semi-transparency as it gets resampled during PNG loading. Is there some library that can help with that? Or is it a good idea to store alpha channels separately (e.g. as separate 8-bit PNG images) and manually apply them?
Thanks!
PNG Images also have transparency support if you want to create transparent image then you have read RGB data along with alpha channels and process alpha
Image transPNG=Image.createImage("/trans.png"); //load the tranparent image
int rgbData[];
transPNG.getRGB(rgbData, 0,transPNG.getWidth(), 0, 0,transPNG.getWidth(), transPNG.getHeight());
Image tranparentImage=Image.createRGBImage(rgbData, width, height, true); //process alpha
transPNG=null;
Above code shows how to create the transparent image and use.
I cant promise this will help, but you can try this way of reading the alpha channel using standard methods from Java util.
BufferedImage image = ImageIO.read(new File(name));
int[] alpha = new int[1]; //containg alpha-value for one pixel.
image.getAlphaRaster().getPixel(x, y, alpha);
System.out.println(alpha[0]); //gives the alpha value for x,y

Resources