00001 #include "squarePixels.h"
00002 #include "../fileUtil.h"
00003
00004 using namespace std;
00005
00006 int MSquarePixels::createJobs(const PlaneList &planes) {
00007 ASSERT( jobs.empty() && !planes.empty()
00008 && moduleRanges() && moduleDomains() && moduleEncoder() );
00009 DEBUG_ONLY( planeList= planes; )
00010
00011 for (PlaneList::const_iterator plane= planes.begin(); plane!=planes.end(); ++plane) {
00012
00013 const IColorTransformer::PlaneSettings *plSet= plane->settings;
00014 PlaneBlock job;
00015 job.width= plSet->width;
00016 job.height= plSet->height;
00017 job.pixels= plane->pixels;
00018 job.sumsValid= false;
00019 job.settings= plSet;
00020 DEBUG_ONLY( job.ranges= 0; job.domains= 0; job.encoder= 0; )
00021
00022 jobs.push_back(job);
00023 }
00024
00025
00026 int maxPixels= powers[maxPartSize()+2*jobs.front().settings->zoom];
00027
00028 Uint i= 0;
00029 while ( i < jobs.size() )
00030 if ( jobs[i].width*jobs[i].height <= maxPixels )
00031 ++i;
00032 else {
00033
00034 bool xdiv= ( jobs[i].width >= jobs[i].height );
00035 int longer= ( xdiv ? jobs[i].width : jobs[i].height );
00036
00037 int bits= log2ceil(longer);
00038 int divSize= ( longer >= powers[bits-1]+powers[bits-2]
00039 ? powers[bits-1]
00040 : powers[bits-2] );
00041
00042 jobs.push_back(jobs[i]);
00043 if (xdiv) {
00044 jobs[i].width= divSize;
00045 jobs.back().pixels.shiftMatrix(divSize,0);
00046 jobs.back().width-= divSize;
00047 } else {
00048 jobs[i].height= divSize;
00049 jobs.back().pixels.shiftMatrix(0,divSize);
00050 jobs.back().height-= divSize;
00051 }
00052 }
00053
00054
00055 for (JobIterator job=jobs.begin(); job!=jobs.end(); ++job) {
00056 job->ranges= moduleRanges() ->clone();
00057 job->domains= moduleDomains()->clone();
00058 job->encoder= moduleEncoder()->clone();
00059 }
00060
00061 return jobs.size();
00062 }
00063
00064 void MSquarePixels::writeSettings(ostream &file) {
00065 ASSERT( moduleRanges() && moduleDomains() && moduleEncoder() );
00066
00067 put<Uchar>( file, maxPartSize() );
00068 file_saveModuleType( file, ModuleRanges );
00069 file_saveModuleType( file, ModuleDomains );
00070 file_saveModuleType( file, ModuleEncoder );
00071
00072 moduleRanges()->writeSettings(file);
00073 moduleDomains()->writeSettings(file);
00074 moduleEncoder()->writeSettings(file);
00075 }
00076
00077 void MSquarePixels::readSettings(istream &file) {
00078 ASSERT( !moduleRanges() && !moduleDomains() && !moduleEncoder() );
00079
00080 maxPartSize()= get<Uchar>(file);
00081 file_loadModuleType( file, ModuleRanges );
00082 file_loadModuleType( file, ModuleDomains );
00083 file_loadModuleType( file, ModuleEncoder );
00084
00085 moduleRanges()->readSettings(file);
00086 moduleDomains()->readSettings(file);
00087 moduleEncoder()->readSettings(file);
00088 }
00089
00090 void MSquarePixels::writeJobs(ostream &file,int phaseBegin,int phaseEnd) {
00091 ASSERT( !jobs.empty() && 0<=phaseBegin && phaseBegin<phaseEnd && phaseEnd<=phaseCount() );
00092
00093 if (!phaseBegin)
00094 for (JobIterator it=jobs.begin(); it!=jobs.end(); ++it) {
00095 STREAM_POS(file);
00096 it->ranges->writeData(file);
00097 STREAM_POS(file);
00098 it->domains->writeData(file);
00099 }
00100
00101 for (int phase=phaseBegin; phase<phaseEnd; ++phase)
00102 for (JobIterator it=jobs.begin(); it!=jobs.end(); ++it)
00103 STREAM_POS(file), it->encoder->writeData(file,phase);
00104 }
00105
00106 void MSquarePixels::readJobs(istream &file,int phaseBegin,int phaseEnd) {
00107 ASSERT( !jobs.empty() && 0<=phaseBegin && phaseBegin<phaseEnd && phaseEnd<=phaseCount() );
00108
00109 if (!phaseBegin)
00110 for (JobIterator it=jobs.begin(); it!=jobs.end(); ++it) {
00111 STREAM_POS(file);
00112 it->ranges->readData_buildRanges(file,*it);
00113 STREAM_POS(file);
00114 it->domains->readData(file);
00115 it->encoder->initialize(IRoot::Decode,*it);
00116 }
00117
00118 for (int phase=phaseBegin; phase<phaseEnd; ++phase)
00119 for (JobIterator it=jobs.begin(); it!=jobs.end(); ++it)
00120 STREAM_POS(file), it->encoder->readData(file,phase);
00121 }