Fawkes API Fawkes Development Version

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 namespace firevision {
00035 #if 0 /* just to make Emacs auto-indent happy */
00036 }
00037 #endif
00038 
00039 /** @class FvRawReader <fvutils/readers/fvraw.h>
00040  * FvRaw image reader implementation.
00041  * @author Tim Niemueller
00042  */
00043 
00044 /** Constructor.
00045  * @param filename filename to read from.
00046  */
00047 FvRawReader::FvRawReader(const char *filename)
00048 {
00049   opened = false;
00050   buffer = NULL;
00051 
00052   infile = fopen(filename, "r");
00053 
00054   if (infile == NULL) {
00055     throw Exception("Could not open file for reading");
00056   }
00057 
00058   if ( fread((char *)&header, sizeof(header), 1, infile) != 1 ) {
00059     throw Exception("Could not read header");
00060   } else {
00061     if (header.file_id != FvRawWriter::FILE_IDENTIFIER) {
00062       throw ("Invalid file identifier");
00063     } else {
00064       
00065       buffer_size = colorspace_buffer_size( header.colorspace, header.width, header.height );
00066       opened = true;
00067     }
00068   }
00069 }
00070 
00071 
00072 /** Destructor. */
00073 FvRawReader::~FvRawReader()
00074 {
00075   fclose( infile );
00076   opened = false;
00077 }
00078 
00079 
00080 void
00081 FvRawReader::set_buffer(unsigned char *yuv422planar_buffer)
00082 {
00083   buffer = yuv422planar_buffer;
00084 }
00085 
00086 
00087 colorspace_t
00088 FvRawReader::colorspace()
00089 {
00090   if ( opened ) {
00091     return header.colorspace;
00092   } else {
00093     return CS_UNKNOWN;
00094   }
00095 }
00096 
00097 
00098 unsigned int
00099 FvRawReader::pixel_width()
00100 {
00101   if ( opened ) {
00102     return header.width;
00103   } else {
00104     return 0;
00105   }
00106 }
00107 
00108 
00109 unsigned int
00110 FvRawReader::pixel_height()
00111 {
00112   if ( opened ) {
00113     return header.height;
00114   } else {
00115     return 0;
00116   }
00117 }
00118 
00119 
00120 void
00121 FvRawReader::read()
00122 {
00123   if ( buffer == NULL ) {
00124     throw Exception("Read failed: buffer == NULL");
00125   }
00126   if ( buffer_size == 0 ) {
00127     throw Exception("Read failed: buffer_size == 0");
00128   }
00129 
00130   if (fread(buffer, buffer_size, 1, infile) != 1) {
00131     throw Exception("Failed to read data", errno);
00132   }
00133 }
00134 
00135 
00136 /** Check if given file contains FvRaw image.
00137  * @param filename file to check
00138  * @return true if file contains FvRaw image, false otherwise
00139  */
00140 bool
00141 FvRawReader::is_FvRaw(const char *filename)
00142 {
00143   FILE *f;
00144   f = fopen(filename, "r");
00145   if (f != NULL) {
00146     FvRawWriter::FvRawHeader header;
00147     if ( fread((char *)&header, sizeof(header), 1, f) == 1 ) {
00148       if (header.file_id == FvRawWriter::FILE_IDENTIFIER) {
00149         fclose(f);
00150         return true;
00151       }
00152     }
00153   }
00154   fclose(f);
00155   return false;
00156 }
00157 
00158 } // end namespace firevision
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends