fvraw.cpp
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
00036 const unsigned int FvRawWriter::FILE_IDENTIFIER = 0x17559358;
00037
00038
00039
00040
00041
00042
00043
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
00057
00058
00059
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
00077
00078
00079
00080
00081
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
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
00165
00166
00167 unsigned char *
00168 FvRawWriter::get_write_buffer()
00169 {
00170 return buffer;
00171 }