GIFToWoWTexture/GIFConverter/TGASharpLib.cs
/// <summary>
/// Encode image with RLE compression (used RLE per line)!
/// </summary>
/// <param name="ImageData">Image data, bytes array with size = Width * Height * BytesPerPixel.</param>
/// <param name="Width">Image Width, must be > 0.</param>
/// <param name="Height">Image Height, must be > 0.</param>
/// <returns>Bytes array with RLE compressed image data.</returns>
Encode image with RLE compression (used RLE per line)!
Encode image with RLE compression ( used RLE per line ) !
byte[] RLE_Encode(byte[] ImageData, int Width, int Height)
        {
            if (ImageData == null)
                throw new ArgumentNullException(nameof(ImageData) + "in null!");
            if (Width <= 0 || Height <= 0)
                throw new ArgumentOutOfRangeException(nameof(Width) + " and " + nameof(Height) + " must be > 0!");
            int Bpp = ImageData.Length / Width / Height; 
            int ScanLineSize = Width * Bpp;
            if (ScanLineSize * Height != ImageData.Length)
                throw new ArgumentOutOfRangeException("ImageData has wrong Length!");
            try
            {
                int Count = 0;
                int Pos = 0;
                bool IsRLE = false;
                List<byte> Encoded = new List<byte>();
                byte[] RowData = new byte[ScanLineSize];
                for (int y = 0; y < Height; y++)
                {
                    Pos = 0;
                    Buffer.BlockCopy(ImageData, y * ScanLineSize, RowData, 0, ScanLineSize);
                    while (Pos < ScanLineSize)
                    {
                        if (Pos >= ScanLineSize - Bpp)
                        {
                            Encoded.Add(0);
                            Encoded.AddRange(BitConverterExt.GetElements(RowData, Pos, Bpp));
                            Pos += Bpp;
                            break;
                        }
                        Count = 0; 
                        IsRLE = BitConverterExt.IsElementsEqual(RowData, Pos, Pos + Bpp, Bpp);
                        for (int i = Pos + Bpp; i < Math.Min(Pos + 128 * Bpp, ScanLineSize) - Bpp; i += Bpp)
                        {
                            if (IsRLE ^ BitConverterExt.IsElementsEqual(RowData, (IsRLE ? Pos : i), i + Bpp, Bpp))
                            {
                                break;
                            }
                            else
                                Count++;
                        }
                        int CountBpp = (Count + 1) * Bpp;
                        Encoded.Add((byte)(IsRLE ? Count | 128 : Count));
                        Encoded.AddRange(BitConverterExt.GetElements(RowData, Pos, (IsRLE ? Bpp : CountBpp)));
                        Pos += CountBpp;
                    }
                }
                return Encoded.ToArray();
            }
            catch
            {
                return null;
            }
        }
byte [ ] RLE_Encode ( byte [ ] ImageData , int Width , int Height ) { if ( ImageData == null ) throw new ArgumentNullException ( nameof ( ImageData ) + " " ) ; if ( Width <= 0 || Height <= 0 ) throw new ArgumentOutOfRangeException ( nameof ( Width ) + " " + nameof ( Height ) + " " ) ; int Bpp = ImageData . Length / Width / Height ; int ScanLineSize = Width * Bpp ; if ( ScanLineSize * Height != ImageData . Length ) throw new ArgumentOutOfRangeException ( " " ) ; try { int Count = 0 ; int Pos = 0 ; bool IsRLE = false ; List < byte > Encoded = new List < byte > ( ) ; byte [ ] RowData = new byte [ ScanLineSize ] ; for ( int y = 0 ; y < Height ; y ++ ) { Pos = 0 ; Buffer . BlockCopy ( ImageData , y * ScanLineSize , RowData , 0 , ScanLineSize ) ; while ( Pos < ScanLineSize ) { if ( Pos >= ScanLineSize - Bpp ) { Encoded . Add ( 0 ) ; Encoded . AddRange ( BitConverterExt . GetElements ( RowData , Pos , Bpp ) ) ; Pos += Bpp ; break ; } Count = 0 ; IsRLE = BitConverterExt . IsElementsEqual ( RowData , Pos , Pos + Bpp , Bpp ) ; for ( int i = Pos + Bpp ; i < Math . Min ( Pos + 128 * Bpp , ScanLineSize ) - Bpp ; i += Bpp ) { if ( IsRLE ^ BitConverterExt . IsElementsEqual ( RowData , ( IsRLE ? Pos : i ) , i + Bpp , Bpp ) ) { break ; } else Count ++ ; } int CountBpp = ( Count + 1 ) * Bpp ; Encoded . Add ( ( byte ) ( IsRLE ? Count | 128 : Count ) ) ; Encoded . AddRange ( BitConverterExt . GetElements ( RowData , Pos , ( IsRLE ? Bpp : CountBpp ) ) ) ; Pos += CountBpp ; } } return Encoded . ToArray ( ) ; } catch { return null ; } }
Encode image with RLE compression (used RLE per line)!
Encode image with RLE compression ( used RLE per line ) !
// Bytes per pixel //1 //Count--;
Bytes array with RLE compressed image data.
Bytes array with RLE compressed image data .
ImageData
byte[]
Image data, bytes array with size = Width * Height * BytesPerPixel.
Image data bytes array with size = Width * Height * BytesPerPixel .
Width
int
Image Width, must be > 0.
Image Width must be > 0 .
Height
int
Image Height, must be > 0.
Image Height must be > 0 .