megapixImage.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**
  2. * Mega pixel image rendering library for iOS6 Safari
  3. *
  4. * Fixes iOS6 Safari's image file rendering issue for large size image (over mega-pixel),
  5. * which causes unexpected subsampling when drawing it in canvas.
  6. * By using this library, you can safely render the image with proper stretching.
  7. *
  8. * Copyright (c) 2012 Shinichi Tomita <shinichi.tomita@gmail.com>
  9. * Released under the MIT license
  10. */
  11. (function () {
  12. /**
  13. * Detect subsampling in loaded image.
  14. * In iOS, larger images than 2M pixels may be subsampled in rendering.
  15. */
  16. function detectSubsampling(img) {
  17. var iw = img.naturalWidth, ih = img.naturalHeight;
  18. if (iw * ih > 1024 * 1024) { // subsampling may happen over megapixel image
  19. var canvas = document.createElement('canvas');
  20. canvas.width = canvas.height = 1;
  21. var ctx = canvas.getContext('2d');
  22. ctx.drawImage(img, -iw + 1, 0);
  23. // subsampled image becomes half smaller in rendering size.
  24. // check alpha channel value to confirm image is covering edge pixel or not.
  25. // if alpha value is 0 image is not covering, hence subsampled.
  26. return ctx.getImageData(0, 0, 1, 1).data[3] === 0;
  27. } else {
  28. return false;
  29. }
  30. }
  31. /**
  32. * Detecting vertical squash in loaded image.
  33. * Fixes a bug which squash image vertically while drawing into canvas for some images.
  34. */
  35. function detectVerticalSquash(img, iw, ih) {
  36. var canvas = document.createElement('canvas');
  37. canvas.width = 1;
  38. canvas.height = ih;
  39. var ctx = canvas.getContext('2d');
  40. ctx.drawImage(img, 0, 0);
  41. var data = ctx.getImageData(0, 0, 1, ih).data;
  42. // search image edge pixel position in case it is squashed vertically.
  43. var sy = 0;
  44. var ey = ih;
  45. var py = ih;
  46. while (py > sy) {
  47. var alpha = data[(py - 1) * 4 + 3];
  48. if (alpha === 0) {
  49. ey = py;
  50. } else {
  51. sy = py;
  52. }
  53. py = (ey + sy) >> 1;
  54. }
  55. var ratio = (py / ih);
  56. return (ratio === 0) ? 1 : ratio;
  57. }
  58. /**
  59. * Rendering image element (with resizing) and get its data URL
  60. */
  61. function renderImageToDataURL(img, options, doSquash) {
  62. var canvas = document.createElement('canvas');
  63. renderImageToCanvas(img, canvas, options, doSquash);
  64. return canvas.toDataURL("image/jpeg", options.quality || 0.8);
  65. }
  66. /**
  67. * Rendering image element (with resizing) into the canvas element
  68. */
  69. function renderImageToCanvas(img, canvas, options, doSquash) {
  70. var iw = img.naturalWidth, ih = img.naturalHeight;
  71. var width = options.width, height = options.height;
  72. var ctx = canvas.getContext('2d');
  73. ctx.save();
  74. transformCoordinate(canvas, ctx, width, height, options.orientation);
  75. var subsampled = detectSubsampling(img);
  76. if (subsampled) {
  77. iw /= 2;
  78. ih /= 2;
  79. }
  80. var d = 1024; // size of tiling canvas
  81. var tmpCanvas = document.createElement('canvas');
  82. tmpCanvas.width = tmpCanvas.height = d;
  83. var tmpCtx = tmpCanvas.getContext('2d');
  84. var vertSquashRatio = doSquash ? detectVerticalSquash(img, iw, ih) : 1;
  85. var dw = Math.ceil(d * width / iw);
  86. var dh = Math.ceil(d * height / ih / vertSquashRatio);
  87. var sy = 0;
  88. var dy = 0;
  89. while (sy < ih) {
  90. var sx = 0;
  91. var dx = 0;
  92. while (sx < iw) {
  93. tmpCtx.clearRect(0, 0, d, d);
  94. tmpCtx.drawImage(img, -sx, -sy);
  95. ctx.drawImage(tmpCanvas, 0, 0, d, d, dx, dy, dw, dh);
  96. sx += d;
  97. dx += dw;
  98. }
  99. sy += d;
  100. dy += dh;
  101. }
  102. ctx.restore();
  103. tmpCanvas = tmpCtx = null;
  104. }
  105. /**
  106. * Transform canvas coordination according to specified frame size and orientation
  107. * Orientation value is from EXIF tag
  108. */
  109. function transformCoordinate(canvas, ctx, width, height, orientation) {
  110. switch (orientation) {
  111. case 5:
  112. case 6:
  113. case 7:
  114. case 8:
  115. canvas.width = height;
  116. canvas.height = width;
  117. break;
  118. default:
  119. canvas.width = width;
  120. canvas.height = height;
  121. }
  122. switch (orientation) {
  123. case 2:
  124. // horizontal flip
  125. ctx.translate(width, 0);
  126. ctx.scale(-1, 1);
  127. break;
  128. case 3:
  129. // 180 rotate left
  130. ctx.translate(width, height);
  131. ctx.rotate(Math.PI);
  132. break;
  133. case 4:
  134. // vertical flip
  135. ctx.translate(0, height);
  136. ctx.scale(1, -1);
  137. break;
  138. case 5:
  139. // vertical flip + 90 rotate right
  140. ctx.rotate(0.5 * Math.PI);
  141. ctx.scale(1, -1);
  142. break;
  143. case 6:
  144. // 90 rotate right
  145. ctx.rotate(0.5 * Math.PI);
  146. ctx.translate(0, -height);
  147. break;
  148. case 7:
  149. // horizontal flip + 90 rotate right
  150. ctx.rotate(0.5 * Math.PI);
  151. ctx.translate(width, -height);
  152. ctx.scale(-1, 1);
  153. break;
  154. case 8:
  155. // 90 rotate left
  156. ctx.rotate(-0.5 * Math.PI);
  157. ctx.translate(-width, 0);
  158. break;
  159. default:
  160. break;
  161. }
  162. }
  163. /**
  164. * MegaPixImage class
  165. */
  166. function MegaPixImage(srcImage) {
  167. if (window.Blob && srcImage instanceof Blob) {
  168. var img = new Image();
  169. var URL = window.URL && window.URL.createObjectURL ? window.URL :
  170. window.webkitURL && window.webkitURL.createObjectURL ? window.webkitURL :
  171. null;
  172. if (!URL) { throw Error("No createObjectURL function found to create blob url"); }
  173. img.src = URL.createObjectURL(srcImage);
  174. this.blob = srcImage;
  175. srcImage = img;
  176. }
  177. if (!srcImage.naturalWidth && !srcImage.naturalHeight) {
  178. var _this = this;
  179. srcImage.onload = function () {
  180. var listeners = _this.imageLoadListeners;
  181. if (listeners) {
  182. _this.imageLoadListeners = null;
  183. for (var i = 0, len = listeners.length; i < len; i++) {
  184. listeners[i]();
  185. }
  186. }
  187. };
  188. this.imageLoadListeners = [];
  189. }
  190. this.srcImage = srcImage;
  191. }
  192. /**
  193. * Rendering megapix image into specified target element
  194. */
  195. MegaPixImage.prototype.render = function (target, options) {
  196. if (this.imageLoadListeners) {
  197. var _this = this;
  198. this.imageLoadListeners.push(function () { _this.render(target, options) });
  199. return;
  200. }
  201. options = options || {};
  202. var imgWidth = this.srcImage.naturalWidth, imgHeight = this.srcImage.naturalHeight,
  203. width = options.width, height = options.height,
  204. maxWidth = options.maxWidth, maxHeight = options.maxHeight,
  205. doSquash = !this.blob || this.blob.type === 'image/jpeg';
  206. if (width && !height) {
  207. height = (imgHeight * width / imgWidth) << 0;
  208. } else if (height && !width) {
  209. width = (imgWidth * height / imgHeight) << 0;
  210. } else {
  211. width = imgWidth;
  212. height = imgHeight;
  213. }
  214. if (maxWidth && width > maxWidth) {
  215. width = maxWidth;
  216. height = (imgHeight * width / imgWidth) << 0;
  217. }
  218. if (maxHeight && height > maxHeight) {
  219. height = maxHeight;
  220. width = (imgWidth * height / imgHeight) << 0;
  221. }
  222. var opt = { width: width, height: height };
  223. for (var k in options) opt[k] = options[k];
  224. var tagName = target.tagName.toLowerCase();
  225. if (tagName === 'img') {
  226. target.src = renderImageToDataURL(this.srcImage, opt, doSquash);
  227. } else if (tagName === 'canvas') {
  228. renderImageToCanvas(this.srcImage, target, opt, doSquash);
  229. }
  230. if (typeof this.onrender === 'function') {
  231. this.onrender(target);
  232. }
  233. };
  234. /**
  235. * Export class to global
  236. */
  237. if (typeof define === 'function' && define.amd) {
  238. define([], function () { return MegaPixImage; }); // for AMD loader
  239. } else {
  240. this.MegaPixImage = MegaPixImage;
  241. }
  242. })();