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
return QColor(new_rgb[0],new_rgb[1],new_rgb[2]).rgb();
}
//
QImage *FloydSteiberg::getIndexed(QImage* source, QVector<QRgb> colors){
timeval t1;
timeval t2;
......@@ -29,8 +31,8 @@ QImage *FloydSteiberg::getIndexed(QImage* source, QVector<QRgb> colors){
long t = 0;
QImage *newImage = new QImage(source->size(),QImage::Format_Indexed8);
newImage->setColorTable(colors);
int w = source->width();
int h = source->height();
int width = source->width();
int height = source->height();
int k;
int errorR;
int errorG;
......@@ -38,21 +40,21 @@ QImage *FloydSteiberg::getIndexed(QImage* source, QVector<QRgb> colors){
int errorA;
QColor currentPixel;
QColor nextPixel;
QColor *nextRow = static_cast<QColor*>(malloc(sizeof (QColor)*w));
QColor *thisRow = static_cast<QColor*>(malloc(sizeof (QColor)*w));
QColor *nextRow = static_cast<QColor*>(malloc(sizeof (QColor)*width));
QColor *thisRow = static_cast<QColor*>(malloc(sizeof (QColor)*width));
QColor *x;
// Floyd-Steinberg Dithering
// - * 7 where *=pixel being processed, -=previously processed pixel
// 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.
for(int i = 1; i < w-1; i++){
for(int i = 1; i < width-1; i++){
nextRow[i]=source->pixelColor(i,1);
}
gettimeofday(&t1,nullptr);
for(int j = 0; j < h-1; j++){
for(int j = 0; j < height-1; j++){
currentPixel = thisRow[0];
for(int i = 0; i < w; i++){
for(int i = 0; i < width; i++){
thisRow[i]=source->pixelColor(i,j);
}
gettimeofday(&t3,nullptr);
......@@ -68,7 +70,7 @@ QImage *FloydSteiberg::getIndexed(QImage* source, QVector<QRgb> colors){
nextPixel=errorAdd(nextPixel,errorR,errorG,errorB,errorA,7);
nextRow[0]=errorAdd(nextRow[0],errorR,errorG,errorB,errorA,5);
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;
gettimeofday(&t3,nullptr);
k = nearestColor(currentPixel, colors);
......@@ -89,24 +91,24 @@ QImage *FloydSteiberg::getIndexed(QImage* source, QVector<QRgb> colors){
k = nearestColor(currentPixel, colors);
gettimeofday(&t4,nullptr);
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]);
errorG = currentPixel.green() - qGreen(colors[k]);
errorB = currentPixel.blue() - qBlue(colors[k]);
errorA = currentPixel.blue() - qAlpha(colors[k]);
nextRow[w-2]=errorAdd(nextRow[w-2],errorR,errorG,errorB,errorA,5);
nextRow[w-1]=errorAdd(nextRow[w-1],errorR,errorG,errorB,errorA,1);
nextRow[width-2]=errorAdd(nextRow[width-2],errorR,errorG,errorB,errorA,5);
nextRow[width-1]=errorAdd(nextRow[width-1],errorR,errorG,errorB,errorA,1);
x = thisRow;
thisRow=nextRow;
nextRow=x;
}
for(int i = 0; i < w-1; i++){
for(int i = 0; i < width-1; i++){
currentPixel = nextPixel;
gettimeofday(&t3,nullptr);
k = nearestColor(currentPixel, colors);
gettimeofday(&t4,nullptr);
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]);
errorG = currentPixel.green() - qGreen(colors[k]);
errorB = currentPixel.blue() - qBlue(colors[k]);
......@@ -114,15 +116,18 @@ QImage *FloydSteiberg::getIndexed(QImage* source, QVector<QRgb> colors){
nextPixel=thisRow[i+1];
nextPixel=errorAdd(nextPixel,errorR,errorG,errorB,errorA,7);
}
k = nearestColor(thisRow[w-1], colors);
newImage->setPixel(w-1,h-1,k);
k = nearestColor(thisRow[width-1], colors);
newImage->setPixel(width-1,height-1,k);
free(nextRow);
free(thisRow);
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;
return newImage;
}
//
int FloydSteiberg::nearestColor(QColor color, QVector<QRgb> colorVect){
int minDistanceSquared = 255 * 255 + 255 * 255 + 255 * 255 + 255 * 255 + 1;
int bestIndex = 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment