Trouble loading and displaying with transparency, 8bit indexed bitmaps

Started by
5 comments, last by SelethD 9 years, 3 months ago

I am working on a project, that uses some legacy graphics, that are in an 8bit indexed bitmaps. These are the ones with a 1 byte pixel, pointing to an array of 256 colors.

So far, this is how I have been doing things.... (note, I'm using C#, with OpenTK, although a solution in C++ can be easily converted)


Bitmap bitmap = new Bitmap(path);
                       System.Drawing.Imaging.BitmapData textureData = bitmap.LockBits(
                           new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                           System.Drawing.Imaging.ImageLockMode.ReadOnly,
                           System.Drawing.Imaging.PixelFormat.Format8bppIndexed);

                        int srcmax = textureData.Height * textureData.Stride;
                        byte[] origBytes = new Byte[srcmax];
                        Marshal.Copy(textureData.Scan0, origBytes, 0, srcmax);

                        bitmap.UnlockBits(textureData);

this gives me the bitmap pixel data (the 1 byte index numbers) in textureData.

So now, I very slowly and very painfully, go through the data, as such....


                        //loop through all the pixels
                        byte[] data = new byte[(bitmap.Width * bitmap.Height) * 4];



                        int srcloc = 0;
                        int dstloc = 0;
                        byte index;
                        Color color;
                        for (int y = 0; y < textureData.Height; y++)
                        {
                            srcloc = y * textureData.Stride;
                            for (int x = 0; x < textureData.Width; x++)
                            {
                                index = origBytes[srcloc];
                                if (index == 0)
                                {
                                    data[dstloc] = 0;
                                    data[dstloc + 1] = 0;
                                    data[dstloc + 2] = 0;
                                    data[dstloc + 3] = 0;
                                }
                                else
                                {
                                    color = bitmap.Palette.Entries[index];
                                    data[dstloc] = color.R;
                                    data[dstloc + 1] = color.G;
                                    data[dstloc + 2] = color.B;
                                    data[dstloc + 3] = color.A;
                                }
                                dstloc += 4;
                                srcloc++;
                            }
                        }

as you can see, I am looking at the color, pointed to, by each pixel index... and manually reversing the red, and blue values, and also, setting alpha to 0, when the index number is 0. This converted data is then used to create an opengl texture, as follows...


                GL.Enable(EnableCap.Texture2D);
                uint textureID = 0;
                GL.GenTextures(1, out textureID);
  
                GL.BindTexture(TextureTarget.Texture2D, ret.ID);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (float)TextureWrapMode.Clamp);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (float)TextureWrapMode.Clamp);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.Nearest);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMagFilter.Nearest);
                GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, width, height, 0,
                    PixelFormat.Rgba, PixelType.UnsignedByte, pixelData);

                GL.Disable(EnableCap.Texture2D);
                

Now... this totally works, I end up with a rgba opengl texture2d, with transparency that can be used to texture a quad and it looks perfect.... only problem is the super slow speed of going through each pixel, as some of these images are > 800x800

So... If anyone knows of a better solution, something faster, or if you know of anything I seem to be doing wrong (opengl wise, as I am an opengl noob) please let me know.

Thanks

Advertisement

Are you sure it is "super slow"? However, some thoughts:

1.) One thing is to avoid the in-loop case distinction. If Palette.Entries[0] would store {0,0,0,0} then index ==0 would be handled as any other index, and the if-clause could be eliminated.

2.) Another thing is perhaps the possibility to reduce the 4x1-byte writes to 1x4-bytes write, but I'm not sure; perhaps modern hardware / compilers can hide that anyway. Maybe somebody else can comment on this attempt ...

3.) If this step is in time critical code, then move it out into a pre-processing step if possible.

Thanks so much for your input.

Well, the case with Palette.Entries[0], is that it's alpha value is non 0, and I really don't care what color the pixels are for index 0, as long as it is transparent. I cant edit the original images, unless there is some magical opengl, trick to mass convert data or something, I'm afraid I'll have to check and set to alpha 0 each time I hit an index 0 byte.

I see what you mean, as far as writing 1x4 bytes, not sure if this would be a speed increase, but its something I will try, just to see if I get different results.

It is a time critical piece of code, and I do have it in a 'title/loading' screen, its just.... slow loading.

Now you mention, am I sure its 'super slow'... yeah, it kinda is... and actually that is a bit surprising, even for an 800x800 image... I would think it would be faster on a modern computer. So, I'm wondering if the part that is taking up so much time, might be the part of the code where I am reading in the Bitmap, with LockBits, and copying its data.

I plan to keep trying and experimenting

If anyone has any other input or have used 8bit indexed bitmaps.... please share some secrets, thanks.

It is definately the 'looping' through the indexes that is causing the slow operation.

I have been searching and searching online for a solution, and came across something interesting...

I've seen


GL.TexImage2D(TextureTarget.Texture2D, 0, 
                    PixelInternalFormat.Rgba, 
                    bitmap.Width, bitmap.Height, 0,
                    PixelFormat.ColorIndex, PixelType.UnsignedByte, data);

now, the PixelFormat is ColorIndex.... this is what I need... however, when I use this, I get pure black images, no transparency...

So, I think I must be missing the 'color palette' somewhere... I'm trying to discover, how to let opengl know the data of the colorpalette tha the ColorIndex will look at.

I also read this...

"Similarly, to offset the color indices of a bitmap to the palette entries you have defined for it, use

glPixelTransferi(GL_INDEX_OFFSET, bitmap_entry);"

it says 'the palette entries you have defined', and im assuming this is 'bitmap_entry', but nothing shows 'HOW' to define the palette entries

So... I'm feeling more lost than ever, surely surely someone has used indexed graphics, and knows how to do this, but the lack of information on the web is troubling, i'm thinking, is it even possible anymore?

Thanks for any help

I think this line makes your code slow:


color = bitmap.Palette.Entries[index];

Because this says: "This property returns a copy of the ColorPalette object used by this Image."

Maybe just grab the palette/entries first so you can access them directly?

Derp


Well, the case with Palette.Entries[0], is that it's alpha value is non 0, and I really don't care what color the pixels are for index 0, as long as it is transparent. I cant edit the original images, unless there is some magical opengl, trick to mass convert data or something, I'm afraid I'll have to check and set to alpha 0 each time I hit an index 0 byte.

Make a copy of the palette in an own byte array and set the entry at index 0 accordingly. That has nothing to do with OpenGL.


now, the PixelFormat is ColorIndex.... this is what I need... however, when I use this, I get pure black images, no transparency...
So, I think I must be missing the 'color palette' somewhere... I'm trying to discover, how to let opengl know the data of the colorpalette tha the ColorIndex will look at.

Color index mode has been deprecated in 2009 with OpenGL 3.0. You should not use it any more.


Because this says: "This property returns a copy of the ColorPalette object used by this Image."

Oh yes, a single access would be better then.

Its too bad there is no apparent 'native support' for 8bit images in opengl anymore. However, I did not know it was getting a copy of the entire palette on each call, I tried to suggestion of making one copy to an array, and using the array, and the results were 100% faster.

I cant even tell how long its taking to load, as it zips through the files.

Thanks so much.

This topic is closed to new replies.

Advertisement