public ChunkyDataPTLD(int version_in, string name_in, byte[] innerData_in) : base("PTLD", version_in, name_in) { layerType = (PTLD_Layers)(int)innerData_in[0]; int layerSize = (innerData_in[4]) + (innerData_in[5] << 8) + (innerData_in[6] << 16) + (innerData_in[7] << 24); image = new byte[layerSize]; for (int i = 0; i < layerSize; i++) { image[i] = innerData_in[i + 8]; } }
public byte[] this[PTLD_Layers key] { get { return(layers[(int)key]); } set { if (value.Length != layerSize) { throw new InvalidOperationException("Data in layer collection must be the same size: " + layerSize.ToString() + " bytes"); } layers[(int)key] = value; } }
public static ChunkyDataPTLD CreateFromTGA(PTLD_Layers layer_in, int version, string name, byte[] tgaData) { //check image type code if (tgaData[2] != 0x03) { bool worked = false; if (tgaData[1] == 0x01 && tgaData[2] == 0x01) { tgaData = ImageConverter.ColourMapToGreyscale(tgaData); worked = true; } if (!worked) { ImageConverter.MapEncStruct format = ImageConverter.getMapEncStruct(tgaData[2]); throw new InvalidFileException("_" + layer_in.ToString() + ".tga must be an unencoded unmapped monochrome Targa image.\r\n" + " Image type: " + format.formatType + "\r\n" + " Image encoded: " + ((format.isEncoded)?"yes":"no") + "\r\n" + " Colour mapped: " + ((format.isMapped)?"yes":"no")); } } //check colour depth if (tgaData[16] != 0x08) { throw new InvalidFileException("_" + layer_in.ToString() + ".tga must be an 8-bit Targa image. Image was " + tgaData[16] + "-bit."); } int width = tgaData[12] + (tgaData[13] << 8); int height = tgaData[14] + (tgaData[15] << 8); byte[] data = new byte[width * height + 8]; //only take the correct number of bytes so that we don't include comments int layerDataLength = data.Length - 8; int layer = (int)layer_in; data[0] = (byte)(layer); data[1] = (byte)(layer >> 8); data[2] = (byte)(layer >> 16); data[3] = (byte)(layer >> 24); data[4] = (byte)layerDataLength; data[5] = (byte)(layerDataLength >> 8); data[6] = (byte)(layerDataLength >> 16); data[7] = (byte)(layerDataLength >> 24); int offset = 10 + tgaData[0]; //should be 18, but we're starting at i=8 so remove 8 here as well bool nonBlack = false; //boolean to check whether they actually included any data in the layer for (int i = 8; i < data.Length; i++) { data[i] = tgaData[i + offset]; //take in to account any possible image ID if (data[i] > 5) { nonBlack = true; } } //make sure we always return a dirt layer, even if they made it all black (all teamcolourable) if (nonBlack || layer_in == PTLD_Layers.Dirt) { return(new ChunkyDataPTLD(version, name, data)); } else { return(null); } }