Package: darkplaces / 0~20140513+svn12208-4

Add-support-for-DP_LINK_PNG-shared-default-is-dlopen.patch Patch series | download
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
From: Simon McVittie <smcv@debian.org>
Date: Thu, 2 Apr 2015 10:31:53 +0100
Subject: Add support for DP_LINK_PNG=shared (default is dlopen)

Like the existing DP_LINK_TO_JPEG, this can be configured to
link libpng in the normal way instead of dlopening it.

Origin: vendor, Debian
Forwarded: no
---
 image_png.c  | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 makefile     |  15 ++++++++
 makefile.inc |   4 +--
 3 files changed, 124 insertions(+), 7 deletions(-)

diff --git a/image_png.c b/image_png.c
index 6f52914..6e52f49 100644
--- a/image_png.c
+++ b/image_png.c
@@ -26,10 +26,80 @@
 //
 //LordHavoc: rewrote most of this.
 
+#ifdef LINK_TO_PNG
+// This has to come before anything else that includes setjmp.h, because
+// libpng specifically wants non-BSD setjmp semantics.
+// quakedef.h includes setjmp.h, but pngconf.h insists that on Linux,
+// setjmp.h has not already been included.
+//
+// Depending whether _BSD_SOURCE is preferred, glibc will provide one of two
+// implementations of setjmp()/longjmp() (it either does or doesn't save the
+// signal mask), acting on different definitions of the jmp_buf struct.
+# include <png.h>
+
+// libpng calls longjmp() internally, and expects its callers to call the
+// version of setjmp() corresponding to the longjmp() call that libpng would
+// make. Set this up before including any other headers that might
+// change which flavour of setjmp() we get.
+static inline int
+my_setjmp (jmp_buf env)
+{
+	return setjmp (env);
+}
+#endif
+
 #include "quakedef.h"
 #include "image.h"
 #include "image_png.h"
 
+#ifdef LINK_TO_PNG
+
+#define qpng_setjmp(png) my_setjmp (png_jmpbuf (png))
+
+#define qpng_set_sig_bytes png_set_sig_bytes
+#define qpng_sig_cmp png_sig_cmp
+#define qpng_create_read_struct png_create_read_struct
+#define qpng_create_write_struct png_create_write_struct
+#define qpng_create_info_struct png_create_info_struct
+#define qpng_read_info png_read_info
+#define qpng_set_compression_level png_set_compression_level
+#define qpng_set_filter png_set_filter
+#define qpng_set_expand png_set_expand
+#define qpng_set_palette_to_rgb png_set_palette_to_rgb
+#define qpng_set_tRNS_to_alpha png_set_tRNS_to_alpha
+#define qpng_set_gray_to_rgb png_set_gray_to_rgb
+#define qpng_set_filler png_set_filler
+#define qpng_set_IHDR png_set_IHDR
+#define qpng_set_packing png_set_packing
+#define qpng_set_bgr png_set_bgr
+#define qpng_set_interlace_handling png_set_interlace_handling
+#define qpng_read_update_info png_read_update_info
+#define qpng_read_image png_read_image
+#define qpng_read_end png_read_end
+#define qpng_destroy_read_struct png_destroy_read_struct
+#define qpng_destroy_write_struct png_destroy_write_struct
+#define qpng_set_read_fn png_set_read_fn
+#define qpng_set_write_fn png_set_write_fn
+#define qpng_get_valid png_get_valid
+#define qpng_get_rowbytes png_get_rowbytes
+#define qpng_get_channels png_get_channels
+#define qpng_get_bit_depth png_get_bit_depth
+#define qpng_get_IHDR png_get_IHDR
+#define qpng_access_version_number png_access_version_number
+#define qpng_write_info png_write_info
+#define qpng_write_row png_write_row
+#define qpng_write_end png_write_end
+
+#else
+/*
+ * Without LINK_TO_PNG, DarkPlaces assumes libpng 1.2 (in which png_uint_32
+ * is always unsigned long even if that's 64-bit), and then attempts
+ * to correct for it if it's actually 1.4 or 1.5 (where png_uint_32 is
+ * unsigned int if that's at least 32 bits, or unsigned long otherwise).
+ *
+ * With LINK_TO_PNG we use the library API/ABI as intended.
+ */
+typedef unsigned long png_uint_32;
 
 typedef struct png_struct_def *png_structp;
 typedef struct png_struct_def **png_structpp;
@@ -49,7 +119,7 @@ static void				(*qpng_set_palette_to_rgb)	(void*);
 static void				(*qpng_set_tRNS_to_alpha)	(void*);
 static void				(*qpng_set_gray_to_rgb)		(void*);
 static void				(*qpng_set_filler)			(void*, unsigned int, int);
