jpeg.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <core/exception.h>
00025 #include <fvutils/readers/jpeg.h>
00026 #include <fvutils/color/rgbyuv.h>
00027
00028 #include <cstdio>
00029 #include <cstdlib>
00030
00031 using namespace fawkes;
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 JpegReader::JpegReader(const char *filename)
00042 {
00043 opened = false;
00044 buffer = NULL;
00045
00046 if ((infile = fopen(filename, "rb")) == NULL) {
00047 throw Exception("Cannot open JPEG file");
00048 }
00049
00050 cinfo.err = jpeg_std_error( &jerr );
00051 jpeg_create_decompress( &cinfo );
00052 jpeg_stdio_src( &cinfo, infile );
00053
00054 jpeg_read_header( &cinfo, true );
00055 jpeg_calc_output_dimensions( &cinfo );
00056
00057
00058
00059
00060
00061
00062
00063 opened = true;
00064 }
00065
00066
00067
00068 JpegReader::~JpegReader()
00069 {
00070 jpeg_destroy_decompress( &cinfo );
00071 fclose( infile );
00072 opened = false;
00073 }
00074
00075
00076 void
00077 JpegReader::set_buffer(unsigned char *yuv422planar_buffer)
00078 {
00079 buffer = yuv422planar_buffer;
00080 }
00081
00082
00083 colorspace_t
00084 JpegReader::colorspace()
00085 {
00086 return YUV422_PLANAR;
00087 }
00088
00089
00090 unsigned int
00091 JpegReader::pixel_width()
00092 {
00093 if ( opened ) {
00094 return cinfo.output_width;
00095 } else {
00096 return 0;
00097 }
00098 }
00099
00100
00101 unsigned int
00102 JpegReader::pixel_height()
00103 {
00104 if ( opened ) {
00105 return cinfo.output_height;
00106 } else {
00107 return 0;
00108 }
00109 }
00110
00111
00112 void
00113 JpegReader::read()
00114 {
00115 if ( buffer == NULL ) {
00116 throw Exception("JpegReader::read: buffer == NULL");
00117 }
00118
00119 jpeg_start_decompress( &cinfo );
00120 row_stride = cinfo.output_width * cinfo.output_components;
00121
00122 row_buffer = (unsigned char *)malloc( row_stride );
00123
00124 while ( cinfo.output_scanline < cinfo.output_height ) {
00125 jpeg_read_scanlines( &cinfo, &row_buffer, 1 );
00126 convert_line_rgb_to_yuv422planar( row_buffer, buffer,
00127 cinfo.output_width, cinfo.output_height,
00128 0, cinfo.output_scanline - 1 );
00129 }
00130
00131 free( row_buffer );
00132 jpeg_finish_decompress( &cinfo );
00133
00134 }