Česky
Kamil Dudka

GED 2006 (C++)

File detail

Name:Downloadimage.cc [Download]
Location: ged2006 > src
Size:2.6 KB
Last modification:2007-08-29 02:16

Source code

/*
 * File: image.cc - abstract class image
 * Project: GED - bitmap editor (ICP)
 * Author: name, login
 * Team: xdudka00, xfilak01, xhefka00, xhradi08
 * Created: 2006-03-10
 */
 
#include <iostream>
#include <string>
 
#include "image.h"
#include "pngimage.h"
#include "pluginsupport.h"
#include "appwnd.h"
 
using std::string;
 
namespace {
	/*
	 * return file "fileName" extension (empty string if no extension)
	 */
	string getExtension (string fileName)
	{
		string ext;
		string::iterator i= fileName.end()-1;
		string::iterator begin= fileName.begin();
 
		for (; i!=begin && '.' != *i; i--)
			ext = *i + ext;
 
		if ('.' == *i)
			return ext;
		else
			return string();
	}
 
	/*
	 * return name of plugin to open file with extension "strExtension"
	 */
	string getFFPluginName (string &strExtension)
	{
#ifndef NDEBUG
		std::cerr<<"getFFPluginName returned \"" << ("fileformat:." + strExtension) << "\"\n";
#endif
		return "fileformat:." + strExtension;
	}
}
 
/*
 * read image file "szFileName" and create Image object
 */
Image *Image::readImageFromFile (const char *szFileName)
{
	std::string fileName (szFileName);
	string ext = getExtension (fileName);
 
	if ("png"==ext || "PNG"==ext)
		return new PNGImage (fileName);
	else
		return new PluginImage (fileName, getFFPluginName (ext));
}
 
/*
 * Create new image of size "size"
 */
Image::Image (const BoxSize &size):
	_bNewImage (true)
{
	this->name (AppWnd::ptr()->newDocName());
	this->resize (size);
}
 
/*
 * filePattern - return file pattern for fl_file_chooser
 */
string Image::filePattern () const
{
	return string ("Image (*.") + getExtension (_fileName) + ")";
}
 
/*
 * open image "fileName" using plugin "pluginName"
 */
PluginImage::PluginImage (const std::string &fileName, const std::string pluginName):
	Image (fileName), _strPluginName (pluginName)
{
	try {
		// Prevent from using blank fb
		this->size (BoxSize (1,1));
 
		// Args for fileformat plugin
		std::string args ("open ");
		args.append (fileName);
 
		// Plugin call
		PluginList::ptr()->callByName (*this, pluginName, args);
	}
	catch (PluginList::ErrPluginNotFound) {
		throw ErrUnknownFF ();
	}
	catch (PluginList::ErrPluginEC) {
		throw ErrLoad ();
	}
}
 
/*
 * save image with name "fileName"
 */
void PluginImage::save (const std::string &fileName) {
	try {
		// Args for fileformat plugin
		std::string args ("save ");
		args.append (fileName);
 
		// Plugin call
		PluginList::ptr()->callByName (*this, _strPluginName, args);
	}
	catch (PluginList::ErrPluginNotFound) {
		throw ErrSave ();
	}
	catch (PluginList::ErrPluginEC) {
		throw ErrSave ();
	}
 
	// set image name
	this->name (fileName);
}