|
1 | 1 | /* |
2 | | - * JavaScript Load Image iOS scaling fixes 1.0.0 |
| 2 | + * JavaScript Load Image iOS scaling fixes 1.0.1 |
3 | 3 | * https://github.com/blueimp/JavaScript-Load-Image |
4 | 4 | * |
5 | 5 | * Copyright 2013, Sebastian Tschan |
|
53 | 53 | }; |
54 | 54 |
|
55 | 55 | // Detects vertical squash in JPEG images: |
56 | | - loadImage.detectVerticalSquash = function (img, correctedHeight) { |
57 | | - var canvas = document.createElement('canvas'), |
| 56 | + loadImage.detectVerticalSquash = function (img, subsampled) { |
| 57 | + var naturalHeight = img.naturalHeight || img.height, |
| 58 | + canvas = document.createElement('canvas'), |
58 | 59 | context = canvas.getContext('2d'), |
59 | 60 | data, |
60 | 61 | sy, |
61 | 62 | ey, |
62 | 63 | py, |
63 | 64 | alpha; |
| 65 | + if (subsampled) { |
| 66 | + naturalHeight /= 2; |
| 67 | + } |
64 | 68 | canvas.width = 1; |
65 | | - canvas.height = correctedHeight; |
| 69 | + canvas.height = naturalHeight; |
66 | 70 | context.drawImage(img, 0, 0); |
67 | | - data = context.getImageData(0, 0, 1, correctedHeight).data; |
| 71 | + data = context.getImageData(0, 0, 1, naturalHeight).data; |
68 | 72 | // search image edge pixel position in case it is squashed vertically: |
69 | 73 | sy = 0; |
70 | | - ey = correctedHeight; |
71 | | - py = correctedHeight; |
| 74 | + ey = naturalHeight; |
| 75 | + py = naturalHeight; |
72 | 76 | while (py > sy) { |
73 | 77 | alpha = data[(py - 1) * 4 + 3]; |
74 | 78 | if (alpha === 0) { |
|
78 | 82 | } |
79 | 83 | py = (ey + sy) >> 1; |
80 | 84 | } |
81 | | - return (py / correctedHeight) || 1; |
| 85 | + return (py / naturalHeight) || 1; |
82 | 86 | }; |
83 | 87 |
|
84 | 88 | // Renders image to canvas while working around iOS image scaling bugs: |
|
100 | 104 | tmpCanvas = document.createElement('canvas'), |
101 | 105 | tileSize = 1024, |
102 | 106 | tmpContext = tmpCanvas.getContext('2d'), |
103 | | - subSampled, |
| 107 | + subsampled, |
104 | 108 | vertSquashRatio, |
105 | 109 | tileX, |
106 | 110 | tileY; |
107 | 111 | tmpCanvas.width = tileSize; |
108 | 112 | tmpCanvas.height = tileSize; |
109 | 113 | context.save(); |
110 | | - subSampled = loadImage.detectSubsampling(img); |
111 | | - if (subSampled) { |
| 114 | + subsampled = loadImage.detectSubsampling(img); |
| 115 | + if (subsampled) { |
| 116 | + sourceX /= 2; |
| 117 | + sourceY /= 2; |
112 | 118 | sourceWidth /= 2; |
113 | 119 | sourceHeight /= 2; |
114 | 120 | } |
115 | | - vertSquashRatio = loadImage.detectVerticalSquash(img, sourceHeight); |
116 | | - if (subSampled && vertSquashRatio !== 1) { |
| 121 | + vertSquashRatio = loadImage.detectVerticalSquash(img, subsampled); |
| 122 | + if (subsampled && vertSquashRatio !== 1) { |
117 | 123 | destWidth = Math.ceil(tileSize * destWidth / sourceWidth); |
118 | 124 | destHeight = Math.ceil( |
119 | 125 | tileSize * destHeight / sourceHeight / vertSquashRatio |
|
0 commit comments