Add RawImage class that handle images as byte array

This commit is contained in:
2019-09-24 18:32:38 +02:00
parent 9c0c0d41c7
commit fa025dff0e
3 changed files with 292 additions and 43 deletions
+2 -1
View File
@@ -28,4 +28,5 @@ HEADERS += mainwindow.h \
imageringlist.h \
database.h \
loadrunable.h \
imageinfo.h
imageinfo.h \
rawimage.h
+108 -42
View File
@@ -5,6 +5,7 @@
#include <QFileInfo>
#include <libexif/exif-data.h>
#include <fitsio2.h>
#include "rawimage.h"
LoadRunable::LoadRunable(const QString &file, Image *receiver) :
m_file(file),
@@ -23,14 +24,86 @@ void loadExifEntry(ImageInfoData &info, ExifContent *content, ExifTag tag)
}
}
QImage loadFITS(QString path, ImageInfoData &info)
bool loadRAW(QString path, ImageInfoData &info, RawImageAbs **image, QImage *qimage)
{
QImage img;
if(!image && !qimage)
return false;
LibRaw raw;
raw.open_file(path.toLocal8Bit().data());
raw.imgdata.params.half_size = true;
raw.imgdata.params.use_camera_wb = true;
raw.imgdata.params.user_flip = 0;
if(raw.unpack())
return false;
libraw_rawdata_t rawdata = raw.imgdata.rawdata;
size_t size = rawdata.sizes.width*rawdata.sizes.height;
std::vector<uint16_t> out;
out.resize(size);
size_t d = 0;
uint h=rawdata.sizes.top_margin+rawdata.sizes.height;
uint w=rawdata.sizes.left_margin+rawdata.sizes.width;
size_t pitch = rawdata.sizes.raw_pitch/sizeof(uint16_t);
for(size_t i=rawdata.sizes.top_margin;i<h;i+=2)
{
for(size_t o=rawdata.sizes.left_margin;o<w;o+=2)
{
uint16_t p = rawdata.raw_image[i*pitch+o];
out[d++] = p;
}
}
if(image)
{
*image = new RawImage<uint16_t>(w, h, out);
}
if(qimage)
{
raw.dcraw_process();
libraw_processed_image_t *rawImg = raw.dcraw_make_mem_image();
QImage img(rawImg->width, rawImg->height, QImage::Format_RGB888);
uint scanLine = rawImg->width*rawImg->colors;
for(uint i=0; i<rawImg->height; i++)
{
memcpy(img.scanLine(i), rawImg->data+(i*scanLine), scanLine);
}
QString shutterSpeed = QString::number(raw.imgdata.other.shutter);
if(raw.imgdata.other.shutter < 1)
{
shutterSpeed = QString("1/%1s").arg(1.0f/raw.imgdata.other.shutter);
}
info.append(StringPair(QObject::tr("Width"), QString::number(rawImg->width)));
info.append(StringPair(QObject::tr("Height"), QString::number(rawImg->height)));
info.append(StringPair(QObject::tr("ISO"), QString::number(raw.imgdata.other.iso_speed)));
info.append(StringPair(QObject::tr("Shutter speed"), shutterSpeed));
#if LIBRAW_MINOR_VERSION>=19
info.append(StringPair(QObject::tr("Camera temperature"), QString::number(raw.imgdata.other.CameraTemperature)));
#endif
raw.dcraw_clear_mem(rawImg);
*qimage = img;
}
return true;
}
bool loadFITS(QString path, ImageInfoData &info, RawImageAbs **image, QImage *qimage)
{
if(!image && !qimage)
return false;
fitsfile *file;
int status = 0;
int type;
fits_open_image(&file, path.toLocal8Bit().data(), READONLY, &status);
fits_get_hdu_type(file, &type, &status);
if(type == IMAGE_HDU)
{
int imgtype;
@@ -38,7 +111,7 @@ QImage loadFITS(QString path, ImageInfoData &info)
long naxes[2];
fits_get_img_param(file, 2, &imgtype, &naxis, naxes, &status);
if(naxis == 2)
if(naxis == 2 && status == 0)
{
std::vector<uint8_t> bits8;
std::vector<uint16_t> bits16;
@@ -46,8 +119,12 @@ QImage loadFITS(QString path, ImageInfoData &info)
long fpixel[2] = {1,1};
size_t size = naxes[0]*naxes[1];
img = QImage(naxes[0], naxes[1], QImage::Format_Grayscale8);
uchar *data = img.bits();
uchar *data = nullptr;
if(qimage)
{
*qimage = QImage(naxes[0], naxes[1], QImage::Format_Grayscale8);
data = qimage->bits();
}
info.append(StringPair(QObject::tr("Width"), QString::number(naxes[0])));
info.append(StringPair(QObject::tr("Height"), QString::number(naxes[1])));
@@ -57,29 +134,45 @@ QImage loadFITS(QString path, ImageInfoData &info)
bits8.resize(size);
fits_read_pix(file, TBYTE, fpixel, size, NULL, &bits8[0], NULL, &status);
if(status)break;
for(size_t i=0; i<size; i++)data[i] = bits8[i];
if(data)
{
for(size_t i=0; i<size; i++)
data[i] = bits8[i];
}
if(image)*image = new RawImage<uint8_t>(naxes[0], naxes[1], bits8);
break;
case SHORT_IMG:
bits16.resize(size);
fits_read_pix(file, TUSHORT, fpixel, size, NULL, &bits16[0], NULL, &status);
if(status)break;
for(size_t i=0; i<size; i++)data[i] = bits16[i] >> 8;
if(data)
{
for(size_t i=0; i<size; i++)
data[i] = bits16[i] >> 8;
}
if(image)*image = new RawImage<uint16_t>(naxes[0], naxes[1], bits16);
break;
case LONG_IMG:
bits32.resize(size);
fits_read_pix(file, TUINT, fpixel, size, NULL, &bits32[0], NULL, &status);
if(status)break;
for(size_t i=0; i<size; i++)data[i] = bits32[i] >> 24;
if(data)
{
for(size_t i=0; i<size; i++)
data[i] = bits32[i] >> 24;
}
if(image)*image = new RawImage<uint32_t>(naxes[0], naxes[1], bits32);
break;
}
}
}
int nheader, more;
int nheader = 0, more;
fits_get_hdrspace(file, &nheader, &more, &status);
for(int i=1; i<=nheader; i++)
{
char key[FLEN_KEYWORD];
char val[FLEN_VALUE];
char comm[FLEN_COMMENT];
fits_read_keyn(file, i, key, val, comm, &status);
@@ -94,7 +187,7 @@ QImage loadFITS(QString path, ImageInfoData &info)
info.append(StringPair(QObject::tr("Error"), QString(err)));
}
return img;
return true;
}
void LoadRunable::run()
@@ -109,42 +202,15 @@ void LoadRunable::run()
if(m_file.endsWith(".CR2", Qt::CaseInsensitive))
{
LibRaw raw;
raw.open_file(m_file.toLocal8Bit().data());
raw.imgdata.params.half_size = true;
raw.imgdata.params.use_camera_wb = true;
raw.imgdata.params.user_flip = 0;
raw.unpack();
raw.dcraw_process();
libraw_processed_image_t *rawImg = raw.dcraw_make_mem_image();
QImage img(rawImg->width, rawImg->height, QImage::Format_RGB888);
QString shutterSpeed = QString::number(raw.imgdata.other.shutter);
if(raw.imgdata.other.shutter < 1)
{
shutterSpeed = QString("1/%1s").arg(1.0f/raw.imgdata.other.shutter);
}
uint scanLine = rawImg->width*rawImg->colors;
for(uint i=0; i<rawImg->height; i++)
{
memcpy(img.scanLine(i), rawImg->data+(i*scanLine), scanLine);
}
info.append(StringPair(QObject::tr("Width"), QString::number(rawImg->width)));
info.append(StringPair(QObject::tr("Height"), QString::number(rawImg->height)));
info.append(StringPair(QObject::tr("ISO"), QString::number(raw.imgdata.other.iso_speed)));
info.append(StringPair(QObject::tr("Shutter speed"), shutterSpeed));
#if LIBRAW_MINOR_VERSION>=19
info.append(StringPair(QObject::tr("Camera temperature"), QString::number(raw.imgdata.other.CameraTemperature)));
#endif
raw.dcraw_clear_mem(rawImg);
QImage img;
loadRAW(m_file, info, nullptr, &img);
QMetaObject::invokeMethod(m_receiver, "imageLoaded", Qt::QueuedConnection, Q_ARG(QImage, img), Q_ARG(ImageInfoData, info));
}
else if(m_file.endsWith(".FIT", Qt::CaseInsensitive))
{
QImage img = loadFITS(m_file, info);
QMetaObject::invokeMethod(m_receiver, "imageLoaded", Qt::QueuedConnection, Q_ARG(QImage, img), Q_ARG(ImageInfoData, info));
QImage img;
if(loadFITS(m_file, info, nullptr, &img))
QMetaObject::invokeMethod(m_receiver, "imageLoaded", Qt::QueuedConnection, Q_ARG(QImage, img), Q_ARG(ImageInfoData, info));
}
else
{
+182
View File
@@ -0,0 +1,182 @@
#ifndef RAWIMAGE_H
#define RAWIMAGE_H
#include <vector>
#include <algorithm>
#include <stdint.h>
#include <math.h>
#include <memory.h>
class RawImageAbs
{
};
template<typename T>
class RawImage : public RawImageAbs
{
uint32_t m_width,m_height;
uint32_t m_width1,m_height1;
std::vector<T> m_img;
bool checkPixel(T c, uint32_t x, uint32_t y) const
{
T d = pixel(x, y);
return c>=d;
}
public:
class Peak
{
T m_v;
uint32_t m_x,m_y;
public:
Peak() : m_v(0), m_x(0), m_y(0) {}
Peak(T v, uint32_t x, uint32_t y) : m_v(v), m_x(x), m_y(y) {}
T v() const { return m_v; }
uint32_t x() const { return m_x; }
uint32_t y() const { return m_y; }
double distance(const Peak &d) const
{
uint32_t dx = m_x-d.m_x;
uint32_t dy = m_y-d.m_y;
return dx*dx + dy*dy;
}
bool operator <(const Peak &d) const
{
return m_v > d.m_v;
}
};
RawImage()
{
m_width = m_height = 0;
}
RawImage(int w, int h)
{
m_width = w;
m_height = h;
m_img.resize(w*h);
}
RawImage(int w, int h, std::vector<T> &img)
{
m_width = w;
m_height = h;
img.resize(w*h);
img.clear();
m_img = std::move(img);
}
T* data()
{
return m_img.data();
}
std::vector<T> dataArray() const
{
return m_img;
}
bool imageStats(T *mean, double *stdDev, T *median, T *min, T *max) const
{
if(m_img.size()==0)return false;
uint64_t sum = 0;
uint64_t sqrSum = 0;
T tMin = UINT64_MAX;
T tMax = 0;
for(T i : m_img)
{
sum += i;
sqrSum += i*i;
tMin = tMin>i ? i : tMin;
tMax = tMax<i ? i : tMax;
}
if(mean)*mean = sum/m_img.size();
if(min)*min = tMin;
if(max)*max = tMax;
if(stdDev)*stdDev = sqrt((sqrSum - (double)sum*sum/m_img.size()) / (double)(m_img.size()-1));
const int shift = sizeof(T)>2 ? sizeof(T)*8 - 16 : 0;
if(median)
{
uint32_t histogram[65536];
memset(histogram, 0, sizeof(histogram));
for(T i : m_img)
histogram[i>>shift]++;
size_t medianSum = 0;
for(int i=0;i<65536;i++)
{
medianSum += histogram[i];
if(medianSum>=m_img.size()/2)
{
*median = i;
break;
}
}
}
return true;
}
T pixel(uint32_t x, uint32_t y) const
{
if(x>=m_width)x=0;
if(y>=m_height)y=0;
return m_img[y*m_width+x];
}
void findPeaks(T background, double distance, std::vector<Peak> &peaks, T sigma) const
{
std::vector<Peak> tmpPeaks;
const int r = 1;
for(uint32_t i=r; i<m_height-r; i++)
{
for(uint32_t o=r; o<m_width-r; o++)
{
T c = pixel(o, i);
if(c>background && checkPixel(c, o-r, i-r) && checkPixel(c, o, i-r) &&
checkPixel(c, o+r, i-r) && checkPixel(c, o-r, i) && checkPixel(c, o+r, i)
&& checkPixel(c, o-r, i+r) && checkPixel(c, o, i+r) && checkPixel(c, o+r, i+r))
{
tmpPeaks.append(Peak(c, o, i));
}
}
}
std::qsort(tmpPeaks.begin(), tmpPeaks.end());
tmpPeaks.resize(10000);
uint32_t d = distance*distance;
for(const Peak &p : tmpPeaks)
{
bool pass = true;
for(const Peak &k : peaks)
{
if(p.distance(k) < d)
{
pass = false;
break;
}
}
if(pass)
{
peaks.append(p);
}
}
}
void medianFilter()
{
std::vector<T> tmp;
tmp.resize(m_width*m_height);
size_t d=0;
for(uint32_t y=0;y<m_height;y++)
{
for(uint32_t x=0;x<m_width;x++)
{
T array[9] = { pixel(x-1, y-1), pixel(x, y-1), pixel(x+1, y-1),
pixel(x-1, y), pixel(x, y), pixel(x+1, y),
pixel(x-1, y+1), pixel(x, y+1), pixel(x+1, y+1)};
std::partial_sort(std::begin(array), array+5, std::end(array));
tmp[d++] = array[4];
}
}
m_img = std::move(tmp);
}
};
#endif // RAWIMAGE_H