Skip to content
Snippets Groups Projects
Commit 12421c8b authored by JPiljug's avatar JPiljug
Browse files

hello

parent 700ab1b2
No related branches found
No related tags found
No related merge requests found
...@@ -21,6 +21,8 @@ QRgb FloydSteiberg::errorAdd(QColor orig, int err_r, int err_g, int err_b, int e ...@@ -21,6 +21,8 @@ QRgb FloydSteiberg::errorAdd(QColor orig, int err_r, int err_g, int err_b, int e
return QColor(new_rgb[0],new_rgb[1],new_rgb[2]).rgb(); return QColor(new_rgb[0],new_rgb[1],new_rgb[2]).rgb();
} }
//
QImage *FloydSteiberg::getIndexed(QImage* source, QVector<QRgb> colors){ QImage *FloydSteiberg::getIndexed(QImage* source, QVector<QRgb> colors){
timeval t1; timeval t1;
timeval t2; timeval t2;
...@@ -29,8 +31,8 @@ QImage *FloydSteiberg::getIndexed(QImage* source, QVector<QRgb> colors){ ...@@ -29,8 +31,8 @@ QImage *FloydSteiberg::getIndexed(QImage* source, QVector<QRgb> colors){
long t = 0; long t = 0;
QImage *newImage = new QImage(source->size(),QImage::Format_Indexed8); QImage *newImage = new QImage(source->size(),QImage::Format_Indexed8);
newImage->setColorTable(colors); newImage->setColorTable(colors);
int w = source->width(); int width = source->width();
int h = source->height(); int height = source->height();
int k; int k;
int errorR; int errorR;
int errorG; int errorG;
...@@ -38,21 +40,21 @@ QImage *FloydSteiberg::getIndexed(QImage* source, QVector<QRgb> colors){ ...@@ -38,21 +40,21 @@ QImage *FloydSteiberg::getIndexed(QImage* source, QVector<QRgb> colors){
int errorA; int errorA;
QColor currentPixel; QColor currentPixel;
QColor nextPixel; QColor nextPixel;
QColor *nextRow = static_cast<QColor*>(malloc(sizeof (QColor)*w)); QColor *nextRow = static_cast<QColor*>(malloc(sizeof (QColor)*width));
QColor *thisRow = static_cast<QColor*>(malloc(sizeof (QColor)*w)); QColor *thisRow = static_cast<QColor*>(malloc(sizeof (QColor)*width));
QColor *x; QColor *x;
// Floyd-Steinberg Dithering // Floyd-Steinberg Dithering
// - * 7 where *=pixel being processed, -=previously processed pixel // - * 7 where *=pixel being processed, -=previously processed pixel
// 3 5 1 and pixel difference is distributed to neighbor pixels // 3 5 1 and pixel difference is distributed to neighbor pixels
// Note: 7+3+5+1=16 so we divide by 16 (>>4) before adding. // Note: 7+3+5+1=16 so we divide by 16 (>>4) before adding.
for(int i = 1; i < w-1; i++){
for(int i = 1; i < width-1; i++){
nextRow[i]=source->pixelColor(i,1); nextRow[i]=source->pixelColor(i,1);
} }
gettimeofday(&t1,nullptr); gettimeofday(&t1,nullptr);
for(int j = 0; j < h-1; j++){ for(int j = 0; j < height-1; j++){
currentPixel = thisRow[0]; currentPixel = thisRow[0];
for(int i = 0; i < w; i++){ for(int i = 0; i < width; i++){
thisRow[i]=source->pixelColor(i,j); thisRow[i]=source->pixelColor(i,j);
} }
gettimeofday(&t3,nullptr); gettimeofday(&t3,nullptr);
...@@ -68,7 +70,7 @@ QImage *FloydSteiberg::getIndexed(QImage* source, QVector<QRgb> colors){ ...@@ -68,7 +70,7 @@ QImage *FloydSteiberg::getIndexed(QImage* source, QVector<QRgb> colors){
nextPixel=errorAdd(nextPixel,errorR,errorG,errorB,errorA,7); nextPixel=errorAdd(nextPixel,errorR,errorG,errorB,errorA,7);
nextRow[0]=errorAdd(nextRow[0],errorR,errorG,errorB,errorA,5); nextRow[0]=errorAdd(nextRow[0],errorR,errorG,errorB,errorA,5);
nextRow[1]=errorAdd(nextRow[1],errorR,errorG,errorB,errorA,1); nextRow[1]=errorAdd(nextRow[1],errorR,errorG,errorB,errorA,1);
for(int i = 1; i < w-1; i++){ for(int i = 1; i < width-1; i++){
currentPixel = nextPixel; currentPixel = nextPixel;
gettimeofday(&t3,nullptr); gettimeofday(&t3,nullptr);
k = nearestColor(currentPixel, colors); k = nearestColor(currentPixel, colors);
...@@ -89,24 +91,24 @@ QImage *FloydSteiberg::getIndexed(QImage* source, QVector<QRgb> colors){ ...@@ -89,24 +91,24 @@ QImage *FloydSteiberg::getIndexed(QImage* source, QVector<QRgb> colors){
k = nearestColor(currentPixel, colors); k = nearestColor(currentPixel, colors);
gettimeofday(&t4,nullptr); gettimeofday(&t4,nullptr);
t += (t4.tv_sec-t3.tv_sec)*1000000+t4.tv_usec-t3.tv_usec; t += (t4.tv_sec-t3.tv_sec)*1000000+t4.tv_usec-t3.tv_usec;
newImage->setPixel(w-1,j,k); newImage->setPixel(width-1,j,k);
errorR = currentPixel.red() - qRed(colors[k]); errorR = currentPixel.red() - qRed(colors[k]);
errorG = currentPixel.green() - qGreen(colors[k]); errorG = currentPixel.green() - qGreen(colors[k]);
errorB = currentPixel.blue() - qBlue(colors[k]); errorB = currentPixel.blue() - qBlue(colors[k]);
errorA = currentPixel.blue() - qAlpha(colors[k]); errorA = currentPixel.blue() - qAlpha(colors[k]);
nextRow[w-2]=errorAdd(nextRow[w-2],errorR,errorG,errorB,errorA,5); nextRow[width-2]=errorAdd(nextRow[width-2],errorR,errorG,errorB,errorA,5);
nextRow[w-1]=errorAdd(nextRow[w-1],errorR,errorG,errorB,errorA,1); nextRow[width-1]=errorAdd(nextRow[width-1],errorR,errorG,errorB,errorA,1);
x = thisRow; x = thisRow;
thisRow=nextRow; thisRow=nextRow;
nextRow=x; nextRow=x;
} }
for(int i = 0; i < w-1; i++){ for(int i = 0; i < width-1; i++){
currentPixel = nextPixel; currentPixel = nextPixel;
gettimeofday(&t3,nullptr); gettimeofday(&t3,nullptr);
k = nearestColor(currentPixel, colors); k = nearestColor(currentPixel, colors);
gettimeofday(&t4,nullptr); gettimeofday(&t4,nullptr);
t += (t4.tv_sec-t3.tv_sec)*1000000+t4.tv_usec-t3.tv_usec; t += (t4.tv_sec-t3.tv_sec)*1000000+t4.tv_usec-t3.tv_usec;
newImage->setPixel(i,h-1,k); newImage->setPixel(i,height-1,k);
errorR = currentPixel.red() - qRed(colors[k]); errorR = currentPixel.red() - qRed(colors[k]);
errorG = currentPixel.green() - qGreen(colors[k]); errorG = currentPixel.green() - qGreen(colors[k]);
errorB = currentPixel.blue() - qBlue(colors[k]); errorB = currentPixel.blue() - qBlue(colors[k]);
...@@ -114,15 +116,18 @@ QImage *FloydSteiberg::getIndexed(QImage* source, QVector<QRgb> colors){ ...@@ -114,15 +116,18 @@ QImage *FloydSteiberg::getIndexed(QImage* source, QVector<QRgb> colors){
nextPixel=thisRow[i+1]; nextPixel=thisRow[i+1];
nextPixel=errorAdd(nextPixel,errorR,errorG,errorB,errorA,7); nextPixel=errorAdd(nextPixel,errorR,errorG,errorB,errorA,7);
} }
k = nearestColor(thisRow[w-1], colors); k = nearestColor(thisRow[width-1], colors);
newImage->setPixel(w-1,h-1,k); newImage->setPixel(width-1,height-1,k);
free(nextRow); free(nextRow);
free(thisRow); free(thisRow);
gettimeofday(&t2,nullptr); gettimeofday(&t2,nullptr);
printf("%ld\n%ld\n%d x %d\n",(t2.tv_sec-t1.tv_sec)*1000000+t2.tv_usec-t1.tv_usec,t,w,h); printf("%ld\n%ld\n%d x %d\n",(t2.tv_sec-t1.tv_sec)*1000000+t2.tv_usec-t1.tv_usec,t,width,height);
//delete source; //delete source;
return newImage; return newImage;
} }
//
int FloydSteiberg::nearestColor(QColor color, QVector<QRgb> colorVect){ int FloydSteiberg::nearestColor(QColor color, QVector<QRgb> colorVect){
int minDistanceSquared = 255 * 255 + 255 * 255 + 255 * 255 + 255 * 255 + 1; int minDistanceSquared = 255 * 255 + 255 * 255 + 255 * 255 + 255 * 255 + 1;
int bestIndex = 0; int bestIndex = 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment