void SampleRenderer::render(CtxG& ctx) { this->pixel_pos_idx = this->sampler->request_sample_2d(); this->preprocess(ctx, *this->scene, *this->sampler); StatTimer t(TIMER_RENDER); Vec2i layout_tiles = Renderer::layout_tiles(ctx, *this->film, *this->sampler); Vec2 tile_size = Vec2(float(this->film->x_res), float(this->film->y_res)) / Vec2(float(layout_tiles.x), float(layout_tiles.y)); uint32_t job_count = layout_tiles.x * layout_tiles.y; stat_sample_int(DISTRIB_INT_RENDER_JOBS, job_count); std::vector<std::shared_ptr<Sampler>> samplers; for(uint32_t i = 0; i < job_count; ++i) { samplers.push_back(this->sampler->split()); } std::mutex film_mutex; fork_join(*ctx.pool, job_count, [&](uint32_t job_i) { uint32_t tile_x = job_i % layout_tiles.x; uint32_t tile_y = job_i / layout_tiles.x; Vec2 corner_0f = tile_size * Vec2(float(tile_x), float(tile_y)); Vec2 corner_1f = corner_0f + tile_size; Recti tile_rect(floor_vec2i(corner_0f), floor_vec2i(corner_1f)); Vec2 margin = this->film->filter.radius; Recti film_rect(floor_vec2i(corner_0f - margin), ceil_vec2i(corner_1f + margin)); Vec2i film_size = film_rect.p_max - film_rect.p_min; Film tile_film(film_size.x, film_size.y, this->film->filter); this->render_tile(ctx, tile_rect, film_rect, tile_film, *samplers.at(job_i)); std::unique_lock<std::mutex> film_lock(film_mutex); this->film->add_tile(film_rect.p_min, tile_film); }); }
bool MapMode::LoadTileset(const string& _lua_filepath) { rpg_script::ReadScript tileset_script; if (!tileset_script.OpenFile(_lua_filepath)) { PRINT_ERROR << "Failed to open tileset script: " << _lua_filepath << endl; return false; } tileset_script.OpenTable("tileset_data"); string image_name = tileset_script.ReadData<string>("name", ""); string image_path = tileset_script.ReadData<string>("image_path", ""); // cout << "|\tImage Name: " << image_name << endl; // cout << "|\tImage Path: " << image_path << endl; int margin = tileset_script.ReadData<int>("margin", -1); int spacing = tileset_script.ReadData<int>("spacing", -1); int num_cols = tileset_script.ReadData<int>("num_cols", -1); int num_rows = tileset_script.ReadData<int>("num_rows", -1); int tile_width = tileset_script.ReadData<int>("tile_width", -1); int tile_height = tileset_script.ReadData<int>("tile_height", -1); // cout << "|\tMargin / Spacing: " << margin << " / " << spacing << endl; // cout << "|\tSize: " << num_cols << " x " << num_rows << endl; // cout << "|\tTile Size: " << tile_width << " x " << tile_height << endl; if (tileset_script.HasError()) { tileset_script.PrintErrors(); return false; } for (int row = 0; row < num_rows; ++row) { for (int col = 0; col < num_cols; ++col) { sf::IntRect tile_rect(margin + spacing * col + col * tile_width, margin + spacing * row + row * tile_height, 32, 32); int loaded_id = rpg_resource::ResourceManager->LoadImageRect(image_path, tile_rect); if (loaded_id >= 0) tile_supervisor->tile_ids.push_back(loaded_id); else { PRINT_ERROR << "Failed to load tile from image" << endl; return false; } } } return true; }
//************************************************************************************************** //! Intercepts the getTile call intended for the adaptee and sets a mutex lock around the //! adaptee's getTile call. //************************************************************************************************** ossimRefPtr<ossimImageData> ossimImageHandlerMtAdaptor::getTile(const ossimIpt& origin, ossim_uint32 rLevel) { if (!m_adaptedHandler.valid()) return NULL; // Establish tile rect to call overloaded getTile(tile_rect): ossim_uint32 h = m_adaptedHandler->getTileHeight(); ossim_uint32 w = m_adaptedHandler->getTileWidth(); ossimIpt lr (origin.x + w - 1, origin.y + h - 1); ossimIrect tile_rect (origin, lr); // Need to unlock to prevent freezing in the called getTile(): return getTile(tile_rect, rLevel); }
//************************************************************************************************* int main(int argc, char *argv[]) { try { ossimInit::instance()->initialize(argc, argv); ossimFilename fname ("PixelFlipperTestChip.tif"); ossimRefPtr<ossimImageHandler> handler = ossimImageHandlerRegistry::instance()->open(fname); vector<ossimIpt> valid_vertices; valid_vertices.push_back(ossimIpt(0,0)); valid_vertices.push_back(ossimIpt(127,0)); valid_vertices.push_back(ossimIpt(127,127)); valid_vertices.push_back(ossimIpt(0,127)); handler->writeValidImageVertices(valid_vertices); // Fetch raw tile: ossimRefPtr<ossimPixelFlipper> flipper = new ossimPixelFlipper(); ossimIrect tile_rect(0,0,127,127); flipper->connectMyInputTo(handler.get()); ossimRefPtr<ossimImageData> tile = handler->getTile(tile_rect); cout<<"\nRaw Tile:"<<endl; showPixels(tile, expected_0); // Target Replacement: flipper->setTargetRange(0, 10); flipper->setReplacementValue(11); flipper->setReplacementMode(ossimPixelFlipper::REPLACE_BAND_IF_TARGET); tile = flipper->getTile(tile_rect); cout<<"\nTarget Replacement REPLACE_BAND_IF_TARGET:"<<endl; showPixels(tile, expected_1); // Target Replacement: flipper->setTargetRange(0, 10); flipper->setReplacementMode(ossimPixelFlipper::REPLACE_BAND_IF_PARTIAL_TARGET); tile = flipper->getTile(tile_rect); cout<<"\nTarget Replacement REPLACE_BAND_IF_PARTIAL_TARGET:"<<endl; showPixels(tile, expected_2); // Target Replacement: flipper->setTargetRange(0, 10); flipper->setReplacementMode(ossimPixelFlipper::REPLACE_ALL_BANDS_IF_PARTIAL_TARGET); tile = flipper->getTile(tile_rect); cout<<"\nTarget Replacement REPLACE_ALL_BANDS_IF_PARTIAL_TARGET:"<<endl; showPixels(tile, expected_3); // Target Replacement: flipper->setTargetRange(0, 10); flipper->setReplacementMode(ossimPixelFlipper::REPLACE_ONLY_FULL_TARGETS); tile = flipper->getTile(tile_rect); cout<<"\nTarget Replacement REPLACE_ONLY_FULL_TARGETS:"<<endl; showPixels(tile, expected_4); // Clamping: flipper->setClampValues(8, 200); tile = flipper->getTile(tile_rect); cout<<"\nClamping:"<<endl; showPixels(tile, expected_5); // Valid Vertices: valid_vertices.clear(); valid_vertices.push_back(ossimIpt(3,0)); valid_vertices.push_back(ossimIpt(127,0)); valid_vertices.push_back(ossimIpt(127,127)); valid_vertices.push_back(ossimIpt(3,127)); handler->writeValidImageVertices(valid_vertices); flipper->initialize(); flipper->setClipMode(ossimPixelFlipper::VALID_VERTICES); tile = flipper->getTile(tile_rect); cout<<"\nValid Vertices (with clamping):"<<endl; showPixels(tile, expected_6); } catch (const ossimException& e) { ossimNotify(ossimNotifyLevel_DEBUG) << e.what() << std::endl; } cout << "FINISHED" << endl; return 0; }