started work on preview generation...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2016-05-22 21:10:37 +03:00
parent 44cd484def
commit d9f4c52f8c
6 changed files with 251 additions and 19 deletions

View File

@ -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')
}

View File

@ -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',
])

View File

@ -52,6 +52,8 @@ core.ImageGridFeatures.Feature('viewer-testing', [
'ui',
'keyboard',
'sharp',
'ui-ribbons-placement',
'ui-fullscreen-controls',

197
ui (gen4)/features/sharp.js Executable file
View File

@ -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 })

View File

@ -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
}

View File

@ -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"
},