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