00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00028
00029 #ifndef PGF_PGFPLATFORM_H
00030 #define PGF_PGFPLATFORM_H
00031
00032 #include <cassert>
00033 #include <cmath>
00034 #include <cstdlib>
00035
00036
00037
00038
00039
00040 #if defined(_HOST_BIG_ENDIAN) || defined(__BIG_ENDIAN__) || defined(WORDS_BIGENDIAN)
00041 #define PGF_USE_BIG_ENDIAN 1
00042 #endif
00043
00044 #if defined(__sgi__) || defined(__sgi) || defined(__powerpc__) || defined(__sparc) || defined(__sparc__)
00045 #define PGF_USE_BIG_ENDIAN 1
00046 #endif
00047
00048 #if defined(__ppc__) || defined(__s390__) || defined(__s390x__)
00049 #define PGF_USE_BIG_ENDIAN 1
00050 #endif
00051
00052 #ifdef TARGET_CPU_PPC
00053 #define PGF_USE_BIG_ENDIAN 1
00054 #endif
00055
00056
00057
00058
00059 #ifndef NPGFROI
00060 #define __PGFROISUPPORT__ // without ROI support the program code gets simpler and smaller
00061 #endif
00062
00063
00064
00065
00066 #ifndef NPGF32
00067 #define __PGF32SUPPORT__ // without 32 bit the memory consumption during encoding and decoding is much lesser
00068 #endif
00069
00070
00071
00072
00073 #define WordWidth 32
00074 #define WordWidthLog 5
00075 #define WordMask 0xFFFFFFE0
00076 #define WordBytes 4
00077 #define WordBytesMask 0xFFFFFFFC
00078 #define WordBytesLog 2
00079
00080
00081
00082
00083 #define DWWIDTHBITS(bits) (((bits) + WordWidth - 1) & WordMask)
00084 #define DWWIDTH(bytes) (((bytes) + WordBytes - 1) & WordBytesMask)
00085 #define DWWIDTHREST(bytes) ((WordBytes - (bytes)%WordBytes)%WordBytes)
00086
00087
00088
00089
00090 #ifndef __min
00091 #define __min(x, y) ((x) <= (y) ? (x) : (y))
00092 #define __max(x, y) ((x) >= (y) ? (x) : (y))
00093 #endif // __min
00094
00095
00096
00097
00098 #define ImageModeBitmap 0
00099 #define ImageModeGrayScale 1
00100 #define ImageModeIndexedColor 2
00101 #define ImageModeRGBColor 3
00102 #define ImageModeCMYKColor 4
00103 #define ImageModeHSLColor 5
00104 #define ImageModeHSBColor 6
00105 #define ImageModeMultichannel 7
00106 #define ImageModeDuotone 8
00107 #define ImageModeLabColor 9
00108 #define ImageModeGray16 10 // 565
00109 #define ImageModeRGB48 11
00110 #define ImageModeLab48 12
00111 #define ImageModeCMYK64 13
00112 #define ImageModeDeepMultichannel 14
00113 #define ImageModeDuotone16 15
00114
00115 #define ImageModeRGBA 17
00116 #define ImageModeGray32 18 // MSB is 0 (can be interpreted as signed 15.16 fixed point format)
00117 #define ImageModeRGB12 19
00118 #define ImageModeRGB16 20
00119 #define ImageModeUnknown 255
00120
00121
00122
00123
00124
00125 #if defined(WIN32) || defined(WINCE) || defined(WIN64)
00126 #define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
00127
00128
00129
00130
00131 #ifdef _MFC_VER
00132
00133 #include <afxwin.h>
00134 #include <afxext.h>
00135 #include <afxdtctl.h>
00136 #ifndef _AFX_NO_AFXCMN_SUPPORT
00137 #include <afxcmn.h>
00138 #endif // _AFX_NO_AFXCMN_SUPPORT
00139 #include <afx.h>
00140
00141 #else
00142
00143 #include <windows.h>
00144 #include <ole2.h>
00145
00146 #endif // _MFC_VER
00147
00148
00149 #define DllExport __declspec( dllexport )
00150
00151
00152
00153
00154 typedef unsigned char UINT8;
00155 typedef unsigned char BYTE;
00156 typedef unsigned short UINT16;
00157 typedef unsigned short WORD;
00158 typedef unsigned int UINT32;
00159 typedef unsigned long DWORD;
00160 typedef unsigned long ULONG;
00161 typedef unsigned __int64 UINT64;
00162 typedef unsigned __int64 ULONGLONG;
00163
00164
00165
00166
00167 typedef signed char INT8;
00168 typedef signed short INT16;
00169 typedef signed int INT32;
00170 typedef signed int BOOL;
00171 typedef signed long LONG;
00172 typedef signed __int64 INT64;
00173 typedef signed __int64 LONGLONG;
00174
00175
00176
00177
00178 typedef int OSError;
00179 typedef bool (__cdecl *CallbackPtr)(double percent, bool escapeAllowed, void *data);
00180
00181
00182
00183
00184
00185
00186
00187
00188 #ifndef ASSERT
00189 #ifdef _DEBUG
00190 #define ASSERT(x) assert(x)
00191 #else
00192 #if defined(__GNUC__)
00193 #define ASSERT(ignore)((void) 0)
00194 #elif _MSC_VER >= 1300
00195 #define ASSERT __noop
00196 #else
00197 #define ASSERT ((void)0)
00198 #endif
00199 #endif //_DEBUG
00200 #endif //ASSERT
00201
00202
00203
00204
00205 #ifdef NEXCEPTIONS
00206 extern OSError _PGF_Error_;
00207 extern OSError GetLastPGFError();
00208
00209 #define ReturnWithError(err) { _PGF_Error_ = err; return; }
00210 #define ReturnWithError2(err, ret) { _PGF_Error_ = err; return ret; }
00211 #else
00212 #define ReturnWithError(err) throw IOException(err)
00213 #define ReturnWithError2(err, ret) throw IOException(err)
00214 #endif //NEXCEPTIONS
00215
00216 #if _MSC_VER >= 1300
00217
00218 #pragma warning( disable : 4290 )
00219 #define THROW_ throw(IOException)
00220 #else
00221 #define THROW_
00222 #endif
00223
00224
00225
00226
00227 #define FSFromStart FILE_BEGIN // 0
00228 #define FSFromCurrent FILE_CURRENT // 1
00229 #define FSFromEnd FILE_END // 2
00230
00231 #define INVALID_SET_FILE_POINTER ((DWORD)-1)
00232
00233
00234
00235
00236 #define NoError ERROR_SUCCESS
00237 #define AppError 0x20000000
00238 #define InsufficientMemory 0x20000001
00239 #define InvalidStreamPos 0x20000002
00240 #define EscapePressed 0x20000003
00241 #define WrongVersion 0x20000004
00242 #define FormatCannotRead 0x20000005
00243 #define ImageTooSmall 0x20000006
00244 #define ZlibError 0x20000007
00245 #define ColorTableError 0x20000008
00246 #define PNGError 0x20000009
00247 #define MissingData 0x2000000A
00248
00249
00250
00251
00252 inline OSError FileRead(HANDLE hFile, int *count, void *buffPtr) {
00253 if (ReadFile(hFile, buffPtr, *count, (ULONG *)count, NULL)) {
00254 return NoError;
00255 } else {
00256 return GetLastError();
00257 }
00258 }
00259
00260 inline OSError FileWrite(HANDLE hFile, int *count, void *buffPtr) {
00261 if (WriteFile(hFile, buffPtr, *count, (ULONG *)count, NULL)) {
00262 return NoError;
00263 } else {
00264 return GetLastError();
00265 }
00266 }
00267
00268 inline OSError GetFPos(HANDLE hFile, UINT64 *pos) {
00269 #ifdef WINCE
00270 LARGE_INTEGER li;
00271 li.QuadPart = 0;
00272
00273 li.LowPart = SetFilePointer (hFile, li.LowPart, &li.HighPart, FILE_CURRENT);
00274 if (li.LowPart == INVALID_SET_FILE_POINTER) {
00275 OSError err = GetLastError();
00276 if (err != NoError) {
00277 return err;
00278 }
00279 }
00280 *pos = li.QuadPart;
00281 return NoError;
00282 #else
00283 LARGE_INTEGER li;
00284 li.QuadPart = 0;
00285 if (SetFilePointerEx(hFile, li, (PLARGE_INTEGER)pos, FILE_CURRENT)) {
00286 return NoError;
00287 } else {
00288 return GetLastError();
00289 }
00290 #endif
00291 }
00292
00293 inline OSError SetFPos(HANDLE hFile, int posMode, INT64 posOff) {
00294 #ifdef WINCE
00295 LARGE_INTEGER li;
00296 li.QuadPart = posOff;
00297
00298 if (SetFilePointer (hFile, li.LowPart, &li.HighPart, posMode) == INVALID_SET_FILE_POINTER) {
00299 OSError err = GetLastError();
00300 if (err != NoError) {
00301 return err;
00302 }
00303 }
00304 return NoError;
00305 #else
00306 LARGE_INTEGER li;
00307 li.QuadPart = posOff;
00308 if (SetFilePointerEx(hFile, li, NULL, posMode)) {
00309 return NoError;
00310 } else {
00311 return GetLastError();
00312 }
00313 #endif
00314 }
00315 #endif //WIN32
00316
00317
00318
00319
00320
00321 #ifdef __APPLE__
00322 #define __POSIX__
00323 #endif // __APPLE__
00324
00325
00326
00327
00328
00329 #if defined(__linux__) || defined(__GLIBC__)
00330 #define __POSIX__
00331 #endif // __linux__ or __GLIBC__
00332
00333
00334
00335
00336
00337 #ifdef __sun
00338 #define __POSIX__
00339 #endif // __sun
00340
00341
00342
00343
00344
00345 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
00346 #ifndef __POSIX__
00347 #define __POSIX__
00348 #endif
00349
00350 #ifndef off64_t
00351 #define off64_t off_t
00352 #endif
00353
00354 #ifndef lseek64
00355 #define lseek64 lseek
00356 #endif
00357
00358 #endif // __NetBSD__ or __OpenBSD__ or __FreeBSD__
00359
00360
00361
00362
00363
00364
00365 #ifdef __POSIX__
00366 #include <unistd.h>
00367 #include <errno.h>
00368 #include <stdint.h>
00369 #include <string.h>
00370
00371
00372
00373
00374
00375 typedef unsigned char UINT8;
00376 typedef unsigned char BYTE;
00377 typedef unsigned short UINT16;
00378 typedef unsigned short WORD;
00379 typedef unsigned int UINT32;
00380 typedef unsigned int DWORD;
00381 typedef unsigned long ULONG;
00382 typedef unsigned long long __Uint64;
00383 typedef __Uint64 UINT64;
00384 typedef __Uint64 ULONGLONG;
00385
00386
00387
00388
00389 typedef signed char INT8;
00390 typedef signed short INT16;
00391 typedef signed int INT32;
00392 typedef signed int BOOL;
00393 typedef signed long LONG;
00394 typedef int64_t INT64;
00395 typedef int64_t LONGLONG;
00396
00397
00398
00399
00400 typedef int OSError;
00401 typedef int HANDLE;
00402 typedef unsigned long ULONG_PTR;
00403 typedef void* PVOID;
00404 typedef char* LPTSTR;
00405 typedef bool (*CallbackPtr)(double percent, bool escapeAllowed, void *data);
00406
00407
00408
00409
00410 typedef struct tagRGBTRIPLE {
00411 BYTE rgbtBlue;
00412 BYTE rgbtGreen;
00413 BYTE rgbtRed;
00414 } RGBTRIPLE;
00415
00416 typedef struct tagRGBQUAD {
00417 BYTE rgbBlue;
00418 BYTE rgbGreen;
00419 BYTE rgbRed;
00420 BYTE rgbReserved;
00421 } RGBQUAD;
00422
00423 typedef union _LARGE_INTEGER {
00424 struct {
00425 DWORD LowPart;
00426 LONG HighPart;
00427 } u;
00428 LONGLONG QuadPart;
00429 } LARGE_INTEGER, *PLARGE_INTEGER;
00430 #endif // __POSIX__
00431
00432
00433 #if defined(__POSIX__) || defined(WINCE)
00434
00435 #define GetKValue(cmyk) ((BYTE)(cmyk))
00436 #define GetYValue(cmyk) ((BYTE)((cmyk)>> 8))
00437 #define GetMValue(cmyk) ((BYTE)((cmyk)>>16))
00438 #define GetCValue(cmyk) ((BYTE)((cmyk)>>24))
00439 #define CMYK(c,m,y,k) ((COLORREF)((((BYTE)(k)|((WORD)((BYTE)(y))<<8))|(((DWORD)(BYTE)(m))<<16))|(((DWORD)(BYTE)(c))<<24)))
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449 __inline int MulDiv(int nNumber, int nNumerator, int nDenominator) {
00450 INT64 multRes = nNumber*nNumerator;
00451 INT32 divRes = INT32(multRes/nDenominator);
00452 return divRes;
00453 }
00454 #endif // __POSIX__ or WINCE
00455
00456
00457 #ifdef __POSIX__
00458
00459
00460
00461 #ifndef ASSERT
00462 #ifdef _DEBUG
00463 #define ASSERT(x) assert(x)
00464 #else
00465 #define ASSERT(x)
00466 #endif //_DEBUG
00467 #endif //ASSERT
00468
00469
00470
00471
00472 #ifdef NEXCEPTIONS
00473 extern OSError _PGF_Error_;
00474 extern OSError GetLastPGFError();
00475
00476 #define ReturnWithError(err) { _PGF_Error_ = err; return; }
00477 #define ReturnWithError2(err, ret) { _PGF_Error_ = err; return ret; }
00478 #else
00479 #define ReturnWithError(err) throw IOException(err)
00480 #define ReturnWithError2(err, ret) throw IOException(err)
00481 #endif //NEXCEPTIONS
00482
00483 #define THROW_ throw(IOException)
00484 #define CONST const
00485
00486
00487
00488
00489 #define FSFromStart SEEK_SET
00490 #define FSFromCurrent SEEK_CUR
00491 #define FSFromEnd SEEK_END
00492
00493
00494
00495
00496 #define NoError 0x0000
00497 #define AppError 0x2000
00498 #define InsufficientMemory 0x2001
00499 #define InvalidStreamPos 0x2002
00500 #define EscapePressed 0x2003
00501 #define WrongVersion 0x2004
00502 #define FormatCannotRead 0x2005
00503 #define ImageTooSmall 0x2006
00504 #define ZlibError 0x2007
00505 #define ColorTableError 0x2008
00506 #define PNGError 0x2009
00507 #define MissingData 0x200A
00508
00509
00510
00511
00512 __inline OSError FileRead(HANDLE hFile, int *count, void *buffPtr) {
00513 *count = (int)read(hFile, buffPtr, *count);
00514 if (*count != -1) {
00515 return NoError;
00516 } else {
00517 return errno;
00518 }
00519 }
00520
00521 __inline OSError FileWrite(HANDLE hFile, int *count, void *buffPtr) {
00522 *count = (int)write(hFile, buffPtr, (size_t)*count);
00523 if (*count != -1) {
00524 return NoError;
00525 } else {
00526 return errno;
00527 }
00528 }
00529
00530 __inline OSError GetFPos(HANDLE hFile, UINT64 *pos) {
00531 #ifdef __APPLE__
00532 off_t ret;
00533 if ((ret = lseek(hFile, 0, SEEK_CUR)) == -1) {
00534 return errno;
00535 } else {
00536 *pos = (UINT64)ret;
00537 return NoError;
00538 }
00539 #else
00540 off64_t ret;
00541 if ((ret = lseek64(hFile, 0, SEEK_CUR)) == -1) {
00542 return errno;
00543 } else {
00544 *pos = (UINT64)ret;
00545 return NoError;
00546 }
00547 #endif
00548 }
00549
00550 __inline OSError SetFPos(HANDLE hFile, int posMode, INT64 posOff) {
00551 #ifdef __APPLE__
00552 if ((lseek(hFile, (off_t)posOff, posMode)) == -1) {
00553 return errno;
00554 } else {
00555 return NoError;
00556 }
00557 #else
00558 if ((lseek64(hFile, (off64_t)posOff, posMode)) == -1) {
00559 return errno;
00560 } else {
00561 return NoError;
00562 }
00563 #endif
00564 }
00565
00566 #endif
00567
00568
00569
00570
00571
00572
00573 #ifdef PGF_USE_BIG_ENDIAN
00574
00575 #ifndef _lrotl
00576 #define _lrotl(x,n) (((x) << ((UINT32)(n))) | ((x) >> (32 - (UINT32)(n))))
00577 #endif
00578
00579 __inline UINT16 ByteSwap(UINT16 wX) {
00580 return ((wX & 0xFF00) >> 8) | ((wX & 0x00FF) << 8);
00581 }
00582
00583 __inline UINT32 ByteSwap(UINT32 dwX) {
00584 #ifdef _X86_
00585 _asm mov eax, dwX
00586 _asm bswap eax
00587 _asm mov dwX, eax
00588 return dwX;
00589 #else
00590 return _lrotl(((dwX & 0xFF00FF00) >> 8) | ((dwX & 0x00FF00FF) << 8), 16);
00591 #endif
00592 }
00593
00594 #if defined(WIN32) || defined(WIN64)
00595 __inline UINT64 ByteSwap(UINT64 ui64) {
00596 return _byteswap_uint64(ui64);
00597 }
00598 #endif
00599
00600 #define __VAL(x) ByteSwap(x)
00601
00602 #else //PGF_USE_BIG_ENDIAN
00603
00604 #define __VAL(x) (x)
00605
00606 #endif //PGF_USE_BIG_ENDIAN
00607
00608
00609
00610 #ifndef LIBPGF_DISABLE_OPENMP
00611 # if defined (_OPENMP)
00612 # if defined (WIN32) || defined(WIN64)
00613 # if defined (_MSC_VER) && (_MSC_VER >= 1500)
00614
00615 # define LIBPGF_USE_OPENMP
00616 # elif defined (__INTEL_COMPILER) && (__INTEL_COMPILER >=910)
00617
00618 # define LIBPGF_USE_OPENMP
00619 # else
00620 # undef LIBPGF_USE_OPENMP
00621 # endif
00622
00623 # elif (defined(__APPLE__) || defined(__MACOSX__)) && defined(_REENTRANT)
00624 # undef LIBPGF_USE_OPENMP
00625 # else
00626 # define LIBPGF_USE_OPENMP
00627 # endif
00628 # endif // defined (_OPENMP)
00629 #endif // ifndef LIBPGF_DISABLE_OPENMP
00630 #ifdef LIBPGF_USE_OPENMP
00631 #include <omp.h>
00632 #endif
00633
00634 #endif //PGF_PGFPLATFORM_H