2
2
* @fileOverview Image
3
3
*/
4
4
define ( [
5
+ '../../base' ,
5
6
'./runtime' ,
6
7
'./util' ,
7
8
'./imagemeta'
8
- ] , function ( Html5Runtime , Util , ImageMeta ) {
9
+ ] , function ( Base , Html5Runtime , Util , ImageMeta ) {
9
10
10
11
var BLANK = 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D' ;
11
12
@@ -27,7 +28,7 @@ define([
27
28
} ;
28
29
29
30
// 读取meta信息。
30
- if ( ! me . _metas && ~ 'image/jpegimage/jpg' . indexOf ( me . type ) ) {
31
+ if ( ! me . _metas && 'image/jpeg' === me . type ) {
31
32
ImageMeta . parse ( me . _blob , function ( error , ret ) {
32
33
me . _metas = ret ;
33
34
me . owner . trigger ( 'load' ) ;
@@ -251,101 +252,126 @@ define([
251
252
}
252
253
} ,
253
254
254
- _renderImageToCanvas : function ( canvas , img , x , y , w , h ) {
255
- canvas . getContext ( '2d' ) . drawImage ( img , x , y , w , h ) ;
256
- }
255
+ // https://github.com/stomita/ios-imagefile-megapixel/
256
+ // blob/master/src/megapix-image.js
257
+ _renderImageToCanvas : ( function ( ) {
257
258
258
- /*_renderImageToCanvas: (function() {
259
- var subsampled, vertSquashRatio;
259
+ // 如果不是ios6, 不需要这么复杂!
260
+ if ( ! Base . os . ios || ~ ~ Base . os . ios !== 6 ) {
261
+ return function ( canvas , img , x , y , w , h ) {
262
+ canvas . getContext ( '2d' ) . drawImage ( img , x , y , w , h ) ;
263
+ } ;
264
+ }
260
265
261
- // Detect subsampling in loaded image.
262
- // In iOS, larger images than 2M pixels may be subsampled in rendering.
263
- function detectSubsampling(img) {
266
+ /**
267
+ * Detect subsampling in loaded image.
268
+ * In iOS, larger images than 2M pixels may be
269
+ * subsampled in rendering.
270
+ */
271
+ function detectSubsampling ( img ) {
264
272
var iw = img . naturalWidth ,
265
- ih = img.naturalHeight;
266
- if (iw * ih > 1024 * 1024) { // subsampling may happen over megapixel image
267
- var canvas = document.createElement('canvas');
273
+ ih = img . naturalHeight ,
274
+ canvas , ctx ;
275
+
276
+ // subsampling may happen overmegapixel image
277
+ if ( iw * ih > 1024 * 1024 ) {
278
+ canvas = document . createElement ( 'canvas' ) ;
268
279
canvas . width = canvas . height = 1 ;
269
- var ctx = canvas.getContext('2d');
270
- ctx.drawImage(img, -iw + 1, 0);
280
+ ctx = canvas . getContext ( '2d' ) ;
281
+ ctx . drawImage ( img , - iw + 1 , 0 ) ;
282
+
271
283
// subsampled image becomes half smaller in rendering size.
272
- // check alpha channel value to confirm image is covering edge pixel or not.
273
- // if alpha value is 0 image is not covering, hence subsampled.
274
- return ctx.getImageData(0, 0, 1, 1).data[3] === 0;
284
+ // check alpha channel value to confirm image is covering
285
+ // edge pixel or not. if alpha value is 0
286
+ // image is not covering, hence subsampled.
287
+ return ctx . getImageData ( 0 , 0 , 1 , 1 ) . data [ 3 ] === 0 ;
275
288
} else {
276
289
return false ;
277
290
}
278
291
}
279
292
280
293
281
- // Detecting vertical squash in loaded image.
282
- // Fixes a bug which squash image vertically while drawing into canvas for some images.
283
- function detectVerticalSquash(img, iw, ih) {
284
- var canvas = document.createElement('canvas');
294
+ /**
295
+ * Detecting vertical squash in loaded image.
296
+ * Fixes a bug which squash image vertically while drawing into
297
+ * canvas for some images.
298
+ */
299
+ function detectVerticalSquash ( img , iw , ih ) {
300
+ var canvas = document . createElement ( 'canvas' ) ,
301
+ ctx = canvas . getContext ( '2d' ) ,
302
+ sy = 0 ,
303
+ ey = ih ,
304
+ py = ih ,
305
+ data , alpha , ratio ;
306
+
307
+
285
308
canvas . width = 1 ;
286
309
canvas . height = ih ;
287
- var ctx = canvas.getContext('2d');
288
- ctx.drawImage(img, 0, 0);
289
- var data = ctx.getImageData(0, 0, 1, ih).data;
290
- // search image edge pixel position in case it is squashed vertically.
291
- var sy = 0;
292
- var ey = ih;
293
- var py = ih;
294
- while (py > sy) {
295
- var alpha = data[(py - 1) * 4 + 3];
296
- if (alpha === 0) {
310
+ ctx . drawImage ( img , 0 , 0 ) ;
311
+ data = ctx . getImageData ( 0 , 0 , 1 , ih ) . data ;
312
+
313
+ // search image edge pixel position in case
314
+ // it is squashed vertically.
315
+ while ( py > sy ) {
316
+ alpha = data [ ( py - 1 ) * 4 + 3 ] ;
317
+
318
+ if ( alpha === 0 ) {
297
319
ey = py ;
298
320
} else {
299
321
sy = py ;
300
322
}
323
+
301
324
py = ( ey + sy ) >> 1 ;
302
325
}
303
- var ratio = (py / ih);
326
+
327
+ ratio = ( py / ih ) ;
304
328
return ( ratio === 0 ) ? 1 : ratio ;
305
329
}
306
330
307
- return function( canvas, img, x, y, w, h ) {
308
-
309
-
310
- var iw = img.naturalWidth, ih = img.naturalHeight;
311
- var width = w, height = h;
312
- var ctx = canvas.getContext('2d');
313
- ctx.save();
331
+ return function ( canvas , img , x , y , width , height ) {
332
+ var iw = img . naturalWidth ,
333
+ ih = img . naturalHeight ,
334
+ ctx = canvas . getContext ( '2d' ) ,
335
+ subsampled = detectSubsampling ( img ) ,
336
+ doSquash = this . type === 'image/jpeg' ,
337
+ d = 1024 ,
338
+ sy = 0 ,
339
+ dy = 0 ,
340
+ tmpCanvas , tmpCtx , vertSquashRatio , dw , dh , sx , dx ;
314
341
315
- subsampled = typeof subsampled === 'undefined' ? detectSubsampling( img ) : subsampled;
316
342
if ( subsampled ) {
317
343
iw /= 2 ;
318
344
ih /= 2 ;
319
345
}
320
346
321
- var d = 1024; // size of tiling canvas
322
- var tmpCanvas = document.createElement('canvas');
347
+ ctx . save ( ) ;
348
+ tmpCanvas = document . createElement ( 'canvas' ) ;
323
349
tmpCanvas . width = tmpCanvas . height = d ;
324
- var tmpCtx = tmpCanvas.getContext('2d');
325
-
326
- vertSquashRatio = vertSquashRatio || detectVerticalSquash(img, iw, ih);
327
- console.log( vertSquashRatio ) ;
328
-
329
- var dw = Math.ceil(d * width / iw);
330
- var dh = Math.ceil(d * height / ih / vertSquashRatio);
331
- var sy = 0;
332
- var dy = 0;
333
- while (sy < ih) {
334
- var sx = 0;
335
- var dx = 0;
336
- while (sx < iw) {
337
- tmpCtx.clearRect(0, 0, d, d );
338
- tmpCtx .drawImage(img, x - sx, y - sy );
339
- ctx.drawImage(tmpCanvas, 0, 0, d, d, dx, dy, dw, dh);
340
- sx += d;
341
- dx += dw;
342
- }
343
- sy += d;
344
- dy += dh;
350
+
351
+ tmpCtx = tmpCanvas . getContext ( '2d' ) ;
352
+ vertSquashRatio = doSquash ?
353
+ detectVerticalSquash ( img , iw , ih ) : 1 ;
354
+
355
+ dw = Math . ceil ( d * width / iw ) ;
356
+ dh = Math . ceil ( d * height / ih / vertSquashRatio ) ;
357
+
358
+ while ( sy < ih ) {
359
+ sx = 0 ;
360
+ dx = 0 ;
361
+ while ( sx < iw ) {
362
+ tmpCtx . clearRect ( 0 , 0 , d , d ) ;
363
+ tmpCtx . drawImage ( img , - sx , - sy ) ;
364
+ ctx . drawImage ( tmpCanvas , 0 , 0 , d , d ,
365
+ x + dx , y + dy , dw , dh ) ;
366
+ sx += d ;
367
+ dx += dw ;
368
+ }
369
+ sy += d ;
370
+ dy += dh ;
345
371
}
346
372
ctx . restore ( ) ;
347
373
tmpCanvas = tmpCtx = null ;
348
374
} ;
349
- })()*/
375
+ } ) ( )
350
376
} ) ;
351
377
} ) ;
0 commit comments