kujikita

Bitmap.cs

Feb 22nd, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 17.54 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using KKtMain = KKtMainLib.Main;
  7.  
  8. namespace KKtImgLib
  9. {
  10.     public class Bitmap
  11.     {
  12.         public PixelFormat PixelFmt;
  13.         public int Width  { get; private set; }
  14.         public int Height { get; private set; }
  15.  
  16.         private object[] bitmap;
  17.  
  18.         public Bitmap(int w = 0, int h = 0, PixelFormat PixelFormat = PixelFormat.R8G8B8A8)
  19.         {
  20.             PixelFmt = PixelFormat;
  21.                  if (PixelFmt == PixelFormat.R16G16B16A16) bitmap = new R16G16B16A16[ w *       h     ];
  22.             else if (PixelFmt == PixelFormat.R16G16B16   ) bitmap = new R16G16B16   [ w *       h     ];
  23.             else if (PixelFmt == PixelFormat.R16G16      ) bitmap = new R16G16      [ w *       h     ];
  24.             else if (PixelFmt == PixelFormat.R16         ) bitmap = new R16         [ w *       h     ];
  25.             else if (PixelFmt == PixelFormat.R8G8B8A8    ) bitmap = new R8G8B8A8    [ w *       h     ];
  26.             else if (PixelFmt == PixelFormat.R8G8B8      ) bitmap = new R8G8B8      [ w *       h     ];
  27.             else if (PixelFmt == PixelFormat.R8G8        ) bitmap = new R8G8        [ w *       h     ];
  28.             else if (PixelFmt == PixelFormat.R8          ) bitmap = new R8          [ w *       h     ];
  29.             else if (PixelFmt == PixelFormat.BC1         ) bitmap = new BC1         [(w / 4) * (h / 4)];
  30.             else if (PixelFmt == PixelFormat.BC2         ) bitmap = new BC2         [(w / 4) * (h / 4)];
  31.             else if (PixelFmt == PixelFormat.BC3         ) bitmap = new BC3         [(w / 4) * (h / 4)];
  32.             else if (PixelFmt == PixelFormat.BC4         ) bitmap = new BC4         [(w / 4) * (h / 4)];
  33.             else if (PixelFmt == PixelFormat.BC5         ) bitmap = new BC5         [(w / 4) * (h / 4)];
  34.         }
  35.  
  36.         public object GetPixel(int x, int y, PixelFormat PixelFormat = PixelFormat.R8G8B8A8)
  37.         {
  38.             if (PixelFormat == PixelFmt)
  39.             {
  40.                 object obj = bitmap[x + y * Width];
  41.             }
  42.  
  43.             return null;
  44.         }
  45.  
  46.         public enum PixelFormat : byte
  47.         {
  48.             R16G16B16A16,
  49.             R16G16B16   ,
  50.             R16G16      ,
  51.             R16         ,
  52.             R8G8B8A8    ,
  53.             R8G8B8      ,
  54.             R8G8        ,
  55.             R8          ,
  56.             BC1         ,
  57.             BC2         ,
  58.             BC3         ,
  59.             BC4         ,
  60.             BC5         ,
  61.         };
  62.  
  63.         public class R16G16B16A16
  64.         {
  65.             public ushort R;
  66.             public ushort G;
  67.             public ushort B;
  68.             public ushort A;
  69.  
  70.             public R16G16B16A16(                            ) {      R = 0;      G = 0;      B = 0; A = 0     ; }
  71.             public R16G16B16A16(ushort R, ushort G, ushort B) { this.R = R; this.G = G; this.B = B; A = 0xFFFF; }
  72.             public R16G16B16A16(ushort R, ushort G, ushort B, ushort A)
  73.             { this.R = R; this.G = G; this.B = B; this.A = A; }
  74.  
  75.             public R16G16B16A16(byte R, byte G, byte B)
  76.             { this.R = (ushort)(R16)(R8)R; this.G = (ushort)(R16)(R8)G; this.B = (ushort)(R16)(R8)B; A = 0xFFFF; }
  77.  
  78.             public R16G16B16A16(byte R, byte G, byte B, byte A)
  79.             { this.R = (ushort)(R16)(R8)R; this.G = (ushort)(R16)(R8)G;
  80.                 this.B = (ushort)(R16)(R8)B; this.A = (ushort)(R16)(R8)A; }
  81.  
  82.             public R16G16B16A16(long RGB)
  83.             { R = (ushort)((RGB >> 32) & 0xFFFF); G = (ushort)((RGB >> 16) & 0xFFFF);
  84.                 B = (ushort)(RGB & 0xFFFF); A = 0xFFFF; }
  85.  
  86.             public R16G16B16A16(long RGB, ushort A)
  87.             { R = (ushort)((RGB >> 32) & 0xFFFF); G = (ushort)((RGB >> 16) & 0xFFFF);
  88.                 B = (ushort)(RGB & 0xFFFF); this.A = A; }
  89.  
  90.             public R16G16B16A16(ulong RGBA)
  91.             { R = (byte)((RGBA >> 48) & 0xFFFF); G = (byte)((RGBA >> 32) & 0xFFFF);
  92.                 B = (byte)((RGBA >> 16) & 0xFFFF); A = (byte)(RGBA & 0xFFFF); }
  93.  
  94.             public static explicit operator ulong   (R16G16B16A16 RGBA) =>
  95.                 ((ulong)RGBA.R << 48) | ((ulong)RGBA.G << 32) | ((ulong)RGBA.G << 16) | RGBA.A;
  96.             public static explicit operator R16G16B16A16(R16G16B16 RGB) => new R16G16B16A16(RGB.R, RGB.G, RGB.B);
  97.             public static explicit operator R16G16B16A16(     long RGB ) => new R16G16B16A16(RGB );
  98.             public static explicit operator R16G16B16A16(    ulong RGBA) => new R16G16B16A16(RGBA);
  99.             public static explicit operator R16G16B16A16( R8G8B8A8 RGBA) =>
  100.                 new R16G16B16A16((ushort)(R16)RGBA.R, (ushort)(R16)RGBA.G, (ushort)(R16)RGBA.B, (ushort)(R16)RGBA.A);
  101.         }
  102.  
  103.         public class R16G16B16
  104.         {
  105.             public ushort R;
  106.             public ushort G;
  107.             public ushort B;
  108.  
  109.             public R16G16B16(                            ) {      R = 0;      G = 0;      B = 0; }
  110.             public R16G16B16(ushort R, ushort G, ushort B) { this.R = R; this.G = G; this.B = B; }
  111.  
  112.             public R16G16B16(byte R, byte G, byte B)
  113.             { this.R = (ushort)(R16)(R8)R; this.G = (ushort)(R16)(R8)G; this.B = (ushort)(R16)(R8)B; }
  114.  
  115.             public R16G16B16(long RGB)
  116.             { R = (ushort)((RGB >> 32) & 0xFFFF); G = (ushort)((RGB >> 16) & 0xFFFF); B = (ushort)(RGB & 0xFFFF); }
  117.  
  118.             public R16G16B16(ulong RGB)
  119.             { R = (ushort)((RGB >> 32) & 0xFFFF); G = (ushort)((RGB >> 16) & 0xFFFF); B = (ushort)(RGB & 0xFFFF); }
  120.  
  121.             public static explicit operator long  (R16G16B16 RGB) => ((long)RGB.R << 32) | ((long)RGB.G << 16) | RGB.B;
  122.             public static explicit operator R16G16B16(  long RGB) => new R16G16B16(RGB);
  123.             public static explicit operator R16G16B16( ulong RGB) => new R16G16B16(RGB);
  124.             public static explicit operator R16G16B16(R8G8B8 RGB) =>
  125.                 new R16G16B16((ushort)(R16)RGB.R, (ushort)(R16)RGB.G, (ushort)(R16)RGB.B);
  126.         }
  127.  
  128.         public class R16G16
  129.         {
  130.             public ushort R;
  131.             public ushort G;
  132.  
  133.             public R16G16() => R = G = 0;
  134.  
  135.             public R16G16(byte R, byte G)
  136.             { this.R = (ushort)(R16)(R8)R; this.G = (ushort)(R16)(R8)G; }
  137.  
  138.             public R16G16(ushort R, ushort G)
  139.             { this.R = R; this.G = G; }
  140.  
  141.             public R16G16(uint RG)
  142.             { R = (ushort)((RG >> 16) & 0xFFFF); G = (ushort)(RG & 0xFFFF); }
  143.  
  144.             public static explicit operator uint(R16G16 RG) => ((uint)RG.R << 16) | RG.G;
  145.             public static explicit operator R16G16(R8G8 RG) => new R16G16((ushort)(R16)RG.R, (ushort)(R16)RG.G);
  146.             public static explicit operator R16G16(uint RG) => new R16G16(RG);
  147.         }
  148.  
  149.         public class R16
  150.         {
  151.             public ushort R;
  152.  
  153.             public R16() => R = 0;
  154.  
  155.             public R16(byte R) => this.R = (ushort)(R16)(R8)R;
  156.  
  157.             public R16(ushort R) => this.R = R;
  158.  
  159.             public static explicit operator ushort(R16 R) => R.R;
  160.             public static explicit operator R16(ushort R) => new R16(R);
  161.             public static explicit operator R16(    R8 R) => new R16((ushort)(R.R * 257.0));
  162.         }
  163.  
  164.         public class R8G8B8A8
  165.         {
  166.             public byte R;
  167.             public byte G;
  168.             public byte B;
  169.             public byte A;
  170.  
  171.             public R8G8B8A8() => R = G = B = A = 0 ;
  172.  
  173.             public R8G8B8A8(byte R, byte G, byte B)
  174.             { this.R = R; this.G = G; this.B = B; A = 0xFF; }
  175.  
  176.             public R8G8B8A8(byte R, byte G, byte B, byte A)
  177.             { this.R = R; this.G = G; this.B = B; this.A = A; }
  178.  
  179.             public R8G8B8A8(ushort R, ushort G, ushort B)
  180.             { this.R = (byte)(R8)(R16)R; this.G = (byte)(R8)(R16)G; this.B = (byte)(R8)(R16)B; A = 0xFF; }
  181.  
  182.             public R8G8B8A8(ushort R, ushort G, ushort B, ushort A)
  183.             { this.R = (byte)(R8)(R16)R; this.G = (byte)(R8)(R16)G;
  184.                 this.B = (byte)(R8)(R16)B; this.A = (byte)(R8)(R16)A; }
  185.  
  186.             public R8G8B8A8(int RGB)
  187.             { R = (byte)((RGB >> 16) & 0xFF); G = (byte)((RGB >> 8) & 0xFF); B = (byte)(RGB & 0xFF); A = 0xFF; }
  188.  
  189.             public R8G8B8A8(int RGB, byte A)
  190.             { R = (byte)((RGB >> 16) & 0xFF); G = (byte)((RGB >> 8) & 0xFF); B = (byte)(RGB & 0xFF); this.A = A; }
  191.  
  192.             public R8G8B8A8(uint RGBA)
  193.             { R = (byte)((RGBA >> 24) & 0xFF); G = (byte)((RGBA >> 16) & 0xFF);
  194.                 B = (byte)((RGBA >> 8) & 0xFF); A = (byte)(RGBA & 0xFF); }
  195.  
  196.             public static explicit operator uint        (R8G8B8A8 RGBA) =>
  197.                 ((uint)RGBA.R << 24) | ((uint)RGBA.G << 16) | ((uint)RGBA.G << 8) | RGBA.A;
  198.             public static explicit operator R8G8B8A8(      R8G8B8 RGB ) => new R8G8B8A8(RGB.R, RGB.G, RGB.B);
  199.             public static explicit operator R8G8B8A8(         int RGB ) => new R8G8B8A8(RGB );
  200.             public static explicit operator R8G8B8A8(        uint RGBA) => new R8G8B8A8(RGBA);
  201.             public static explicit operator R8G8B8A8(R16G16B16A16 RGBA) =>
  202.                 new R8G8B8A8((byte)(R8)RGBA.R, (byte)(R8)RGBA.G, (byte)(R8)RGBA.B, (byte)(R8)RGBA.A);
  203.         }
  204.  
  205.         public class R8G8B8
  206.         {
  207.             public byte R;
  208.             public byte G;
  209.             public byte B;
  210.  
  211.             public R8G8B8() => R = G = B = 0;
  212.  
  213.             public R8G8B8(byte R, byte G, byte B)
  214.             { this.R = R; this.G = G; this.B = B; }
  215.  
  216.             public R8G8B8(ushort R, ushort G, ushort B)
  217.             { this.R = (byte)(R8)(R16)R; this.G = (byte)(R8)(R16)G; this.B = (byte)(R8)(R16)B; }
  218.  
  219.             public R8G8B8( int RGB)
  220.             { R = (byte)((RGB >> 16) & 0xFF); G = (byte)((RGB >> 8) & 0xFF); B = (byte)(RGB & 0xFF); }
  221.  
  222.             public R8G8B8(uint RGB)
  223.             { R = (byte)((RGB >> 16) & 0xFF); G = (byte)((RGB >> 8) & 0xFF); B = (byte)(RGB & 0xFF); }
  224.  
  225.             public static explicit operator uint  (   R8G8B8 RGB ) => ((uint)RGB.R << 16) | RGB.G;
  226.             public static explicit operator R8G8B8( R8G8B8A8 RGBA) => new R8G8B8(RGBA.R, RGBA.G, RGBA.B);
  227.             public static explicit operator R8G8B8(      int RGB ) => new R8G8B8(RGB);
  228.             public static explicit operator R8G8B8(     uint RGB ) => new R8G8B8(RGB);
  229.             public static explicit operator R8G8B8(R16G16B16 RGB ) =>
  230.                 new R8G8B8((byte)(R8)RGB.R, (byte)(R8)RGB.G, (byte)(R8)RGB.B);
  231.         }
  232.  
  233.         public class R8G8
  234.         {
  235.             public byte R;
  236.             public byte G;
  237.  
  238.             public R8G8() => R = G = 0;
  239.  
  240.             public R8G8( byte R,byte G)
  241.             { this.R = R; this.G = G; }
  242.  
  243.             public R8G8(ushort R, ushort G)
  244.             { this.R = (byte)(R8)(R16)R; this.G = (byte)(R8)(R16)G;}
  245.  
  246.             public R8G8(ushort RG)
  247.             { R = (byte)((RG >> 8) & 0xFF); G = (byte)(RG & 0xFF); }
  248.  
  249.             public static explicit operator ushort(R8G8 RG) => (ushort)((RG.R << 8) | RG.G);
  250.             public static explicit operator R8G8(R16G16 RG) => new R8G8((byte)(R8)RG.R, (byte)(R8)RG.G);
  251.             public static explicit operator R8G8(ushort RG) => new R8G8(RG);
  252.         }
  253.  
  254.         public class R8
  255.         {
  256.             public byte R;
  257.  
  258.             public R8() => R = 0;
  259.  
  260.             public R8(byte R) => this.R = R;
  261.  
  262.             public R8(ushort R) => this.R = (byte)(R8)(R16)R;
  263.  
  264.             public static explicit operator byte(R8 R) => R.R;
  265.             public static explicit operator R8(byte R) => new R8(R);
  266.             public static explicit operator R8( R16 R) => new R8((byte)(R.R / 257.0));
  267.         }
  268.  
  269.         public class BC1
  270.         {
  271.             public ushort Max;
  272.             public ushort Min;
  273.             public uint Inds;
  274.  
  275.             public BC1(ushort Max = 0, ushort Min = 0, uint Inds = 0)
  276.             { this.Max = Max; this.Min = Min; this.Inds = Inds; }
  277.  
  278.             public BC1(long bits = 0)
  279.             { Max = (ushort)bits; Min = (ushort)(bits >> 16);
  280.                 Inds = (uint)(bits >> 32); }
  281.  
  282.             public R8G8B8A8[] ToR8G8B8A8()
  283.             {
  284.                 R8G8B8A8[] RGBA = new R8G8B8A8[16];
  285.                 if (Max == 0 && Min == 0)
  286.                 {
  287.                     for (byte i = 0; i < 16; i++) RGBA[i] = new R8G8B8A8();
  288.                     return RGBA;
  289.                 }
  290.  
  291.                 byte[] Col = new byte[12];
  292.                 Col[0] = KKtMain.CFTB(((Max >> 11) & 0x1F) * BitmapExtensions._5bit);
  293.                 Col[1] = KKtMain.CFTB(((Max >>  5) & 0x3F) * BitmapExtensions._6bit);
  294.                 Col[2] = KKtMain.CFTB(( Max        & 0x1F) * BitmapExtensions._5bit);
  295.                 Col[3] = KKtMain.CFTB(((Min >> 11) & 0x1F) * BitmapExtensions._5bit);
  296.                 Col[4] = KKtMain.CFTB(((Min >>  5) & 0x3F) * BitmapExtensions._6bit);
  297.                 Col[5] = KKtMain.CFTB( (Min        & 0x1F) * BitmapExtensions._5bit);
  298.  
  299.                 if (Max != Min)
  300.                     Max = (ushort)(Max - Min + Min);
  301.                 for (byte i = 0; i < 3; i++)
  302.                     if (Max < Min) Col[6 + i] = KKtMain.CFTB((    Col[i] + Col[3 + i]) / 2f);
  303.                     else {         Col[6 + i] = KKtMain.CFTB((2 * Col[i] + Col[3 + i]) / 3f);
  304.                                    Col[9 + i] = KKtMain.CFTB((Col[i] + 2 * Col[3 + i]) / 3f); }
  305.  
  306.  
  307.                 for (byte j = 0, k = 0, sel = 0; j < 4; j++)
  308.                     for (byte i = 0; i < 4; i++, k++)
  309.                     {
  310.                         sel = (byte)((Inds & (0x03 << (k << 1))) >> (k << 1));
  311.                         RGBA[j * 4 + i] = new R8G8B8A8(Col[sel * 3 + 0], Col[sel * 3 + 1], Col[sel * 3 + 2],
  312.                             (byte)(Max >= Min ? 0xFF : sel < 3 ? 0xFF : 0x00));
  313.                     }
  314.                
  315.                 return RGBA;
  316.             }
  317.         }
  318.  
  319.         public class BC2
  320.         {
  321.             public BC1 RGB;
  322.             public long Alpha;
  323.  
  324.             public BC2(long Alpha = 0, long Color = 0)
  325.             { RGB = new BC1(Color); this.Alpha = Alpha; }
  326.  
  327.             public BC2(ushort Max = 0, ushort Min = 0, uint Inds = 0, long Alpha = 0)
  328.             { RGB = new BC1(Max, Min, Inds); this.Alpha = Alpha; }
  329.  
  330.             public R8G8B8A8[] ToR8G8B8A8()
  331.             {
  332.                 R8G8B8A8[] RGBA = RGB.ToR8G8B8A8();
  333.                 for (byte i = 0; i < 16; i++)
  334.                     RGBA[i].A = (byte)(((Alpha >> ((15 - i) * 4)) & 0xF) * BitmapExtensions. _4bit);
  335.                 return RGBA;
  336.             }
  337.  
  338.             public R8G8B8[] ToR8G8B8() => RGB.ToR8G8B8A8().ToR8G8B8();
  339.         }
  340.  
  341.         public class BC3
  342.         {
  343.             public BC1 RGB;
  344.             public BC4 Alpha;
  345.  
  346.             public BC3(long Alpha = 0, long Color = 0)
  347.             { RGB = new BC1(Color); this.Alpha = new BC4(Alpha); }
  348.  
  349.             public BC3(ushort Max = 0, ushort Min = 0, uint Inds = 0, long Alpha = 0)
  350.             { RGB = new BC1(Max, Min, Inds); this.Alpha = new BC4(Alpha); }
  351.  
  352.             public R8G8B8A8[] ToR8G8B8A8()
  353.             {
  354.                 R8G8B8A8[] RGBA = RGB.ToR8G8B8A8();
  355.                 R8[] Alpha = this.Alpha.ToR8();
  356.                 for (byte i = 0; i < 16; i++)
  357.                     RGBA[i].A = Alpha[i].R;
  358.                 return RGBA;
  359.             }
  360.  
  361.             public R8G8B8[] ToR8G8B8() => RGB.ToR8G8B8A8().ToR8G8B8();
  362.         }
  363.  
  364.         public class BC4
  365.         {
  366.             public long bits;
  367.  
  368.             public BC4(long bits = 0) => this.bits = bits;
  369.  
  370.             public R8G8B8A8[] ToR8G8B8A8() => ToR8().ToR8G8B8A8();
  371.  
  372.             public R8[] ToR8()
  373.             {
  374.                 R8[] block = new R8[16];
  375.  
  376.                 for (byte i = 0; i < 16; i++) block[i] = new R8();
  377.                 if ((bits & 0xFFFF) != 0)
  378.                 {
  379.                     byte a = (byte) bits;
  380.                     byte b = (byte)(bits >> 8);
  381.                     double mode = a > b ? 7 : 5;
  382.                     long Val = a << 8 | b;
  383.                     for (byte i = 1; i < mode; i++)
  384.                         Val = (Val << 8) | KKtMain.CFTB(((mode - i) * a + i * b) / mode);
  385.                     if (a <= b)
  386.                         Val = Val << 16 | 0xFF;
  387.  
  388.                     long mask = bits >> 16;
  389.                     for (byte j = 0; j < 4; j++)
  390.                         for (byte i = 0; i < 4; i++, mask >>= 3)
  391.                             block[j * 4 + i].R = (byte)(Val >> (byte)(((7 - (mask & 0x07)) << 3)) & 0xFF);
  392.                 }
  393.                 return block;
  394.             }
  395.  
  396.             public R16[] ToR16()
  397.             {
  398.                 R8[] R = ToR8();
  399.                 R16[] R16 = new R16[16];
  400.                 for (byte i = 0; i < 16; i++) R16[i] = (R16)R[i];
  401.                 return R16;
  402.             }
  403.         }
  404.  
  405.         public class BC5
  406.         {
  407.             public BC4 R;
  408.             public BC4 G;
  409.  
  410.             public BC5(long R = 0, long G = 0)
  411.             { this.R = new BC4(R); this.G = new BC4(G); }
  412.  
  413.             public R8G8B8A8[] ToR8G8B8A8() => ToR8G8().ToR8G8B8A8();
  414.  
  415.             public R8G8[] ToR8G8()
  416.             {
  417.                 R8[] R = this.R.ToR8();
  418.                 R8[] G = this.G.ToR8();
  419.                 R8G8[] RG = new R8G8[16];
  420.                 for (byte i = 0; i < 16; i++) RG[i] = new R8G8(R[i].R, G[i].R);
  421.                 return RG;
  422.             }
  423.  
  424.             public R16G16[] ToR16G16()
  425.             {
  426.                 R16[] R = this.R.ToR16();
  427.                 R16[] G = this.G.ToR16();
  428.                 R16G16[] RG = new R16G16[16];
  429.                 for (byte i = 0; i < 16; i++) RG[i] = new R16G16(R[i].R, G[i].R);
  430.                 return RG;
  431.             }
  432.         }
  433.     }
  434. }
Add Comment
Please, Sign In to add comment