surface composer::compose(const std::string &src, const std::string &dest)
{
	cutter cut;
	cut.set_verbose(verbose_);

	const config src_conf = cut.load_config(src);
	const config dest_conf = cut.load_config(dest);

	if(verbose_) {
		std::cerr << "Loading masks...\n";
	}
	cut.load_masks(src_conf);
	cut.load_masks(dest_conf);

	if(verbose_) {
		std::cerr << "Loading images...\n";
	}
	const surface src_surface(make_neutral_surface(IMG_Load(src.c_str())));
	if(src_surface == NULL)
		throw exploder_failure("Unable to load the source image " + src);

	const surface dest_surface(make_neutral_surface(IMG_Load(dest.c_str())));
	if(dest_surface == NULL)
		throw exploder_failure("Unable to load the destination image " + dest);

	if(verbose_) {
		std::cerr << "Cutting images...\n";
	}
	const cutter::surface_map src_surfaces = cut.cut_surface(src_surface, src_conf);
	const cutter::surface_map dest_surfaces = cut.cut_surface(dest_surface, dest_conf);

	for(cutter::surface_map::const_iterator itor = dest_surfaces.begin();
			itor != dest_surfaces.end(); ++itor) {

		const std::string& name = itor->second.name;

		if(src_surfaces.find(name) == src_surfaces.end())
			continue;

		const cutter::positioned_surface& src_ps = src_surfaces.find(name)->second;
		const cutter::positioned_surface& dest_ps = itor->second;

		if(!image_empty(dest_ps.image)) {
			if(interactive_) {
				//TODO: make "interactive" mode work
			} else {
				std::cerr << "Warning: element " << name << " not empty on destination image\n";
			}
		}
		if(verbose_) {
			std::cerr << "Inserting image " << name
				<< " on position (" << dest_ps.pos.x
				<< ", " << dest_ps.pos.y << ")\n";
		}
		masked_overwrite_surface(dest_surface, src_ps.image,
				src_ps.mask.image,
				dest_ps.pos.x, dest_ps.pos.y);
	}

	return dest_surface;
}
Beispiel #2
0
int main(int argc, char* argv[])
{
	std::string src;
	std::string dest_dir;
	cutter cut;

	// Parse arguments that shouldn't require a display device
	int arg;
	for(arg = 1; arg != argc; ++arg) {
		const std::string val(argv[arg]);
		if(val.empty()) {
			continue;
		}

		if(val == "--help" || val == "-h") {
			print_usage(argv[0]);
			return 0;
		} else if(val == "--verbose" || val == "-v") {
			cut.set_verbose(true);
		} else if(val == "--directory" || val == "-d" ) {
			game_config::path = argv[++arg];
		} else {
			if(src.empty()) {
				src = val;
			} else if(dest_dir.empty()) {
				dest_dir = val;
			} else {
				print_usage(argv[0]);
				return 1;
			}
		}
	}

	if(src.empty() || dest_dir.empty()) {
		print_usage(argv[0]);
		return 1;
	}

	try {
		const config conf = cut.load_config(src);
		cut.load_masks(conf);

		const surface src_surface(make_neutral_surface(IMG_Load(src.c_str())));
		if(src_surface == NULL)
			throw exploder_failure("Unable to load the source image " + src);

		const cutter::surface_map surfaces = cut.cut_surface(src_surface, conf);

		for(cutter::surface_map::const_iterator itor = surfaces.begin();
				itor != surfaces.end(); ++itor) {
			const cutter::mask &mask = itor->second.mask;

			surface surf = create_compatible_surface(
					  itor->second.image
					, mask.cut.w
					, mask.cut.h);

			masked_overwrite_surface(surf, itor->second.image, mask.image,
					mask.cut.x - mask.shift.x, mask.cut.y - mask.shift.y);

			save_image(surf, dest_dir + "/" + mask.name + ".png");
		}

	} catch(exploder_failure& err) {
		std::cerr << "Failed: " << err.message << "\n";
		return 1;
	}

	return 0;
}