Fawkes API  Fawkes Development Version
compressed.cpp
1 
2 /***************************************************************************
3  * compressed_image_writer.cpp - Write image arbitrarily compressed with an
4  * ImageCompressor
5  *
6  * Generated: Sat Aug 12 14:03:08 2006
7  * Copyright 2005-2007 Tim Niemueller [www.niemueller.de]
8  *
9  ****************************************************************************/
10 
11 /* This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version. A runtime exception applies to
15  * this software (see LICENSE.GPL_WRE file mentioned below for details).
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Library General Public License for more details.
21  *
22  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
23  */
24 
25 #include <core/exception.h>
26 #include <core/exceptions/system.h>
27 #include <fvutils/compression/imagecompressor.h>
28 #include <fvutils/writers/compressed.h>
29 #include <utils/system/console_colors.h>
30 
31 #include <cstdio>
32 #include <cstdlib>
33 #include <cstring>
34 
35 namespace firevision {
36 
37 /** @class CompressedImageWriter <fvutils/writers/compressed.h>
38  * Writer for arbitrarily compressed images.
39  * This class uses any image compressor to write compressed images to
40  * a file.
41  *
42  * @author Tim Niemueller
43  */
44 
45 /** Constructor.
46  * @param ic ImageCompressor to use for image compression
47  */
49 {
50  width = height = 0;
51  filename = strdup("");
52  cspace = CS_UNKNOWN;
53  buffer = NULL;
54 
55  image_compressor = ic;
56 }
57 
58 /** Destructor. */
60 {
61  free(filename);
62 }
63 
64 void
65 CompressedImageWriter::set_filename(const char *filename)
66 {
67  free(this->filename);
68  this->filename = strdup(filename);
69 
70  if (image_compressor != NULL) {
71  image_compressor->set_filename(filename);
72  }
73 }
74 
75 void
76 CompressedImageWriter::set_dimensions(unsigned int width, unsigned int height)
77 {
78  this->width = width;
79  this->height = height;
80  if (image_compressor != NULL) {
81  image_compressor->set_image_dimensions(width, height);
82  }
83 }
84 
85 void
86 CompressedImageWriter::set_buffer(colorspace_t cspace, unsigned char *buffer)
87 {
88  this->cspace = cspace;
89  this->buffer = buffer;
90  if (image_compressor != NULL) {
91  image_compressor->set_image_buffer(cspace, buffer);
92  }
93 }
94 
95 void
97 {
98  if (image_compressor != NULL) {
101  image_compressor->compress();
104  unsigned int comp_buffer_size = image_compressor->recommended_compressed_buffer_size();
105  unsigned char *comp_buffer = (unsigned char *)malloc(comp_buffer_size);
106  image_compressor->set_destination_buffer(comp_buffer, comp_buffer_size);
107  image_compressor->compress();
108  FILE *f = fopen(filename, "wb");
109  if (fwrite(comp_buffer, image_compressor->compressed_size(), 1, f) != 1) {
110  throw fawkes::FileWriteException(filename, "Failed to write data");
111  }
112  fclose(f);
113  free(comp_buffer);
114  } else {
115  throw fawkes::Exception("Supplied ImageCompressor does neither support compressing "
116  " to file nor to a memory buffer. Cannot write.");
117  }
118  }
119 }
120 
121 /** Set image compressor.
122  * Use this method to change the used image compressor at runtime.
123  * @param ic new image compressor.
124  */
125 void
127 {
128  image_compressor = ic;
129  if (ic != NULL) {
130  ic->set_filename(filename);
131  ic->set_image_dimensions(width, height);
132  ic->set_image_buffer(cspace, buffer);
133  }
134 }
135 
136 } // end namespace firevision
firevision::ImageCompressor::supports_compression_destination
virtual bool supports_compression_destination(CompressionDestination cd)=0
firevision::Writer::buffer
unsigned char * buffer
Definition: writer.h:63
firevision::ImageCompressor::COMP_DEST_MEM
write compressed image to buffer in memory
Definition: imagecompressor.h:52
firevision::ImageCompressor::recommended_compressed_buffer_size
virtual size_t recommended_compressed_buffer_size()=0
firevision::Writer::cspace
colorspace_t cspace
Definition: writer.h:61
firevision::ImageCompressor::COMP_DEST_FILE
write compressed image to file
Definition: imagecompressor.h:51
firevision::CompressedImageWriter::set_dimensions
virtual void set_dimensions(unsigned int width, unsigned int height)
Set dimensions of image in pixels.
Definition: compressed.cpp:81
firevision::ImageCompressor::set_compression_destination
virtual void set_compression_destination(CompressionDestination cd)=0
firevision::CompressedImageWriter::set_filename
virtual void set_filename(const char *filename)
Set filename.
Definition: compressed.cpp:70
firevision::ImageCompressor::set_image_buffer
virtual void set_image_buffer(colorspace_t cspace, unsigned char *buffer)=0
firevision::CompressedImageWriter::write
virtual void write()
Definition: compressed.cpp:101
firevision::ImageCompressor::set_image_dimensions
virtual void set_image_dimensions(unsigned int width, unsigned int height)=0
firevision::CompressedImageWriter::set_image_compressor
virtual void set_image_compressor(ImageCompressor *ic)
Set image compressor.
Definition: compressed.cpp:131
firevision::ImageCompressor::compressed_size
virtual size_t compressed_size()=0
firevision::ImageCompressor::compress
virtual void compress()=0
firevision::Writer::filename
char * filename
Definition: writer.h:54
firevision::CompressedImageWriter::CompressedImageWriter
CompressedImageWriter(ImageCompressor *ic=NULL)
Constructor.
Definition: compressed.cpp:53
firevision::ImageCompressor::set_destination_buffer
virtual void set_destination_buffer(unsigned char *buf, unsigned int buf_size)=0
fawkes::FileWriteException
Definition: system.h:72
firevision::CompressedImageWriter::~CompressedImageWriter
virtual ~CompressedImageWriter()
Destructor.
Definition: compressed.cpp:64
firevision::ImageCompressor::set_filename
virtual void set_filename(const char *filename)=0
firevision::CompressedImageWriter::set_buffer
virtual void set_buffer(colorspace_t cspace, unsigned char *buffer)
Set image buffer.
Definition: compressed.cpp:91
firevision::Writer::height
unsigned int height
Definition: writer.h:59
firevision::Writer::width
unsigned int width
Definition: writer.h:58
fawkes::Exception
Definition: exception.h:39