-static void				(*qpng_set_IHDR)			(void*, void*, unsigned long, unsigned long, int, int, int, int, int);
+static void				(*qpng_set_IHDR)			(void*, void*, png_uint_32, png_uint_32, int, int, int, int, int);
 static void				(*qpng_set_packing)			(void*);
 static void				(*qpng_set_bgr)				(void*);
 static int				(*qpng_set_interlace_handling)	(void*);
@@ -64,7 +134,7 @@ static unsigned int		(*qpng_get_valid)			(void*, void*, unsigned int);
 static unsigned int		(*qpng_get_rowbytes)		(void*, void*);
 static unsigned char	(*qpng_get_channels)		(void*, void*);
 static unsigned char	(*qpng_get_bit_depth)		(void*, void*);
-static unsigned int		(*qpng_get_IHDR)			(void*, void*, unsigned long*, unsigned long*, int *, int *, int *, int *, int *);
+static png_uint_32		(*qpng_get_IHDR)			(void*, void*, png_uint_32 *, png_uint_32 *, int *, int *, int *, int *, int *);
 static unsigned int			(*qpng_access_version_number)		(void); // FIXME is this return type right? It is a png_uint_32 in libpng
 static void				(*qpng_write_info)			(void*, void*);
 static void				(*qpng_write_row)			(void*, unsigned char*);
@@ -82,6 +152,8 @@ static jmp_buf* (*qpng_set_longjmp_fn) (void *, qpng_longjmp_ptr, size_t);
 #define qpng_jmpbuf(png_ptr) \
 	(qpng_set_longjmp_fn ? qpng_jmpbuf_14(png_ptr) : qpng_jmpbuf_12(png_ptr))
 
