00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00056 const char *file = fileName.toLatin1 ();
00057
00058
00059 int fich=open(file,O_RDONLY);
00060 if(fich<0){
00061 qDebug("Error: file not found.");
00062 return;
00063 }
00064
00065
00066 head=(S3DHead*)malloc(sizeof(S3DHead));
00067
00068 int br=read(fich,head,sizeof(S3DHead));
00069
00070
00071 if(br!=sizeof(S3DHead)){
00072 qDebug("Error: file corrupt.");
00073 return;
00074 }
00075
00076
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
00083 if(br!=(int)sizeof(TPixelBW)*npix){
00084 qDebug("Error: file corrupt.");
00085 return;
00086 }
00087 }
00088
00089
00090 if(head->colorFormat == 0)
00091 colorFormatText = "Black and White";
00092 else
00093 colorFormatText = "Color RGB";
00094
00095
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
00103 if(br!=(int)sizeof(TPixelColor)*npix){
00104 qDebug("Error: file corrupt.");
00105 return;
00106 }
00107 }
00108
00109
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
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
00125 close(fich);
00126
00127
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){
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{
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
00147 if(head->encoding==0){
00148 for(int i=0;i<head->np;i++){
00149
00150 TPoint3D p=points[i];
00151 pointArray[i][0] = p.x;
00152 pointArray[i][1] = p.y;
00153 pointArray[i][2] = p.z;
00154 pointArray[i][3] = (int) p.info2d/head->ncol;
00155 pointArray[i][4] = (int) ( p.info2d - pointArray[i][3] * head->ncol );
00156 }
00157 }else{
00158 if(head->colorFormat==0){
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{
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 }