private static byte GetByteForCompression(int qtReaded, int qtRepeated) { byte nib1 = 0; switch (qtReaded) { case 1: nib1 = 0x8; break; case int n when(n == 2 || n == 3): nib1 = 0xC; break; case int n when(n == 4 || n == 5): nib1 = 0xD; break; case int n when(n == 6 || n == 7): nib1 = 0xE; break; case int n when(n == 8 || n == 9): nib1 = 0xF; break; } byte nib2 = (byte)(qtRepeated - 2); if (qtReaded % 2 == 1 && nib1 != 0x8) { nib2 += 8; } return(CGeneric.NibbleToByte(nib1, nib2)); }
private static (byte[] palette, byte[] data) ConvertRGBAtoMax16Colors(byte[] texture) { //construct palette and use it. List <Color> colors = ExtractPaletteFromByteArray(texture); //first array for palette color byte[] palette = new byte[colors.Count * 4]; //second array for palette data byte[] finalArray = new byte[texture.Length / 8]; // write palette for (int i = 0; i < colors.Count(); i++) { palette[i * 4] = colors[i].R; palette[i * 4 + 1] = colors[i].G; palette[i * 4 + 2] = colors[i].B; palette[i * 4 + 3] = colors[i].A; } int index = 0; byte nibble1; byte nibble2; // write data with palette information for (int i = 0; i < texture.Length; i += 8) { nibble1 = GetIndexFromPalette(colors, Color.FromArgb(texture[i + 3], texture[i], texture[i + 1], texture[i + 2])); nibble2 = GetIndexFromPalette(colors, Color.FromArgb(texture[i + 7], texture[i + 4], texture[i + 5], texture[i + 6])); finalArray[index] = CGeneric.NibbleToByte(nibble1, nibble2); index++; } return(palette, finalArray); }
//specific format for BFF header private static byte[] SetHeader(CGeneric.Compression textureType, byte greenAlphaIndex, byte compressedValue, byte[] displayedWidth, byte[] pixelWidth, byte[] displayHeight, byte[] bffName, byte[] colorCount) { //fixedsize = 36 + bffName + 1 if bffName size is not pair + 4 for palette color count byte[] headerBFF2 = new Byte[36 + bffName.Length + bffName.Length % 2 + colorCount.Length]; headerBFF2[8] = 0x8; //write "BFF2" for (int i = 0; i < CGeneric.patternBFF2.Length; i++) { headerBFF2[12 + i] = CGeneric.patternBFF2[i]; } //decompose the next two bytes in nibbles //grab the nibbles of alpha color List <byte> alphaColor = CGeneric.ByteToNibble(greenAlphaIndex); //write two nibbles for the both bytes.. headerBFF2[17] = CGeneric.NibbleToByte(2, alphaColor[0]); headerBFF2[18] = CGeneric.NibbleToByte(alphaColor[1], compressedValue); //write texture type headerBFF2[19] = (byte)textureType; //write displayed width for (int i = 0; i < displayedWidth.Length; i++) { headerBFF2[20 + i] = displayedWidth[i]; } //write pixel width for (int i = 0; i < pixelWidth.Length; i++) { headerBFF2[22 + i] = pixelWidth[i]; } //write display height for (int i = 0; i < displayHeight.Length; i++) { headerBFF2[26 + i] = displayHeight[i]; } //prepare instruction length (depending of the pixelWidth value) double instructionLengthValue = CGeneric.ConvertByteArrayToInt(pixelWidth); switch (textureType) { case CGeneric.Compression.greyscale: instructionLengthValue *= 2; break; case CGeneric.Compression.max16Colors: instructionLengthValue /= 2; break; case CGeneric.Compression.trueColor16Bits: instructionLengthValue *= 2; break; case CGeneric.Compression.trueColor32Bits: instructionLengthValue *= 4; break; } byte[] instructLength = CGeneric.ConvertIntToByteArray((int)Math.Floor(instructionLengthValue)); //write instruction length for (int i = 0; i < instructLength.Length; i++) { headerBFF2[28 + i] = instructLength[i]; } //length of file name if (bffName.Length % 2 == 0) { headerBFF2[35] = (byte)bffName.Length; } else { headerBFF2[35] = (byte)(bffName.Length + 1); } //write bff name for (int i = 0; i < bffName.Length; i++) { headerBFF2[36 + i] = bffName[i]; } for (int i = 0; i < colorCount.Length; i++) { headerBFF2[36 + bffName.Length + bffName.Length % 2 + i] = colorCount[i]; } return(headerBFF2); }