libpgf  6.11.42
PGF - Progressive Graphics File
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
PGFplatform.h
Go to the documentation of this file.
1 /*
2  * The Progressive Graphics File; http://www.libpgf.org
3  *
4  * $Date: 2007-06-12 19:27:47 +0200 (Di, 12 Jun 2007) $
5  * $Revision: 307 $
6  *
7  * This file Copyright (C) 2006 xeraina GmbH, Switzerland
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  */
23 
28 
29 #ifndef PGF_PGFPLATFORM_H
30 #define PGF_PGFPLATFORM_H
31 
32 #include <cassert>
33 #include <cmath>
34 #include <cstdlib>
35 
36 //-------------------------------------------------------------------------------
37 // Endianess detection taken from lcms2 header.
38 // This list can be endless, so only some checks are performed over here.
39 //-------------------------------------------------------------------------------
40 #if defined(_HOST_BIG_ENDIAN) || defined(__BIG_ENDIAN__) || defined(WORDS_BIGENDIAN)
41 #define PGF_USE_BIG_ENDIAN 1
42 #endif
43 
44 #if defined(__sgi__) || defined(__sgi) || defined(__powerpc__) || defined(__sparc) || defined(__sparc__)
45 #define PGF_USE_BIG_ENDIAN 1
46 #endif
47 
48 #if defined(__ppc__) || defined(__s390__) || defined(__s390x__)
49 #define PGF_USE_BIG_ENDIAN 1
50 #endif
51 
52 #ifdef TARGET_CPU_PPC
53 #define PGF_USE_BIG_ENDIAN 1
54 #endif
55 
56 //-------------------------------------------------------------------------------
57 // ROI support
58 //-------------------------------------------------------------------------------
59 #ifndef NPGFROI
60 #define __PGFROISUPPORT__ // without ROI support the program code gets simpler and smaller
61 #endif
62 
63 //-------------------------------------------------------------------------------
64 // 32 bit per channel support
65 //-------------------------------------------------------------------------------
66 #ifndef NPGF32
67 #define __PGF32SUPPORT__ // without 32 bit the memory consumption during encoding and decoding is much lesser
68 #endif
69 
70 //-------------------------------------------------------------------------------
71 // 32 Bit platform constants
72 //-------------------------------------------------------------------------------
73 #define WordWidth 32
74 #define WordWidthLog 5
75 #define WordMask 0xFFFFFFE0
76 #define WordBytes 4
77 #define WordBytesMask 0xFFFFFFFC
78 #define WordBytesLog 2
79 
80 //-------------------------------------------------------------------------------
81 // Alignment macros (used in PGF based libraries)
82 //-------------------------------------------------------------------------------
83 #define DWWIDTHBITS(bits) (((bits) + WordWidth - 1) & WordMask)
84 #define DWWIDTH(bytes) (((bytes) + WordBytes - 1) & WordBytesMask)
85 #define DWWIDTHREST(bytes) ((WordBytes - (bytes)%WordBytes)%WordBytes)
86 
87 //-------------------------------------------------------------------------------
88 // Min-Max macros
89 //-------------------------------------------------------------------------------
90 #ifndef __min
91  #define __min(x, y) ((x) <= (y) ? (x) : (y))
92  #define __max(x, y) ((x) >= (y) ? (x) : (y))
93 #endif // __min
94 
95 //-------------------------------------------------------------------------------
96 // Defines -- Adobe image modes.
97 //-------------------------------------------------------------------------------
98 #define ImageModeBitmap 0
99 #define ImageModeGrayScale 1
100 #define ImageModeIndexedColor 2
101 #define ImageModeRGBColor 3
102 #define ImageModeCMYKColor 4
103 #define ImageModeHSLColor 5
104 #define ImageModeHSBColor 6
105 #define ImageModeMultichannel 7
106 #define ImageModeDuotone 8
107 #define ImageModeLabColor 9
108 #define ImageModeGray16 10
109 #define ImageModeRGB48 11
110 #define ImageModeLab48 12
111 #define ImageModeCMYK64 13
112 #define ImageModeDeepMultichannel 14
113 #define ImageModeDuotone16 15
114 // pgf extension
115 #define ImageModeRGBA 17
116 #define ImageModeGray32 18
117 #define ImageModeRGB12 19
118 #define ImageModeRGB16 20
119 #define ImageModeUnknown 255
120 
121 
122 //-------------------------------------------------------------------------------
123 // WINDOWS 32
124 //-------------------------------------------------------------------------------
125 #if defined(WIN32) || defined(WINCE)
126 #define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
127 
128 //-------------------------------------------------------------------------------
129 // MFC
130 //-------------------------------------------------------------------------------
131 #ifdef _MFC_VER
132 
133 #include <afxwin.h> // MFC core and standard components
134 #include <afxext.h> // MFC extensions
135 #include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
136 #ifndef _AFX_NO_AFXCMN_SUPPORT
137 #include <afxcmn.h> // MFC support for Windows Common Controls
138 #endif // _AFX_NO_AFXCMN_SUPPORT
139 #include <afx.h>
140 
141 #else
142 
143 #include <windows.h>
144 #include <ole2.h>
145 
146 #endif // _MFC_VER
147 //-------------------------------------------------------------------------------
148 
149 #define DllExport __declspec( dllexport )
150 
151 //-------------------------------------------------------------------------------
152 // unsigned number type definitions
153 //-------------------------------------------------------------------------------
154 typedef unsigned char UINT8;
155 typedef unsigned char BYTE;
156 typedef unsigned short UINT16;
157 typedef unsigned short WORD;
158 typedef unsigned int UINT32;
159 typedef unsigned long DWORD;
160 typedef unsigned long ULONG;
161 typedef unsigned __int64 UINT64;
162 typedef unsigned __int64 ULONGLONG;
163 
164 //-------------------------------------------------------------------------------
165 // signed number type definitions
166 //-------------------------------------------------------------------------------
167 typedef signed char INT8;
168 typedef signed short INT16;
169 typedef signed int INT32;
170 typedef signed int BOOL;
171 typedef signed long LONG;
172 typedef signed __int64 INT64;
173 typedef signed __int64 LONGLONG;
174 
175 //-------------------------------------------------------------------------------
176 // other types
177 //-------------------------------------------------------------------------------
178 typedef int OSError;
179 typedef bool (__cdecl *CallbackPtr)(double percent, bool escapeAllowed, void *data);
180 
181 //-------------------------------------------------------------------------------
182 // struct type definitions
183 //-------------------------------------------------------------------------------
184 
185 //-------------------------------------------------------------------------------
186 // DEBUG macros
187 //-------------------------------------------------------------------------------
188 #ifndef ASSERT
189  #ifdef _DEBUG
190  #define ASSERT(x) assert(x)
191  #else
192  #if defined(__GNUC__)
193  #define ASSERT(ignore)((void) 0)
194  #elif _MSC_VER >= 1300
195  #define ASSERT __noop
196  #else
197  #define ASSERT ((void)0)
198  #endif
199  #endif //_DEBUG
200 #endif //ASSERT
201 
202 //-------------------------------------------------------------------------------
203 // Exception handling macros
204 //-------------------------------------------------------------------------------
205 #ifdef NEXCEPTIONS
206  extern OSError _PGF_Error_;
207  extern OSError GetLastPGFError();
208 
209  #define ReturnWithError(err) { _PGF_Error_ = err; return; }
210  #define ReturnWithError2(err, ret) { _PGF_Error_ = err; return ret; }
211 #else
212  #define ReturnWithError(err) throw IOException(err)
213  #define ReturnWithError2(err, ret) throw IOException(err)
214 #endif //NEXCEPTIONS
215 
216 #if _MSC_VER >= 1300
217  //#define THROW_ throw(...)
218  #pragma warning( disable : 4290 )
219  #define THROW_ throw(IOException)
220 #else
221  #define THROW_
222 #endif
223 
224 //-------------------------------------------------------------------------------
225 // constants
226 //-------------------------------------------------------------------------------
227 #define FSFromStart FILE_BEGIN // 0
228 #define FSFromCurrent FILE_CURRENT // 1
229 #define FSFromEnd FILE_END // 2
230 
231 #define INVALID_SET_FILE_POINTER ((DWORD)-1)
232 
233 //-------------------------------------------------------------------------------
234 // IO Error constants
235 //-------------------------------------------------------------------------------
236 #define NoError ERROR_SUCCESS
237 #define AppError 0x20000000
238 #define InsufficientMemory 0x20000001
239 #define InvalidStreamPos 0x20000002
240 #define EscapePressed 0x20000003
241 #define WrongVersion 0x20000004
242 #define FormatCannotRead 0x20000005
243 #define ImageTooSmall 0x20000006
244 #define ZlibError 0x20000007
245 #define ColorTableError 0x20000008
246 #define PNGError 0x20000009
247 #define MissingData 0x2000000A
248 
249 //-------------------------------------------------------------------------------
250 // methods
251 //-------------------------------------------------------------------------------
252 inline OSError FileRead(HANDLE hFile, int *count, void *buffPtr) {
253  if (ReadFile(hFile, buffPtr, *count, (ULONG *)count, NULL)) {
254  return NoError;
255  } else {
256  return GetLastError();
257  }
258 }
259 
260 inline OSError FileWrite(HANDLE hFile, int *count, void *buffPtr) {
261  if (WriteFile(hFile, buffPtr, *count, (ULONG *)count, NULL)) {
262  return NoError;
263  } else {
264  return GetLastError();
265  }
266 }
267 
268 inline OSError GetFPos(HANDLE hFile, UINT64 *pos) {
269 #ifdef WINCE
270  LARGE_INTEGER li;
271  li.QuadPart = 0;
272 
273  li.LowPart = SetFilePointer (hFile, li.LowPart, &li.HighPart, FILE_CURRENT);
274  if (li.LowPart == INVALID_SET_FILE_POINTER) {
275  OSError err = GetLastError();
276  if (err != NoError) {
277  return err;
278  }
279  }
280  *pos = li.QuadPart;
281  return NoError;
282 #else
283  LARGE_INTEGER li;
284  li.QuadPart = 0;
285  if (SetFilePointerEx(hFile, li, (PLARGE_INTEGER)pos, FILE_CURRENT)) {
286  return NoError;
287  } else {
288  return GetLastError();
289  }
290 #endif
291 }
292 
293 inline OSError SetFPos(HANDLE hFile, int posMode, INT64 posOff) {
294 #ifdef WINCE
295  LARGE_INTEGER li;
296  li.QuadPart = posOff;
297 
298  if (SetFilePointer (hFile, li.LowPart, &li.HighPart, posMode) == INVALID_SET_FILE_POINTER) {
299  OSError err = GetLastError();
300  if (err != NoError) {
301  return err;
302  }
303  }
304  return NoError;
305 #else
306  LARGE_INTEGER li;
307  li.QuadPart = posOff;
308  if (SetFilePointerEx(hFile, li, NULL, posMode)) {
309  return NoError;
310  } else {
311  return GetLastError();
312  }
313 #endif
314 }
315 #endif //WIN32
316 
317 
318 //-------------------------------------------------------------------------------
319 // Apple OSX
320 //-------------------------------------------------------------------------------
321 #ifdef __APPLE__
322 #define __POSIX__
323 #endif // __APPLE__
324 
325 
326 //-------------------------------------------------------------------------------
327 // LINUX
328 //-------------------------------------------------------------------------------
329 #if defined(__linux__) || defined(__GLIBC__)
330 #define __POSIX__
331 #endif // __linux__ or __GLIBC__
332 
333 
334 //-------------------------------------------------------------------------------
335 // SOLARIS
336 //-------------------------------------------------------------------------------
337 #ifdef __sun
338 #define __POSIX__
339 #endif // __sun
340 
341 
342 //-------------------------------------------------------------------------------
343 // *BSD
344 //-------------------------------------------------------------------------------
345 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
346 #ifndef __POSIX__
347 #define __POSIX__
348 #endif
349 
350 #ifndef off64_t
351 #define off64_t off_t
352 #endif
353 
354 #ifndef lseek64
355 #define lseek64 lseek
356 #endif
357 
358 #endif // __NetBSD__ or __OpenBSD__ or __FreeBSD__
359 
360 
361 //-------------------------------------------------------------------------------
362 // POSIX *NIXes
363 //-------------------------------------------------------------------------------
364 
365 #ifdef __POSIX__
366 #include <unistd.h>
367 #include <errno.h>
368 #include <stdint.h> // for int64_t and uint64_t
369 #include <string.h> // memcpy()
370 
371 //-------------------------------------------------------------------------------
372 // unsigned number type definitions
373 //-------------------------------------------------------------------------------
374 
375 typedef unsigned char UINT8;
376 typedef unsigned char BYTE;
377 typedef unsigned short UINT16;
378 typedef unsigned short WORD;
379 typedef unsigned int UINT32;
380 typedef unsigned int DWORD;
381 typedef unsigned long ULONG;
382 typedef unsigned long long __Uint64;
383 typedef __Uint64 UINT64;
384 typedef __Uint64 ULONGLONG;
385 
386 //-------------------------------------------------------------------------------
387 // signed number type definitions
388 //-------------------------------------------------------------------------------
389 typedef signed char INT8;
390 typedef signed short INT16;
391 typedef signed int INT32;
392 typedef signed int BOOL;
393 typedef signed long LONG;
394 typedef int64_t INT64;
395 typedef int64_t LONGLONG;
396 
397 //-------------------------------------------------------------------------------
398 // other types
399 //-------------------------------------------------------------------------------
400 typedef int OSError;
401 typedef int HANDLE;
402 typedef unsigned long ULONG_PTR;
403 typedef void* PVOID;
404 typedef char* LPTSTR;
405 typedef bool (*CallbackPtr)(double percent, bool escapeAllowed, void *data);
406 
407 //-------------------------------------------------------------------------------
408 // struct type definitions
409 //-------------------------------------------------------------------------------
410 typedef struct tagRGBTRIPLE {
411  BYTE rgbtBlue;
412  BYTE rgbtGreen;
413  BYTE rgbtRed;
414 } RGBTRIPLE;
415 
416 typedef struct tagRGBQUAD {
417  BYTE rgbBlue;
418  BYTE rgbGreen;
419  BYTE rgbRed;
420  BYTE rgbReserved;
421 } RGBQUAD;
422 
423 typedef union _LARGE_INTEGER {
424  struct {
425  DWORD LowPart;
426  LONG HighPart;
427  };
428  struct {
429  DWORD LowPart;
430  LONG HighPart;
431  } u;
432  LONGLONG QuadPart;
433 } LARGE_INTEGER, *PLARGE_INTEGER;
434 #endif // __POSIX__
435 
436 
437 #if defined(__POSIX__) || defined(WINCE)
438 // CMYK macros
439 #define GetKValue(cmyk) ((BYTE)(cmyk))
440 #define GetYValue(cmyk) ((BYTE)((cmyk)>> 8))
441 #define GetMValue(cmyk) ((BYTE)((cmyk)>>16))
442 #define GetCValue(cmyk) ((BYTE)((cmyk)>>24))
443 #define CMYK(c,m,y,k) ((COLORREF)((((BYTE)(k)|((WORD)((BYTE)(y))<<8))|(((DWORD)(BYTE)(m))<<16))|(((DWORD)(BYTE)(c))<<24)))
444 
445 //-------------------------------------------------------------------------------
446 // methods
447 //-------------------------------------------------------------------------------
448 /* The MulDiv function multiplies two 32-bit values and then divides the 64-bit
449  * result by a third 32-bit value. The return value is rounded up or down to
450  * the nearest integer.
451  * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winprog/winprog/muldiv.asp
452  * */
453 __inline int MulDiv(int nNumber, int nNumerator, int nDenominator) {
454  INT64 multRes = nNumber*nNumerator;
455  INT32 divRes = INT32(multRes/nDenominator);
456  return divRes;
457 }
458 #endif // __POSIX__ or WINCE
459 
460 
461 #ifdef __POSIX__
462 //-------------------------------------------------------------------------------
463 // DEBUG macros
464 //-------------------------------------------------------------------------------
465 #ifndef ASSERT
466  #ifdef _DEBUG
467  #define ASSERT(x) assert(x)
468  #else
469  #define ASSERT(x)
470  #endif //_DEBUG
471 #endif //ASSERT
472 
473 //-------------------------------------------------------------------------------
474 // Exception handling macros
475 //-------------------------------------------------------------------------------
476 #ifdef NEXCEPTIONS
477  extern OSError _PGF_Error_;
478  extern OSError GetLastPGFError();
479 
480  #define ReturnWithError(err) { _PGF_Error_ = err; return; }
481  #define ReturnWithError2(err, ret) { _PGF_Error_ = err; return ret; }
482 #else
483  #define ReturnWithError(err) throw IOException(err)
484  #define ReturnWithError2(err, ret) throw IOException(err)
485 #endif //NEXCEPTIONS
486 
487 #define THROW_ throw(IOException)
488 #define CONST const
489 
490 //-------------------------------------------------------------------------------
491 // constants
492 //-------------------------------------------------------------------------------
493 #define FSFromStart SEEK_SET
494 #define FSFromCurrent SEEK_CUR
495 #define FSFromEnd SEEK_END
496 
497 //-------------------------------------------------------------------------------
498 // IO Error constants
499 //-------------------------------------------------------------------------------
500 #define NoError 0x0000
501 #define AppError 0x2000
502 #define InsufficientMemory 0x2001
503 #define InvalidStreamPos 0x2002
504 #define EscapePressed 0x2003
505 #define WrongVersion 0x2004
506 #define FormatCannotRead 0x2005
507 #define ImageTooSmall 0x2006
508 #define ZlibError 0x2007
509 #define ColorTableError 0x2008
510 #define PNGError 0x2009
511 #define MissingData 0x200A
512 
513 //-------------------------------------------------------------------------------
514 // methods
515 //-------------------------------------------------------------------------------
516 __inline OSError FileRead(HANDLE hFile, int *count, void *buffPtr) {
517  *count = (int)read(hFile, buffPtr, *count);
518  if (*count != -1) {
519  return NoError;
520  } else {
521  return errno;
522  }
523 }
524 
525 __inline OSError FileWrite(HANDLE hFile, int *count, void *buffPtr) {
526  *count = (int)write(hFile, buffPtr, (size_t)*count);
527  if (*count != -1) {
528  return NoError;
529  } else {
530  return errno;
531  }
532 }
533 
534 __inline OSError GetFPos(HANDLE hFile, UINT64 *pos) {
535  #ifdef __APPLE__
536  off_t ret;
537  if ((ret = lseek(hFile, 0, SEEK_CUR)) == -1) {
538  return errno;
539  } else {
540  *pos = (UINT64)ret;
541  return NoError;
542  }
543  #else
544  off64_t ret;
545  if ((ret = lseek64(hFile, 0, SEEK_CUR)) == -1) {
546  return errno;
547  } else {
548  *pos = (UINT64)ret;
549  return NoError;
550  }
551  #endif
552 }
553 
554 __inline OSError SetFPos(HANDLE hFile, int posMode, INT64 posOff) {
555  #ifdef __APPLE__
556  if ((lseek(hFile, (off_t)posOff, posMode)) == -1) {
557  return errno;
558  } else {
559  return NoError;
560  }
561  #else
562  if ((lseek64(hFile, (off64_t)posOff, posMode)) == -1) {
563  return errno;
564  } else {
565  return NoError;
566  }
567  #endif
568 }
569 
570 #endif /* __POSIX__ */
571 //-------------------------------------------------------------------------------
572 
573 
574 //-------------------------------------------------------------------------------
575 // Big Endian
576 //-------------------------------------------------------------------------------
577 #ifdef PGF_USE_BIG_ENDIAN
578 
579 #ifndef _lrotl
580  #define _lrotl(x,n) (((x) << ((UINT32)(n))) | ((x) >> (32 - (UINT32)(n))))
581 #endif
582 
583 __inline UINT16 ByteSwap(UINT16 wX) {
584  return ((wX & 0xFF00) >> 8) | ((wX & 0x00FF) << 8);
585 }
586 
587 __inline UINT32 ByteSwap(UINT32 dwX) {
588 #ifdef _X86_
589  _asm mov eax, dwX
590  _asm bswap eax
591  _asm mov dwX, eax
592  return dwX;
593 #else
594  return _lrotl(((dwX & 0xFF00FF00) >> 8) | ((dwX & 0x00FF00FF) << 8), 16);
595 #endif
596 }
597 
598 #if defined WIN32
599 __inline UINT64 ByteSwap(UINT64 ui64) {
600  return _byteswap_uint64(ui64);
601 }
602 #endif
603 
604 #define __VAL(x) ByteSwap(x)
605 
606 #else //PGF_USE_BIG_ENDIAN
607 
608  #define __VAL(x) (x)
609 
610 #endif //PGF_USE_BIG_ENDIAN
611 
612 // OpenMP rules (inspired from libraw project)
613 // NOTE: Use LIBPGF_DISABLE_OPENMP to disable OpenMP support in whole libpgf
614 #ifndef LIBPGF_DISABLE_OPENMP
615 #if defined (_OPENMP)
616 # if defined (WIN32)
617 # if defined (_MSC_VER) && (_MSC_VER >= 1500)
618 // VS2008 SP1 and VS2010+ : OpenMP works OK
619 # define LIBPGF_USE_OPENMP
620 # elif defined (__INTEL_COMPILER) && (__INTEL_COMPILER >=910)
621 // untested on 9.x and 10.x, Intel documentation claims OpenMP 2.5 support in 9.1
622 # define LIBPGF_USE_OPENMP
623 # else
624 # undef LIBPGF_USE_OPENMP
625 # endif
626 // Not Win32
627 # elif (defined(__APPLE__) || defined(__MACOSX__)) && defined(_REENTRANT)
628 # undef LIBPGF_USE_OPENMP
629 # else
630 # define LIBPGF_USE_OPENMP
631 # endif
632 #endif // defined (_OPENMP)
633 #endif // ifndef LIBPGF_DISABLE_OPENMP
634 #ifdef LIBPGF_USE_OPENMP
635 #include <omp.h>
636 #endif
637 
638 #endif //PGF_PGFPLATFORM_H