libpgf  7.21.2
PGF - Progressive Graphics File
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 // 565
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 // MSB is 0 (can be interpreted as signed 15.16 fixed point format)
117 #define ImageModeRGB12 19
118 #define ImageModeRGB16 20
119 #define ImageModeUnknown 255
120 
121 
122 //-------------------------------------------------------------------------------
123 // WINDOWS
124 //-------------------------------------------------------------------------------
125 #if defined(WIN32) || defined(WINCE) || defined(WIN64)
126 #define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
127 
128 //-------------------------------------------------------------------------------
129 // MFC
130 //-------------------------------------------------------------------------------
131 #ifdef _MFC_VER
132 #ifndef _WIN32_WINNT // Specifies that the minimum required platform is Windows Vista.
133 #define _WIN32_WINNT 0x0600 // Change this to the appropriate value to target other versions of Windows.
134 #endif
135 #include <afx.h>
136 #include <afxwin.h> // MFC core and standard components
137 #include <afxext.h> // MFC extensions
138 #include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
139 #ifndef _AFX_NO_AFXCMN_SUPPORT
140 #include <afxcmn.h> // MFC support for Windows Common Controls
141 #endif // _AFX_NO_AFXCMN_SUPPORT
142 
143 #else
144 
145 #include <windows.h>
146 #include <ole2.h>
147 
148 #endif // _MFC_VER
149 //-------------------------------------------------------------------------------
150 
151 #define DllExport __declspec( dllexport )
152 
153 //-------------------------------------------------------------------------------
154 // unsigned number type definitions
155 //-------------------------------------------------------------------------------
156 typedef unsigned char UINT8;
157 typedef unsigned char BYTE;
158 typedef unsigned short UINT16;
159 typedef unsigned short WORD;
160 typedef unsigned int UINT32;
161 typedef unsigned long DWORD;
162 typedef unsigned long ULONG;
163 typedef unsigned __int64 UINT64;
164 typedef unsigned __int64 ULONGLONG;
165 
166 //-------------------------------------------------------------------------------
167 // signed number type definitions
168 //-------------------------------------------------------------------------------
169 typedef signed char INT8;
170 typedef signed short INT16;
171 typedef signed int INT32;
172 typedef signed int BOOL;
173 typedef signed long LONG;
174 typedef signed __int64 INT64;
175 typedef signed __int64 LONGLONG;
176 
177 //-------------------------------------------------------------------------------
178 // other types
179 //-------------------------------------------------------------------------------
180 typedef int OSError;
181 typedef bool (__cdecl *CallbackPtr)(double percent, bool escapeAllowed, void *data);
182 
183 //-------------------------------------------------------------------------------
184 // struct type definitions
185 //-------------------------------------------------------------------------------
186 
187 //-------------------------------------------------------------------------------
188 // DEBUG macros
189 //-------------------------------------------------------------------------------
190 #ifndef ASSERT
191  #ifdef _DEBUG
192  #define ASSERT(x) assert(x)
193  #else
194  #if defined(__GNUC__)
195  #define ASSERT(ignore)((void) 0)
196  #elif _MSC_VER >= 1300
197  #define ASSERT __noop
198  #else
199  #define ASSERT ((void)0)
200  #endif
201  #endif //_DEBUG
202 #endif //ASSERT
203 
204 //-------------------------------------------------------------------------------
205 // Exception handling macros
206 //-------------------------------------------------------------------------------
207 #ifdef NEXCEPTIONS
208  extern OSError _PGF_Error_;
209  extern OSError GetLastPGFError();
210 
211  #define ReturnWithError(err) { _PGF_Error_ = err; return; }
212  #define ReturnWithError2(err, ret) { _PGF_Error_ = err; return ret; }
213 #else
214  #define ReturnWithError(err) throw IOException(err)
215  #define ReturnWithError2(err, ret) throw IOException(err)
216 #endif //NEXCEPTIONS
217 
218 //-------------------------------------------------------------------------------
219 // constants
220 //-------------------------------------------------------------------------------
221 #define FSFromStart FILE_BEGIN // 0
222 #define FSFromCurrent FILE_CURRENT // 1
223 #define FSFromEnd FILE_END // 2
224 
225 #define INVALID_SET_FILE_POINTER ((DWORD)-1)
226 
227 //-------------------------------------------------------------------------------
228 // IO Error constants
229 //-------------------------------------------------------------------------------
230 #define NoError ERROR_SUCCESS
231 #define AppError 0x20000000
232 #define InsufficientMemory 0x20000001
233 #define InvalidStreamPos 0x20000002
234 #define EscapePressed 0x20000003
235 #define WrongVersion 0x20000004
236 #define FormatCannotRead 0x20000005
237 #define ImageTooSmall 0x20000006
238 #define ZlibError 0x20000007
239 #define ColorTableError 0x20000008
240 #define PNGError 0x20000009
241 #define MissingData 0x2000000A
242 
243 //-------------------------------------------------------------------------------
244 // methods
245 //-------------------------------------------------------------------------------
246 inline OSError FileRead(HANDLE hFile, int *count, void *buffPtr) {
247  if (ReadFile(hFile, buffPtr, *count, (ULONG *)count, nullptr)) {
248  return NoError;
249  } else {
250  return GetLastError();
251  }
252 }
253 
254 inline OSError FileWrite(HANDLE hFile, int *count, void *buffPtr) {
255  if (WriteFile(hFile, buffPtr, *count, (ULONG *)count, nullptr)) {
256  return NoError;
257  } else {
258  return GetLastError();
259  }
260 }
261 
262 inline OSError GetFPos(HANDLE hFile, UINT64 *pos) {
263 #ifdef WINCE
264  LARGE_INTEGER li;
265  li.QuadPart = 0;
266 
267  li.LowPart = SetFilePointer (hFile, li.LowPart, &li.HighPart, FILE_CURRENT);
268  if (li.LowPart == INVALID_SET_FILE_POINTER) {
269  OSError err = GetLastError();
270  if (err != NoError) {
271  return err;
272  }
273  }
274  *pos = li.QuadPart;
275  return NoError;
276 #else
277  LARGE_INTEGER li;
278  li.QuadPart = 0;
279  if (SetFilePointerEx(hFile, li, (PLARGE_INTEGER)pos, FILE_CURRENT)) {
280  return NoError;
281  } else {
282  return GetLastError();
283  }
284 #endif
285 }
286 
287 inline OSError SetFPos(HANDLE hFile, int posMode, INT64 posOff) {
288 #ifdef WINCE
289  LARGE_INTEGER li;
290  li.QuadPart = posOff;
291 
292  if (SetFilePointer (hFile, li.LowPart, &li.HighPart, posMode) == INVALID_SET_FILE_POINTER) {
293  OSError err = GetLastError();
294  if (err != NoError) {
295  return err;
296  }
297  }
298  return NoError;
299 #else
300  LARGE_INTEGER li;
301  li.QuadPart = posOff;
302  if (SetFilePointerEx(hFile, li, nullptr, posMode)) {
303  return NoError;
304  } else {
305  return GetLastError();
306  }
307 #endif
308 }
309 #endif //WIN32
310 
311 
312 //-------------------------------------------------------------------------------
313 // Apple OSX
314 //-------------------------------------------------------------------------------
315 #ifdef __APPLE__
316 #define __POSIX__
317 #endif // __APPLE__
318 
319 
320 //-------------------------------------------------------------------------------
321 // LINUX
322 //-------------------------------------------------------------------------------
323 #if defined(__linux__) || defined(__GLIBC__)
324 #define __POSIX__
325 #endif // __linux__ or __GLIBC__
326 
327 
328 //-------------------------------------------------------------------------------
329 // SOLARIS
330 //-------------------------------------------------------------------------------
331 #ifdef __sun
332 #define __POSIX__
333 #endif // __sun
334 
335 
336 //-------------------------------------------------------------------------------
337 // *BSD
338 //-------------------------------------------------------------------------------
339 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
340 #ifndef __POSIX__
341 #define __POSIX__
342 #endif
343 
344 #ifndef off64_t
345 #define off64_t off_t
346 #endif
347 
348 #ifndef lseek64
349 #define lseek64 lseek
350 #endif
351 
352 #endif // __NetBSD__ or __OpenBSD__ or __FreeBSD__
353 
354 
355 //-------------------------------------------------------------------------------
356 // POSIX *NIXes
357 //-------------------------------------------------------------------------------
358 
359 #ifdef __POSIX__
360 #include <unistd.h>
361 #include <errno.h>
362 #include <stdint.h> // for int64_t and uint64_t
363 #include <string.h> // memcpy()
364 
365 #undef major
366 
367 //-------------------------------------------------------------------------------
368 // unsigned number type definitions
369 //-------------------------------------------------------------------------------
370 
371 typedef unsigned char UINT8;
372 typedef unsigned char BYTE;
373 typedef unsigned short UINT16;
374 typedef unsigned short WORD;
375 typedef unsigned int UINT32;
376 typedef unsigned int DWORD;
377 typedef unsigned long ULONG;
378 typedef unsigned long long __Uint64;
379 typedef __Uint64 UINT64;
380 typedef __Uint64 ULONGLONG;
381 
382 //-------------------------------------------------------------------------------
383 // signed number type definitions
384 //-------------------------------------------------------------------------------
385 typedef signed char INT8;
386 typedef signed short INT16;
387 typedef signed int INT32;
388 typedef signed int BOOL;
389 typedef signed long LONG;
390 typedef int64_t INT64;
391 typedef int64_t LONGLONG;
392 
393 //-------------------------------------------------------------------------------
394 // other types
395 //-------------------------------------------------------------------------------
396 typedef int OSError;
397 typedef int HANDLE;
398 typedef unsigned long ULONG_PTR;
399 typedef void* PVOID;
400 typedef char* LPTSTR;
401 typedef bool (*CallbackPtr)(double percent, bool escapeAllowed, void *data);
402 
403 //-------------------------------------------------------------------------------
404 // struct type definitions
405 //-------------------------------------------------------------------------------
406 typedef struct tagRGBTRIPLE {
407  BYTE rgbtBlue;
408  BYTE rgbtGreen;
409  BYTE rgbtRed;
410 } RGBTRIPLE;
411 
412 typedef struct tagRGBQUAD {
413  BYTE rgbBlue;
414  BYTE rgbGreen;
415  BYTE rgbRed;
416  BYTE rgbReserved;
417 } RGBQUAD;
418 
419 typedef union _LARGE_INTEGER {
420  struct {
421  DWORD LowPart;
422  LONG HighPart;
423  } u;
424  LONGLONG QuadPart;
425 } LARGE_INTEGER, *PLARGE_INTEGER;
426 #endif // __POSIX__
427 
428 
429 #if defined(__POSIX__) || defined(WINCE)
430 // CMYK macros
431 #define GetKValue(cmyk) ((BYTE)(cmyk))
432 #define GetYValue(cmyk) ((BYTE)((cmyk)>> 8))
433 #define GetMValue(cmyk) ((BYTE)((cmyk)>>16))
434 #define GetCValue(cmyk) ((BYTE)((cmyk)>>24))
435 #define CMYK(c,m,y,k) ((COLORREF)((((BYTE)(k)|((WORD)((BYTE)(y))<<8))|(((DWORD)(BYTE)(m))<<16))|(((DWORD)(BYTE)(c))<<24)))
436 
437 //-------------------------------------------------------------------------------
438 // methods
439 //-------------------------------------------------------------------------------
440 /* The MulDiv function multiplies two 32-bit values and then divides the 64-bit
441  * result by a third 32-bit value. The return value is rounded up or down to
442  * the nearest integer.
443  * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winprog/winprog/muldiv.asp
444  * */
445 __inline int MulDiv(int nNumber, int nNumerator, int nDenominator) {
446  INT64 multRes = nNumber*nNumerator;
447  INT32 divRes = INT32(multRes/nDenominator);
448  return divRes;
449 }
450 #endif // __POSIX__ or WINCE
451 
452 
453 #ifdef __POSIX__
454 //-------------------------------------------------------------------------------
455 // DEBUG macros
456 //-------------------------------------------------------------------------------
457 #ifndef ASSERT
458  #ifdef _DEBUG
459  #define ASSERT(x) assert(x)
460  #else
461  #define ASSERT(x)
462  #endif //_DEBUG
463 #endif //ASSERT
464 
465 //-------------------------------------------------------------------------------
466 // Exception handling macros
467 //-------------------------------------------------------------------------------
468 #ifdef NEXCEPTIONS
469  extern OSError _PGF_Error_;
470  extern OSError GetLastPGFError();
471 
472  #define ReturnWithError(err) { _PGF_Error_ = err; return; }
473  #define ReturnWithError2(err, ret) { _PGF_Error_ = err; return ret; }
474 #else
475  #define ReturnWithError(err) throw IOException(err)
476  #define ReturnWithError2(err, ret) throw IOException(err)
477 #endif //NEXCEPTIONS
478 
479 #define THROW_ throw(IOException)
480 #define CONST const
481 
482 //-------------------------------------------------------------------------------
483 // constants
484 //-------------------------------------------------------------------------------
485 #define FSFromStart SEEK_SET
486 #define FSFromCurrent SEEK_CUR
487 #define FSFromEnd SEEK_END
488 #if defined(__cplusplus) && __cplusplus < 201103L
489  #define nullptr NULL
490 #endif
491 
492 //-------------------------------------------------------------------------------
493 // IO Error constants
494 //-------------------------------------------------------------------------------
495 #define NoError 0x0000
496 #define AppError 0x2000
497 #define InsufficientMemory 0x2001
498 #define InvalidStreamPos 0x2002
499 #define EscapePressed 0x2003
500 #define WrongVersion 0x2004
501 #define FormatCannotRead 0x2005
502 #define ImageTooSmall 0x2006
503 #define ZlibError 0x2007
504 #define ColorTableError 0x2008
505 #define PNGError 0x2009
506 #define MissingData 0x200A
507 
508 //-------------------------------------------------------------------------------
509 // methods
510 //-------------------------------------------------------------------------------
511 __inline OSError FileRead(HANDLE hFile, int *count, void *buffPtr) {
512  *count = (int)read(hFile, buffPtr, *count);
513  if (*count != -1) {
514  return NoError;
515  } else {
516  return errno;
517  }
518 }
519 
520 __inline OSError FileWrite(HANDLE hFile, int *count, void *buffPtr) {
521  *count = (int)write(hFile, buffPtr, (size_t)*count);
522  if (*count != -1) {
523  return NoError;
524  } else {
525  return errno;
526  }
527 }
528 
529 __inline OSError GetFPos(HANDLE hFile, UINT64 *pos) {
530  #ifdef __APPLE__
531  off_t ret;
532  if ((ret = lseek(hFile, 0, SEEK_CUR)) == -1) {
533  return errno;
534  } else {
535  *pos = (UINT64)ret;
536  return NoError;
537  }
538  #else
539  off64_t ret;
540  if ((ret = lseek64(hFile, 0, SEEK_CUR)) == -1) {
541  return errno;
542  } else {
543  *pos = (UINT64)ret;
544  return NoError;
545  }
546  #endif
547 }
548 
549 __inline OSError SetFPos(HANDLE hFile, int posMode, INT64 posOff) {
550  #ifdef __APPLE__
551  if ((lseek(hFile, (off_t)posOff, posMode)) == -1) {
552  return errno;
553  } else {
554  return NoError;
555  }
556  #else
557  if ((lseek64(hFile, (off64_t)posOff, posMode)) == -1) {
558  return errno;
559  } else {
560  return NoError;
561  }
562  #endif
563 }
564 
565 #endif /* __POSIX__ */
566 //-------------------------------------------------------------------------------
567 
568 
569 //-------------------------------------------------------------------------------
570 // Big Endian
571 //-------------------------------------------------------------------------------
572 #ifdef PGF_USE_BIG_ENDIAN
573 
574 #ifndef _lrotl
575  #define _lrotl(x,n) (((x) << ((UINT32)(n))) | ((x) >> (32 - (UINT32)(n))))
576 #endif
577 
578 __inline UINT16 ByteSwap(UINT16 wX) {
579  return ((wX & 0xFF00) >> 8) | ((wX & 0x00FF) << 8);
580 }
581 
582 __inline UINT32 ByteSwap(UINT32 dwX) {
583 #ifdef _X86_
584  _asm mov eax, dwX
585  _asm bswap eax
586  _asm mov dwX, eax
587  return dwX;
588 #else
589  return _lrotl(((dwX & 0xFF00FF00) >> 8) | ((dwX & 0x00FF00FF) << 8), 16);
590 #endif
591 }
592 
593 #if defined(WIN32) || defined(WIN64)
594 __inline UINT64 ByteSwap(UINT64 ui64) {
595  return _byteswap_uint64(ui64);
596 }
597 #endif
598 
599 #define __VAL(x) ByteSwap(x)
600 
601 #else //PGF_USE_BIG_ENDIAN
602 
603  #define __VAL(x) (x)
604 
605 #endif //PGF_USE_BIG_ENDIAN
606 
607 // OpenMP rules (inspired from libraw project)
608 // NOTE: Use LIBPGF_DISABLE_OPENMP to disable OpenMP support in whole libpgf
609 #ifndef LIBPGF_DISABLE_OPENMP
610 # if defined (_OPENMP)
611 # if defined (WIN32) || defined(WIN64)
612 # if defined (_MSC_VER) && (_MSC_VER >= 1500)
613 // VS2008 SP1 and VS2010+ : OpenMP works OK
614 # define LIBPGF_USE_OPENMP
615 # elif defined (__INTEL_COMPILER) && (__INTEL_COMPILER >=910)
616 // untested on 9.x and 10.x, Intel documentation claims OpenMP 2.5 support in 9.1
617 # define LIBPGF_USE_OPENMP
618 # else
619 # undef LIBPGF_USE_OPENMP
620 # endif
621 // Not Win32
622 # elif (defined(__APPLE__) || defined(__MACOSX__)) && defined(_REENTRANT)
623 # undef LIBPGF_USE_OPENMP
624 # else
625 # define LIBPGF_USE_OPENMP
626 # endif
627 # endif // defined (_OPENMP)
628 #endif // ifndef LIBPGF_DISABLE_OPENMP
629 #ifdef LIBPGF_USE_OPENMP
630 #include <omp.h>
631 #endif
632 
633 #endif //PGF_PGFPLATFORM_H