diff --git a/imageselector.pro b/imageselector.pro index 6af2146..fa187d7 100644 --- a/imageselector.pro +++ b/imageselector.pro @@ -28,4 +28,5 @@ HEADERS += mainwindow.h \ imageringlist.h \ database.h \ loadrunable.h \ - imageinfo.h + imageinfo.h \ + rawimage.h diff --git a/loadrunable.cpp b/loadrunable.cpp index 3d05f4e..9397ce4 100644 --- a/loadrunable.cpp +++ b/loadrunable.cpp @@ -5,6 +5,7 @@ #include #include #include +#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 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(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; iheight; 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 bits8; std::vector 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(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> 8; + if(data) + { + for(size_t i=0; i> 8; + } + if(image)*image = new RawImage(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> 24; + if(data) + { + for(size_t i=0; i> 24; + } + if(image)*image = new RawImage(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; iheight; 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 { diff --git a/rawimage.h b/rawimage.h new file mode 100644 index 0000000..8778d5d --- /dev/null +++ b/rawimage.h @@ -0,0 +1,182 @@ +#ifndef RAWIMAGE_H +#define RAWIMAGE_H + +#include +#include +#include +#include +#include + +class RawImageAbs +{ +}; + +template +class RawImage : public RawImageAbs +{ + uint32_t m_width,m_height; + uint32_t m_width1,m_height1; + std::vector 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 &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 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 = tMax2 ? 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 &peaks, T sigma) const + { + std::vector tmpPeaks; + const int r = 1; + for(uint32_t i=r; ibackground && 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 tmp; + tmp.resize(m_width*m_height); + size_t d=0; + for(uint32_t y=0;y