Axiom.Media.Bitwise.IntRead C# (CSharp) Method

IntRead() public static method

Read a n*8 bits integer value to memory in native endian.
public static IntRead ( IntPtr src, int n ) : uint
src System.IntPtr
n int
return uint
		public static uint IntRead( IntPtr src, int n )
		{
            unsafe
            {
                switch (n)
                {
                    case 1:
                        return ((byte*)src)[0];
                    case 2:
                        return ((ushort*)src)[0];
                    case 3:
#if BIG_ENDIAN
                    return ((uint)((byte*)src)[0]<<16)|
                            ((uint)((byte*)src)[1]<<8)|
                            ((uint)((byte*)src)[2]);
#else
                        return ((uint)((byte*)src)[0]) |
                                ((uint)((byte*)src)[1] << 8) |
                                ((uint)((byte*)src)[2] << 16);
#endif
                    case 4:
                        return ((uint*)src)[0];
                }
                return 0; // ?
            }
		}

Usage Example

        //           /** Unpack a color value from memory
        //              @param color	The color is returned here
        //              @param pf		Pixelformat in which to read the color
        //              @param src		Source memory location
        //           */
        //          protected static void UnpackColor(ref System.Drawing.Color color, PixelFormat pf,  IntPtr src) {
        //              UnpackColor(color.r, color.g, color.b, color.a, pf, src);
        //          }

        /** Unpack a color value from memory
         * @param r,g,b,a	The color is returned here (as byte)
         * @param pf		Pixelformat in which to read the color
         * @param src		Source memory location
         * @remarks     This function returns the color components in 8 bit precision,
         *    this will lose precision when coming from A2R10G10B10 or floating
         *    point formats.
         */
        unsafe public static void UnpackColor(ref byte r, ref byte g, ref byte b, ref byte a,
                                              PixelFormat pf, byte *src)
        {
            PixelFormatDescription des = PixelConverter.GetDescriptionFor(pf);

            if ((des.flags & PixelFormatFlags.NativeEndian) != 0)
            {
                // Shortcut for integer formats unpacking
                uint value = Bitwise.IntRead(src, des.elemBytes);
                if ((des.flags & PixelFormatFlags.Luminance) != 0)
                {
                    // Luminance format -- only rbits used
                    r = g = b = (byte)Bitwise.FixedToFixed((value & des.rmask) >> des.rshift, des.rbits, 8);
                }
                else
                {
                    r = (byte)Bitwise.FixedToFixed((value & des.rmask) >> des.rshift, des.rbits, 8);
                    g = (byte)Bitwise.FixedToFixed((value & des.gmask) >> des.gshift, des.gbits, 8);
                    b = (byte)Bitwise.FixedToFixed((value & des.bmask) >> des.bshift, des.bbits, 8);
                }
                if ((des.flags & PixelFormatFlags.HasAlpha) != 0)
                {
                    a = (byte)Bitwise.FixedToFixed((value & des.amask) >> des.ashift, des.abits, 8);
                }
                else
                {
                    a = 255; // No alpha, default a component to full
                }
            }
            else
            {
                // Do the operation with the more generic floating point
                float rr, gg, bb, aa;
                UnpackColor(out rr, out gg, out bb, out aa, pf, src);
                r = Bitwise.FloatToByteFixed(rr);
                g = Bitwise.FloatToByteFixed(gg);
                b = Bitwise.FloatToByteFixed(bb);
                a = Bitwise.FloatToByteFixed(aa);
            }
        }
All Usage Examples Of Axiom.Media.Bitwise::IntRead