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
35namespace 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
64void
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
75void
76CompressedImageWriter::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
85void
86CompressedImageWriter::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
95void
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 */
125void
127{
128 image_compressor = ic;
129 if (ic != NULL) {
133 }
134}
135
136} // end namespace firevision
Base class for exceptions in Fawkes.
Definition: exception.h:36
Could not write to file.
Definition: system.h:69
virtual void set_dimensions(unsigned int width, unsigned int height)
Set dimensions of image in pixels.
Definition: compressed.cpp:76
virtual ~CompressedImageWriter()
Destructor.
Definition: compressed.cpp:59
virtual void set_image_compressor(ImageCompressor *ic)
Set image compressor.
Definition: compressed.cpp:126
CompressedImageWriter(ImageCompressor *ic=NULL)
Constructor.
Definition: compressed.cpp:48
virtual void write()
Write to file.
Definition: compressed.cpp:96
virtual void set_buffer(colorspace_t cspace, unsigned char *buffer)
Set image buffer.
Definition: compressed.cpp:86
virtual void set_filename(const char *filename)
Set filename.
Definition: compressed.cpp:65
Image compressor interface.
virtual void set_image_dimensions(unsigned int width, unsigned int height)=0
Set dimensions of image to compress.
virtual bool supports_compression_destination(CompressionDestination cd)=0
Check if compressor supports desired compression destination.
virtual void set_filename(const char *filename)=0
Set file name.
virtual void compress()=0
Compress image.
virtual size_t recommended_compressed_buffer_size()=0
Get the recommended size for the compressed buffer.
@ COMP_DEST_MEM
write compressed image to buffer in memory
@ COMP_DEST_FILE
write compressed image to file
virtual void set_destination_buffer(unsigned char *buf, unsigned int buf_size)=0
Set destination buffer (if compressing to memory).
virtual void set_image_buffer(colorspace_t cspace, unsigned char *buffer)=0
Set image buffer to compress.
virtual size_t compressed_size()=0
Get compressed size.
virtual void set_compression_destination(CompressionDestination cd)=0
Set compression destination.
unsigned int width
The width of the image.
Definition: writer.h:49
colorspace_t cspace
The colorspace of the image.
Definition: writer.h:52
unsigned char * buffer
The image-buffer.
Definition: writer.h:54
unsigned int height
The height of the image.
Definition: writer.h:50
char * filename
The complete filename.
Definition: writer.h:45