|
|
@ -1,64 +1,78 @@
|
|
|
|
import {findTagsInObjectURL} from './exif_helper';
|
|
|
|
import {findTagsInObjectURL} from './exif_helper';
|
|
|
|
import {generateSourceInterface} from './SourceInterface';
|
|
|
|
import {Source} from './SourceInterface';
|
|
|
|
|
|
|
|
|
|
|
|
export function fromImage(input, constraints = {width: 800, height: 800, channels: 3}) {
|
|
|
|
class ImageSource extends Source {
|
|
|
|
var $image = null;
|
|
|
|
constructor() {
|
|
|
|
var src = null;
|
|
|
|
super("IMAGE");
|
|
|
|
|
|
|
|
this._$image = null;
|
|
|
|
|
|
|
|
this._src = null;
|
|
|
|
|
|
|
|
this.tags = null;
|
|
|
|
|
|
|
|
this.colorChannels = 3;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
applyConstraints(newConstraints) {
|
|
|
|
|
|
|
|
this.constraints = newConstraints;
|
|
|
|
|
|
|
|
this.colorChannels = this.constraints.channels || this.colorChannels;
|
|
|
|
|
|
|
|
return this._applyInput(this.constraints.src)
|
|
|
|
|
|
|
|
._loadResource()
|
|
|
|
|
|
|
|
.then(() => findTagsInObjectURL(this._src, ['orientation']))
|
|
|
|
|
|
|
|
.then((tags) => {this.tags = tags;})
|
|
|
|
|
|
|
|
.then(this._determineDimensions.bind(this))
|
|
|
|
|
|
|
|
.then(() => this);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_loadResource() {
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
|
|
if (this._src || !this._$image.complete) {
|
|
|
|
|
|
|
|
this._$image.addEventListener('load', resolve, false);
|
|
|
|
|
|
|
|
this._$image.addEventListener('error', reject, false);
|
|
|
|
|
|
|
|
if (this._src) {
|
|
|
|
|
|
|
|
console.log(`Setting src = ${this._src}`);
|
|
|
|
|
|
|
|
this._$image.src = this._src;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return resolve();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_applyInput(input) {
|
|
|
|
if (typeof input === 'string') {
|
|
|
|
if (typeof input === 'string') {
|
|
|
|
// data or url, or queryString
|
|
|
|
// data or url, or queryString
|
|
|
|
$image = new Image();
|
|
|
|
this._$image = new Image();
|
|
|
|
src = input;
|
|
|
|
this._src = input;
|
|
|
|
} else if (input instanceof HTMLImageElement) {
|
|
|
|
} else if (input instanceof HTMLImageElement) {
|
|
|
|
$image = input;
|
|
|
|
this._$image = input;
|
|
|
|
} else if (input instanceof File) {
|
|
|
|
} else if (input instanceof File) {
|
|
|
|
$image = new Image();
|
|
|
|
this._$image = new Image();
|
|
|
|
src = URL.createObjectURL(input);
|
|
|
|
this._src = URL.createObjectURL(input);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
return Promise.reject("fromImage needs a src, HTMLImageElement or File");
|
|
|
|
throw new Error("fromImage needs a src, HTMLImageElement or File");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
return this;
|
|
|
|
if (src || !$image.complete) {
|
|
|
|
|
|
|
|
console.log('Adding eventlistener');
|
|
|
|
|
|
|
|
$image.addEventListener('load', function() {
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
$image.addEventListener('error', function(e) {
|
|
|
|
|
|
|
|
reject(e);
|
|
|
|
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
if (src) {
|
|
|
|
|
|
|
|
console.log(`Setting src = ${src}`);
|
|
|
|
|
|
|
|
$image.src = src;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return resolve();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
.then(() => findTagsInObjectURL(src, ['orientation']))
|
|
|
|
_determineDimensions() {
|
|
|
|
.then((tags) => {
|
|
|
|
let width = this._$image.naturalWidth;
|
|
|
|
let width = $image.naturalWidth;
|
|
|
|
let height = this._$image.naturalHeight;
|
|
|
|
let height = $image.naturalHeight;
|
|
|
|
let desiredWidth = this.constraints.width;
|
|
|
|
if (tags && tags.orientation) {
|
|
|
|
if (this.tags && this.tags.orientation) {
|
|
|
|
switch (tags.orientation) {
|
|
|
|
switch (this.tags.orientation) {
|
|
|
|
case 6:
|
|
|
|
case 6:
|
|
|
|
case 8:
|
|
|
|
case 8:
|
|
|
|
width = $image.naturalHeight;
|
|
|
|
width = this._$image.naturalHeight;
|
|
|
|
height = $image.naturalWidth;
|
|
|
|
height = this._$image.naturalWidth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const imageAR = width / height;
|
|
|
|
const imageAR = width / height;
|
|
|
|
const calculatedWidth = imageAR > 1 ? constraints.width : Math.floor((imageAR) * constraints.width);
|
|
|
|
const calculatedWidth = imageAR > 1 ? desiredWidth : Math.floor((imageAR) * desiredWidth);
|
|
|
|
const calculatedHeight = imageAR > 1 ? Math.floor((1 / imageAR) * constraints.width) : constraints.width;
|
|
|
|
const calculatedHeight = imageAR > 1 ? Math.floor((1 / imageAR) * desiredWidth) : desiredWidth;
|
|
|
|
const colorChannels = constraints.channels || 3;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Object.assign(generateSourceInterface(), {
|
|
|
|
this._dimensions = {
|
|
|
|
type: "IMAGE",
|
|
|
|
|
|
|
|
colorChannels,
|
|
|
|
|
|
|
|
tags,
|
|
|
|
|
|
|
|
getDimensions() {
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
viewport: {
|
|
|
|
viewport: {
|
|
|
|
width: $image.naturalWidth, // AR
|
|
|
|
width, // AR
|
|
|
|
height: $image.naturalHeight, // AR
|
|
|
|
height, // AR
|
|
|
|
x: 0, // AR
|
|
|
|
x: 0, // AR
|
|
|
|
y: 0, // AR
|
|
|
|
y: 0, // AR
|
|
|
|
},
|
|
|
|
},
|
|
|
@ -67,16 +81,22 @@ export function fromImage(input, constraints = {width: 800, height: 800, channel
|
|
|
|
height: calculatedHeight, // AR
|
|
|
|
height: calculatedHeight, // AR
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
},
|
|
|
|
}
|
|
|
|
getDrawable: function() {
|
|
|
|
|
|
|
|
return $image;
|
|
|
|
getDimensions() {
|
|
|
|
},
|
|
|
|
return this._dimensions;
|
|
|
|
getLabel: function() {
|
|
|
|
}
|
|
|
|
return $image.src;
|
|
|
|
|
|
|
|
},
|
|
|
|
getDrawable() {
|
|
|
|
getConstraints: function() {
|
|
|
|
return this._$image;
|
|
|
|
return constraints;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
});
|
|
|
|
getLabel() {
|
|
|
|
});
|
|
|
|
return this._$image.src;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function fromImage(constraints = {width: 800, height: 800, channels: 3}) {
|
|
|
|
|
|
|
|
const imageSource = new ImageSource();
|
|
|
|
|
|
|
|
return imageSource.applyConstraints(constraints);
|
|
|
|
}
|
|
|
|
}
|
|
|
|