Scribus
Open source desktop publishing at your fingertips
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 
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  } u;
428  LONGLONG QuadPart;
429 } LARGE_INTEGER, *PLARGE_INTEGER;
430 #endif // __POSIX__
431 
432 
433 #if defined(__POSIX__) || defined(WINCE)
434 // CMYK macros
435 #define GetKValue(cmyk) ((BYTE)(cmyk))
436 #define GetYValue(cmyk) ((BYTE)((cmyk)>> 8))
437 #define GetMValue(cmyk) ((BYTE)((cmyk)>>16))
438 #define GetCValue(cmyk) ((BYTE)((cmyk)>>24))
439 #define CMYK(c,m,y,k) ((COLORREF)((((BYTE)(k)|((WORD)((BYTE)(y))<<8))|(((DWORD)(BYTE)(m))<<16))|(((DWORD)(BYTE)(c))<<24)))
440 
441 //-------------------------------------------------------------------------------
442 // methods
443 //-------------------------------------------------------------------------------
444 /* The MulDiv function multiplies two 32-bit values and then divides the 64-bit
445  * result by a third 32-bit value. The return value is rounded up or down to
446  * the nearest integer.
447  * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winprog/winprog/muldiv.asp
448  * */
449 __inline int MulDiv(int nNumber, int nNumerator, int nDenominator) {
450  INT64 multRes = nNumber*nNumerator;
451  INT32 divRes = INT32(multRes/nDenominator);
452  return divRes;
453 }
454 #endif // __POSIX__ or WINCE
455 
456 
457 #ifdef __POSIX__
458 //-------------------------------------------------------------------------------
459 // DEBUG macros
460 //-------------------------------------------------------------------------------
461 #ifndef ASSERT
462  #ifdef _DEBUG
463  #define ASSERT(x) assert(x)
464  #else
465  #define ASSERT(x)
466  #endif //_DEBUG
467 #endif //ASSERT
468 
469 //-------------------------------------------------------------------------------
470 // Exception handling macros
471 //-------------------------------------------------------------------------------
472 #ifdef NEXCEPTIONS
473  extern OSError _PGF_Error_;
474  extern OSError GetLastPGFError();
475 
476  #define ReturnWithError(err) { _PGF_Error_ = err; return; }
477  #define ReturnWithError2(err, ret) { _PGF_Error_ = err; return ret; }
478 #else
479  #define ReturnWithError(err) throw IOException(err)
480  #define ReturnWithError2(err, ret) throw IOException(err)
481 #endif //NEXCEPTIONS
482 
483 #define THROW_ throw(IOException)
484 #define CONST const
485 
486 //-------------------------------------------------------------------------------
487 // constants
488 //-------------------------------------------------------------------------------
489 #define FSFromStart SEEK_SET
490 #define FSFromCurrent SEEK_CUR
491 #define FSFromEnd SEEK_END
492 
493 //-------------------------------------------------------------------------------
494 // IO Error constants
495 //-------------------------------------------------------------------------------
496 #define NoError 0x0000
497 #define AppError 0x2000
498 #define InsufficientMemory 0x2001
499 #define InvalidStreamPos 0x2002
500 #define EscapePressed 0x2003
501 #define WrongVersion 0x2004
502 #define FormatCannotRead 0x2005
503 #define ImageTooSmall 0x2006
504 #define ZlibError 0x2007
505 #define ColorTableError 0x2008
506 #define PNGError 0x2009
507 #define MissingData 0x200A
508 
509 //-------------------------------------------------------------------------------
510 // methods
511 //-------------------------------------------------------------------------------
512 __inline OSError FileRead(HANDLE hFile, int *count, void *buffPtr) {
513  *count = (int)read(hFile, buffPtr, *count);
514  if (*count != -1) {
515  return NoError;
516  } else {
517  return errno;
518  }
519 }
520 
521 __inline OSError FileWrite(HANDLE hFile, int *count, void *buffPtr) {
522  *count = (int)write(hFile, buffPtr, (size_t)*count);
523  if (*count != -1) {
524  return NoError;
525  } else {
526  return errno;
527  }
528 }
529 
530 __inline OSError GetFPos(HANDLE hFile, UINT64 *pos) {
531  #ifdef __APPLE__
532  off_t ret;
533  if ((ret = lseek(hFile, 0, SEEK_CUR)) == -1) {
534  return errno;
535  } else {
536  *pos = (UINT64)ret;
537  return NoError;
538  }
539  #else
540  off64_t ret;
541  if ((ret = lseek64(hFile, 0, SEEK_CUR)) == -1) {
542  return errno;
543  } else {
544  *pos = (UINT64)ret;
545  return NoError;
546  }
547  #endif
548 }
549 
550 __inline OSError SetFPos(HANDLE hFile, int posMode, INT64 posOff) {
551  #ifdef __APPLE__
552  if ((lseek(hFile, (off_t)posOff, posMode)) == -1) {
553  return errno;
554  } else {
555  return NoError;
556  }
557  #else
558  if ((lseek64(hFile, (off64_t)posOff, posMode)) == -1) {
559  return errno;
560  } else {
561  return NoError;
562  }
563  #endif
564 }
565 
566 #endif /* __POSIX__ */
567 //-------------------------------------------------------------------------------
568 
569 
570 //-------------------------------------------------------------------------------
571 // Big Endian
572 //-------------------------------------------------------------------------------
573 #ifdef PGF_USE_BIG_ENDIAN
574 
575 #ifndef _lrotl
576  #define _lrotl(x,n) (((x) << ((UINT32)(n))) | ((x) >> (32 - (UINT32)(n))))
577 #endif
578 
579 __inline UINT16 ByteSwap(UINT16 wX) {
580  return ((wX & 0xFF00) >> 8) | ((wX & 0x00FF) << 8);
581 }
582 
583 __inline UINT32 ByteSwap(UINT32 dwX) {
584 #ifdef _X86_
585  _asm mov eax, dwX
586  _asm bswap eax
587  _asm mov dwX, eax
588  return dwX;
589 #else
590  return _lrotl(((dwX & 0xFF00FF00) >> 8) | ((dwX & 0x00FF00FF) << 8), 16);
591 #endif
592 }
593 
594 #if defined(WIN32) || defined(WIN64)
595 __inline UINT64 ByteSwap(UINT64 ui64) {
596  return _byteswap_uint64(ui64);
597 }
598 #endif
599 
600 #define __VAL(x) ByteSwap(x)
601 
602 #else //PGF_USE_BIG_ENDIAN
603 
604  #define __VAL(x) (x)
605 
606 #endif //PGF_USE_BIG_ENDIAN
607 
608 // OpenMP rules (inspired from libraw project)
609 // NOTE: Use LIBPGF_DISABLE_OPENMP to disable OpenMP support in whole libpgf
610 #define LIBPGF_DISABLE_OPENMP
611 #undef LIBPGF_USE_OPENMP
612 
613 #ifndef LIBPGF_DISABLE_OPENMP
614 # if defined (_OPENMP)
615 # if defined (WIN32) || defined(WIN64)
616 # if defined (_MSC_VER) && (_MSC_VER >= 1500)
617 // VS2008 SP1 and VS2010+ : OpenMP works OK
618 # define LIBPGF_USE_OPENMP
619 # elif defined (__INTEL_COMPILER) && (__INTEL_COMPILER >=910)
620 // untested on 9.x and 10.x, Intel documentation claims OpenMP 2.5 support in 9.1
621 # define LIBPGF_USE_OPENMP
622 # else
623 # undef LIBPGF_USE_OPENMP
624 # endif
625 // Not Win32
626 # elif (defined(__APPLE__) || defined(__MACOSX__)) && defined(_REENTRANT)
627 # undef LIBPGF_USE_OPENMP
628 # else
629 # define LIBPGF_USE_OPENMP
630 # endif
631 # endif // defined (_OPENMP)
632 #endif // ifndef LIBPGF_DISABLE_OPENMP
633 #ifdef LIBPGF_USE_OPENMP
634 #include <omp.h>
635 #endif
636 
637 #endif //PGF_PGFPLATFORM_H