summaryrefslogtreecommitdiff
path: root/fitz/fitz_base.h
blob: 6bee3ecb90433bfbd43c642cca5559d8a913777d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
 * Include the basic standard libc headers.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <assert.h>
#include <stdarg.h>

#include <limits.h>	/* INT_MIN, MAX ... */
#include <float.h>	/* DBL_EPSILON */
#include <math.h>

#include <errno.h>
#include <fcntl.h>	/* O_RDONLY & co */

/* Stupid macros that don't exist everywhere */

#ifndef O_BINARY
#define O_BINARY 0
#endif

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

#ifndef M_SQRT2
#define M_SQRT2 1.41421356237309504880
#endif

/* Some useful semi-standard functions */

extern char *fz_strsep(char **stringp, const char *delim);
extern int fz_strlcpy(char *dst, const char *src, int n);
extern int fz_strlcat(char *dst, const char *src, int n);

#ifdef NEED_STRSEP
#define strsep fz_strsep
#endif

#ifdef NEED_STRLCPY
#define strlcpy fz_strlcpy
#define strlcat fz_strlcat
#endif

#ifdef NEED_GETOPT
extern int getopt(int nargc, char * const * nargv, const char *ostr);
extern int opterr, optind, optopt;
extern char *optarg;
#endif

#ifdef MSVC /* stupid stone-age compiler */

#include <io.h>

extern int gettimeofday(struct timeval *tv, struct timezone *tz);

#define FZ_FLEX 1
#define restrict

#ifdef _MSC_VER
#define inline __inline
#else
#define inline __inline__
#endif

#define __func__ __FUNCTION__

#if _MSC_VER < 1500
#define vsnprintf _vsnprintf
#endif

#ifndef isnan
#define isnan _isnan
#endif

#ifndef va_copy
#define va_copy(a,b) (a) = (b)
#endif

#ifndef R_OK
#define R_OK 4
#endif

#else /* C99 or close enough */ 

#include <unistd.h>
#define FZ_FLEX

#endif

/*
 * CPU detection and flags
 */

#if defined(ARCH_X86) || defined(ARCH_X86_64)
#  define HAVE_CPUDEP
#  define HAVE_MMX        (1<<0)
#  define HAVE_MMXEXT     (1<<1)
#  define HAVE_SSE        (1<<2)
#  define HAVE_SSE2       (1<<3)
#  define HAVE_SSE3       (1<<4)
#  define HAVE_3DNOW      (1<<5)
#  define HAVE_AMD64      (1<<6)

#elif defined (ARCH_PPC)
#  define HAVE_CPUDEP
#  define HAVE_ALTIVEC    (1<<7)

#elif defined (ARCH_SPARC)
#  define HAVE_CPUDEP
#  define HAVE_VIS        (1<<8)

#endif

/* call this before using fitz */
extern void fz_cpudetect();

/* treat as constant! */
extern unsigned fz_cpuflags;

/*
 * Base Fitz runtime.
 */

#ifndef __printflike
#if __GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ >= 7
#define __printflike(fmtarg, firstvararg) \
        __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
#else
#define __printflike(fmtarg, firstvararg)
#endif
#endif

#ifndef nil
#define nil ((void*)0)
#endif

#ifndef offsetof
#define offsetof(s, m) (unsigned long)(&(((s*)0)->m))
#endif

#ifndef nelem
#define nelem(x) (sizeof(x)/sizeof((x)[0]))
#endif

#ifndef ABS
#define ABS(x) ( (x) < 0 ? -(x) : (x) )
#endif

#ifndef MAX
#define MAX(a,b) ( (a) > (b) ? (a) : (b) )
#endif

#ifndef MIN
#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
#endif

#ifndef CLAMP
#define CLAMP(x,a,b) ( (x) > (b) ? (b) : ( (x) < (a) ? (a) : (x) ) )
#endif

#define MAX4(a,b,c,d) MAX(MAX(a,b), MAX(c,d))
#define MIN4(a,b,c,d) MIN(MIN(a,b), MIN(c,d))

#define STRIDE(n, bcp) (((bpc) * (n) + 7) / 8)

/* plan9 stuff for utf-8 and path munging */
int chartorune(int *rune, char *str);
int runetochar(char *str, int *rune);
int runelen(long c);
int runenlen(int *r, int nrune);
int fullrune(char *str, int n);

typedef int fz_error;

#define fz_throw(...) fz_throwimp(__func__, __FILE__, __LINE__, __VA_ARGS__)
#define fz_rethrow(cause, ...) fz_rethrowimp(cause, __func__, __FILE__, __LINE__, __VA_ARGS__)
#define fz_catch(cause, ...) fz_catchimp(cause, __func__, __FILE__, __LINE__, __VA_ARGS__)
#define fz_okay ((fz_error)0)

