public CLUTButton(CLUTEntry clutEntry) { MouseUp += CLUTButton_Click; CLUTEntry = clutEntry; Width = 25; Height = 25; UpdateButton(); }
public static int NearestColor(CLUTEntry[] entries, Color color) { int nearest = -1; int difference = 765; for (int i = 0; i < entries.Length; i++) { CLUTEntry entry = entries[i]; int diff = ColorDifference(entry.Color, color); if (diff < difference) { nearest = i; difference = diff; } } return(nearest); }
/// <summary> /// Converts a bitmap into a TIM file /// </summary> /// <param name="bitmap">Bitmap</param> /// <param name="settings">Encoding settings</param> private void BMP2TIM(Bitmap bitmap, TIMEncodingSettings settings) { CLUTEntry[] oldEntries = new CLUTEntry[TIMHeader.ClutEntries.Length]; Array.Copy(TIMHeader.ClutEntries, oldEntries, oldEntries.Length); if (!settings.UseOriginalCLUT) { TIMHeader.GenerateClut(bitmap, settings); } if (settings.UseOriginalColor) { TIMHeader.SetOriginalColor(oldEntries); } if (settings.UseOriginalTransparency) { TIMHeader.SetSemiTransparentBits(oldEntries); } using (BinaryWriter writer = new BinaryWriter(new FileStream(FilePath, FileMode.OpenOrCreate))) { TIMHeader.Write(writer.BaseStream); switch (TIMHeader.BPP) { case 24: for (int y = 0; y < bitmap.Height; y++) { for (int x = 0; x < bitmap.Width; x += 2) { Color color1 = bitmap.GetPixel(x, y); Color color2 = bitmap.GetPixel(x + 1, y); writer.Write((ushort)((color1.G << 8) | color1.R)); writer.Write((ushort)((color2.R << 8) | color1.B)); writer.Write((ushort)((color2.B << 8) | color2.G)); } } break; case 16: for (int y = 0; y < bitmap.Height; y++) { for (int x = 0; x < bitmap.Width; x++) { Color color = bitmap.GetPixel(x, y); PS_RGB rgb = new PS_RGB(color.R, color.G, color.B); writer.Write(rgb.ToRGBPSX(settings)); } } break; case 8: for (int y = 0; y < bitmap.Height; y++) { for (int x = 0; x < bitmap.Width; x += 2) { Color color1 = bitmap.GetPixel(x, y); Color color2 = bitmap.GetPixel(x + 1, y); ushort shortbuf = (ushort)((GetColorIndex(bitmap, color2, settings.UseOriginalCLUT) << 8) | GetColorIndex(bitmap, color1, settings.UseOriginalCLUT)); writer.Write(shortbuf); } } break; case 4: for (int y = 0; y < bitmap.Height; y++) { for (int x = 0; x < bitmap.Width; x += 4) { Color color1 = bitmap.GetPixel(x, y); Color color2 = bitmap.GetPixel(x + 1, y); Color color3 = bitmap.GetPixel(x + 2, y); Color color4 = bitmap.GetPixel(x + 3, y); ushort shortbuf = (ushort)((GetColorIndex(bitmap, color4, settings.UseOriginalCLUT) << 12) | (GetColorIndex(bitmap, color3, settings.UseOriginalCLUT) << 8) | (GetColorIndex(bitmap, color2, settings.UseOriginalCLUT) << 4) | GetColorIndex(bitmap, color1, settings.UseOriginalCLUT)); writer.Write(shortbuf); } } break; } } }