Reimplemented things working with summed pixels, other fixes.
[fic.git] / modules / squarePixels.cpp
blobfc94b4864ae49d3358b624adc012be2cf120bc43
1 #include "squarePixels.h"
2 #include "../fileUtil.h"
4 using namespace std;
6 int MSquarePixels::createJobs(const PlaneList &planes) {
7 ASSERT( jobs.empty() && !planes.empty()
8 && moduleRanges() && moduleDomains() && moduleEncoder() );
9 DEBUG_ONLY( planeList= planes; )
11 for (PlaneList::const_iterator plane= planes.begin(); plane!=planes.end(); ++plane) {
12 // convert the Plane into a PlaneBlock (containing the whole contents)
13 const IColorTransformer::PlaneSettings *plSet= plane->settings;
14 Job job;
15 job.width= plSet->width;
16 job.height= plSet->height;
17 job.pixels= plane->pixels;
18 job.sumsValid= false;
19 job.settings= plSet;
20 DEBUG_ONLY( job.ranges= 0; job.domains= 0; job.encoder= 0; )
21 // append the result to the jobs
22 jobs.push_back(job);
25 // the zoom only affects maxPixels (zoom is assumed to be the same for all planes)
26 int maxPixels= powers[maxPartSize()+2*jobs.front().settings->zoom];
27 // split jobs until they're small enough
28 Uint i= 0;
29 while ( i < jobs.size() )
30 if ( jobs[i].width*jobs[i].height <= maxPixels ) {
31 ++i; // the part is small enough, move on
32 } else {
34 // divide the job
35 // splitting the longer coordinate
36 bool xdiv= ( jobs[i].width >= jobs[i].height );
37 int longer= ( xdiv ? jobs[i].width : jobs[i].height );
38 // get the place to split (at least one of the parts will have size 2^q)
39 int bits= log2ceil(longer);
40 int divSize= ( longer >= powers[bits-1]+powers[bits-2]
41 ? powers[bits-1]
42 : powers[bits-2] );
43 // split the job (reusing the splitted-one's space and appending the second one)
44 jobs.push_back(jobs[i]);
45 if (xdiv) {
46 jobs[i].width= divSize; // reducing the width of the first job
47 jobs.back().pixels.shiftMatrix(divSize,0); // shifting the second job
48 jobs.back().width-= divSize; // reducing the width of the second job
49 } else {
50 jobs[i].height= divSize; // reducing the height of the first job
51 jobs.back().pixels.shiftMatrix(0,divSize); // shifting the second job
52 jobs.back().height-= divSize; // reducing the height of the second job
56 // create the modules in the jobs by cloning those from module's settings
57 for (JobIterator job=jobs.begin(); job!=jobs.end(); ++job) {
58 job->ranges= clone(moduleRanges());
59 job->domains= clone(moduleDomains());
60 job->encoder= clone(moduleEncoder());
63 return jobs.size();
66 void MSquarePixels::writeSettings(ostream &file) {
67 ASSERT( moduleRanges() && moduleDomains() && moduleEncoder() );
68 // put settings and ID's of child modules
69 put<Uchar>( file, maxPartSize() );
70 file_saveModuleType( file, ModuleRanges );
71 file_saveModuleType( file, ModuleDomains );
72 file_saveModuleType( file, ModuleEncoder );
73 // put settings of child modules
74 moduleRanges()->writeSettings(file);
75 moduleDomains()->writeSettings(file);
76 moduleEncoder()->writeSettings(file);
79 void MSquarePixels::readSettings(istream &file) {
80 ASSERT( !moduleRanges() && !moduleDomains() && !moduleEncoder() );
81 // get settings and create the right child modules
82 maxPartSize()= get<Uchar>(file);
83 file_loadModuleType( file, ModuleRanges );
84 file_loadModuleType( file, ModuleDomains );
85 file_loadModuleType( file, ModuleEncoder );
86 // get settings of child modules
87 moduleRanges()->readSettings(file);
88 moduleDomains()->readSettings(file);
89 moduleEncoder()->readSettings(file);
92 void MSquarePixels::writeJobs(ostream &file,int phaseBegin,int phaseEnd) {
93 ASSERT( !jobs.empty() && 0<=phaseBegin && phaseBegin<phaseEnd && phaseEnd<=phaseCount() );
94 // if writing phase 0, for each job: write data of domain and range modules
95 if (!phaseBegin)
96 for (JobIterator it=jobs.begin(); it!=jobs.end(); ++it) {
97 STREAM_POS(file);
98 it->ranges->writeData(file);
99 STREAM_POS(file);
100 it->domains->writeData(file);
102 // write all the requested phases of all jobs (phase-sequentially)
103 for (int phase=phaseBegin; phase<phaseEnd; ++phase)
104 for (JobIterator it=jobs.begin(); it!=jobs.end(); ++it)
105 STREAM_POS(file), it->encoder->writeData(file,phase);
108 void MSquarePixels::readJobs(istream &file,int phaseBegin,int phaseEnd) {
109 ASSERT( !jobs.empty() && 0<=phaseBegin && phaseBegin<phaseEnd && phaseEnd<=phaseCount() );
110 // if reading phase 0, for each job: read data of domain and range modules
111 if (!phaseBegin)
112 for (JobIterator it=jobs.begin(); it!=jobs.end(); ++it) {
113 STREAM_POS(file);
114 it->ranges->readData_buildRanges(file,*it);
115 STREAM_POS(file);
116 it->domains->readData(file);
117 it->encoder->initialize(IRoot::Decode,*it);
119 // read all the requested phases of all jobs (phase-sequentially)
120 for (int phase=phaseBegin; phase<phaseEnd; ++phase)
121 for (JobIterator it=jobs.begin(); it!=jobs.end(); ++it)
122 STREAM_POS(file), it->encoder->readData(file,phase);