00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "ioDAT.hpp"
00033 #include <vlCore/LoadWriterManager.hpp>
00034 #include <vlCore/VisualizationLibrary.hpp>
00035 #include <vlCore/VisualizationLibrary.hpp>
00036 #include <vlCore/FileSystem.hpp>
00037 #include <vlCore/VirtualFile.hpp>
00038 #include <vlCore/Image.hpp>
00039 #include <vlCore/TextStream.hpp>
00040 #include <stdio.h>
00041
00042 using namespace vl;
00043
00044 #include <vlCore/ImageTools.hpp>
00045
00046
00047 ref<Image> vl::loadDAT( const String& path )
00048 {
00049 ref<VirtualFile> file = defFileSystem()->locateFile(path);
00050 if ( !file )
00051 {
00052 Log::error( Say("File '%s' not found.\n") << path );
00053 return NULL;
00054 }
00055
00056 return loadDAT(file.get());
00057 }
00058
00059 ref<Image> vl::loadDAT(VirtualFile* file)
00060 {
00061 if (!file->open(OM_ReadOnly))
00062 {
00063 Log::error( Say("loadDAT: could not find DAT file '%s'.\n") << file->path() );
00064 return NULL;
00065 }
00066
00067 #define BUFFER_SIZE 1024
00068
00069 ref<TextStream> stream = new TextStream(file);
00070 std::string line;
00071 char buffer[BUFFER_SIZE ];
00072 char filename[BUFFER_SIZE ];
00073 char typ[BUFFER_SIZE ];
00074 char fmt[BUFFER_SIZE ];
00075 float a=0,b=0,c=0;
00076 int width=0, height=0, depth=0, bytealign=1;
00077 EImageFormat format;
00078 EImageType type;
00079
00080
00081 stream->readLine(line);
00082 if ( sscanf(line.c_str(), "%s %s", buffer, filename) != 2 )
00083 return NULL;
00084
00085 filename[BUFFER_SIZE-1] = 0;
00086 stream->readLine(line);
00087 if ( sscanf(line.c_str(), "%s %d %d %d", buffer, &width, &height, &depth) != 4 )
00088 return NULL;
00089 stream->readLine(line);
00090 if ( sscanf(line.c_str(), "%s %f %f %f", buffer, &a,&b,&c) != 4 )
00091 return NULL;
00092 stream->readLine(line);
00093 if ( sscanf(line.c_str(), "%s %s", buffer, typ) != 2 )
00094 return NULL;
00095
00096 typ[BUFFER_SIZE-1] = 0;
00097 stream->readLine(line);
00098 if ( sscanf(line.c_str(), "%s %s", buffer, fmt) != 2 )
00099 return NULL;
00100
00101 fmt[BUFFER_SIZE-1] = 0;
00102 file->close();
00103
00104
00105 int len = strlen(filename);
00106 if (len > 2)
00107 {
00108 if (filename[0] == '\'' || filename[0] == '"')
00109 memmove(filename, filename + 1, len);
00110 len = (int)strlen(filename);
00111 if (filename[len-1] == '\'' || filename[len-1] == '"')
00112 filename[len-1] = 0;
00113 }
00114
00115
00116 String raw_file = file->path().extractPath() + filename;
00117
00118 if (String(typ) == "UCHAR")
00119 type = IT_UNSIGNED_BYTE;
00120 else
00121 {
00122 Log::error( Say("loadDAT('%s'): type '%s' not supported.\n") << file->path() << typ );
00123 return NULL;
00124 }
00125
00126 if (String(fmt) == "LUMINANCE")
00127 format = IF_LUMINANCE;
00128 else
00129 if (String(fmt) == "LUMINANCE_ALPHA")
00130 format = IF_LUMINANCE_ALPHA;
00131 else
00132 if (String(fmt) == "RGB")
00133 format = IF_RGB;
00134 else
00135 if (String(fmt) == "RGBA")
00136 format = IF_RGBA;
00137 else
00138 {
00139 Log::error( Say("loadDAT('%s'): format '%s' not supported.\n") << file->path() << fmt );
00140 return NULL;
00141 }
00142
00143 ref<VirtualFile> rawf = defFileSystem()->locateFile(raw_file);
00144 if (rawf)
00145 {
00146 return loadRAW( rawf.get(), -1, width, height, depth, bytealign, format, type );
00147 }
00148 else
00149 {
00150 Log::error( Say("loadDAT('%s'): could not find RAW file '%s'.\n") << file->path() << raw_file );
00151 return NULL;
00152 }
00153 }
00154
00155 bool vl::isDAT(VirtualFile* file)
00156 {
00157 #define BUFFER_SIZE 1024
00158
00159 if (!file->open(OM_ReadOnly))
00160 return false;
00161
00162 ref<TextStream> stream = new TextStream(file);
00163 std::string line;
00164 char buffer[BUFFER_SIZE];
00165 char filename[BUFFER_SIZE];
00166 char typ[BUFFER_SIZE];
00167 char fmt[BUFFER_SIZE];
00168 float a=0,b=0,c=0;
00169 int width=0, height=0, depth=0;
00170 EImageFormat format;
00171 EImageType type;
00172
00173
00174 stream->readLine(line);
00175 if ( sscanf(line.c_str(), "%s %s", buffer, filename) != 2 )
00176 return false;
00177
00178 filename[BUFFER_SIZE-1] = 0;
00179 stream->readLine(line);
00180 if ( sscanf(line.c_str(), "%s %d %d %d", buffer, &width, &height, &depth) != 4 )
00181 return false;
00182 stream->readLine(line);
00183 if ( sscanf(line.c_str(), "%s %f %f %f", buffer, &a,&b,&c) != 4 )
00184 return false;
00185 stream->readLine(line);
00186 if ( sscanf(line.c_str(), "%s %s", buffer, typ) != 2 )
00187 return false;
00188
00189 typ[BUFFER_SIZE-1] = 0;
00190 stream->readLine(line);
00191 if ( sscanf(line.c_str(), "%s %s", buffer, fmt) != 2 )
00192 return false;
00193
00194 fmt[BUFFER_SIZE-1] = 0;
00195 file->close();
00196
00197
00198 int len = strlen(filename);
00199 if (len > 2)
00200 {
00201 if (filename[0] == '\'' || filename[0] == '"')
00202 memmove(filename,filename + 1, len);
00203 len = (int)strlen(filename);
00204 if (filename[len-1] == '\'' || filename[len-1] == '"')
00205 filename[len-1] = 0;
00206 }
00207
00208
00209 String raw_file = file->path().extractPath() + filename;
00210
00211
00212 if (String(typ) == "UCHAR")
00213 type = IT_UNSIGNED_BYTE;
00214 else
00215 return false;
00216
00217 if (String(fmt) == "LUMINANCE")
00218 format = IF_LUMINANCE;
00219 else
00220 if (String(fmt) == "LUMINANCE_ALPHA")
00221 format = IF_LUMINANCE_ALPHA;
00222 else
00223 if (String(fmt) == "RGB")
00224 format = IF_RGB;
00225 else
00226 if (String(fmt) == "RGBA")
00227 format = IF_RGBA;
00228 else
00229 return false;
00230
00231 if (defFileSystem()->locateFile(raw_file))
00232 return true;
00233 else
00234 return false;
00235 }
00236