Ejemplo n.º 1
0
        public static byte[] bitmapToRawImg(ImageData img, int ID)
        {
            ImageType   t     = ImageTypesByID[ID];
            int         w     = (int)img.Width;
            int         h     = (int)img.Height;
            List <byte> array = new List <byte>(w * h * (int)t.e);
            //w = h = Math.Max(nlpo2(w), nlpo2(h));
            uint  x = 0, y = 0;
            Color c;
            int   p = gcm(w, 8) >> 3;

            if (p == 0)
            {
                p = 1;
            }
            if (!BinaryMath.IsPowerOfTwo(p))
            {
                throw new Exception("NOT POWER OF TWO");
            }
            int plog = BinaryMath.Log2(p);

            for (uint i = 0; i < w * h; i++)
            {
                d2xy(i.FastMod(64), out x, out y);
                uint tile = i >> 6;
                x += (uint)tile.FastMod(p) * 8;
                y += (uint)tile.FastDiv(plog) * 8;

                c = img.GetPixel(x, y);
                //if (c.A == 0) c = new Color { A = 0, R = 0xFF, G = 0xFF, B = 0xFF };
                if (t.e == Encoding.RGB565)
                {
                    uint val = (uint)((c.R >> 3) & 0x1f) << 11;
                    val += (uint)(((c.G >> 2) & 0x3f) << 5);
                    val += (uint)((c.B >> 3) & 0x1f);
                    array.Add((byte)(val & 0xFF));
                    array.Add((byte)((val >> 8) & 0xFF));
                }
                else if (t.e == Encoding.BGR888)
                {
                    array.AddRange(new byte[3] {
                        c.B, c.G, c.R
                    });
                }
                else if (t.e == Encoding.A8)
                {
                    array.Add((byte)c.R);
                }
            }

            return(array.ToArray());
        }
Ejemplo n.º 2
0
 public static uint FastDiv(this uint num, int num2)
 {
     return (uint)BinaryMath.DivideByPowerOf2((int)num, num2);
 }
Ejemplo n.º 3
0
        //Binary math is used to make % and / faster
        public static ImageData getImage(byte[] data, int ID)
        {
            ImageType t     = ImageTypesByID[ID];
            ImageData Image = new ImageData((uint)t.s.x, (uint)t.s.y);

            uint x = 0, y = 0;
            int  p = t.s.x >> 3;

            if (p == 0)
            {
                p = 1;
            }

            if (!BinaryMath.IsPowerOfTwo(p))
            {
                throw new Exception("NOT POWER OF TWO");
            }
            int          plog   = BinaryMath.Log2(p);
            BinaryReader dec_br = new BinaryReader(new MemoryStream(data));

            int red, blue, green;

            if (t.e == Encoding.RGB565)
            {
                uint i = 0;
                while (dec_br.BaseStream.Position < dec_br.BaseStream.Length)
                {
                    uint pix = dec_br.ReadUInt16();

                    d2xy(i.FastMod(64), out x, out y);
                    uint tile = i >> 6;
                    // Shift Tile Coordinate into Tilemap
                    x    += (uint)tile.FastMod(p) * 8;
                    y    += (uint)tile.FastDiv(plog) * 8;
                    red   = (byte)((pix >> 11) & 0x1f) * 8;
                    green = (byte)(((pix >> 5) & 0x3f)) * 4;
                    blue  = (byte)((pix) & 0x1f) * 8;
                    Image.SetPixel(x, y, red, green, blue, 0xFF);
                    i++;
                }
            }
            else if (t.e == Encoding.BGR888)
            {
                uint i = 0;
                while (dec_br.BaseStream.Position < dec_br.BaseStream.Length)
                {
                    d2xy(i.FastMod(64), out x, out y);
                    uint tile = i >> 6;
                    // Shift Tile Coordinate into Tilemap
                    x    += (uint)tile.FastMod(p) * 8;
                    y    += (uint)tile.FastDiv(plog) * 8;
                    blue  = dec_br.ReadByte();
                    green = dec_br.ReadByte();
                    red   = dec_br.ReadByte();
                    Image.SetPixel(x, y, red, green, blue, 0xFF);
                    i++;
                }
            }
            else if (t.e == Encoding.A8)
            {
                uint i = 0;
                while (dec_br.BaseStream.Position < dec_br.BaseStream.Length)
                {
                    d2xy(i.FastMod(64), out x, out y);
                    uint tile = i >> 6;
                    // Shift Tile Coordinate into Tilemap
                    x += (uint)tile.FastMod(p) * 8;
                    y += (uint)tile.FastDiv(plog) * 8;
                    byte col = dec_br.ReadByte();
                    Image.SetPixel(x, y, col, col, col, 0xFF);
                    i++;
                }
            }

            return(Image);
        }
Ejemplo n.º 4
0
 public static uint FastMod(this uint num, int num2)
 {
     return (uint)BinaryMath.FastMod((int)num, num2);
 }