Linear Methods for Image Interpolation
basic.c
Go to the documentation of this file.
00001 
00029 #include <stdlib.h>
00030 #include <stdarg.h>
00031 #include "basic.h"
00032 
00033 
00034 /* Autodetect whether to use Windows, POSIX,
00035    or fallback implementation for Clock.  */
00036 #if !defined(USE_GETSYSTEMTIME) && !defined(USE_GETTIMEOFDAY) && !defined(USE_TIME)
00037 #   if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
00038 #       define USE_GETSYSTEMTIME
00039 #   elif defined(unix) || defined(__unix__) || defined(__unix)
00040 #       include <unistd.h>
00041 #       if (_POSIX_TIMERS) || (_POSIX_VERSION >= 200112L)
00042 #           define USE_GETTIMEOFDAY
00043 #       endif
00044 #   endif
00045 #endif
00046 
00047 /* Define Clock(), get the system clock in milliseconds */
00048 #if defined(USE_GETSYSTEMTIME)
00049 #define WIN32_LEAN_AND_MEAN
00050 #include <windows.h>
00051 
00052 unsigned long Clock()   /* Windows implementation */
00053 {
00054     static SYSTEMTIME TimeVal;
00055     GetSystemTime(&TimeVal);
00056     return (unsigned long)((unsigned long)TimeVal.wMilliseconds
00057         + 1000*((unsigned long)TimeVal.wSecond
00058         + 60*((unsigned long)TimeVal.wMinute
00059         + 60*((unsigned long)TimeVal.wHour
00060         + 24*(unsigned long)TimeVal.wDay))));
00061 }
00062 #elif defined(USE_GETTIMEOFDAY)
00063 #include <unistd.h>
00064 #include <sys/time.h>
00065 
00066 unsigned long Clock()   /* POSIX implementation */
00067 {
00068     struct timeval TimeVal;
00069     gettimeofday(&TimeVal, NULL);
00070     return (unsigned long)(TimeVal.tv_usec/1000 + TimeVal.tv_sec*1000);
00071 }
00072 #else
00073 #include <time.h>
00074 
00075 unsigned long Clock()   /* Fallback implementation */
00076 {
00077     time_t RawTime;
00078     struct tm *TimeVal;
00079     time(&RawTime);
00080     TimeVal = localtime(&RawTime);
00081     return (unsigned long)(1000*((unsigned long)TimeVal->tm_sec
00082         + 60*((unsigned long)TimeVal->tm_min
00083         + 60*((unsigned long)TimeVal->tm_hour
00084         + 24*(unsigned long)TimeVal->tm_mday))));
00085 }
00086 #endif
00087 
00088 
00090 void *MallocWithErrorMessage(size_t Size)
00091 {
00092     void *Ptr;
00093 
00094     if(!(Ptr = malloc(Size)))
00095         ErrorMessage("Memory allocation of %u bytes failed.\n", Size);
00096 
00097     return Ptr;
00098 }
00099 
00100 
00102 void *ReallocWithErrorMessage(void *Ptr, size_t Size)
00103 {
00104     void *NewPtr;
00105 
00106     if(!(NewPtr = realloc(Ptr, Size)))
00107     {
00108         ErrorMessage("Memory reallocation of %u bytes failed.\n", Size);
00109         Free(Ptr);  /* Free the previous block on failure */
00110     }
00111 
00112     return NewPtr;
00113 }
00114 
00115 
00117 void ErrorMessage(const char *Format, ...)
00118 {
00119     va_list Args;
00120 
00121     va_start(Args, Format);
00122     /* Write a formatted error message to stderr */
00123     vfprintf(stderr, Format, Args);
00124     va_end(Args);
00125 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines