Fawkes API  Fawkes Development Version
jpeg.cpp
1 
2 /***************************************************************************
3  * jpeg.cp - JPEG writer
4  *
5  * Generated: Wed Jun 28 11:36:54 2006 (my brother's 18th birthday)
6  * Copyright 2005-2009 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <core/exception.h>
25 #include <fvutils/writers/jpeg.h>
26 #include <fvutils/color/yuvrgb.h>
27 
28 #include <cstdio>
29 #include <cerrno>
30 #include <cstdlib>
31 #include <cstring>
32 #include <string.h>
33 extern "C" {
34 #include <jpeglib.h>
35 }
36 
37 using namespace fawkes;
38 
39 namespace firevision {
40 #if 0 /* just to make Emacs auto-indent happy */
41 }
42 #endif
43 
44 /** @class JpegWriter <fvutils/writers/jpeg.h>
45  * JPEG file writer.
46  * @author Tim Niemueller
47  */
48 
49 /** Constructor.
50  * @param quality quality, value between 0 and 100
51  */
52 JpegWriter::JpegWriter(int quality)
53  : Writer("jpg")
54 {
55  buffer = NULL;
56 
57  this->quality = (quality > 0) ? quality : -quality;
58 }
59 
60 /** Constructor.
61  * @param filename file name to write to
62  * @param quality quality, value between 0 and 100
63  */
64 JpegWriter::JpegWriter(const char *filename, int quality)
65  : Writer("jpg")
66 {
67  set_filename(filename);
68 
69  buffer = NULL;
70 
71  this->quality = (quality > 0) ? quality : -quality;
72 }
73 
74 
75 /** Destructor. */
77 {
78 }
79 
80 
81 void
82 JpegWriter::set_buffer(colorspace_t cspace, unsigned char *buffer)
83 {
84  if (cspace == YUV422_PLANAR) {
85  this->buffer = buffer;
86  } else {
87  throw Exception("Incompatible colorspace, can only hand YUV422_PLANAR images");
88  }
89 }
90 
91 
92 void
94 {
95  if ( buffer == NULL ) {
96  throw Exception("JpegWriter::read() error: buffer == NULL");
97  }
98 
99  if ((outfile = fopen(filename, "wb")) == NULL) {
100  Exception e("Cannot open JPEG file for writing", errno);
101  e.append("File %s could not be opened", filename);
102  throw e;
103  }
104 
105  int row_stride;
106  struct jpeg_compress_struct cinfo;
107  struct jpeg_error_mgr jerr;
108 
109  cinfo.err = jpeg_std_error( &jerr );
110  jpeg_create_compress( &cinfo );
111  jpeg_stdio_dest( &cinfo, outfile );
112 
113  cinfo.image_width = width;
114  cinfo.image_height = height;
115  cinfo.input_components = 3;
116  cinfo.in_color_space = JCS_RGB;
117 
118  jpeg_set_defaults(&cinfo);
119  jpeg_set_quality(&cinfo, quality, true /* limit to baseline-JPEG values */);
120 
121  jpeg_start_compress( &cinfo, true );
122  row_stride = cinfo.image_width * cinfo.input_components;
123 
124  row_buffer = (unsigned char *)malloc( row_stride );
125 
126  while ( cinfo.next_scanline < cinfo.image_height ) {
127  convert_line_yuv422planar_to_rgb( buffer, row_buffer,
128  cinfo.image_width, cinfo.image_height,
129  cinfo.next_scanline, 0 );
130  jpeg_write_scanlines( &cinfo, &row_buffer, 1 );
131  }
132 
133  free(row_buffer);
134 
135  jpeg_finish_compress( &cinfo );
136 
137  jpeg_destroy_compress( &cinfo );
138  fclose( outfile );
139 
140 }
141 
142 } // end namespace firevision
colorspace_t cspace
The colorspace of the image.
Definition: writer.h:55
virtual void set_buffer(colorspace_t cspace, unsigned char *buffer)
Set image buffer.
Definition: jpeg.cpp:82
virtual ~JpegWriter()
Destructor.
Definition: jpeg.cpp:76
Fawkes library namespace.
Interface to write images.
Definition: writer.h:34
virtual void write()
Write to file.
Definition: jpeg.cpp:93
Base class for exceptions in Fawkes.
Definition: exception.h:36
JpegWriter(int quality=80)
Constructor.
Definition: jpeg.cpp:52
virtual void set_filename(const char *filename)
Set filename.
Definition: writer.cpp:106
unsigned int width
The width of the image.
Definition: writer.h:52
unsigned char * buffer
The image-buffer.
Definition: writer.h:57
unsigned int height
The height of the image.
Definition: writer.h:53
char * filename
The complete filename.
Definition: writer.h:48
void append(const char *format,...)
Append messages to the message list.
Definition: exception.cpp:341