void fz_warn(char *fmt, ...) __printflike(1,2);
fz_error fz_throwimp(const char *func, const char *file, int line, char *fmt, ...) __printflike(4, 5);
fz_error fz_rethrowimp(fz_error cause, const char *func, const char *file, int line, char *fmt, ...) __printflike(5, 6);
fz_error fz_catchimp(fz_error cause, const char *func, const char *file, int line, char *fmt, ...) __printflike(5, 6);

void *fz_malloc(int n);
void *fz_realloc(void *p, int n);
void fz_free(void *p);
char *fz_strdup(char *s);

/*
 * Generic hash-table with fixed-length keys.
 */

typedef struct fz_hashtable_s fz_hashtable;

fz_error fz_newhash(fz_hashtable **tablep, int initialsize, int keylen);
fz_error fz_resizehash(fz_hashtable *table, int newsize);
void fz_debughash(fz_hashtable *table);
void fz_emptyhash(fz_hashtable *table);
void fz_drophash(fz_hashtable *table);

void *fz_hashfind(fz_hashtable *table, void *key);
fz_error fz_hashinsert(fz_hashtable *table, void *key, void *val);
fz_error fz_hashremove(fz_hashtable *table, void *key);

int fz_hashlen(fz_hashtable *table);
void *fz_hashgetkey(fz_hashtable *table, int idx);
void *fz_hashgetval(fz_hashtable *table, int idx);

/* multiply 8-bit fixpoint (0..1) so that 0*0==0 and 255*255==255 */
#define fz_mul255(a,b) (((a) * ((b) + 1)) >> 8)
#define fz_floor(x) floor(x)
#define fz_ceil(x) ceil(x)

typedef struct fz_matrix_s fz_matrix;
typedef struct fz_point_s fz_point;
typedef struct fz_rect_s fz_rect;
typedef struct fz_ipoint_s fz_ipoint;
typedef struct fz_irect_s fz_irect;

extern fz_rect fz_emptyrect;
extern fz_rect fz_infiniterect;

#define fz_isemptyrect(r) ((r).x0 == (r).x1)
#define fz_isinfiniterect(r) ((r).x0 > (r).x1)

/*
	/ a b 0 \
	| c d 0 |
	\ e f 1 /
*/

struct fz_matrix_s
{
	float a, b, c, d, e, f;
};

struct fz_point_s
{
	float x, y;
};

struct fz_rect_s
{
	float x0, y0;
	float x1, y1;
};

struct fz_ipoint_s
{
	int x, y;
};

struct fz_irect_s
{
	int x0, y0;
	int x1, y1;
};

void fz_invert3x3(float *dst, float *m);

fz_matrix fz_concat(fz_matrix one, fz_matrix two);
fz_matrix fz_identity(void);
fz_matrix fz_scale(float sx, float sy);
fz_matrix fz_rotate(float theta);
fz_matrix fz_translate(float tx, float ty);
fz_matrix fz_invertmatrix(fz_matrix m);
int fz_isrectilinear(fz_matrix m);
float fz_matrixexpansion(fz_matrix m);

fz_rect fz_intersectrects(fz_rect a, fz_rect b);
fz_rect fz_mergerects(fz_rect a, fz_rect b);

fz_irect fz_roundrect(fz_rect r);
fz_irect fz_intersectirects(fz_irect a, fz_irect b);
fz_irect fz_mergeirects(fz_irect a, fz_irect b);

fz_point fz_transformpoint(fz_matrix m, fz_point p);
fz_rect fz_transformaabb(fz_matrix m, fz_rect r);

/*
 *TODO: move this into draw module
 */

/*
pixmaps have n components per pixel. the first is always alpha.
premultiplied alpha when rendering, but non-premultiplied for colorspace
conversions and rescaling.
*/

typedef struct fz_pixmap_s fz_pixmap;
typedef unsigned char fz_sample;

struct fz_pixmap_s
{
	int x, y, w, h, n;
	fz_sample *samples;
};

fz_error fz_newpixmapwithrect(fz_pixmap **mapp, fz_irect bbox, int n);
fz_error fz_newpixmap(fz_pixmap **mapp, int x, int y, int w, int h, int n);
fz_error fz_newpixmapcopy(fz_pixmap **pixp, fz_pixmap *old);

void fz_debugpixmap(fz_pixmap *map, char *prefix);
void fz_clearpixmap(fz_pixmap *map);
void fz_droppixmap(fz_pixmap *map);

fz_error fz_scalepixmap(fz_pixmap **dstp, fz_pixmap *src, int xdenom, int ydenom);

/* needed for tiled rendering */
fz_error fz_newscaledpixmap(fz_pixmap **dstp, int w, int h, int n, int xdenom, int ydenom);
fz_error fz_scalepixmaptile(fz_pixmap *dstp, int xoffs, int yoffs,
			     fz_pixmap *tile, int xdenom, int ydenom);