Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using KKtMain = KKtMainLib.Main;
- namespace KKtImgLib
- {
- public class Bitmap
- {
- public PixelFormat PixelFmt;
- public int Width { get; private set; }
- public int Height { get; private set; }
- private object[] bitmap;
- public Bitmap(int w = 0, int h = 0, PixelFormat PixelFormat = PixelFormat.R8G8B8A8)
- {
- PixelFmt = PixelFormat;
- if (PixelFmt == PixelFormat.R16G16B16A16) bitmap = new R16G16B16A16[ w * h ];
- else if (PixelFmt == PixelFormat.R16G16B16 ) bitmap = new R16G16B16 [ w * h ];
- else if (PixelFmt == PixelFormat.R16G16 ) bitmap = new R16G16 [ w * h ];
- else if (PixelFmt == PixelFormat.R16 ) bitmap = new R16 [ w * h ];
- else if (PixelFmt == PixelFormat.R8G8B8A8 ) bitmap = new R8G8B8A8 [ w * h ];
- else if (PixelFmt == PixelFormat.R8G8B8 ) bitmap = new R8G8B8 [ w * h ];
- else if (PixelFmt == PixelFormat.R8G8 ) bitmap = new R8G8 [ w * h ];
- else if (PixelFmt == PixelFormat.R8 ) bitmap = new R8 [ w * h ];
- else if (PixelFmt == PixelFormat.BC1 ) bitmap = new BC1 [(w / 4) * (h / 4)];
- else if (PixelFmt == PixelFormat.BC2 ) bitmap = new BC2 [(w / 4) * (h / 4)];
- else if (PixelFmt == PixelFormat.BC3 ) bitmap = new BC3 [(w / 4) * (h / 4)];
- else if (PixelFmt == PixelFormat.BC4 ) bitmap = new BC4 [(w / 4) * (h / 4)];
- else if (PixelFmt == PixelFormat.BC5 ) bitmap = new BC5 [(w / 4) * (h / 4)];
- }
- public object GetPixel(int x, int y, PixelFormat PixelFormat = PixelFormat.R8G8B8A8)
- {
- if (PixelFormat == PixelFmt)
- {
- object obj = bitmap[x + y * Width];
- }
- return null;
- }
- public enum PixelFormat : byte
- {
- R16G16B16A16,
- R16G16B16 ,
- R16G16 ,
- R16 ,
- R8G8B8A8 ,
- R8G8B8 ,
- R8G8 ,
- R8 ,
- BC1 ,
- BC2 ,
- BC3 ,
- BC4 ,
- BC5 ,
- };
- public class R16G16B16A16
- {
- public ushort R;
- public ushort G;
- public ushort B;
- public ushort A;
- public R16G16B16A16( ) { R = 0; G = 0; B = 0; A = 0 ; }
- public R16G16B16A16(ushort R, ushort G, ushort B) { this.R = R; this.G = G; this.B = B; A = 0xFFFF; }
- public R16G16B16A16(ushort R, ushort G, ushort B, ushort A)
- { this.R = R; this.G = G; this.B = B; this.A = A; }
- public R16G16B16A16(byte R, byte G, byte B)
- { this.R = (ushort)(R16)(R8)R; this.G = (ushort)(R16)(R8)G; this.B = (ushort)(R16)(R8)B; A = 0xFFFF; }
- public R16G16B16A16(byte R, byte G, byte B, byte A)
- { this.R = (ushort)(R16)(R8)R; this.G = (ushort)(R16)(R8)G;
- this.B = (ushort)(R16)(R8)B; this.A = (ushort)(R16)(R8)A; }
- public R16G16B16A16(long RGB)
- { R = (ushort)((RGB >> 32) & 0xFFFF); G = (ushort)((RGB >> 16) & 0xFFFF);
- B = (ushort)(RGB & 0xFFFF); A = 0xFFFF; }
- public R16G16B16A16(long RGB, ushort A)
- { R = (ushort)((RGB >> 32) & 0xFFFF); G = (ushort)((RGB >> 16) & 0xFFFF);
- B = (ushort)(RGB & 0xFFFF); this.A = A; }
- public R16G16B16A16(ulong RGBA)
- { R = (byte)((RGBA >> 48) & 0xFFFF); G = (byte)((RGBA >> 32) & 0xFFFF);
- B = (byte)((RGBA >> 16) & 0xFFFF); A = (byte)(RGBA & 0xFFFF); }
- public static explicit operator ulong (R16G16B16A16 RGBA) =>
- ((ulong)RGBA.R << 48) | ((ulong)RGBA.G << 32) | ((ulong)RGBA.G << 16) | RGBA.A;
- public static explicit operator R16G16B16A16(R16G16B16 RGB) => new R16G16B16A16(RGB.R, RGB.G, RGB.B);
- public static explicit operator R16G16B16A16( long RGB ) => new R16G16B16A16(RGB );
- public static explicit operator R16G16B16A16( ulong RGBA) => new R16G16B16A16(RGBA);
- public static explicit operator R16G16B16A16( R8G8B8A8 RGBA) =>
- new R16G16B16A16((ushort)(R16)RGBA.R, (ushort)(R16)RGBA.G, (ushort)(R16)RGBA.B, (ushort)(R16)RGBA.A);
- }
- public class R16G16B16
- {
- public ushort R;
- public ushort G;
- public ushort B;
- public R16G16B16( ) { R = 0; G = 0; B = 0; }
- public R16G16B16(ushort R, ushort G, ushort B) { this.R = R; this.G = G; this.B = B; }
- public R16G16B16(byte R, byte G, byte B)
- { this.R = (ushort)(R16)(R8)R; this.G = (ushort)(R16)(R8)G; this.B = (ushort)(R16)(R8)B; }
- public R16G16B16(long RGB)
- { R = (ushort)((RGB >> 32) & 0xFFFF); G = (ushort)((RGB >> 16) & 0xFFFF); B = (ushort)(RGB & 0xFFFF); }
- public R16G16B16(ulong RGB)
- { R = (ushort)((RGB >> 32) & 0xFFFF); G = (ushort)((RGB >> 16) & 0xFFFF); B = (ushort)(RGB & 0xFFFF); }
- public static explicit operator long (R16G16B16 RGB) => ((long)RGB.R << 32) | ((long)RGB.G << 16) | RGB.B;
- public static explicit operator R16G16B16( long RGB) => new R16G16B16(RGB);
- public static explicit operator R16G16B16( ulong RGB) => new R16G16B16(RGB);
- public static explicit operator R16G16B16(R8G8B8 RGB) =>
- new R16G16B16((ushort)(R16)RGB.R, (ushort)(R16)RGB.G, (ushort)(R16)RGB.B);
- }
- public class R16G16
- {
- public ushort R;
- public ushort G;
- public R16G16() => R = G = 0;
- public R16G16(byte R, byte G)
- { this.R = (ushort)(R16)(R8)R; this.G = (ushort)(R16)(R8)G; }
- public R16G16(ushort R, ushort G)
- { this.R = R; this.G = G; }
- public R16G16(uint RG)
- { R = (ushort)((RG >> 16) & 0xFFFF); G = (ushort)(RG & 0xFFFF); }
- public static explicit operator uint(R16G16 RG) => ((uint)RG.R << 16) | RG.G;
- public static explicit operator R16G16(R8G8 RG) => new R16G16((ushort)(R16)RG.R, (ushort)(R16)RG.G);
- public static explicit operator R16G16(uint RG) => new R16G16(RG);
- }
- public class R16
- {
- public ushort R;
- public R16() => R = 0;
- public R16(byte R) => this.R = (ushort)(R16)(R8)R;
- public R16(ushort R) => this.R = R;
- public static explicit operator ushort(R16 R) => R.R;
- public static explicit operator R16(ushort R) => new R16(R);
- public static explicit operator R16( R8 R) => new R16((ushort)(R.R * 257.0));
- }
- public class R8G8B8A8
- {
- public byte R;
- public byte G;
- public byte B;
- public byte A;
- public R8G8B8A8() => R = G = B = A = 0 ;
- public R8G8B8A8(byte R, byte G, byte B)
- { this.R = R; this.G = G; this.B = B; A = 0xFF; }
- public R8G8B8A8(byte R, byte G, byte B, byte A)
- { this.R = R; this.G = G; this.B = B; this.A = A; }
- public R8G8B8A8(ushort R, ushort G, ushort B)
- { this.R = (byte)(R8)(R16)R; this.G = (byte)(R8)(R16)G; this.B = (byte)(R8)(R16)B; A = 0xFF; }
- public R8G8B8A8(ushort R, ushort G, ushort B, ushort A)
- { this.R = (byte)(R8)(R16)R; this.G = (byte)(R8)(R16)G;
- this.B = (byte)(R8)(R16)B; this.A = (byte)(R8)(R16)A; }
- public R8G8B8A8(int RGB)
- { R = (byte)((RGB >> 16) & 0xFF); G = (byte)((RGB >> 8) & 0xFF); B = (byte)(RGB & 0xFF); A = 0xFF; }
- public R8G8B8A8(int RGB, byte A)
- { R = (byte)((RGB >> 16) & 0xFF); G = (byte)((RGB >> 8) & 0xFF); B = (byte)(RGB & 0xFF); this.A = A; }
- public R8G8B8A8(uint RGBA)
- { R = (byte)((RGBA >> 24) & 0xFF); G = (byte)((RGBA >> 16) & 0xFF);
- B = (byte)((RGBA >> 8) & 0xFF); A = (byte)(RGBA & 0xFF); }
- public static explicit operator uint (R8G8B8A8 RGBA) =>
- ((uint)RGBA.R << 24) | ((uint)RGBA.G << 16) | ((uint)RGBA.G << 8) | RGBA.A;
- public static explicit operator R8G8B8A8( R8G8B8 RGB ) => new R8G8B8A8(RGB.R, RGB.G, RGB.B);
- public static explicit operator R8G8B8A8( int RGB ) => new R8G8B8A8(RGB );
- public static explicit operator R8G8B8A8( uint RGBA) => new R8G8B8A8(RGBA);
- public static explicit operator R8G8B8A8(R16G16B16A16 RGBA) =>
- new R8G8B8A8((byte)(R8)RGBA.R, (byte)(R8)RGBA.G, (byte)(R8)RGBA.B, (byte)(R8)RGBA.A);
- }
- public class R8G8B8
- {
- public byte R;
- public byte G;
- public byte B;
- public R8G8B8() => R = G = B = 0;
- public R8G8B8(byte R, byte G, byte B)
- { this.R = R; this.G = G; this.B = B; }
- public R8G8B8(ushort R, ushort G, ushort B)
- { this.R = (byte)(R8)(R16)R; this.G = (byte)(R8)(R16)G; this.B = (byte)(R8)(R16)B; }
- public R8G8B8( int RGB)
- { R = (byte)((RGB >> 16) & 0xFF); G = (byte)((RGB >> 8) & 0xFF); B = (byte)(RGB & 0xFF); }
- public R8G8B8(uint RGB)
- { R = (byte)((RGB >> 16) & 0xFF); G = (byte)((RGB >> 8) & 0xFF); B = (byte)(RGB & 0xFF); }
- public static explicit operator uint ( R8G8B8 RGB ) => ((uint)RGB.R << 16) | RGB.G;
- public static explicit operator R8G8B8( R8G8B8A8 RGBA) => new R8G8B8(RGBA.R, RGBA.G, RGBA.B);
- public static explicit operator R8G8B8( int RGB ) => new R8G8B8(RGB);
- public static explicit operator R8G8B8( uint RGB ) => new R8G8B8(RGB);
- public static explicit operator R8G8B8(R16G16B16 RGB ) =>
- new R8G8B8((byte)(R8)RGB.R, (byte)(R8)RGB.G, (byte)(R8)RGB.B);
- }
- public class R8G8
- {
- public byte R;
- public byte G;
- public R8G8() => R = G = 0;
- public R8G8( byte R,byte G)
- { this.R = R; this.G = G; }
- public R8G8(ushort R, ushort G)
- { this.R = (byte)(R8)(R16)R; this.G = (byte)(R8)(R16)G;}
- public R8G8(ushort RG)
- { R = (byte)((RG >> 8) & 0xFF); G = (byte)(RG & 0xFF); }
- public static explicit operator ushort(R8G8 RG) => (ushort)((RG.R << 8) | RG.G);
- public static explicit operator R8G8(R16G16 RG) => new R8G8((byte)(R8)RG.R, (byte)(R8)RG.G);
- public static explicit operator R8G8(ushort RG) => new R8G8(RG);
- }
- public class R8
- {
- public byte R;
- public R8() => R = 0;
- public R8(byte R) => this.R = R;
- public R8(ushort R) => this.R = (byte)(R8)(R16)R;
- public static explicit operator byte(R8 R) => R.R;
- public static explicit operator R8(byte R) => new R8(R);
- public static explicit operator R8( R16 R) => new R8((byte)(R.R / 257.0));
- }
- public class BC1
- {
- public ushort Max;
- public ushort Min;
- public uint Inds;
- public BC1(ushort Max = 0, ushort Min = 0, uint Inds = 0)
- { this.Max = Max; this.Min = Min; this.Inds = Inds; }
- public BC1(long bits = 0)
- { Max = (ushort)bits; Min = (ushort)(bits >> 16);
- Inds = (uint)(bits >> 32); }
- public R8G8B8A8[] ToR8G8B8A8()
- {
- R8G8B8A8[] RGBA = new R8G8B8A8[16];
- if (Max == 0 && Min == 0)
- {
- for (byte i = 0; i < 16; i++) RGBA[i] = new R8G8B8A8();
- return RGBA;
- }
- byte[] Col = new byte[12];
- Col[0] = KKtMain.CFTB(((Max >> 11) & 0x1F) * BitmapExtensions._5bit);
- Col[1] = KKtMain.CFTB(((Max >> 5) & 0x3F) * BitmapExtensions._6bit);
- Col[2] = KKtMain.CFTB(( Max & 0x1F) * BitmapExtensions._5bit);
- Col[3] = KKtMain.CFTB(((Min >> 11) & 0x1F) * BitmapExtensions._5bit);
- Col[4] = KKtMain.CFTB(((Min >> 5) & 0x3F) * BitmapExtensions._6bit);
- Col[5] = KKtMain.CFTB( (Min & 0x1F) * BitmapExtensions._5bit);
- if (Max != Min)
- Max = (ushort)(Max - Min + Min);
- for (byte i = 0; i < 3; i++)
- if (Max < Min) Col[6 + i] = KKtMain.CFTB(( Col[i] + Col[3 + i]) / 2f);
- else { Col[6 + i] = KKtMain.CFTB((2 * Col[i] + Col[3 + i]) / 3f);
- Col[9 + i] = KKtMain.CFTB((Col[i] + 2 * Col[3 + i]) / 3f); }
- for (byte j = 0, k = 0, sel = 0; j < 4; j++)
- for (byte i = 0; i < 4; i++, k++)
- {
- sel = (byte)((Inds & (0x03 << (k << 1))) >> (k << 1));
- RGBA[j * 4 + i] = new R8G8B8A8(Col[sel * 3 + 0], Col[sel * 3 + 1], Col[sel * 3 + 2],
- (byte)(Max >= Min ? 0xFF : sel < 3 ? 0xFF : 0x00));
- }
- return RGBA;
- }
- }
- public class BC2
- {
- public BC1 RGB;
- public long Alpha;
- public BC2(long Alpha = 0, long Color = 0)
- { RGB = new BC1(Color); this.Alpha = Alpha; }
- public BC2(ushort Max = 0, ushort Min = 0, uint Inds = 0, long Alpha = 0)
- { RGB = new BC1(Max, Min, Inds); this.Alpha = Alpha; }
- public R8G8B8A8[] ToR8G8B8A8()
- {
- R8G8B8A8[] RGBA = RGB.ToR8G8B8A8();
- for (byte i = 0; i < 16; i++)
- RGBA[i].A = (byte)(((Alpha >> ((15 - i) * 4)) & 0xF) * BitmapExtensions. _4bit);
- return RGBA;
- }
- public R8G8B8[] ToR8G8B8() => RGB.ToR8G8B8A8().ToR8G8B8();
- }
- public class BC3
- {
- public BC1 RGB;
- public BC4 Alpha;
- public BC3(long Alpha = 0, long Color = 0)
- { RGB = new BC1(Color); this.Alpha = new BC4(Alpha); }
- public BC3(ushort Max = 0, ushort Min = 0, uint Inds = 0, long Alpha = 0)
- { RGB = new BC1(Max, Min, Inds); this.Alpha = new BC4(Alpha); }
- public R8G8B8A8[] ToR8G8B8A8()
- {
- R8G8B8A8[] RGBA = RGB.ToR8G8B8A8();
- R8[] Alpha = this.Alpha.ToR8();
- for (byte i = 0; i < 16; i++)
- RGBA[i].A = Alpha[i].R;
- return RGBA;
- }
- public R8G8B8[] ToR8G8B8() => RGB.ToR8G8B8A8().ToR8G8B8();
- }
- public class BC4
- {
- public long bits;
- public BC4(long bits = 0) => this.bits = bits;
- public R8G8B8A8[] ToR8G8B8A8() => ToR8().ToR8G8B8A8();
- public R8[] ToR8()
- {
- R8[] block = new R8[16];
- for (byte i = 0; i < 16; i++) block[i] = new R8();
- if ((bits & 0xFFFF) != 0)
- {
- byte a = (byte) bits;
- byte b = (byte)(bits >> 8);
- double mode = a > b ? 7 : 5;
- long Val = a << 8 | b;
- for (byte i = 1; i < mode; i++)
- Val = (Val << 8) | KKtMain.CFTB(((mode - i) * a + i * b) / mode);
- if (a <= b)
- Val = Val << 16 | 0xFF;
- long mask = bits >> 16;
- for (byte j = 0; j < 4; j++)
- for (byte i = 0; i < 4; i++, mask >>= 3)
- block[j * 4 + i].R = (byte)(Val >> (byte)(((7 - (mask & 0x07)) << 3)) & 0xFF);
- }
- return block;
- }
- public R16[] ToR16()
- {
- R8[] R = ToR8();
- R16[] R16 = new R16[16];
- for (byte i = 0; i < 16; i++) R16[i] = (R16)R[i];
- return R16;
- }
- }
- public class BC5
- {
- public BC4 R;
- public BC4 G;
- public BC5(long R = 0, long G = 0)
- { this.R = new BC4(R); this.G = new BC4(G); }
- public R8G8B8A8[] ToR8G8B8A8() => ToR8G8().ToR8G8B8A8();
- public R8G8[] ToR8G8()
- {
- R8[] R = this.R.ToR8();
- R8[] G = this.G.ToR8();
- R8G8[] RG = new R8G8[16];
- for (byte i = 0; i < 16; i++) RG[i] = new R8G8(R[i].R, G[i].R);
- return RG;
- }
- public R16G16[] ToR16G16()
- {
- R16[] R = this.R.ToR16();
- R16[] G = this.G.ToR16();
- R16G16[] RG = new R16G16[16];
- for (byte i = 0; i < 16; i++) RG[i] = new R16G16(R[i].R, G[i].R);
- return RG;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment