00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00037 }
00038 #endif
00039
00040
00041 const unsigned int FvRawWriter::FILE_IDENTIFIER = 0x17559358;
00042
00043
00044
00045
00046
00047
00048
00049
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
00063
00064
00065
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
00083
00084
00085
00086
00087
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
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
00171
00172
00173 unsigned char *
00174 FvRawWriter::get_write_buffer()
00175 {
00176 return buffer;
00177 }
00178
00179 }