B2G/gaia/apps/gallery/js/MetadataParser.js
// If it is a large image, create and save a preview for it as well.
If it is a large image, create and save a preview for it as well.
If it is a large image create and save a preview for it as well .
function createThumbnailAndPreview(file, callback, error, nopreview,
                                     bigFile) {
    var metadata = {};
    var url = URL.createObjectURL(file);
    offscreenImage.src = url;
    offscreenImage.onerror = function() {
      URL.revokeObjectURL(url);
      offscreenImage.src = '';
      error('createThumbnailAndPreview: Image failed to load');
    };
    offscreenImage.onload = function() {
      URL.revokeObjectURL(url);
      var iw = metadata.width = offscreenImage.width;
      var ih = metadata.height = offscreenImage.height;
       XXX: When bug 854795 is fixed, we'll be able to create previews
       for large images without using so much memory, and we can remove
       this flag then.
      if (iw * ih > 2 * 1024 * 1024 && bigFile)
        bigFile();
       If the image was already thumbnail size, it is its own thumbnail
       and it does not need a preview
      if (metadata.width <= THUMBNAIL_WIDTH &&
          metadata.height <= THUMBNAIL_HEIGHT) {
        offscreenImage.src = '';
         XXX
         Because of a gecko bug, we can't just store the image file itself
         we've got to create an equivalent but distinct blob.
         When https:bugzilla.mozilla.org/show_bug.cgi?id=794619 is fixed
         the line below can change to just assign file.
        metadata.thumbnail = file.slice(0, file.size, file.type);
        callback(metadata);
      }
      else {
        createThumbnailFromElement(offscreenImage, false, 0, gotThumbnail);
      }
      function gotThumbnail(thumbnail) {
        metadata.thumbnail = thumbnail;
         If no preview was requested, or if if the image was less
         than half a megapixel then it does not need a preview
         image, and we can call the callback right away
        if (nopreview || metadata.width * metadata.height < 512 * 1024) {
          offscreenImage.src = '';
          callback(metadata);
        }
        else {
           Otherwise, this was a big image and we need to create a
           preview for it so we can avoid decoding the full size
           image again when possible
          createAndSavePreview();
        }
      }
      function createAndSavePreview() {
         Figure out the preview size.
         Make sure the size is big enough for both landscape and portrait
        var scale = Math.max(Math.min(sw / iw, sh / ih, 1),
                             Math.min(sh / iw, sw / ih, 1));
        var pw = iw * scale, ph = ih * scale;  preview width and height;
         Create the preview in a canvas
        var canvas = document.createElement('canvas');
        canvas.width = pw;
        canvas.height = ph;
        var context = canvas.getContext('2d');
        context.drawImage(offscreenImage, 0, 0, iw, ih, 0, 0, pw, ph);
        canvas.toBlob(function(blob) {
          offscreenImage.src = '';
          canvas.width = canvas.height = 0;
          savePreview(blob);
        }, 'image/jpeg');
        function savePreview(previewblob) {
          var storage = navigator.getDeviceStorage('pictures');
          var filename;
          if (file.name[0] === '/') {
             We expect file.name to be a fully qualified name (perhaps
             something like /sdcard/DCIM/100MZLLA/IMG_0001.jpg).
            var slashIndex = file.name.indexOf('/', 1);
            if (slashIndex < 0) {
              error("savePreview: Bad filename: '" + file.name + "'");
              return;
            }
            filename =
              file.name.substring(0, slashIndex) +  storageName (i.e. /sdcard)
              '/.gallery/previews' +
              file.name.substring(slashIndex);  rest of path (i,e, /DCIM/...)
          } else {
             On non-composite storage areas (e.g. desktop), file.name will be
             a relative path.
            filename = '.gallery/previews/' + file.name;
          }
           Delete any existing preview by this name
          var delreq = storage.delete(filename);
          delreq.onsuccess = delreq.onerror = save;
          function save() {
            var savereq = storage.addNamed(previewblob, filename);
            savereq.onerror = function() {
              console.error('Could not save preview image', filename);
            };
             Don't actually wait for the save to complete. Go start
             scanning the next one.
            metadata.preview = {
              filename: filename,
              width: pw,
              height: ph
            };
            callback(metadata);
          }
        }
      }
    };
  }
