[4] | 1 | /****************************************************************************** |
---|
| 2 | * Fast DXT - a realtime DXT compression tool |
---|
| 3 | * |
---|
| 4 | * Author : Luc Renambot |
---|
| 5 | * |
---|
| 6 | * Copyright (C) 2007 Electronic Visualization Laboratory, |
---|
| 7 | * University of Illinois at Chicago |
---|
| 8 | * |
---|
| 9 | * All rights reserved. |
---|
| 10 | * |
---|
| 11 | * Redistribution and use in source and binary forms, with or without |
---|
| 12 | * modification, are permitted provided that the following conditions are met: |
---|
| 13 | * |
---|
| 14 | * * Redistributions of source code must retain the above copyright |
---|
| 15 | * notice, this list of conditions and the following disclaimer. |
---|
| 16 | * * Redistributions in binary form must reproduce the above |
---|
| 17 | * copyright notice, this list of conditions and the following disclaimer |
---|
| 18 | * in the documentation and/or other materials provided with the distribution. |
---|
| 19 | * * Neither the name of the University of Illinois at Chicago nor |
---|
| 20 | * the names of its contributors may be used to endorse or promote |
---|
| 21 | * products derived from this software without specific prior written permission. |
---|
| 22 | * |
---|
| 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
| 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
| 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
| 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
---|
| 27 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
| 28 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
| 29 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
| 30 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
| 31 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
| 32 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
| 33 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 34 | * |
---|
| 35 | * Direct questions, comments etc about SAGE to http://www.evl.uic.edu/cavern/forum/ |
---|
| 36 | * |
---|
| 37 | *****************************************************************************/ |
---|
| 38 | |
---|
| 39 | #include "util.h" |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | #if defined(WIN32) |
---|
| 43 | #include <io.h> |
---|
| 44 | LARGE_INTEGER perf_freq; |
---|
| 45 | LARGE_INTEGER perf_start; |
---|
| 46 | HANDLE win_err; // stderr in a console |
---|
| 47 | #elif defined(__APPLE__) |
---|
| 48 | #include <mach/mach_time.h> |
---|
| 49 | static double perf_conversion = 0.0; |
---|
| 50 | static uint64_t perf_start; |
---|
| 51 | #else |
---|
| 52 | struct timeval tv_start; |
---|
| 53 | #endif |
---|
| 54 | |
---|
| 55 | #if !defined(WIN32) |
---|
| 56 | #include <sys/stat.h> |
---|
| 57 | #include <unistd.h> |
---|
| 58 | #endif |
---|
| 59 | |
---|
| 60 | #include <fcntl.h> |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | void aInitialize() |
---|
| 64 | { |
---|
| 65 | #if defined(WIN32) |
---|
| 66 | QueryPerformanceCounter(&perf_start); |
---|
| 67 | QueryPerformanceFrequency(&perf_freq); |
---|
| 68 | AllocConsole(); |
---|
| 69 | win_err = GetStdHandle(STD_ERROR_HANDLE); |
---|
| 70 | #elif defined(__APPLE__) |
---|
| 71 | if( perf_conversion == 0.0 ) |
---|
| 72 | { |
---|
| 73 | mach_timebase_info_data_t info; |
---|
| 74 | kern_return_t err = mach_timebase_info( &info ); |
---|
| 75 | |
---|
| 76 | //Convert the timebase into seconds |
---|
| 77 | if( err == 0 ) |
---|
| 78 | perf_conversion = 1e-9 * (double) info.numer / (double) info.denom; |
---|
| 79 | } |
---|
| 80 | // Start of time |
---|
| 81 | perf_start = mach_absolute_time(); |
---|
| 82 | |
---|
| 83 | // Initialize the random generator |
---|
| 84 | srand(getpid()); |
---|
| 85 | #else |
---|
| 86 | // Start of time |
---|
| 87 | gettimeofday(&tv_start,0); |
---|
| 88 | // Initialize the random generator |
---|
| 89 | srand(getpid()); |
---|
| 90 | #endif |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | double aTime() |
---|
| 94 | // return time since start of process in seconds |
---|
| 95 | { |
---|
| 96 | #if defined(WIN32) |
---|
| 97 | LARGE_INTEGER perf_counter; |
---|
| 98 | #else |
---|
| 99 | struct timeval tv; |
---|
| 100 | #endif |
---|
| 101 | |
---|
| 102 | #if defined(WIN32) |
---|
| 103 | // Windows: get performance counter and subtract starting mark |
---|
| 104 | QueryPerformanceCounter(&perf_counter); |
---|
| 105 | return (double)(perf_counter.QuadPart - perf_start.QuadPart) / (double)perf_freq.QuadPart; |
---|
| 106 | #elif defined(__APPLE__) |
---|
| 107 | uint64_t difference = mach_absolute_time() - perf_start; |
---|
| 108 | return perf_conversion * (double) difference; |
---|
| 109 | #else |
---|
| 110 | // UNIX: gettimeofday |
---|
| 111 | gettimeofday(&tv,0); |
---|
| 112 | return (double)(tv.tv_sec - tv_start.tv_sec) + (double)(tv.tv_usec - tv_start.tv_usec) / 1000000.0; |
---|
| 113 | #endif |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | void aLog(char* format,...) |
---|
| 119 | { |
---|
| 120 | va_list vl; |
---|
| 121 | char line[2048]; |
---|
| 122 | |
---|
| 123 | va_start(vl,format); |
---|
| 124 | vsprintf(line,format,vl); |
---|
| 125 | va_end(vl); |
---|
| 126 | |
---|
| 127 | #if defined(WIN32) |
---|
| 128 | DWORD res; |
---|
| 129 | WriteFile(win_err, line, (DWORD)strlen(line), &res, NULL); |
---|
| 130 | #endif |
---|
| 131 | fprintf(stderr,"%s",line); |
---|
| 132 | fflush(stderr); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | void aError(char* format,...) |
---|
| 136 | { |
---|
| 137 | va_list vl; |
---|
| 138 | char line[2048]; |
---|
| 139 | |
---|
| 140 | va_start(vl,format); |
---|
| 141 | vsprintf(line,format,vl); |
---|
| 142 | va_end(vl); |
---|
| 143 | |
---|
| 144 | #if defined(WIN32) |
---|
| 145 | DWORD res; |
---|
| 146 | WriteFile(win_err, line, (DWORD)strlen(line), &res, NULL); |
---|
| 147 | #endif |
---|
| 148 | fprintf(stderr,"%s",line); |
---|
| 149 | fflush(stderr); |
---|
| 150 | |
---|
| 151 | exit(1); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | |
---|
| 155 | |
---|
| 156 | void* aAlloc(size_t const n) |
---|
| 157 | { |
---|
| 158 | void* result; |
---|
| 159 | #if defined(WIN32) |
---|
| 160 | result = LocalAlloc(0,n); |
---|
| 161 | #else |
---|
| 162 | result = malloc(n); |
---|
| 163 | #endif |
---|
| 164 | // Filling with zeros |
---|
| 165 | memset(result, 0, n); |
---|
| 166 | |
---|
| 167 | if(!result) |
---|
| 168 | aError("Aura: not enough memory for %d bytes",n); |
---|
| 169 | return result; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | void aFree(void* const p) |
---|
| 173 | { |
---|
| 174 | #if defined(WIN32) |
---|
| 175 | LocalFree(p); |
---|
| 176 | #else |
---|
| 177 | if (p) |
---|
| 178 | free(p); |
---|
| 179 | else |
---|
| 180 | aError("Alloc> Trying to free a NULL pointer\n"); |
---|
| 181 | #endif |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | #if defined(WIN32) |
---|
| 185 | float |
---|
| 186 | drand48(void) |
---|
| 187 | { |
---|
| 188 | return (((float) rand()) / RAND_MAX); |
---|
| 189 | } |
---|
| 190 | #endif |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | void *aligned_malloc(size_t size, size_t align_size) { |
---|
| 194 | |
---|
| 195 | char *ptr,*ptr2,*aligned_ptr; |
---|
| 196 | int align_mask = (int)align_size - 1; |
---|
| 197 | |
---|
| 198 | ptr=(char *)malloc(size + align_size + sizeof(int)); |
---|
| 199 | if(ptr==NULL) return(NULL); |
---|
| 200 | |
---|
| 201 | ptr2 = ptr + sizeof(int); |
---|
| 202 | aligned_ptr = ptr2 + (align_size - ((size_t)ptr2 & align_mask)); |
---|
| 203 | |
---|
| 204 | |
---|
| 205 | ptr2 = aligned_ptr - sizeof(int); |
---|
| 206 | *((int *)ptr2)=(int)(aligned_ptr - ptr); |
---|
| 207 | |
---|
| 208 | return(aligned_ptr); |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | void aligned_free(void *ptr) |
---|
| 212 | { |
---|
| 213 | int *ptr2=(int *)ptr - 1; |
---|
| 214 | ptr = (char*)ptr - *ptr2; |
---|
| 215 | free(ptr); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | |
---|
| 219 | /// File findind |
---|
| 220 | // From Nvidia toolkit |
---|
| 221 | |
---|
| 222 | using namespace std; |
---|
| 223 | |
---|
| 224 | string data_path::get_path(std::string filename) |
---|
| 225 | { |
---|
| 226 | FILE* fp; |
---|
| 227 | bool found = false; |
---|
| 228 | for(unsigned int i=0; i < path.size(); i++) |
---|
| 229 | { |
---|
| 230 | path_name = path[i] + "/" + filename; |
---|
| 231 | fp = ::fopen(path_name.c_str(), "r"); |
---|
| 232 | |
---|
| 233 | if(fp != 0) |
---|
| 234 | { |
---|
| 235 | fclose(fp); |
---|
| 236 | found = true; |
---|
| 237 | break; |
---|
| 238 | } |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | if (found == false) |
---|
| 242 | { |
---|
| 243 | path_name = filename; |
---|
| 244 | fp = ::fopen(path_name.c_str(),"r"); |
---|
| 245 | if (fp != 0) |
---|
| 246 | { |
---|
| 247 | fclose(fp); |
---|
| 248 | found = true; |
---|
| 249 | } |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | if (found == false) |
---|
| 253 | return ""; |
---|
| 254 | |
---|
| 255 | int loc = path_name.rfind('\\'); |
---|
| 256 | if (loc == -1) |
---|
| 257 | { |
---|
| 258 | loc = path_name.rfind('/'); |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | if (loc != -1) |
---|
| 262 | file_path = path_name.substr(0, loc); |
---|
| 263 | else |
---|
| 264 | file_path = "."; |
---|
| 265 | return file_path; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | string data_path::get_file(std::string filename) |
---|
| 269 | { |
---|
| 270 | FILE* fp; |
---|
| 271 | |
---|
| 272 | for(unsigned int i=0; i < path.size(); i++) |
---|
| 273 | { |
---|
| 274 | path_name = path[i] + "/" + filename; |
---|
| 275 | fp = ::fopen(path_name.c_str(), "r"); |
---|
| 276 | |
---|
| 277 | if(fp != 0) |
---|
| 278 | { |
---|
| 279 | fclose(fp); |
---|
| 280 | return path_name; |
---|
| 281 | } |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | path_name = filename; |
---|
| 285 | fp = ::fopen(path_name.c_str(),"r"); |
---|
| 286 | if (fp != 0) |
---|
| 287 | { |
---|
| 288 | fclose(fp); |
---|
| 289 | return path_name; |
---|
| 290 | } |
---|
| 291 | return ""; |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | // data files, for read only |
---|
| 295 | FILE * data_path::fopen(std::string filename, const char * mode) |
---|
| 296 | { |
---|
| 297 | |
---|
| 298 | for(unsigned int i=0; i < path.size(); i++) |
---|
| 299 | { |
---|
| 300 | std::string s = path[i] + "/" + filename; |
---|
| 301 | FILE * fp = ::fopen(s.c_str(), mode); |
---|
| 302 | |
---|
| 303 | if(fp != 0) |
---|
| 304 | return fp; |
---|
| 305 | else if (!strcmp(path[i].c_str(),"")) |
---|
| 306 | { |
---|
| 307 | FILE* fp = ::fopen(filename.c_str(),mode); |
---|
| 308 | if (fp != 0) |
---|
| 309 | return fp; |
---|
| 310 | } |
---|
| 311 | } |
---|
| 312 | // no luck... return null |
---|
| 313 | return 0; |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | // fill the file stats structure |
---|
| 317 | // useful to get the file size and stuff |
---|
| 318 | int data_path::fstat(std::string filename, |
---|
| 319 | #ifdef WIN32 |
---|
| 320 | struct _stat |
---|
| 321 | #else |
---|
| 322 | struct stat |
---|
| 323 | #endif |
---|
| 324 | * stat) |
---|
| 325 | { |
---|
| 326 | for(unsigned int i=0; i < path.size(); i++) |
---|
| 327 | { |
---|
| 328 | std::string s = path[i] + "/" + filename; |
---|
| 329 | #ifdef WIN32 |
---|
| 330 | int fh = ::_open(s.c_str(), _O_RDONLY); |
---|
| 331 | #else |
---|
| 332 | int fh = ::open(s.c_str(), O_RDONLY); |
---|
| 333 | #endif |
---|
| 334 | if(fh >= 0) |
---|
| 335 | { |
---|
| 336 | #ifdef WIN32 |
---|
| 337 | int result = ::_fstat( fh, stat ); |
---|
| 338 | #else |
---|
| 339 | int result = ::fstat (fh,stat); |
---|
| 340 | #endif |
---|
| 341 | if( result != 0 ) |
---|
| 342 | { |
---|
| 343 | fprintf( stderr, "An fstat error occurred.\n" ); |
---|
| 344 | return 0; |
---|
| 345 | } |
---|
| 346 | #ifdef WIN32 |
---|
| 347 | ::_close( fh ); |
---|
| 348 | #else |
---|
| 349 | ::close (fh); |
---|
| 350 | #endif |
---|
| 351 | return 1; |
---|
| 352 | } |
---|
| 353 | } |
---|
| 354 | // no luck... |
---|
| 355 | return 0; |
---|
| 356 | } |
---|
| 357 | |
---|