From d9f4c52f8c574590405a6d85812ff68662a9c334 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sun, 22 May 2016 21:10:37 +0300 Subject: [PATCH] started work on preview generation... Signed-off-by: Alex A. Naanou --- ui (gen4)/features/all.js | 1 + ui (gen4)/features/filesystem.js | 67 ++++++++--- ui (gen4)/features/meta.js | 2 + ui (gen4)/features/sharp.js | 197 +++++++++++++++++++++++++++++++ ui (gen4)/lib/features.js | 2 +- ui (gen4)/package.json | 1 + 6 files changed, 251 insertions(+), 19 deletions(-) create mode 100755 ui (gen4)/features/sharp.js diff --git a/ui (gen4)/features/all.js b/ui (gen4)/features/all.js index 735c588c..8cb50124 100755 --- a/ui (gen4)/features/all.js +++ b/ui (gen4)/features/all.js @@ -35,6 +35,7 @@ require('features/demo') // node features... if(typeof(window) == 'undefined' || window.nodejs != null){ require('features/filesystem') + require('features/sharp') require('features/cli') } diff --git a/ui (gen4)/features/filesystem.js b/ui (gen4)/features/filesystem.js index 9cb29784..58b80b42 100755 --- a/ui (gen4)/features/filesystem.js +++ b/ui (gen4)/features/filesystem.js @@ -45,6 +45,38 @@ if(typeof(process) != 'undefined'){ +/*********************************************************************/ + +var FileSystemInfoActions = actions.Actions({ + getImagePath: ['- System/', + function(gid, type){ + gid = this.data.getImage(gid) + + var img = this.images[gid] + + return pathlib.join(img.base_path || this.location.path, img.path) + }], +}) + + +var FileSystemInfo = +module.FileSystemInfo = core.ImageGridFeatures.Feature({ + title: '', + doc: '', + + tag: 'fs-info', + depends: [ + 'location', + ], + + actions: FileSystemInfoActions, + + isApplicable: function(){ + return this.runtime == 'node' || this.runtime == 'nw' }, +}) + + + /*********************************************************************/ // Loader... @@ -398,6 +430,7 @@ module.FileSystemLoader = core.ImageGridFeatures.Feature({ tag: 'fs-loader', depends: [ + 'fs-info', 'location', 'tasks', ], @@ -411,24 +444,6 @@ module.FileSystemLoader = core.ImageGridFeatures.Feature({ isApplicable: function(){ return this.runtime == 'node' || this.runtime == 'nw' }, - - handlers: [ - // save/resore .savecomments - // - ['json', - function(res){ - if(this.savecomments != null){ - res.savecomments = JSON.parse(JSON.stringify(this.savecomments)) - } - }], - ['load', - function(_, data){ - if(data.savecomments != null){ - this.savecomments = data.savecomments - } - }], - - ], }) @@ -720,6 +735,21 @@ module.FileSystemSaveHistory = core.ImageGridFeatures.Feature({ actions: FileSystemSaveHistoryActions, handlers: [ + // save/resore .savecomments + // + ['json', + function(res){ + if(this.savecomments != null){ + res.savecomments = JSON.parse(JSON.stringify(this.savecomments)) + } + }], + ['load', + function(_, data){ + if(data.savecomments != null){ + this.savecomments = data.savecomments + } + }], + // Prepare comments for writing... // // NOTE: defining this here enables us to actually post-bind to @@ -1939,6 +1969,7 @@ module.FileSystemWriterUI = core.ImageGridFeatures.Feature({ //--------------------------------------------------------------------- core.ImageGridFeatures.Feature('fs', [ + 'fs-info', 'fs-loader', 'fs-writer', ]) diff --git a/ui (gen4)/features/meta.js b/ui (gen4)/features/meta.js index fe041c84..5787939f 100755 --- a/ui (gen4)/features/meta.js +++ b/ui (gen4)/features/meta.js @@ -52,6 +52,8 @@ core.ImageGridFeatures.Feature('viewer-testing', [ 'ui', 'keyboard', + 'sharp', + 'ui-ribbons-placement', 'ui-fullscreen-controls', diff --git a/ui (gen4)/features/sharp.js b/ui (gen4)/features/sharp.js new file mode 100755 index 00000000..da990da0 --- /dev/null +++ b/ui (gen4)/features/sharp.js @@ -0,0 +1,197 @@ +/********************************************************************** +* +* +* +**********************************************************************/ + +define(function(require){ var module = {} + +//var DEBUG = DEBUG != null ? DEBUG : true + +var actions = require('lib/actions') +var features = require('lib/features') + +var core = require('features/core') + +try{ + var sharp = requirejs('sharp') + +} catch(err){ + sharp = null +} + +if(typeof(process) != 'undefined'){ + var fse = requirejs('fs-extra') + var pathlib = requirejs('path') + var glob = requirejs('glob') + var file = requirejs('./file') +} + + + +/*********************************************************************/ + +if(typeof(process) != 'undefined'){ + var ensureDir = file.denodeify(fse.ensureDir) +} + + +/*********************************************************************/ + +var SharpActions = actions.Actions({ + config: { + 'preview-path': '$INDEX/$RESOLUTIONpx', + + 'preview-normalized': true, + + 'preview-sizes': [ + //1920, + //1280, + 900, + 350, + 150, + 75, + ] + }, + + // .makePreviews() + // .makePreviews('current') + // -> actions + // + // .makePreviews(gid) + // -> actions + // + // .makePreviews([gid, gid, ..]) + // -> actions + // + // .makePreviews('all') + // -> actions + // + // XXX should this account for non-jpeg images??? + makePreviews: ['Sharp/Make image previews', + function(images, sizes, logger){ + logger = logger || this.logger + + images = images || this.current + // keywords... + images = images == 'all' ? this.data.getImages('all') + : images == 'current' ? this.current + : images + images = images instanceof Array ? images : [images] + + var cfg_sizes = this.config['preview-sizes'] || [] + cfg_sizes + .sort() + .reverse() + + sizes = sizes || cfg_sizes + sizes = sizes instanceof Array ? sizes : [sizes] + + // normalize to preview size... + sizes = (this.config['preview-normalized'] ? + sizes + .map(function(s){ + return cfg_sizes.filter(function(c){ return c > s }).pop() || s }) + : sizes) + .unique() + + var that = this + return Promise.all(images.map(function(gid){ + var data = that.images[gid] + var preview = data.preview = data.preview || {} + var path = that.getImagePath(gid) + + var img = sharp(path) + + return img.metadata().then(function(metadata){ + var orig_res = Math.max(metadata.width, metadata.height) + + return Promise.all(sizes.map(function(res){ + + // skip if image is smaller than res... + if(res >= orig_res){ + return + } + + var ext = data.ext || '' + + // build the target path... + var target = (that.config['preview-path'] || '$INDEX') + .replace(/\$INDEX|\$\{INDEX\}/g, that.config['index-dir']) + .replace(/\$RESOLUTION|\$\{RESOLUTION\}/g, res) + // XXX do we need to account for non-jpeg extensions??? + var target = pathlib.join(target, gid +' - '+ data.name + ext) + + var base = data.base_path || that.location.path + var path = pathlib.join(base, target) + + logger && logger.emit('queued', target) + + return ensureDir(pathlib.dirname(path)) + .then(function(){ + // check if image exists... + if(fse.existsSync(path)){ + preview[res + 'px'] = target + that.markChanged(gid) + + logger && logger.emit('skipped', target) + + return + } + + return img.clone() + .resize(res, res) + .max() + .interpolateWith('nohalo') + .withMetadata() + .toFile(path) + .then(function(){ + preview[res + 'px'] = target + that.markChanged(gid) + + logger && logger.emit('done', target) + }) + }) + })) + }) + })) + }], +}) + +var Sharp = +module.Sharp = core.ImageGridFeatures.Feature({ + title: '', + doc: '', + + tag: 'sharp', + depends: [ + 'location', + ], + + actions: SharpActions, + + isApplicable: function(){ return !!sharp }, + + handlers: [ + ['updateImage.pre', + function(gid){ + var that = this + if(this.images[gid].preview == null){ + sharp(this.getImagePath(gid)) + .metadata() + .then(function(metadata){ + if(Math.max(metadata.width, metadata.height) + > Math.max.apply(Math, that.config['preview-sizes'])){ + that.makePreviews(gid) + } + }) + } + }] + ], +}) + + + +/********************************************************************** +* vim:set ts=4 sw=4 : */ +return module }) diff --git a/ui (gen4)/lib/features.js b/ui (gen4)/lib/features.js index 5692f2bb..90f5d19f 100755 --- a/ui (gen4)/lib/features.js +++ b/ui (gen4)/lib/features.js @@ -487,7 +487,7 @@ module.FeatureSet = { } // check applicability... - if(!e.isApplicable.call(that, obj)){ + if(e.isApplicable && !e.isApplicable.call(that, obj)){ unapplicable.push(n) return false } diff --git a/ui (gen4)/package.json b/ui (gen4)/package.json index 54e35cca..e7cd48c7 100755 --- a/ui (gen4)/package.json +++ b/ui (gen4)/package.json @@ -23,6 +23,7 @@ "fs-walk": "0.0.1", "glob": "^4.0.6", "guarantee-events": "^1.0.0", + "openseadragon": "^2.1.0", "requirejs": "^2.1.23", "sharp": "^0.12.0" },