fvraw.cpp

00001
00002 /***************************************************************************
00003  *  fvraw.cpp - writer for FireVision raw files
00004  *
00005  *  Generated: Sat Mar 25 00:15:47 2006
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 <core/exception.h>
00025 #include <fvutils/writers/fvraw.h>
00026
00027 #include <string.h>
00028 #include <stdlib.h>
00029
00030 #include <cstdio>
00031 #include <cerrno>
00032
00033 using namespace fawkes;
00034 
00035 /** File identifier for FvRaw images. */
00036 const unsigned int FvRawWriter::FILE_IDENTIFIER = 0x17559358; // 16
00037 
00038 /** @class FvRawWriter fvraw.h <fvutils/writers/fvraw.h>
00039  * FvRaw Writer implementation.
00040  * This class allows for writing FvRaw images to a file.
00041  */
00042 
00043 /** Constructor. */
00044 FvRawWriter::FvRawWriter()
00045   : Writer("raw")
00046 {
00047   header.file_id = FILE_IDENTIFIER;
00048   header.width = 0;
00049   header.height = 0;
00050   header.colorspace = CS_UNKNOWN;
00051
00052   buffer = NULL;
00053 }
00054
00055 
00056 /** Constructor.
00057  * @param filename file name to write to
00058  * @param width width of image
00059  * @param height height of image
00060  */
00061 FvRawWriter::FvRawWriter(const char *filename,
00062                          unsigned int width, unsigned int height)
00063   : Writer("raw")
00064 {
00065   set_filename(filename);
00066
00067   header.file_id    = FILE_IDENTIFIER;
00068   header.width      = width;
00069   header.height     = height;
00070   header.colorspace = CS_UNKNOWN;
00071
00072   buffer = NULL;
00073 }
00074
00075 
00076 /** Constructor.
00077  * @param filename file name to write to
00078  * @param width width of image
00079  * @param height height of image
00080  * @param colorspace colorspace
00081  * @param buffer buffer
00082  */
00083 FvRawWriter::FvRawWriter(const char *filename,
00084                          unsigned int width, unsigned int height,
00085                          colorspace_t colorspace, unsigned char *buffer)
00086   : Writer("raw")
00087 {
00088   set_filename(filename);
00089
00090   header.file_id    = FILE_IDENTIFIER;
00091   header.width      = width;
00092   header.height     = height;
00093   header.colorspace = colorspace;
00094
00095   this->buffer = buffer;
00096 }
00097
00098 
00099 /** Destructor. */
00100 FvRawWriter::~FvRawWriter()
00101 {
00102 }
00103
00104
00105 void
00106 FvRawWriter::set_dimensions(unsigned int width, unsigned int height)
00107 {
00108   header.width = width;
00109   header.height = height;
00110 }
00111
00112
00113 void
00114 FvRawWriter::set_buffer(colorspace_t cspace, unsigned char *buffer)
00115 {
00116   header.colorspace = cspace;
00117   this->buffer = buffer;
00118 }
00119
00120
00121 void
00122 FvRawWriter::write()
00123 {
00124   if ( strlen(filename) == 0 ) {
00125     throw Exception("Cannot write if no file name given");
00126   }
00127   if ( header.width == 0 ) {
00128     throw Exception("Cannot write if width = 0");
00129   }
00130   if ( header.height == 0 ) {
00131     throw Exception("Cannot write if height = 0");
00132   }
00133   if ( header.colorspace == CS_UNKNOWN ) {
00134     throw Exception("Cannot write if colorspace unknown");
00135   }
00136   if ( buffer == NULL ) {
00137     throw Exception("Cannot write if no buffer set");
00138   }
00139
00140   FILE *imagefile=fopen(filename, "w");
00141   if( imagefile == NULL) {
00142     throw Exception("Cannot not open file for writing");
00143   }
00144
00145   unsigned int buffer_size = colorspace_buffer_size(header.colorspace,
00146                                                     header.width,
00147                                                     header.height);
00148
00149   if ( fwrite((const char *)&header, 1, sizeof(header), imagefile) != sizeof(header) ) {
00150     throw Exception("Cannot write header to file", errno);
00151     fclose(imagefile);
00152   }
00153
00154   if ( fwrite((const char *)buffer, 1, buffer_size, imagefile) != buffer_size ) {
00155     throw Exception("Cannot write data to file", errno);
00156     fclose(imagefile);
00157   }
00158
00159   fclose(imagefile);
00160
00161 }
00162
00163 
00164 /** Get write buffer.
00165  * @return write buffer
00166  */
00167 unsigned char *
00168 FvRawWriter::get_write_buffer()
00169 {
00170   return buffer;
00171 }