Visualization Library 2.0.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
ioDAT.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #include "ioDAT.hpp"
36 #include <vlCore/FileSystem.hpp>
37 #include <vlCore/VirtualFile.hpp>
38 #include <vlCore/Image.hpp>
39 #include <vlCore/TextStream.hpp>
40 #include <stdio.h>
41 
42 using namespace vl;
43 
44 #include <vlCore/ImageTools.hpp>
45 
46 //-----------------------------------------------------------------------------
48 {
50  if ( !file )
51  {
52  Log::error( Say("File '%s' not found.\n") << path );
53  return NULL;
54  }
55 
56  return loadDAT(file.get());
57 }
58 //-----------------------------------------------------------------------------
60 {
61  if (!file->open(OM_ReadOnly))
62  {
63  Log::error( Say("loadDAT: could not find DAT file '%s'.\n") << file->path() );
64  return NULL;
65  }
66 
67  #define BUFFER_SIZE 1024
68 
69  ref<TextStream> stream = new TextStream(file);
70  std::string line;
71  char buffer[BUFFER_SIZE ];
72  char filename[BUFFER_SIZE ];
73  char typ[BUFFER_SIZE ];
74  char fmt[BUFFER_SIZE ];
75  float a=0,b=0,c=0;
76  int width=0, height=0, depth=0, bytealign=1;
77  EImageFormat format;
78  EImageType type;
79 
80  // safe way, get a line first then sscanf the string
81  stream->readLine(line);
82  if ( sscanf(line.c_str(), "%s %s", buffer, filename) != 2 )
83  return NULL;
84  // make sure it is zero terminated
85  filename[BUFFER_SIZE-1] = 0;
86  stream->readLine(line);
87  if ( sscanf(line.c_str(), "%s %d %d %d", buffer, &width, &height, &depth) != 4 )
88  return NULL;
89  stream->readLine(line);
90  if ( sscanf(line.c_str(), "%s %f %f %f", buffer, &a,&b,&c) != 4 )
91  return NULL;
92  stream->readLine(line);
93  if ( sscanf(line.c_str(), "%s %s", buffer, typ) != 2 )
94  return NULL;
95  // make sure it is zero terminated
96  typ[BUFFER_SIZE-1] = 0;
97  stream->readLine(line);
98  if ( sscanf(line.c_str(), "%s %s", buffer, fmt) != 2 )
99  return NULL;
100  // make sure it is zero terminated
101  fmt[BUFFER_SIZE-1] = 0;
102  file->close();
103 
104  // strip quotes
105  int len = (int)strlen(filename);
106  if (len > 2)
107  {
108  if (filename[0] == '\'' || filename[0] == '"')
109  memmove(filename, filename + 1, len);
110  len = (int)strlen(filename);
111  if (filename[len-1] == '\'' || filename[len-1] == '"')
112  filename[len-1] = 0;
113  }
114 
115  // extract path
116  String raw_file = file->path().extractPath() + filename;
117 
118  if (String(typ) == "UCHAR")
119  type = IT_UNSIGNED_BYTE;
120  else
121  if (String(typ) == "USHORT")
122  type = IT_UNSIGNED_SHORT;
123  else
124  {
125  Log::error( Say("loadDAT('%s'): type '%s' not supported.\n") << file->path() << typ );
126  return NULL;
127  }
128 
129  if (String(fmt) == "LUMINANCE")
130  format = IF_LUMINANCE;
131  else
132  if (String(fmt) == "LUMINANCE_ALPHA")
133  format = IF_LUMINANCE_ALPHA;
134  else
135  if (String(fmt) == "RGB")
136  format = IF_RGB;
137  else
138  if (String(fmt) == "RGBA")
139  format = IF_RGBA;
140  else
141  {
142  Log::error( Say("loadDAT('%s'): format '%s' not supported.\n") << file->path() << fmt );
143  return NULL;
144  }
145 
146  ref<VirtualFile> rawf = defFileSystem()->locateFile(raw_file);
147  if (rawf)
148  {
149  return loadRAW( rawf.get(), -1, width, height, depth, bytealign, format, type );
150  }
151  else
152  {
153  Log::error( Say("loadDAT('%s'): could not find RAW file '%s'.\n") << file->path() << raw_file );
154  return NULL;
155  }
156 }
157 //-----------------------------------------------------------------------------
159 {
160  #define BUFFER_SIZE 1024
161 
162  if (!file->open(OM_ReadOnly))
163  return false;
164 
165  ref<TextStream> stream = new TextStream(file);
166  std::string line;
167  char buffer[BUFFER_SIZE];
168  char filename[BUFFER_SIZE];
169  char typ[BUFFER_SIZE];
170  char fmt[BUFFER_SIZE];
171  float a=0,b=0,c=0;
172  int width=0, height=0, depth=0;
173  // EImageFormat format;
174  // EImageType type;
175 
176  // safe way, get a line first then sscanf the string
177  stream->readLine(line);
178  if ( sscanf(line.c_str(), "%s %s", buffer, filename) != 2 )
179  return false;
180  // make sure it is zero terminated
181  filename[BUFFER_SIZE-1] = 0;
182  stream->readLine(line);
183  if ( sscanf(line.c_str(), "%s %d %d %d", buffer, &width, &height, &depth) != 4 )
184  return false;
185  stream->readLine(line);
186  if ( sscanf(line.c_str(), "%s %f %f %f", buffer, &a,&b,&c) != 4 )
187  return false;
188  stream->readLine(line);
189  if ( sscanf(line.c_str(), "%s %s", buffer, typ) != 2 )
190  return false;
191  // make sure it is zero terminated
192  typ[BUFFER_SIZE-1] = 0;
193  stream->readLine(line);
194  if ( sscanf(line.c_str(), "%s %s", buffer, fmt) != 2 )
195  return false;
196  // make sure it is zero terminated
197  fmt[BUFFER_SIZE-1] = 0;
198  file->close();
199 
200  // strip quotes
201  int len = (int)strlen(filename);
202  if (len > 2)
203  {
204  if (filename[0] == '\'' || filename[0] == '"')
205  memmove(filename,filename + 1, len);
206  len = (int)strlen(filename);
207  if (filename[len-1] == '\'' || filename[len-1] == '"')
208  filename[len-1] = 0;
209  }
210 
211  // extract path
212  String raw_file = file->path().extractPath() + filename;
213 
214  // check type
215  if (String(typ) != "UCHAR")
216  return false;
217 
218  if (String(fmt) != "LUMINANCE" && String(fmt) != "LUMINANCE_ALPHA" && String(fmt) != "RGB" && String(fmt) != "RGBA")
219  return false;
220 
221  if (defFileSystem()->locateFile(raw_file))
222  return true;
223  else
224  return false;
225 }
226 //-----------------------------------------------------------------------------
VLCORE_EXPORT FileSystem * defFileSystem()
Returns the default FileSystem used by VisualizationLibrary.
Definition: pimpl.cpp:97
const T * get() const
Definition: Object.hpp:128
An abstract class representing a file.
Definition: VirtualFile.hpp:60
bool readLine(std::string &utf8)
Definition: TextStream.hpp:67
A simple String formatting class.
Definition: Say.hpp:124
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
VLCORE_EXPORT ref< Image > loadRAW(VirtualFile *file, long long file_offset, int width, int height, int depth, int bytealign, EImageFormat format, EImageType type)
Loads a raw image file.
Definition: Image.cpp:1280
static void error(const String &message)
Use this function to provide information about run-time errors: file not found, out of memory...
Definition: Log.cpp:165
String extractPath() const
If the String contains a file path the function returns the path with trailing slash, without the file name.
Definition: String.cpp:830
virtual ref< VirtualFile > locateFile(const String &full_path, const String &alternate_path=String()) const
Looks for a VirtualFile on the disk and in the currently active FileSystem.
Definition: FileSystem.cpp:61
VLCORE_EXPORT ref< Image > loadDAT(VirtualFile *file)
Definition: ioDAT.cpp:59
virtual void close()=0
Closes the file.
const String & path() const
Returns the path of the file.
Definition: VirtualFile.hpp:98
Visualization Library main namespace.
The TextStream class can be used to conveniently read or parse utf8-encoded text files.
Definition: TextStream.hpp:46
VLCORE_EXPORT bool isDAT(VirtualFile *file)
Definition: ioDAT.cpp:158
virtual bool open(EOpenMode mode)=0
Opens the file in the specified mode.
EImageFormat
#define NULL
Definition: OpenGLDefs.hpp:81
EImageType
VLCORE_EXPORT ref< VirtualFile > locateFile(const String &path)
Utility function, equivalent to vl::defFileSystem()->locateFile(path)
Definition: VirtualFile.cpp:41
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
#define BUFFER_SIZE