Visualization Library v1.0.3A lightweight C++ OpenGL middleware for 2D/3D graphics |
[Download] [Tutorials] [All Classes] [Grouped Classes] |
00001 /**************************************************************************************/ 00002 /* */ 00003 /* Visualization Library */ 00004 /* http://visualizationlibrary.org */ 00005 /* */ 00006 /* Copyright (c) 2005-2010, Michele Bosi */ 00007 /* All rights reserved. */ 00008 /* */ 00009 /* Redistribution and use in source and binary forms, with or without modification, */ 00010 /* are permitted provided that the following conditions are met: */ 00011 /* */ 00012 /* - Redistributions of source code must retain the above copyright notice, this */ 00013 /* list of conditions and the following disclaimer. */ 00014 /* */ 00015 /* - Redistributions in binary form must reproduce the above copyright notice, this */ 00016 /* list of conditions and the following disclaimer in the documentation and/or */ 00017 /* other materials provided with the distribution. */ 00018 /* */ 00019 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */ 00020 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */ 00021 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */ 00022 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */ 00023 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */ 00024 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */ 00025 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */ 00026 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */ 00027 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */ 00028 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 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 // safe way, get a line first then sscanf the string 00081 stream->readLine(line); 00082 if ( sscanf(line.c_str(), "%s %s", buffer, filename) != 2 ) 00083 return NULL; 00084 // make sure it is zero terminated 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 // make sure it is zero terminated 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 // make sure it is zero terminated 00101 fmt[BUFFER_SIZE-1] = 0; 00102 file->close(); 00103 00104 // strip quotes 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 // extract path 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 // safe way, get a line first then sscanf the string 00174 stream->readLine(line); 00175 if ( sscanf(line.c_str(), "%s %s", buffer, filename) != 2 ) 00176 return false; 00177 // make sure it is zero terminated 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 // make sure it is zero terminated 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 // make sure it is zero terminated 00194 fmt[BUFFER_SIZE-1] = 0; 00195 file->close(); 00196 00197 // strip quotes 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 // extract path 00209 String raw_file = file->path().extractPath() + filename; 00210 00211 // check type 00212 if (String(typ) != "UCHAR") 00213 return false; 00214 00215 if (String(fmt) != "LUMINANCE" && String(fmt) != "LUMINANCE_ALPHA" && String(fmt) != "RGB" && String(fmt) != "RGBA") 00216 return false; 00217 00218 if (defFileSystem()->locateFile(raw_file)) 00219 return true; 00220 else 00221 return false; 00222 } 00223 //-----------------------------------------------------------------------------