src/utils/convert.js
/**
* Detecting vertical squash in loaded image.
* Fixes a bug which squash image vertically while drawing into canvas for some images.
*/
Detecting vertical squash in loaded image.
Fixes a bug which squash image vertically while drawing into canvas for some images.
Detecting vertical squash in loaded image . Fixes a bug which squash image vertically while drawing into canvas for some images .
function detectVerticalSquash(img, iw, ih) {
  var canvas = document.createElement('canvas');
  canvas.width = 1;
  canvas.height = ih;
  var ctx = canvas.getContext('2d');
  ctx.drawImage(img, 0, 0);
  var data = ctx.getImageData(0, 0, 1, ih).data;
  var sy = 0;
  var ey = ih;
  var py = ih;
  while (py > sy) {
    var alpha = data[(py - 1) * 4 + 3];
    if (alpha === 0) {
      ey = py;
    } else {
      sy = py;
    }
    py = (ey + sy) >> 1;
  }
  var ratio = (py / ih);
  return (ratio === 0) ? 1 : ratio;
}
function detectVerticalSquash ( img , iw , ih ) { var canvas = document . createElement ( 'canvas' ) ; canvas . width = 1 ; canvas . height = ih ; var ctx = canvas . getContext ( '2d' ) ; ctx . drawImage ( img , 0 , 0 ) ; var data = ctx . getImageData ( 0 , 0 , 1 , ih ) . data ; var sy = 0 ; var ey = ih ; var py = ih ; while ( py > sy ) { var alpha = data [ ( py - 1 ) * 4 + 3 ] ; if ( alpha === 0 ) { ey = py ; } else { sy = py ; } py = ( ey + sy ) >> 1 ; } var ratio = ( py / ih ) ; return ( ratio === 0 ) ? 1 : ratio ; }
Detecting vertical squash in loaded image.
Detecting vertical squash in loaded image .
// search image edge pixel position in case it is squashed vertically.