fvraw.cpp

00001
00002 /***************************************************************************
00003  *  fvraw.h - FvRaw Reader
00004  *
00005  *  Generated: Sun Jun 05 01:22:35 2006 (watching Terminator 2)
00006  *  Copyright  2005-2007  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023
00024 #include <core/exception.h>
00025 #include <fvutils/readers/fvraw.h>
00026 #include <fvutils/writers/fvraw.h>
00027 #include <fvutils/color/colorspaces.h>
00028
00029 #include <cstdio>
00030 #include <errno.h>
00031
00032 using namespace fawkes;
00033 
00034 /** @class FvRawReader fvraw.h <fvutils/readers/fvraw.h>
00035  * FvRaw image reader implementation.
00036  * @author Tim Niemueller
00037  */
00038 
00039 /** Constructor.
00040  * @param filename filename to read from.
00041  */
00042 FvRawReader::FvRawReader(const char *filename)
00043 {
00044   opened = false;
00045   buffer = NULL;
00046
00047   infile = fopen(filename, "r");
00048
00049   if (infile == NULL) {
00050     throw Exception("Could not open file for reading");
00051   }
00052
00053   if ( fread((char *)&header, sizeof(header), 1, infile) != 1 ) {
00054     throw Exception("Could not read header");
00055   } else {
00056     if (header.file_id != FvRawWriter::FILE_IDENTIFIER) {
00057       throw ("Invalid file identifier");
00058     } else {
00059
00060       buffer_size = colorspace_buffer_size( header.colorspace, header.width, header.height );
00061       opened = true;
00062     }
00063   }
00064 }
00065
00066 
00067 /** Destructor. */
00068 FvRawReader::~FvRawReader()
00069 {
00070   fclose( infile );
00071   opened = false;
00072 }
00073
00074
00075 void
00076 FvRawReader::set_buffer(unsigned char *yuv422planar_buffer)
00077 {
00078   buffer = yuv422planar_buffer;
00079 }
00080
00081
00082 colorspace_t
00083 FvRawReader::colorspace()
00084 {
00085   if ( opened ) {
00086     return header.colorspace;
00087   } else {
00088     return CS_UNKNOWN;
00089   }
00090 }
00091
00092
00093 unsigned int
00094 FvRawReader::pixel_width()
00095 {
00096   if ( opened ) {
00097     return header.width;
00098   } else {
00099     return 0;
00100   }
00101 }
00102
00103
00104 unsigned int
00105 FvRawReader::pixel_height()
00106 {
00107   if ( opened ) {
00108     return header.height;
00109   } else {
00110     return 0;
00111   }
00112 }
00113
00114
00115 void
00116 FvRawReader::read()
00117 {
00118   if ( buffer == NULL ) {
00119     throw Exception("Read failed: buffer == NULL");
00120   }
00121   if ( buffer_size == 0 ) {
00122     throw Exception("Read failed: buffer_size == 0");
00123   }
00124
00125   if (fread(buffer, buffer_size, 1, infile) != 1) {
00126     throw Exception("Failed to read data", errno);
00127   }
00128 }
00129
00130 
00131 /** Check if given file contains FvRaw image.
00132  * @param filename file to check
00133  * @return true if file contains FvRaw image, false otherwise
00134  */
00135 bool
00136 FvRawReader::is_FvRaw(const char *filename)
00137 {
00138   FILE *f;
00139   f = fopen(filename, "r");
00140   if (f != NULL) {
00141     FvRawWriter::FvRawHeader header;
00142     if ( fread((char *)&header, sizeof(header), 1, f) == 1 ) {
00143       if (header.file_id == FvRawWriter::FILE_IDENTIFIER) {
00144         fclose(f);
00145         return true;
00146       }
00147     }
00148   }
00149   fclose(f);
00150   return false;
00151 }