public static OutputPixel OperationPerComponent(OutputPixel c1, OutputPixel c2, Func<byte, byte, byte> func) { return new OutputPixel() { R = func(c1.R, c2.R), G = func(c1.G, c2.G), B = func(c1.B, c2.B), A = func(c1.A, c2.A), }; }
public void SetPixel(int X, int Y, OutputPixel Color) { if (!IsValidPosition(X, Y)) return; var Position = PixelFormatDecoder.GetPixelsSize(GuPixelFormat, Y * Width + X); uint Value = this.ColorFormat.Encode(Color); switch (this.BitsPerPixel) { case 16: *(ushort*)&Address[Position] = (ushort)Value; break; case 32: *(uint*)&Address[Position] = (uint)Value; break; default: throw(new NotImplementedException()); } }
/* static public uint EncodePixel(GuPixelFormats PixelFormat, OutputPixel Color) { return ColorFormatFromPixelFormat(PixelFormat).Encode(Color.R, Color.G, Color.B, Color.A); } static public OutputPixel DecodePixel(GuPixelFormats PixelFormat, uint Value) { throw new NotImplementedException(); } */ public static void Decode(GuPixelFormats PixelFormat, void* Input, OutputPixel* Output, int Width, int Height, void* Palette = null, GuPixelFormats PaletteType = GuPixelFormats.NONE, int PaletteCount = 0, int PaletteStart = 0, int PaletteShift = 0, int PaletteMask = 0xFF, int StrideWidth = -1, bool IgnoreAlpha = false) { if (StrideWidth == -1) StrideWidth = GetPixelsSize(PixelFormat, Width); var PixelFormatInt = (int)PixelFormat; var PixelFormatDecoder = new PixelFormatDecoder() { _Input = Input, InputByte = (byte *)Input, InputShort = (ushort*)Input, InputInt = (uint*)Input, Output = Output, StrideWidth = StrideWidth, Width = Width, Height = Height, Palette = Palette, PaletteType = PaletteType, PaletteCount = PaletteCount, PaletteStart = PaletteStart, PaletteShift = PaletteShift, PaletteMask = PaletteMask, }; //Console.WriteLine(PixelFormat); switch (PixelFormat) { case GuPixelFormats.RGBA_5650: PixelFormatDecoder.Decode_RGBA_5650(); break; case GuPixelFormats.RGBA_5551: PixelFormatDecoder.Decode_RGBA_5551(); break; case GuPixelFormats.RGBA_4444: PixelFormatDecoder.Decode_RGBA_4444(); break; case GuPixelFormats.RGBA_8888: PixelFormatDecoder.Decode_RGBA_8888(); break; case GuPixelFormats.PALETTE_T4: PixelFormatDecoder.Decode_PALETTE_T4(); break; case GuPixelFormats.PALETTE_T8: PixelFormatDecoder.Decode_PALETTE_T8(); break; case GuPixelFormats.PALETTE_T16: PixelFormatDecoder.Decode_PALETTE_T16(); break; case GuPixelFormats.PALETTE_T32: PixelFormatDecoder.Decode_PALETTE_T32(); break; case GuPixelFormats.COMPRESSED_DXT1: PixelFormatDecoder.Decode_COMPRESSED_DXT1(); break; case GuPixelFormats.COMPRESSED_DXT3: PixelFormatDecoder.Decode_COMPRESSED_DXT3(); break; case GuPixelFormats.COMPRESSED_DXT5: PixelFormatDecoder.Decode_COMPRESSED_DXT5(); break; default: throw(new InvalidOperationException()); } if (IgnoreAlpha) { for (int y = 0, n = 0; y < Height; y++) for (int x = 0; x < Width; x++, n++) Output[n].A = 0xFF; } //DecoderCallbackTable[PixelFormatInt](Input, Output, PixelCount, Width, Palette, PaletteType, PaletteCount, PaletteStart, PaletteShift, PaletteMask); }
public static unsafe void Decode_RGBA_5551_Pixel(ushort Value, out OutputPixel OutputPixel) { #if true OutputPixel.R = (byte)(((Value >> 0) & 0x1F) * 255 / 0x1F); OutputPixel.G = (byte)(((Value >> 5) & 0x1F) * 255 / 0x1F); OutputPixel.B = (byte)(((Value >> 10) & 0x1F) * 255 / 0x1F); OutputPixel.A = (byte)(((Value >> 15) != 0) ? 255 : 0); #else OutputPixel.R = (byte)Value.ExtractUnsignedScale(0, 5, 255); OutputPixel.G = (byte)Value.ExtractUnsignedScale(5, 5, 255); OutputPixel.B = (byte)Value.ExtractUnsignedScale(10, 5, 255); OutputPixel.A = (byte)Value.ExtractUnsignedScale(15, 1, 255); #endif }
private unsafe void Decode_PALETTE_T4() { var Input = (byte*)_Input; if (Palette == null || PaletteType == GuPixelFormats.NONE) throw(new Exception("Palette required!")); OutputPixel[] PalettePixels; int PaletteSize = 256; PalettePixels = new OutputPixel[PaletteSize]; var Translate = new int[PaletteSize]; fixed (OutputPixel* PalettePixelsPtr = PalettePixels) { Decode(PaletteType, Palette, PalettePixelsPtr, PalettePixels.Length, 1); //Decode(PaletteType, Palette, PalettePixelsPtr, PaletteCount); } //Console.WriteLine(PalettePixels.Length); for (int n = 0; n < 16; n++) { Translate[n] = ((PaletteStart + n) >> PaletteShift) & PaletteMask; //Console.WriteLine(PalettePixels[Translate[n]]); } for (int y = 0, n = 0; y < Height; y++) { var InputRow = (byte*)&InputByte[y * StrideWidth]; for (int x = 0; x < Width / 2; x++, n++) { byte Value = InputRow[x]; Output[n * 2 + 0] = PalettePixels[Translate[(Value >> 0) & 0xF]]; Output[n * 2 + 1] = PalettePixels[Translate[(Value >> 4) & 0xF]]; } } }
private unsafe void Decode_COMPRESSED_DXT1() { var Colors = new OutputPixel[4]; for (int y = 0, ni = 0; y < Height; y += 4) { for (int x = 0; x < Width; x += 4, ni++) { var Block = ((Dxt1Block*)InputByte)[ni]; Colors[0] = Decode_RGBA_5650_Pixel(Block.Color0).Transform((R, G, B, A) => OutputPixel.FromRGBA(B, G, R, A)); Colors[1] = Decode_RGBA_5650_Pixel(Block.Color1).Transform((R, G, B, A) => OutputPixel.FromRGBA(B, G, R, A)); if (Block.Color0 > Block.Color1) { Colors[2] = OutputPixel.OperationPerComponent(Colors[0], Colors[1], (a, b) => { return (byte)(((a * 2) / 3) + ((b * 1) / 3)); }); Colors[3] = OutputPixel.OperationPerComponent(Colors[0], Colors[1], (a, b) => { return (byte)(((a * 1) / 3) + ((b * 2) / 3)); }); } else { Colors[2] = OutputPixel.OperationPerComponent(Colors[0], Colors[1], (a, b) => { return (byte)(((a * 1) / 2) + ((b * 1) / 2)); }); Colors[3] = OutputPixel.FromRGBA(0, 0, 0, 0); } for (int y2 = 0, no = 0; y2 < 4; y2++) { for (int x2 = 0; x2 < 4; x2++, no++) { var Color = ((Block.ColorLookup >> (2 * no)) & 0x3); int rx = (x + x2); int ry = (y + y2); int n = ry * Width + rx; Output[n] = Colors[Color]; } } } } }
private unsafe void Decode_COMPRESSED_DXT5() { //Console.Error.WriteLine("Not Implemented: Decode_COMPRESSED_DXT5"); //throw new NotImplementedException(); //_Decode_Unimplemented(); var Colors = new OutputPixel[4]; for (int y = 0, ni = 0; y < Height; y += 4) { for (int x = 0; x < Width; x += 4, ni++) { var Block = ((Dxt5Block*)InputByte)[ni]; Colors[0] = Decode_RGBA_5650_Pixel(Block.Color0).Transform((R, G, B, A) => OutputPixel.FromRGBA(B, G, R, A)); Colors[1] = Decode_RGBA_5650_Pixel(Block.Color1).Transform((R, G, B, A) => OutputPixel.FromRGBA(B, G, R, A)); Colors[2] = OutputPixel.OperationPerComponent(Colors[0], Colors[1], (a, b) => { return (byte)(((a * 2) / 3) + ((b * 1) / 3)); }); Colors[3] = OutputPixel.OperationPerComponent(Colors[0], Colors[1], (a, b) => { return (byte)(((a * 1) / 3) + ((b * 2) / 3)); }); // Create Alpha Lookup var AlphaLookup = new byte[8]; var Alphas = (ushort)(Block.Alpha >> 48); var Alpha0 = (byte)((Alphas >> 0) & 0xFF); var Alpha1 = (byte)((Alphas >> 8) & 0xFF); AlphaLookup[0] = Alpha0; AlphaLookup[1] = Alpha1; if (Alpha0 > Alpha1) { AlphaLookup[2] = (byte)((6 * Alpha0 + Alpha1) / 7); AlphaLookup[3] = (byte)((5 * Alpha0 + 2 * Alpha1) / 7); AlphaLookup[4] = (byte)((4 * Alpha0 + 3 * Alpha1) / 7); AlphaLookup[5] = (byte)((3 * Alpha0 + 4 * Alpha1) / 7); AlphaLookup[6] = (byte)((2 * Alpha0 + 5 * Alpha1) / 7); AlphaLookup[7] = (byte)((Alpha0 + 6 * Alpha1) / 7); } else { AlphaLookup[2] = (byte)((4 * Alpha0 + Alpha1) / 5); AlphaLookup[3] = (byte)((3 * Alpha0 + 2 * Alpha1) / 5); AlphaLookup[4] = (byte)((2 * Alpha0 + 3 * Alpha1) / 5); AlphaLookup[5] = (byte)((Alpha0 + 4 * Alpha1) / 5); AlphaLookup[6] = (byte)(0x00); AlphaLookup[7] = (byte)(0xFF); } for (int y2 = 0, no = 0; y2 < 4; y2++) { for (int x2 = 0; x2 < 4; x2++, no++) { var Alpha = AlphaLookup[((Block.Alpha >> (3 * no)) & 0x7)]; var Color = ((Block.ColorLookup >> (2 * no)) & 0x3); int rx = (x + x2); int ry = (y + y2); int n = ry * Width + rx; Output[n] = Colors[Color]; Output[n].A = Alpha; } } } } }
public static uint Encode(this ColorFormat ColorFormat, OutputPixel OutputPixel) { return ColorFormat.Encode(OutputPixel.R, OutputPixel.G, OutputPixel.B, OutputPixel.A); }
public bool SetData(OutputPixel *Pixels, int TextureWidth, int TextureHeight) { //lock (OpenglGpuImpl.GpuLock) { //if (TextureId != 0) { this.Width = TextureWidth; this.Height = TextureHeight; Bind(); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, TextureWidth, TextureHeight, 0, PixelFormat.Rgba, PixelType.UnsignedInt8888Reversed, new IntPtr(Pixels)); GL.Flush(); var GlError = GL.GetError(); if (GlError != ErrorCode.NoError) { //Console.Error.WriteLine("TexImage2D: {0} : TexId:{1} : {2} : {3}x{4}", GlError, TextureId, new IntPtr(Pixels), TextureWidth, TextureHeight); TextureId = 0; Bind(); return false; } } } return true; //glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE, 1.0); // 2.0 in scale_2x //GL.TexEnv(TextureEnvTarget.TextureEnv, GL_TEXTURE_ENV_MODE, TextureEnvModeTranslate[state.texture.effect]); }
public void SetPixel(int X, int Y, OutputPixel Color) { if (X < 0 || Y < 0) return; if (X >= Width || Y >= Height) return; var Offset = GetOffset(X, Y); var WriteAddress = (byte *)(Address + Offset); //byte C = (byte)((Color.R + Color.G + Color.B) * 15 / 3 / 255); byte C = (byte)(Color.R * 15 / 255); switch (FontPixelFormat) { case sceLibFont.FontPixelFormat.PSP_FONT_PIXELFORMAT_4: *WriteAddress = (byte)((*WriteAddress & 0xF0) | ((C & 0xF) << 0)); break; case sceLibFont.FontPixelFormat.PSP_FONT_PIXELFORMAT_4_REV: *WriteAddress = (byte)((*WriteAddress & 0x0F) | ((C & 0xF) << 4)); break; case sceLibFont.FontPixelFormat.PSP_FONT_PIXELFORMAT_8: *WriteAddress = Color.A; break; case sceLibFont.FontPixelFormat.PSP_FONT_PIXELFORMAT_24: *(WriteAddress + 0) = Color.R; *(WriteAddress + 1) = Color.G; *(WriteAddress + 2) = Color.B; break; case sceLibFont.FontPixelFormat.PSP_FONT_PIXELFORMAT_32: *(WriteAddress + 0) = Color.R; *(WriteAddress + 1) = Color.G; *(WriteAddress + 2) = Color.B; *(WriteAddress + 3) = Color.A; break; } }