Skip to content

Commit ca90481

Browse files
committed
Bug fix: compress images not are too different from full
- compute standard error as a test for compression
1 parent 2d4b1c7 commit ca90481

File tree

9 files changed

+74
-14
lines changed

9 files changed

+74
-14
lines changed

src/firefly/java/edu/caltech/ipac/firefly/server/visualize/DirectStretchUtils.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ static private byte[] makeDecimated(byte[] in, int factor, int width, int height
264264
return out;
265265
}
266266

267-
static byte averageCells(byte[] in,int width,int rowIdx, int colIdx, int factor) {
267+
static byte averageCellsORIGINAL(byte[] in,int width,int rowIdx, int colIdx, int factor) {
268268
int sum= 0;
269269
int cnt= 0;
270270
int inIdx;
@@ -280,6 +280,41 @@ static byte averageCells(byte[] in,int width,int rowIdx, int colIdx, int factor)
280280
return (byte) (sum/cnt);
281281
}
282282

283+
static byte averageCells(byte[] in,int width,int rowIdx, int colIdx, int factor) {
284+
int sum= 0;
285+
int cnt= 0;
286+
int inIdx;
287+
int factHalf= factor/2;
288+
int startColIdx, endColIdx;
289+
int startRowIdx, endRowIdx;
290+
if (colIdx-factHalf<0) {
291+
startColIdx= colIdx;
292+
endColIdx= colIdx+factor;
293+
}
294+
else {
295+
startColIdx= colIdx-factHalf;
296+
endColIdx= colIdx+factHalf;
297+
}
298+
if (rowIdx-factHalf<0) {
299+
startRowIdx= rowIdx;
300+
endRowIdx= rowIdx+factor;
301+
}
302+
else {
303+
startRowIdx= rowIdx-factHalf;
304+
endRowIdx= rowIdx+factHalf;
305+
}
306+
for(int j=startColIdx; (j<endColIdx); j++) {
307+
for(int i=startRowIdx; (i<endRowIdx); i++) {
308+
inIdx= j * width + i;
309+
if (inIdx>=0 && inIdx<in.length) {
310+
sum+= Byte.toUnsignedInt(in[inIdx]);
311+
cnt++;
312+
}
313+
}
314+
}
315+
return (byte) (sum/cnt);
316+
}
317+
283318
private interface SetupStretchTask<T> { Callable<Void> makeTask(StretchTileDef stdef, T stretchContainer); }
284319
private record ThreeCComponents(float[][] float1dAry, ImageHeader[] imHeadAry, Histogram[] histAry) {}
285320
private record StretchVars(int totWidth, int totHeight, int xPanels, int yPanels, int tileLen, CompressType ct) {}

src/firefly/java/edu/caltech/ipac/firefly/server/visualize/ImagePlotCreator.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,15 @@ private static WebFitsData makeWebFitsData(ActiveFitsReadGroup frGroup, Band ban
284284
FitsRead fr= frGroup.getFitsReadAry()[band.getIdx()];
285285
if (fr==null) return null;
286286
double dataMin= Double.NaN;
287-
double dataMmax= Double.NaN;
287+
double dataMax= Double.NaN;
288+
double standardErr= 0;
288289
if (!fr.isDeferredRead()) {
289290
Histogram hist= fr.getHistogram();
290291
dataMin= hist.getDNMin() * fr.getBscale() + fr.getBzero();
291-
dataMmax= hist.getDNMax() * fr.getBscale() + fr.getBzero();
292+
dataMax= hist.getDNMax() * fr.getBscale() + fr.getBzero();
293+
standardErr= hist.getStandardErr();
292294
}
293-
return new WebFitsData( dataMin, dataMmax, fileLength, fr.getFluxUnits());
295+
return new WebFitsData( dataMin, dataMax, standardErr, fileLength, fr.getFluxUnits());
294296
}
295297

296298
public record PlotInfo(PlotState state, ImagePlot plot, FileInfo fileInfo,

src/firefly/java/edu/caltech/ipac/firefly/server/visualize/VisJsonSerializer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ private static JSONObject serializeWebFitsData(WebFitsData wfData) {
292292
JSONObject map = new JSONObject();
293293
putDoubleNot0(map, "dataMin", wfData.dataMin());
294294
putDoubleNot0(map,"dataMax", wfData.dataMax());
295+
putDoubleNot0(map,"standardErr", wfData.standardErr());
295296
putStr(map, "fluxUnits", wfData.fluxUnits());
296297
putNum(map, "getFitsFileSize", wfData.fitsFileSize());
297298
return map;

src/firefly/java/edu/caltech/ipac/firefly/visualize/WebFitsData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
/**
77
* @author Trey Roby
88
*/
9-
public record WebFitsData(double dataMin, double dataMax, long fitsFileSize, String fluxUnits) { }
9+
public record WebFitsData(double dataMin, double dataMax, double standardErr, long fitsFileSize, String fluxUnits) { }
1010

src/firefly/java/edu/caltech/ipac/visualize/plot/Histogram.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class Histogram implements HasSizeOf {
2929
private double histBinsize;
3030
private final double irafMin;
3131
private final double irafMax;
32+
private final double standardErr;
3233

3334

3435

@@ -151,6 +152,7 @@ else if (i>HISTSIZ2)
151152

152153
irafMin = datamin;
153154
irafMax = datamax;
155+
standardErr= get_sigma(1,true)/Math.sqrt(float1dArray.length);
154156
}
155157

156158

@@ -389,4 +391,6 @@ public double[] getTblArray() {
389391
public long getSizeOf() {
390392
return hist.length*4L + 32L;
391393
}
394+
395+
public double getStandardErr() { return standardErr; }
392396
}

src/firefly/js/visualize/WebPlot.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const HIPS_DATA_HEIGHT= 10000000000;
7373
* @prop {number} zoomFactor - the zoom factor
7474
* @prop {boolean} blank - true if the is a blank plot, default to false
7575
* @prop {string} title - title of the plot
76-
* @prop {object} webFitsData - needs documentation
76+
* @prop {WebFitsData} webFitsData - needs documentation
7777
* @prop {ImageTileData} tileData - object contains the image tile information
7878
* @prop {CoordinateSys} imageCoordSys - the image coordinate system
7979
* @prop {Dimension} screenSize - width/height in screen pixels
@@ -91,6 +91,17 @@ const HIPS_DATA_HEIGHT= 10000000000;
9191
* @see PlotView
9292
*/
9393

94+
/**
95+
* @global
96+
* @public
97+
* @typedef {Object} WebFitsData
98+
*
99+
* @prop {number} dataMin
100+
* @prop {number} dataMax
101+
* @prop {number} standardErr,
102+
* @prop {number} fitsFileSize
103+
* @prop {number} fluxUnits
104+
*/
94105

95106

96107
/**

src/firefly/js/visualize/rawData/RawDataOps.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ function getFirstDataCompress(plot, mask) {
220220
if (mask) return 'FULL';
221221
const {dataWidth, dataHeight}= plot;
222222
const size= dataWidth*dataHeight;
223-
if (!isThreeColor(plot) && plot.webFitsData[Band.NO_BAND.value].dataMin<0) { //these type of image don't seem to compress very well
224-
return (size < 100*MEG) ? 'FULL' : 'HALF';
223+
if (!isThreeColor(plot) && plot.webFitsData[Band.NO_BAND.value].standardErr<.01) { //these type of image don't seem to compress very well
224+
return (size < 55*MEG) ? 'FULL' : 'QUARTER';
225225
}
226226
if (size < 6*MEG) return 'FULL';
227227
if (size < 10*MEG) return 'HALF';
@@ -241,6 +241,9 @@ function getNextDataCompress(firstCompress, plot) {
241241
const size= dataWidth*dataHeight;
242242
if (firstCompress!=='QUARTER' && size < MAX_FULL_DATA_SIZE) return 'FULL';
243243
if (size > MAX_FULL_DATA_SIZE) return 'HALF';
244+
if (!isThreeColor(plot) && plot.webFitsData[Band.NO_BAND.value].standardErr<.01) { //these type of image don't seem to compress very well
245+
return 'FULL';
246+
}
244247
if (size > 70*MEG && zoomFactor<.4) return 'HALF';
245248
return 'FULL';
246249
}
@@ -292,7 +295,7 @@ export async function loadStretchData(pv, plot, dispatcher) {
292295

293296
if (plotInvalid()) return;
294297
let currPlot= primePlot(visRoot(),plotId);
295-
const waitTime= currPlot.zoomFactor<.3 ? 4000 : 100; // wait 4 sec if small zoom level is otherwise 1 sec
298+
const waitTime= currPlot.zoomFactor<.3 ? 4000 : 1000; // wait 4 sec if small zoom level is otherwise 1 sec
296299
const nextDataCompress= getNextDataCompress(dataCompress,currPlot);
297300
const secondSuccess= await requestAgain(reqId, plotId, currPlot, waitTime, nextDataCompress, workerKey, dispatcher);
298301
if (secondSuccess && nextDataCompress==='HALF' && dataSize < MAX_FULL_DATA_SIZE) {

src/firefly/js/visualize/ui/ColorBandPanel.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ function suggestedValuesPanel( plot,band ) {
148148
const dataMaxStr = `Data Max: ${sprintf('%.6f',dataMax)} `;
149149
const dataMinStr = `Data Min: ${sprintf('%.6f', dataMin)}`;
150150

151+
if (!dataMin && !dataMax) return <div style={style}/>
152+
151153
return (
152154
<div style={style}>
153155
<span style={{float:'left', paddingRight:2, opacity:.5, marginLeft:40 }}>

src/firefly/js/visualize/ui/ColorPanelReducer.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,9 @@ function computeLowerRangeField(fields,rv,fitsData,lowerWhich='lowerWhich', lowe
122122
break;
123123
case ABSOLUTE:
124124
retval= {
125-
validator: !isNaN(fitsData.dataMin) && !isNaN(fitsData.dataMax) ?
126-
Validate.floatRange.bind(null,fitsData.dataMin, fitsData.dataMax, 3, 'Lower range') : undefined,
125+
validator: fitsData.dataMin && fitsData.dataMax ?
126+
Validate.floatRange.bind(null,fitsData.dataMin, fitsData.dataMax, 3, 'Lower range') :
127+
() => ({valid:true, message:''}),
127128
value : resetDefault ? fitsData.dataMin : fields[lowerRange].value
128129
};
129130
break;
@@ -158,9 +159,10 @@ function computeUpperRangeField(fields,rv,fitsData) {
158159

159160
case ABSOLUTE:
160161
retval= {
161-
validator: !isNaN(fitsData.dataMin) && !isNaN(fitsData.dataMax) ?
162-
Validate.floatRange.bind(null,fitsData.dataMin, fitsData.dataMax, 3, 'Upper range') : undefined,
163-
value : resetDefault ? fitsData.dataMax : fields.upperRange.value
162+
validator: fitsData.dataMin && fitsData.dataMax ?
163+
Validate.floatRange.bind(null,fitsData.dataMin, fitsData.dataMax, 3, 'Upper range') :
164+
() => ({valid:true, message:''}),
165+
value : resetDefault ? fitsData.dataMax : fields.upperRange.value
164166
};
165167
break;
166168
case SIGMA:

0 commit comments

Comments
 (0)