PARP Research Group University of Murcia, Spain


src/qvio/qvio.cpp

Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2007, 2008, 2009. PARP Research Group.
00003  *      <http://perception.inf.um.es>
00004  *      University of Murcia, Spain.
00005  *
00006  *      This file is part of the QVision library.
00007  *
00008  *      QVision is free software: you can redistribute it and/or modify
00009  *      it under the terms of the GNU Lesser General Public License as
00010  *      published by the Free Software Foundation, version 3 of the License.
00011  *
00012  *      QVision is distributed in the hope that it will be useful,
00013  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *      GNU Lesser General Public License for more details.
00016  *
00017  *      You should have received a copy of the GNU Lesser General Public
00018  *      License along with QVision. If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00024 
00025 #include <qvio.h>
00026 #include <qvipp.h>
00027 
00028 template<int Channels> void writeUCharImageToFile(QFile &file, const QVImage<uChar, Channels> &image)
00029         {
00030         for (uInt i = 0; i < image.getRows(); i++)
00031                 file.write((char *) image.getReadData() + i*image.getStep(), Channels * image.getCols());
00032         }
00033 
00034 bool writeYUV4MPEG2Header(QFile &file, const int cols, const int rows, const int fps)
00035         {
00036         file.write(QString("YUV4MPEG2 W%1 H%2 F%3:1 Ip A0:0\x0a").arg(cols).arg(rows).arg(fps).toAscii());
00037 
00038         return true;
00039         }
00040 
00041 bool writeYUV4MPEG2Frame(QFile &file, const QVImage<uChar,1> imageY, const QVImage<uChar,1> imageU, const QVImage<uChar,1> imageV)
00042         {
00043         // Write 'FRAME' keyword.
00044         file.write(QString("FRAME\x0a").toAscii());
00045 
00046         // Write image data buffers
00047         writeUCharImageToFile<1>(file, imageY);
00048         writeUCharImageToFile<1>(file, imageU);
00049         writeUCharImageToFile<1>(file, imageV);
00050 
00051         file.flush();
00052 
00053         return true;
00054         }
00055 
00056 bool writeYUV4MPEG2Frame(QFile &file, const QVImage<uChar,3> imageRGB)
00057         {
00058         const int cols = imageRGB.getCols(), rows = imageRGB.getRows();
00059         QVImage<uChar, 1> imageY(cols, rows);
00060         QVImage<uChar, 1> imageU(cols/2, rows/2);
00061         QVImage<uChar, 1> imageV(cols/2, rows/2);
00062 
00063         RGBToYUV420(imageRGB, imageY, imageU, imageV);
00064 
00065         writeYUV4MPEG2Frame(file, imageY, imageU, imageV);
00066 
00067         return true;
00068         }
00069 
00070 bool writeQVImageToFile(const QString fileName, const QVImage<uChar, 3> &image)
00071         { return ((QImage) image).save(fileName); }
00072 
00073 bool readQVImageFromFile(const QString fileName, QVImage<uChar, 3> &image)
00074         {
00075         QImage qimg;
00076 
00077         if(not qimg.load(fileName)) return FALSE;
00078         
00079         image = QVImage<uChar, 3>(qimg);
00080 
00081         return TRUE;
00082         }
00083 
00084 
00086 
00087 bool readToBuffer(QFile &file, QVImage<uChar, 1> &image)
00088         {
00089         bool finished = false;
00090         uChar *buf_img_aux = image.getWriteData();
00091 
00092         int nbytes=0;
00093         for(unsigned int i=0;i<image.getRows();i++)
00094                 {
00095                 nbytes = file.read((char *)(buf_img_aux), image.getCols());
00096 
00097                 if(nbytes==0 or nbytes==-1)
00098                         {
00099                         finished = TRUE;
00100                         break;
00101                         }               
00102                 buf_img_aux += image.getStep();
00103                 }
00104 
00105         return !finished;
00106         }
00107 
00108 #include <QStringList>
00109 #include <QRegExp>
00110 
00111 bool readYUV4MPEG2Header(QFile &file, int &cols, int &rows, int &fps)
00112         {
00113         const QString readedLine = file.readLine();
00114 
00115         if (readedLine == NULL || readedLine == QString("MPLAYER ERROR\n"))
00116                 {
00117                 return false;
00118                 }
00119 
00120         QRegExp rx;
00121         QStringList widthTokens;
00122 
00123         // Parse for special identifier
00124         rx.setPattern("YUV4MPEG2");
00125         if (rx.indexIn(readedLine) == -1)
00126                 return false;
00127 
00128         // Parse for width (cols)
00129         rx.setPattern("(W)([0-9]+)");
00130         if (rx.indexIn(readedLine) == -1)
00131                 return false;
00132         widthTokens = rx.capturedTexts();
00133         cols = widthTokens.at(2).toInt();
00134 
00135         // Parse for height (rows)
00136         rx.setPattern("(H)([0-9]+)");
00137         if (rx.indexIn(readedLine) == -1)
00138                 return false;
00139         widthTokens = rx.capturedTexts();
00140         rows = widthTokens.at(2).toInt();
00141 
00142         // Parse for fps
00143         rx.setPattern("(F)([0-9]+):([0-9]+)");
00144         if (rx.indexIn(readedLine) == -1)
00145                 return false;
00146         widthTokens = rx.capturedTexts();
00147         fps = widthTokens.at(2).toInt() / widthTokens.at(3).toInt();
00148 
00149         return true;
00150         }
00151 
00152 bool readYUV4MPEG2Frame(QFile &file, QVImage<uChar> &imageY, QVImage<uChar> &imageU, QVImage<uChar> &imageV)
00153         {
00154         const QString yuv4mpeg2header = file.readLine();
00155 
00156         if (    readToBuffer(file, imageY)      &&
00157                 readToBuffer(file, imageU)      &&
00158                 readToBuffer(file, imageV)      )
00159                 return true;
00160         else
00161                 return false;
00162         }



QVision framework. PARP research group, copyright 2007, 2008.