Exemplo n.º 1
0
void MagickImageLoader::convertImageDpi(Magick::Image& image) {
    if (image.magick() == "PDF" | image.magick() == "SVG" || image.magick() == "DJVU") {
        //change from default 72 dpi
        image.resolutionUnits(Magick::PixelsPerInchResolution);
        image.density(Magick::Geometry(MIN_DPI_FOR_VECTOR_FORMAT, MIN_DPI_FOR_VECTOR_FORMAT));
    }
}
Exemplo n.º 2
0
bool ResourceManager::AddImage(const boost::filesystem::path& path,
							   const std::string& imgname,
							   const float width,
							   const float height,
							   const std::string& key) {
	if(!boost::filesystem::is_regular_file(path/imgname)) {
		Logger::Critical(LogOrigin::RESOURCEMANAGER, "Tried loading image path'"+(path/imgname).string()+"' but this image path doesn't exist!");
		exit(1);
	}

    // create Original file path
    std::string originalFile = (path / imgname).string();


    // if the optional param key is not given, use the basename as key
    std::string image_key = "";
    if(key == "") {
        image_key = boost::filesystem::basename(originalFile);
    } else {
        image_key = key;
    }

    // Create Cache Paths
	boost::filesystem::path cacheDir = (path / "cached").string();
    std::string cacheFile = (cacheDir / image_key ).string()+".png";

    // if an image with that key already exists in the dictionary, return
    if(mImages.count(image_key) != 0) {
        return true;
    }

	// Log output
	Logger::Debug(LogOrigin::RESOURCEMANAGER, "Caching image " + originalFile);

	// Create cache directory
	boost::filesystem::create_directory(cacheDir.string());

	// Load, convert and save image (originalFile > cacheFile)
	Magick::Image mimage;
	mimage.backgroundColor(Magick::Color(0, 0, 0, 65535));
	mimage.density(Magick::Geometry(144, 144));
	mimage.read(originalFile);
	// Conver floats to view pixels so that images will always be at the same scale
	const Vector2D vec(Coordinates::WorldFloatToWorldPixel(Vector2D(width, height)));
	mimage.zoom(Magick::Geometry(vec.x, vec.y));
	mimage.depth(8);
	mimage.write(cacheFile);

	// Load cached file
	sf::Image sfimage;
	sfimage.LoadFromFile(cacheFile);
	sfimage.SetSmooth(true);

	// Save loaded Image in Dictionary
	// mImages.insert(new std::pair<std::string, sf::Image>(image_key, sfimage));
	mImages[image_key] = sfimage;

    return true;
}
Exemplo n.º 3
0
void MagickImageLoader::convertImageToDib(Magick::Image& image, Magick::Blob& blob) {
    image.magick("DIB");

    if(image.xResolution() == 0 || image.yResolution() == 0) {
        image.resolutionUnits(Magick::PixelsPerInchResolution);
        image.density(Magick::Geometry(75, 75));
    }

    image.write(&blob);
}
Exemplo n.º 4
0
ImagePtr MagickImageLoader::load(const ImageURL& url)
{
    try {
        Magick::Image image;
        image.density(Magick::Geometry(10, 10));
        image.ping(url.path());
        convertImageDpi(image);
        image.read(url.path());
        return load(image);
    } catch (Magick::Exception &e) {
        cfError() << e.what();
        throw Exception("[MagickImageLoader::load] failed");
    }
}
Exemplo n.º 5
0
ImagePtr MagickImageLoader::load(std::istream& stream) {
    if (stream.fail())
        throw Exception("[MagickImageLoader::load] invalid stream given");

    long int stream_size = streamSize(stream);
    if (stream_size < 1)
        throw Exception("[MagickImageLoader::load] empty stream given");

    boost::scoped_array<char> tmp(new char[stream_size]);
    Magick::Blob blob;
    blob.updateNoCopy(tmp.get(), stream_size);
    try {
        Magick::Image image;
        image.density("10");
        image.ping(blob);
        convertImageDpi(image);
        image.read(blob);

        return load(image);
    } catch (Magick::Exception &e) {
        cfError(MODULE_RDIB) << e.what();
        throw Exception("[MagickImageLoader::load] failed");
    }
}
Exemplo n.º 6
0
bool ResourceManager::AddTexture(const boost::filesystem::path& path,
							   const std::string& imgname,
							   const float width,
							   const float height) {

	if(!boost::filesystem::is_regular_file(path/imgname)) {
		std::cerr << "Tried loading image path '" << (path/imgname).string() << "' but this image path doesn't exist!" << std::endl;
		exit(1);
	}

	// convert coords
	const Vector2D size(Coordinates::WorldFloatToWorldPixel(Vector2D(width, height)));

    // create Original file path
    std::string originalFile = (path / imgname).string();

    // if the optional param key is not given, use the basename as key
	std::string image_key = boost::filesystem::basename(originalFile);

    // Create Cache Paths
	boost::filesystem::path cacheDir = (path / "cached").string();
    std::string cacheFile = (cacheDir / image_key ).string()+".png";

    // if an image with that key already exists in the dictionary, return
    if(mTextures.count(image_key) != 0) {
        return true;
    }

	sf::Texture sftexture;
	bool cache = true;

	if(boost::filesystem::is_regular_file(cacheFile)) {
		// Load cached file
		bool success = sftexture.LoadFromFile(cacheFile);
		if (success && (int)sftexture.GetHeight() == (int)size.x && (int)sftexture.GetWidth() == (int)size.y) {
			cache = false;
			std::cout << "Texture " << originalFile << " already exists. Not caching. "<< std::endl;
		} else if (success) {
			std::cout << "Texture " << originalFile << " does not exist in the resolution "
					<< size.x << "x" << size.y << " but in " << sftexture.GetHeight() << "x" << sftexture.GetWidth() << "." << std::endl;
		}
	}

	if(cache){
		std::cout << ":: Caching image " << originalFile << std::endl;

		// Create cache directory
		boost::filesystem::create_directory(cacheDir.string());

		// Load, convert and save image (originalFile > cacheFile)
		Magick::Image mimage;
		mimage.backgroundColor(Magick::Color(0, 0, 0, 65535));
		mimage.density(Magick::Geometry(144, 144));
		mimage.read(originalFile);


		mimage.zoom(Magick::Geometry(std::max(size.x,size.y), std::max(size.x,size.y)));
		mimage.depth(8);
		mimage.write(cacheFile);

		// Load cached file
		sftexture.LoadFromFile(cacheFile);
	}


	sftexture.SetSmooth(true);


	//std::cout << "  Added image: "<<image_key << std::endl;
	// Save loaded Texture in Dictionary
	mTextures[image_key] = sftexture;

    return true;
}
Exemplo n.º 7
0
void Magick::densityImage::operator()( Magick::Image &image_ ) const
{
  image_.density( _geomery );
}
Exemplo n.º 8
0
void Magick::densityImage::operator()( Magick::Image &image_ ) const
{
  image_.density( _point );
}