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 <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 /** @class CompressedImageWriter <fvutils/writers/compressed.h>
00036  * Writer for arbitrarily compressed images.
00037  * This class uses any image compressor to write compressed images to
00038  * a file.
00039  *
00040  * @author Tim Niemueller
00041  */
00042 
00043 /** Constructor.
00044  * @param ic ImageCompressor to use for image compression
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 /** Destructor. */
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 /** Set image compressor.
00124  * Use this method to change the used image compressor at runtime.
00125  * @param ic new image compressor.
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 }