Blender V2.61 - r43446

tile.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright 2011, Blender Foundation.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  */
00018 
00019 #include "tile.h"
00020 
00021 #include "util_algorithm.h"
00022 
00023 CCL_NAMESPACE_BEGIN
00024 
00025 TileManager::TileManager(bool progressive_, int samples_, int tile_size_, int min_size_)
00026 {
00027     progressive = progressive_;
00028     tile_size = tile_size_;
00029     min_size = min_size_;
00030 
00031     BufferParams buffer_params;
00032     reset(buffer_params, 0);
00033 }
00034 
00035 TileManager::~TileManager()
00036 {
00037 }
00038 
00039 void TileManager::reset(BufferParams& params_, int samples_)
00040 {
00041     params = params_;
00042 
00043     start_resolution = 1;
00044 
00045     int w = params.width, h = params.height;
00046 
00047     if(min_size != INT_MAX) {
00048         while(w*h > min_size*min_size) {
00049             w = max(1, w/2); 
00050             h = max(1, h/2); 
00051 
00052             start_resolution *= 2;
00053         }
00054     }
00055 
00056     samples = samples_;
00057 
00058     state.buffer = BufferParams();
00059     state.sample = -1;
00060     state.resolution = start_resolution;
00061     state.tiles.clear();
00062 }
00063 
00064 void TileManager::set_samples(int samples_)
00065 {
00066     samples = samples_;
00067 }
00068 
00069 void TileManager::set_tiles()
00070 {
00071     int resolution = state.resolution;
00072     int image_w = max(1, params.width/resolution);
00073     int image_h = max(1, params.height/resolution);
00074     int tile_w = (tile_size >= image_w)? 1: (image_w + tile_size - 1)/tile_size;
00075     int tile_h = (tile_size >= image_h)? 1: (image_h + tile_size - 1)/tile_size;
00076     int sub_w = image_w/tile_w;
00077     int sub_h = image_h/tile_h;
00078 
00079     state.tiles.clear();
00080 
00081     for(int tile_y = 0; tile_y < tile_h; tile_y++) {
00082         for(int tile_x = 0; tile_x < tile_w; tile_x++) {
00083             int x = tile_x * sub_w;
00084             int y = tile_y * sub_h;
00085             int w = (tile_x == tile_w-1)? image_w - x: sub_w;
00086             int h = (tile_y == tile_h-1)? image_h - y: sub_h;
00087 
00088             state.tiles.push_back(Tile(x, y, w, h));
00089         }
00090     }
00091 
00092     state.buffer.width = image_w;
00093     state.buffer.height = image_h;
00094 
00095     state.buffer.full_x = params.full_x/resolution;
00096     state.buffer.full_y = params.full_y/resolution;
00097     state.buffer.full_width = max(1, params.full_width/resolution);
00098     state.buffer.full_height = max(1, params.full_height/resolution);
00099 }
00100 
00101 bool TileManager::done()
00102 {
00103     return (state.sample+1 >= samples && state.resolution == 1);
00104 }
00105 
00106 bool TileManager::next()
00107 {
00108     if(done())
00109         return false;
00110 
00111     if(progressive && state.resolution > 1) {
00112         state.sample = 0;
00113         state.resolution /= 2;
00114         set_tiles();
00115     }
00116     else {
00117         state.sample++;
00118         state.resolution = 1;
00119         set_tiles();
00120     }
00121 
00122     return true;
00123 }
00124 
00125 CCL_NAMESPACE_END
00126