00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00024
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include <float.h>
00028 #include <iostream>
00029
00030 #include <qvip.h>
00031 #include <qvmath.h>
00032 #include <qvdefines.h>
00033
00034 #include <QVPolyline>
00035 #include <QVPolylineF>
00036 #include <QList>
00037
00038 #ifdef QVIPP
00039 #include <qvipp.h>
00040 #endif
00041
00042 template<int Channels> void writeUCharImageToFile(QFile &file, const QVImage<uChar, Channels> &image)
00043 {
00044 for (uInt i = 0; i < image.getRows(); i++)
00045 file.write((char *) image.getReadData() + i*image.getStep(), Channels * image.getCols());
00046 }
00047
00048 bool writeYUV4MPEG2Header(QFile &file, const int cols, const int rows, const int fps)
00049 {
00050 file.write(QString("YUV4MPEG2 W%1 H%2 F%3:1 Ip A0:0\x0a").arg(cols).arg(rows).arg(fps).toAscii());
00051
00052 return true;
00053 }
00054
00055 bool writeYUV4MPEG2Frame(QFile &file, const QVImage<uChar,1> imageY, const QVImage<uChar,1> imageU, const QVImage<uChar,1> imageV)
00056 {
00057
00058 file.write(QString("FRAME\x0a").toAscii());
00059
00060
00061 writeUCharImageToFile<1>(file, imageY);
00062 writeUCharImageToFile<1>(file, imageU);
00063 writeUCharImageToFile<1>(file, imageV);
00064
00065 file.flush();
00066
00067 return true;
00068 }
00069
00070 bool writeYUV4MPEG2Frame(QFile &file, const QVImage<uChar,1> imageGray)
00071 {
00072 const int cols = imageGray.getCols(), rows = imageGray.getRows();
00073 QVImage<uChar, 1> imageU(cols/2, rows/2);
00074 QVImage<uChar, 1> imageV(cols/2, rows/2);
00075 Set(128, imageU);
00076 Set(128, imageV);
00077
00078 return writeYUV4MPEG2Frame(file, imageGray, imageU, imageV);
00079 }
00080
00081 #ifdef QVIPP
00082 bool writeYUV4MPEG2Frame(QFile &file, const QVImage<uChar,3> imageRGB)
00083 {
00084 const int cols = imageRGB.getCols(), rows = imageRGB.getRows();
00085 QVImage<uChar, 1> imageY(cols, rows);
00086 QVImage<uChar, 1> imageU(cols/2, rows/2);
00087 QVImage<uChar, 1> imageV(cols/2, rows/2);
00088
00089 RGBToYUV420(imageRGB, imageY, imageU, imageV);
00090
00091 return writeYUV4MPEG2Frame(file, imageY, imageU, imageV);
00092 }
00093 #endif
00094
00095 bool writeQVImageToFile(const QString fileName, const QVImage<uChar, 3> &image)
00096 { return ((QImage) image).save(fileName); }
00097
00098 bool readQVImageFromFile(const QString fileName, QVImage<uChar, 3> &image)
00099 {
00100 QImage qimg;
00101
00102 if(not qimg.load(fileName)) return FALSE;
00103
00104 image = QVImage<uChar, 3>(qimg);
00105
00106 return TRUE;
00107 }
00108
00109
00111
00112 bool readToBuffer(QFile &file, QVImage<uChar, 1> &image)
00113 {
00114 bool finished = false;
00115 uChar *buf_img_aux = image.getWriteData();
00116
00117 int nbytes=0;
00118 for(unsigned int i=0;i<image.getRows();i++)
00119 {
00120 nbytes = file.read((char *)(buf_img_aux), image.getCols());
00121
00122 if(nbytes==0 or nbytes==-1)
00123 {
00124 finished = TRUE;
00125 break;
00126 }
00127 buf_img_aux += image.getStep();
00128 }
00129
00130 return !finished;
00131 }
00132
00133 #include <QStringList>
00134 #include <QRegExp>
00135
00136 bool readYUV4MPEG2Header(QFile &file, int &cols, int &rows, int &fps)
00137 {
00138 const QString readedLine = file.readLine();
00139
00140 if (readedLine == NULL || readedLine == QString("MPLAYER ERROR\n"))
00141 {
00142 return false;
00143 }
00144
00145 QRegExp rx;
00146 QStringList widthTokens;
00147
00148
00149 rx.setPattern("YUV4MPEG2");
00150 if (rx.indexIn(readedLine) == -1)
00151 return false;
00152
00153
00154 rx.setPattern("(W)([0-9]+)");
00155 if (rx.indexIn(readedLine) == -1)
00156 return false;
00157 widthTokens = rx.capturedTexts();
00158 cols = widthTokens.at(2).toInt();
00159
00160
00161 rx.setPattern("(H)([0-9]+)");
00162 if (rx.indexIn(readedLine) == -1)
00163 return false;
00164 widthTokens = rx.capturedTexts();
00165 rows = widthTokens.at(2).toInt();
00166
00167
00168 rx.setPattern("(F)([0-9]+):([0-9]+)");
00169 if (rx.indexIn(readedLine) == -1)
00170 return false;
00171 widthTokens = rx.capturedTexts();
00172 fps = widthTokens.at(2).toInt() / widthTokens.at(3).toInt();
00173
00174 return true;
00175 }
00176
00177 bool readYUV4MPEG2Frame(QFile &file, QVImage<uChar> &imageY, QVImage<uChar> &imageU, QVImage<uChar> &imageV)
00178 {
00179 const QString yuv4mpeg2header = file.readLine();
00180
00181 if ( readToBuffer(file, imageY) &&
00182 readToBuffer(file, imageU) &&
00183 readToBuffer(file, imageV) )
00184 return true;
00185 else
00186 return false;
00187 }