function createThumbnailAndPreview ( file , callback , error , nopreview , bigFile ) { var metadata = { } ; var url = URL . createObjectURL ( file ) ; offscreenImage . src = url ; offscreenImage . onerror = function ( ) { URL . revokeObjectURL ( url ) ; offscreenImage . src = '' ; error ( 'createThumbnailAndPreview: Image failed to load' ) ; } ; offscreenImage . onload = function ( ) { URL . revokeObjectURL ( url ) ; var iw = metadata . width = offscreenImage . width ; var ih = metadata . height = offscreenImage . height ; if ( iw * ih > 2 * 1024 * 1024 && bigFile ) bigFile ( ) ; if ( metadata . width <= THUMBNAIL_WIDTH && metadata . height <= THUMBNAIL_HEIGHT ) { offscreenImage . src = '' ; metadata . thumbnail = file . slice ( 0 , file . size , file . type ) ; callback ( metadata ) ; } else { createThumbnailFromElement ( offscreenImage , false , 0 , gotThumbnail ) ; } function gotThumbnail ( thumbnail ) { metadata . thumbnail = thumbnail ; if ( nopreview || metadata . width * metadata . height < 512 * 1024 ) { offscreenImage . src = '' ; callback ( metadata ) ; } else { createAndSavePreview ( ) ; } } function createAndSavePreview ( ) { var scale = Math . max ( Math . min ( sw / iw , sh / ih , 1 ) , Math . min ( sh / iw , sw / ih , 1 ) ) ; var pw = iw * scale , ph = ih * scale ; var canvas = document . createElement ( 'canvas' ) ; canvas . width = pw ; canvas . height = ph ; var context = canvas . getContext ( '2d' ) ; context . drawImage ( offscreenImage , 0 , 0 , iw , ih , 0 , 0 , pw , ph ) ; canvas . toBlob ( function ( blob ) { offscreenImage . src = '' ; canvas . width = canvas . height = 0 ; savePreview ( blob ) ; } , 'image/jpeg' ) ; function savePreview ( previewblob ) { var storage = navigator . getDeviceStorage ( 'pictures' ) ; var filename ; if ( file . name [ 0 ] === '/' ) { var slashIndex = file . name . indexOf ( '/' , 1 ) ; if ( slashIndex < 0 ) { error ( "savePreview: Bad filename: '" + file . name + "'" ) ; return ; } filename = file . name . substring ( 0 , slashIndex ) + '/.gallery/previews' + file . name . substring ( slashIndex ) ; } else { filename = '.gallery/previews/' + file . name ; } var delreq = storage . delete ( filename ) ; delreq . onsuccess = delreq . onerror = save ; function save ( ) { var savereq = storage . addNamed ( previewblob , filename ) ; savereq . onerror = function ( ) { console . error ( 'Could not save preview image' , filename ) ; } ; metadata . preview = { filename : filename , width : pw , height : ph } ; callback ( metadata ) ; } } } } ; }
If it is a large image, create and save a preview for it as well.
If it is a large image create and save a preview for it as well .
// If this is a big image, then decoding it takes a lot of memory. // We set this flag to prevent the user from zooming in on other // images at the same time because that also takes a lot of memory // and we don't want to crash with an OOM. If we find one big image // we assume that there may be others, so the flag remains set until // the current scan is complete. // // XXX: When bug 854795 is fixed, we'll be able to create previews // for large images without using so much memory, and we can remove // this flag then. // If the image was already thumbnail size, it is its own thumbnail // and it does not need a preview // // XXX // Because of a gecko bug, we can't just store the image file itself // we've got to create an equivalent but distinct blob. // When https://bugzilla.mozilla.org/show_bug.cgi?id=794619 is fixed // the line below can change to just assign file. // // If no preview was requested, or if if the image was less // than half a megapixel then it does not need a preview // image, and we can call the callback right away // Otherwise, this was a big image and we need to create a // preview for it so we can avoid decoding the full size // image again when possible // Figure out the preview size. // Make sure the size is big enough for both landscape and portrait // preview width and height; // Create the preview in a canvas // We expect file.name to be a fully qualified name (perhaps // something like /sdcard/DCIM/100MZLLA/IMG_0001.jpg). // storageName (i.e. /sdcard) // rest of path (i,e, /DCIM/...) // On non-composite storage areas (e.g. desktop), file.name will be // a relative path. // Delete any existing preview by this name // Don't actually wait for the save to complete. Go start // scanning the next one.