Skip to content

Commit

Permalink
remove tiff dep
Browse files Browse the repository at this point in the history
  • Loading branch information
bmatthieu3 committed Jul 29, 2024
1 parent 3c4ec3a commit d8ff8aa
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 155 deletions.
11 changes: 2 additions & 9 deletions examples/al-image-with-AVM-tags.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,10 @@
<script type="module">
import A from '../src/js/A.js';
A.init.then(() => {
let aladin = A.aladin('#aladin-lite-div', {fov: 30, survey: "CDS/P/PanSTARRS/DR1/color-z-zg-g", target: "280 +0", projection: "AIT", showShareControl:true, showSettingsControl: true, showContextMenu:true});
let aladin = A.aladin('#aladin-lite-div', {fov: 30, survey: "CDS/P/DSS2/color", target: "280 +0", projection: "AIT", showShareControl:true, showSettingsControl: true, showContextMenu:true});

aladin.setOverlayImageLayer(A.image(
//"https://www.virtualastronomy.org/images/sig05-013.jpg",
//"https://www.eso.org/public/archives/images/original/eso1137_hubble_dark.tif",
//"images/eso1137_hubble_dark.jpg",
//"https://nova.astrometry.net/new_fits_file/11015534",
//"https://chandra.harvard.edu/photo/2024/25th/25th_gcenter.jpg",
//"https://www.astropix.org/archive/spitzer/ssc2008-14b/spitzer_ssc2008-14b_original.jpg",
//"https://chandra.harvard.edu/photo/2024/25th/25th_crab.jpg",
"https://www.eso.org/public/archives/images/original/eso1023d.tif",
"https://www.virtualastronomy.org/images/sig05-013.jpg",
{
name: "sig05-017",
successCallback: (ra, dec, fov, image) => {
Expand Down
4 changes: 2 additions & 2 deletions examples/al-image-with-WCS.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
CUNIT1: 'deg', // X pixel scale units
CUNIT2: 'deg', // Y pixel scale units
CD1_1: -0.000223666022989, // Transformation matrix
CD1_2: 0.000296578064584, // no comment
CD1_2: -0.000296578064584, // no comment
CD2_1: -0.000296427555509, // no comment
CD2_2: -0.000223774308964, // no comment
CD2_2: 0.000223774308964, // no comment
NAXIS1: 1080, // Image width, in pixels.
NAXIS2: 705 // Image height, in pixels.
},
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,5 @@
"vite-plugin-wasm": "^3.2.2",
"vite-plugin-wasm-pack": "^0.1.12",
"vitest": "^0.32.2"
},
"dependencies": {
"tiff": "^6.1.0"
}
}
154 changes: 23 additions & 131 deletions src/js/Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { ColorCfg } from "./ColorCfg.js";
import { Aladin } from "./Aladin.js";
import { Utils } from "./Utils";
import { AVM } from "./libs/avm.js";
import * as tiff from "tiff";

/**
* @typedef {Object} ImageOptions
Expand All @@ -51,7 +50,7 @@ import * as tiff from "tiff";
* @property {number} [contrast=0.0] - The contrast value for the color configuration.
* @property {Object} [wcs] - an object describing the WCS of the image. In case of a fits image
* this property will be ignored as the WCS taken will be the one present in the fits file.
* @property {string} [imgFormat] - Optional image format. Giving it will prevent the auto extension determination algorithm to be triggered. Possible values are 'jpeg', 'png' or 'fits'.
* @property {string} [imgFormat] - Optional image format. Giving it will prevent the auto extension determination algorithm to be triggered. Possible values are 'jpeg', 'png' or 'fits'. tiff files are not supported. You can convert your tiff files to jpg ones by using the fantastic image magick suite.
*
* @example
*
Expand All @@ -60,24 +59,24 @@ import * as tiff from "tiff";
* {
* name: "M61",
* wcs: {
* NAXIS: 0, // Minimal header
* CTYPE1: 'RA---TAN', // TAN (gnomic) projection + SIP distortions
* CTYPE2: 'DEC--TAN', // TAN (gnomic) projection + SIP distortions
* EQUINOX: 2000.0, // Equatorial coordinates definition (yr)
* LONPOLE: 180.0, // no comment
* LATPOLE: 0.0, // no comment
* CRVAL1: 185.445488837, // RA of reference point
* CRVAL2: 4.47896032431, // DEC of reference point
* CRPIX1: 588.995094299, // X reference pixel
* CRPIX2: 308.307905197, // Y reference pixel
* CUNIT1: 'deg', // X pixel scale units
* CUNIT2: 'deg', // Y pixel scale units
* CD1_1: -0.000223666022989, // Transformation matrix
* CD1_2: 0.000296578064584, // no comment
* CD2_1: -0.000296427555509, // no comment
* CD2_2: -0.000223774308964, // no comment
* NAXIS1: 1080, // Image width, in pixels.
* NAXIS2: 705 // Image height, in pixels.
NAXIS: 0, // Minimal header
CTYPE1: 'RA---TAN', // TAN (gnomic) projection
CTYPE2: 'DEC--TAN', // TAN (gnomic) projection
EQUINOX: 2000.0, // Equatorial coordinates definition (yr)
LONPOLE: 180.0, // no comment
LATPOLE: 0.0, // no comment
CRVAL1: 185.445488837, // RA of reference point
CRVAL2: 4.47896032431, // DEC of reference point
CRPIX1: 588.995094299, // X reference pixel
CRPIX2: 308.307905197, // Y reference pixel
CUNIT1: 'deg', // X pixel scale units
CUNIT2: 'deg', // Y pixel scale units
CD1_1: -0.000223666022989, // Transformation matrix
CD1_2: -0.000296578064584, // no comment
CD2_1: -0.000296427555509, // no comment
CD2_2: 0.000223774308964, // no comment
NAXIS1: 1080, // Image width, in pixels.
NAXIS2: 705 // Image height, in pixels.
* },
* successCallback: (ra, dec, fov, image) => {
* aladin.gotoRaDec(ra, dec);
Expand Down Expand Up @@ -254,14 +253,7 @@ export let Image = (function () {
imgFormat: 'fits',
},
layer
).catch(e => {
//console.log(e)
console.log('jj')
const reader = stream.getReader();
reader.releaseLock();
console.log("jjjjj")
return Promise.reject(e);
});
)
},
});
}
Expand All @@ -273,96 +265,6 @@ export let Image = (function () {
})
}

Image.prototype._addTiff = function(layer) {
let self = this;
const url = Aladin.JSONP_PROXY + '?url=' + self.url;
console.log("parse through proxy", url)
console.log("test tiff")

return Utils.fetch({
url,
dataType: "blob",
cors: "Anonymous"
})
.then((blob) => {
return blob.arrayBuffer()
})
.then((arrayBuffer) => {
return new Promise((resolve, reject) => {
var ifds = tiff.decode(arrayBuffer);
//var rgba = UTIF.toRGBA8(ifds[0]); // Uint8Array with RGBA pixels
console.log(ifds[0].width, ifds[0].height, ifds[0]);

let try2FindAVM = [];
for(var v of ifds[0].fields.values()) {
try2FindAVM.push(new Promise((resolve, reject) => {
let string = new TextDecoder().decode(v)
console.log(string, v)
let avm = new AVM(v);
avm.load((obj) => {
// obj contains the following information:
// obj.id (string) = The ID provided for the image
// obj.img (object) = The image object
// obj.xmp (string) = The raw XMP header
// obj.wcsdata (Boolean) = If WCS have been loaded
// obj.tags (object) = An array containing all the loaded tags e.g. obj.tags['Headline']
// obj.wcs (object) = The wcs parsed from the image
console.log(obj)
if (obj.wcsdata) {
if (img.width !== obj.wcs.NAXIS1) {
obj.wcs.NAXIS1 = img.width;
}

if (img.height !== obj.wcs.NAXIS2) {
obj.wcs.NAXIS2 = img.height;
}

self.options.wcs = obj.wcs;

const blob = new Blob([ifds[0]]);
const stream = blob.stream(1024);
resolve(stream)
} else {
// no tags found
reject('No WCS have been found in the image')
}
})
}))
}

return Promise.any(try2FindAVM);
})
})
.then((readableStream) => {
console.log("blob", readableStream)
let wcs = self.options && self.options.wcs;
wcs.NAXIS1 = wcs.NAXIS1 || img.width;
wcs.NAXIS2 = wcs.NAXIS2 || img.height;

return self.view.wasm
.addImageWithWCS(
readableStream,
wcs,
{
...self.colorCfg.get(),
longitudeReversed: false,
imgFormat: 'jpeg',
},
layer
)
})
.then((imageParams) => {
self.imgFormat = 'tiff';
return Promise.resolve(imageParams);
})
.catch(e => {
console.error(e)
console.log("aaa", e)

return Promise.reject(e)
})
}

Image.prototype._addJPGOrPNG = function(layer) {
let self = this;
let img = document.createElement('img');
Expand Down Expand Up @@ -482,24 +384,14 @@ export let Image = (function () {
console.error(`Image located at ${this.url} could not be parsed as a ${this.imgFormat} file. Is the imgFormat specified correct?`);
return Promise.reject(e)
})
} else if (this.imgFormat === 'tiff') {
promise = this._addTiff(layer)
.catch(e => {
console.error(`Image located at ${this.url} could not be parsed as a ${this.imgFormat} file. Is the imgFormat specified correct?`);
return Promise.reject(e)
})
} else {
// imgformat not defined we will try first supposing it is a fits file and then use the jpg heuristic
promise = this._addTiff(layer)
promise = self._addFITS(layer)
.catch(e => {
console.warn(`Image located at ${self.url} could not be parsed as fits file. Try as a jpg/png image...:`, e)
return self._addJPGOrPNG(layer)
.catch(e => {
return self._addFITS(layer)
.catch(e => {
console.error(`Image located at ${self.url} could not be parsed as jpg/png/tif image file. Aborting...`)
return Promise.reject(e);
})
console.error(`Image located at ${self.url} could not be parsed as jpg/png/tif image file. Aborting...`)
return Promise.reject(e);
})
})
}
Expand Down
12 changes: 2 additions & 10 deletions src/js/libs/avm.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ export let AVM = (function() {
if(!this.img && this.id) {
this.img = document.getElementById(this.id);
}
console.log("jksjdk2f")

if (this.img instanceof ArrayBuffer) {
this.getData(fnCallback);
Expand All @@ -105,8 +104,6 @@ export let AVM = (function() {
}

AVM.prototype.getData = function(fnCallback){
console.log("jksjdk2f")

if(!this.imageHasData()){
this.getImageData(this.img, fnCallback);
}else{
Expand All @@ -128,7 +125,6 @@ export let AVM = (function() {
if (typeof fnCallback=="function") fnCallback(_obj);
};
if (oImg instanceof ArrayBuffer) {
console.log("jksjdkf")
findAVM(oImg)
} else {
let reqwst = new Request(oImg.src, {
Expand Down Expand Up @@ -188,7 +184,6 @@ export let AVM = (function() {

if (tags) {
this.tags = tags;
console.log(tags)

let unwindTag = (tag) => {
if (Array.isArray(tag)) {
Expand All @@ -210,7 +205,7 @@ export let AVM = (function() {
wcs.NAXIS2 = tags['Spatial.ReferenceDimension'] && +tags['Spatial.ReferenceDimension'][1];

if (tags['Spatial.CDMatrix']) {
console.warn("Spatial.CDMatrix is deprecated");
console.warn("Spatial.CDMatrix is deprecated in favor of Spatial.Scale + Spatial.Rotation");

wcs.CD1_1 = +tags['Spatial.CDMatrix'][0];
wcs.CD1_2 = +tags['Spatial.CDMatrix'][1];
Expand Down Expand Up @@ -257,16 +252,13 @@ export let AVM = (function() {
}
}
}

console.log(wcs)
}
}
return wcs;
}

AVM.prototype.readXMP = function(oFile) {
var iEntries = oFile.byteLength;
var bBigEnd = false;
var prev_n_hex = '';
var record = false;
var recordn = 0;
Expand Down Expand Up @@ -334,7 +326,7 @@ export let AVM = (function() {
final = str.indexOf('</'+key+'>',start);
// Parse out the HTML tags and build an array of the resulting values
var tmps = str.substring(start-1,final);
console.log(tmps)

var tmparr = new Array(0);
var tmpstr = tmps.replace(/[\n\r]/g,"");
tmpstr = tmpstr.replace(/ +/g," ");
Expand Down

0 comments on commit d8ff8aa

Please sign in to comment.