Ejemplo n.º 1
0
        public VTMapEntry FindTexture(string textureName, VTMap vtMap)
        {
            VTMapEntry tex = null;

            for (int i = 0; i < vtMap.Entries.Count; i++)
            {
                if (Path.GetFileName(vtMap.Entries[i].FileName).ToLower() == textureName.ToLower())
                {
                    return(vtMap.Entries[i]);
                }
            }

            return(tex);
        }
Ejemplo n.º 2
0
        public List <VTMapTile> GetTiles(VTMapEntry textureEntry)
        {
            List <VTMapTile> output = new List <VTMapTile>();

            int divisor = GetDivisor();
            int xPos    = PageNum == 0 ? textureEntry.Column : textureEntry.Column / divisor;
            int yPos    = PageNum == 0 ? textureEntry.Row : textureEntry.Row / divisor;
            int tileRow = (int)Math.Floor(yPos / (float)TileSize);
            int tileCol = (int)Math.Floor(xPos / (float)TileSize);

            tileRow = tileRow < 0 ? 0 : tileRow;
            tileCol = tileCol < 0 ? 0 : tileCol;

            int numTilesX = (int)Math.Ceiling(textureEntry.GetWidth(PageNum) / (float)TileSize);
            int numTilesY = (int)Math.Ceiling(textureEntry.GetHeight(PageNum) / (float)TileSize);

            for (int row = tileRow; row < Tiles.Count && row <= tileRow + numTilesY; row++)
            {
                int rowStart = row * TileSize;
                int rowEnd   = (row + 1) * TileSize;

                for (int col = tileCol; col < Tiles[row].Count && col <= tileCol + numTilesX; col++)
                {
                    if (Tiles[row][col] == null)
                    {
                        continue;
                    }

                    int colStart = col * TileSize;
                    int colEnd   = (col + 1) * TileSize;

                    if (Tiles[row][col].Texture == null)
                    {
                        Tiles[row][col].GetTextureFromZAD();
                    }
                    if (Tiles[row][col].Texture == null)
                    {
                        continue;
                    }

                    output.Add((from tile in Tiles[row][col].Coords where tile.Row == row && tile.Column == col select tile).First());
                }
            }

            return(output);
        }
Ejemplo n.º 3
0
        private void SaveTexture(VTMapEntry textureToExport, String FileName, VTPage vtPage)
        {
            var         fileType  = Path.GetExtension(FileName).ToUpper();
            ImageFormat imgFormat = ImageFormat.Png;

            if (fileType == ".TGA")
            {
                vtPage.SaveTexture(textureToExport, FileName, false, true, false, ImageFormat.Png);
            }
            else if (fileType == ".TDX")
            {
                vtPage.SaveTexture(textureToExport, FileName, true, false, false, ImageFormat.Png);
            }
            else
            {
                switch (fileType)
                {
                case ".JPG":
                    imgFormat = ImageFormat.Jpeg;
                    break;

                case ".JPEG":
                    imgFormat = ImageFormat.Jpeg;
                    break;

                case ".TIFF":
                    imgFormat = ImageFormat.Tiff;
                    break;

                case ".TIF":
                    imgFormat = ImageFormat.Tiff;
                    break;

                case ".BMP":
                    imgFormat = ImageFormat.Bmp;
                    break;

                case ".PNG":
                    imgFormat = ImageFormat.Png;
                    break;
                }

                vtPage.SaveTexture(textureToExport, FileName, imgFormat);
            }
        }
Ejemplo n.º 4
0
        public VTTextureResult FindTexture(string textureName)
        {
            List <string>   vtList = GetVTList();
            VTTextureResult result = new VTTextureResult();

            foreach (string vtPath in vtList)
            {
                VirtualTextureDef vtDef = LoadVT(vtPath);
                VTMapEntry        tex   = FindTexture(textureName, vtDef);
                if (tex != null)
                {
                    result.vtDef   = vtDef;
                    result.Texture = tex;
                    break;
                }
            }

            return(result);
        }
Ejemplo n.º 5
0
        public VTMapEntry FindTexture(string textureName, VirtualTextureDef vtDef, bool searchDiffuse, bool searchSpecular, bool searchNormals)
        {
            VTMapEntry tex = null;

            if (searchDiffuse)
            {
                tex = FindTexture(textureName, vtDef.DiffuseMap);
            }

            if (searchSpecular && tex == null)
            {
                tex = FindTexture(textureName, vtDef.SpecularMap);
            }

            if (searchNormals && tex == null)
            {
                tex = FindTexture(textureName, vtDef.NormalMap);
            }

            return(tex);
        }