+#define qpng_setjmp(png_ptr) setjmp(qpng_jmpbuf(png_ptr))
+
 static dllfunction_t pngfuncs[] =
 {
 	{"png_set_sig_bytes",		(void **) &qpng_set_sig_bytes},
@@ -129,6 +201,7 @@ static dllfunction_t png14funcs[] =
 dllhandle_t png_dll = NULL;
 dllhandle_t png14_dll = NULL;
 
+#endif
 
 /*
 =================================================================
@@ -147,6 +220,9 @@ Try to load the PNG DLL
 */
 qboolean PNG_OpenLibrary (void)
 {
+#ifdef LINK_TO_PNG
+	return TRUE;
+#else
 	const char* dllnames [] =
 	{
 #if WIN32
@@ -186,6 +262,7 @@ qboolean PNG_OpenLibrary (void)
 			return false;
 		}
 	return true;
+#endif
 }
 
 
@@ -198,8 +275,10 @@ Unload the PNG DLL
 */
 void PNG_CloseLibrary (void)
 {
+#ifndef LINK_TO_PNG
 	Sys_UnloadLibrary (&png14_dll);
 	Sys_UnloadLibrary (&png_dll);
+#endif
 }
 
 /*
@@ -210,6 +289,8 @@ void PNG_CloseLibrary (void)
 =================================================================
 */
 
+#ifndef LINK_TO_PNG
+
 #define PNG_LIBPNG_VER_STRING_12 "1.2.4"
 #define PNG_LIBPNG_VER_STRING_14 "1.4.0"
 #define PNG_LIBPNG_VER_STRING_15 "1.5.0"
@@ -230,6 +311,8 @@ void PNG_CloseLibrary (void)
 
 #define PNG_INFO_tRNS 0x0010
 
+#endif
+
 // this struct is only used for status information during loading
 static struct
 {
@@ -249,8 +332,8 @@ static struct
 	int		BitDepth;
 	int		BytesPerPixel;
 	int		ColorType;
-	unsigned long	Height; // retarded libpng 1.2 pngconf.h uses long (64bit/32bit depending on arch)
-	unsigned long	Width; // retarded libpng 1.2 pngconf.h uses long (64bit/32bit depending on arch)
+	png_uint_32	Height;
+	png_uint_32	Width;
 	int		Interlace;
 	int		Compression;
 	int		Filter;
@@ -307,18 +390,25 @@ unsigned char *PNG_LoadImage_BGRA (const unsigned char *raw, int filesize, int *
 
 	// FIXME: register an error handler so that abort() won't be called on error
 
+#ifndef LINK_TO_PNG
 	// No DLL = no PNGs
 	if (!png_dll)
 		return NULL;
+#endif
 
 	// png_sig_cmp is not fully const-correct
 	if(qpng_sig_cmp((unsigned char *) raw, 0, filesize))
 		return NULL;
+
 	png = (void *)qpng_create_read_struct(
+#ifdef LINK_TO_PNG
+		PNG_LIBPNG_VER_STRING,
+#else
 		(qpng_access_version_number() / 100 == 102) ? PNG_LIBPNG_VER_STRING_12 :
 		(qpng_access_version_number() / 100 == 104) ? PNG_LIBPNG_VER_STRING_14 :
 		(qpng_access_version_number() / 100 == 105) ? PNG_LIBPNG_VER_STRING_15 :
 		PNG_LIBPNG_VER_STRING_16, // nasty hack... whatever
+#endif
 		0, PNG_error_fn, PNG_warning_fn
 	);
 	if(!png)
@@ -330,7 +420,7 @@ unsigned char *PNG_LoadImage_BGRA (const unsigned char *raw, int filesize, int *
 
 	// NOTE: this relies on jmp_buf being the first thing in the png structure
 	// created by libpng! (this is correct for libpng 1.2.x)
-	if (setjmp(qpng_jmpbuf(png)))
+	if (qpng_setjmp(png))
 	{
 		if (my_png.Data)
 			Mem_Free(my_png.Data);
@@ -366,11 +456,16 @@ unsigned char *PNG_LoadImage_BGRA (const unsigned char *raw, int filesize, int *
 	qpng_read_info(png, pnginfo);
 	qpng_get_IHDR(png, pnginfo, &my_png.Width, &my_png.Height,&my_png.BitDepth, &my_png.ColorType, &my_png.Interlace, &my_png.Compression, &my_png.Filter);
 
+#ifndef LINK_TO_PNG
 	// this check guards against pngconf.h with unsigned int *width/height parameters on big endian systems by detecting the strange values and shifting them down 32bits
 	// (if it's little endian the unwritten bytes are the most significant
 	//  ones and we don't worry about that)
 	//
 	// this is only necessary because of retarded 64bit png_uint_32 types in libpng 1.2, which can (conceivably) vary by platform
+	//
+	// If LINK_TO_PNG is defined, we should never need this,
+	// because we get the correct definition of png_uint_32 from the
+	// system libpng header.
 #if LONG_MAX > 4000000000
 	if (my_png.Width > LONG_MAX || my_png.Height > LONG_MAX)
 	{
@@ -378,6 +473,7 @@ unsigned char *PNG_LoadImage_BGRA (const unsigned char *raw, int filesize, int *
 		my_png.Height >>= 32;
 	}
 #endif
+#endif
 
 	if (my_png.ColorType == PNG_COLOR_TYPE_PALETTE)
 		qpng_set_palette_to_rgb(png);
@@ -490,18 +586,24 @@ qboolean PNG_SaveImage_preflipped (const char *filename, int width, int height,
 	unsigned char ioBuffer[8192];
 	int passes, i, j;
 
+#ifndef LINK_TO_PNG
 	// No DLL = no JPEGs
 	if (!png_dll)
 	{
 		Con_Print("You need the libpng library to save PNG images\n");
 		return false;
 	}
+#endif
 
 	png = qpng_create_write_struct(
+#ifdef LINK_TO_PNG
+		PNG_LIBPNG_VER_STRING,
+#else
 		(qpng_access_version_number() / 100 == 102) ? PNG_LIBPNG_VER_STRING_12 :
 		(qpng_access_version_number() / 100 == 104) ? PNG_LIBPNG_VER_STRING_14 :
 		(qpng_access_version_number() / 100 == 105) ? PNG_LIBPNG_VER_STRING_15 :
 		PNG_LIBPNG_VER_STRING_16, // nasty hack... whatever
+#endif
 		0, PNG_error_fn, PNG_warning_fn
 	);
 	if(!png)
diff --git a/makefile b/makefile
index 6a00037..a46097f 100644
--- a/makefile
+++ b/makefile
@@ -114,6 +114,7 @@ ifeq ($(DP_MAKE_TARGET), linux)
 	DP_LINK_CRYPTO_RIJNDAEL?=dlopen
 	DP_LINK_VORBIS?=dlopen
 	DP_LINK_THEORA?=dlopen
+	DP_LINK_PNG?=dlopen
 endif
 
 # Mac OS X configuration
@@ -151,6 +152,7 @@ ifeq ($(DP_MAKE_TARGET), macosx)
 	DP_LINK_CRYPTO_RIJNDAEL?=dlopen
 	DP_LINK_VORBIS?=dlopen
 	DP_LINK_THEORA?=dlopen
+	DP_LINK_PNG?=dlopen
 
 	# on OS X, we don't build the CL by default because it uses deprecated
 	# and not-implemented-in-64bit Carbon
@@ -194,6 +196,7 @@ ifeq ($(DP_MAKE_TARGET), sunos)
 	DP_LINK_CRYPTO_RIJNDAEL?=dlopen
 	DP_LINK_VORBIS?=dlopen
 	DP_LINK_THEORA?=dlopen
+	DP_LINK_PNG?=dlopen
 endif
 
 # BSD configuration
@@ -231,6 +234,7 @@ endif
 	DP_LINK_CRYPTO_RIJNDAEL?=dlopen
 	DP_LINK_VORBIS?=dlopen
 	DP_LINK_THEORA?=dlopen
+	DP_LINK_PNG?=dlopen
 endif
 
 # Win32 configuration
@@ -292,6 +296,7 @@ ifeq ($(DP_MAKE_TARGET), mingw)
 	DP_LINK_CRYPTO_RIJNDAEL?=dlopen
 	DP_LINK_VORBIS?=dlopen
 	DP_LINK_THEORA?=dlopen
+	DP_LINK_PNG?=dlopen
 endif
 
 # set these to "" if you want to use dynamic loading instead
@@ -364,6 +369,16 @@ ifeq ($(DP_LINK_THEORA), dlopen)
 	LIB_THEORA=
 endif
 
+# png
+ifeq ($(DP_LINK_PNG), shared)
+	CFLAGS_LIBPNG=-DLINK_TO_PNG `pkg-config --cflags libpng`
+	LIB_PNG=`pkg-config --libs libpng`
+endif
+ifeq ($(DP_LINK_PNG), dlopen)
+	CFLAGS_LIBPNG=
+	LIB_PNG=
+endif
+
 ##### Sound configuration #####
 
 ifndef DP_SOUND_API
diff --git a/makefile.inc b/makefile.inc
index 9d29592..cb40f5a 100644
--- a/makefile.inc
+++ b/makefile.inc
@@ -172,7 +172,7 @@ OBJ_SDL= builddate.c sys_sdl.o vid_sdl.o thread_sdl.o $(OBJ_MENU) $(OBJ_SND_COMM
 
 
 # Compilation
-CFLAGS_COMMON=$(CFLAGS_MAKEDEP) $(CFLAGS_PRELOAD) $(CFLAGS_FS) $(CFLAGS_WARNINGS) $(CFLAGS_LIBZ) $(CFLAGS_LIBJPEG) $(CFLAGS_LIBVORBIS) $(CFLAGS_LIBTHEORA) $(CFLAGS_D3D) -D_FILE_OFFSET_BITS=64 -D__KERNEL_STRICT_NAMES -I../../../
+CFLAGS_COMMON=$(CFLAGS_MAKEDEP) $(CFLAGS_PRELOAD) $(CFLAGS_FS) $(CFLAGS_WARNINGS) $(CFLAGS_LIBZ) $(CFLAGS_LIBJPEG) $(CFLAGS_LIBVORBIS) $(CFLAGS_LIBTHEORA) $(CFLAGS_LIBPNG) $(CFLAGS_D3D) -D_FILE_OFFSET_BITS=64 -D__KERNEL_STRICT_NAMES -I../../../
 CFLAGS_CLIENT=-DCONFIG_MENU -DCONFIG_CD $(CFLAGS_VIDEO_CAPTURE)
 CFLAGS_SERVER=
 CFLAGS_DEBUG=-ggdb
@@ -205,7 +205,7 @@ LDFLAGS_RELEASE=$(OPTIM_RELEASE) -DSVNREVISION=`{ test -d .svn && svnversion; }
 
 OBJ_GLX= builddate.c sys_linux.o vid_glx.o thread_pthread.o keysym2ucs.o $(OBJ_MENU) $(OBJ_SOUND) $(OBJ_CD) $(OBJ_VIDEO_CAPTURE) $(OBJ_COMMON)
 
-LDFLAGS_UNIXCOMMON=-lm $(LIB_ODE) $(LIB_Z) $(LIB_JPEG) $(LIB_CRYPTO) $(LIB_CRYPTO_RIJNDAEL)
+LDFLAGS_UNIXCOMMON=-lm $(LIB_ODE) $(LIB_Z) $(LIB_JPEG) $(LIB_CRYPTO) $(LIB_CRYPTO_RIJNDAEL) $(LIB_PNG)
 LDFLAGS_UNIXCL=-L$(UNIX_X11LIBPATH) -lX11 -lXpm -lXext -lXxf86vm -pthread $(LIB_SOUND) $(LIB_VORBIS) $(LIB_THEORA)
 LDFLAGS_UNIXCL_PRELOAD=-lz -ljpeg -lpng -logg -ltheora -lvorbis -lvorbisenc -lvorbisfile -lcurl
 LDFLAGS_UNIXSV_PRELOAD=-lz -ljpeg -lpng -lcurl