C:/sources/c++/buddhabrot/buddhabrot/imagecalculator.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright 2010 Pierre SCHWARTZ
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
00005 documentation files (the "Software"), to deal in the Software without restriction, including without 
00006 limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
00007 of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 
00008 conditions: The above copyright notice and this permission notice shall be included in all copies or 
00009 substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
00010 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
00011 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
00012 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF 
00013 OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
00014 */
00015 
00023 #include "imagecalculator.h"
00024 #include "imagetiler.h"
00025 #include "networkimagetiler.h"
00026 #include "fractale.h"
00027 
00028 ImageCalculator::ImageCalculator(int nb, QImage *d, QRectF w, QSize r, fractale &f, bool type, long nbr, bool a, QStringList& networkList):nbThreads(nb), destination(d), window(w), size(r), frac(f), nbRandom(nbr), useRandomCalculation(type), animate(a), networkAdresses(networkList){     
00029         for (int j=0; j<r.width(); j++){
00030                 QVector<long> v;
00031                 v.fill(0,r.height());                   
00032                 result.push_back(v);
00033         }               
00034         
00035         nbProcessedRows = 0;
00036         lastProgression = 0;
00037 }
00038 
00039 ImageCalculator::~ImageCalculator(){
00040 }
00041 
00045 void ImageCalculator::run(){
00046         int nbAgents = nbThreads + networkAdresses.length();
00047 
00048         for(int i=0; i<nbAgents; i++){
00049                 ImageTiler *it = new ImageTiler(result, mutex, window, size, &frac, useRandomCalculation, nbRandom, i, nbAgents, i);
00050                 
00051                 // lier les slots
00052                 QObject::connect(it, SIGNAL(processed()), this, SLOT(s_processing()));
00053                 
00054                 it->run();
00055                 tilers.push_back(it);
00056         }
00057 
00058         for (int i=0; i<networkAdresses.length(); i++){
00059                 NetworkImageTiler *it = new NetworkImageTiler(networkAdresses.at(i), result, mutex, window, size, &frac, useRandomCalculation, nbRandom, nbThreads+i, nbAgents);
00060                 it->run();
00061                 networkTilers.push_back(it);
00062         }
00063 
00064         for (int i=0; i<nbThreads; i++)
00065                 tilers[i]->wait();
00066         for (int i=0; i<networkAdresses.length(); i++)
00067                 networkTilers[i]->wait();
00068 
00069         sumImagesIntoDestination();
00070 }
00071 
00075 void ImageCalculator::sumImagesIntoDestination(){
00076         int v;
00077         maxValue =  0;
00078         for (int x = 0; x< size.width(); x++)
00079                 for (int y=0; y<size.height(); y++){
00080                         v = result[x][y];
00081                         destination->setPixel(x,y,ImageCalculator::intToRGBA( v ));
00082                         if (v > maxValue)
00083                                 maxValue = v;
00084                 }
00085 
00086         for (int i=0; i<tilers.count(); i++)            
00087                 delete tilers[i];
00088         for (int i=0; i<networkTilers.count(); i++)
00089                 delete networkTilers[i];
00090 
00091         emit render();
00092 }
00093 
00097 void ImageCalculator::s_processing(){
00098         nbProcessedRows ++;
00099 
00100         if (animate){
00101                 if (useRandomCalculation){
00102                         // rendu aléatoire
00103                         if (int(100 * nbProcessedRows / nbRandom) > lastProgression){
00104                                 sumImagesIntoDestination();
00105                                 emit render();
00106                         }
00107                 }else{
00108                         sumImagesIntoDestination();
00109                         emit render();
00110                 }
00111         }
00112 
00113         if (useRandomCalculation){
00114                 // rendu aléatoire
00115                 lastProgression = 100 * nbProcessedRows / nbRandom;
00116                 emit progress(lastProgression);
00117         }else{
00118                 // rendu par point
00119                 emit progress(100 * nbProcessedRows / size.width());
00120         }
00121 }
00122 
00123 
00124  

Generated on Fri Feb 26 21:07:52 2010 for BuddhaBrot by  doxygen 1.4.6-NO