@@ -22,7 +22,8 @@ namespace FModel.ViewModels;
2222
2323public class TabImage : ViewModel
2424{
25- public string ExportName { get ; }
25+ public string ExportName { get ; set ; }
26+
2627 public byte [ ] ImageBuffer { get ; set ; }
2728
2829 public TabImage ( string name , bool rnn , SKBitmap img )
@@ -32,6 +33,13 @@ public TabImage(string name, bool rnn, SKBitmap img)
3233 SetImage ( img ) ;
3334 }
3435
36+ public TabImage ( string name , bool rnn , CTexture img )
37+ {
38+ ExportName = name ;
39+ RenderNearestNeighbor = rnn ;
40+ SetImage ( img ) ;
41+ }
42+
3543 private BitmapImage _image ;
3644 public BitmapImage Image
3745 {
@@ -71,11 +79,42 @@ private void SetImage(SKBitmap bitmap)
7179 }
7280
7381 _bmp = bitmap ;
74- using var data = _bmp . Encode ( NoAlpha ? ETextureFormat . Jpeg : UserSettings . Default . TextureExportFormat , 100 ) ;
82+ ExportName += "." + ( NoAlpha ? "jpg" : "png" ) ;
83+ using var data = _bmp . Encode ( NoAlpha ? SKEncodedImageFormat . Jpeg : SKEncodedImageFormat . Png , 100 ) ;
7584 using var stream = new MemoryStream ( ImageBuffer = data . ToArray ( ) , false ) ;
76- if ( UserSettings . Default . TextureExportFormat == ETextureFormat . Tga )
85+ var image = new BitmapImage ( ) ;
86+ image . BeginInit ( ) ;
87+ image . CacheOption = BitmapCacheOption . OnLoad ;
88+ image . StreamSource = stream ;
89+ image . EndInit ( ) ;
90+ image . Freeze ( ) ;
91+ Image = image ;
92+ }
93+
94+ private void SetImage ( CTexture bitmap )
95+ {
96+ if ( bitmap is null )
97+ {
98+ ImageBuffer = null ;
99+ Image = null ;
77100 return ;
101+ }
102+
103+ _bmp = bitmap . ToSkBitmap ( ) ;
104+ byte [ ] imageData = _bmp . Encode ( NoAlpha ? SKEncodedImageFormat . Jpeg : SKEncodedImageFormat . Png , 100 ) . ToArray ( ) ;
78105
106+ if ( PixelFormatUtils . IsHDR ( bitmap . PixelFormat ) || ( UserSettings . Default . TextureExportFormat != ETextureFormat . Jpeg && UserSettings . Default . TextureExportFormat != ETextureFormat . Png ) )
107+ {
108+ ImageBuffer = bitmap . Encode ( UserSettings . Default . TextureExportFormat , out var ext ) ;
109+ ExportName += "." + ext ;
110+ }
111+ else
112+ {
113+ ImageBuffer = imageData ;
114+ ExportName += "." + ( NoAlpha ? "jpg" : "png" ) ;
115+ }
116+
117+ using var stream = new MemoryStream ( imageData ) ;
79118 var image = new BitmapImage ( ) ;
80119 image . BeginInit ( ) ;
81120 image . CacheOption = BitmapCacheOption . OnLoad ;
@@ -253,7 +292,7 @@ public void SoftReset(GameFile entry)
253292 public void AddImage ( UTexture texture , bool save , bool updateUi )
254293 {
255294 var appendLayerNumber = false ;
256- var img = new SKBitmap [ 1 ] ;
295+ var img = new CTexture [ 1 ] ;
257296 if ( texture is UTexture2DArray textureArray )
258297 {
259298 img = textureArray . DecodeTextureArray ( UserSettings . Default . CurrentDir . TexturePlatform ) ;
@@ -264,7 +303,7 @@ public void AddImage(UTexture texture, bool save, bool updateUi)
264303 img [ 0 ] = texture . Decode ( UserSettings . Default . CurrentDir . TexturePlatform ) ;
265304 if ( texture is UTextureCube )
266305 {
267- img [ 0 ] = img [ 0 ] ? . ToPanorama ( ) ;
306+ img [ 0 ] = img [ 0 ] . ToPanorama ( ) ;
268307 }
269308 }
270309
@@ -279,6 +318,29 @@ public void AddImage(string name, bool rnn, SKBitmap[] img, bool save, bool upda
279318 }
280319 }
281320
321+ public void AddImage ( string name , bool rnn , CTexture [ ] img , bool save , bool updateUi , bool appendLayerNumber = false )
322+ {
323+ for ( var i = 0 ; i < img . Length ; i ++ )
324+ {
325+ AddImage ( $ "{ name } { ( appendLayerNumber ? $ "_{ i } " : "" ) } ", rnn , img [ i ] , save , updateUi ) ;
326+ }
327+ }
328+
329+ public void AddImage ( string name , bool rnn , CTexture img , bool save , bool updateUi )
330+ {
331+ Application . Current . Dispatcher . Invoke ( ( ) =>
332+ {
333+ var t = new TabImage ( name , rnn , img ) ;
334+ if ( save ) SaveImage ( t , updateUi ) ;
335+ if ( ! updateUi ) return ;
336+
337+ _images . Add ( t ) ;
338+ SelectedImage ??= t ;
339+ RaisePropertyChanged ( "Page" ) ;
340+ RaisePropertyChanged ( "HasMultipleImages" ) ;
341+ } ) ;
342+ }
343+
282344 public void AddImage ( string name , bool rnn , SKBitmap img , bool save , bool updateUi )
283345 {
284346 Application . Current . Dispatcher . Invoke ( ( ) =>
@@ -312,23 +374,14 @@ public void SetDocumentText(string text, bool save, bool updateUi)
312374 public void SaveImage ( ) => SaveImage ( SelectedImage , true ) ;
313375 private void SaveImage ( TabImage image , bool updateUi )
314376 {
315- if ( image == null ) return ;
316-
317- var ext = UserSettings . Default . TextureExportFormat switch
318- {
319- ETextureFormat . Png => ".png" ,
320- ETextureFormat . Jpeg => ".jpg" ,
321- ETextureFormat . Tga => ".tga" ,
322- _ => ".png"
323- } ;
377+ if ( image == null )
378+ return ;
324379
325- var fileName = image . ExportName + ext ;
326- var path = Path . Combine ( UserSettings . Default . TextureDirectory ,
327- UserSettings . Default . KeepDirectoryStructure ? Entry . Directory : "" , fileName ! ) . Replace ( '\\ ' , '/' ) ;
380+ var path = Path . Combine ( UserSettings . Default . TextureDirectory , UserSettings . Default . KeepDirectoryStructure ? Entry . Directory : "" , image . ExportName ) . Replace ( '\\ ' , '/' ) ;
328381
329382 Directory . CreateDirectory ( path . SubstringBeforeLast ( '/' ) ) ;
330383
331- SaveImage ( image , path , fileName , updateUi ) ;
384+ SaveImage ( image , path , image . ExportName , updateUi ) ;
332385 }
333386
334387 private void SaveImage ( TabImage image , string path , string fileName , bool updateUi )
0 commit comments