Fawkes API Fawkes Development Version
jpeg.cpp
1
2/***************************************************************************
3 * jpeg.cpp - JPEG Reader
4 *
5 * Generated: Sun Jun 04 23:18:06 2006 (watching Terminator 2)
6 * Copyright 2005-2007 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/rgbyuv.h>
26#include <fvutils/readers/jpeg.h>
27
28#include <cstdio>
29#include <cstdlib>
30
31using namespace fawkes;
32
33namespace firevision {
34
35/** @class JpegReader <fvutils/readers/jpeg.h>
36 * JPEG file reader.
37 * @author Tim Niemueller
38 */
39
40/** Constructor.
41 * @param filename file to read
42 */
43JpegReader::JpegReader(const char *filename)
44{
45 opened = false;
46 buffer = NULL;
47
48 if ((infile = fopen(filename, "rb")) == NULL) {
49 throw Exception("Cannot open JPEG file");
50 }
51
52 cinfo.err = jpeg_std_error(&jerr);
53 jpeg_create_decompress(&cinfo);
54 jpeg_stdio_src(&cinfo, infile);
55
56 jpeg_read_header(&cinfo, true);
57 jpeg_calc_output_dimensions(&cinfo);
58
59 /*
60 cout << "Read JPEG header, image info:" << endl
61 << " width: " << cinfo.output_width << endl
62 << " height: " << cinfo.output_height << endl;
63 */
64
65 opened = true;
66}
67
68/** Destructor. */
70{
71 jpeg_destroy_decompress(&cinfo);
72 fclose(infile);
73 opened = false;
74}
75
76void
77JpegReader::set_buffer(unsigned char *yuv422planar_buffer)
78{
79 buffer = yuv422planar_buffer;
80}
81
82colorspace_t
84{
85 return YUV422_PLANAR;
86}
87
88unsigned int
90{
91 if (opened) {
92 return cinfo.output_width;
93 } else {
94 return 0;
95 }
96}
97
98unsigned int
100{
101 if (opened) {
102 return cinfo.output_height;
103 } else {
104 return 0;
105 }
106}
107
108void
110{
111 if (buffer == NULL) {
112 throw Exception("JpegReader::read: buffer == NULL");
113 }
114
115 jpeg_start_decompress(&cinfo);
116 row_stride = cinfo.output_width * cinfo.output_components;
117
118 row_buffer = (unsigned char *)malloc(row_stride);
119
120 while (cinfo.output_scanline < cinfo.output_height) {
121 jpeg_read_scanlines(&cinfo, &row_buffer, 1);
122 convert_line_rgb_to_yuv422planar(
123 row_buffer, buffer, cinfo.output_width, cinfo.output_height, 0, cinfo.output_scanline - 1);
124 }
125
126 free(row_buffer);
127 jpeg_finish_decompress(&cinfo);
128}
129
130} // end namespace firevision
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual unsigned int pixel_width()
Get width of read image in pixels.
Definition: jpeg.cpp:89
JpegReader(const char *filename)
Constructor.
Definition: jpeg.cpp:43
virtual ~JpegReader()
Destructor.
Definition: jpeg.cpp:69
virtual colorspace_t colorspace()
Get colorspace from the just read image.
Definition: jpeg.cpp:83
virtual unsigned int pixel_height()
Get height of read image in pixels.
Definition: jpeg.cpp:99
virtual void read()
Read data from file.
Definition: jpeg.cpp:109
virtual void set_buffer(unsigned char *yuv422planar_buffer)
Set buffer that the read image should be written to.
Definition: jpeg.cpp:77
Fawkes library namespace.