Frage

What exactly does System.Drawing.Imaging.PixelFormat.Alpha represent anyone? Look like this is similar to OpenGL.GL_APHA which is depreciated.

I am trying to convert a line of Java code that looks like this:

  TextureData textureData = new TextureData(GL.GL_ALPHA, size, size, 0, GL.GL_ALPHA,
            GL.GL_UNSIGNED_BYTE, false, false, false, textureBytes.rewind(), null);

to its C# represenation.

I have defined a class to encapsulate Pixel format to allow me go back and forth between DirectX and OpenGL as follows:

  public class PixelFormat
    {
        public static readonly PixelFormat R8 = new PixelFormat(8, 0, DataType.UnsignedByte);
        public static readonly PixelFormat R5G5B5A1 = new PixelFormat(5, 1, DataType.UnsignedShort);

        public bool IsColorPremultipliedByAlpha { get; private set; }
        public bool IsCompressed { get; private set; }
        public int Format { get; set; }
        public double Gamma { get; private set; }
        public int BitsPerPixel { get { return NumberOfRedBits + NumberOfGreenBits + NumberOfBlueBits; }}

        public ushort NumberOfRedBits { get; private set; }
        public ushort NumberOfGreenBits { get; private set; }      
        public ushort NumberOfBlueBits { get; private set; }
        public ushort NumberOfAlphaBits { get; private set; }
        public int IndexIntoColorPallete { get; set; }
        public bool IsIndexed { get; set; }
        public DataType DataType { get; set; }
        public IndexedColorFormat IndexedColorFormat { get; set; }

        public OpenglPixelAttributes GetOpenglConstant()
        {
            return null;
        }      
    }    

I will like to represent System.Drawing.Imaging.PixelFormat.Alpha or its OpenGL cousin using an instance of my PixelFormat class.

Anyone ideas?

War es hilfreich?

Lösung

GL_ALPHA really just boils down to a single-component texture where the single component is taken to mean the alpha channel. If you store the image data in a non-deprecated internal format (e.g. GL_R8), you can write your own Get method that does conversion into RGBA (assuming you actually need this).

The thing is, I do not see anywhere in your proposed PixelFormat class right now that actually exposes the internal representation of your data. About the closest this code ever comes to having a lack of GL_ALPHA make a difference is the ability to query the storage per-component.


To answer your original question, GL_ALPHA represents this:

vec4 (0.0, 0.0, 0.0, alpha)

If you want to use a GL_R8 image format to replace GL_ALPHA, you can do this in a shader:

vec4 (0.0, 0.0, 0.0, red)

You can just as easily do the same thing on the CPU side of things if you ever need to return an RGBA image from your Alpha pixel format. Otherwise, just return an array of pixels where RedBits = 0, GreenBits = 0, BlueBits = 0 and AlphaBits = .... This will amount to an array of alpha values, and while GL would consider each one of those pixels to be a red color component, that hardly matters here.

TL;DR: There will only be a handful of places where the fact that modern GL interprets single-component image formats as red even matters. Since enough of the pipeline is programmable these days it makes no sense to have GL handle alpha image formats specially. The same goes for luminance, all of this can be accomplished with swizzles in a shader.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top