} //Structure to hold univeral texture data public static byte[] GenerateTextureDataData(TextureDataStruct texDat) { int offset = 0; byte[] textureDataChunk = new byte[1 + texDat.Name.Length + (9 * 4)]; Array.Copy(BitConverter.GetBytes(texDat.Name.Length), 0, textureDataChunk, offset++, 1); Array.Copy(Encoding.ASCII.GetBytes(texDat.Name), 0, textureDataChunk, offset, texDat.Name.Length); offset += texDat.Name.Length; Array.Copy(BitConverter.GetBytes(texDat.version), 0, textureDataChunk, offset, 4); offset += 4; Array.Copy(BitConverter.GetBytes(texDat.width), 0, textureDataChunk, offset, 4); offset += 4; Array.Copy(BitConverter.GetBytes(texDat.height), 0, textureDataChunk, offset, 4); offset += 4; Array.Copy(BitConverter.GetBytes(texDat.bitDepth), 0, textureDataChunk, offset, 4); offset += 4; Array.Copy(BitConverter.GetBytes(texDat.alphaDepth), 0, textureDataChunk, offset, 4); offset += 4; Array.Copy(BitConverter.GetBytes(texDat.mipMapCount), 0, textureDataChunk, offset, 4); offset += 4; Array.Copy(BitConverter.GetBytes(texDat.textureType), 0, textureDataChunk, offset, 4); offset += 4; Array.Copy(BitConverter.GetBytes(texDat.usage), 0, textureDataChunk, offset, 4); offset += 4; Array.Copy(BitConverter.GetBytes(texDat.priority), 0, textureDataChunk, offset, 4); return(textureDataChunk); }
public static TextureDataStruct GetTextureData(P3D.Chunk texture) { TextureDataStruct textureData = new TextureDataStruct(); int offset = 0; int nameLength = texture.Data[offset++]; textureData.Name = System.Text.Encoding.UTF8.GetString(Utility.GetRange(texture.Data, offset, nameLength)); offset += nameLength; textureData.version = Utility.Byte4ToInt(texture.Data, offset); offset += 4; textureData.width = Utility.Byte4ToInt(texture.Data, offset); offset += 4; textureData.height = Utility.Byte4ToInt(texture.Data, offset); offset += 4; textureData.bitDepth = Utility.Byte4ToInt(texture.Data, offset); offset += 4; textureData.alphaDepth = Utility.Byte4ToInt(texture.Data, offset); offset += 4; textureData.mipMapCount = Utility.Byte4ToInt(texture.Data, offset); offset += 4; textureData.textureType = Utility.Byte4ToInt(texture.Data, offset); offset += 4; textureData.usage = Utility.Byte4ToInt(texture.Data, offset); offset += 4; textureData.priority = Utility.Byte4ToInt(texture.Data, offset); return(textureData); }
//Will take a List of chunks and edit images in Texture chunks public Chunk ShindlerTextures(Chunk currentChunks, Color baseColour, int threshhold) { (ImageDataStruct, Chunk, Bitmap) ProcessImageChunk(Chunk subChunk) { ImageDataStruct imageData = GetImageData(subChunk); Chunk image = subChunk.subChunks[0]; if (!image.ChunkType.SequenceEqual(chunkOrder[IMAGEDATA_CHUNK])) { //TODO } Bitmap imageBitmap = Images.BytesToBitmap(GetImage(image), imageData.format, imageData.width, imageData.height); return(imageData, image, imageBitmap); } for (int i = 0; i < currentChunks.subChunks.Count; i++) { Chunk chunk = currentChunks.subChunks[i]; if (chunk.ChunkType.SequenceEqual(chunkOrder[TEXTURE_CHUNK])) { TextureDataStruct textureData = GetTextureData(chunk); for (int j = 0; j < chunk.subChunks.Count; j++) { Chunk subChunk = chunk.subChunks[j]; if (subChunk.ChunkType.SequenceEqual(chunkOrder[IMAGE_CHUNK])) { var imageChunk = ProcessImageChunk(subChunk); ImageDataStruct imageData = imageChunk.Item1; Chunk image = imageChunk.Item2; Bitmap imageBitmap = imageChunk.Item3; imageBitmap = Images.ShindlerBitmap(imageBitmap, baseColour, threshhold); //EditImageData imageData.format = 1; int bitDepth = Image.GetPixelFormatSize(imageBitmap.PixelFormat); textureData.bitDepth = bitDepth; imageData.bitDepth = bitDepth; //Generate new chunk byte[] imageDataData = GenerateImageDataData(imageBitmap); subChunk.subChunks[0] = RecalculateChunk(subChunk.subChunks[0], imageDataData); byte[] imageDataBytes = GenerateImageData(imageData); chunk.subChunks[j] = RecalculateChunk(chunk.subChunks[j], imageDataBytes); } } byte[] textureDataBytes = GenerateTextureDataData(textureData); uncompressedLength -= currentChunks.subChunks[i].FullChunkSize; currentChunks.subChunks[i] = RecalculateChunk(currentChunks.subChunks[i], textureDataBytes); uncompressedLength += currentChunks.subChunks[i].FullChunkSize; } else if (chunk.ChunkType.SequenceEqual(chunkOrder[SPRITE_CHUNK])) { for (int j = 0; j < chunk.subChunks.Count; j++) { Chunk subChunk = chunk.subChunks[j]; if (subChunk.ChunkType.SequenceEqual(chunkOrder[IMAGE_CHUNK])) { var imageChunk = ProcessImageChunk(subChunk); ImageDataStruct imageData = imageChunk.Item1; Chunk image = imageChunk.Item2; Bitmap imageBitmap = imageChunk.Item3; imageBitmap = Images.ShindlerBitmap(imageBitmap, baseColour, threshhold); //EditImageData imageData.format = 1; int bitDepth = Image.GetPixelFormatSize(imageBitmap.PixelFormat); imageData.bitDepth = bitDepth; //Generate new chunk byte[] imageDataData = GenerateImageDataData(imageBitmap); subChunk.subChunks[0] = RecalculateChunk(subChunk.subChunks[0], imageDataData); byte[] imageDataBytes = GenerateImageData(imageData); chunk.subChunks[j] = RecalculateChunk(chunk.subChunks[j], imageDataBytes); } } uncompressedLength -= currentChunks.subChunks[i].FullChunkSize; currentChunks.subChunks[i] = RecalculateChunk(currentChunks.subChunks[i], currentChunks.subChunks[i].Data); uncompressedLength += currentChunks.subChunks[i].FullChunkSize; } else if (chunk.subChunks.Count > 0) { chunk = ShindlerTextures(chunk, baseColour, threshhold); chunk = RecalculateChunk(chunk, chunk.Data); currentChunks.subChunks[i] = chunk; } } return(currentChunks); }