iDevGames Forums
can photoshop save a png with only alpha channel? - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: can photoshop save a png with only alpha channel? (/thread-8528.html)



can photoshop save a png with only alpha channel? - sefiroths - Jan 11, 2011 05:01 AM

i have a black and alpha image.
i use texture2d from apple (a bit modified, i report some code)
Code:
// Create a variable which will store the CGImageRef from the image which has been passed in
        CGImageRef image;        
        
        // Grab the CGImage from the image which has been passed in
        image = [aImage CGImage];
        
        // Check to make sure we have been able to get the CGImage from the Image passed in.  If not then
        // raise an error.  We don't want the application to continue as a missing image could create unexpected
        // results
        NSAssert(image, @"ERROR - Texture2D: The supplied UIImage was null.");
        
        // Check to see if the image contains alpha information by reading the alpha info from the image
        // supplied.  Set hasAlpha accordingly
        CGImageAlphaInfo info = CGImageGetAlphaInfo(image);
        BOOL hasAlpha = ((info == kCGImageAlphaPremultipliedLast) ||
                    (info == kCGImageAlphaPremultipliedFirst) ||
                    (info == kCGImageAlphaLast) ||
                    (info == kCGImageAlphaFirst) ? YES : NO);
        
        // Check to see what pixel format the image is using
        if(CGImageGetColorSpace(image)) {
            if(hasAlpha)
                pixelFormat = kTexture2DPixelFormat_RGBA8888;
            else
                pixelFormat = kTexture2DPixelFormat_RGB565;
        } else  //NOTE: No colorspace means a mask image
            pixelFormat = kTexture2DPixelFormat_A8;




switch(pixelFormat) {        
            case kTexture2DPixelFormat_RGBA8888:
                colorSpace = CGColorSpaceCreateDeviceRGB();
                data = malloc(height * width * 4);
                context = CGBitmapContextCreate(data, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
                CGColorSpaceRelease(colorSpace);
                break;
                
            case kTexture2DPixelFormat_RGB565:
                colorSpace = CGColorSpaceCreateDeviceRGB();
                data = malloc(height * width * 4);
                context = CGBitmapContextCreate(data, width, height, 8, 4 * width, colorSpace, kCGImageAlphaNoneSkipLast | kCGBitmapByteOrder32Big);
                CGColorSpaceRelease(colorSpace);
                break;
                
            case kTexture2DPixelFormat_A8:
                data = malloc(height * width);
                context = CGBitmapContextCreate(data, width, height, 8, width, NULL, kCGImageAlphaOnly);
                break;                
            default:
                [NSException raise:NSInternalInconsistencyException format:@"Invalid pixel format"];
        }
i want that the image loaded is of format: kTexture2DPixelFormat_A8, but i don't know how to achive this.
thanks


RE: can photoshop save a png with only alpha channel? - Skorche - Jan 11, 2011 07:34 AM

Save it as a greyscale image and load it as alpha. It's pretty much as simple as that.


RE: can photoshop save a png with only alpha channel? - sefiroths - Jan 11, 2011 10:10 AM

thanks for the quik reply! :-)


RE: can photoshop save a png with only alpha channel? - sefiroths - Jan 12, 2011 01:30 AM

perhaps i'm doing something wrong because the image displayed is not the same as i saved, so, i think that the image i'm allocating and loading has not the correct format...
i have saved grayscale.png and loaded as alpha, tried with bitmap and other format...
i'm trying with this method:
CGImageGetColorSpace-> The source color space for the specified bitmap image, or NULL if the image is an image mask
i need to create an image mask, tried with black and white bitmap, but the function returns like the image is a RGB (without alpha)
what kind of image can i pass to have CGImageGetColorSpace returns NULL (image mask)?

thanks a lot for help


RE: can photoshop save a png with only alpha channel? - AnotherJake - Jan 12, 2011 12:42 PM

I would highly recommend using SOIL instead. It is soooo much simpler to deal with than CG. You don't even have to compile it separately as a library, just include the source files in your project. There are a few errors that pop up at first, but they're easy to track down and adjust.


RE: can photoshop save a png with only alpha channel? - SethWillits - Jan 12, 2011 03:07 PM

Speaking of which, and at the risk of thread-jacking, I had trouble getting SOIL to compile. Right out of the box it failed immediately with some error of not being able to create a directory in the tmp folder. If you can get a clean download of SOIL building AnotherJake, I'd love to know the steps.


RE: can photoshop save a png with only alpha channel? - AnotherJake - Jan 12, 2011 03:31 PM

Ah right, I forgot about that. This post I wrote a while back should help.

The other way is to drag the SOIL source and header files directly into your Xcode project and track down and fix the minor errors it'll produce. That's how I originally started using it for iOS.

Alternatively, I have two custom pre-built fat libs, one for iOS and one for MacOS, which I'd be happy to send you if you'd prefer to try those instead. I can send my makefile and sources too if you'd like, but it'd be fair of me to warn you that my makefile is pretty messy Rasp


RE: can photoshop save a png with only alpha channel? - sefiroths - Jan 13, 2011 02:08 AM

thanks for help!


RE: can photoshop save a png with only alpha channel? - sefiroths - Jan 20, 2011 01:49 AM

found another solution, i post it because i think it can be interesting and hopefully could help someone!
if there is no image can be an image mask then you can create one from that image however!
Code:
CGImageRef maskRef = [uiImage CGImage];
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
        CGImageGetHeight(maskRef),
        CGImageGetBitsPerComponent(maskRef),
        CGImageGetBitsPerPixel(maskRef),
        CGImageGetBytesPerRow(maskRef),
        CGImageGetDataProvider(maskRef), NULL, false);