Skip to content

Commit

Permalink
fixed transparent image
Browse files Browse the repository at this point in the history
  • Loading branch information
SashaCX75 committed Dec 8, 2021
1 parent 57ad544 commit f9e8e07
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 28 deletions.
1 change: 1 addition & 0 deletions ImageToGTR3/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

154 changes: 128 additions & 26 deletions ImageToGTR3/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,36 @@ public partial class Form1 : Form
private byte[] _streamBuffer;
List<Color> colorMapList = new List<Color>();
int ImageWidth;
public Form1()
bool Shutdown = false;
public Form1(string[] args)
{
InitializeComponent();
if (args.Length > 0)
{
progressBar1.Value = 0;
progressBar1.Maximum = args.Length;
progressBar1.Visible = true;
string fileNameFull = "";
Shutdown = true;
foreach (string file in args)
{
progressBar1.Value++;
if (File.Exists(file) && Path.GetExtension(file).ToLower() == ".png")
{
try
{
ImageMagick.MagickImage image = new ImageMagick.MagickImage(file);
fileNameFull = PngToTga(file);
if (fileNameFull != null) ImageFix(fileNameFull);
}
catch (Exception)
{
TgaToPng(file);
}
}
}
progressBar1.Visible = false;
}
}

// меняем цвет текста и рамки для groupBox
Expand Down Expand Up @@ -79,6 +106,7 @@ private void button_TgaToPng_Click(object sender, EventArgs e)
foreach (String file in FileNames)
{
progressBar1.Value++;
path = TgaToPng(file);
try
{
//string fileNameFull = openFileDialog.FileName;
Expand Down Expand Up @@ -128,13 +156,76 @@ private void button_TgaToPng_Click(object sender, EventArgs e)
}
}
progressBar1.Visible = false;
if (Directory.Exists(path))
if (path.Length > 5 && Directory.Exists(path))
{
Process.Start(new ProcessStartInfo("explorer.exe", path));
}
}
}

private string TgaToPng(string file)
{
string path = "";
if (File.Exists(file))
{
try
{
//string fileNameFull = openFileDialog.FileName;
ImageMagick.MagickImage image;
string fileNameFull = file;
string fileName = Path.GetFileNameWithoutExtension(fileNameFull);
path = Path.GetDirectoryName(fileNameFull);
//fileName = Path.Combine(path, fileName);
int RealWidth = -1;
using (var fileStream = File.OpenRead(fileNameFull))
{
_streamBuffer = new byte[fileStream.Length];
fileStream.Read(_streamBuffer, 0, (int)fileStream.Length);

Header header = new Header(_streamBuffer);
ImageDescription imageDescription = new ImageDescription(_streamBuffer, header.GetImageIDLength());
RealWidth = imageDescription.GetRealWidth();

//image = new ImageMagick.MagickImage(fileStream, ImageMagick.MagickFormat.Tga);
}

using (var fileStream = File.OpenRead(fileNameFull))
{
image = new ImageMagick.MagickImage(fileStream, ImageMagick.MagickFormat.Tga);
}

//image = new ImageMagick.MagickImage(fileNameFull, ImageMagick.MagickFormat.Tga);
image.Format = ImageMagick.MagickFormat.Png32;
if (RealWidth > 0 && RealWidth != image.Width)
{
int height = image.Height;
image = (ImageMagick.MagickImage)image.Clone(RealWidth, height);
}

ImageMagick.IMagickImage Blue = image.Separate(ImageMagick.Channels.Blue).First();
ImageMagick.IMagickImage Red = image.Separate(ImageMagick.Channels.Red).First();
image.Composite(Red, ImageMagick.CompositeOperator.Replace, ImageMagick.Channels.Blue);
image.Composite(Blue, ImageMagick.CompositeOperator.Replace, ImageMagick.Channels.Red);

//image.ColorType = ImageMagick.ColorType.Palette;
path = Path.Combine(path, "Png");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string newFileName = Path.Combine(path, fileName + ".png");
image.Write(newFileName);
//Bitmap bitmap = image.ToBitmap();
//panel1.BackgroundImage = bitmap;
}
catch (Exception exp)
{
MessageBox.Show("Не верный формат исходного файла" + Environment.NewLine +
exp);
}
}
return path;
}
private void button_PngToTga_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
Expand Down Expand Up @@ -174,8 +265,20 @@ private string PngToTga(string fileNameFull)
string fileName = Path.GetFileNameWithoutExtension(fileNameFull);
string path = Path.GetDirectoryName(fileNameFull);
//fileName = Path.Combine(path, fileName);
ImageMagick.MagickImage image = new ImageMagick.MagickImage(fileNameFull);
ImageMagick.MagickImage image_temp = new ImageMagick.MagickImage(fileNameFull);
ImageMagick.MagickImage image;
ImageMagick.MagickImage image_temp;

