14 #include <qapplication.h> 19 #define MIN(x,y) ((x) < (y) ? (x) : (y)) 20 #define MAX(x,y) ((x) < (y) ? (x) : (y)) 25 #include "../tools/imageTools.h" 26 #include "../../gui/statusWidget.h" 259 #define MAX_TILES 216 299 if( editedImage->depth() < 32 )
302 editedImage =
new QImage( tmp->convertDepth( 32, Qt::AutoColor ) );
303 delete tmp; tmp=NULL;
307 bool useBusyIndicators =
false;
309 if( options != NULL && options->
getStatus() != NULL )
311 useBusyIndicators =
true;
316 srand(
unsigned(time(NULL)) );
320 if(options == NULL) tileSize = QSize(6,6);
325 if( options != NULL && options->
getFileList().size() > 0 )
337 if(useBusyIndicators)
339 QString statusMessage = qApp->translate(
"mosaicEffect",
"Applying Mosaic Effect:" );
341 qApp->processEvents();
345 const int updateIncrement = (int) ( (0.01 * editedImage->width() * editedImage->height()) /
346 (tileSize.width() * tileSize.height()) );
351 for(y=0; y<editedImage->height(); y+=tileSize.height())
353 for( x=0; x<editedImage->width(); x+=tileSize.width())
359 if(useBusyIndicators)
362 if(newProgress >= updateIncrement)
366 qApp->processEvents();
383 int colorRes = (int)pow(
MAX_TILES, 1.0/3 );
386 int colorIncrement = 255 / (colorRes-1);
393 for(r=0; r<=255; r+=colorIncrement)
395 for(g=0; g<=255; g+=colorIncrement)
397 for(b=0; b<=255; b+=colorIncrement)
399 colorTiles.
tiles[tile].
image.create( tileSize.width(), tileSize.height(), 32);
400 colorTiles.
tiles[tile].
image.fill( qRgb(r, g, b) );
405 QColor(r,g,b).getHsv( &h, &(colorTiles.
tiles[tile].
avgS), &(colorTiles.
tiles[tile].
avgL) );
425 int* fileIndicesUsed =
new int[files.size()];
427 for(i=0; i<imageTiles.
numInitialized; i++) { fileIndices[i] = -1; }
428 for(i=0; i<((int)files.size()); i++) { fileIndicesUsed[i] = 0; }
433 double percentage = ((double)rand()) / RAND_MAX;
434 int fileNum = (int) ( (files.size() - (i+1)) * percentage);
438 int realFileNum = fileNum;
441 if( fileIndicesUsed[j] == 1 ) { realFileNum++; }
448 fileIndices[i] = realFileNum;
449 fileIndicesUsed[realFileNum] = 1;
459 if( fileIndices[j] > fileIndices[j+1] )
461 int tmp = fileIndices[j+1];
462 fileIndices[j+1] = fileIndices[j];
463 fileIndices[j] = tmp;
469 QStringList chosenFiles;
470 QStringList::iterator it;
471 int curFileIndex = 0;
472 int nextDesiredFileIndex = 0;
473 for(it = files.begin(); it != files.end(); it++ )
475 if( curFileIndex == fileIndices[nextDesiredFileIndex] )
477 chosenFiles.append( *it );
478 nextDesiredFileIndex++;
494 delete fileIndicesUsed;
496 fileIndicesUsed = NULL;
502 for(it = chosenFiles.begin(); it != chosenFiles.end(); it++ )
508 int intermediateWidth = -1;
509 int intermediateHeight = -1;
510 if( ((
double)imageRes.width()) / tileSize.width() > ((double)imageRes.height()) / tileSize.height() )
512 intermediateHeight = tileSize.height();
513 intermediateWidth = (int) ( ((1.0*intermediateHeight*imageRes.width()) / imageRes.height()) + 0.5 );
517 intermediateWidth = tileSize.width();
518 intermediateHeight = (int) ( ((1.0*intermediateWidth*imageRes.height()) / imageRes.width()) + 0.5 );
522 scaleImage( *it, scaledImage, intermediateWidth, intermediateHeight );
525 if( scaledImage.width() != tileSize.width() || scaledImage.height() != tileSize.height() )
526 scaledImage = scaledImage.scaled( tileSize, Qt::IgnoreAspectRatio );
529 imageTiles.
tiles[tile].
image.create( tileSize.width(), tileSize.height(), 32);
530 imageTiles.
tiles[tile].
image.fill( qRgb(255,255,255) );
533 int xOffset = (scaledImage.width() - tileSize.width())/2;
534 int yOffset = (scaledImage.height() - tileSize.height())/2;
536 uchar* scaledScanLine;
537 uchar* croppedScanLine;
541 double avgR=0;
double avgG=0;
double avgB=0;
542 double avgS=0;
double avgL=0;
546 if( scaledImage.isNull() )
548 avgR = avgG = avgB = 255;
553 for( y=0; y<tileSize.height(); y++)
555 scaledScanLine = scaledImage.scanLine(y + yOffset);
556 croppedScanLine = imageTiles.
tiles[tile].
image.scanLine(y);
558 for( x=0; x<tileSize.width(); x++)
560 scaledRgb = ((QRgb*) scaledScanLine) +x + xOffset;
561 croppedRgb = ((QRgb*) croppedScanLine) + x;
564 *croppedRgb = *scaledRgb;
567 QColor color( *croppedRgb );
570 avgG += color.green();
571 avgB += color.blue();
574 color.getHsv( &h, &s, &l );
581 int pixelCount = tileSize.width()*tileSize.height();
589 imageTiles.
tiles[tile].
avgColor = QColor( (
int)avgR, (
int)avgG, (
int)avgB );
606 uchar* imageScanLine;
611 int xMax =
MIN( tileSet->
tiles[0].
image.width(), image->width() - topLeftCorner.x() );
613 int yMax =
MIN( tileSet->
tiles[0].
image.height(), image->height() - topLeftCorner.y() );
616 double avgR=0;
double avgG=0;
double avgB=0;
619 for(i=0; i<361; i++) { hueHist[i] = 0; }
620 double avgS=0;
double avgL=0;
622 for( y=yMin; y<yMax; y++)
624 imageScanLine = image->scanLine(y+topLeftCorner.y());
625 for( x=xMin; x<xMax; x++)
627 imageRgb = ((QRgb*)imageScanLine+x+topLeftCorner.x());
628 QColor color( *imageRgb );
631 avgG += color.green();
632 avgB += color.blue();
635 color.getHsv( &h, &s, &l );
636 hueHist[
MIN(
MAX(h,0), 360 ) ]++;
643 int pixelCount = (yMax-yMin) * (xMax-xMin);
651 int mostCommonHue = 0;
654 if( hueHist[i] > hueHist[mostCommonHue] ) { mostCommonHue = i; }
675 distances[i] = ((2+(rBar/256)) * dR * dR) +
677 ((2 + ((255.0-rBar)/256)) * dB * dB);
684 double epsilon = 0.000000001;
687 distances[i] = 1.0 /
MAX(distances[i], epsilon);
692 double percentage = ((double)rand()) / RAND_MAX;
693 double number = sum * percentage;
709 double sInc = avgS - tileSet->
tiles[TILE].
avgS;
710 double lInc = avgL - tileSet->
tiles[TILE].
avgL;
714 for( y=yMin; y<yMax; y++ )
717 imageScanLine = image->scanLine( (y+topLeftCorner.y()) );
718 tileScanLine = tileSet->
tiles[TILE].
image.scanLine(y);
719 for( x=xMin; x<xMax; x++)
722 tileRgb = ((QRgb*) tileScanLine) + x;;
723 QColor color( *tileRgb );
727 color.getHsv( &h, &s, &l );
734 s = (int)
MIN(
MAX( s+sInc, 0), 255 );
735 l = (int)
MIN(
MAX( l+lInc, 0), 255 );
738 color.setHsv( mostCommonHue, s, l );
741 imageRgb = ((QRgb*)imageScanLine) + x + topLeftCorner.x();
743 *imageRgb = color.rgb();
MosaicOptions(QStringList files, QSize tileSize, StatusWidget *status)
QStringList getFileList()
StatusWidget * getStatus()
QImage * mosaicEffect(QString filename, MosaicOptions *options)
void splatBestTile(QImage *image, QPoint topLeftCorner, TileSet *tileSet)
void constructImageTiles(QStringList files, QSize tileSize)
void constructColorTiles(QSize tileSize)