00001 #ifndef Impala_Core_Array_ReadPgm_h
00002 #define Impala_Core_Array_ReadPgm_h
00003
00004 #include <string>
00005 #include "Basis/Timer.h"
00006 #include "Util/IOBufferFile.h"
00007 #include "Core/Array/Arrays.h"
00008
00009 namespace Impala
00010 {
00011 namespace Core
00012 {
00013 namespace Array
00014 {
00015
00016 inline void SkipEmpty(Util::IOBuffer* buf){
00017 ILOG_VAR(Impala.Core.Array.SkipEmpty);
00018 char c;
00019 while(1){
00020 buf->Read(&c,1);
00021
00022 if((c == '\n')||(c == ' '))
00023 {
00024 continue;
00025 }
00026 else
00027 {
00028
00029 if(c=='#'){
00030 String comment=buf->ReadLine();
00031 }
00032 else
00033 {
00034 break;
00035 }
00036 }
00037 }
00038 buf->SetPosition(buf->GetPosition()-1);
00039 }
00040
00041 template <class ArrayT>
00042 inline void
00043 ReadPgm(ArrayT*& dst, Util::IOBuffer* buffer)
00044 {
00045 ILOG_VAR(Impala.Core.Array.ReadPgm);
00046 char c;
00047 bool IsAscii;
00048 int Width,Height,MaxVal;
00049
00050
00051 buffer->Read(&c,1);
00052 if(c!='P')
00053 {
00054 ILOG_ERROR("File is not in PGM format! Was waiting for P,but "<<c);
00055 return;
00056 }
00057
00058 buffer->Read(&c,1);
00059 if(c=='2')
00060 IsAscii=true;
00061 else if(c=='5')
00062 IsAscii=false;
00063 else
00064 {
00065 ILOG_ERROR("File is not in PGM format! "<<c);
00066 return;
00067 }
00068 SkipEmpty(buffer);
00069
00070 buffer->NativeTypeRead(&Width);
00071 buffer->NativeTypeRead(&Height);
00072 buffer->NativeTypeRead(&MaxVal);
00073
00074 ILOG_DEBUG("PGM Image with "<<Width<<"x"<<Height<<", maxval:"<<MaxVal);
00075
00076
00077 if(MaxVal>255){
00078 ILOG_ERROR("MaxVal is greater than 255");
00079 return;
00080 }
00081
00082 if (dst == 0)
00083 dst = ArrayCreate<ArrayT>(Width, Height);
00084
00085 if(IsAscii){
00086 for(int k=0;k<Width;k++)
00087 {
00088 for(int l=0;l<Height;l++)
00089 {
00090 buffer->NativeTypeRead(&c);
00091 dst->SetValue(c,l,k);
00092 }
00093 }
00094 }else{
00095 int read=buffer->Read(dst->PB(),Width*Height);
00096 if(read!=Width*Height){
00097 ILOG_ERROR("Not Enough bytes could be read!");
00098 return;
00099 }
00100
00101 }
00102 }
00103
00104
00105 template <class ArrayT>
00106 inline void
00107 ReadPgm(ArrayT*& dst, String fileName, Util::Database* db)
00108 {
00109 Util::IOBuffer* buf = db->GetIOBuffer(fileName, true, false, "");
00110 if (buf)
00111 {
00112 ReadPgm(dst, buf);
00113 delete buf;
00114 }
00115 }
00116
00117 }
00118 }
00119 }
00120
00121 #endif