public ushort ToRGBPSX(TIMEncodingSettings settings) { ushort result; result = (ushort)(R >> 3); result |= (ushort)((G >> 3) << 5); result |= (ushort)((B >> 3) << 10); if (result == 0 && settings.BlackTransparent) { result |= 0x8000; } if (result == ((31) | (31 << 10)) && settings.MagicPinkTransparent) { result = 0; } if (settings.SetSemiTransparencyBit) { if (settings.BlackTransparent && result == 0) { return(result); } if (settings.MagicPinkTransparent && result == ((31) | (31 << 10))) { return(result); } result |= 0x8000; } return(result); }
/// <summary> /// Sets the TIM to the given bitmap by converting it. /// </summary> /// <param name="bitmap">Bitmap</param> /// <param name="settings">Encoding settings</param> public void SetBitmap(Bitmap bitmap, TIMEncodingSettings settings) { if (TIMHeader.ImageWidthPixels != bitmap.Width || TIMHeader.ImageHeight != bitmap.Height) { MessageBox.Show("The given bitmap has the wrong size!", "Incorrect bitmap size!"); return; } if (TIMHeader.PixelFormat != bitmap.PixelFormat) { MessageBox.Show("The given bitmap has the wrong pixel format!", "Incorrect bitmap format!"); return; } BMP2TIM(bitmap, settings); }
/// <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; } } }