Ejemplo n.º 6
0
        public VTMap(byte[] buff)
        {
            using (MemoryStream ms = new MemoryStream(buff))
            using (BinaryReader br = new BinaryReader(ms))
            {
                type = (VTMapType)br.ReadInt16();

                if (!IsDeadBeef(br))
                {
                    Logger.LogToFile(Logger.LogLevel.Error, "OH F**K IT'S NOT DEADBEEF #1!\nPosition: {0:x2}", br.BaseStream.Position);
                    return;
                }

                width = br.ReadInt32();
                height = br.ReadInt32();
                pageCount = br.ReadInt32();
                tileSize = br.ReadInt32();
                tilePadding = br.ReadInt32();

                if (!IsDeadBeef(br))
                {
                    Logger.LogToFile(Logger.LogLevel.Error, "OH F**K IT'S NOT DEADBEEF #2!\nPosition: {0:x2}", br.BaseStream.Position);
                    return;
                }

                textureCount = br.ReadInt32();

                for (int i = 0; i < textureCount; i++)
                {
                    VTMapEntry entry = new VTMapEntry();
                    entry.Row = br.ReadInt32();
                    entry.Column = br.ReadInt32();
                    entry.Width = br.ReadInt32();
                    entry.Height = br.ReadInt32();
                    entry.FileName = br.ReadNullTerminatedString();
                    br.ReadByte();

                    entries.Add(entry);
                }

                if (!IsDeadBeef(br))
                {
                    Logger.LogToFile(Logger.LogLevel.Error, "OH F**K IT'S NOT DEADBEEF #3!\nPosition: {0:x2}", br.BaseStream.Position);
                    return;
                }

                numberOfTiles = br.ReadInt32();
                int j = 0;
                for (int i = 0; i < numberOfTiles; i++)
                {
                    VTMapTile tile = new VTMapTile();
                    tile.Column = br.ReadInt32();
                    tile.Row = br.ReadInt32();
                    tile.Page = br.ReadInt32();

                    tile.Hash = br.ReadUInt32();
                    tile.TileName = string.Format("{0:x8}", tile.Hash);
                    tile.ZadTileName = string.Format("{0}/{1}_{2}.tdx", tile.TileName.Substring(0, 2), tile.TileName, type.ToString().Substring(0, 1));

                    //Logger.LogToFile("\tTile {0}\n\t\t FileName: {1}\n\t\tUnknown1: {2}\n\t\tUnknown2: {3}\n\t\tUnknown3: {4}", j, BitConverter.ToString( tile.TileName), tile.Unknown1, tile.Unknown2, tile.Unknown3);
                    j++;
                    if (!TilesByName.ContainsKey(tile.TileName))
                    {
                        VTMapTileTDX tileTDX = new VTMapTileTDX();
                        tileTDX.TileName = tile.TileName;
                        tileTDX.Coords.Add(tile);
                        TilesByName.Add(tile.TileName, tileTDX);
                    }
                    else
                    {
                        //Logger.LogToFile("Tile #{0} \"{1}\" already in TilesByName", i, tile.TileNameString);
                        TilesByName[tile.TileName].Coords.Add(tile);
                    }
                    if (tile.Page >= TilesPages.Count)
                    {
                        for (int x = TilesPages.Count; x <= tile.Page; x++)
                        {
                            TilesPages.Add(new VTMapPage() { PageNumber = tile.Page });
                        }
                    }
                    TilesPages[tile.Page].AddTile(tile);
                }
                if (!IsDeadBeef(br))
                {
                    Logger.LogToFile(Logger.LogLevel.Error, "OH F**K IT'S NOT DEADBEEF #4!\nPosition: {0}", br.BaseStream.Position);
                    return;
                }
                int anotherTextureCount = br.ReadInt32();
                List<int> unknownValues = new List<int>();
                for (int i = 0; i < anotherTextureCount; i++)
                {
                    string fileName = br.ReadNullTerminatedString();

                    if (entries[i].FileName == fileName)
                    {
                        entries[i].TimeStamp = br.ReadInt32();
                        entries[i].Unknown2 = br.ReadInt32();
                        if (!unknownValues.Contains(entries[i].TimeStamp)) unknownValues.Add(entries[i].TimeStamp);
                    }
                    else
                    {
                        for (int k = 0; k < entries.Count; k++)
                        {
                            if (entries[k].FileName == fileName)
                            {
                                entries[k].TimeStamp = br.ReadInt32();
                                entries[k].Unknown2 = br.ReadInt32();
                                if (!unknownValues.Contains(entries[k].TimeStamp)) unknownValues.Add(entries[k].TimeStamp);
                                break;
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 7
0
        public void SaveTextureTGA(VTMapEntry textureEntry, string outputPath)
        {
            int divisor = GetDivisor();
            int xPos = PageNum == 0 ? textureEntry.Column : textureEntry.Column / divisor; //(int)Math.Floor(((double)textureEntry.Column / Pages[0].Width) * Width);
            int yPos = PageNum == 0 ? textureEntry.Row : textureEntry.Row / divisor; // (int)Math.Floor(((double)textureEntry.Row / Pages[0].Height) * Height);
            int tileRow = (int)Math.Floor(yPos / (float)TileSize);
            int tileCol = (int)Math.Floor(xPos / (float)TileSize);
            tileRow = tileRow < 0 ? 0 : tileRow;
            tileCol = tileCol < 0 ? 0 : tileCol;
            int numTilesX = (int)Math.Ceiling(textureEntry.GetWidth(PageNum) / (float)TileSize);
            int numTilesY = (int)Math.Ceiling(textureEntry.GetHeight(PageNum) / (float)TileSize);

            int bitmapheight = textureEntry.GetHeight(PageNum);
            int bitmapwidth = textureEntry.GetWidth(PageNum);
            Bitmap stitched = new Bitmap(bitmapwidth, bitmapheight);

            string tileprefix = Path.GetFullPath(outputPath) + Path.GetFileNameWithoutExtension(outputPath);

            if (!Directory.Exists(Path.GetDirectoryName(outputPath))) Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
            if (File.Exists(outputPath)) File.Delete(outputPath);
            int pos;
            using (var stream = new FileStream(outputPath, FileMode.OpenOrCreate))
            {
                using (var writer = new BinaryWriter(stream))
                {
                    int tgaWidth = Width;
                    int tgaHeight = Height;
                    int tgaTileRowCount = tiles.Count - 1;

                    writer.Write((byte)0);
                    writer.Write((byte)0);
                    writer.Write((byte)2);
                    writer.Write((short)0);
                    writer.Write((short)0);
                    writer.Write((byte)0);
                    writer.Write((short)0);
                    writer.Write((short)0);
                    writer.Write((short)bitmapwidth);
                    writer.Write((short)bitmapheight);
                    writer.Write((byte)32);
                    writer.Write((byte)0);

                    pos = (int)writer.BaseStream.Position;

                    List<byte[]> rowTiles = new List<byte[]>();

                    //writer.Seek(pos, SeekOrigin.Begin);
                    for (int row = tileRow + numTilesY; row >= tileRow && row >= 0; row--)
                    {
                        rowTiles.Clear();
                        int rowStart = row * (TileSize+TilePadding+TilePadding) * tgaWidth * 4;

                        int tgaTileColCount = tiles[tileRow].Count;

                        //if (colStart >= 128 * maxTilesToStitch) break;
                        for (int y = (TileSize+TilePadding+TilePadding)-1; y >= 0; y--)
                        {

                            for (int col = tileCol; col < tiles[row].Count && col <= tileCol + numTilesX; col++)
                            {
                                if (y == (TileSize + TilePadding + TilePadding)-1)
                                {
                                    if (tiles[row][col] == null)
                                    {
                                        rowTiles.Add(Enumerable.Repeat((byte)0, 4 * (TileSize + TilePadding + TilePadding) * (TileSize + TilePadding + TilePadding)).ToArray());
                                    }
                                    else
                                    {
                                        //if (rowStart >= 128 * maxTilesToStitch) break;
                                        if (tiles[row][col].Texture == null) tiles[row][col].GetTextureFromZAD();
                                        rowTiles.Add(tiles[row][col].Texture.Decompress(tiles[row][col].Texture.MipMaps[0]));
                                    }
                                }
                                int colStart = col * (TileSize + TilePadding + TilePadding) * 4 + rowStart;
                                //for (int y = 0; y < tiles[tileRow][tileCol].Texture.MipMaps[0].Height; y++)
                                {
                                    var decompressed = rowTiles[col];

                                    for (int x = 0; x < (TileSize + TilePadding + TilePadding); x++)
                                    {
                                        int pixel = y * (TileSize + TilePadding + TilePadding) * 4 + x * 4;

                                        writer.Write((byte)decompressed[pixel + 0]);
                                        writer.Write((byte)decompressed[pixel + 1]);
                                        writer.Write((byte)decompressed[pixel + 2]);
                                        writer.Write((byte)decompressed[pixel + 3]);

                                    }
                                }
                            }
                        }
                    }
                }
            }

            //stitched.Save(File.OpenWrite(outputPath), format);
        }
Ejemplo n.º 8
0
        public void SaveTexture(VTMapEntry textureEntry, string outputPath, bool SaveTDX, bool SaveTGA, bool SaveOther, ImageFormat format)
        {
            int divisor = GetDivisor();
            int xPos = PageNum == 0 ? textureEntry.Column : textureEntry.Column / divisor; //(int)Math.Floor(((double)textureEntry.Column / Pages[0].Width) * Width);
            int yPos = PageNum == 0 ? textureEntry.Row : textureEntry.Row / divisor; // (int)Math.Floor(((double)textureEntry.Row / Pages[0].Height) * Height);
            int tileRow = (int)Math.Floor(yPos / (float)TileSize);
            int tileCol = (int)Math.Floor(xPos / (float)TileSize);
            tileRow = tileRow < 0 ? 0 : tileRow;
            tileCol = tileCol < 0 ? 0 : tileCol;
            int numTilesX = (int)Math.Ceiling(textureEntry.GetWidth(PageNum) / (float)TileSize);
            int numTilesY = (int)Math.Ceiling(textureEntry.GetHeight(PageNum) / (float)TileSize);

            int bitmapheight = textureEntry.GetHeight(PageNum);
            int bitmapwidth = textureEntry.GetWidth(PageNum);
            Bitmap stitched = new Bitmap(bitmapwidth, bitmapheight);

            //string tileprefix = Path.GetFullPath(outputPath) + Path.GetFileNameWithoutExtension(outputPath);
            //if (textureEntry.FileName == "Data_Core\\Content\\Levels\\outskirts\\accessories\\sw_Museum_Moon\\sw_Museum_Moon_S")
            {
                //Console.WriteLine("This is sw_Museum_moon");
            }
            D3DFormat guessedFormat = D3DFormat.DXT5;
            bool formatGuessed = false;
            for (int row = tileRow; row < tiles.Count && row <= tileRow + numTilesY; row++)
            {

                int rowStart = row * TileSize;
                int rowEnd = (row + 1) * TileSize;
                //if (yPos + bitmapheight < rowStart) break;
                //if (yPos >= rowEnd) continue;
                //if (colStart >= 128 * maxTilesToStitch) break;
                for (int col = tileCol; col < tiles[row].Count && col <= tileCol + numTilesX; col++)
                {
                    //if (row == 70 && col == 67)
                    {
                        //Logger.LogToFile("this is row 70, col 67");
                    }
                    if (tiles[row][col] == null) continue;
                    //tiles[tileCol][tileRow].Texture.SaveAsDDS(@"E:\Games\Steam\SteamApps\common\Carmageddon_Reincarnation\ZAD_VT\outskirts\output\" + "tile_" + tileCol + "_" + tileRow);
                    int colStart = col * TileSize;
                    int colEnd = (col + 1) * TileSize;

                    //if (xPos >= colEnd) continue;
                    //if (xPos + bitmapwidth < colStart) break;
                    //if (rowStart >= 128 * maxTilesToStitch) break;

                    if (tiles[row][col].Texture == null) tiles[row][col].GetTextureFromZAD();
                    if (tiles[row][col].Texture == null) continue;
                    if (!formatGuessed) guessedFormat = tiles[row][col].Texture.Format;
                    Bitmap decompressed = tiles[row][col].Texture.Decompress(0, false);
                    //decompressed.Save(tileprefix + "_tile_" + row + "_" + col + ".png");

                    int firstPixelRow = rowStart < yPos ? yPos - rowStart : TilePadding;
                    int lastPixelRow = TileSize+ TilePadding;// rowEnd >= yPos + bitmapheight ? rowEnd - (yPos + bitmapheight) : 124;

                    int firstPixelCol = colStart < xPos ? xPos - colStart : TilePadding;
                    int lastPixelCol = TileSize+ TilePadding;// colEnd > xPos + bitmapwidth ? colEnd - (xPos + bitmapwidth) : 124;
                    //BitmapData bmd = stiched.LockBits(new Rectangle(colStart, rowStart, 128, 128), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
                    for (int y = firstPixelRow; y < lastPixelRow/*rowPair.Value.MipMaps[0].Height*/; y++)
                    {
                        if ((row * TileSize + (y - TilePadding)) - (yPos) >= bitmapheight) break;
                        for (int x = firstPixelCol; x < lastPixelCol /*rowPair.Value.MipMaps[0].Width*/; x++)
                        {
                            if ((col * TileSize + (x - TilePadding)) - xPos >= bitmapwidth) break;
                            //int oldPixel = y * 128 + x;
                            //int newPixel = ((row * 120 + y-8) * Width - (yPos) * Width) + ((col * 120 + x-4) - xPos); //((rowStart + y) * (128 * maxTilesToStitch) + colStart + x);
                            Color pixelColour = decompressed.GetPixel(x, y);

                            stitched.SetPixel((col * TileSize + (x - TilePadding)) - xPos, (row * TileSize + (y - TilePadding)) - (yPos), pixelColour);
                            /*tdx.MipMaps[0].Data[newPixel] = pixelColour.R;
                            tdx.MipMaps[0].Data[newPixel+1] = pixelColour.G;
                            tdx.MipMaps[0].Data[newPixel+2] = pixelColour.B;
                            tdx.MipMaps[0].Data[newPixel+3] = pixelColour.A; //rowPair.Value.MipMaps[0].Data[oldPixel];*/
                        }
                    }
                    //stiched.UnlockBits(bmd);
                }
            }

            if (!Directory.Exists(Path.GetDirectoryName(outputPath))) Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
            if (SaveTDX)
            {
                TDX newtdx = TDX.LoadFromBitmap(stitched, Path.GetFileNameWithoutExtension(outputPath), guessedFormat);
                newtdx.Save(outputPath);
            }
            if (SaveTGA)
            {
                if (File.Exists(outputPath)) File.Delete(outputPath);

                using (var stream = new FileStream(outputPath, FileMode.OpenOrCreate))
                {
                    using (var writer = new BinaryWriter(stream))
                    {
                        int tgaWidth = bitmapwidth;
                        int tgaHeight = bitmapheight;
                        int tgaTileRowCount = tiles.Count - 1;

                        writer.Write((byte)0);
                        writer.Write((byte)0);
                        writer.Write((byte)2);
                        writer.Write((short)0);
                        writer.Write((short)0);
                        writer.Write((byte)0);
                        writer.Write((short)0);
                        writer.Write((short)0);
                        writer.Write((short)bitmapwidth);
                        writer.Write((short)bitmapheight);
                        writer.Write((byte)32);
                        writer.Write((byte)0);
                        BitmapData data = stitched.LockBits(new Rectangle(0, 0, stitched.Width, stitched.Height), ImageLockMode.ReadOnly, stitched.PixelFormat);
                        unsafe
                        {
                            // important to use the BitmapData object's Width and Height
                            // properties instead of the Bitmap's.
                            for (int x = data.Height - 1; x >= 0; x--)
                            {
                                byte* row = (byte*)data.Scan0 + (x * data.Stride);
                                for (int y = 0; y < data.Width; y++)
                                {
                                    int columnOffset = y * 4;
                                    byte B = row[columnOffset];
                                    byte G = row[columnOffset + 1];
                                    byte R = row[columnOffset + 2];
                                    byte alpha = row[columnOffset + 3];
                                    writer.Write(B);
                                    writer.Write(G);
                                    writer.Write(R);
                                    writer.Write(alpha);
                                }
                            }
                        }
                        stitched.UnlockBits(data);
                    }
                }
            }
            if (SaveOther)
            {
                using (var str = File.OpenWrite(outputPath))
                {
                    stitched.Save(str, format);
                }
            }
            stitched.Dispose();
        }
Ejemplo n.º 9
0
 public void SaveTexture(VTMapEntry textureEntry, string outputPath, ImageFormat format)
 {
     SaveTexture(textureEntry, outputPath, false, false, true, format);
 }
Ejemplo n.º 10
0
 public void SaveTexture(VTMapEntry textureEntry, string outputPath)
 {
     SaveTexture(textureEntry, outputPath, ImageFormat.Png);
 }
Ejemplo n.º 11
0
        public List<VTMapTileTDX> ImportTexture(Bitmap image, VTMapEntry textureEntry)
        {
            var output = new List<VTMapTileTDX>();

            int divisor = GetDivisor();
            int xPos = PageNum == 0 ? textureEntry.Column : textureEntry.Column / divisor; //(int)Math.Floor(((double)textureEntry.Column / Pages[0].Width) * Width);
            int yPos = PageNum == 0 ? textureEntry.Row : textureEntry.Row / divisor; // (int)Math.Floor(((double)textureEntry.Row / Pages[0].Height) * Height);
            int tileRow = (int)Math.Floor(yPos / (float)TileSize);
            int tileCol = (int)Math.Floor(xPos / (float)TileSize);
            tileRow = tileRow < 0 ? 0 : tileRow;
            tileCol = tileCol < 0 ? 0 : tileCol;
            int numTilesX = (int)Math.Ceiling(textureEntry.GetWidth(PageNum) / (float)TileSize);
            int numTilesY = (int)Math.Ceiling(textureEntry.GetHeight(PageNum) / (float)TileSize);

            int bitmapheight = textureEntry.GetHeight(PageNum);
            int bitmapwidth = textureEntry.GetWidth(PageNum);

            for (int row = tileRow; row < tiles.Count && row <= tileRow + numTilesY; row++)
            {

                int rowStart = row * TileSize;
                int rowEnd = (row + 1) * TileSize;
                //if (yPos + bitmapheight < rowStart) break;
                //if (yPos >= rowEnd) continue;
                //if (colStart >= 128 * maxTilesToStitch) break;
                for (int col = tileCol; col < tiles[row].Count && col <= tileCol + numTilesX; col++)
                {
                    //if (row == 70 && col == 67)
                    {
                        //Logger.LogToFile("this is row 70, col 67");
                    }
                    if (tiles[row][col] == null) continue;
                    //tiles[tileCol][tileRow].Texture.SaveAsDDS(@"E:\Games\Steam\SteamApps\common\Carmageddon_Reincarnation\ZAD_VT\outskirts\output\" + "tile_" + tileCol + "_" + tileRow);
                    int colStart = col * TileSize;
                    int colEnd = (col + 1) * TileSize;

                    if (tiles[row][col].Texture == null) tiles[row][col].GetTextureFromZAD();
                    if (tiles[row][col].Texture == null) continue;

                    Bitmap decompressed = tiles[row][col].Texture.Decompress(0, false);

                    int firstPixelRow = rowStart < yPos ? yPos - rowStart : 0;// TilePadding;
                    int lastPixelRow = TileSize+TilePadding+TilePadding;// rowEnd >= yPos + bitmapheight ? rowEnd - (yPos + bitmapheight) : 124;

                    int firstPixelCol = colStart < xPos ? xPos - colStart : 0;// TilePadding;
                    int lastPixelCol = TileSize+TilePadding+TilePadding;// colEnd > xPos + bitmapwidth ? colEnd - (xPos + bitmapwidth) : 124;

                    for (int y = firstPixelRow; y < lastPixelRow; y++)
                    {
                        if ((row * TileSize + (y - TilePadding)) - (yPos) >= bitmapheight+TilePadding) break;
                        for (int x = firstPixelCol; x < lastPixelCol; x++)
                        {
                            if ((col * TileSize + (x - TilePadding)) - xPos >= bitmapwidth + TilePadding) break;
                            var originalX = (col * TileSize + (x - TilePadding)) - xPos;
                            var originalY = (row * TileSize + (y - TilePadding)) - (yPos);

                            if (originalX < 0) originalX = 0;
                            else if (originalX >= bitmapwidth) originalX = bitmapwidth - 1;
                            if (originalY < 0) originalY = 0;
                            else if (originalY >= bitmapheight) originalY = bitmapheight - 1;

                            Color colour = image.GetPixel(originalX, originalY );
                            decompressed.SetPixel(x, y, colour);

                        }
                    }
                    TDX newTileTDX = TDX.LoadFromBitmap(decompressed, tiles[row][col].Texture.Name, tiles[row][col].Texture.Format);
                    newTileTDX.SetFlags(TDX.Flags.Unknown8 | TDX.Flags.Unknown128);
                    tiles[row][col].Texture = newTileTDX;
                    output.Add(tiles[row][col]);

                }
            }

            return output;
        }
Ejemplo n.º 12
0
        public List<VTMapTile> GetTiles(VTMapEntry textureEntry)
        {
            var output = new List<VTMapTile>();

            int divisor = GetDivisor();
            int xPos = PageNum == 0 ? textureEntry.Column : textureEntry.Column / divisor; //(int)Math.Floor(((double)textureEntry.Column / Pages[0].Width) * Width);
            int yPos = PageNum == 0 ? textureEntry.Row : textureEntry.Row / divisor; // (int)Math.Floor(((double)textureEntry.Row / Pages[0].Height) * Height);
            int tileRow = (int)Math.Floor(yPos / (float)TileSize);
            int tileCol = (int)Math.Floor(xPos / (float)TileSize);
            tileRow = tileRow < 0 ? 0 : tileRow;
            tileCol = tileCol < 0 ? 0 : tileCol;
            int numTilesX = (int)Math.Ceiling(textureEntry.GetWidth(PageNum) / (float)TileSize);
            int numTilesY = (int)Math.Ceiling(textureEntry.GetHeight(PageNum) / (float)TileSize);
            for (int row = tileRow; row < tiles.Count && row <= tileRow + numTilesY; row++)
            {

                int rowStart = row * TileSize;
                int rowEnd = (row + 1) * TileSize;
                //if (yPos + bitmapheight < rowStart) break;
                //if (yPos >= rowEnd) continue;
                //if (colStart >= 128 * maxTilesToStitch) break;
                for (int col = tileCol; col < tiles[row].Count && col <= tileCol + numTilesX; col++)
                {
                    if (tiles[row][col] == null) continue;
                    //tiles[tileCol][tileRow].Texture.SaveAsDDS(@"E:\Games\Steam\SteamApps\common\Carmageddon_Reincarnation\ZAD_VT\outskirts\output\" + "tile_" + tileCol + "_" + tileRow);
                    int colStart = col * TileSize;
                    int colEnd = (col + 1) * TileSize;

                    //if (xPos >= colEnd) continue;
                    //if (xPos + bitmapwidth < colStart) break;
                    //if (rowStart >= 128 * maxTilesToStitch) break;

                    if (tiles[row][col].Texture == null) tiles[row][col].GetTextureFromZAD();
                    if (tiles[row][col].Texture == null) continue;
                    output.Add((from tile in tiles[row][col].Coords where tile.Row == row && tile.Column == col select tile).First());
                    //stiched.UnlockBits(bmd);
                }
            }

            return output;
        }
Ejemplo n.º 13
0
        public void SaveTextureTGA(VTMapEntry textureEntry, string outputPath)
        {
            int divisor = GetDivisor();
            int xPos    = PageNum == 0 ? textureEntry.Column : textureEntry.Column / divisor;
            int yPos    = PageNum == 0 ? textureEntry.Row : textureEntry.Row / divisor;
            int tileRow = (int)Math.Floor(yPos / (float)TileSize);
            int tileCol = (int)Math.Floor(xPos / (float)TileSize);

            tileRow = tileRow < 0 ? 0 : tileRow;
            tileCol = tileCol < 0 ? 0 : tileCol;
            int numTilesX = (int)Math.Ceiling(textureEntry.GetWidth(PageNum) / (float)TileSize);
            int numTilesY = (int)Math.Ceiling(textureEntry.GetHeight(PageNum) / (float)TileSize);

            int    bitmapheight = textureEntry.GetHeight(PageNum);
            int    bitmapwidth  = textureEntry.GetWidth(PageNum);
            Bitmap stitched     = new Bitmap(bitmapwidth, bitmapheight);

            string tileprefix = Path.GetFullPath(outputPath) + Path.GetFileNameWithoutExtension(outputPath);

            if (!Directory.Exists(Path.GetDirectoryName(outputPath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
            }
            if (File.Exists(outputPath))
            {
                File.Delete(outputPath);
            }
            int pos;

            using (FileStream stream = new FileStream(outputPath, FileMode.OpenOrCreate))
                using (BinaryWriter writer = new BinaryWriter(stream))
                {
                    int tgaWidth        = Width;
                    int tgaHeight       = Height;
                    int tgaTileRowCount = Tiles.Count - 1;

                    writer.Write((byte)0);
                    writer.Write((byte)0);
                    writer.Write((byte)2);
                    writer.Write((short)0);
                    writer.Write((short)0);
                    writer.Write((byte)0);
                    writer.Write((short)0);
                    writer.Write((short)0);
                    writer.Write((short)bitmapwidth);
                    writer.Write((short)bitmapheight);
                    writer.Write((byte)32);
                    writer.Write((byte)0);

                    pos = (int)writer.BaseStream.Position;

                    List <byte[]> rowTiles = new List <byte[]>();

                    for (int row = tileRow + numTilesY; row >= tileRow && row >= 0; row--)
                    {
                        rowTiles.Clear();
                        int rowStart = row * (TileSize + TilePadding + TilePadding) * tgaWidth * 4;

                        int tgaTileColCount = Tiles[tileRow].Count;

                        for (int y = (TileSize + TilePadding + TilePadding) - 1; y >= 0; y--)
                        {
                            for (int col = tileCol; col < Tiles[row].Count && col <= tileCol + numTilesX; col++)
                            {
                                if (y == (TileSize + TilePadding + TilePadding) - 1)
                                {
                                    if (Tiles[row][col] == null)
                                    {
                                        rowTiles.Add(Enumerable.Repeat((byte)0, 4 * (TileSize + TilePadding + TilePadding) * (TileSize + TilePadding + TilePadding)).ToArray());
                                    }
                                    else
                                    {
                                        if (Tiles[row][col].Texture == null)
                                        {
                                            Tiles[row][col].GetTextureFromZAD();
                                        }

                                        // TODO: fix this
                                        //rowTiles.Add(Tiles[row][col].Texture.Decompress(Tiles[row][col].Texture.MipMaps[0]));
                                    }
                                }
                                int    colStart     = col * (TileSize + TilePadding + TilePadding) * 4 + rowStart;
                                byte[] decompressed = rowTiles[col];

                                for (int x = 0; x < (TileSize + TilePadding + TilePadding); x++)
                                {
                                    int pixel = y * (TileSize + TilePadding + TilePadding) * 4 + x * 4;

                                    writer.Write((byte)decompressed[pixel + 0]);
                                    writer.Write((byte)decompressed[pixel + 1]);
                                    writer.Write((byte)decompressed[pixel + 2]);
                                    writer.Write((byte)decompressed[pixel + 3]);
                                }
                            }
                        }
                    }
                }
        }
Ejemplo n.º 14
0
        public void SaveTexture(VTMapEntry textureEntry, string outputPath, bool SaveTDX, bool SaveTGA, bool SaveOther, ImageFormat format)
        {
            int divisor = GetDivisor();
            int xPos    = PageNum == 0 ? textureEntry.Column : textureEntry.Column / divisor;
            int yPos    = PageNum == 0 ? textureEntry.Row : textureEntry.Row / divisor;
            int tileRow = (int)Math.Floor(yPos / (float)TileSize);
            int tileCol = (int)Math.Floor(xPos / (float)TileSize);

            tileRow = tileRow < 0 ? 0 : tileRow;
            tileCol = tileCol < 0 ? 0 : tileCol;

            int numTilesX = (int)Math.Ceiling(textureEntry.GetWidth(PageNum) / (float)TileSize);
            int numTilesY = (int)Math.Ceiling(textureEntry.GetHeight(PageNum) / (float)TileSize);

            int    bitmapheight = textureEntry.GetHeight(PageNum);
            int    bitmapwidth  = textureEntry.GetWidth(PageNum);
            Bitmap stitched     = new Bitmap(bitmapwidth, bitmapheight);

            D3DFormat guessedFormat = D3DFormat.DXT5;
            bool      formatGuessed = false;

            for (int row = tileRow; row < Tiles.Count && row <= tileRow + numTilesY; row++)
            {
                int rowStart = row * TileSize;
                int rowEnd   = (row + 1) * TileSize;

                for (int col = tileCol; col < Tiles[row].Count && col <= tileCol + numTilesX; col++)
                {
                    if (Tiles[row][col] == null)
                    {
                        continue;
                    }

                    int colStart = col * TileSize;
                    int colEnd   = (col + 1) * TileSize;

                    if (Tiles[row][col].Texture == null)
                    {
                        Tiles[row][col].GetTextureFromZAD();
                    }
                    if (Tiles[row][col].Texture == null)
                    {
                        continue;
                    }
                    if (!formatGuessed)
                    {
                        guessedFormat = Tiles[row][col].Texture.Format;
                    }

                    Bitmap decompressed = Tiles[row][col].Texture.GetBitmap();

                    int firstPixelRow = rowStart < yPos ? yPos - rowStart : TilePadding;
                    int lastPixelRow  = TileSize + TilePadding;

                    int firstPixelCol = colStart < xPos ? xPos - colStart : TilePadding;
                    int lastPixelCol  = TileSize + TilePadding;

                    for (int y = firstPixelRow; y < lastPixelRow; y++)
                    {
                        if ((row * TileSize + (y - TilePadding)) - (yPos) >= bitmapheight)
                        {
                            break;
                        }
                        for (int x = firstPixelCol; x < lastPixelCol; x++)
                        {
                            if ((col * TileSize + (x - TilePadding)) - xPos >= bitmapwidth)
                            {
                                break;
                            }

                            Color pixelColour = decompressed.GetPixel(x, y);

                            stitched.SetPixel((col * TileSize + (x - TilePadding)) - xPos, (row * TileSize + (y - TilePadding)) - (yPos), pixelColour);
                        }
                    }
                }
            }

            if (!Directory.Exists(Path.GetDirectoryName(outputPath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
            }

            if (SaveTDX)
            {
                TDX newtdx = TDX.LoadFromBitmap(stitched, Path.GetFileNameWithoutExtension(outputPath), guessedFormat);
                newtdx.Save(outputPath);
            }

            if (SaveTGA)
            {
                if (File.Exists(outputPath))
                {
                    File.Delete(outputPath);
                }

                using (FileStream stream = new FileStream(outputPath, FileMode.OpenOrCreate))
                    using (BinaryWriter writer = new BinaryWriter(stream))
                    {
                        int tgaWidth        = bitmapwidth;
                        int tgaHeight       = bitmapheight;
                        int tgaTileRowCount = Tiles.Count - 1;

                        writer.Write((byte)0);
                        writer.Write((byte)0);
                        writer.Write((byte)2);
                        writer.Write((short)0);
                        writer.Write((short)0);
                        writer.Write((byte)0);
                        writer.Write((short)0);
                        writer.Write((short)0);
                        writer.Write((short)bitmapwidth);
                        writer.Write((short)bitmapheight);
                        writer.Write((byte)32);
                        writer.Write((byte)0);
                        BitmapData data = stitched.LockBits(new Rectangle(0, 0, stitched.Width, stitched.Height), ImageLockMode.ReadOnly, stitched.PixelFormat);
                        unsafe
                        {
                            // important to use the BitmapData object's Width and Height
                            // properties instead of the Bitmap's.
                            for (int x = data.Height - 1; x >= 0; x--)
                            {
                                byte *row = (byte *)data.Scan0 + (x * data.Stride);
                                for (int y = 0; y < data.Width; y++)
                                {
                                    int  columnOffset = y * 4;
                                    byte B            = row[columnOffset];
                                    byte G            = row[columnOffset + 1];
                                    byte R            = row[columnOffset + 2];
                                    byte alpha        = row[columnOffset + 3];
                                    writer.Write(B);
                                    writer.Write(G);
                                    writer.Write(R);
                                    writer.Write(alpha);
                                }
                            }
                        }
                        stitched.UnlockBits(data);
                    }
            }

            if (SaveOther)
            {
                using (FileStream str = File.OpenWrite(outputPath))
                {
                    stitched.Save(str, format);
                }
            }

            stitched.Dispose();
        }
Ejemplo n.º 15
0
 public void SaveTexture(VTMapEntry textureEntry, string outputPath, ImageFormat format)
 {
     SaveTexture(textureEntry, outputPath, false, false, true, format);
 }
Ejemplo n.º 16
0
 public void SaveTexture(VTMapEntry textureEntry, string outputPath)
 {
     SaveTexture(textureEntry, outputPath, ImageFormat.Png);
 }
Ejemplo n.º 17
0
        public List <VTMapTileTDX> ImportTexture(Bitmap image, VTMapEntry textureEntry)
        {
            List <VTMapTileTDX> output = new List <VTMapTileTDX>();

            int divisor = GetDivisor();
            int xPos    = PageNum == 0 ? textureEntry.Column : textureEntry.Column / divisor;
            int yPos    = PageNum == 0 ? textureEntry.Row : textureEntry.Row / divisor;
            int tileRow = (int)Math.Floor(yPos / (float)TileSize);
            int tileCol = (int)Math.Floor(xPos / (float)TileSize);

            tileRow = tileRow < 0 ? 0 : tileRow;
            tileCol = tileCol < 0 ? 0 : tileCol;

            int numTilesX = (int)Math.Ceiling(textureEntry.GetWidth(PageNum) / (float)TileSize);
            int numTilesY = (int)Math.Ceiling(textureEntry.GetHeight(PageNum) / (float)TileSize);

            int bitmapheight = textureEntry.GetHeight(PageNum);
            int bitmapwidth  = textureEntry.GetWidth(PageNum);

            for (int row = tileRow; row < Tiles.Count && row <= tileRow + numTilesY; row++)
            {
                int rowStart = row * TileSize;
                int rowEnd   = (row + 1) * TileSize;

                for (int col = tileCol; col < Tiles[row].Count && col <= tileCol + numTilesX; col++)
                {
                    if (Tiles[row][col] == null)
                    {
                        continue;
                    }

                    int colStart = col * TileSize;
                    int colEnd   = (col + 1) * TileSize;

                    if (Tiles[row][col].Texture == null)
                    {
                        Tiles[row][col].GetTextureFromZAD();
                    }
                    if (Tiles[row][col].Texture == null)
                    {
                        continue;
                    }

                    Bitmap decompressed = Tiles[row][col].Texture.GetBitmap();

                    int firstPixelRow = rowStart < yPos ? yPos - rowStart : 0;
                    int lastPixelRow  = TileSize + TilePadding + TilePadding;

                    int firstPixelCol = colStart < xPos ? xPos - colStart : 0;
                    int lastPixelCol  = TileSize + TilePadding + TilePadding;

                    for (int y = firstPixelRow; y < lastPixelRow; y++)
                    {
                        if ((row * TileSize + (y - TilePadding)) - (yPos) >= bitmapheight + TilePadding)
                        {
                            break;
                        }

                        for (int x = firstPixelCol; x < lastPixelCol; x++)
                        {
                            if ((col * TileSize + (x - TilePadding)) - xPos >= bitmapwidth + TilePadding)
                            {
                                break;
                            }

                            int originalX = (col * TileSize + (x - TilePadding)) - xPos;
                            int originalY = (row * TileSize + (y - TilePadding)) - (yPos);

                            if (originalX < 0)
                            {
                                originalX = 0;
                            }
                            else if (originalX >= bitmapwidth)
                            {
                                originalX = bitmapwidth - 1;
                            }

                            if (originalY < 0)
                            {
                                originalY = 0;
                            }
                            else if (originalY >= bitmapheight)
                            {
                                originalY = bitmapheight - 1;
                            }

                            Color colour = image.GetPixel(originalX, originalY);
                            decompressed.SetPixel(x, y, colour);
                        }
                    }

                    TDX newTileTDX = TDX.LoadFromBitmap(decompressed, Tiles[row][col].Texture.Name, Tiles[row][col].Texture.Format);
                    newTileTDX.Flags        = TDX.TDXFlags.AlphaNbit | TDX.TDXFlags.DoNot16bit;
                    Tiles[row][col].Texture = newTileTDX;
                    output.Add(Tiles[row][col]);
                }
            }

            return(output);
        }