11import { Img } from '@nativescript-community/ui-image' ;
22import { applyMixins , colorMatrixProperty , cssProperty } from './index-common' ;
3- import { Image } from '@nativescript/core' ;
43
54declare module '@nativescript-community/ui-image' {
6- interface Img { }
7- }
8-
9- interface ImgAugmented extends Img {
10- colorMatrix : number [ ] ;
11- applyColorFilter ( ) ;
5+ interface Img {
6+ mCIFilter : CIFilter ;
7+ initImage ( ) ;
8+ }
129}
1310
1411const FloatConstructor = interop . sizeof ( interop . types . id ) === 4 ? Float32Array : Float64Array ;
1512
1613class ImgExtended {
1714 nativeImageViewProtected : SDAnimatedImageView | UIImageView ;
1815 @cssProperty colorMatrix : number [ ] ;
19- _filter : CIFilter ;
16+ mCIFilter : CIFilter ;
17+ mCanRequestImage : boolean ;
18+ mNeedRequestImage : boolean ;
2019 [ colorMatrixProperty . setNative ] ( value : number [ ] ) {
2120 if ( value ) {
22- if ( ! this . _filter ) {
23- this . _filter = CIFilter . filterWithName ( 'CIColorMatrix' ) ;
21+ if ( ! this . mCIFilter ) {
22+ this . mCIFilter = CIFilter . filterWithName ( 'CIColorMatrix' ) ;
2423 }
25- this . _filter . setValueForKey ( CIVector . vectorWithValuesCount ( new FloatConstructor ( value . slice ( 0 , 4 ) ) . buffer as any , 4 ) , 'inputRVector' ) ;
26- this . _filter . setValueForKey ( CIVector . vectorWithValuesCount ( new FloatConstructor ( value . slice ( 5 , 9 ) ) . buffer as any , 4 ) , 'inputGVector' ) ;
27- this . _filter . setValueForKey ( CIVector . vectorWithValuesCount ( new FloatConstructor ( value . slice ( 10 , 14 ) ) . buffer as any , 4 ) , 'inputBVector' ) ;
28- this . _filter . setValueForKey ( CIVector . vectorWithValuesCount ( new FloatConstructor ( value . slice ( 15 , 19 ) ) . buffer as any , 4 ) , 'inputAVector' ) ;
29- this . _filter . setValueForKey ( CIVector . vectorWithValuesCount ( new FloatConstructor ( [ value [ 4 ] , value [ 9 ] , value [ 14 ] , value [ 19 ] ] ) . buffer as any , 4 ) , 'inputBiasVector' ) ;
24+ this . mCIFilter . setValueForKey ( CIVector . vectorWithValuesCount ( new FloatConstructor ( value . slice ( 0 , 4 ) ) . buffer as any , 4 ) , 'inputRVector' ) ;
25+ this . mCIFilter . setValueForKey ( CIVector . vectorWithValuesCount ( new FloatConstructor ( value . slice ( 5 , 9 ) ) . buffer as any , 4 ) , 'inputGVector' ) ;
26+ this . mCIFilter . setValueForKey ( CIVector . vectorWithValuesCount ( new FloatConstructor ( value . slice ( 10 , 14 ) ) . buffer as any , 4 ) , 'inputBVector' ) ;
27+ this . mCIFilter . setValueForKey ( CIVector . vectorWithValuesCount ( new FloatConstructor ( value . slice ( 15 , 19 ) ) . buffer as any , 4 ) , 'inputAVector' ) ;
28+ this . mCIFilter . setValueForKey ( CIVector . vectorWithValuesCount ( new FloatConstructor ( [ value [ 4 ] , value [ 9 ] , value [ 14 ] , value [ 19 ] ] ) . buffer as any , 4 ) , 'inputBiasVector' ) ;
29+ this . mCIFilter . setName ( JSON . stringify ( value ) ) ;
3030 } else {
31- this . _filter = null ;
31+ this . mCIFilter = null ;
32+ }
33+ if ( this instanceof Img ) {
34+ if ( ! this . mCanRequestImage ) {
35+ this . mNeedRequestImage = true ;
36+ } else {
37+ ( this as Img ) . initImage ( ) ;
38+ }
39+ } else {
40+ ( this as any as ImgExtended3 ) . applyColorFilter ( ) ;
3241 }
33- this [ 'applyColorFilter' ] ( ) ;
3442 }
3543}
3644class ImgExtended2 {
@@ -43,12 +51,13 @@ class ImgExtended3 {
4351 nativeImageViewProtected : SDAnimatedImageView | UIImageView ;
4452 nativeViewProtected : SDAnimatedImageView | UIImageView ;
4553 _oldImage : UIImage ;
46- _filter : CIFilter ;
54+ mCIFilter : CIFilter ;
4755
4856 filteredImage ( image : UIImage , filter : CIFilter ) {
57+ console . log ( 'filteredImage' , image , filter ) ;
4958 if ( image !== null && filter !== null ) {
5059 const tmp = CIImage . alloc ( ) . initWithImage ( image ) ;
51- this . _filter . setValueForKey ( tmp , 'inputImage' ) ;
60+ this . mCIFilter . setValueForKey ( tmp , 'inputImage' ) ;
5261
5362 const outputRect = tmp . extent ;
5463 const context = CIContext . contextWithOptions ( null ) ;
@@ -64,36 +73,46 @@ class ImgExtended3 {
6473 if ( ! this . _oldImage ) {
6574 this . _oldImage = image ;
6675 }
67- return this . filteredImage ( this . _oldImage , this . _filter ) ;
76+ return this . filteredImage ( this . _oldImage , this . mCIFilter ) ;
6877 }
6978 return null ;
7079 }
7180 applyColorFilter ( ) {
7281 const nativeView = this . nativeImageViewProtected || this . nativeViewProtected ;
73- nativeView . image = this . _applyColorFilter ( nativeView . image ) ;
82+ if ( nativeView . image ) {
83+ nativeView . image = this . _applyColorFilter ( nativeView . image ) ;
84+ }
7485 }
7586 public _setNativeImage ( superCall , ...args ) {
87+ // we only need to do that with N Image, Img will use a transformer
88+ if ( this instanceof Img ) {
89+ superCall . apply ( this , args ) ;
90+ return ;
91+ }
7692 this . _oldImage = args [ 0 ] ;
77- if ( this . _filter ) {
93+ if ( this . mCIFilter ) {
7894 args [ 0 ] = this . _applyColorFilter ( args [ 0 ] ) ;
7995 }
8096 superCall . apply ( this , args ) ;
8197 }
8298}
8399
84100let mixinInstalled = false ;
85- export function overrideImgBase ( ) {
101+ export function overrideImgBase ( overrideNImage = true ) {
86102 applyMixins ( Img , [ ImgExtended ] , { override : true } ) ;
87103 applyMixins ( Img , [ ImgExtended2 ] ) ;
88104 applyMixins ( Img , [ ImgExtended3 ] , { callWithSuper : true } ) ;
89- applyMixins ( Image , [ ImgExtended ] , { override : true } ) ;
90- applyMixins ( Image , [ ImgExtended2 ] ) ;
91- applyMixins ( Image , [ ImgExtended3 ] , { callWithSuper : true } ) ;
105+ if ( overrideNImage ) {
106+ const Image = require ( '@nativescript/core' ) . Image ;
107+ applyMixins ( Image , [ ImgExtended ] , { override : true } ) ;
108+ applyMixins ( Image , [ ImgExtended2 ] ) ;
109+ applyMixins ( Image , [ ImgExtended3 ] , { callWithSuper : true } ) ;
110+ }
92111}
93112
94- export function installMixins ( ) {
113+ export function installMixins ( overrideNImage = true ) {
95114 if ( ! mixinInstalled ) {
96115 mixinInstalled = true ;
97- overrideImgBase ( ) ;
116+ overrideImgBase ( overrideNImage ) ;
98117 }
99118}
0 commit comments