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