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/color/yuvrgb.h>
26#include <fvutils/writers/jpeg.h>
27
28#include <cerrno>
29#include <cstdio>
30#include <cstdlib>
31#include <cstring>
32#include <string.h>
33extern "C" {
34#include <jpeglib.h>
35}
36
37using namespace fawkes;
38
39namespace firevision {
40
41/** @class JpegWriter <fvutils/writers/jpeg.h>
42 * JPEG file writer.
43 * @author Tim Niemueller
44 */
45
46/** Constructor.
47 * @param quality quality, value between 0 and 100
48 */
49JpegWriter::JpegWriter(int quality) : Writer("jpg")
50{
51 buffer = NULL;
52
53 this->quality = (quality > 0) ? quality : -quality;
54}
55
56/** Constructor.
57 * @param filename file name to write to
58 * @param quality quality, value between 0 and 100
59 */
60JpegWriter::JpegWriter(const char *filename, int quality) : Writer("jpg")
61{
63
64 buffer = NULL;
65
66 this->quality = (quality > 0) ? quality : -quality;
67}
68
69/** Destructor. */
71{
72}
73
74void
75JpegWriter::set_buffer(colorspace_t cspace, unsigned char *buffer)
76{
77 if (cspace == YUV422_PLANAR) {
78 this->buffer = buffer;
79 } else {
80 throw Exception("Incompatible colorspace, can only hand YUV422_PLANAR images");
81 }
82}
83
84void
86{
87 if (buffer == NULL) {
88 throw Exception("JpegWriter::read() error: buffer == NULL");
89 }
90
91 if ((outfile = fopen(filename, "wb")) == NULL) {
92 Exception e("Cannot open JPEG file for writing", errno);
93 e.append("File %s could not be opened", filename);
94 throw e;
95 }
96
97 int row_stride;
98 struct jpeg_compress_struct cinfo;
99 struct jpeg_error_mgr jerr;
100
101 cinfo.err = jpeg_std_error(&jerr);
102 jpeg_create_compress(&cinfo);
103 jpeg_stdio_dest(&cinfo, outfile);
104
105 cinfo.image_width = width;
106 cinfo.image_height = height;
107 cinfo.input_components = 3;
108 cinfo.in_color_space = JCS_RGB;
109
110 jpeg_set_defaults(&cinfo);
111 jpeg_set_quality(&cinfo, quality, true /* limit to baseline-JPEG values */);
112
113 jpeg_start_compress(&cinfo, true);
114 row_stride = cinfo.image_width * cinfo.input_components;
115
116 row_buffer = (unsigned char *)malloc(row_stride);
117
118 while (cinfo.next_scanline < cinfo.image_height) {
119 convert_line_yuv422planar_to_rgb(
120 buffer, row_buffer, cinfo.image_width, cinfo.image_height, cinfo.next_scanline, 0);
121 jpeg_write_scanlines(&cinfo, &row_buffer, 1);
122 }
123
124 free(row_buffer);
125
126 jpeg_finish_compress(&cinfo);
127
128 jpeg_destroy_compress(&cinfo);
129 fclose(outfile);
130}
131
132} // end namespace firevision
Base class for exceptions in Fawkes.
Definition: exception.h:36
void append(const char *format,...) noexcept
Append messages to the message list.
Definition: exception.cpp:333
JpegWriter(int quality=80)
Constructor.
Definition: jpeg.cpp:49
virtual ~JpegWriter()
Destructor.
Definition: jpeg.cpp:70
virtual void write()
Write to file.
Definition: jpeg.cpp:85
virtual void set_buffer(colorspace_t cspace, unsigned char *buffer)
Set image buffer.
Definition: jpeg.cpp:75
Interface to write images.
Definition: writer.h:32
unsigned int width
The width of the image.
Definition: writer.h:49
colorspace_t cspace
The colorspace of the image.
Definition: writer.h:52
virtual void set_filename(const char *filename)
Set filename.
Definition: writer.cpp:102
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
Fawkes library namespace.