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 
31 using namespace fawkes;
32 
33 namespace 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  */
43 JpegReader::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. */
69 JpegReader::~JpegReader()
70 {
71  jpeg_destroy_decompress(&cinfo);
72  fclose(infile);
73  opened = false;
74 }
75 
76 void
77 JpegReader::set_buffer(unsigned char *yuv422planar_buffer)
78 {
79  buffer = yuv422planar_buffer;
80 }
81 
82 colorspace_t
83 JpegReader::colorspace()
84 {
85  return YUV422_PLANAR;
86 }
87 
88 unsigned int
89 JpegReader::pixel_width()
90 {
91  if (opened) {
92  return cinfo.output_width;
93  } else {
94  return 0;
95  }
96 }
97 
98 unsigned int
99 JpegReader::pixel_height()
100 {
101  if (opened) {
102  return cinfo.output_height;
103  } else {
104  return 0;
105  }
106 }
107 
108 void
109 JpegReader::read()
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
Fawkes library namespace.