using (var fileStream = File.OpenRead(fileNameFull))
{
image = new ImageMagick.MagickImage(fileStream);
}
using (var fileStream = File.OpenRead(fileNameFull))
{
//image = new ImageMagick.MagickImage(fileStream);
image_temp = new ImageMagick.MagickImage(fileStream);
}
//image = new ImageMagick.MagickImage(fileNameFull);
//image_temp = new ImageMagick.MagickImage(fileNameFull);
ImageWidth = image.Width;
int newWidth = ImageWidth;
int newHeight = image.Height;
Expand All @@ -187,37 +290,24 @@ private string PngToTga(string fileNameFull)

if (ImageWidth != newWidth)
{
//ImageMagick.MagickImage imageNew =
// new ImageMagick.MagickImage(fileNameFull);
////imageNew.ColorSpace = ImageMagick.ColorSpace.sRGB;
////imageNew.Format = ImageMagick.MagickFormat.Png;
////imageNew.Depth = 8;
////imageNew.ColorSpace = ImageMagick.ColorSpace.sRGB;
////image = (ImageMagick.MagickImage)image.Clone(newWidth, newHeight);
////image_temp = (ImageMagick.MagickImage)image_temp.Clone(newWidth, newHeight);
////imageNew.Composite(image, ImageMagick.CompositeOperator.Src);
//ImageMagick.Drawables dr = new ImageMagick.Drawables();
//dr.Rectangle(image.Width + 1, 0, newWidth, newHeight);
//dr.FillOpacity(new ImageMagick.Percentage(100));
//image = (ImageMagick.MagickImage)image.Clone(newWidth, newHeight);
//image_temp = (ImageMagick.MagickImage)image_temp.Clone(newWidth, newHeight);
//image.Draw();
//image_temp.Draw();
////image = (ImageMagick.MagickImage)imageNew.Clone(newWidth, newHeight);
//image_temp = (ImageMagick.MagickImage)imageNew.Clone(newWidth, newHeight);
Bitmap bitmap = image.ToBitmap();
Bitmap bitmapNew = new Bitmap(newWidth, newHeight);
Graphics gfx = Graphics.FromImage(bitmapNew);
gfx.DrawImage(bitmap, 0, 0, bitmap.Width, bitmap.Height);
image = new ImageMagick.MagickImage(bitmapNew);
image_temp = new ImageMagick.MagickImage(bitmapNew);
}
ImageMagick.Pixel pixel = image.GetPixels().GetPixel(0, 0);
//pixel = new ImageMagick.Pixel(0, 0, 4);
bool transparent = false;
//if (pixel.Channels == 4 && pixel[3] < 256) transparent = true;

image.ColorType = ImageMagick.ColorType.Palette;
if (image.ColorSpace != ImageMagick.ColorSpace.sRGB)
{
image = image_temp;
//image.ColorSpace = ImageMagick.ColorSpace.sRGB;
ImageMagick.Pixel pixel = image.GetPixels().GetPixel(0, 0);
//ImageMagick.Pixel pixel = image.GetPixels().GetPixel(0, 0);
ushort[] p;
if (pixel[2] > 256)
{
Expand All @@ -227,7 +317,11 @@ private string PngToTga(string fileNameFull)
else
{
if (pixel.Channels == 4) p = new ushort[] { pixel[0], pixel[1], (ushort)(pixel[2] + 256), pixel[3] };
else p = new ushort[] { pixel[0], pixel[1], (ushort)(pixel[2] + 256) };
else
{
p = new ushort[] { pixel[0], pixel[1], (ushort)(pixel[2] + 256) };
transparent = true;
}
}
image.GetPixels().SetPixel(0, 0, p);
pixel = image.GetPixels().GetPixel(0, 0);
Expand All @@ -246,12 +340,17 @@ private string PngToTga(string fileNameFull)
//List<string> colorMapList = new List<string>();
for (int i = 0; i < image.ColormapSize; i++)
{

colorMapList.Add(image.GetColormap(i));

//Color tempColor = image.GetColormap(i);
//colorMapList.Add(tempColor.ToArgb().ToString());
//Color tempColor2 = Color.FromArgb(Int32.Parse(colorMapList[i]));
}
if (transparent && colorMapList.Count == 2)
{
colorMapList[0] = Color.FromArgb(0, colorMapList[0].R, colorMapList[0].G, colorMapList[0].B);
colorMapList[1] = Color.FromArgb(0, colorMapList[1].R, colorMapList[1].G, colorMapList[1].B);
}
//File.WriteAllLines(fileName + ".txt", colorMapList);
path = Path.Combine(path, "Fix");
if (!Directory.Exists(path))
Expand Down Expand Up @@ -373,6 +472,9 @@ private void ImageFix(string fileNameFull)
}
}


private void Form1_Shown(object sender, EventArgs e)
{
if (Shutdown) Application.Exit();
}
}
}
4 changes: 2 additions & 2 deletions ImageToGTR3/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ static class Program
/// Главная точка входа для приложения.
/// </summary>
[STAThread]
static void Main()
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
Application.Run(new Form1(args));
}
}
}

0 comments on commit f9e8e07

Please sign in to comment.