00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <core/exception.h>
00026 #include <core/exceptions/system.h>
00027 #include <utils/system/console_colors.h>
00028
00029 #include <fvutils/writers/compressed.h>
00030 #include <fvutils/compression/imagecompressor.h>
00031
00032 #include <cstdlib>
00033 #include <cstring>
00034 #include <cstdio>
00035
00036 namespace firevision {
00037 #if 0
00038 }
00039 #endif
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 CompressedImageWriter::CompressedImageWriter(ImageCompressor *ic)
00053 {
00054 width = height = 0;
00055 filename = strdup("");
00056 cspace = CS_UNKNOWN;
00057 buffer = NULL;
00058
00059 image_compressor = ic;
00060 }
00061
00062
00063
00064 CompressedImageWriter::~CompressedImageWriter()
00065 {
00066 free(filename);
00067 }
00068
00069
00070 void
00071 CompressedImageWriter::set_filename(const char *filename)
00072 {
00073 free(this->filename);
00074 this->filename = strdup(filename);
00075
00076 if ( image_compressor != NULL ) {
00077 image_compressor->set_filename( filename );
00078 }
00079 }
00080
00081
00082 void
00083 CompressedImageWriter::set_dimensions(unsigned int width, unsigned int height)
00084 {
00085 this->width = width;
00086 this->height = height;
00087 if ( image_compressor != NULL ) {
00088 image_compressor->set_image_dimensions( width, height );
00089 }
00090 }
00091
00092
00093 void
00094 CompressedImageWriter::set_buffer(colorspace_t cspace, unsigned char *buffer)
00095 {
00096 this->cspace = cspace;
00097 this->buffer = buffer;
00098 if ( image_compressor != NULL ) {
00099 image_compressor->set_image_buffer( cspace, buffer );
00100 }
00101 }
00102
00103
00104 void
00105 CompressedImageWriter::write()
00106 {
00107 if ( image_compressor != NULL ) {
00108 if ( image_compressor->supports_compression_destination( ImageCompressor::COMP_DEST_FILE ) ) {
00109 image_compressor->set_compression_destination( ImageCompressor::COMP_DEST_FILE );
00110 image_compressor->compress();
00111 } else if ( image_compressor->supports_compression_destination( ImageCompressor::COMP_DEST_MEM ) ) {
00112 image_compressor->set_compression_destination( ImageCompressor::COMP_DEST_MEM );
00113 unsigned int comp_buffer_size = image_compressor->recommended_compressed_buffer_size();
00114 unsigned char *comp_buffer = (unsigned char *)malloc(comp_buffer_size);
00115 image_compressor->set_destination_buffer( comp_buffer, comp_buffer_size );
00116 image_compressor->compress();
00117 FILE *f = fopen(filename, "wb");
00118 if (fwrite(comp_buffer, image_compressor->compressed_size(), 1, f) != 1) {
00119 throw fawkes::FileWriteException(filename, "Failed to write data");
00120 }
00121 fclose(f);
00122 free(comp_buffer);
00123 } else {
00124 throw fawkes::Exception("Supplied ImageCompressor does neither support compressing "
00125 " to file nor to a memory buffer. Cannot write.");
00126 }
00127 }
00128 }
00129
00130
00131
00132
00133
00134
00135 void
00136 CompressedImageWriter::set_image_compressor(ImageCompressor *ic)
00137 {
00138 image_compressor = ic;
00139 if ( ic != NULL ) {
00140 ic->set_filename( filename );
00141 ic->set_image_dimensions( width, height );
00142 ic->set_image_buffer(cspace, buffer);
00143 }
00144 }
00145
00146 }