1 /*
2  * The Progressive Graphics File; http://www.libpgf.org
3  *
4  * $Date: 2007-06-11 10:56:17 +0200 (Mo, 11 Jun 2007) $
5  * $Revision: 299 $
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 
24 //////////////////////////////////////////////////////////////////////
25 /// @file PGFtypes.h
26 /// @brief PGF definitions
27 /// @author C. Stamm
28 
29 #ifndef PGF_PGFTYPES_H
30 #define PGF_PGFTYPES_H
31 
32 #include "PGFplatform.h"
33 
34 //-------------------------------------------------------------------------------
35 //	Codec versions
36 //
37 // Version 2:	modified data structure PGFHeader (backward compatibility assured)
38 // Version 4:	DataT: INT32 instead of INT16, allows 31 bit per pixel and channel (backward compatibility assured)
39 // Version 5:	ROI, new block-reordering scheme (backward compatibility assured)
40 // Version 6:	modified data structure PGFPreHeader: hSize (header size) is now a UINT32 instead of a UINT16 (backward compatibility assured)
41 // Version 7:	last two bytes in header are now used for extended version numbers; new data representation for bitmaps (backward compatibility assured)
42 //
43 //-------------------------------------------------------------------------------
44 #define PGFMajorNumber		7
45 #define PGFYear				19
46 #define	PGFWeek				3
47 
48 #define PPCAT_NX(A, B) A ## B
49 #define PPCAT(A, B) PPCAT_NX(A, B)
50 #define STRINGIZE_NX(A) #A
51 #define STRINGIZE(A) STRINGIZE_NX(A)
52 
53 //#define PGFCodecVersionID		0x071822
54 #define PGFCodecVersionID PPCAT(PPCAT(PPCAT(0x0, PGFMajorNumber), PGFYear), PGFWeek)
55 //#define PGFCodecVersion		"7.19.3"			///< Major number, Minor number: Year (2) Week (2)
56 #define PGFCodecVersion STRINGIZE(PPCAT(PPCAT(PPCAT(PPCAT(PGFMajorNumber, .), PGFYear), .), PGFWeek))
57 
58 //-------------------------------------------------------------------------------
59 //	Image constants
60 //-------------------------------------------------------------------------------
61 #define PGFMagic			"PGF"				///< PGF identification
62 #define MaxLevel			30					///< maximum number of transform levels
63 #define NSubbands			4					///< number of subbands per level
64 #define MaxChannels			8					///< maximum number of (color) channels
65 #define DownsampleThreshold 3					///< if quality is larger than this threshold than downsampling is used
66 #define ColorTableLen		256					///< size of color lookup table (clut)
67 // version flags
68 #define Version2			2					///< data structure PGFHeader of major version 2
69 #define PGF32				4					///< 32 bit values are used -> allows at maximum 31 bits, otherwise 16 bit values are used -> allows at maximum 15 bits
70 #define PGFROI				8					///< supports Regions Of Interest
71 #define Version5			16					///< new coding scheme since major version 5
72 #define Version6			32					///< hSize in PGFPreHeader uses 32 bits instead of 16 bits
73 #define Version7			64					///< Codec major and minor version number stored in PGFHeader
74 // version numbers
75 #ifdef __PGF32SUPPORT__
76 #define PGFVersion			(Version2 | PGF32 | Version5 | Version6 | Version7)	///< current standard version
77 #else
78 #define PGFVersion			(Version2 |         Version5 | Version6 | Version7)	///< current standard version
79 #endif
80 
81 //-------------------------------------------------------------------------------
82 //	Coder constants
83 //-------------------------------------------------------------------------------
84 #define BufferSize			16384				///< must be a multiple of WordWidth, BufferSize <= UINT16_MAX
85 #define RLblockSizeLen		15					///< block size length (< 16): ld(BufferSize) < RLblockSizeLen <= 2*ld(BufferSize)
86 #define LinBlockSize		8					///< side length of a coefficient block in a HH or LL subband
87 #define InterBlockSize		4					///< side length of a coefficient block in a HL or LH subband
88 #ifdef __PGF32SUPPORT__
89 	#define MaxBitPlanes	31					///< maximum number of bit planes of m_value: 32 minus sign bit
90 #else
91 	#define MaxBitPlanes	15					///< maximum number of bit planes of m_value: 16 minus sign bit
92 #endif
93 #define MaxBitPlanesLog		5					///< number of bits to code the maximum number of bit planes (in 32 or 16 bit mode)
94 #define MaxQuality			MaxBitPlanes		///< maximum quality
95 
96 //-------------------------------------------------------------------------------
97 // Types
98 //-------------------------------------------------------------------------------
99 enum Orientation		{ LL = 0, HL = 1, LH = 2, HH = 3 };
100 enum ProgressMode		{ PM_Relative, PM_Absolute };
101 enum UserdataPolicy		{ UP_Skip = 0, UP_CachePrefix = 1, UP_CacheAll = 2 };
102 
103 /// general PGF file structure
104 /// PGFPreHeader PGFHeader [PGFPostHeader] LevelLengths Level_n-1 Level_n-2 ... Level_0
105 /// PGFPostHeader ::= [ColorTable] [UserData]
106 /// LevelLengths  ::= UINT32[nLevels]
107 
108 #pragma pack(1)
109 /////////////////////////////////////////////////////////////////////
110 /// PGF magic and version (part of PGF pre-header)
111 /// @author C. Stamm
112 /// @brief PGF identification and version
113 struct PGFMagicVersion {
114 	char magic[3];				///< PGF identification = "PGF"
115 	UINT8 version;				///< PGF version
116 	// total: 4 Bytes
117 };
118 
119 /////////////////////////////////////////////////////////////////////
120 /// PGF pre-header defined header length and PGF identification and version
121 /// @author C. Stamm
122 /// @brief PGF pre-header
123 struct PGFPreHeader : PGFMagicVersion {
124 	UINT32 hSize;				///< total size of PGFHeader, [ColorTable], and [UserData] in bytes (since Version 6: 4 Bytes)
125 	// total: 8 Bytes
126 };
127 
128 /////////////////////////////////////////////////////////////////////
129 /// Version number since major version 7
130 /// @author C. Stamm
131 /// @brief version number stored in header since major version 7
132 struct PGFVersionNumber {
PGFVersionNumberPGFVersionNumber133 	PGFVersionNumber(UINT8 _major, UINT8 _year, UINT8 _week) : major(_major), year(_year), week(_week) {}
134 
135 #ifdef PGF_USE_BIG_ENDIAN
136 	UINT16 week  : 6;	///< week number in a year
137 	UINT16 year  : 6;	///< year since 2000 (year 2001 = 1)
138 	UINT16 major : 4;	///< major version number
139 #else
140 	UINT16 major : 4;	///< major version number
141 	UINT16 year  : 6;	///< year since 2000 (year 2001 = 1)
142 	UINT16 week  : 6;	///< week number in a year
143 #endif // PGF_USE_BIG_ENDIAN
144 };
145 
146 /////////////////////////////////////////////////////////////////////
147 /// PGF header contains image information
148 /// @author C. Stamm
149 /// @brief PGF header
150 struct PGFHeader {
PGFHeaderPGFHeader151 	PGFHeader() : width(0), height(0), nLevels(0), quality(0), bpp(0), channels(0), mode(ImageModeUnknown), usedBitsPerChannel(0), version(0, 0, 0) {}
152 	UINT32 width;				///< image width in pixels
153 	UINT32 height;				///< image height in pixels
154 	UINT8 nLevels;				///< number of FWT transforms
155 	UINT8 quality;				///< quantization parameter: 0=lossless, 4=standard, 6=poor quality
156 	UINT8 bpp;					///< bits per pixel
157 	UINT8 channels;				///< number of channels
158 	UINT8 mode;					///< image mode according to Adobe's image modes
159 	UINT8 usedBitsPerChannel;	///< number of used bits per channel in 16- and 32-bit per channel modes
160 	PGFVersionNumber version;	///< codec version number: (since Version 7)
161 	// total: 16 Bytes
162 };
163 
164 /////////////////////////////////////////////////////////////////////
165 /// PGF post-header is optional. It contains color table and user data
166 /// @author C. Stamm
167 /// @brief Optional PGF post-header
168 struct PGFPostHeader {
169 	RGBQUAD clut[ColorTableLen];///< color table for indexed color images (optional part of file header)
170 	UINT8 *userData;			///< user data of size userDataLen (optional part of file header)
171 	UINT32 userDataLen;			///< user data size in bytes (not part of file header)
172 	UINT32 cachedUserDataLen;	///< cached user data size in bytes (not part of file header)
173 };
174 
175 /////////////////////////////////////////////////////////////////////
176 /// ROI block header is used with ROI coding scheme. It contains block size and tile end flag
177 /// @author C. Stamm
178 /// @brief Block header used with ROI coding scheme
179 union ROIBlockHeader {
180 	UINT16 val; ///< unstructured union value
181 	/// @brief Named ROI block header (part of the union)
182 	struct RBH {
183 #ifdef PGF_USE_BIG_ENDIAN
184 		UINT16 tileEnd   :				1;	///< 1: last part of a tile
185 		UINT16 bufferSize: RLblockSizeLen;	///< number of uncoded UINT32 values in a block
186 #else
187 		UINT16 bufferSize: RLblockSizeLen;	///< number of uncoded UINT32 values in a block
188 		UINT16 tileEnd   :				1;	///< 1: last part of a tile
189 #endif // PGF_USE_BIG_ENDIAN
190 	} rbh;	///< ROI block header
191 	// total: 2 Bytes
192 
193 	/// Constructor
194 	/// @param v Buffer size
ROIBlockHeader(UINT16 v)195 	ROIBlockHeader(UINT16 v) { val = v; }
196 
197 	/// Constructor
198 	/// @param size Buffer size
199 	/// @param end 0/1 Flag; 1: last part of a tile
ROIBlockHeader(UINT32 size,bool end)200 	ROIBlockHeader(UINT32 size, bool end) { ASSERT(size < (1 << RLblockSizeLen)); rbh.bufferSize = size; rbh.tileEnd = end; }
201 };
202 
203 #pragma pack()
204 
205 /////////////////////////////////////////////////////////////////////
206 /// PGF I/O exception
207 /// @author C. Stamm
208 /// @brief PGF exception
209 struct IOException {
210 	OSError error;				///< operating system error code
211 
212 	/// Standard constructor
IOExceptionIOException213 	IOException() : error(NoError) {}
214 
215 	/// Constructor
216 	/// @param err Run-time error
IOExceptionIOException217 	IOException(OSError err) : error(err) {}
218 };
219 
220 /////////////////////////////////////////////////////////////////////
221 /// Rectangle
222 /// @author C. Stamm
223 /// @brief Rectangle
224 struct PGFRect {
225 	UINT32 left, top, right, bottom;
226 
227 	/// Standard constructor
PGFRectPGFRect228 	PGFRect() : left(0), top(0), right(0), bottom(0) {}
229 
230 	/// Constructor
231 	/// @param x Left offset
232 	/// @param y Top offset
233 	/// @param width Rectangle width
234 	/// @param height Rectangle height
PGFRectPGFRect235 	PGFRect(UINT32 x, UINT32 y, UINT32 width, UINT32 height) : left(x), top(y), right(x + width), bottom(y + height) {}
236 
237 #ifdef WIN32
PGFRectPGFRect238 	PGFRect(const RECT& rect) : left(rect.left), top(rect.top), right(rect.right), bottom(rect.bottom) {
239 		ASSERT(rect.left >= 0 && rect.right >= 0 && rect.left <= rect.right);
240 		ASSERT(rect.top >= 0 && rect.bottom >= 0 && rect.top <= rect.bottom);
241 	}
242 
243 	PGFRect& operator=(const RECT& rect) {
244 		left = rect.left; top = rect.top; right = rect.right; bottom = rect.bottom;
245 		return *this;
246 	}
247 
RECTPGFRect248 	operator RECT() {
249 		RECT rect = { (LONG)left, (LONG)top, (LONG)right, (LONG)bottom };
250 		return rect;
251 	}
252 #endif
253 
254 	/// @return Rectangle width
WidthPGFRect255 	UINT32 Width() const					{ return right - left; }
256 
257 	/// @return Rectangle height
HeightPGFRect258 	UINT32 Height() const					{ return bottom - top; }
259 
260 	/// Test if point (x,y) is inside this rectangle (inclusive top-left edges, exclusive bottom-right edges)
261 	/// @param x Point coordinate x
262 	/// @param y Point coordinate y
263 	/// @return True if point (x,y) is inside this rectangle (inclusive top-left edges, exclusive bottom-right edges)
IsInsidePGFRect264 	bool IsInside(UINT32 x, UINT32 y) const { return (x >= left && x < right && y >= top && y < bottom); }
265 };
266 
267 #ifdef __PGF32SUPPORT__
268 typedef INT32 DataT;
269 #else
270 typedef INT16 DataT;
271 #endif
272 
273 typedef void (*RefreshCB)(void *p);
274 
275 //-------------------------------------------------------------------------------
276 // Image constants
277 //-------------------------------------------------------------------------------
278 #define MagicVersionSize	sizeof(PGFMagicVersion)
279 #define PreHeaderSize		sizeof(PGFPreHeader)
280 #define HeaderSize			sizeof(PGFHeader)
281 #define ColorTableSize		(ColorTableLen*sizeof(RGBQUAD))
282 #define DataTSize			sizeof(DataT)
283 #define MaxUserDataSize		0x7FFFFFFF
284 
285 #endif //PGF_PGFTYPES_H
286