diff --git a/src/image.c b/src/image.c
--- a/src/image.c
+++ b/src/image.c
/*Header Definitions*/
#include "fits2jpeg.h"
-void scale_image(int scale, int npixels,
+/*---------------------------------------------------------------------------*
+ * SCALE_PIXELS: Changes the pixel scale to linear/log/sqroot/etc..
+ *---------------------------------------------------------------------------*/
+void scale_pixels(int scale, unsigned int npixels,
float *data, JSAMPLE *image_buffer)
{
unsigned int i = 0;
float scl_data = 0.0;
- /* first find min & max in data */
+ /* first find min & max in data */
datamax = -1.0 * FLT_MAX;
datamin = FLT_MAX;
for (i = 0; i < npixels; ++i)
if (data[i] < datamin) datamin = data[i];
} /*endfor*/
- /* Convert data into bytscaled values for jpeg file */
- /* the dynamic range is reduced to 255 for jpeg */
+
+ /* Convert data into bytscaled values for jpeg file */
+ /* the dynamic range is reduced to 255 for jpeg */
scl_data = (datamax - datamin)/(float)JMAXVAL;
+
for (i = 0; i < npixels; ++i)
data[i] = (data[i] - datamin)/scl_data;
- /* All data is now squeezed into the range 0 - 255 */
- /* NOTE: At this point onwards min & max is 0 and 255 respectively */
+
+ /* All data is now squeezed into the range 0 - 255 */
+ /* NOTE: At this point onwards min & max is 0 and 255 respectively */
datamax = (float)JMAXVAL;
datamin = 0.0;
- /* initialize image histogram. ensure all are zeroes in hist[] */
- /*-------------------------------------------------------------------*/
+ /* initialize image histogram. ensure all are zeroes in hist[] */
+ /*-----------------------------------------------------------------------*/
for (i = 0; i <= JMAXVAL; ++i) hist[i] = 0;
/* construct the image histogram */
cumhist[i] += cumhist[i - 1] + hist[i];
/* Linear scale (min-max) : This is the default scaling
- * if we dont generate image_buffer here, histo-eq will fail */
+ * histo-eq will fail if we dont generate image_buffer here */
for (i = 0; i < npixels; ++i)
image_buffer[i] = (int)(data[i]);
- /*-------------------------------------------------------------------*/
+ /*-----------------------------------------------------------------------*/
switch (scale)
else if (image_buffer[i] <= datamin)
image_buffer[i] = 0;
else
- image_buffer[i] = (int) abs((image_buffer[i] - datamin)/scl_data);
+ image_buffer[i] = (int) abs((image_buffer[i]
+ - datamin)/scl_data);
}
break;
/* histogram equalization */
printinfo("Performing Histogram Equalization");
for (i = 0; i < npixels; ++i)
- image_buffer[i] = cumhist[image_buffer[i]] * 255;
+ image_buffer[i] = cumhist[image_buffer[i]] * JMAXVAL;
break;
default :
printinfo("Using linear scale");
}
}
+
+/*---------------------------------------------------------------------------*
+ * RESIZE_IMAGE: Scales down/up the image_buffer
+ *---------------------------------------------------------------------------*/
+void resize_image(long *xdim, long *ydim, float zoomfact, JSAMPLE *image_buffer)
+{
+ int offset = 0, index = 0;
+ int A, B, C, D, x, y, gray;
+ JSAMPLE *buff;
+ unsigned int i = 0, j = 0;
+ unsigned long npixels = 0;
+ long w = *xdim, h = *ydim;
+ long zxdim = 0, zydim = 0;
+ float xdiff, ydiff, xratio, yratio;
+
+ zxdim = (int)(w * zoomfact);
+ zydim = (int)(h * zoomfact);
+
+ npixels= zxdim * zydim;
+
+ xratio = ((float)(w - 1))/zxdim;
+ yratio = ((float)(h - 1))/zydim;
+
+ /* allocate space for *buff */
+ buff = (unsigned char *) malloc(sizeof(char) * zxdim * zydim);
+
+ index = 0;
+ offset = 0;
+ for (i = 0; i < zydim; i++)
+ {
+ y = (int)(yratio * i);
+ ydiff = (yratio * i) - y;
+
+ for (j = 0; j < zxdim; j++)
+ {
+ x = (int)(xratio * j);
+
+ xdiff = (xratio * j) - x;
+ index = y * w + x;
+
+ A = image_buffer[index] & 0xff;
+ B = image_buffer[index + 1] & 0xff;
+ C = image_buffer[index + w] & 0xff;
+ D = image_buffer[index + w + 1] & 0xff;
+
+ gray = (int)(A * (1 - xdiff) * (1 - ydiff)
+ + B * (xdiff) * (1 - ydiff)
+ + C * (ydiff) * (1 - xdiff)
+ + D * (xdiff) * (ydiff)
+ );
+ buff[offset++] = gray;
+ }
+ }
+ *xdim = zxdim;
+ *ydim = zydim;
+ image_buffer = realloc(image_buffer, sizeof(char) * npixels);
+ if (!image_buffer)
+ printerro("Failed to allocate memory");
+
+ for (i = 0; i < npixels; ++i)
+ image_buffer[i] = buff[i];
+}