writer.cpp

00001
00002 /***************************************************************************
00003  *  writer.cpp - Writer interface
00004  *
00005  *  Generated: Tue Mar 27 17:24:55 2007
00006  *  Copyright  2005-2007  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023
00024 #include <fvutils/writers/writer.h>
00025
00026 #include <core/exception.h>
00027 #include <core/exceptions/system.h>
00028
00029 #include <cstring>
00030 #include <cstdlib>
00031 #include <cstdio>
00032 
00033 /** @class Writer <fvutils/writers/writer.h>
00034  * Interface to write images.
00035  * The writer interface defines the general API for image writers. These
00036  * writers are used to write images to files on your harddrive (like JPEGs,
00037  * PNGs etc.).
00038  *
00039  * @author Tim Niemueller
00040  */
00041 
00042 /** @fn void Writer::write()
00043  * Write to file.
00044  */
00045 
00046 /** @var Writer::filename
00047  * The complete filename.
00048  */
00049 
00050 /** @var Writer::basename
00051  * The basename of the file.
00052  */
00053 /** @var Writer::extension
00054  * The extension of the file.
00055  */
00056 /** @var Writer::width
00057  * The width of the image.
00058  */
00059 /** @var Writer::height
00060  * The height of the image.
00061  */
00062 /** @var Writer::cspace
00063  * The colorspace of the image.
00064  */
00065 /** @var Writer::buffer
00066  * The image-buffer.
00067  */
00068 
00069 /** Constructor.
00070  * @param extension the file extension
00071  */
00072 Writer::Writer(const char *extension)
00073 {
00074   basename = 0;
00075   filename = 0;
00076
00077   this->extension = 0;
00078   if (0 != extension) {
00079     this->extension = strdup(extension);
00080   }
00081
00082   width = 0;
00083   height = 0;
00084   cspace = CS_UNKNOWN;
00085   buffer = 0;
00086 }
00087 
00088 /** Virtual empty destructor. */
00089 Writer::~Writer()
00090 {
00091   free(filename);
00092   free(basename);
00093   free(extension);
00094 }
00095 
00096 /** Set filename.
00097  * @param filename name of file to write to. This can either be the complete filename
00098  * (including) extension or the basename only in which case the extension is added.
00099  */
00100 void
00101 Writer::set_filename(const char *filename)
00102 {
00103   free(this->filename);
00104
00105   if ( 0 != strstr(filename, ".") ) {
00106     this->filename = strdup(filename);
00107   } else {
00108     free(this->basename);
00109     this->basename = strdup(filename);
00110
00111     // re-generate complete filename
00112     if (0 == extension) {
00113       throw fawkes::Exception("Extension not set");
00114     }
00115
00116     if (asprintf(&(this->filename), "%s.%s", basename, extension) == -1) {
00117       throw fawkes::OutOfMemoryException("Writer::set_filename(): asprintf() failed");
00118     }
00119   }
00120 }
00121 
00122 /** Set dimensions of image in pixels.
00123  * @param width width of image in pixels
00124  * @param height height of image in pixels.
00125  */
00126 void
00127 Writer::set_dimensions(unsigned int width, unsigned int height)
00128 {
00129   this->width = width;
00130   this->height = height;
00131 }
00132 
00133 /** Set image buffer.
00134  * @param cspace color space of image
00135  * @param buffer buffer of image
00136  */
00137 void
00138 Writer::set_buffer(colorspace_t cspace, unsigned char *buffer)
00139 {
00140   this->cspace = cspace;
00141   this->buffer = buffer;
00142 }
00143 
00144 /** Set the filename extension for file written by this writer.
00145  * @param extension the extension
00146  */
00147 void
00148 Writer::set_extension(const char *extension)
00149 {
00150   free(this->extension);
00151   this->extension = strdup(extension);
00152
00153   // re-generate complete filename
00154   free(this->filename);
00155   this->filename = (char *) malloc( strlen(basename) + strlen(extension) + 1 );
00156   strcpy(filename, basename);
00157   strcat(this->filename, ".");
00158   strcat(filename, extension);
00159 }