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