filereader.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Author:  Jaime Valls Miro <jaime.vallsmiro@eng.uts.edu.au>
00004 **          Hue Tuan Thi <huetuan.thi@uts.edu.au>, (C) 2006
00005 **
00006 ** This file is part of the S3D Viewer Project
00007 **
00008 ** This file may be used under the terms of the GNU General Public
00009 ** License version 2.0 as published by the Free Software Foundation
00010 ** and appearing in the file LICENSE.GPL included in the packaging of
00011 ** this file.  Please review the following information to ensure GNU
00012 ** General Public Licensing requirements will be met:
00013 ** http://www.trolltech.com/products/qt/opensource.html
00014 **
00015 ** If you are unsure which license is appropriate for your use, please
00016 ** review the following information:
00017 ** http://www.trolltech.com/products/qt/licensing.html or contact the
00018 ** sales department at sales@trolltech.com.
00019 **
00020 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00021 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00022 **
00023 ****************************************************************************/
00024 
00032 #include "filereader.h"
00033 
00038 
00039 FileReader::FileReader()
00040 {
00041 }
00042 
00044 FileReader::~FileReader()
00045 {
00046 }
00047 
00053 void FileReader::readFile(QString &fileName)
00054 {
00055     // convert fileName to string
00056     const char *file = fileName.toLatin1 ();
00057 
00058     // open file under READ ONLY mode
00059     int fich=open(file,O_RDONLY);
00060     if(fich<0){
00061         qDebug("Error: file not found.");
00062         return;
00063     }
00064 
00065     // initiate head structure
00066     head=(S3DHead*)malloc(sizeof(S3DHead));
00067     // number of bytes read
00068     int br=read(fich,head,sizeof(S3DHead)); 
00069 
00070     // error reading control
00071     if(br!=sizeof(S3DHead)){
00072         qDebug("Error: file corrupt.");
00073         return;
00074     }
00075 
00076     // Gray image (if the file contains it)
00077     if(head->encoding==0 && head->colorFormat==0){
00078         int npix=head->nrow*head->ncol;
00079         image_bw=(TPixelBW*)malloc(sizeof(TPixelBW)*npix);
00080         br=read(fich,image_bw,sizeof(TPixelBW)*npix);
00081 
00082         //error reading control
00083         if(br!=(int)sizeof(TPixelBW)*npix){
00084             qDebug("Error: file corrupt.");
00085             return;
00086         }
00087     }
00088 
00089     // color format for info purpose
00090     if(head->colorFormat == 0)
00091         colorFormatText = "Black and White";
00092     else
00093         colorFormatText = "Color RGB";
00094 
00095     // Color image (if the file contains it)
00096     TPixelColor *image_color=NULL;
00097     if(head->encoding==0 && head->colorFormat==1){
00098         int npix=head->nrow*head->ncol;
00099         image_color=(TPixelColor*)malloc(sizeof(TPixelColor)*npix);
00100         br=read(fich,image_color,sizeof(TPixelColor)*npix);
00101 
00102         // error reading control
00103         if(br!=(int)sizeof(TPixelColor)*npix){
00104             qDebug("Error: file corrupt.");
00105             return;
00106         }
00107     }
00108 
00109     // 3D Points
00110     TPoint3D *points=NULL;
00111     if(head->np>0){
00112         points=(TPoint3D*)malloc(sizeof(TPoint3D)*head->np);
00113         br=read(fich,points,sizeof(TPoint3D)*head->np);
00114 
00115         //error reading control
00116         if(br!=(int)sizeof(TPoint3D)*head->np){
00117             if(image_bw) free(image_bw);
00118             if(image_color) free(image_color);
00119             qDebug("Error: file corrupt.");
00120             return;
00121         }
00122     }
00123 
00124     // At this point, we have read all of the file data
00125     close(fich);
00126 
00127     // Image (if exists)
00128     if(head->encoding==0){
00129         for(int i=0;i<head->nrow;i++){
00130             for(int j=0;j<head->ncol;j++){
00131                 if(head->colorFormat==0){ //Black and white case
00132                     TPixelBW p=image_bw[ i*head->ncol+j ];
00133                     imagePixels[i][j][0] = (GLubyte) ( p.color );
00134                     imagePixels[i][j][1] = (GLubyte) ( p.color );
00135                     imagePixels[i][j][2] = (GLubyte) ( p.color );
00136                 }else{                    //Color RGB case
00137                     TPixelColor p=image_color[ i*head->ncol+j ];
00138                     imagePixels[i][j][0] = (GLubyte) ( p.r );
00139                     imagePixels[i][j][1] = (GLubyte) ( p.g );
00140                     imagePixels[i][j][2] = (GLubyte) ( p.b );
00141                 }
00142             }
00143         }
00144     }
00145 
00146     // 3D points
00147     if(head->encoding==0){
00148         for(int i=0;i<head->np;i++){
00149             // we show the 3D coordinates (XYZ) and the image coordinates (IJ) of the points
00150             TPoint3D p=points[i];
00151             pointArray[i][0] = p.x; // x coord
00152             pointArray[i][1] = p.y; // y coord
00153             pointArray[i][2] = p.z; // z coord
00154             pointArray[i][3] = (int) p.info2d/head->ncol; // x reference image coord
00155             pointArray[i][4] = (int) ( p.info2d - pointArray[i][3] * head->ncol ); // y coord
00156         }
00157     }else{
00158         if(head->colorFormat==0){ // point cloud with gray levels
00159             for(int i=0;i<head->np;i++){
00160                 TPoint3D p=points[i];
00161                 pointArray[i][0] = p.x;
00162                 pointArray[i][1] = p.y;
00163                 pointArray[i][2] = p.z;
00164                 pointArray[i][3] = p.info2d;
00165                 pointArray[i][4] = p.info2d;
00166                 pointArray[i][5] = p.info2d;
00167             }
00168         }else{                    // point cloud with color rgb values
00169             for(int i=0;i<head->np;i++){
00170                 TPoint3D p=points[i];
00171                 int r,g,b;
00172                 r=(p.info2d & 0xff0000)>>16;
00173                 g=(p.info2d & 0xff00)>>8;
00174                 b=(p.info2d & 0xff);
00175 
00176                 pointArray[i][0] = p.x;
00177                 pointArray[i][1] = p.y;
00178                 pointArray[i][2] = p.z;
00179                 pointArray[i][3] = r;
00180                 pointArray[i][4] = g;
00181                 pointArray[i][5] = b;
00182             }
00183         }
00184     }
00185 }
00186 
00191 QString FileReader::getInfo()
00192 {
00193     return QString( head->info );
00194 }
00195 
00200 int FileReader::getColorFormat()
00201 {
00202     return head->colorFormat;
00203 }
00204 
00209 QString FileReader::getColorText()
00210 {
00211     return colorFormatText;
00212 }
00213 
00218 int FileReader::getNRow()
00219 {
00220     return head->nrow;
00221 }
00222 
00227 int FileReader::getNCol()
00228 {
00229     return head->ncol;
00230 }
00231 
00236 int FileReader::getNp()
00237 {
00238     return head->np;
00239 }
00240 
00245 int FileReader::getEncoding()
00246 {
00247     return head->encoding;
00248 }
00249 
00254 char FileReader::getColor()
00255 {
00256     return image_bw->color;
00257 }
00258 
00263 char FileReader::getR()
00264 {
00265     return image_color->r;
00266 }
00267 
00272 char FileReader::getG()
00273 {
00274     return image_color->g;
00275 }
00276 
00281 char FileReader::getB()
00282 {
00283     return image_color->b;
00284 }
00285 
00290 float FileReader::getX()
00291 {
00292     return points->x;
00293 }
00294 
00299 float FileReader::getY()
00300 {
00301     return points->y;
00302 }
00303 
00308 float FileReader::getZ()
00309 {
00310     return points->z;
00311 }
00312 
00317 int FileReader::getInfo2d()
00318 {
00319     return points->info2d;
00320 }

Generated on Mon Aug 14 10:44:19 2006 for S3DViewer by  doxygen 1.4.4