DpxFile.C

Go to the documentation of this file.
00001 /*!@file Raster/DpxFile.C DPX (Digital Picture Exchange) image file format */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00005 // by the University of Southern California (USC) and the iLab at USC.  //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Rob Peters <rjpeters at usc dot edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Raster/DpxFile.C $
00035 // $Id: DpxFile.C 14376 2011-01-11 02:44:34Z pez $
00036 //
00037 
00038 #ifndef RASTER_DPXFILE_C_DEFINED
00039 #define RASTER_DPXFILE_C_DEFINED
00040 
00041 #include "Raster/DpxFile.H"
00042 #include "Util/log.H"
00043 #include <cstdio>
00044 
00045 DpxFile::DpxFile(const char* fname)
00046 {
00047   LDEBUG("sizeof(DpxFile::FileHeader) = %"ZU, sizeof(DpxFile::FileHeader));
00048 
00049   FILE* fp = fopen(fname, "r");
00050   if (fp == 0)
00051     LFATAL("couldn't open '%s' for reading", fname);
00052 
00053   if (fread(&this->fh, sizeof(this->fh), 1, fp) != 1)
00054     LFATAL("couldn't read file header from '%s'", fname);
00055 
00056   // bool doswap = false;
00057 
00058   if (this->fh.MagicNumber == 0x53445058)
00059     // OK, no byte-swapping
00060     ; //doswap = false;
00061   else if (this->fh.MagicNumber == 0x58504453)
00062     {
00063       // doswap = true;
00064       LFATAL("byte-swapping not yet implemented for DPX images");
00065     }
00066   else
00067     LFATAL("invalid DPX magic number '%d' in '%s'",
00068            this->fh.MagicNumber, fname);
00069 
00070 #define SHOW(x) LDEBUG("(%s) " #x " = %u", fname, x)
00071 #define SHOWF(x) LDEBUG("(%s) " #x " = %f", fname, x)
00072 #define SHOWS(x,w) LDEBUG("(%s) " #x " = '%." #w "s'", fname, x)
00073 
00074   SHOW(this->fh.MagicNumber);
00075   SHOW(this->fh.ImageDataOffset);
00076   SHOW(this->fh.FileSize);
00077   SHOW(this->fh.DittoKey);
00078   SHOW(this->fh.GenericHeaderSize);
00079   SHOW(this->fh.IndustryHeaderSize);
00080   SHOW(this->fh.UserDefinedDataSize);
00081   SHOW(this->fh.EncryptKey);
00082 
00083   SHOWS(this->fh.HeaderVersion, 8);
00084   SHOWS(this->fh.ImageFilename, 100);
00085   SHOWS(this->fh.CreateTime, 23);
00086   SHOWS(this->fh.Creator, 100);
00087   SHOWS(this->fh.Project, 200);
00088   SHOWS(this->fh.Copyright, 200);
00089 
00090   LDEBUG("sizeof(DpxFile::ImageHeader) = %"ZU,
00091          sizeof(DpxFile::ImageHeader));
00092 
00093   if (fread(&this->ih, sizeof(this->ih), 1, fp) != 1)
00094     LFATAL("couldn't read image header from '%s'", fname);
00095 
00096   SHOW(this->ih.Orientation);
00097   SHOW(this->ih.NumberElements);
00098   SHOW(this->ih.PixelsPerLine);
00099   SHOW(this->ih.LinesPerElement);
00100   SHOW(this->ih.ImageElement[0].DataSign);
00101   SHOW(this->ih.ImageElement[0].LowData);
00102   SHOWF(this->ih.ImageElement[0].LowQuantity);
00103   SHOW(this->ih.ImageElement[0].HighData);
00104   SHOWF(this->ih.ImageElement[0].HighQuantity);
00105   SHOW(this->ih.ImageElement[0].Descriptor);
00106   SHOW(this->ih.ImageElement[0].Transfer);
00107   SHOW(this->ih.ImageElement[0].Colorimetric);
00108   SHOW(this->ih.ImageElement[0].BitSize);
00109   SHOW(this->ih.ImageElement[0].Packing);
00110   SHOW(this->ih.ImageElement[0].Encoding);
00111   SHOW(this->ih.ImageElement[0].DataOffset);
00112   SHOW(this->ih.ImageElement[0].EndOfLinePadding);
00113   SHOW(this->ih.ImageElement[0].EndOfImagePadding);
00114   SHOWS(this->ih.ImageElement[0].Description, 32);
00115 
00116   LDEBUG("sizeof(DpxFile::OrientationHeader) = %"ZU,
00117          sizeof(DpxFile::OrientationHeader));
00118 
00119   if (fread(&this->oh, sizeof(this->oh), 1, fp) != 1)
00120     LFATAL("couldn't read orientation header from '%s'", fname);
00121 
00122   SHOW(this->oh.XOffset);
00123   SHOW(this->oh.YOffset);
00124   SHOWF(this->oh.XCenter);
00125   SHOWF(this->oh.YCenter);
00126   SHOW(this->oh.XOriginalSize);
00127   SHOW(this->oh.YOriginalSize);
00128   SHOWS(this->oh.FileName, 100);
00129   SHOWS(this->oh.TimeDate, 24);
00130   SHOWS(this->oh.InputName, 32);
00131   SHOWS(this->oh.InputSN, 32);
00132   SHOW(this->oh.Border[0]);
00133   SHOW(this->oh.Border[1]);
00134   SHOW(this->oh.Border[2]);
00135   SHOW(this->oh.Border[3]);
00136   SHOW(this->oh.AspectRatio[0]);
00137   SHOW(this->oh.AspectRatio[1]);
00138 
00139   LDEBUG("sizeof(DpxFile::FilmIndustryHeader) = %"ZU,
00140          sizeof(DpxFile::FilmIndustryHeader));
00141 
00142   if (fread(&this->fih, sizeof(this->fih), 1, fp) != 1)
00143     LFATAL("couldn't read film industry header from '%s'", fname);
00144 
00145   SHOWS(this->fih.FilmMfgId, 2);
00146   SHOWS(this->fih.FilmType, 2);
00147   SHOWS(this->fih.Offset, 2);
00148   SHOWS(this->fih.Prefix, 6);
00149   SHOWS(this->fih.Count, 4);
00150   SHOWS(this->fih.Format, 32);
00151   SHOW(this->fih.FramePosition);
00152   SHOW(this->fih.SequenceLen);
00153   SHOW(this->fih.HeldCount);
00154   SHOWF(this->fih.FrameRate);
00155   SHOWF(this->fih.ShutterAngle);
00156   SHOWS(this->fih.FrameId, 32);
00157   SHOWS(this->fih.SlateInfo, 100);
00158 
00159   LDEBUG("sizeof(DpxFile::TelevisionIndustryHeader) = %"ZU,
00160          sizeof(DpxFile::TelevisionIndustryHeader));
00161 
00162   if (fread(&this->tih, sizeof(this->tih), 1, fp) != 1)
00163     LFATAL("couldn't read television industry header from '%s'", fname);
00164 
00165   SHOW(this->tih.TimeCode);
00166   SHOW(this->tih.UserBits);
00167   SHOW(this->tih.Interlace);
00168   SHOW(this->tih.FiledNumber);
00169   SHOW(this->tih.VideoSignal);
00170   SHOW(this->tih.Padding);
00171   SHOWF(this->tih.HorzSampleRate);
00172   SHOWF(this->tih.VertSampleRate);
00173   SHOWF(this->tih.FrameRate);
00174   SHOWF(this->tih.TimeOffset);
00175   SHOWF(this->tih.Gamma);
00176   SHOWF(this->tih.BlackLevel);
00177   SHOWF(this->tih.BlackGain);
00178   SHOWF(this->tih.Breakpoint);
00179   SHOWF(this->tih.WhiteLevel);
00180   SHOWF(this->tih.IntegrationTimes);
00181 
00182   if (fseek(fp, this->fh.ImageDataOffset, SEEK_SET) != 0)
00183     LFATAL("couldn't fseek() to start of image data in '%s'", fname);
00184 
00185   ASSERT(this->ih.PixelsPerLine > 0);
00186   ASSERT(this->ih.LinesPerElement > 0);
00187 
00188   this->dims = Dims(this->ih.PixelsPerLine, this->ih.LinesPerElement);
00189   this->rawimage = Image<uint16>(this->dims, NO_INIT);
00190 
00191   ASSERT(this->ih.ImageElement[0].BitSize == 16);
00192   ASSERT(this->ih.ImageElement[0].EndOfLinePadding == 0xFFFF);
00193 
00194   if (fread(this->rawimage.getArrayPtr(),
00195             sizeof(*(this->rawimage.getArrayPtr())),
00196             this->dims.sz(), fp)
00197       != size_t(this->dims.sz()))
00198     LFATAL("couldn't read image data from '%s'", fname);
00199 
00200   fclose(fp);
00201 }
00202 
00203 // ######################################################################
00204 /* So things look consistent in everyone's emacs... */
00205 /* Local Variables: */
00206 /* mode: c++ */
00207 /* indent-tabs-mode: nil */
00208 /* End: */
00209 
00210 #endif // RASTER_DPXFILE_C_DEFINED
Generated on Sun May 8 08:05:34 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3