1+ import { decodeTga } from "@lunapaint/tga-codec" ;
12import { colorDistance } from "../util/util" ;
3+ import { fs } from "../native_apis" ;
24
35BARS . defineActions ( function ( ) {
46
@@ -689,29 +691,45 @@ BARS.defineActions(function() {
689691 icon : 'stacked_bar_chart' ,
690692 category : 'textures' ,
691693 condition : { modes : [ 'paint' ] , selected : { texture : true } } ,
692- click ( ) {
694+ async click ( ) {
693695 let texture = Texture . getDefault ( ) ;
694696 let original_data = texture . ctx . getImageData ( 0 , 0 , texture . canvas . width , texture . canvas . height ) ;
697+ if ( texture . file_format == 'tga' && isApp ) {
698+
699+ let data = fs . readFileSync ( texture . path ) ;
700+ if ( data instanceof ArrayBuffer ) data = new Uint8Array ( data ) ;
701+ let result = await decodeTga ( data ) ;
702+ original_data . data . set ( result . image . data ) ;
703+ }
695704
696705 Undo . initEdit ( { textures : [ texture ] , bitmap : true } ) ;
697706
698707 texture . layers_enabled = true ;
699708 texture . layers . empty ( ) ;
700709 let i = 0 ;
701- for ( let color of [ 'red' , 'green' , 'blue' ] ) {
710+ for ( let color of [ 'red' , 'green' , 'blue' , 'alpha' ] ) {
702711 let data_copy = new ImageData ( original_data . data . slice ( ) , original_data . width , original_data . height ) ;
703712 for ( let j = 0 ; j < data_copy . data . length ; j += 4 ) {
704713 if ( i != 0 ) data_copy . data [ j + 0 ] = 0 ;
705714 if ( i != 1 ) data_copy . data [ j + 1 ] = 0 ;
706715 if ( i != 2 ) data_copy . data [ j + 2 ] = 0 ;
716+ if ( i == 3 ) {
717+ data_copy . data [ j + 0 ] = data_copy . data [ j + 1 ] = data_copy . data [ j + 2 ] = data_copy . data [ j + 3 ] ;
718+ }
719+ data_copy . data [ j + 3 ] = 255 ;
707720 }
708721 let layer = new TextureLayer ( {
709722 name : color ,
710723 blend_mode : 'add'
711724 } , texture ) ;
712725 layer . setSize ( original_data . width , original_data . height ) ;
713726 layer . ctx . putImageData ( data_copy , 0 , 0 ) ;
714- texture . layers . unshift ( layer ) ;
727+ if ( color == 'alpha' ) {
728+ texture . layers . push ( layer ) ;
729+ layer . blend_mode = 'alpha_mask'
730+ } else {
731+ texture . layers . unshift ( layer ) ;
732+ }
715733 if ( color == 'red' ) {
716734 layer . select ( ) ;
717735 }
@@ -723,6 +741,54 @@ BARS.defineActions(function() {
723741 BARS . updateConditions ( ) ;
724742 }
725743 } )
744+ new Action ( 'split_alpha_into_layer' , {
745+ icon : 'tab_inactive' ,
746+ category : 'textures' ,
747+ condition : { modes : [ 'paint' ] , selected : { texture : true } } ,
748+ async click ( ) {
749+ let texture = Texture . getDefault ( ) ;
750+ let original_data = texture . ctx . getImageData ( 0 , 0 , texture . canvas . width , texture . canvas . height ) ;
751+ if ( texture . file_format == 'tga' && isApp ) {
752+
753+ let data = fs . readFileSync ( texture . path ) ;
754+ if ( data instanceof ArrayBuffer ) data = new Uint8Array ( data ) ;
755+ let result = await decodeTga ( data ) ;
756+ original_data . data . set ( result . image . data ) ;
757+ }
758+
759+ Undo . initEdit ( { textures : [ texture ] , bitmap : true } ) ;
760+
761+ texture . layers_enabled = true ;
762+ texture . layers . empty ( ) ;
763+
764+ // Color
765+ let data_copy = new ImageData ( original_data . data . slice ( ) , original_data . width , original_data . height ) ;
766+ for ( let j = 0 ; j < data_copy . data . length ; j += 4 ) {
767+ data_copy . data [ j + 3 ] = 255 ;
768+ }
769+ let layer = new TextureLayer ( { name : 'color' } , texture ) ;
770+ layer . setSize ( original_data . width , original_data . height ) ;
771+ layer . ctx . putImageData ( data_copy , 0 , 0 ) ;
772+ texture . layers . push ( layer ) ;
773+ layer . select ( ) ;
774+
775+ // Alpha
776+ let data_alpha = new ImageData ( original_data . data . slice ( ) , original_data . width , original_data . height ) ;
777+ for ( let j = 0 ; j < data_alpha . data . length ; j += 4 ) {
778+ data_alpha . data [ j + 0 ] = data_alpha . data [ j + 1 ] = data_alpha . data [ j + 2 ] = data_alpha . data [ j + 3 ] ;
779+ data_alpha . data [ j + 3 ] = 255 ;
780+ }
781+ let alpha_layer = new TextureLayer ( { name : 'alpha' , blend_mode : 'alpha_mask' } , texture ) ;
782+ alpha_layer . setSize ( original_data . width , original_data . height ) ;
783+ alpha_layer . ctx . putImageData ( data_alpha , 0 , 0 ) ;
784+ texture . layers . push ( alpha_layer ) ;
785+
786+ texture . updateLayerChanges ( true ) ;
787+ Undo . finishEdit ( 'Split texture alpha into alpha mask layers' ) ;
788+ updateInterfacePanels ( ) ;
789+ BARS . updateConditions ( ) ;
790+ }
791+ } )
726792 new Action ( 'clear_unused_texture_space' , {
727793 icon : 'cleaning_services' ,
728794 category : 'textures' ,
0 commit comments