cleanup...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2023-05-10 14:29:59 +03:00
parent f2306bb23b
commit 214c8d62c0

View File

@ -39,15 +39,18 @@ var object = require('lib/object')
var Filters = var Filters =
module.Filters = { module.Filters = {
makeCanvas: function(w, h, canvas){ makeCanvas: function(w, h, canvas){
var c = canvas || document.createElement('canvas') var c = canvas
|| document.createElement('canvas')
c.width = w c.width = w
c.height = h c.height = h
return c }, return c },
// as input takes an HTML Image object... // as input takes an HTML Image object...
getPixels: function(img, tmp_canvas, w, h){ getPixels: function(img, tmp_canvas, w, h){
var w = w || img.naturalWidth var w = w
var h = h || img.naturalHeight || img.naturalWidth
var h = h
|| img.naturalHeight
var c = this.makeCanvas(w, h, tmp_canvas) var c = this.makeCanvas(w, h, tmp_canvas)
var context = c.getContext('2d') var context = c.getContext('2d')
if(img == null){ if(img == null){
@ -58,8 +61,10 @@ module.Filters = {
context.drawImage(img, 0, 0, w, h) } context.drawImage(img, 0, 0, w, h) }
return context.getImageData(0, 0, c.width, c.height) }, return context.getImageData(0, 0, c.width, c.height) },
setPixels: function(c, data, w, h){ setPixels: function(c, data, w, h){
w = c.width = w || data.width w = c.width = w
h = c.height = h || data.height || data.width
h = c.height = h
|| data.height
var context = c.getContext('2d') var context = c.getContext('2d')
context.putImageData(data, 0, 0) }, context.putImageData(data, 0, 0) },
@ -72,10 +77,13 @@ module.Filters = {
// focusImage: 2 // focusImage: 2
// showMetadata // showMetadata
getNormalizedPixels: function(img, tmp_canvas, s, rotate, flip){ getNormalizedPixels: function(img, tmp_canvas, s, rotate, flip){
s = s || Math.max(img.naturalWidth, img.naturalHeight) s = s
rotate = rotate || 0 || Math.max(img.naturalWidth, img.naturalHeight)
rotate = rotate
|| 0
;(rotate == 90 || rotate == 270) ;(rotate == 90
|| rotate == 270)
&& (flip = flip == 'horizontal' ? && (flip = flip == 'horizontal' ?
'vertical' 'vertical'
: flip == 'vertical' ? : flip == 'vertical' ?
@ -116,13 +124,17 @@ module.Filters = {
var b = d[i+2] var b = d[i+2]
// CIE luminance for the RGB // CIE luminance for the RGB
// The human eye is bad at seeing red and blue, so we de-emphasize them. // The human eye is bad at seeing red and blue, so we de-emphasize them.
var v = 0.2126*r + 0.7152*g + 0.0722*b var v = 0.2126*r
+ 0.7152*g
+ 0.0722*b
d[i] = d[i+1] = d[i+2] = v } d[i] = d[i+1] = d[i+2] = v }
return pixels }, return pixels },
// XXX need to resize this... // XXX need to resize this...
histogram: function(pixels, mode, color){ histogram: function(pixels, mode, color){
color = color || 'fill' color = color
mode = mode || 'luminance' || 'fill'
mode = mode
|| 'luminance'
var size = 255 var size = 255
var w = size var w = size
@ -148,11 +160,14 @@ module.Filters = {
count[v] = count[v+1] = count[v+2] = (count[v] || 0) + 1 count[v] = count[v+1] = count[v+2] = (count[v] || 0) + 1
} else { } else {
if(mode == 'color' || mode == 'R'){ if(mode == 'color'
|| mode == 'R'){
count[r*4] = (count[r*4] || 0) + 1 } count[r*4] = (count[r*4] || 0) + 1 }
if(mode == 'color' || mode == 'G'){ if(mode == 'color'
|| mode == 'G'){
count[g*4+1] = (count[g*4+1] || 0) + 1 } count[g*4+1] = (count[g*4+1] || 0) + 1 }
if(mode == 'color' || mode == 'B'){ if(mode == 'color'
|| mode == 'B'){
count[b*4+2] = (count[b*4+2] || 0) + 1 } } } count[b*4+2] = (count[b*4+2] || 0) + 1 } } }
var m = size / Math.max(...count.filter(function(){ return true })) var m = size / Math.max(...count.filter(function(){ return true }))
@ -179,8 +194,10 @@ module.Filters = {
return out }, return out },
waveform: function(pixels, mode, color){ waveform: function(pixels, mode, color){
mode = mode || 'luminance' mode = mode
color = color || 'normalized' || 'luminance'
color = color
|| 'normalized'
var w = pixels.width var w = pixels.width
var h = pixels.height var h = pixels.height
@ -225,20 +242,17 @@ module.Filters = {
var Bl = 0.0722 var Bl = 0.0722
for(var i=0; i<d.length; i+=4){ for(var i=0; i<d.length; i+=4){
var r = d[i] var r = d[i]
var g = d[i+1] var g = d[i+1]
var b = d[i+2] var b = d[i+2]
var c, j, f, x, y var c, j, f, x, y
if(mode === 'luminance'){ if(mode === 'luminance'){
var v = Rl*r + Gl*g + Bl*b var v = Rl*r + Gl*g + Bl*b
c = count[j = pos(i, v)] = (count[j] || 0) + 1 c = count[j = pos(i, v)] = (count[j] || 0) + 1
od[j] = od[j+1] = od[j+2] = c * gain od[j] = od[j+1] = od[j+2] = c * gain
} else { } else {
if(mode === 'color' || mode === 'R'){ if(mode === 'color' || mode === 'R'){
f = Rl f = Rl
x = 1 x = 1
@ -246,7 +260,6 @@ module.Filters = {
j = pos(i, r) j = pos(i, r)
c = count[j] = (count[j] || 0) + 1 c = count[j] = (count[j] || 0) + 1
od[j] = c * gain } od[j] = c * gain }
if(mode === 'color' || mode === 'G'){ if(mode === 'color' || mode === 'G'){
f = Gl f = Gl
x = -1 x = -1
@ -254,7 +267,6 @@ module.Filters = {
j = pos(i, g) + 1 j = pos(i, g) + 1
c = count[j] = (count[j] || 0) + 1 c = count[j] = (count[j] || 0) + 1
od[j] = c * gain } od[j] = c * gain }
if(mode === 'color' || mode === 'B'){ if(mode === 'color' || mode === 'B'){
f = Bl f = Bl
x = -2 x = -2
@ -443,7 +455,8 @@ object.Constructor('igImageGraph', HTMLElement, {
(img.src = value) (img.src = value)
: img.src) }, : img.src) },
get mode(){ get mode(){
return this.getAttribute('mode') || 'color' }, return this.getAttribute('mode')
|| 'color' },
set mode(value){ set mode(value){
this.modes.includes(value) this.modes.includes(value)
&& this.setAttribute('mode', value) && this.setAttribute('mode', value)
@ -452,7 +465,8 @@ object.Constructor('igImageGraph', HTMLElement, {
this.update_controls() this.update_controls()
this.update() }, this.update() },
get color(){ get color(){
return this.getAttribute('color') || 'normalized' }, return this.getAttribute('color')
|| 'normalized' },
set color(value){ set color(value){
this.color_modes.includes(value) this.color_modes.includes(value)
&& this.setAttribute('color', value) && this.setAttribute('color', value)
@ -461,7 +475,8 @@ object.Constructor('igImageGraph', HTMLElement, {
this.update() }, this.update() },
get orientation(){ get orientation(){
return this.getAttribute('orientation') || 0 }, return this.getAttribute('orientation')
|| 0 },
set orientation(value){ set orientation(value){
;(['top', 'left', 'bottom', 'right'].includes(value) ;(['top', 'left', 'bottom', 'right'].includes(value)
|| typeof(value) == typeof(123)) || typeof(value) == typeof(123))
@ -566,7 +581,10 @@ object.Constructor('igImageGraph', HTMLElement, {
.map(function(m){ .map(function(m){
var button = document.createElement('button') var button = document.createElement('button')
button.innerText = m button.innerText = m
button.classList.add(m, ...(m == mode ? ['current'] : [])) button.classList.add(m,
...(m == mode ?
['current']
: []))
button.onclick = function(){ button.onclick = function(){
that.mode = m } that.mode = m }
return button }), return button }),