compressed.cpp

00001 
00002 /***************************************************************************
00003  *  compressed_image_writer.cpp - Write image arbitrarily compressed with an
00004  *                                ImageCompressor
00005  *
00006  *  Generated: Sat Aug 12 14:03:08 2006
00007  *  Copyright  2005-2007  Tim Niemueller [www.niemueller.de]
00008  *
00009  ****************************************************************************/
00010 
00011 /*  This program is free software; you can redistribute it and/or modify
00012  *  it under the terms of the GNU General Public License as published by
00013  *  the Free Software Foundation; either version 2 of the License, or
00014  *  (at your option) any later version. A runtime exception applies to
00015  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00016  *
00017  *  This program is distributed in the hope that it will be useful,
00018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  *  GNU Library General Public License for more details.
00021  *
00022  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
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 /* just to make Emacs auto-indent happy */
00038 }
00039 #endif
00040 
00041 /** @class CompressedImageWriter <fvutils/writers/compressed.h>
00042  * Writer for arbitrarily compressed images.
00043  * This class uses any image compressor to write compressed images to
00044  * a file.
00045  *
00046  * @author Tim Niemueller
00047  */
00048 
00049 /** Constructor.
00050  * @param ic ImageCompressor to use for image compression
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 /** Destructor. */
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 /** Set image compressor.
00132  * Use this method to change the used image compressor at runtime.
00133  * @param ic new image compressor.
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 } // end namespace firevision

Generated on Tue Feb 22 13:31:17 2011 for Fawkes API by  doxygen 1.4.7