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 <utils/system/console_colors.h>
28 
29 #include <fvutils/writers/compressed.h>
30 #include <fvutils/compression/imagecompressor.h>
31 
32 #include <cstdlib>
33 #include <cstring>
34 #include <cstdio>
35 
36 namespace firevision {
37 #if 0 /* just to make Emacs auto-indent happy */
38 }
39 #endif
40 
41 /** @class CompressedImageWriter <fvutils/writers/compressed.h>
42  * Writer for arbitrarily compressed images.
43  * This class uses any image compressor to write compressed images to
44  * a file.
45  *
46  * @author Tim Niemueller
47  */
48 
49 /** Constructor.
50  * @param ic ImageCompressor to use for image compression
51  */
53 {
54  width = height = 0;
55  filename = strdup("");
56  cspace = CS_UNKNOWN;
57  buffer = NULL;
58 
59  image_compressor = ic;
60 }
61 
62 
63 /** Destructor. */
65 {
66  free(filename);
67 }
68 
69 
70 void
72 {
73  free(this->filename);
74  this->filename = strdup(filename);
75 
76  if ( image_compressor != NULL ) {
77  image_compressor->set_filename( filename );
78  }
79 }
80 
81 
82 void
84 {
85  this->width = width;
86  this->height = height;
87  if ( image_compressor != NULL ) {
88  image_compressor->set_image_dimensions( width, height );
89  }
90 }
91 
92 
93 void
94 CompressedImageWriter::set_buffer(colorspace_t cspace, unsigned char *buffer)
95 {
96  this->cspace = cspace;
97  this->buffer = buffer;
98  if ( image_compressor != NULL ) {
99  image_compressor->set_image_buffer( cspace, buffer );
100  }
101 }
102 
103 
104 void
106 {
107  if ( image_compressor != NULL ) {
110  image_compressor->compress();
111  } else if ( image_compressor->supports_compression_destination( ImageCompressor::COMP_DEST_MEM ) ) {
113  unsigned int comp_buffer_size = image_compressor->recommended_compressed_buffer_size();
114  unsigned char *comp_buffer = (unsigned char *)malloc(comp_buffer_size);
115  image_compressor->set_destination_buffer( comp_buffer, comp_buffer_size );
116  image_compressor->compress();
117  FILE *f = fopen(filename, "wb");
118  if (fwrite(comp_buffer, image_compressor->compressed_size(), 1, f) != 1) {
119  throw fawkes::FileWriteException(filename, "Failed to write data");
120  }
121  fclose(f);
122  free(comp_buffer);
123  } else {
124  throw fawkes::Exception("Supplied ImageCompressor does neither support compressing "
125  " to file nor to a memory buffer. Cannot write.");
126  }
127  }
128 }
129 
130 
131 /** Set image compressor.
132  * Use this method to change the used image compressor at runtime.
133  * @param ic new image compressor.
134  */
135 void
137 {
138  image_compressor = ic;
139  if ( ic != NULL ) {
140  ic->set_filename( filename );
143  }
144 }
145 
146 } // end namespace firevision
virtual void set_dimensions(unsigned int width, unsigned int height)
Set dimensions of image in pixels.
Definition: compressed.cpp:83
virtual ~CompressedImageWriter()
Destructor.
Definition: compressed.cpp:64
virtual void set_buffer(colorspace_t cspace, unsigned char *buffer)
Set image buffer.
Definition: compressed.cpp:94
colorspace_t cspace
The colorspace of the image.
Definition: writer.h:55
write compressed image to file
virtual void set_image_dimensions(unsigned int width, unsigned int height)=0
Set dimensions of image to compress.
virtual void set_destination_buffer(unsigned char *buf, unsigned int buf_size)=0
Set destination buffer (if compressing to memory).
CompressedImageWriter(ImageCompressor *ic=NULL)
Constructor.
Definition: compressed.cpp:52
virtual bool supports_compression_destination(CompressionDestination cd)=0
Check if compressor supports desired compression destination.
virtual void set_image_compressor(ImageCompressor *ic)
Set image compressor.
Definition: compressed.cpp:136
virtual void set_compression_destination(CompressionDestination cd)=0
Set compression destination.
virtual void set_filename(const char *filename)
Set filename.
Definition: compressed.cpp:71
Base class for exceptions in Fawkes.
Definition: exception.h:36
Could not write to file.
Definition: system.h:68
virtual size_t recommended_compressed_buffer_size()=0
Get the recommended size for the compressed buffer.
virtual void set_filename(const char *filename)=0
Set file name.
write compressed image to buffer in memory
unsigned int width
The width of the image.
Definition: writer.h:52
unsigned char * buffer
The image-buffer.
Definition: writer.h:57
virtual void write()
Write to file.
Definition: compressed.cpp:105
Image compressor interface.
unsigned int height
The height of the image.
Definition: writer.h:53
char * filename
The complete filename.
Definition: writer.h:48
virtual size_t compressed_size()=0
Get compressed size.
virtual void compress()=0
Compress image.
virtual void set_image_buffer(colorspace_t cspace, unsigned char *buffer)=0
Set image buffer to compress.