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