00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00044 file.write(QString("FRAME\x0a").toAscii());
00045
00046
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
00124 rx.setPattern("YUV4MPEG2");
00125 if (rx.indexIn(readedLine) == -1)
00126 return false;
00127
00128
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
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
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 }