Re: NSGLImage doesnt open large images
Re: NSGLImage doesnt open large images
- Subject: Re: NSGLImage doesnt open large images
- From: John Pannell <email@hidden>
- Date: Thu, 10 Mar 2005 20:32:21 -0700
Hi all-
I struggled with loading images into textures for quite a while... in retrospect, it seemed like all the necessary pieces were there in Apple sample code and on these lists, but not all in the same place at once. Here is the solution I have arrived at, based primarily on the Apple GLImage sample object (the only way this will make any sense is if you have already been working with Apple's sample code and the GLImage object :-):
in the method
-(void)loadTextureFromFile:(NSString*)filePath; I do this:
...
NSImage *sourceImage = [[NSImage alloc] initWithContentsOfFile:filePath];
bmpRep = [[NSBitmapImageRep alloc] initWithData: [sourceImage TIFFRepresentation]];
imgWidth = [bmpRep pixelsWide];
imgHeight = [bmpRep pixelsHigh];
[self getTextureInfo];
...
The sample code loads the imageRep directly via
[[NSBitmapImageRep imageRepWithContentsOfFile:filePath] retain] , but I found this to be problematic later. All sample code seems to assume that images are 24 or 32 bit depth, but my app retrieves images off the web, where bit depths and color spaces and resolutions conspire to wreck things. By loading the data into NSImage and then pulling the TIFFRepresentation, some image irregularities are already taken care of for you.
In the
-(void)getTextureInfo method...
if([bmpRep hasAlpha]){
bpp = GL_RGBA;
pixfmt = GL_RGBA;
} else {
// need to be smart about image encoding - web images can be a mess
switch([bmpRep samplesPerPixel]){
case 1: // grayscale image
bpp = GL_LUMINANCE;
pixfmt = GL_LUMINANCE;
break;
case 2: // grayscale + alpha
bpp = GL_LUMINANCE_ALPHA;
pixfmt = GL_LUMINANCE;
break;
case 3:
bpp = GL_RGB;
pixfmt = GL_RGB;
break;
default:
bpp = GL_RGB;
pixfmt = GL_RGB;
break;
}
}
datatype = GL_UNSIGNED_BYTE;
I try to account for more possibilities that Apple's sample, allowing for the possibility of grayscale and the like.
In the
-(void)initGL method
...
// Enable texturing
glEnable(GL_TEXTURE_RECTANGLE_EXT);
// Enable client storage
glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, 1);
glPixelStorei(GL_UNPACK_ROW_LENGTH, [bmpRep bytesPerRow]/([bmpRep bitsPerPixel] >> 3)); //imgWidth
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Generate a texture ID
glGenTextures(1, &txID);
// Bind to our newly created texture ID
glBindTexture(GL_TEXTURE_RECTANGLE_EXT, txID);
// Specify the texture
glTexImage2D(GL_TEXTURE_RECTANGLE_EXT, 0, bpp, imgWidth, imgHeight, 0, pixfmt, datatype, [bmpRep bitmapData]);
...
Some notes:
GL_TEXTURE_RECTANGLE_EXT allows for non-power-of-2 images for textures. The setting of
GL_UNPACK_ROW_LENGTH was quite tricky for me. Example code and common sense tells me that imgWidth would suffice, but certain images with strange bit depths and other magic would steadfastly resist classification by imgWidth. A cocoadev wiki entry clued me into the math to calculate the actual width of the data in one row of the image...
[bmpRep bytesPerRow]/([bmpRep bitsPerPixel] >> 3) . Finally, I believe the sample this was based on was missing
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); but other Apple samples had it. It is needed.
Hopefully this makes sense and is of help. It did resolve all of my image distortion issues! It, of course, does nothing to help the original post's concern of large image sizes. As others have mentioned, multiple textures will be needed to overcome that limitation.
John
On Mar 10, 2005, at 5:28 PM, Pradeep Kumar wrote:
Hi John
Yes, there is a query for this. I get a value 2048. So whenever the image size is greater than
2048 pixs in width or height the image doesn't display. However there are some images
which are less than this size which doesnt display properly. Although openGL doesn't report
any error for such images, it displays garbled up. It's like there is some difference in the
parameters between the image and the parameters that are used by openGL. It's like some
kind of mismatch in the row bytes.
I am reconsidering the use of openGL for displaying images because of these issues unless
someone has a way to solve this problem.
--
P
On Fri, 11 Mar 2005 John Stiles wrote :
>My guess would be that you're going over the maximum texture size.
>OpenGL doesn't let you create arbitrarily large textures; there's a limit. I'm pretty sure the
limit is queryable.
>
>
>On Mar 10, 2005, at 12:31 PM, Pradeep Kumar wrote:
>
>> Hi
>>
>> I have been trying to use NSGLImage sample code provided by Apple. It works fine for
small
>> images (less than 2300pixs width). But for larger images (> 2400 pixs) it gives an error
>> WARNING: OpenGL Error 0x501 while loading texture with glTexImage2D()
>>
>> The view doesnt display the image. The image is a JPEG image taken using a 5MP
digital
>> camera.
>>
>> Does anyone know why this is happening? I am new to openGL.
>>
>> TIA
>> prady
>>
>>
>> <inbox.gif> _______________________________________________
>>Do not post admin requests to the list. They will be ignored.
>>Cocoa-dev mailing list (email@hidden)
>>Help/Unsubscribe/Update your Subscription:
>>
>>This email sent to email@hidden
>
<image.tiff> _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden