File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -1071,6 +1071,36 @@ export class Image {
10711071 } ;
10721072 }
10731073
1074+ fisheye ( radius = 2 ) {
1075+ const r = new Image ( this . width , this . height ) ;
1076+
1077+ const w = this . width ;
1078+ const h = this . height ;
1079+ const tu32 = this . __u32__ ;
1080+ const ru32 = r . __u32__ ;
1081+ const iw = 1 / w ;
1082+ const ih = 1 / h ;
1083+
1084+ for ( const [ x , y ] of this ) {
1085+ const xco = x * iw - .5 ;
1086+ const yco = y * ih - .5 ;
1087+ const dfc = Math . sqrt ( xco ** 2 + yco ** 2 ) ;
1088+ const dis = 2 * dfc ** radius ;
1089+ const nx = ( ( dis * xco / dfc + 0.5 ) * w ) | 0 ;
1090+ const ny = ( ( dis * yco / dfc + 0.5 ) * h ) | 0 ;
1091+
1092+ if ( nx < 1 || nx > w || ny < 1 || ny > h || isNaN ( nx ) || isNaN ( ny ) )
1093+ continue ;
1094+
1095+ ru32 [ y * w + x ] = tu32 [ w * ny + nx ] ;
1096+ }
1097+
1098+ const cO = tu32 . length * .5 + w / 2 ;
1099+ ru32 [ cO ] = tu32 [ cO ] ;
1100+
1101+ return this . __apply__ ( r ) ;
1102+ }
1103+
10741104 /**
10751105 * Encodes the image into a PNG
10761106 * @param {number } compression The compression level to use (0-3)
Original file line number Diff line number Diff line change 1+ const fs = require ( 'fs' ) . promises ;
2+ const { Image} = require ( '../ImageScript' ) ;
3+
4+ ( async ( ) => {
5+ const input = await fs . readFile ( './tests/targets/external.png' ) ;
6+
7+ const image = await Image . decode ( input ) ;
8+ image . fisheye ( ) ;
9+
10+ const output = await image . encode ( ) ;
11+ const target = await fs . readFile ( './tests/targets/fisheye.png' ) ;
12+
13+ if ( ! Buffer . from ( target ) . equals ( Buffer . from ( output ) ) )
14+ process . exit ( 1 ) ;
15+ } ) ( ) ;
You can’t perform that action at this time.
0 commit comments