diff --git a/src/CameraImageWrapper.cpp b/src/CameraImageWrapper.cpp index 7a1148d..2b7ebd7 100644 --- a/src/CameraImageWrapper.cpp +++ b/src/CameraImageWrapper.cpp @@ -3,81 +3,78 @@ #include #include -CameraImageWrapper::CameraImageWrapper() : LuminanceSource(0,0) +CameraImageWrapper::CameraImageWrapper() : LuminanceSource(0,0), image(NULL) { } CameraImageWrapper::CameraImageWrapper(const QImage &sourceImage) : LuminanceSource(sourceImage.width(), sourceImage.height()) { - image = sourceImage.copy(); + image = grayScaleImage( &sourceImage ); } CameraImageWrapper::CameraImageWrapper(CameraImageWrapper& otherInstance) : LuminanceSource(otherInstance.getWidth(), otherInstance.getHeight()) { - image = otherInstance.getOriginalImage().copy(); + image = new QImage(otherInstance.getOriginalImage()); } CameraImageWrapper::~CameraImageWrapper() { + if(image) + delete image; } CameraImageWrapper *CameraImageWrapper::Factory(const QImage &sourceImage, int maxWidth, int maxHeight, bool smoothTransformation) { - QImage image; if((maxWidth != 1 || maxHeight != 1) && (sourceImage.width() > maxWidth || sourceImage.height() > maxHeight)) + { + QImage image; image = sourceImage.scaled( maxWidth != -1 ? maxWidth : sourceImage.width(), maxHeight != -1 ? maxHeight : sourceImage.height(), Qt::KeepAspectRatio, smoothTransformation ? Qt::SmoothTransformation : Qt::FastTransformation); + return new CameraImageWrapper(image); + } else - image = sourceImage; - - return new CameraImageWrapper(image); + return new CameraImageWrapper(sourceImage); } int CameraImageWrapper::getWidth() const { - return image.width(); + return image->width(); } int CameraImageWrapper::getHeight() const { - return image.height(); + return image->height(); } unsigned char CameraImageWrapper::getPixel(int x, int y) const { - QRgb pixel = image.pixel(x,y); - - return qGray(pixel);//((qRed(pixel) + qGreen(pixel) + qBlue(pixel)) / 3); + return image->pixel(x,y); } unsigned char* CameraImageWrapper::copyMatrix() const { - unsigned char* newMatrix = (unsigned char*)malloc(image.width()*image.height()*sizeof(unsigned char)); + unsigned char* newMatrix = (unsigned char*)malloc(image->width() * image->height() * sizeof(unsigned char)); int cnt = 0; - for(int i=0; iwidth(); i++) + for(int j=0; jheight(); j++) newMatrix[cnt++] = getPixel(i,j); - } - } return newMatrix; } -QImage CameraImageWrapper::grayScaleImage(QImage::Format f) +QImage* CameraImageWrapper::grayScaleImage(const QImage *origin) { - QImage tmp(image.width(), image.height(), f); - for(int i=0; iwidth(), origin->height(), QImage::Format_Grayscale8); + for(int i=0; iwidth(); i++) { - for(int j=0; jheight(); j++) { - int pix = (int)getPixel(i,j); - tmp.setPixel(i,j, qRgb(pix ,pix,pix)); + int pix = qGray(origin->pixel(QPoint(i,j))); + tmp->setPixel(i,j, qRgb(pix ,pix,pix)); } } @@ -86,7 +83,7 @@ QImage CameraImageWrapper::grayScaleImage(QImage::Format f) QImage CameraImageWrapper::getOriginalImage() { - return image; + return *image; } ArrayRef CameraImageWrapper::getRow(int y, ArrayRef row) const @@ -109,19 +106,20 @@ ArrayRef CameraImageWrapper::getMatrix() const char* matrix = new char[width*height]; char* m = matrix; + ArrayRef tmpRow(0); + for(int y=0; y tmpRow; - tmpRow = getRow(y, ArrayRef(width)); + tmpRow = getRow(y, tmpRow); #if __cplusplus > 199711L memcpy(m, tmpRow->values().data(), width); #else - memcpy(m, &tmpRow->values()[0], width); + memcpy(m, &tmpRow[0], width); #endif m += width * sizeof(unsigned char); } - ArrayRef arr = ArrayRef(matrix, width*height); + ArrayRef arr = ArrayRef(matrix, width*height); if (matrix) { delete matrix; @@ -129,3 +127,42 @@ ArrayRef CameraImageWrapper::getMatrix() const return arr; } + +QImage *CameraImageWrapper::sharpen(const QImage *origin) +{ + QImage * newImage = new QImage(* origin); + + int kernel [3][3]= {{0,-1,0}, + {-1,5,-1}, + {0,-1,0}}; + int kernelSize = 3; + int sumKernel = 1; + int r,g,b; + QColor color; + + for(int x=kernelSize/2; xwidth()-(kernelSize/2); x++){ + for(int y=kernelSize/2; yheight()-(kernelSize/2); y++){ + + r = 0; + g = 0; + b = 0; + + for(int i = -kernelSize/2; i<= kernelSize/2; i++){ + for(int j = -kernelSize/2; j<= kernelSize/2; j++){ + color = QColor(origin->pixel(x+i, y+j)); + r += color.red()*kernel[kernelSize/2+i][kernelSize/2+j]; + g += color.green()*kernel[kernelSize/2+i][kernelSize/2+j]; + b += color.blue()*kernel[kernelSize/2+i][kernelSize/2+j]; + } + } + + r = qBound(0, r/sumKernel, 255); + g = qBound(0, g/sumKernel, 255); + b = qBound(0, b/sumKernel, 255); + + newImage->setPixel(x,y, qRgb(r,g,b)); + + } + } + return newImage; +} diff --git a/src/CameraImageWrapper.h b/src/CameraImageWrapper.h index ea9bb18..3dc2d8c 100644 --- a/src/CameraImageWrapper.h +++ b/src/CameraImageWrapper.h @@ -23,7 +23,7 @@ public: unsigned char getPixel(int x, int y) const; unsigned char* copyMatrix() const; - QImage grayScaleImage(QImage::Format f); + QImage* grayScaleImage(const QImage *origin); QImage getOriginalImage(); // Callers take ownership of the returned memory and must call delete [] on it themselves. @@ -31,7 +31,9 @@ public: ArrayRef getMatrix() const; private: - QImage image; + QImage* sharpen(const QImage *origin); + + QImage* image; unsigned char* pRow; unsigned char* pMatrix; };