Fawkes API Fawkes Development Version

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 namespace firevision {
00034 #if 0 /* just to make Emacs auto-indent happy */
00035 }
00036 #endif
00037 
00038 /** @class Writer <fvutils/writers/writer.h>
00039  * Interface to write images.
00040  * The writer interface defines the general API for image writers. These
00041  * writers are used to write images to files on your harddrive (like JPEGs,
00042  * PNGs etc.).
00043  *
00044  * @author Tim Niemueller
00045  */
00046 
00047 /** @fn void Writer::write()
00048  * Write to file.
00049  */
00050 
00051 /** @var Writer::filename
00052  * The complete filename.
00053  */
00054 
00055 /** @var Writer::basename
00056  * The basename of the file.
00057  */
00058 /** @var Writer::extension
00059  * The extension of the file.
00060  */
00061 /** @var Writer::width
00062  * The width of the image.
00063  */
00064 /** @var Writer::height
00065  * The height of the image.
00066  */
00067 /** @var Writer::cspace
00068  * The colorspace of the image.
00069  */
00070 /** @var Writer::buffer
00071  * The image-buffer.
00072  */
00073 
00074 /** Constructor.
00075  * @param extension the file extension
00076  */
00077 Writer::Writer(const char *extension)
00078 {
00079   basename = 0;
00080   filename = 0;
00081 
00082   this->extension = 0;
00083   if (0 != extension) {
00084     this->extension = strdup(extension);
00085   }
00086 
00087   width = 0;
00088   height = 0;
00089   cspace = CS_UNKNOWN;
00090   buffer = 0;
00091 }
00092 
00093 /** Virtual empty destructor. */
00094 Writer::~Writer()
00095 {
00096   free(filename);
00097   free(basename);
00098   free(extension);
00099 }
00100 
00101 /** Set filename.
00102  * @param filename name of file to write to. This can either be the complete filename
00103  * (including) extension or the basename only in which case the extension is added.
00104  */
00105 void
00106 Writer::set_filename(const char *filename)
00107 {
00108   free(this->filename);
00109   
00110   if ( 0 != strstr(filename, ".") ) {
00111     this->filename = strdup(filename);
00112   } else {
00113     free(this->basename);
00114     this->basename = strdup(filename);
00115 
00116     // re-generate complete filename
00117     if (0 == extension) {
00118       throw fawkes::Exception("Extension not set");
00119     }
00120 
00121     if (asprintf(&(this->filename), "%s.%s", basename, extension) == -1) {
00122       throw fawkes::OutOfMemoryException("Writer::set_filename(): asprintf() failed");
00123     }
00124   }
00125 }
00126 
00127 /** Set dimensions of image in pixels.
00128  * @param width width of image in pixels
00129  * @param height height of image in pixels.
00130  */
00131 void
00132 Writer::set_dimensions(unsigned int width, unsigned int height)
00133 {
00134   this->width = width;
00135   this->height = height;
00136 }
00137 
00138 /** Set image buffer.
00139  * @param cspace color space of image
00140  * @param buffer buffer of image
00141  */
00142 void
00143 Writer::set_buffer(colorspace_t cspace, unsigned char *buffer)
00144 {
00145   this->cspace = cspace;
00146   this->buffer = buffer;
00147 }
00148 
00149 /** Set the filename extension for file written by this writer.
00150  * @param extension the extension
00151  */
00152 void
00153 Writer::set_extension(const char *extension)
00154 {
00155   free(this->extension);
00156   this->extension = strdup(extension);
00157 
00158   // re-generate complete filename
00159   free(this->filename);
00160   this->filename = (char *) malloc( strlen(basename) + strlen(extension) + 1 );
00161   strcpy(filename, basename);
00162   strcat(this->filename, ".");
00163   strcat(filename, extension);
00164 }
00165 
00166 } // end namespace firevision
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends