@@ -18,9 +18,36 @@ public partial class Form1 : Form
18
18
private byte [ ] _streamBuffer ;
19
19
List < Color > colorMapList = new List < Color > ( ) ;
20
20
int ImageWidth ;
21
- public Form1 ( )
21
+ bool Shutdown = false ;
22
+ public Form1 ( string [ ] args )
22
23
{
23
24
InitializeComponent ( ) ;
25
+ if ( args . Length > 0 )
26
+ {
27
+ progressBar1 . Value = 0 ;
28
+ progressBar1 . Maximum = args . Length ;
29
+ progressBar1 . Visible = true ;
30
+ string fileNameFull = "" ;
31
+ Shutdown = true ;
32
+ foreach ( string file in args )
33
+ {
34
+ progressBar1 . Value ++ ;
35
+ if ( File . Exists ( file ) && Path . GetExtension ( file ) . ToLower ( ) == ".png" )
36
+ {
37
+ try
38
+ {
39
+ ImageMagick . MagickImage image = new ImageMagick . MagickImage ( file ) ;
40
+ fileNameFull = PngToTga ( file ) ;
41
+ if ( fileNameFull != null ) ImageFix ( fileNameFull ) ;
42
+ }
43
+ catch ( Exception )
44
+ {
45
+ TgaToPng ( file ) ;
46
+ }
47
+ }
48
+ }
49
+ progressBar1 . Visible = false ;
50
+ }
24
51
}
25
52
26
53
// меняем цвет текста и рамки для groupBox
@@ -79,6 +106,7 @@ private void button_TgaToPng_Click(object sender, EventArgs e)
79
106
foreach ( String file in FileNames )
80
107
{
81
108
progressBar1 . Value ++ ;
109
+ path = TgaToPng ( file ) ;
82
110
try
83
111
{
84
112
//string fileNameFull = openFileDialog.FileName;
@@ -128,13 +156,76 @@ private void button_TgaToPng_Click(object sender, EventArgs e)
128
156
}
129
157
}
130
158
progressBar1 . Visible = false ;
131
- if ( Directory . Exists ( path ) )
159
+ if ( path . Length > 5 && Directory . Exists ( path ) )
132
160
{
133
161
Process . Start ( new ProcessStartInfo ( "explorer.exe" , path ) ) ;
134
162
}
135
163
}
136
164
}
137
165
166
+ private string TgaToPng ( string file )
167
+ {
168
+ string path = "" ;
169
+ if ( File . Exists ( file ) )
170
+ {
171
+ try
172
+ {
173
+ //string fileNameFull = openFileDialog.FileName;
174
+ ImageMagick . MagickImage image ;
175
+ string fileNameFull = file ;
176
+ string fileName = Path . GetFileNameWithoutExtension ( fileNameFull ) ;
177
+ path = Path . GetDirectoryName ( fileNameFull ) ;
178
+ //fileName = Path.Combine(path, fileName);
179
+ int RealWidth = - 1 ;
180
+ using ( var fileStream = File . OpenRead ( fileNameFull ) )
181
+ {
182
+ _streamBuffer = new byte [ fileStream . Length ] ;
183
+ fileStream . Read ( _streamBuffer , 0 , ( int ) fileStream . Length ) ;
184
+
185
+ Header header = new Header ( _streamBuffer ) ;
186
+ ImageDescription imageDescription = new ImageDescription ( _streamBuffer , header . GetImageIDLength ( ) ) ;
187
+ RealWidth = imageDescription . GetRealWidth ( ) ;
188
+
189
+ //image = new ImageMagick.MagickImage(fileStream, ImageMagick.MagickFormat.Tga);
190
+ }
191
+
192
+ using ( var fileStream = File . OpenRead ( fileNameFull ) )
193
+ {
194
+ image = new ImageMagick . MagickImage ( fileStream , ImageMagick . MagickFormat . Tga ) ;
195
+ }
196
+
197
+ //image = new ImageMagick.MagickImage(fileNameFull, ImageMagick.MagickFormat.Tga);
198
+ image . Format = ImageMagick . MagickFormat . Png32 ;
199
+ if ( RealWidth > 0 && RealWidth != image . Width )
200
+ {
201
+ int height = image . Height ;
202
+ image = ( ImageMagick . MagickImage ) image . Clone ( RealWidth , height ) ;
203
+ }
204
+
205
+ ImageMagick . IMagickImage Blue = image . Separate ( ImageMagick . Channels . Blue ) . First ( ) ;
206
+ ImageMagick . IMagickImage Red = image . Separate ( ImageMagick . Channels . Red ) . First ( ) ;
207
+ image . Composite ( Red , ImageMagick . CompositeOperator . Replace , ImageMagick . Channels . Blue ) ;
208
+ image . Composite ( Blue , ImageMagick . CompositeOperator . Replace , ImageMagick . Channels . Red ) ;
209
+
210
+ //image.ColorType = ImageMagick.ColorType.Palette;
211
+ path = Path . Combine ( path , "Png" ) ;
212
+ if ( ! Directory . Exists ( path ) )
213
+ {
214
+ Directory . CreateDirectory ( path ) ;
215
+ }
216
+ string newFileName = Path . Combine ( path , fileName + ".png" ) ;
217
+ image . Write ( newFileName ) ;
218
+ //Bitmap bitmap = image.ToBitmap();
219
+ //panel1.BackgroundImage = bitmap;
220
+ }
221
+ catch ( Exception exp )
222
+ {
223
+ MessageBox . Show ( "Не верный формат исходного файла" + Environment . NewLine +
224
+ exp ) ;
225
+ }
226
+ }
227
+ return path ;
228
+ }
138
229
private void button_PngToTga_Click ( object sender , EventArgs e )
139
230
{
140
231
OpenFileDialog openFileDialog = new OpenFileDialog ( ) ;
@@ -174,8 +265,20 @@ private string PngToTga(string fileNameFull)
174
265
string fileName = Path . GetFileNameWithoutExtension ( fileNameFull ) ;
175
266
string path = Path . GetDirectoryName ( fileNameFull ) ;
176
267
//fileName = Path.Combine(path, fileName);
177
- ImageMagick . MagickImage image = new ImageMagick . MagickImage ( fileNameFull ) ;
178
- ImageMagick . MagickImage image_temp = new ImageMagick . MagickImage ( fileNameFull ) ;
268
+ ImageMagick . MagickImage image ;
269
+ ImageMagick . MagickImage image_temp ;
270
+
271
+ using ( var fileStream = File . OpenRead ( fileNameFull ) )
272
+ {
273
+ image = new ImageMagick . MagickImage ( fileStream ) ;
274
+ }
275
+ using ( var fileStream = File . OpenRead ( fileNameFull ) )
276
+ {
277
+ //image = new ImageMagick.MagickImage(fileStream);
278
+ image_temp = new ImageMagick . MagickImage ( fileStream ) ;
279
+ }
280
+ //image = new ImageMagick.MagickImage(fileNameFull);
281
+ //image_temp = new ImageMagick.MagickImage(fileNameFull);
179
282
ImageWidth = image . Width ;
180
283
int newWidth = ImageWidth ;
181
284
int newHeight = image . Height ;
@@ -187,37 +290,24 @@ private string PngToTga(string fileNameFull)
187
290
188
291
if ( ImageWidth != newWidth )
189
292
{
190
- //ImageMagick.MagickImage imageNew =
191
- // new ImageMagick.MagickImage(fileNameFull);
192
- ////imageNew.ColorSpace = ImageMagick.ColorSpace.sRGB;
193
- ////imageNew.Format = ImageMagick.MagickFormat.Png;
194
- ////imageNew.Depth = 8;
195
- ////imageNew.ColorSpace = ImageMagick.ColorSpace.sRGB;
196
- ////image = (ImageMagick.MagickImage)image.Clone(newWidth, newHeight);
197
- ////image_temp = (ImageMagick.MagickImage)image_temp.Clone(newWidth, newHeight);
198
- ////imageNew.Composite(image, ImageMagick.CompositeOperator.Src);
199
- //ImageMagick.Drawables dr = new ImageMagick.Drawables();
200
- //dr.Rectangle(image.Width + 1, 0, newWidth, newHeight);
201
- //dr.FillOpacity(new ImageMagick.Percentage(100));
202
- //image = (ImageMagick.MagickImage)image.Clone(newWidth, newHeight);
203
- //image_temp = (ImageMagick.MagickImage)image_temp.Clone(newWidth, newHeight);
204
- //image.Draw();
205
- //image_temp.Draw();
206
- ////image = (ImageMagick.MagickImage)imageNew.Clone(newWidth, newHeight);
207
- //image_temp = (ImageMagick.MagickImage)imageNew.Clone(newWidth, newHeight);
208
293
Bitmap bitmap = image . ToBitmap ( ) ;
209
294
Bitmap bitmapNew = new Bitmap ( newWidth , newHeight ) ;
210
295
Graphics gfx = Graphics . FromImage ( bitmapNew ) ;
211
296
gfx . DrawImage ( bitmap , 0 , 0 , bitmap . Width , bitmap . Height ) ;
212
297
image = new ImageMagick . MagickImage ( bitmapNew ) ;
213
298
image_temp = new ImageMagick . MagickImage ( bitmapNew ) ;
214
299
}
300
+ ImageMagick . Pixel pixel = image . GetPixels ( ) . GetPixel ( 0 , 0 ) ;
301
+ //pixel = new ImageMagick.Pixel(0, 0, 4);
302
+ bool transparent = false ;
303
+ //if (pixel.Channels == 4 && pixel[3] < 256) transparent = true;
304
+
215
305
image . ColorType = ImageMagick . ColorType . Palette ;
216
306
if ( image . ColorSpace != ImageMagick . ColorSpace . sRGB )
217
307
{
218
308
image = image_temp ;
219
309
//image.ColorSpace = ImageMagick.ColorSpace.sRGB;
220
- ImageMagick . Pixel pixel = image . GetPixels ( ) . GetPixel ( 0 , 0 ) ;
310
+ // ImageMagick.Pixel pixel = image.GetPixels().GetPixel(0, 0);
221
311
ushort [ ] p ;
222
312
if ( pixel [ 2 ] > 256 )
223
313
{
@@ -227,7 +317,11 @@ private string PngToTga(string fileNameFull)
227
317
else
228
318
{
229
319
if ( pixel . Channels == 4 ) p = new ushort [ ] { pixel [ 0 ] , pixel [ 1 ] , ( ushort ) ( pixel [ 2 ] + 256 ) , pixel [ 3 ] } ;
230
- else p = new ushort [ ] { pixel [ 0 ] , pixel [ 1 ] , ( ushort ) ( pixel [ 2 ] + 256 ) } ;
320
+ else
321
+ {
322
+ p = new ushort [ ] { pixel [ 0 ] , pixel [ 1 ] , ( ushort ) ( pixel [ 2 ] + 256 ) } ;
323
+ transparent = true ;
324
+ }
231
325
}
232
326
image . GetPixels ( ) . SetPixel ( 0 , 0 , p ) ;
233
327
pixel = image . GetPixels ( ) . GetPixel ( 0 , 0 ) ;
@@ -246,12 +340,17 @@ private string PngToTga(string fileNameFull)
246
340
//List<string> colorMapList = new List<string>();
247
341
for ( int i = 0 ; i < image . ColormapSize ; i ++ )
248
342
{
249
-
250
343
colorMapList . Add ( image . GetColormap ( i ) ) ;
344
+
251
345
//Color tempColor = image.GetColormap(i);
252
346
//colorMapList.Add(tempColor.ToArgb().ToString());
253
347
//Color tempColor2 = Color.FromArgb(Int32.Parse(colorMapList[i]));
254
348
}
349
+ if ( transparent && colorMapList . Count == 2 )
350
+ {
351
+ colorMapList [ 0 ] = Color . FromArgb ( 0 , colorMapList [ 0 ] . R , colorMapList [ 0 ] . G , colorMapList [ 0 ] . B ) ;
352
+ colorMapList [ 1 ] = Color . FromArgb ( 0 , colorMapList [ 1 ] . R , colorMapList [ 1 ] . G , colorMapList [ 1 ] . B ) ;
353
+ }
255
354
//File.WriteAllLines(fileName + ".txt", colorMapList);
256
355
path = Path . Combine ( path , "Fix" ) ;
257
356
if ( ! Directory . Exists ( path ) )
@@ -373,6 +472,9 @@ private void ImageFix(string fileNameFull)
373
472
}
374
473
}
375
474
376
-
475
+ private void Form1_Shown ( object sender , EventArgs e )
476
+ {
477
+ if ( Shutdown ) Application . Exit ( ) ;
478
+ }
377
479
}
378
480
}
0 commit comments