Ejemplo n.º 1
0
        /*private static ByteTexture ConvertTexture(DWordTexture tex, out Palette p)
        {
            var top = GetTopColors(tex).Select(x => new ByteColor(x));
            p = new Palette() {Colour = top.ToArray()};
            var res = new ByteTexture(new byte[256][]);
            for (var y = 0; y < 256; y++)
            {
                res.Pixels[y] = new byte[256];

                for (var x = 0; x < 256; x++)
                {
                    res.Pixels[y][x] = (byte) closestColor1(top, new ByteColor(tex.Pixels[y][x]));
                }
            }
            return res;
        }*/

        private unsafe void Gen8bitPaletteFrom32bitTex(DWordTexture[] texs)
        {
            foreach (var bt in texs)
            {
                var bmp = new Bitmap(256, 256, PixelFormat.Format32bppArgb);
                var bmpData = bmp.LockBits(new Rectangle(0, 0, 256, 256), ImageLockMode.WriteOnly,
                    PixelFormat.Format32bppArgb);
                var scan0 = (uint*) bmpData.Scan0;
                for (var y = 0; y < 256; y++)
                {
                    var scanline = bt.Pixels[y];
                    fixed (uint* ptr = scanline)
                    {
                        for (var i = 0; i < 256; i++)
                            scan0[y * 256 + i] = (ptr[i] & 0xff00ff00) |
                                                 ((ptr[i] & 0x00ff0000) >> 16) |
                                                 ((ptr[i] & 0x000000ff) << 16);
                    }
                }
                var bmp8 = bmp.Clone(new Rectangle(0, 0, 256, 256), PixelFormat.Format8bppIndexed);
                Palette = new Palette() {Colour = bmp8.Palette.Entries.Select(x => (ByteColor) x).ToArray()};
            }
            /*var top = GetTopColors(texs).Select(x => new ByteColor(x)).ToArray();
            Palette = new Palette() { Colour = top };*/
        }
Ejemplo n.º 2
0
        private static DWordTexture ConvertTexture(Palette p, ByteTexture t)
        {
            var ret = new DWordTexture(new uint[256][]);
            for (var y = 0; y < 256; y++)
            {
                ret.Pixels[y] = new uint[256];

                for (var x = 0; x < 256; x++)
                {
                    var color = t.Pixels[y][x];
                    uint val = 0;
                    if(color > 0)
                    {
                        var palc = p.Colour[color];
                        val = (uint)(palc.R | (palc.G << 8) | (palc.B << 16) | (0xff << 24));
                    }
                    ret.Pixels[y][x] = val;
                }
            }
            return ret;
        }
Ejemplo n.º 3
0
        public void GenTexAndPalettesIfEmpty()
        {
            if (EngineVersion == Engine.TR2) return; // TR2 already has 8-bit, 16-bit and 32-bit tex and palette

            if ((Texture16 ?? new WordTexture[0]).Length == 0)
            {
                // Generate 16-bit textures
                Texture16 = new WordTexture[Textures.Length];
                for (var i = 0; i < Texture16.Length; i++)
                {
                    Texture16[i] = new WordTexture(new ushort[256][]);
                    for (var y = 0; y < 256; y++)
                    {
                        Texture16[i].Pixels[y] = new ushort[256];

                        for (var x = 0; x < 256; x++)
                        {
                            Texture16[i].Pixels[y][x] = new ByteColor(Textures[i].Pixels[y][x]).ToUInt16();
                        }
                    }
                }
            }

            

            if (WriteGameVersion < TRGame.TR4 && (Equals(Palette, default(Palette)) || (Texture8 ?? new ByteTexture[0]).Length == 0))
            {
                unsafe
                {
                    Texture8 = new ByteTexture[Textures.Length];

                    var bigBmp = new Bitmap(Textures.Length * 256, 256, PixelFormat.Format32bppArgb);
                    for (var bi = 0; bi < Textures.Length; bi++)
                    {
                        var bt = Textures[bi];
                        var bmp = new Bitmap(256, 256, PixelFormat.Format32bppArgb);
                        var bmpData = bmp.LockBits(new Rectangle(0, 0, 256, 256), ImageLockMode.WriteOnly,
                            PixelFormat.Format32bppArgb);
                        var scan0 = (uint*)bmpData.Scan0;
                        for (var y = 0; y < 256; y++)
                        {
                            var scanline = bt.Pixels[y];
                            fixed (uint* ptr = scanline)
                            {
                                for (var i = 0; i < 256; i++)
                                    scan0[y * 256 + i] = (ptr[i] & 0xff00ff00) |
                                                         ((ptr[i] & 0x00ff0000) >> 16) |
                                                         ((ptr[i] & 0x000000ff) << 16);
                            }
                        }
                        bmp.UnlockBits(bmpData);
                        using (var gr = Graphics.FromImage(bigBmp))
                            gr.DrawImage(bmp, new Rectangle(bi * 256, 0, 256, 256), 0, 0, 256, 256, GraphicsUnit.Pixel);
                    }
                    var bmp8 = bigBmp.Clone(new Rectangle(0, 0, bigBmp.Width, 256), PixelFormat.Format8bppIndexed);
                    Palette = new Palette() { Colour = bmp8.Palette.Entries.Select(x => (ByteColor)x).ToArray().Resize(256) };

                    for (var i = 0; i < Textures.Length; i++)
                    {
                        Texture8[i] = new ByteTexture(new byte[256][]);

                        for (var y = 0; y < 256; y++)
                        {
                            Texture8[i].Pixels[y] = new byte[256];

                            for (var x = 0; x < 256; x++)
                            {
                                Texture8[i].Pixels[y][x] = (byte)Array.IndexOf(bmp8.Palette.Entries, bmp8.GetPixel(i * 256 + x, y));
                            }
                        }
                    }




                    //Gen8bitPaletteFrom32bitTex(Textures);

                    // Generate 8-bit textures

                    /*for (var i = 0; i < Textures.Length; i++)
                    {
                        var tex = Textures[i];
                        Texture8[i] = new ByteTexture(new byte[256][]);
                        for (var y = 0; y < 256; y++)
                        {
                            Texture8[i].Pixels[y] = new byte[256];

                            for (var x = 0; x < 256; x++)
                            {
                                Texture8[i].Pixels[y][x] =
                                    (byte) Helper.closestColor1(Palette.Colour, new ByteColor(tex.Pixels[y][x]));
                            }
                        }
                    }*/
                }
            }
            if (WriteEngineVersion < Engine.TR4 && Equals(Palette16, default(Palette)))
            {
                Palette16 = new Palette {Colour = Palette.Colour.Select(x => new ByteColor(x.R, x.G, x.B, 0xFF)).ToArray()};
            }
        }