]> git.pld-linux.org Git - packages/php4.git/blob - php4-gd.patch
- drop gd.dsp
[packages/php4.git] / php4-gd.patch
1 diff -urN php-4.4.8.org/ext/gd/config.m4 php-4.4.8/ext/gd/config.m4
2 --- php-4.4.8.org/ext/gd/config.m4      2007-03-10 14:06:37.000000000 +0100
3 +++ php-4.4.8/ext/gd/config.m4  2008-01-22 22:38:05.833815388 +0100
4 @@ -259,12 +259,13 @@
5    PHP_CHECK_LIBRARY(gd, gdCacheCreate,          [AC_DEFINE(HAVE_GD_CACHE_CREATE,     1, [ ])], [], [ -L$GD_LIB $GD_SHARED_LIBADD ])
6    PHP_CHECK_LIBRARY(gd, gdFontCacheShutdown,    [AC_DEFINE(HAVE_GD_FONTCACHESHUTDOWN,1, [ ])], [], [ -L$GD_LIB $GD_SHARED_LIBADD ])
7    PHP_CHECK_LIBRARY(gd, gdFreeFontCache,        [AC_DEFINE(HAVE_GD_FREEFONTCACHE,    1, [ ])], [], [ -L$GD_LIB $GD_SHARED_LIBADD ])
8 +  PHP_CHECK_LIBRARY(gd, gdFontCacheMutexSetup,  [AC_DEFINE(HAVE_GD_FONTMUTEX,        1, [ ])], [], [ -L$GD_LIB $GD_SHARED_LIBADD ])
9    PHP_CHECK_LIBRARY(gd, gdNewDynamicCtxEx,      [AC_DEFINE(HAVE_GD_DYNAMIC_CTX_EX,   1, [ ])], [], [ -L$GD_LIB $GD_SHARED_LIBADD ])
10  ])
11  
12  dnl
13  dnl Main GD configure
14 -dnl 
15 +dnl
16  
17  if test "$PHP_GD" = "yes"; then
18    GD_MODULE_TYPE=builtin
19 @@ -310,6 +311,7 @@
20    AC_DEFINE(HAVE_GD_GIF_CREATE,       1, [ ])
21    AC_DEFINE(HAVE_GD_IMAGEELLIPSE,     1, [ ])
22    AC_DEFINE(HAVE_GD_FONTCACHESHUTDOWN,1, [ ])
23 +  AC_DEFINE(HAVE_GD_FONTMUTEX,        1, [ ])
24    AC_DEFINE(HAVE_GD_DYNAMIC_CTX_EX,   1, [ ])
25    AC_DEFINE(HAVE_GD_GIF_CTX,          1, [ ])
26  
27 diff -urN php-4.4.8.org/ext/gd/CREDITS php-4.4.8/ext/gd/CREDITS
28 --- php-4.4.8.org/ext/gd/CREDITS        2003-03-01 02:16:00.000000000 +0100
29 +++ php-4.4.8/ext/gd/CREDITS    2008-01-22 22:38:05.837148890 +0100
30 @@ -1,2 +1,2 @@
31  GD imaging
32 -Rasmus Lerdorf, Stig Bakken, Jim Winstead, Jouni Ahto, Ilia Alshanetsky, Pierre-Alain Joye
33 +Rasmus Lerdorf, Stig Bakken, Jim Winstead, Jouni Ahto, Ilia Alshanetsky, Pierre-Alain Joye, Marcus Boerger
34 diff -urN php-4.4.8.org/ext/gd/gd.c php-4.4.8/ext/gd/gd.c
35 --- php-4.4.8.org/ext/gd/gd.c   2007-12-31 08:22:47.000000000 +0100
36 +++ php-4.4.8/ext/gd/gd.c       2008-01-22 22:38:05.837148890 +0100
37 @@ -29,7 +29,13 @@
38  #include "config.h"
39  #endif
40  
41 +#ifdef HAVE_GD_PNG
42 +/* needs to be first */
43 +#include <png.h>
44 +#endif
45 +
46  #include "php.h"
47 +#include "php_ini.h"
48  #include "ext/standard/head.h"
49  #include <math.h>
50  #include "SAPI.h"
51 @@ -46,6 +52,9 @@
52  #ifdef PHP_WIN32
53  # include <io.h>
54  # include <fcntl.h>
55 +#include <windows.h>
56 +#include <Winuser.h>
57 +#include <Wingdi.h>
58  #endif
59  
60  #if HAVE_LIBGD
61 @@ -64,10 +73,20 @@
62  #include <gdfontmb.h> /* 3 Medium bold font */
63  #include <gdfontl.h>  /* 4 Large font */
64  #include <gdfontg.h>  /* 5 Giant font */
65 +#if HAVE_GD_BUNDLED
66  #ifdef HAVE_GD_WBMP
67  #include "libgd/wbmp.h"
68  #endif
69 +#endif
70  #ifdef ENABLE_GD_TTF
71 +# ifdef HAVE_LIBFREETYPE
72 +#  include <ft2build.h>
73 +#  include FT_FREETYPE_H
74 +# else
75 +#  ifdef HAVE_LIBTTF
76 +#   include <freetype.h>
77 +#  endif
78 +# endif
79  # include "gdttf.h"
80  #endif
81  
82 @@ -112,6 +131,40 @@
83  #define gdNewDynamicCtxEx(len, data, val) gdNewDynamicCtx(len, data)
84  #endif
85  
86 +/* Section Filters Declarations */
87 +/* IMPORTANT NOTE FOR NEW FILTER
88 + * Do not forget to update:
89 + * IMAGE_FILTER_MAX: define the last filter index
90 + * IMAGE_FILTER_MAX_ARGS: define the biggest amout of arguments
91 + * image_filter array in PHP_FUNCTION(imagefilter)
92 + * */
93 +#if HAVE_GD_BUNDLED
94 +#define IMAGE_FILTER_NEGATE         0
95 +#define IMAGE_FILTER_GRAYSCALE      1
96 +#define IMAGE_FILTER_BRIGHTNESS     2
97 +#define IMAGE_FILTER_CONTRAST       3
98 +#define IMAGE_FILTER_COLORIZE       4
99 +#define IMAGE_FILTER_EDGEDETECT     5
100 +#define IMAGE_FILTER_EMBOSS         6
101 +#define IMAGE_FILTER_GAUSSIAN_BLUR  7
102 +#define IMAGE_FILTER_SELECTIVE_BLUR 8
103 +#define IMAGE_FILTER_MEAN_REMOVAL   9
104 +#define IMAGE_FILTER_SMOOTH         10
105 +#define IMAGE_FILTER_MAX            10
106 +#define IMAGE_FILTER_MAX_ARGS       5
107 +static void php_image_filter_negate(INTERNAL_FUNCTION_PARAMETERS);
108 +static void php_image_filter_grayscale(INTERNAL_FUNCTION_PARAMETERS);
109 +static void php_image_filter_brightness(INTERNAL_FUNCTION_PARAMETERS);
110 +static void php_image_filter_contrast(INTERNAL_FUNCTION_PARAMETERS);
111 +static void php_image_filter_colorize(INTERNAL_FUNCTION_PARAMETERS);
112 +static void php_image_filter_edgedetect(INTERNAL_FUNCTION_PARAMETERS);
113 +static void php_image_filter_emboss(INTERNAL_FUNCTION_PARAMETERS);
114 +static void php_image_filter_gaussian_blur(INTERNAL_FUNCTION_PARAMETERS);
115 +static void php_image_filter_selective_blur(INTERNAL_FUNCTION_PARAMETERS);
116 +static void php_image_filter_mean_removal(INTERNAL_FUNCTION_PARAMETERS);
117 +static void php_image_filter_smooth(INTERNAL_FUNCTION_PARAMETERS);
118 +#endif
119 +/* End Section filters declarations */
120  static gdImagePtr _php_image_create_from_string (zval **Data, char *tn, gdImagePtr (*ioctx_func_p)() TSRMLS_DC);
121  static void _php_image_create_from(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, gdImagePtr (*func_p)(), gdImagePtr (*ioctx_func_p)());
122  static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)());
123 @@ -121,7 +174,7 @@
124  
125  /* {{{ gd_functions[]
126   */
127 -function_entry gd_functions[] = {
128 +zend_function_entry gd_functions[] = {
129         PHP_FE(gd_info,                                 NULL)
130         PHP_FE(imagearc,                                                                NULL)
131         PHP_FE(imageellipse,                                                    NULL)
132 @@ -167,6 +220,11 @@
133         PHP_FE(imagecopyresampled,                                              NULL)
134  #endif
135  
136 +#ifdef PHP_WIN32
137 +       PHP_FE(imagegrabwindow,                                                 NULL)
138 +       PHP_FE(imagegrabscreen,                                                 NULL)
139 +#endif
140 +
141  #ifdef HAVE_GD_BUNDLED
142         PHP_FE(imagerotate,                                                     NULL)
143         PHP_FE(imageantialias,                                                  NULL)
144 @@ -277,6 +335,12 @@
145  #if HAVE_GD_BUNDLED
146         PHP_FE(imagelayereffect,                                                NULL)
147         PHP_FE(imagecolormatch,                                                 NULL)
148 +       PHP_FE(imagexbm,                                NULL)
149 +#endif
150 +/* gd filters */
151 +#ifdef HAVE_GD_BUNDLED
152 +       PHP_FE(imagefilter,                                                     NULL)
153 +       PHP_FE(imageconvolution,                                                NULL)
154  #endif
155  
156         {NULL, NULL, NULL}
157 @@ -288,9 +352,13 @@
158         "gd",
159         gd_functions,
160         PHP_MINIT(gd),
161 +#if HAVE_LIBT1 || HAVE_GD_FONTMUTEX
162         PHP_MSHUTDOWN(gd),
163 +#else
164 +       NULL,
165 +#endif
166         NULL,
167 -#if HAVE_LIBGD20 && HAVE_GD_STRINGFT && (HAVE_GD_FONTCACHESHUTDOWN || HAVE_GD_FREEFONTCACHE)
168 +#if HAVE_LIBGD20 && HAVE_GD_STRINGFT && (HAVE_LIBFREETYPE && (HAVE_GD_FONTCACHESHUTDOWN || HAVE_GD_FREEFONTCACHE))
169         PHP_RSHUTDOWN(gd),
170  #else
171         NULL,
172 @@ -304,6 +372,12 @@
173  ZEND_GET_MODULE(gd)
174  #endif
175  
176 +/* {{{ PHP_INI_BEGIN */
177 +PHP_INI_BEGIN()
178 +       PHP_INI_ENTRY("gd.jpeg_ignore_warning", "0", PHP_INI_ALL, NULL)
179 +PHP_INI_END()
180 +/* }}} */
181 +       
182  /* {{{ php_free_gd_image
183   */
184  static void php_free_gd_image(zend_rsrc_list_entry *rsrc TSRMLS_DC)
185 @@ -326,15 +400,21 @@
186  }
187  /* }}} */
188  
189 +
190  /* {{{ PHP_MSHUTDOWN_FUNCTION
191   */
192 +#if HAVE_LIBT1 || HAVE_GD_FONTMUTEX
193  PHP_MSHUTDOWN_FUNCTION(gd)
194  {
195  #if HAVE_LIBT1
196         T1_CloseLib();
197  #endif
198 +#if HAVE_GD_FONTMUTEX && HAVE_LIBFREETYPE
199 +       gdFontCacheMutexShutdown();
200 +#endif
201         return SUCCESS;
202  }
203 +#endif
204  /* }}} */
205  
206  
207 @@ -344,6 +424,10 @@
208  {
209         le_gd = zend_register_list_destructors_ex(php_free_gd_image, NULL, "gd", module_number);
210         le_gd_font = zend_register_list_destructors_ex(php_free_gd_font, NULL, "gd font", module_number);
211 +
212 +#if HAVE_GD_FONTMUTEX && HAVE_LIBFREETYPE
213 +       gdFontCacheMutexSetup();
214 +#endif
215  #if HAVE_LIBT1
216         T1_SetBitmapPad(8);
217         T1_InitLib(NO_LOGFILE | IGNORE_CONFIGFILE | IGNORE_FONTDATABASE);
218 @@ -352,6 +436,8 @@
219         le_ps_enc = zend_register_list_destructors_ex(php_free_ps_enc, NULL, "gd PS encoding", module_number);
220  #endif
221  
222 +       REGISTER_INI_ENTRIES();
223 +       
224         REGISTER_LONG_CONSTANT("IMG_GIF", 1, CONST_CS | CONST_PERSISTENT);
225         REGISTER_LONG_CONSTANT("IMG_JPG", 2, CONST_CS | CONST_PERSISTENT);
226         REGISTER_LONG_CONSTANT("IMG_JPEG", 2, CONST_CS | CONST_PERSISTENT);
227 @@ -387,16 +473,58 @@
228         REGISTER_LONG_CONSTANT("IMG_EFFECT_NORMAL", gdEffectNormal, CONST_CS | CONST_PERSISTENT);
229         REGISTER_LONG_CONSTANT("IMG_EFFECT_OVERLAY", gdEffectOverlay, CONST_CS | CONST_PERSISTENT);
230         REGISTER_LONG_CONSTANT("GD_BUNDLED", 1, CONST_CS | CONST_PERSISTENT);
231 +
232 +       /* Section Filters */
233 +       REGISTER_LONG_CONSTANT("IMG_FILTER_NEGATE", IMAGE_FILTER_NEGATE, CONST_CS | CONST_PERSISTENT);
234 +       REGISTER_LONG_CONSTANT("IMG_FILTER_GRAYSCALE", IMAGE_FILTER_GRAYSCALE, CONST_CS | CONST_PERSISTENT);
235 +       REGISTER_LONG_CONSTANT("IMG_FILTER_BRIGHTNESS", IMAGE_FILTER_BRIGHTNESS, CONST_CS | CONST_PERSISTENT);
236 +       REGISTER_LONG_CONSTANT("IMG_FILTER_CONTRAST", IMAGE_FILTER_CONTRAST, CONST_CS | CONST_PERSISTENT);
237 +       REGISTER_LONG_CONSTANT("IMG_FILTER_COLORIZE", IMAGE_FILTER_COLORIZE, CONST_CS | CONST_PERSISTENT);
238 +       REGISTER_LONG_CONSTANT("IMG_FILTER_EDGEDETECT", IMAGE_FILTER_EDGEDETECT, CONST_CS | CONST_PERSISTENT);
239 +       REGISTER_LONG_CONSTANT("IMG_FILTER_GAUSSIAN_BLUR", IMAGE_FILTER_GAUSSIAN_BLUR, CONST_CS | CONST_PERSISTENT);
240 +       REGISTER_LONG_CONSTANT("IMG_FILTER_SELECTIVE_BLUR", IMAGE_FILTER_SELECTIVE_BLUR, CONST_CS | CONST_PERSISTENT);
241 +       REGISTER_LONG_CONSTANT("IMG_FILTER_EMBOSS", IMAGE_FILTER_EMBOSS, CONST_CS | CONST_PERSISTENT);
242 +       REGISTER_LONG_CONSTANT("IMG_FILTER_MEAN_REMOVAL", IMAGE_FILTER_MEAN_REMOVAL, CONST_CS | CONST_PERSISTENT);
243 +       REGISTER_LONG_CONSTANT("IMG_FILTER_SMOOTH", IMAGE_FILTER_SMOOTH, CONST_CS | CONST_PERSISTENT);
244 +       /* End Section Filters */
245  #else
246         REGISTER_LONG_CONSTANT("GD_BUNDLED", 0, CONST_CS | CONST_PERSISTENT);
247  #endif
248 +
249 +#ifdef GD_VERSION_STRING
250 +       REGISTER_STRING_CONSTANT("GD_VERSION", GD_VERSION_STRING, CONST_CS | CONST_PERSISTENT);
251 +#endif
252 +
253 +#if defined(GD_MAJOR_VERSION) && defined(GD_MINOR_VERSION) && defined(GD_RELEASE_VERSION) && defined(GD_EXTRA_VERSION)
254 +       REGISTER_LONG_CONSTANT("GD_MAJOR_VERSION", GD_MAJOR_VERSION, CONST_CS | CONST_PERSISTENT);
255 +       REGISTER_LONG_CONSTANT("GD_MINOR_VERSION", GD_MINOR_VERSION, CONST_CS | CONST_PERSISTENT);
256 +       REGISTER_LONG_CONSTANT("GD_RELEASE_VERSION", GD_RELEASE_VERSION, CONST_CS | CONST_PERSISTENT);
257 +       REGISTER_STRING_CONSTANT("GD_EXTRA_VERSION", GD_EXTRA_VERSION, CONST_CS | CONST_PERSISTENT);
258 +#endif
259 +
260 +
261 +#ifdef HAVE_GD_PNG
262 +
263 +/*
264 + * cannot include #include "png.h"
265 + * /usr/include/pngconf.h:310:2: error: #error png.h already includes setjmp.h with some additional fixup.
266 + * as error, use the values for now...
267 + */
268 +       REGISTER_LONG_CONSTANT("PNG_NO_FILTER",         0x00, CONST_CS | CONST_PERSISTENT);
269 +       REGISTER_LONG_CONSTANT("PNG_FILTER_NONE",       0x08, CONST_CS | CONST_PERSISTENT);
270 +       REGISTER_LONG_CONSTANT("PNG_FILTER_SUB",        0x10, CONST_CS | CONST_PERSISTENT);
271 +       REGISTER_LONG_CONSTANT("PNG_FILTER_UP",         0x20, CONST_CS | CONST_PERSISTENT);
272 +       REGISTER_LONG_CONSTANT("PNG_FILTER_AVG",        0x40, CONST_CS | CONST_PERSISTENT);
273 +       REGISTER_LONG_CONSTANT("PNG_FILTER_PAETH",      0x80, CONST_CS | CONST_PERSISTENT);
274 +       REGISTER_LONG_CONSTANT("PNG_ALL_FILTERS",       0x08 | 0x10 | 0x20 | 0x40 | 0x80, CONST_CS | CONST_PERSISTENT);
275 +#endif
276         return SUCCESS;
277  }
278  /* }}} */
279  
280  /* {{{ PHP_RSHUTDOWN_FUNCTION
281   */
282 -#if HAVE_LIBGD20 && HAVE_GD_STRINGFT && (HAVE_GD_FONTCACHESHUTDOWN || HAVE_GD_FREEFONTCACHE)
283 +#if HAVE_LIBGD20 && HAVE_GD_STRINGFT && (HAVE_LIBFREETYPE && (HAVE_GD_FONTCACHESHUTDOWN || HAVE_GD_FREEFONTCACHE))
284  PHP_RSHUTDOWN_FUNCTION(gd)
285  {
286  #if HAVE_GD_FONTCACHESHUTDOWN
287 @@ -410,7 +538,7 @@
288  /* }}} */
289  
290  #if HAVE_GD_BUNDLED
291 -#define PHP_GD_VERSION_STRING "bundled (2.0.28 compatible)"
292 +#define PHP_GD_VERSION_STRING "bundled (2.0.34 compatible)"
293  #elif HAVE_LIBGD20
294  #define PHP_GD_VERSION_STRING "2.0 or higher"
295  #elif HAVE_GDIMAGECOLORRESOLVE
296 @@ -436,8 +564,24 @@
297         php_info_print_table_row(2, "FreeType Support", "enabled");
298  #if HAVE_LIBFREETYPE
299         php_info_print_table_row(2, "FreeType Linkage", "with freetype");
300 +       {
301 +               char tmp[256];
302 +#ifdef FREETYPE_PATCH
303 +               snprintf(tmp, sizeof(tmp), "%d.%d.%d", FREETYPE_MAJOR, FREETYPE_MINOR, FREETYPE_PATCH);
304 +#elif defined(FREETYPE_MAJOR)
305 +               snprintf(tmp, sizeof(tmp), "%d.%d", FREETYPE_MAJOR, FREETYPE_MINOR);
306 +#else
307 +               snprintf(tmp, sizeof(tmp), "1.x");
308 +#endif
309 +               php_info_print_table_row(2, "FreeType Version", tmp);
310 +       }
311  #elif HAVE_LIBTTF
312         php_info_print_table_row(2, "FreeType Linkage", "with TTF library");
313 +       {
314 +               char tmp[256];
315 +               snprintf(tmp, sizeof(tmp), "%d.%d", TT_FREETYPE_MAJOR, TT_FREETYPE_MINOR);
316 +               php_info_print_table_row(2, "FreeType Version", tmp);
317 +       }
318  #else
319         php_info_print_table_row(2, "FreeType Linkage", "with unknown library");
320  #endif
321 @@ -464,6 +608,9 @@
322  #ifdef HAVE_GD_WBMP
323         php_info_print_table_row(2, "WBMP Support", "enabled");
324  #endif
325 +#if defined(HAVE_GD_XPM) && defined(HAVE_GD_BUNDLED)
326 +       php_info_print_table_row(2, "XPM Support", "enabled");
327 +#endif
328  #ifdef HAVE_GD_XBM
329         php_info_print_table_row(2, "XBM Support", "enabled");
330  #endif
331 @@ -530,6 +677,11 @@
332  #else
333         add_assoc_bool(return_value, "WBMP Support", 0);
334  #endif
335 +#if defined(HAVE_GD_XPM) && defined(HAVE_GD_BUNDLED)
336 +       add_assoc_bool(return_value, "XPM Support", 1);
337 +#else
338 +       add_assoc_bool(return_value, "XPM Support", 0);
339 +#endif
340  #ifdef HAVE_GD_XBM
341         add_assoc_bool(return_value, "XBM Support", 1);
342  #else
343 @@ -548,6 +700,7 @@
344  {
345         return le_gd;
346  }
347 +/* }}} */
348  
349  #ifndef HAVE_GDIMAGECOLORRESOLVE
350  
351 @@ -763,13 +916,19 @@
352         convert_to_long_ex(x_size);
353         convert_to_long_ex(y_size);
354  
355 -       if (Z_LVAL_PP(x_size) <= 0 || Z_LVAL_PP(y_size) <= 0) {
356 +       if (Z_LVAL_PP(x_size) <= 0 || Z_LVAL_PP(y_size) <= 0 ||
357 +                       Z_LVAL_PP(x_size) >= INT_MAX || Z_LVAL_PP(y_size) >= INT_MAX 
358 +               ) {
359                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid image dimensions");
360                 RETURN_FALSE;
361         }
362  
363         im = gdImageCreateTrueColor(Z_LVAL_PP(x_size), Z_LVAL_PP(y_size));
364  
365 +       if (!im) {
366 +               RETURN_FALSE;
367 +       }
368 +
369         ZEND_REGISTER_RESOURCE(return_value, im, le_gd);
370  }
371  /* }}} */
372 @@ -836,15 +995,19 @@
373         result = gdImageColorMatch(im1, im2);
374         switch (result) {
375                 case -1:
376 -                       php_error_docref(NULL TSRMLS_CC, E_ERROR, "Image1 must be TrueColor" );
377 +                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Image1 must be TrueColor" );
378                         RETURN_FALSE;
379                         break;
380                 case -2:
381 -                       php_error_docref(NULL TSRMLS_CC, E_ERROR, "Image2 must be Palette" );
382 +                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Image2 must be Palette" );
383                         RETURN_FALSE;
384                         break;
385                 case -3:
386 -                       php_error_docref(NULL TSRMLS_CC, E_ERROR, "Image1 and Image2 must be the same size" );
387 +                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Image1 and Image2 must be the same size" );
388 +                       RETURN_FALSE;
389 +                       break;
390 +               case -4:
391 +                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Image2 must have at least one color" );
392                         RETURN_FALSE;
393                         break;
394         }
395 @@ -977,6 +1140,7 @@
396  
397         RETURN_TRUE;
398  }
399 +/* }}} */
400  #endif
401  
402  #if HAVE_GD_BUNDLED
403 @@ -1008,6 +1172,7 @@
404         zval *IM;
405         long red, green, blue, alpha;
406         gdImagePtr im;
407 +       int ct = (-1);
408  
409         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zllll", &IM, &red, &green, &blue, &alpha) == FAILURE) {
410                 RETURN_FALSE;
411 @@ -1015,7 +1180,12 @@
412  
413         ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
414  
415 -       RETURN_LONG(gdImageColorAllocateAlpha(im, red, green, blue, alpha));
416 +       ct = gdImageColorAllocateAlpha(im, red, green, blue, alpha);
417 +       if (ct < 0) {
418 +               RETURN_FALSE;
419 +       }
420 +
421 +       RETURN_LONG((long)ct);
422  }
423  /* }}} */
424  
425 @@ -1125,28 +1295,173 @@
426  /* }}} */
427  #endif
428  
429 +#ifdef PHP_WIN32
430 +/* {{{ proto resource imagegrabwindow(int window_handle [, int client_area])
431 +   Grab a window or its client area using a windows handle (HWND property in COM instance) */
432 +PHP_FUNCTION(imagegrabwindow)
433 +{
434 +       HWND window;
435 +       long client_area = 0;
436 +       RECT rc = {0};
437 +       RECT rc_win = {0};
438 +       int Width, Height;
439 +       HDC             hdc;
440 +       HDC memDC;
441 +       HBITMAP memBM;
442 +       HBITMAP hOld;
443 +       HINSTANCE handle;
444 +       long lwindow_handle;
445 +       typedef BOOL (WINAPI *tPrintWindow)(HWND, HDC,UINT);
446 +       tPrintWindow pPrintWindow = 0;
447 +       gdImagePtr im;
448 +
449 +       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &lwindow_handle, &client_area) == FAILURE) {
450 +               RETURN_FALSE;
451 +       }
452 +
453 +       window = (HWND) lwindow_handle;
454 +
455 +       if (!IsWindow(window)) {
456 +               php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid window handle");
457 +               RETURN_FALSE;
458 +       }
459 +
460 +       hdc             = GetDC(0);
461 +
462 +       if (client_area) {
463 +               GetClientRect(window, &rc);
464 +               Width = rc.right;
465 +               Height = rc.bottom;
466 +       } else {
467 +               GetWindowRect(window, &rc);
468 +               Width   = rc.right - rc.left;
469 +               Height  = rc.bottom - rc.top;
470 +       }
471 +
472 +       Width           = (Width/4)*4;
473 +
474 +       memDC   = CreateCompatibleDC(hdc);
475 +       memBM   = CreateCompatibleBitmap(hdc, Width, Height);
476 +       hOld    = (HBITMAP) SelectObject (memDC, memBM);
477 +
478 +
479 +       handle = LoadLibrary("User32.dll");
480 +       if ( handle == 0 ) {
481 +               goto clean;
482 +       }
483 +       pPrintWindow = (tPrintWindow) GetProcAddress(handle, "PrintWindow");  
484 +
485 +       if ( pPrintWindow )  {
486 +               pPrintWindow(window, memDC, (UINT) client_area);
487 +       } else {
488 +               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Windows API too old");
489 +               RETURN_FALSE;
490 +               goto clean;
491 +       }
492 +
493 +       FreeLibrary(handle);
494 +
495 +       im = gdImageCreateTrueColor(Width, Height);
496 +       if (im) {
497 +               int x,y;
498 +               for (y=0; y <= Height; y++) {
499 +                       for (x=0; x <= Width; x++) {
500 +                               int c = GetPixel(memDC, x,y);
501 +                               gdImageSetPixel(im, x, y, gdTrueColor(GetRValue(c), GetGValue(c), GetBValue(c)));
502 +                       }
503 +               }
504 +       }
505 +
506 +clean:
507 +       SelectObject(memDC,hOld);
508 +       DeleteObject(memBM);
509 +       DeleteDC(memDC);
510 +       ReleaseDC( 0, hdc );
511 +
512 +       if (!im) {
513 +               RETURN_FALSE;
514 +       } else {
515 +               ZEND_REGISTER_RESOURCE(return_value, im, le_gd);
516 +       }
517 +}
518 +/* }}} */
519 +
520 +/* {{{ proto resource imagegrabscreen()
521 +   Grab a screenshot */
522 +PHP_FUNCTION(imagegrabscreen)
523 +{
524 +       HWND window = GetDesktopWindow();
525 +       RECT rc = {0};
526 +       int Width, Height;
527 +       HDC             hdc;
528 +       HDC memDC;
529 +       HBITMAP memBM;
530 +       HBITMAP hOld;
531 +       HINSTANCE handle;
532 +       long lwindow_handle;
533 +       typedef BOOL (WINAPI *tPrintWindow)(HWND, HDC,UINT);
534 +       tPrintWindow pPrintWindow = 0;
535 +       gdImagePtr im;
536 +       hdc             = GetDC(0);
537 +
538 +       if (!hdc) {
539 +               RETURN_FALSE;
540 +       }
541 +
542 +       GetWindowRect(window, &rc);
543 +       Width   = rc.right - rc.left;
544 +       Height  = rc.bottom - rc.top;
545 +
546 +       Width           = (Width/4)*4;
547 +
548 +       memDC   = CreateCompatibleDC(hdc);
549 +       memBM   = CreateCompatibleBitmap(hdc, Width, Height);
550 +       hOld    = (HBITMAP) SelectObject (memDC, memBM);
551 +       BitBlt( memDC, 0, 0, Width, Height , hdc, rc.left, rc.top , SRCCOPY );
552 +
553 +       im = gdImageCreateTrueColor(Width, Height);
554 +       if (im) {
555 +               int x,y;
556 +               for (y=0; y <= Height; y++) {
557 +                       for (x=0; x <= Width; x++) {
558 +                               int c = GetPixel(memDC, x,y);
559 +                               gdImageSetPixel(im, x, y, gdTrueColor(GetRValue(c), GetGValue(c), GetBValue(c)));
560 +                       }
561 +               }
562 +       }
563 +
564 +       SelectObject(memDC,hOld);
565 +       DeleteObject(memBM);
566 +       DeleteDC(memDC);
567 +       ReleaseDC( 0, hdc );
568 +
569 +       if (!im) {
570 +               RETURN_FALSE;
571 +       } else {
572 +               ZEND_REGISTER_RESOURCE(return_value, im, le_gd);
573 +       }
574 +}
575 +/* }}} */
576 +#endif /* PHP_WIN32 */
577 +
578  #ifdef HAVE_GD_BUNDLED
579 -/* {{{ proto resource imagerotate(resource src_im, float angle, int bgdcolor)
580 +/* {{{ proto resource imagerotate(resource src_im, float angle, int bgdcolor [, int ignoretransparent])
581     Rotate an image using a custom angle */
582  PHP_FUNCTION(imagerotate)
583  {
584 -       zval **SIM, **ANGLE, **BGDCOLOR;
585 +       zval *SIM;
586         gdImagePtr im_dst, im_src;
587         double degrees;
588         long color;
589 +       long ignoretransparent = 0;
590  
591 -       if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &SIM, &ANGLE, &BGDCOLOR) == FAILURE) {
592 -               ZEND_WRONG_PARAM_COUNT();
593 +       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rdl|l", &SIM, &degrees, &color, &ignoretransparent) == FAILURE) {
594 +               RETURN_FALSE;
595         }
596  
597 -       ZEND_FETCH_RESOURCE(im_src, gdImagePtr, SIM, -1, "Image", le_gd);
598 -
599 -       convert_to_long_ex(BGDCOLOR);
600 -       color = Z_LVAL_PP(BGDCOLOR);
601 +       ZEND_FETCH_RESOURCE(im_src, gdImagePtr, &SIM, -1, "Image", le_gd);
602  
603 -       convert_to_double_ex(ANGLE);
604 -       degrees = Z_DVAL_PP(ANGLE);
605 -       im_dst = gdImageRotate(im_src, degrees, color);
606 +       im_dst = gdImageRotate(im_src, degrees, color, ignoretransparent);
607  
608         if (im_dst != NULL) {
609                 ZEND_REGISTER_RESOURCE(return_value, im_dst, le_gd);
610 @@ -1215,13 +1530,19 @@
611         convert_to_long_ex(x_size);
612         convert_to_long_ex(y_size);
613  
614 -       if (Z_LVAL_PP(x_size) <= 0 || Z_LVAL_PP(y_size) <= 0) {
615 +       if (Z_LVAL_PP(x_size) <= 0 || Z_LVAL_PP(y_size) <= 0 ||
616 +                       Z_LVAL_PP(x_size) >= INT_MAX || Z_LVAL_PP(y_size) >= INT_MAX
617 +               ) {
618                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid image dimensions");
619                 RETURN_FALSE;
620         }
621  
622         im = gdImageCreate(Z_LVAL_PP(x_size), Z_LVAL_PP(y_size));
623  
624 +       if (!im) {
625 +               RETURN_FALSE;
626 +       }
627 +
628         ZEND_REGISTER_RESOURCE(return_value, im, le_gd);
629  }
630  /* }}} */
631 @@ -1323,6 +1644,11 @@
632         im = (*ioctx_func_p)(io_ctx);
633         if (!im) {
634                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Passed data is not in '%s' format", tn);
635 +#if HAVE_LIBGD204
636 +               io_ctx->gd_free(io_ctx);
637 +#else
638 +               io_ctx->free(io_ctx);
639 +#endif
640                 return NULL;
641         }
642  
643 @@ -1350,6 +1676,11 @@
644         }
645  
646         convert_to_string_ex(data);
647 +       if (Z_STRLEN_PP(data) < 8) {
648 +               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty string or invalid image");
649 +               RETURN_FALSE;
650 +       }
651 +
652         memcpy(sig, Z_STRVAL_PP(data), 8);
653  
654         imtype = _php_image_type(sig);
655 @@ -1401,7 +1732,7 @@
656                         break;
657  
658                 default:
659 -                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Data is not in a recognized format.");
660 +                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Data is not in a recognized format");
661                         RETURN_FALSE;
662         }
663  
664 @@ -1425,6 +1756,9 @@
665         php_stream *stream;
666         FILE * fp = NULL;
667         int argc=ZEND_NUM_ARGS();
668 +#ifdef HAVE_GD_JPG
669 +       long ignore_warning;
670 +#endif
671  
672         if ((image_type == PHP_GDIMG_TYPE_GD2PART && argc != 5) ||
673                 (image_type != PHP_GDIMG_TYPE_GD2PART && argc != 1) ||
674 @@ -1436,6 +1770,10 @@
675  
676         if (argc == 5 && image_type == PHP_GDIMG_TYPE_GD2PART) {
677                 multi_convert_to_long_ex(4, srcx, srcy, width, height);
678 +               if (Z_LVAL_PP(width) < 1 || Z_LVAL_PP(height) < 1) {
679 +                       php_error_docref(NULL TSRMLS_CC, E_WARNING,"Zero width or height not allowed");
680 +                       RETURN_FALSE;
681 +               }
682         }
683  
684         fn = Z_STRVAL_PP(file);
685 @@ -1471,6 +1809,7 @@
686  
687                 io_ctx = gdNewDynamicCtxEx(buff_size, buff, 0);
688                 if (!io_ctx) {
689 +                       pefree(buff, 1);
690                         php_error_docref(NULL TSRMLS_CC, E_WARNING,"Cannot allocate GD IO context");
691                         goto out_err;
692                 }
693 @@ -1485,7 +1824,7 @@
694  #else
695                 io_ctx->free(io_ctx);
696  #endif
697 -
698 +               pefree(buff, 1);
699  #endif
700         }
701         else {
702 @@ -1505,6 +1844,18 @@
703                                 im = gdImageCreateFromXpm(fn);
704                                 break;
705  #endif
706 +
707 +#ifdef HAVE_GD_JPG
708 +                       case PHP_GDIMG_TYPE_JPG:
709 +                               ignore_warning = INI_INT("gd.jpeg_ignore_warning");
710 +#ifdef HAVE_GD_BUNDLED
711 +                               im = gdImageCreateFromJpeg(fp, ignore_warning);
712 +#else
713 +                               im = gdImageCreateFromJpeg(fp);
714 +#endif
715 +                       break;
716 +#endif
717 +
718                         default:
719                                 im = (*func_p)(fp);
720                                 break;
721 @@ -1685,6 +2036,14 @@
722                                 (*func_p)(im, fp);
723                                 break;
724  #endif
725 +#ifdef HAVE_GD_GD2
726 +                       case PHP_GDIMG_TYPE_GD2:
727 +                               if (q == -1) {
728 +                                       q = 128;
729 +                               }
730 +                               (*func_p)(im, fp, q, t);
731 +                               break;
732 +#endif
733                         default:
734                                 if (q == -1) {
735                                         q = 128;
736 @@ -1737,6 +2096,14 @@
737                                 (*func_p)(im, tmp);
738                                 break;
739  #endif
740 +#ifdef HAVE_GD_GD2
741 +                       case PHP_GDIMG_TYPE_GD2:
742 +                               if (q == -1) {
743 +                                       q = 128;
744 +                               }
745 +                               (*func_p)(im, tmp, q, t);
746 +                               break;
747 +#endif
748                         default:
749                                 (*func_p)(im, tmp);
750                                 break;
751 @@ -1762,6 +2129,16 @@
752  }
753  /* }}} */
754  
755 +/* {{{ proto int imagexbm(int im, string filename [, int foreground])
756 +   Output XBM image to browser or file */
757 +#if HAVE_GD_BUNDLED
758 +PHP_FUNCTION(imagexbm)
759 +{
760 +       _php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_XBM, "XBM", gdImageXbmCtx);
761 +}
762 +#endif
763 +/* }}} */
764 +
765  #ifdef HAVE_GD_GIF_CREATE
766  /* {{{ proto bool imagegif(resource im [, string filename])
767     Output GIF image to browser or file */
768 @@ -1782,7 +2159,7 @@
769  PHP_FUNCTION(imagepng)
770  {
771  #ifdef USE_GD_IOCTX
772 -       _php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_PNG, "PNG", gdImagePngCtx);
773 +       _php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_PNG, "PNG", gdImagePngCtxEx);
774  #else
775         _php_image_output(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_PNG, "PNG", gdImagePng);
776  #endif
777 @@ -1862,6 +2239,7 @@
778  {
779         zval **IM, **red, **green, **blue;
780         gdImagePtr im;
781 +       int ct = (-1);
782  
783         if (ZEND_NUM_ARGS() != 4 || zend_get_parameters_ex(4, &IM, &red, &green, &blue) == FAILURE) {
784                 ZEND_WRONG_PARAM_COUNT();
785 @@ -1872,8 +2250,11 @@
786         convert_to_long_ex(red);
787         convert_to_long_ex(green);
788         convert_to_long_ex(blue);
789 -
790 -       RETURN_LONG(gdImageColorAllocate(im, Z_LVAL_PP(red), Z_LVAL_PP(green), Z_LVAL_PP(blue)));
791 +       ct = gdImageColorAllocate(im, Z_LVAL_PP(red), Z_LVAL_PP(green), Z_LVAL_PP(blue));
792 +       if (ct < 0) {
793 +               RETURN_FALSE;
794 +       }
795 +       RETURN_LONG(ct);
796  }
797  /* }}} */
798  
799 @@ -2508,7 +2889,7 @@
800  static void php_imagepolygon(INTERNAL_FUNCTION_PARAMETERS, int filled)
801  {
802         zval **IM, **POINTS, **NPOINTS, **COL;
803 -       pval **var = NULL;
804 +       zval **var = NULL;
805         gdImagePtr im;
806         gdPointPtr points;
807         int npoints, col, nelem, i;
808 @@ -2716,7 +3097,7 @@
809                 ch = (int)((unsigned char)*(Z_STRVAL_PP(C)));
810         } else {
811                 str = (unsigned char *) estrndup(Z_STRVAL_PP(C), Z_STRLEN_PP(C));
812 -               l = strlen(str);
813 +               l = strlen((char *)str);
814         }
815  
816         y = Z_LVAL_PP(Y);
817 @@ -3083,7 +3464,7 @@
818         {
819                 char tmp_font_path[MAXPATHLEN];
820  
821 -               if (VCWD_REALPATH(fontname, tmp_font_path)) {
822 +               if (VCWD_REALPATH((char *)fontname, tmp_font_path)) {
823                         fontname = (unsigned char *) fontname;
824                 } else {
825                         fontname = NULL;
826 @@ -3093,16 +3474,18 @@
827         fontname = (unsigned char *) fontname;
828  #endif
829  
830 +       PHP_GD_CHECK_OPEN_BASEDIR((char *)fontname, "Invalid font filename");
831 +       
832  #ifdef USE_GD_IMGSTRTTF
833  # if HAVE_GD_STRINGFTEX
834         if (extended) {
835 -               error = gdImageStringFTEx(im, brect, col, fontname, ptsize, angle, x, y, str, &strex);
836 +               error = gdImageStringFTEx(im, brect, col, (char *)fontname, ptsize, angle, x, y, (char *)str, &strex);
837         }
838         else
839  # endif
840  
841  # if HAVE_GD_STRINGFT
842 -       error = gdImageStringFT(im, brect, col, fontname, ptsize, angle, x, y, str);
843 +       error = gdImageStringFT(im, brect, col, (char *)fontname, ptsize, angle, x, y, (char *)str);
844  # elif HAVE_GD_STRINGTTF
845         error = gdImageStringTTF(im, brect, col, fontname, ptsize, angle, x, y, str);
846  # endif
847 @@ -3155,6 +3538,9 @@
848  {
849         zval **file;
850         int f_ind, *font;
851 +#ifdef PHP_WIN32
852 +       struct stat st;
853 +#endif
854  
855         if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &file) == FAILURE) {
856                 ZEND_WRONG_PARAM_COUNT();
857 @@ -3162,24 +3548,18 @@
858  
859         convert_to_string_ex(file);
860  
861 +#ifdef PHP_WIN32
862 +       if (VCWD_STAT(Z_STRVAL_PP(file), &st) < 0) {
863 +               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Font file not found (%s)", Z_STRVAL_PP(file));
864 +               RETURN_FALSE;
865 +       }
866 +#endif
867 +
868         f_ind = T1_AddFont(Z_STRVAL_PP(file));
869  
870         if (f_ind < 0) {
871 -               switch (f_ind) {
872 -               case -1:
873 -                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't find the font file");
874 -                       RETURN_FALSE;
875 -                       break;
876 -               case -2:
877 -               case -3:
878 -                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Memory allocation fault in t1lib");
879 -                       RETURN_FALSE;
880 -                       break;
881 -               default:
882 -                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "An unknown error occurred in t1lib");
883 -                       RETURN_FALSE;
884 -                       break;
885 -               }
886 +               php_error_docref(NULL TSRMLS_CC, E_WARNING, "T1Lib Error (%i): %s", f_ind, T1_StrError(f_ind));
887 +               RETURN_FALSE;
888         }
889  
890         if (T1_LoadFont(f_ind)) {
891 @@ -3317,6 +3697,11 @@
892  
893         T1_DeleteAllSizes(*f_ind);
894  
895 +       if (Z_DVAL_PP(ext) <= 0) {
896 +               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Second parameter %F out of range (must be > 0)", Z_DVAL_PP(ext));
897 +               RETURN_FALSE;
898 +       }
899 +
900         if (T1_ExtendFont(*f_ind, Z_DVAL_PP(ext)) != 0) {
901                 RETURN_FALSE;
902         }
903 @@ -3348,7 +3733,7 @@
904  }
905  /* }}} */
906  
907 -/* {{{ proto array imagepstext(resource image, string text, resource font, int size, int xcoord, int ycoord [, int space, int tightness, float angle, int antialias])
908 +/* {{{ proto array imagepstext(resource image, string text, resource font, int size, int foreground, int background, int xcoord, int ycoord [, int space, int tightness, float angle, int antialias])
909     Rasterize a string over an image */
910  PHP_FUNCTION(imagepstext)
911  {
912 @@ -3371,11 +3756,6 @@
913         T1_TMATRIX *transform = NULL;
914         char *str;
915         int str_len;
916 -       int argc = ZEND_NUM_ARGS();
917 -
918 -       if (argc != 8 && argc != 12) {
919 -               ZEND_WRONG_PARAM_COUNT();
920 -       }
921  
922         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsrlllll|lldl", &img, &str, &str_len, &fnt, &size, &_fg, &_bg, &x, &y, &space, &width, &angle, &aa_steps) == FAILURE) {
923                 return;
924 @@ -3455,7 +3835,7 @@
925  
926                 if (!str_path) {
927                         if (T1_errno) {
928 -                               php_error_docref(NULL TSRMLS_CC, E_WARNING, "libt1 returned error %d", T1_errno);
929 +                               php_error_docref(NULL TSRMLS_CC, E_WARNING, "T1Lib Error: %s", T1_StrError(T1_errno));
930                         }
931                         RETURN_FALSE;
932                 }
933 @@ -3476,7 +3856,7 @@
934                 str_img = T1_AASetString(*f_ind, str,  str_len, space, T1_KERNING, size, transform);
935         }
936         if (T1_errno) {
937 -               php_error_docref(NULL TSRMLS_CC, E_WARNING, "libt1 returned error %d", T1_errno);
938 +               php_error_docref(NULL TSRMLS_CC, E_WARNING, "T1Lib Error: %s", T1_StrError(T1_errno));
939                 RETURN_FALSE;
940         }
941  
942 @@ -3719,7 +4099,10 @@
943         int int_threshold;
944         int x, y;
945         float x_ratio, y_ratio;
946 -
947 +#ifdef HAVE_GD_JPG
948 +    long ignore_warning;
949 +#endif
950 +       
951         if (argc != 5 || zend_get_parameters_ex(argc, &f_org, &f_dest, &height, &width, &threshold) == FAILURE) {
952                 ZEND_WRONG_PARAM_COUNT();
953         }
954 @@ -3775,7 +4158,12 @@
955  
956  #ifdef HAVE_GD_JPG
957                 case PHP_GDIMG_TYPE_JPG:
958 +                       ignore_warning = INI_INT("gd.jpeg_ignore_warning");
959 +#ifdef HAVE_GD_BUNDLED
960 +                       im_org = gdImageCreateFromJpeg(org, ignore_warning);
961 +#else
962                         im_org = gdImageCreateFromJpeg(org);
963 +#endif
964                         if (im_org == NULL) {
965                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open '%s' Not a valid JPEG file", fn_dest);
966                                 RETURN_FALSE;
967 @@ -3886,7 +4274,282 @@
968  /* }}} */
969  #endif /* HAVE_GD_WBMP */
970  
971 +#endif /* HAVE_LIBGD */
972 +
973 +/* Section Filters */
974  #ifdef HAVE_GD_BUNDLED
975 +
976 +#define PHP_GD_SINGLE_RES      \
977 +       zval **SIM;     \
978 +       gdImagePtr im_src;      \
979 +       if (zend_get_parameters_ex(1, &SIM) == FAILURE) {       \
980 +               RETURN_FALSE;   \
981 +       }       \
982 +       ZEND_FETCH_RESOURCE(im_src, gdImagePtr, SIM, -1, "Image", le_gd);       \
983 +       if (im_src == NULL) {   \
984 +               RETURN_FALSE;   \
985 +       }
986 +
987 +static void php_image_filter_negate(INTERNAL_FUNCTION_PARAMETERS)
988 +{
989 +       PHP_GD_SINGLE_RES
990 +
991 +       if (gdImageNegate(im_src) == 1) {
992 +               RETURN_TRUE;
993 +       }
994 +
995 +       RETURN_FALSE;
996 +}
997 +
998 +static void php_image_filter_grayscale(INTERNAL_FUNCTION_PARAMETERS)
999 +{
1000 +       PHP_GD_SINGLE_RES
1001 +
1002 +       if (gdImageGrayScale(im_src) == 1) {
1003 +               RETURN_TRUE;
1004 +       }
1005 +
1006 +       RETURN_FALSE;
1007 +}
1008 +
1009 +static void php_image_filter_brightness(INTERNAL_FUNCTION_PARAMETERS)
1010 +{
1011 +       zval *SIM;
1012 +       gdImagePtr im_src;
1013 +       long brightness, tmp;
1014 +
1015 +       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zll", &SIM, &tmp, &brightness) == FAILURE) {
1016 +               RETURN_FALSE;
1017 +       }
1018 +
1019 +       ZEND_FETCH_RESOURCE(im_src, gdImagePtr, &SIM, -1, "Image", le_gd);
1020 +
1021 +       if (im_src == NULL) {
1022 +               RETURN_FALSE;
1023 +       }
1024 +
1025 +       if (gdImageBrightness(im_src, (int)brightness) == 1) {
1026 +               RETURN_TRUE;
1027 +       }
1028 +
1029 +       RETURN_FALSE;
1030 +}
1031 +
1032 +static void php_image_filter_contrast(INTERNAL_FUNCTION_PARAMETERS)
1033 +{
1034 +       zval *SIM;
1035 +       gdImagePtr im_src;
1036 +       long contrast, tmp;
1037 +
1038 +       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &SIM, &tmp, &contrast) == FAILURE) {
1039 +               RETURN_FALSE;
1040 +       }
1041 +
1042 +       ZEND_FETCH_RESOURCE(im_src, gdImagePtr, &SIM, -1, "Image", le_gd);
1043 +
1044 +       if (im_src == NULL) {
1045 +               RETURN_FALSE;
1046 +       }
1047 +
1048 +       if (gdImageContrast(im_src, (int)contrast) == 1) {
1049 +               RETURN_TRUE;
1050 +       }
1051 +
1052 +       RETURN_FALSE;
1053 +}
1054 +
1055 +static void php_image_filter_colorize(INTERNAL_FUNCTION_PARAMETERS)
1056 +{
1057 +       zval *SIM;
1058 +       gdImagePtr im_src;
1059 +       long r,g,b,tmp;
1060 +       long a = 0;
1061 +
1062 +       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll|l", &SIM, &tmp, &r, &g, &b, &a) == FAILURE) {
1063 +               RETURN_FALSE;
1064 +       }
1065 +
1066 +       ZEND_FETCH_RESOURCE(im_src, gdImagePtr, &SIM, -1, "Image", le_gd);
1067 +
1068 +       if (im_src == NULL) {
1069 +               RETURN_FALSE;
1070 +       }
1071 +
1072 +       if (gdImageColor(im_src, (int) r, (int) g, (int) b, (int) a) == 1) {
1073 +               RETURN_TRUE;
1074 +       }
1075 +
1076 +       RETURN_FALSE;
1077 +}
1078 +
1079 +static void php_image_filter_edgedetect(INTERNAL_FUNCTION_PARAMETERS)
1080 +{
1081 +       PHP_GD_SINGLE_RES
1082 +
1083 +       if (gdImageEdgeDetectQuick(im_src) == 1) {
1084 +               RETURN_TRUE;
1085 +       }
1086 +
1087 +       RETURN_FALSE;
1088 +}
1089 +
1090 +static void php_image_filter_emboss(INTERNAL_FUNCTION_PARAMETERS)
1091 +{
1092 +       PHP_GD_SINGLE_RES
1093 +
1094 +       if (gdImageEmboss(im_src) == 1) {
1095 +               RETURN_TRUE;
1096 +       }
1097 +
1098 +       RETURN_FALSE;
1099 +}
1100 +
1101 +static void php_image_filter_gaussian_blur(INTERNAL_FUNCTION_PARAMETERS)
1102 +{
1103 +       PHP_GD_SINGLE_RES
1104 +
1105 +       if (gdImageGaussianBlur(im_src) == 1) {
1106 +               RETURN_TRUE;
1107 +       }
1108 +
1109 +       RETURN_FALSE;
1110 +}
1111 +
1112 +static void php_image_filter_selective_blur(INTERNAL_FUNCTION_PARAMETERS)
1113 +{
1114 +       PHP_GD_SINGLE_RES
1115 +
1116 +       if (gdImageSelectiveBlur(im_src) == 1) {
1117 +               RETURN_TRUE;
1118 +       }
1119 +
1120 +       RETURN_FALSE;
1121 +}
1122 +
1123 +static void php_image_filter_mean_removal(INTERNAL_FUNCTION_PARAMETERS)
1124 +{
1125 +       PHP_GD_SINGLE_RES
1126 +
1127 +       if (gdImageMeanRemoval(im_src) == 1) {
1128 +               RETURN_TRUE;
1129 +       }
1130 +
1131 +       RETURN_FALSE;
1132 +}
1133 +
1134 +static void php_image_filter_smooth(INTERNAL_FUNCTION_PARAMETERS)
1135 +{
1136 +       zval *SIM;
1137 +       long tmp;
1138 +       gdImagePtr im_src;
1139 +       double weight;
1140 +
1141 +       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rld", &SIM, &tmp, &weight) == FAILURE) {
1142 +               RETURN_FALSE;
1143 +       }
1144 +
1145 +       ZEND_FETCH_RESOURCE(im_src, gdImagePtr, &SIM, -1, "Image", le_gd);
1146 +
1147 +       if (im_src == NULL) {
1148 +               RETURN_FALSE;
1149 +       }
1150 +
1151 +       if (gdImageSmooth(im_src, (float)weight)==1) {
1152 +               RETURN_TRUE;
1153 +       }
1154 +
1155 +       RETURN_FALSE;
1156 +}
1157 +
1158 +/* {{{ proto bool imagefilter(resource src_im, int filtertype, [args] )
1159 +   Applies Filter an image using a custom angle */
1160 +PHP_FUNCTION(imagefilter)
1161 +{
1162 +       zval *tmp;
1163 +
1164 +       typedef void (*image_filter)(INTERNAL_FUNCTION_PARAMETERS);
1165 +       long filtertype;
1166 +       image_filter filters[] =
1167 +       {
1168 +               php_image_filter_negate ,
1169 +               php_image_filter_grayscale,
1170 +               php_image_filter_brightness,
1171 +               php_image_filter_contrast,
1172 +               php_image_filter_colorize,
1173 +               php_image_filter_edgedetect,
1174 +               php_image_filter_emboss,
1175 +               php_image_filter_gaussian_blur,
1176 +               php_image_filter_selective_blur,
1177 +               php_image_filter_mean_removal,
1178 +               php_image_filter_smooth
1179 +       };
1180 +
1181 +       if (ZEND_NUM_ARGS() < 2 || ZEND_NUM_ARGS() > 6) {
1182 +               WRONG_PARAM_COUNT;
1183 +       } else if (zend_parse_parameters(2 TSRMLS_CC, "rl", &tmp, &filtertype) == FAILURE) {
1184 +               return;
1185 +       }
1186 +
1187 +       if (filtertype >= 0 && filtertype <= IMAGE_FILTER_MAX) {
1188 +               filters[filtertype](INTERNAL_FUNCTION_PARAM_PASSTHRU);
1189 +       }
1190 +}
1191 +/* }}} */
1192 +
1193 +/* {{{ proto resource imageconvolution(resource src_im, array matrix3x3, double div, double offset)
1194 +   Apply a 3x3 convolution matrix, using coefficient div and offset */
1195 +PHP_FUNCTION(imageconvolution)
1196 +{
1197 +       zval *SIM, *hash_matrix;
1198 +       zval **var = NULL, **var2 = NULL;
1199 +       gdImagePtr im_src = NULL;
1200 +       double div, offset;
1201 +       int nelem, i, j, res;
1202 +       float matrix[3][3] = {{0,0,0}, {0,0,0}, {0,0,0}};
1203 +
1204 +       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "radd", &SIM, &hash_matrix, &div, &offset) == FAILURE) {
1205 +               RETURN_FALSE;
1206 +       }
1207 +
1208 +       ZEND_FETCH_RESOURCE(im_src, gdImagePtr, &SIM, -1, "Image", le_gd);
1209 +
1210 +       nelem = zend_hash_num_elements(Z_ARRVAL_P(hash_matrix));
1211 +       if (nelem != 3) {
1212 +               php_error_docref(NULL TSRMLS_CC, E_WARNING, "You must have 3x3 array");
1213 +               RETURN_FALSE;
1214 +       }
1215 +
1216 +       for (i=0; i<3; i++) {
1217 +               if (zend_hash_index_find(Z_ARRVAL_P(hash_matrix), (i), (void **) &var) == SUCCESS && Z_TYPE_PP(var) == IS_ARRAY) {
1218 +                       if (Z_TYPE_PP(var) != IS_ARRAY || zend_hash_num_elements(Z_ARRVAL_PP(var)) != 3 ) {
1219 +                               php_error_docref(NULL TSRMLS_CC, E_WARNING, "You must have 3x3 array");
1220 +                               RETURN_FALSE;
1221 +                       }
1222 +
1223 +                       for (j=0; j<3; j++) {
1224 +                               if (zend_hash_index_find(Z_ARRVAL_PP(var), (j), (void **) &var2) == SUCCESS) {
1225 +                                       SEPARATE_ZVAL(var2);
1226 +                                       convert_to_double(*var2);
1227 +                                       matrix[i][j] = Z_DVAL_PP(var2);
1228 +                               } else {
1229 +                                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "You must have a 3x3 matrix");
1230 +                                       RETURN_FALSE;
1231 +                               }
1232 +                       }
1233 +               }
1234 +       }
1235 +       res = gdImageConvolution(im_src, matrix, div, offset);
1236 +
1237 +       if (res) {
1238 +               RETURN_TRUE;
1239 +       } else {
1240 +               RETURN_FALSE;
1241 +       }
1242 +}
1243 +/* }}} */
1244 +
1245 +/* End section: Filters */
1246 +
1247  /* {{{ proto bool imageantialias(resource im, bool on)
1248     Should antialiased functions used or not*/
1249  PHP_FUNCTION(imageantialias)
1250 @@ -3908,8 +4571,6 @@
1251  /* }}} */
1252  #endif
1253  
1254 -#endif /* HAVE_LIBGD */
1255 -
1256  /*
1257   * Local variables:
1258   * tab-width: 4
1259 diff -urN php-4.4.8.org/ext/gd/gdcache.c php-4.4.8/ext/gd/gdcache.c
1260 --- php-4.4.8.org/ext/gd/gdcache.c      2005-01-09 22:05:31.000000000 +0100
1261 +++ php-4.4.8/ext/gd/gdcache.c  2008-01-22 22:38:05.863816915 +0100
1262 @@ -1,8 +1,8 @@
1263 -/* 
1264 - * $Id$
1265 +/*
1266 + * $Id$
1267   *
1268 - * Caches of pointers to user structs in which the least-recently-used 
1269 - * element is replaced in the event of a cache miss after the cache has 
1270 + * Caches of pointers to user structs in which the least-recently-used
1271 + * element is replaced in the event of a cache miss after the cache has
1272   * reached a given size.
1273   *
1274   * John Ellson  (ellson@lucent.com)  Oct 31, 1997
1275 @@ -17,17 +17,17 @@
1276   * The head structure has a pointer to the most-recently-used
1277   * element, and elements are moved to this position in the list each
1278   * time they are used.  The head also contains pointers to three
1279 - * user defined functions: 
1280 - *             - a function to test if a cached userdata matches some keydata 
1281 - *             - a function to provide a new userdata struct to the cache 
1282 + * user defined functions:
1283 + *             - a function to test if a cached userdata matches some keydata
1284 + *             - a function to provide a new userdata struct to the cache
1285   *          if there has been a cache miss.
1286   *             - a function to release a userdata struct when it is
1287   *          no longer being managed by the cache
1288   *
1289   * In the event of a cache miss the cache is allowed to grow up to
1290   * a specified maximum size.  After the maximum size is reached then
1291 - * the least-recently-used element is discarded to make room for the 
1292 - * new.  The most-recently-returned value is always left at the 
1293 + * the least-recently-used element is discarded to make room for the
1294 + * new.  The most-recently-returned value is always left at the
1295   * beginning of the list after retrieval.
1296   *
1297   * In the current implementation the cache is traversed by a linear
1298 @@ -59,9 +59,9 @@
1299         int                                     size,
1300         gdCacheTestFn_t         gdCacheTest,
1301         gdCacheFetchFn_t        gdCacheFetch,
1302 -       gdCacheReleaseFn_t      gdCacheRelease ) 
1303 +       gdCacheReleaseFn_t      gdCacheRelease )
1304  {
1305 -       gdCache_head_t *head; 
1306 +       gdCache_head_t *head;
1307  
1308         head = (gdCache_head_t *)pemalloc(sizeof(gdCache_head_t), 1);
1309         head->mru = NULL;
1310 diff -urN php-4.4.8.org/ext/gd/gdcache.h php-4.4.8/ext/gd/gdcache.h
1311 --- php-4.4.8.org/ext/gd/gdcache.h      2003-03-05 17:04:20.000000000 +0100
1312 +++ php-4.4.8/ext/gd/gdcache.h  2008-01-22 22:38:05.863816915 +0100
1313 @@ -1,8 +1,8 @@
1314 -/* 
1315 - * $Id$ 
1316 +/*
1317 + * $Id$
1318   *
1319 - * Caches of pointers to user structs in which the least-recently-used 
1320 - * element is replaced in the event of a cache miss after the cache has 
1321 + * Caches of pointers to user structs in which the least-recently-used
1322 + * element is replaced in the event of a cache miss after the cache has
1323   * reached a given size.
1324   *
1325   * John Ellson  (ellson@lucent.com)  Oct 31, 1997
1326 @@ -17,17 +17,17 @@
1327   * The head structure has a pointer to the most-recently-used
1328   * element, and elements are moved to this position in the list each
1329   * time they are used.  The head also contains pointers to three
1330 - * user defined functions: 
1331 - *             - a function to test if a cached userdata matches some keydata 
1332 - *             - a function to provide a new userdata struct to the cache 
1333 + * user defined functions:
1334 + *             - a function to test if a cached userdata matches some keydata
1335 + *             - a function to provide a new userdata struct to the cache
1336   *          if there has been a cache miss.
1337   *             - a function to release a userdata struct when it is
1338   *          no longer being managed by the cache
1339   *
1340   * In the event of a cache miss the cache is allowed to grow up to
1341   * a specified maximum size.  After the maximum size is reached then
1342 - * the least-recently-used element is discarded to make room for the 
1343 - * new.  The most-recently-returned value is always left at the 
1344 + * the least-recently-used element is discarded to make room for the
1345 + * new.  The most-recently-returned value is always left at the
1346   * beginning of the list after retrieval.
1347   *
1348   * In the current implementation the cache is traversed by a linear
1349 diff -urN php-4.4.8.org/ext/gd/gd_ctx.c php-4.4.8/ext/gd/gd_ctx.c
1350 --- php-4.4.8.org/ext/gd/gd_ctx.c       2007-12-31 08:22:47.000000000 +0100
1351 +++ php-4.4.8/ext/gd/gd_ctx.c   2008-01-22 22:38:05.863816915 +0100
1352 @@ -1,13 +1,13 @@
1353  /*
1354     +----------------------------------------------------------------------+
1355 -   | PHP Version 4                                                        |
1356 +   | PHP Version 5                                                        |
1357     +----------------------------------------------------------------------+
1358 -   | Copyright (c) 1997-2008 The PHP Group                                |
1359 +   | Copyright (c) 1997-2007 The PHP Group                                |
1360     +----------------------------------------------------------------------+
1361     | This source file is subject to version 3.01 of the PHP license,      |
1362     | that is bundled with this package in the file LICENSE, and is        |
1363     | available through the world-wide-web at the following url:           |
1364 -   | http://www.php.net/license/3_0.txt.                                  |
1365 +   | http://www.php.net/license/3_01.txt                                  |
1366     | If you did not receive a copy of the PHP license and are unable to   |
1367     | obtain it through the world-wide-web, please send a note to          |
1368     | license@php.net so we can mail you a copy immediately.               |
1369 @@ -15,11 +15,13 @@
1370     | Authors: Stanislav Malyshev <stas@php.net>                           |
1371     +----------------------------------------------------------------------+
1372   */
1373 -#include "php_gd.h"
1374  
1375 +/* $Id$ */
1376 +
1377 +#include "php_gd.h"
1378  
1379  #define CTX_PUTC(c,ctx) ctx->putC(ctx, c)
1380 -       
1381 +
1382  static void _php_image_output_putc(struct gdIOCtx *ctx, int c)
1383  {
1384         /* without the following downcast, the write will fail
1385 @@ -43,20 +45,29 @@
1386                 efree(ctx);
1387         }
1388  }
1389 -       
1390 -static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)()) 
1391 +
1392 +/* {{{ _php_image_output_ctx */
1393 +static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)())
1394  {
1395 -       zval **imgind, **file, **quality;
1396 +       zval **imgind, **file, **quality, **basefilter;
1397         gdImagePtr im;
1398         char *fn = NULL;
1399         FILE *fp = NULL;
1400         int argc = ZEND_NUM_ARGS();
1401         int q = -1, i;
1402 +       int f = -1;
1403         gdIOCtx *ctx;
1404  
1405 -       /* The quality parameter for Wbmp stands for the threshold when called from image2wbmp() */
1406 -       
1407 -       if (argc < 1 || argc > 3 || zend_get_parameters_ex(argc, &imgind, &file, &quality) == FAILURE) 
1408 +       /* The third (quality) parameter for Wbmp stands for the threshold when called from image2wbmp().
1409 +        * The third (quality) parameter for Wbmp and Xbm stands for the foreground color index when called
1410 +        * from imagey<type>().
1411 +        */
1412 +
1413 +       if (argc < 2 && image_type == PHP_GDIMG_TYPE_XBM) {
1414 +               WRONG_PARAM_COUNT;
1415 +       }
1416 +
1417 +       if (argc < 1 || argc > 4 || zend_get_parameters_ex(argc, &imgind, &file, &quality, &basefilter) == FAILURE)
1418         {
1419                 WRONG_PARAM_COUNT;
1420         }
1421 @@ -64,20 +75,27 @@
1422         ZEND_FETCH_RESOURCE(im, gdImagePtr, imgind, -1, "Image", phpi_get_le_gd());
1423  
1424         if (argc > 1) {
1425 -               convert_to_string_ex(file);
1426 +               if (argc >= 2 && Z_TYPE_PP(file) != IS_NULL) {
1427 +                       convert_to_string_ex(file);
1428 +               }
1429                 fn = Z_STRVAL_PP(file);
1430 -               if (argc == 3) {
1431 +               if (argc >= 3) {
1432                         convert_to_long_ex(quality);
1433 -                       q = Z_LVAL_PP(quality);
1434 +                       q = Z_LVAL_PP(quality);/* or colorindex for foreground of BW images (defaults to black) */
1435 +                       if (argc == 4) {
1436 +                               convert_to_long_ex(basefilter);
1437 +                               f = Z_LVAL_PP(basefilter);
1438 +                       }
1439                 }
1440         }
1441  
1442 -       if ((argc == 2) || (argc > 2 && Z_STRLEN_PP(file))) {
1443 +       if (argc > 1 && (Z_TYPE_PP(file) != IS_NULL && ((argc == 2) || (argc > 2 && Z_STRLEN_PP(file))))) {
1444 +
1445                 PHP_GD_CHECK_OPEN_BASEDIR(fn, "Invalid filename");
1446  
1447                 fp = VCWD_FOPEN(fn, "wb");
1448                 if (!fp) {
1449 -                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open '%s' for writing", fn);
1450 +                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open '%s' for writing: %s", fn, strerror(errno));
1451                         RETURN_FALSE;
1452                 }
1453  
1454 @@ -90,7 +108,7 @@
1455                 ctx->gd_free = _php_image_output_ctxfree;
1456  #else
1457                 ctx->free = _php_image_output_ctxfree;
1458 -#endif         
1459 +#endif
1460  
1461  #if APACHE && defined(CHARSET_EBCDIC)
1462                 /* XXX this is unlikely to work any more thies@thieso.net */
1463 @@ -107,27 +125,48 @@
1464                 case PHP_GDIMG_TYPE_JPG:
1465                         (*func_p)(im, ctx, q);
1466                         break;
1467 +               case PHP_GDIMG_TYPE_PNG:
1468 +                       (*func_p)(im, ctx, q, f);
1469 +                       break;
1470 +               case PHP_GDIMG_TYPE_XBM:
1471                 case PHP_GDIMG_TYPE_WBM:
1472 -                       for(i=0; i < gdImageColorsTotal(im); i++) {
1473 -                               if(gdImageRed(im, i) == 0) break;
1474 -                       } 
1475 -                       (*func_p)(im, i, ctx);
1476 +                       if (argc < 3) {
1477 +                               for(i=0; i < gdImageColorsTotal(im); i++) {
1478 +                                       if(!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) break;
1479 +                               }
1480 +                               q = i;
1481 +                       }
1482 +                       if (image_type == PHP_GDIMG_TYPE_XBM) {
1483 +                               (*func_p)(im, fn, q, ctx);
1484 +                       } else {
1485 +                               (*func_p)(im, q, ctx);
1486 +                       }
1487                         break;
1488                 default:
1489                         (*func_p)(im, ctx);
1490                         break;
1491         }
1492  
1493 -#if HAVE_LIBGD204      
1494 +#if HAVE_LIBGD204
1495         ctx->gd_free(ctx);
1496  #else
1497         ctx->free(ctx);
1498 -#endif         
1499 +#endif
1500  
1501         if(fp) {
1502                 fflush(fp);
1503                 fclose(fp);
1504         }
1505 -       
1506 +
1507      RETURN_TRUE;
1508  }
1509 +/* }}} */
1510 +
1511 +/*
1512 + * Local variables:
1513 + * tab-width: 4
1514 + * c-basic-offset: 4
1515 + * End:
1516 + * vim600: sw=4 ts=4 fdm=marker
1517 + * vim<600: sw=4 ts=4
1518 + */
1519 diff -urN php-4.4.8.org/ext/gd/gdttf.c php-4.4.8/ext/gd/gdttf.c
1520 --- php-4.4.8.org/ext/gd/gdttf.c        2005-01-09 22:05:31.000000000 +0100
1521 +++ php-4.4.8/ext/gd/gdttf.c    2008-01-22 22:38:05.887151436 +0100
1522 @@ -2,7 +2,7 @@
1523  /*                                          */
1524  /* John Ellson   ellson@lucent.com          */
1525  
1526 -/* $Id$ */
1527 +/* $Id$ */
1528  
1529  #include "php.h"
1530  
1531 @@ -28,11 +28,11 @@
1532  /* number of fonts cached before least recently used is replaced */
1533  #define FONTCACHESIZE 6
1534  
1535 -/* number of character glyphs cached per font before 
1536 +/* number of character glyphs cached per font before
1537         least-recently-used is replaced */
1538  #define GLYPHCACHESIZE 120
1539  
1540 -/* number of bitmaps cached per glyph before 
1541 +/* number of bitmaps cached per glyph before
1542         least-recently-used is replaced */
1543  #define BITMAPCACHESIZE 8
1544  
1545 @@ -42,15 +42,15 @@
1546  /* ptsize below which anti-aliasing is ineffective */
1547  #define MINANTIALIASPTSIZE 0
1548  
1549 -/* display resolution - (Not really.  This has to be 72 or hinting is wrong) */ 
1550 +/* display resolution - (Not really.  This has to be 72 or hinting is wrong) */
1551  #define RESOLUTION 72
1552  
1553  /* Number of colors used for anti-aliasing */
1554  #undef NUMCOLORS
1555  #define NUMCOLORS 4
1556  
1557 -/* Line separation as a factor of font height.  
1558 -      No space between if LINESPACE = 1.00 
1559 +/* Line separation as a factor of font height.
1560 +      No space between if LINESPACE = 1.00
1561        Line separation will be rounded up to next pixel row*/
1562  #define LINESPACE 1.05
1563  
1564 @@ -125,7 +125,7 @@
1565         glyph_t                         *glyph;
1566  } bitmapkey_t;
1567  
1568 -typedef struct { 
1569 +typedef struct {
1570      unsigned char       pixel;         /* key */
1571      unsigned char       bgcolor;       /* key */
1572      int                                        fgcolor;        /* key */ /* -ve means no antialias */
1573 @@ -138,7 +138,7 @@
1574      unsigned char       bgcolor;    /* key */
1575      int                                        fgcolor;    /* key */ /* -ve means no antialias */
1576      gdImagePtr          im;                    /* key */
1577 -} tweencolorkey_t;  
1578 +} tweencolorkey_t;
1579  
1580  /* forward declarations so that glyphCache can be initialized by font code */
1581  static int glyphTest ( void *element, void *key );
1582 @@ -196,7 +196,7 @@
1583   *
1584   *---------------------------------------------------------------------------
1585   */
1586
1587 +
1588  #ifndef CHARSET_EBCDIC
1589  #define ASC(ch) (ch)
1590  #else /*CHARSET_EBCDIC*/
1591 @@ -205,17 +205,16 @@
1592  
1593  #define Tcl_UniChar int
1594  #define TCL_UTF_MAX 3
1595 -static int
1596 -gdTcl_UtfToUniChar(char *str, Tcl_UniChar *chPtr)
1597 +static int gdTcl_UtfToUniChar(char *str, Tcl_UniChar *chPtr)
1598  /* str is the UTF8 next character pointer */
1599  /* chPtr is the int for the result */
1600  {
1601 -    int byte;
1602 -    
1603 +       int byte;
1604 +
1605         /* HTML4.0 entities in decimal form, e.g. &#197; */
1606 -    byte = *((unsigned char *) str);
1607 +       byte = *((unsigned char *) str);
1608         if (byte == '&') {
1609 -               int i, n=0;
1610 +               int i, n = 0;
1611  
1612                 byte = *((unsigned char *) (str+1));
1613                 if (byte == '#') {
1614 @@ -223,9 +222,9 @@
1615                                 byte = *((unsigned char *) (str+i));
1616                                 if (byte >= '0' && byte <= '9') {
1617                                         n = (n * 10) + (byte - '0');
1618 -                               } 
1619 -                               else
1620 +                               } else {
1621                                         break;
1622 +                               }
1623                         }
1624                         if (byte == ';') {
1625                                 *chPtr = (Tcl_UniChar) n;
1626 @@ -233,105 +232,91 @@
1627                         }
1628                 }
1629         }
1630 -        
1631 -    /*
1632 -     * Unroll 1 to 3 byte UTF-8 sequences, use loop to handle longer ones.
1633 -     */
1634 -
1635 -    byte = ASC(*((unsigned char *) str));
1636 -    if (byte < 0xC0) {
1637 -       /*
1638 -        * Handles properly formed UTF-8 characters between 0x01 and 0x7F.
1639 -        * Also treats \0 and naked trail bytes 0x80 to 0xBF as valid
1640 -        * characters representing themselves.
1641 -        */
1642  
1643 -       *chPtr = (Tcl_UniChar) byte;
1644 -       return 1;
1645 -    } else if (byte < 0xE0) {
1646 -       if ((ASC(str[1]) & 0xC0) == 0x80) {
1647 -           /*
1648 -            * Two-byte-character lead-byte followed by a trail-byte.
1649 -            */
1650 -            
1651 -           *chPtr = (Tcl_UniChar) (((byte & 0x1F) << 6) | (ASC(str[1]) & 0x3F));
1652 -           return 2;
1653 -       }
1654 -       /*
1655 -        * A two-byte-character lead-byte not followed by trail-byte
1656 -        * represents itself.
1657 -        */
1658 -        
1659 -       *chPtr = (Tcl_UniChar) byte;
1660 -       return 1;
1661 -    } else if (byte < 0xF0) {
1662 -       if (((ASC(str[1]) & 0xC0) == 0x80) && ((ASC(str[2]) & 0xC0) == 0x80)) {
1663 -           /*
1664 -            * Three-byte-character lead byte followed by two trail bytes.
1665 -            */
1666 -
1667 -           *chPtr = (Tcl_UniChar) (((byte & 0x0F) << 12) 
1668 -                   | ((ASC(str[1]) & 0x3F) << 6) | (ASC(str[2]) & 0x3F));
1669 -           return 3;
1670 -       }
1671 -       /*
1672 -        * A three-byte-character lead-byte not followed by two trail-bytes
1673 -        * represents itself.
1674 -        */
1675 +       /* Unroll 1 to 3 byte UTF-8 sequences, use loop to handle longer ones. */
1676  
1677 -       *chPtr = (Tcl_UniChar) byte;
1678 -       return 1;
1679 -    }
1680 +       byte = ASC(*((unsigned char *) str));
1681 +       if (byte < 0xC0) {
1682 +               /*
1683 +                * Handles properly formed UTF-8 characters between 0x01 and 0x7F.
1684 +                * Also treats \0 and naked trail bytes 0x80 to 0xBF as valid
1685 +                * characters representing themselves.
1686 +                */
1687 +
1688 +               *chPtr = (Tcl_UniChar) byte;
1689 +               return 1;
1690 +       } else if (byte < 0xE0) {
1691 +               if ((ASC(str[1]) & 0xC0) == 0x80) {
1692 +                       /* Two-byte-character lead-byte followed by a trail-byte. */
1693 +
1694 +                       *chPtr = (Tcl_UniChar) (((byte & 0x1F) << 6) | (ASC(str[1]) & 0x3F));
1695 +                       return 2;
1696 +               }
1697 +               /*
1698 +                * A two-byte-character lead-byte not followed by trail-byte
1699 +                * represents itself.
1700 +                */
1701 +
1702 +               *chPtr = (Tcl_UniChar) byte;
1703 +               return 1;
1704 +       } else if (byte < 0xF0) {
1705 +               if (((ASC(str[1]) & 0xC0) == 0x80) && ((ASC(str[2]) & 0xC0) == 0x80)) {
1706 +                   /* Three-byte-character lead byte followed by two trail bytes. */
1707 +
1708 +                       *chPtr = (Tcl_UniChar) (((byte & 0x0F) << 12) | ((ASC(str[1]) & 0x3F) << 6) | (ASC(str[2]) & 0x3F));
1709 +                       return 3;
1710 +               }
1711 +               /* A three-byte-character lead-byte not followed by two trail-bytes represents itself. */
1712 +
1713 +               *chPtr = (Tcl_UniChar) byte;
1714 +               return 1;
1715 +       }
1716  #if TCL_UTF_MAX > 3
1717 -    else {
1718 -       int ch, total, trail;
1719 +       else {
1720 +               int ch, total, trail;
1721  
1722 -       total = totalBytes[byte];
1723 -       trail = total - 1;
1724 -       if (trail > 0) {
1725 -           ch = byte & (0x3F >> trail);
1726 -           do {
1727 -               str++;
1728 -               if ((ASC(*str) & 0xC0) != 0x80) {
1729 -                   *chPtr = byte;
1730 -                   return 1;
1731 -               }
1732 -               ch <<= 6;
1733 -               ch |= (ASC(*str) & 0x3F);
1734 -               trail--;
1735 -           } while (trail > 0);
1736 -           *chPtr = ch;
1737 -           return total;
1738 +               total = totalBytes[byte];
1739 +               trail = total - 1;
1740 +               if (trail > 0) {
1741 +                       ch = byte & (0x3F >> trail);
1742 +                       do {
1743 +                               str++;
1744 +                               if ((ASC(*str) & 0xC0) != 0x80) {
1745 +                                       *chPtr = byte;
1746 +                                       return 1;
1747 +                               }
1748 +                               ch <<= 6;
1749 +                               ch |= (ASC(*str) & 0x3F);
1750 +                               trail--;
1751 +                       } while (trail > 0);
1752 +                       *chPtr = ch;
1753 +                       return total;
1754 +               }
1755         }
1756 -    }
1757  #endif
1758  
1759 -    *chPtr = (Tcl_UniChar) byte;
1760 -    return 1;
1761 +       *chPtr = (Tcl_UniChar) byte;
1762 +       return 1;
1763  }
1764  
1765  /********************************************************************/
1766  /* font cache functions                                             */
1767  
1768 -static int
1769 -fontTest ( void *element, void *key )
1770 +static int fontTest ( void *element, void *key )
1771  {
1772 -       font_t *a=(font_t *)element;
1773 -       fontkey_t *b=(fontkey_t *)key;
1774 +       font_t *a = (font_t *)element;
1775 +       fontkey_t *b = (fontkey_t *)key;
1776  
1777 -       return ( strcmp(a->fontname, b->fontname) == 0
1778 -                       &&      a->ptsize == b->ptsize
1779 -                       &&  a->angle == b->angle);
1780 +       return (strcmp(a->fontname, b->fontname) == 0 && a->ptsize == b->ptsize && a->angle == b->angle);
1781  }
1782  
1783 -static void *
1784 -fontFetch ( char **error, void *key )
1785 +static void * fontFetch ( char **error, void *key )
1786  {
1787 -       TT_Error                err;
1788 -       font_t                  *a;
1789 -       fontkey_t               *b=(fontkey_t *)key;
1790 -       int                             i, n, map_found;
1791 -       short                   platform, encoding;
1792 +       TT_Error        err;
1793 +       font_t          *a;
1794 +       fontkey_t       *b = (fontkey_t *)key;
1795 +       int             i, n, map_found;
1796 +       short           platform, encoding;
1797         TSRMLS_FETCH();
1798  
1799         a = (font_t *)pemalloc(sizeof(font_t), 1);
1800 @@ -354,8 +339,7 @@
1801         if ((err = TT_Open_Face(*b->engine, a->fontname, &a->face))) {
1802                 if (err == TT_Err_Could_Not_Open_File) {
1803                         *error = "Could not find/open font";
1804 -               }
1805 -               else {
1806 +               } else {
1807                         *error = "Could not read font";
1808                 }
1809                 pefree(a, 1);
1810 @@ -370,7 +354,7 @@
1811                 pefree(a, 1);
1812                 return NULL;
1813         }
1814 -       
1815 +
1816         if (TT_Set_Instance_Resolutions(a->instance, RESOLUTION, RESOLUTION)) {
1817                 *error = "Could not set device resolutions";
1818                 pefree(a, 1);
1819 @@ -384,12 +368,12 @@
1820         }
1821  
1822         TT_Get_Instance_Metrics(a->instance, &a->imetrics);
1823 -       
1824 +
1825         /* First, look for a Unicode charmap */
1826         n = TT_Get_CharMap_Count(a->face);
1827  
1828         for (i = 0; i < n; i++) {
1829 -        TT_Get_CharMap_ID(a->face, i, &platform, &encoding);
1830 +               TT_Get_CharMap_ID(a->face, i, &platform, &encoding);
1831                 if ((platform == 3 && encoding == 1)           /* Windows Unicode */
1832                         || (platform == 2 && encoding == 1)
1833                         || (platform == 0)) {        /* ?? Unicode */
1834 @@ -407,7 +391,7 @@
1835                 }
1836         }
1837  
1838 -       if (! map_found) {
1839 +       if (!map_found) {
1840                 *error = "Unable to find a CharMap that I can handle";
1841                 pefree(a, 1);
1842                 return NULL;
1843 @@ -418,16 +402,14 @@
1844         a->matrix.xy = - a->matrix.yx;
1845         a->matrix.yy = a->matrix.xx;
1846  
1847 -       a->glyphCache = gdCacheCreate( GLYPHCACHESIZE,
1848 -                                       glyphTest, glyphFetch, glyphRelease);
1849 +       a->glyphCache = gdCacheCreate(GLYPHCACHESIZE, glyphTest, glyphFetch, glyphRelease);
1850  
1851         return (void *)a;
1852  }
1853  
1854 -static void
1855 -fontRelease( void *element )
1856 +static void fontRelease( void *element )
1857  {
1858 -       font_t *a=(font_t *)element;
1859 +       font_t *a = (font_t *)element;
1860  
1861         gdCacheDelete(a->glyphCache);
1862         TT_Done_Instance(a->instance);
1863 @@ -439,26 +421,22 @@
1864  /********************************************************************/
1865  /* glyph cache functions                                            */
1866  
1867 -static int
1868 -glyphTest ( void *element, void *key )
1869 +static int glyphTest ( void *element, void *key )
1870  {
1871 -       glyph_t *a=(glyph_t *)element;
1872 -       glyphkey_t *b=(glyphkey_t *)key;
1873 +       glyph_t *a = (glyph_t *)element;
1874 +       glyphkey_t *b = (glyphkey_t *)key;
1875 +
1876 +       return (a->character == b->character && a->hinting == b->hinting && a->gray_render == b->gray_render);
1877 +}
1878  
1879 -       return (a->character == b->character
1880 -               && a->hinting == b->hinting
1881 -               && a->gray_render == b->gray_render);
1882 -}
1883 -
1884 -static void *
1885 -glyphFetch ( char **error, void *key )
1886 -{
1887 -       glyph_t                         *a;
1888 -       glyphkey_t                      *b=(glyphkey_t *)key;
1889 -       short                           glyph_code;
1890 -       int                                     flags, err;
1891 -       int                                     crect[8], xmin, xmax, ymin, ymax;
1892 -       double                          cos_a, sin_a;
1893 +static void * glyphFetch ( char **error, void *key )
1894 +{
1895 +       glyph_t         *a;
1896 +       glyphkey_t      *b = (glyphkey_t *)key;
1897 +       short           glyph_code;
1898 +       int             flags, err;
1899 +       int             crect[8], xmin, xmax, ymin, ymax;
1900 +       double          cos_a, sin_a;
1901  
1902         a = (glyph_t *)pemalloc(sizeof(glyph_t), 1);
1903         a->character = b->character;
1904 @@ -523,38 +501,34 @@
1905         a->Bit.flow = TT_Flow_Up;
1906         if (a->gray_render) {
1907                 a->Bit.cols = a->Bit.width;               /* 1 byte per pixel */
1908 -       }
1909 -       else {
1910 +       } else {
1911                 a->Bit.cols = (a->Bit.width + 7) / 8;     /* 1 bit per pixel */
1912         }
1913         a->Bit.cols = (a->Bit.cols + 3) & ~3;         /* pad to 32 bits */
1914         a->Bit.size = a->Bit.rows * a->Bit.cols;      /* # of bytes in buffer */
1915         a->Bit.bitmap = NULL;
1916  
1917 -       a->bitmapCache = gdCacheCreate( BITMAPCACHESIZE,
1918 -                                       bitmapTest, bitmapFetch, bitmapRelease);
1919 +       a->bitmapCache = gdCacheCreate(BITMAPCACHESIZE, bitmapTest, bitmapFetch, bitmapRelease);
1920  
1921         return (void *)a;
1922  }
1923  
1924 -static void
1925 -glyphRelease( void *element )
1926 +static void glyphRelease( void *element )
1927  {
1928 -       glyph_t *a=(glyph_t *)element;
1929 +       glyph_t *a = (glyph_t *)element;
1930  
1931         gdCacheDelete(a->bitmapCache);
1932 -       TT_Done_Glyph( a->glyph );
1933 -       pefree ((char *)element, 1);
1934 +       TT_Done_Glyph(a->glyph);
1935 +       pefree((char *)element, 1);
1936  }
1937  
1938  /********************************************************************/
1939  /* bitmap cache functions                                            */
1940  
1941 -static int
1942 -bitmapTest ( void *element, void *key )
1943 +static int bitmapTest ( void *element, void *key )
1944  {
1945 -       bitmap_t *a=(bitmap_t *)element;
1946 -       bitmapkey_t *b=(bitmapkey_t *)key;
1947 +       bitmap_t *a = (bitmap_t *)element;
1948 +       bitmapkey_t *b = (bitmapkey_t *)key;
1949  
1950         if (a->xoffset == b->xoffset && a->yoffset == b->yoffset) {
1951                 b->glyph->Bit.bitmap = a->bitmap;
1952 @@ -563,11 +537,10 @@
1953         return FALSE;
1954  }
1955  
1956 -static void *
1957 -bitmapFetch ( char **error, void *key )
1958 +static void * bitmapFetch ( char **error, void *key )
1959  {
1960 -       bitmap_t                        *a;
1961 -       bitmapkey_t                     *b=(bitmapkey_t *)key;
1962 +       bitmap_t        *a;
1963 +       bitmapkey_t     *b = (bitmapkey_t *)key;
1964  
1965         a = (bitmap_t *)pemalloc(sizeof(bitmap_t), 1);
1966         a->xoffset = b->xoffset;
1967 @@ -577,56 +550,47 @@
1968         memset(a->bitmap, 0, b->glyph->Bit.size);
1969         /* render glyph */
1970         if (b->glyph->gray_render) {
1971 -               TT_Get_Glyph_Pixmap(b->glyph->glyph, &b->glyph->Bit,
1972 -                       a->xoffset, a->yoffset);
1973 -       }
1974 -       else {
1975 -               TT_Get_Glyph_Bitmap(b->glyph->glyph, &b->glyph->Bit,
1976 -                       a->xoffset, a->yoffset);
1977 +               TT_Get_Glyph_Pixmap(b->glyph->glyph, &b->glyph->Bit, a->xoffset, a->yoffset);
1978 +       } else {
1979 +               TT_Get_Glyph_Bitmap(b->glyph->glyph, &b->glyph->Bit, a->xoffset, a->yoffset);
1980         }
1981         return (void *)a;
1982  }
1983  
1984 -static void
1985 -bitmapRelease( void *element )
1986 +static void bitmapRelease( void *element )
1987  {
1988 -       bitmap_t *a=(bitmap_t *)element;
1989 +       bitmap_t *a = (bitmap_t *)element;
1990  
1991 -       pefree (a->bitmap, 1);
1992 -       pefree ((char *)element, 1);
1993 +       pefree(a->bitmap, 1);
1994 +       pefree((char *)element, 1);
1995  }
1996  
1997  /********************************************************************/
1998  /* tweencolor cache functions                                            */
1999  
2000 -static int
2001 -tweenColorTest (void *element, void *key)
2002 -{ 
2003 -    tweencolor_t *a=(tweencolor_t *)element;
2004 -    tweencolorkey_t *b=(tweencolorkey_t *)key;
2005 -    
2006 -    return (a->pixel == b->pixel    
2007 -         && a->bgcolor == b->bgcolor
2008 -         && a->fgcolor == b->fgcolor
2009 -         && a->im == b->im);
2010 -} 
2011 +static int tweenColorTest (void *element, void *key)
2012 +{
2013 +       tweencolor_t *a = (tweencolor_t *)element;
2014 +       tweencolorkey_t *b = (tweencolorkey_t *)key;
2015 +
2016 +       return (a->pixel == b->pixel && a->bgcolor == b->bgcolor && a->fgcolor == b->fgcolor && a->im == b->im);
2017 +}
2018  
2019 -static void *
2020 -tweenColorFetch (char **error, void *key)
2021 +static void * tweenColorFetch (char **error, void *key)
2022  {
2023 -    tweencolor_t *a;
2024 -    tweencolorkey_t *b=(tweencolorkey_t *)key;
2025 +       tweencolor_t *a;
2026 +       tweencolorkey_t *b = (tweencolorkey_t *)key;
2027         int pixel, npixel, bg, fg;
2028         gdImagePtr im;
2029 -   
2030 -    a = (tweencolor_t *)pemalloc(sizeof(tweencolor_t), 1);
2031 +
2032 +       a = (tweencolor_t *)pemalloc(sizeof(tweencolor_t), 1);
2033         pixel = a->pixel = b->pixel;
2034         bg = a->bgcolor = b->bgcolor;
2035         fg = a->fgcolor = b->fgcolor;
2036         im = b->im;
2037  
2038         /* if fg is specified by a negative color idx, then don't antialias */
2039 -       if (fg <0) {
2040 +       if (fg < 0) {
2041                 a->tweencolor = -fg;
2042         } else {
2043                 npixel = NUMCOLORS - pixel;
2044 @@ -635,20 +599,19 @@
2045                         (pixel * im->green[fg] + npixel * im->green[bg]) / NUMCOLORS,
2046                         (pixel * im->blue [fg] + npixel * im->blue [bg]) / NUMCOLORS);
2047         }
2048 -    *error = NULL;
2049 -    return (void *)a;
2050 -}   
2051 -        
2052 -static void
2053 -tweenColorRelease(void *element)
2054 -{   
2055 -    pefree((char *)element, 1);
2056 -}   
2057 +       *error = NULL;
2058 +       return (void *)a;
2059 +}
2060 +
2061 +static void tweenColorRelease(void *element)
2062 +{
2063 +       pefree((char *)element, 1);
2064 +}
2065  
2066  /********************************************************************/
2067  /* gdttfchar -  render one character onto a gd image                */
2068  
2069 -static int OneTime=0;
2070 +static int OneTime = 0;
2071  static gdCache_head_t   *tweenColorCache;
2072  
2073  char *
2074 @@ -656,38 +619,37 @@
2075         int x, int y,                                   /* string start pos in pixels */
2076         TT_F26Dot6 x1,  TT_F26Dot6 y1,  /* char start offset (*64) from x,y */
2077         TT_F26Dot6 *advance,
2078 -       TT_BBox **bbox, 
2079 +       TT_BBox **bbox,
2080         char **next)
2081  {
2082 -    int pc, ch, len;
2083 +       int pc, ch, len;
2084         int row, col;
2085 -       int x2, y2;     /* char start pos in pixels */ 
2086 +       int x2, y2;     /* char start pos in pixels */
2087         int x3, y3;     /* current pixel pos */
2088         unsigned char *pixel;
2089  
2090 -    glyph_t *glyph;
2091 -    glyphkey_t glyphkey;
2092 -    bitmapkey_t bitmapkey;
2093 +       glyph_t *glyph;
2094 +       glyphkey_t glyphkey;
2095 +       bitmapkey_t bitmapkey;
2096         tweencolor_t *tweencolor;
2097         tweencolorkey_t tweencolorkey;
2098  
2099         /****** set up tweenColorCache on first call ************/
2100 -       if (! OneTime) {
2101 -               tweenColorCache = gdCacheCreate(TWEENCOLORCACHESIZE,
2102 -                       tweenColorTest, tweenColorFetch, tweenColorRelease);
2103 +       if (!OneTime) {
2104 +               tweenColorCache = gdCacheCreate(TWEENCOLORCACHESIZE, tweenColorTest, tweenColorFetch, tweenColorRelease);
2105                 OneTime++;
2106         }
2107         /**************/
2108  
2109         if (font->have_char_map_Unicode) { /* use UTF-8 mapping from ASCII */
2110 -        len = gdTcl_UtfToUniChar(*next, &ch);
2111 -        *next += len;
2112 +               len = gdTcl_UtfToUniChar(*next, &ch);
2113 +               *next += len;
2114         } else {
2115 -       /*
2116 -        * Big 5 mapping:
2117 -        * use "JIS-8 half-width katakana" coding from 8-bit characters.  Ref:
2118 -        * ftp://ftp.ora.com/pub/examples/nutshell/ujip/doc/japan.inf-032092.sjs
2119 -        */
2120 +               /*
2121 +                * Big 5 mapping:
2122 +                * use "JIS-8 half-width katakana" coding from 8-bit characters.  Ref:
2123 +                * ftp://ftp.ora.com/pub/examples/nutshell/ujip/doc/japan.inf-032092.sjs
2124 +                */
2125                 ch = (**next) & 255;         /* don't extend sign */
2126                 (*next)++;
2127                 if (ch >= 161                /* first code of JIS-8 pair */
2128 @@ -700,18 +662,20 @@
2129         glyphkey.character = ch;
2130         glyphkey.hinting = 1;
2131         /* if fg is specified by a negative color idx, then don't antialias */
2132 -       glyphkey.gray_render = ((font->ptsize < MINANTIALIASPTSIZE) || (fg <0))?FALSE:TRUE;
2133 -    glyphkey.font = font;
2134 -    glyph = (glyph_t *)gdCacheGet(font->glyphCache, &glyphkey);
2135 -    if (! glyph)
2136 +       glyphkey.gray_render = ((font->ptsize < MINANTIALIASPTSIZE) || (fg < 0)) ? FALSE  : TRUE;
2137 +       glyphkey.font = font;
2138 +       glyph = (glyph_t *)gdCacheGet(font->glyphCache, &glyphkey);
2139 +       if (!glyph) {
2140                 return font->glyphCache->error;
2141 +       }
2142  
2143         *bbox = &glyph->metrics.bbox;
2144         *advance = glyph->metrics.advance;
2145  
2146         /* if null *im, or invalid color,  then assume user just wants brect */
2147 -       if (!im || fg > 255 || fg < -255)
2148 +       if (!im || fg > 255 || fg < -255) {
2149                 return (char *)NULL;
2150 +       }
2151  
2152         /* render (via cache) a bitmap for the current fractional offset */
2153         bitmapkey.xoffset = ((x1+32) & 63) - 32 - ((glyph->xmin+32) & -64);
2154 @@ -720,30 +684,32 @@
2155         gdCacheGet(glyph->bitmapCache, &bitmapkey);
2156  
2157         /* copy to gif, mapping colors */
2158 -      x2 = x + (((glyph->xmin+32) & -64) + ((x1+32) & -64)) / 64;
2159 -      y2 = y - (((glyph->ymin+32) & -64) + ((y1+32) & -64)) / 64;
2160 +       x2 = x + (((glyph->xmin+32) & -64) + ((x1+32) & -64)) / 64;
2161 +       y2 = y - (((glyph->ymin+32) & -64) + ((y1+32) & -64)) / 64;
2162         tweencolorkey.fgcolor = fg;
2163         tweencolorkey.im = im;
2164         for (row = 0; row < glyph->Bit.rows; row++) {
2165 -               if (glyph->gray_render)
2166 +               if (glyph->gray_render) {
2167                         pc = row * glyph->Bit.cols;
2168 -               else
2169 +               } else {
2170                         pc = row * glyph->Bit.cols * 8;
2171 +               }
2172                 y3 = y2 - row;
2173 -               if (y3 >= im->sy || y3 < 0) continue;
2174 +               if (y3 >= im->sy || y3 < 0) {
2175 +                       continue;
2176 +               }
2177                 for (col = 0; col < glyph->Bit.width; col++, pc++) {
2178                         if (glyph->gray_render) {
2179 -                               tweencolorkey.pixel = 
2180 -                                       *((unsigned char *)(glyph->Bit.bitmap) + pc);
2181 +                               tweencolorkey.pixel = *((unsigned char *)(glyph->Bit.bitmap) + pc);
2182                         } else {
2183 -                               tweencolorkey.pixel = 
2184 -                                       (((*((unsigned char *)(glyph->Bit.bitmap) + pc/8))
2185 -                                               <<(pc%8))&128)?4:0;
2186 +                               tweencolorkey.pixel = (((*((unsigned char *)(glyph->Bit.bitmap) + pc/8)) << (pc%8))&128)?4:0;
2187                         }
2188                         /* if not background */
2189                         if (tweencolorkey.pixel > 0) {
2190                                 x3 = x2 + col;
2191 -                               if (x3 >= im->sx || x3 < 0) continue;
2192 +                               if (x3 >= im->sx || x3 < 0) {
2193 +                                       continue;
2194 +                               }
2195  #if HAVE_LIBGD20
2196                                 if (im->trueColor) {
2197                                         pixel = &im->tpixels[y3][x3];
2198 @@ -757,8 +723,7 @@
2199  #endif
2200                                 }
2201                                 tweencolorkey.bgcolor = *pixel;
2202 -                               tweencolor = (tweencolor_t *)gdCacheGet(
2203 -                                       tweenColorCache, &tweencolorkey);
2204 +                               tweencolor = (tweencolor_t *)gdCacheGet(tweenColorCache, &tweencolorkey);
2205                                 *pixel = tweencolor->tweencolor;
2206                         }
2207                 }
2208 @@ -769,29 +734,26 @@
2209  /********************************************************************/
2210  /* gdttf -  render a utf8 string onto a gd image                                       */
2211  
2212 -char *
2213 -gdttf(gdImage *im, int *brect, int fg, char *fontname,
2214 -       double ptsize, double angle, int x, int y, char *str)
2215 +char * gdttf(gdImage *im, int *brect, int fg, char *fontname, double ptsize, double angle, int x, int y, char *str)
2216  {
2217 -       TT_F26Dot6 ur_x=0, ur_y=0, ll_x=0, ll_y=0;
2218 +       TT_F26Dot6 ur_x = 0, ur_y = 0, ll_x = 0, ll_y = 0;
2219         TT_F26Dot6 advance_x, advance_y, advance, x1, y1;
2220         TT_BBox *bbox;
2221         double sin_a, cos_a;
2222 -    int i=0, ch;
2223 +       int i=0, ch;
2224         font_t *font;
2225         fontkey_t fontkey;
2226         char *error, *next;
2227  
2228         /****** initialize font engine on first call ************/
2229 -    static gdCache_head_t      *fontCache;
2230 +       static gdCache_head_t   *fontCache;
2231         static TT_Engine        engine;
2232  
2233 -       if (! fontCache) {
2234 +       if (!fontCache) {
2235                 if (TT_Init_FreeType(&engine)) {
2236                         return "Failure to initialize font engine";
2237                 }
2238 -               fontCache = gdCacheCreate( FONTCACHESIZE,
2239 -                       fontTest, fontFetch, fontRelease);
2240 +               fontCache = gdCacheCreate(FONTCACHESIZE, fontTest, fontFetch, fontRelease);
2241         }
2242         /**************/
2243  
2244 @@ -801,15 +763,15 @@
2245         fontkey.angle = angle;
2246         fontkey.engine = &engine;
2247         font = (font_t *)gdCacheGet(fontCache, &fontkey);
2248 -       if (! font) {
2249 +       if (!font) {
2250                 return fontCache->error;
2251         }
2252         sin_a = font->sin_a;
2253         cos_a = font->cos_a;
2254         advance_x = advance_y = 0;
2255  
2256 -       next=str;
2257 -       while (*next) {   
2258 +       next = str;
2259 +       while (*next) {
2260                 ch = *next;
2261  
2262                 /* carriage returns */
2263 @@ -820,8 +782,8 @@
2264                 }
2265                 /* newlines */
2266                 if (ch == '\n') {
2267 -                      advance_y -= (TT_F26Dot6)(font->imetrics.y_ppem * LINESPACE * 64);
2268 -                      advance_y = (advance_y-32) & -64; /* round to next pixel row */
2269 +                       advance_y -= (TT_F26Dot6)(font->imetrics.y_ppem * LINESPACE * 64);
2270 +                       advance_y = (advance_y-32) & -64; /* round to next pixel row */
2271                         next++;
2272                         continue;
2273                 }
2274 @@ -829,20 +791,24 @@
2275                 x1 = (TT_F26Dot6)(advance_x * cos_a - advance_y * sin_a);
2276                 y1 = (TT_F26Dot6)(advance_x * sin_a + advance_y * cos_a);
2277  
2278 -               if ((error=gdttfchar(im, fg, font, x, y, x1, y1, &advance, &bbox, &next)))
2279 +               if ((error = gdttfchar(im, fg, font, x, y, x1, y1, &advance, &bbox, &next))) {
2280                         return error;
2281 +               }
2282  
2283 -               if (! i++) { /* if first character, init BB corner values */
2284 +               if (!i++) { /* if first character, init BB corner values */
2285                         ll_x = bbox->xMin;
2286                         ll_y = bbox->yMin;
2287                         ur_x = bbox->xMax;
2288                         ur_y = bbox->yMax;
2289 -               }
2290 -               else {
2291 -                       if (! advance_x) ll_x = MIN(bbox->xMin, ll_x);
2292 +               } else {
2293 +                       if (!advance_x) {
2294 +                               ll_x = MIN(bbox->xMin, ll_x);
2295 +                       }
2296                         ll_y = MIN(advance_y + bbox->yMin, ll_y);
2297                         ur_x = MAX(advance_x + bbox->xMax, ur_x);
2298 -                       if (! advance_y) ur_y = MAX(bbox->yMax, ur_y);
2299 +                       if (!advance_y) {
2300 +                               ur_y = MAX(bbox->yMax, ur_y);
2301 +                       }
2302                 }
2303                 advance_x += advance;
2304         }
2305 @@ -859,7 +825,7 @@
2306  
2307         /* scale, round and offset brect */
2308         i = 0;
2309 -       while (i<8) {
2310 +       while (i < 8) {
2311                 brect[i] = x + (brect[i] + 32) / 64;
2312                 i++;
2313                 brect[i] = y - (brect[i] + 32) / 64;
2314 @@ -868,7 +834,7 @@
2315  
2316      return (char *)NULL;
2317  }
2318 -   
2319 +
2320  #endif /* HAVE_LIBTTF */
2321  
2322  /*
2323 diff -urN php-4.4.8.org/ext/gd/libgd/gd_arc_f_buggy.c php-4.4.8/ext/gd/libgd/gd_arc_f_buggy.c
2324 --- php-4.4.8.org/ext/gd/libgd/gd_arc_f_buggy.c 2003-03-05 17:04:20.000000000 +0100
2325 +++ php-4.4.8/ext/gd/libgd/gd_arc_f_buggy.c     2005-08-18 14:54:43.000000000 +0200
2326 @@ -1,6 +1,6 @@
2327  /* This is potentially great stuff, but fails against the test
2328 -   program at the end. This would probably be much more 
2329 -   efficent than the implementation currently in gd.c if the 
2330 +   program at the end. This would probably be much more
2331 +   efficent than the implementation currently in gd.c if the
2332     errors in the output were corrected. TBB */
2333  
2334  #if 0
2335 @@ -698,7 +698,7 @@
2336  #define WIDTH  500
2337  #define HEIGHT 300
2338  
2339 -int 
2340 +int
2341  main (int argc, char *argv[])
2342  {
2343    gdImagePtr im = gdImageCreate (WIDTH, HEIGHT);
2344 @@ -726,12 +726,12 @@
2345    out = fopen ("test/arctest.png", "wb");
2346    if (!out)
2347      {
2348 -      php_gd_error("Can't create test/arctest.png\n");
2349 +      php_gd_error("Can't create test/arctest.png");
2350        exit (1);
2351      }
2352    gdImagePng (im, out);
2353    fclose (out);
2354 -  php_gd_error("Test image written to test/arctest.png\n");
2355 +  php_gd_error("Test image written to test/arctest.png");
2356    /* Destroy it */
2357    gdImageDestroy (im);
2358  
2359 diff -urN php-4.4.8.org/ext/gd/libgd/gd.c php-4.4.8/ext/gd/libgd/gd.c
2360 --- php-4.4.8.org/ext/gd/libgd/gd.c     2007-10-20 17:29:04.000000000 +0200
2361 +++ php-4.4.8/ext/gd/libgd/gd.c 2007-11-05 00:56:00.000000000 +0100
2362 @@ -1,4 +1,4 @@
2363 -#include <stdio.h>
2364 +
2365  #include <math.h>
2366  #include <string.h>
2367  #include <stdlib.h>
2368 @@ -90,18 +90,16 @@
2369  static void gdImageBrushApply(gdImagePtr im, int x, int y);
2370  static void gdImageTileApply(gdImagePtr im, int x, int y);
2371  static void gdImageAntiAliasedApply(gdImagePtr im, int x, int y);
2372 -static int gdFullAlphaBlend(int dst, int src);
2373  static int gdLayerOverlay(int dst, int src);
2374 -static int gdAlphaBlendColor(int b1, int b2, int a1, int a2);
2375  static int gdAlphaOverlayColor(int src, int dst, int max);
2376  int gdImageGetTrueColorPixel(gdImagePtr im, int x, int y);
2377  
2378 -void php_gd_error_ex(int type, const char *format, ...) 
2379 +void php_gd_error_ex(int type, const char *format, ...)
2380  {
2381         va_list args;
2382 -       
2383 +
2384         TSRMLS_FETCH();
2385 -       
2386 +
2387         va_start(args, format);
2388         php_verror(NULL, "", type, format, args TSRMLS_CC);
2389         va_end(args);
2390 @@ -110,9 +108,9 @@
2391  void php_gd_error(const char *format, ...)
2392  {
2393         va_list args;
2394 -       
2395 +
2396         TSRMLS_FETCH();
2397 -       
2398 +
2399         va_start(args, format);
2400         php_verror(NULL, "", E_WARNING, format, args TSRMLS_CC);
2401         va_end(args);
2402 @@ -122,11 +120,20 @@
2403  {
2404         int i;
2405         gdImagePtr im;
2406 -       im = (gdImage *) gdMalloc(sizeof(gdImage));
2407 -       memset(im, 0, sizeof(gdImage));
2408 +
2409 +       if (overflow2(sx, sy)) {
2410 +               return NULL;
2411 +       }
2412 +
2413 +       if (overflow2(sizeof(unsigned char *), sy)) {
2414 +               return NULL;
2415 +       }
2416 +
2417 +       im = (gdImage *) gdCalloc(1, sizeof(gdImage));
2418 +
2419         /* Row-major ever since gd 1.3 */
2420 -       im->pixels = (unsigned char **) safe_emalloc(sizeof(unsigned char *), sy, 0);
2421 -       im->AA_opacity = (unsigned char **) safe_emalloc(sizeof(unsigned char *), sy, 0);
2422 +       im->pixels = (unsigned char **) gdMalloc(sizeof(unsigned char *) * sy);
2423 +       im->AA_opacity = (unsigned char **) gdMalloc(sizeof(unsigned char *) * sy);
2424         im->polyInts = 0;
2425         im->polyAllocated = 0;
2426         im->brush = 0;
2427 @@ -164,10 +171,23 @@
2428  {
2429         int i;
2430         gdImagePtr im;
2431 +
2432 +       if (overflow2(sx, sy)) {
2433 +               return NULL;
2434 +       }
2435 +
2436 +       if (overflow2(sizeof(unsigned char *), sy)) {
2437 +               return NULL;
2438 +       }
2439 +       
2440 +       if (overflow2(sizeof(int), sx)) {
2441 +               return NULL;
2442 +       }
2443 +
2444         im = (gdImage *) gdMalloc(sizeof(gdImage));
2445         memset(im, 0, sizeof(gdImage));
2446 -       im->tpixels = (int **) safe_emalloc(sizeof(int *), sy, 0);
2447 -       im->AA_opacity = (unsigned char **) safe_emalloc(sizeof(unsigned char *), sy, 0);
2448 +       im->tpixels = (int **) gdMalloc(sizeof(int *) * sy);
2449 +       im->AA_opacity = (unsigned char **) gdMalloc(sizeof(unsigned char *) * sy);
2450         im->polyInts = 0;
2451         im->polyAllocated = 0;
2452         im->brush = 0;
2453 @@ -267,8 +287,8 @@
2454  }
2455  
2456  /* This code is taken from http://www.acm.org/jgt/papers/SmithLyons96/hwb_rgb.html, an article
2457 - * on colour conversion to/from RBG and HWB colour systems. 
2458 - * It has been modified to return the converted value as a * parameter. 
2459 + * on colour conversion to/from RBG and HWB colour systems.
2460 + * It has been modified to return the converted value as a * parameter.
2461   */
2462  
2463  #define RETURN_HWB(h, w, b) {HWB->H = h; HWB->W = w; HWB->B = b; return HWB;}
2464 @@ -287,8 +307,8 @@
2465  
2466  
2467  /*
2468 - * Theoretically, hue 0 (pure red) is identical to hue 6 in these transforms. Pure 
2469 - * red always maps to 6 in this implementation. Therefore UNDEFINED can be 
2470 + * Theoretically, hue 0 (pure red) is identical to hue 6 in these transforms. Pure
2471 + * red always maps to 6 in this implementation. Therefore UNDEFINED can be
2472   * defined as 0 in situations where only unsigned numbers are desired.
2473   */
2474  typedef struct
2475 @@ -305,8 +325,8 @@
2476  static HWBType * RGB_to_HWB (RGBType RGB, HWBType * HWB)
2477  {
2478         /*
2479 -        * RGB are each on [0, 1]. W and B are returned on [0, 1] and H is  
2480 -        * returned on [0, 6]. Exception: H is returned UNDEFINED if W == 1 - B.  
2481 +        * RGB are each on [0, 1]. W and B are returned on [0, 1] and H is
2482 +        * returned on [0, 6]. Exception: H is returned UNDEFINED if W == 1 - B.
2483          */
2484  
2485         float R = RGB.R, G = RGB.G, B = RGB.B, w, v, b, f;
2486 @@ -320,7 +340,7 @@
2487         }
2488         f = (R == w) ? G - B : ((G == w) ? B - R : R - G);
2489         i = (R == w) ? 3 : ((G == w) ? 5 : 1);
2490 -       
2491 +
2492         RETURN_HWB(i - f / (v - w), w, b);
2493  }
2494  
2495 @@ -363,9 +383,9 @@
2496   */
2497  static RGBType * HWB_to_RGB (HWBType HWB, RGBType * RGB)
2498  {
2499 -       /* 
2500 -        * H is given on [0, 6] or UNDEFINED. W and B are given on [0, 1].  
2501 -        * RGB are each returned on [0, 1]. 
2502 +       /*
2503 +        * H is given on [0, 6] or UNDEFINED. W and B are given on [0, 1].
2504 +        * RGB are each returned on [0, 1].
2505          */
2506  
2507         float h = HWB.H, w = HWB.W, b = HWB.B, v, n, f;
2508 @@ -478,7 +498,7 @@
2509         im->blue[ct] = b;
2510         im->alpha[ct] = a;
2511         im->open[ct] = 0;
2512 -       
2513 +
2514         return ct;
2515  }
2516  
2517 @@ -629,7 +649,7 @@
2518   * Given the end points of a line, and a bounding rectangle (which we
2519   * know to be from (0,0) to (SX,SY)), adjust the endpoints to be on
2520   * the edges of the rectangle if the line should be drawn at all,
2521 - * otherwise return a failure code 
2522 + * otherwise return a failure code
2523   */
2524  
2525  /* this does "one-dimensional" clipping: note that the second time it
2526 @@ -637,7 +657,7 @@
2527   *  - the comments ignore this (if you can understand it when it's
2528   *  looking at the X parameters, it should become clear what happens on
2529   *  the second call!)  The code is simplified from that in the article,
2530 - *  as we know that gd images always start at (0,0) 
2531 + *  as we know that gd images always start at (0,0)
2532   */
2533  
2534  static int clip_1d(int *x0, int *y0, int *x1, int *y1, int maxdim) {
2535 @@ -664,7 +684,7 @@
2536                 }
2537                 m = (*y1 - *y0)/(double)(*x1 - *x0);  /* calculate the slope of the line */
2538                 *y0 += (int)(m * (maxdim - *x0)); /* adjust so point is on the right boundary */
2539 -               *x0 = maxdim;                  
2540 +               *x0 = maxdim;
2541                 /* now, perhaps, adjust the end of the line */
2542                 if (*x1 < 0) {
2543                         *y1 -= (int)(m * *x1);
2544 @@ -737,7 +757,7 @@
2545                                                         im->tpixels[y][x] = gdAlphaBlend(im->tpixels[y][x], color);
2546                                                         break;
2547                                                 case gdEffectNormal:
2548 -                                                       im->tpixels[y][x] = gdFullAlphaBlend(im->tpixels[y][x], color);
2549 +                                                       im->tpixels[y][x] = gdAlphaBlend(im->tpixels[y][x], color);
2550                                                         break;
2551                                                 case gdEffectOverlay :
2552                                                         im->tpixels[y][x] = gdLayerOverlay(im->tpixels[y][x], color);
2553 @@ -756,7 +776,7 @@
2554         int p = gdImageGetPixel(im, x, y);
2555  
2556         if (!im->trueColor)  {
2557 -               return gdTrueColorAlpha(im->red[p], im->green[p], im->blue[p], (im->transparent == p) ? gdAlphaTransparent : gdAlphaOpaque);
2558 +               return gdTrueColorAlpha(im->red[p], im->green[p], im->blue[p], (im->transparent == p) ? gdAlphaTransparent : im->alpha[p]);
2559         } else {
2560                 return p;
2561         }
2562 @@ -780,7 +800,7 @@
2563         x1 = x - hx;
2564         x2 = x1 + gdImageSX(im->brush);
2565         srcy = 0;
2566 -       
2567 +
2568         if (im->trueColor) {
2569                 if (im->brush->trueColor) {
2570                         for (ly = y1; ly < y2; ly++) {
2571 @@ -823,8 +843,8 @@
2572                                 if (p != gdImageGetTransparent(im->brush)) {
2573                                         /* Truecolor brush. Very slow on a palette destination. */
2574                                         if (im->brush->trueColor) {
2575 -                                               gdImageSetPixel(im, lx, ly, gdImageColorResolveAlpha(im, gdTrueColorGetRed(p), 
2576 -                                                                                                        gdTrueColorGetGreen(p), 
2577 +                                               gdImageSetPixel(im, lx, ly, gdImageColorResolveAlpha(im, gdTrueColorGetRed(p),
2578 +                                                                                                        gdTrueColorGetGreen(p),
2579                                                                                                          gdTrueColorGetBlue(p),
2580                                                                                                          gdTrueColorGetAlpha(p)));
2581                                         } else {
2582 @@ -849,7 +869,9 @@
2583         srcy = y % gdImageSY(im->tile);
2584         if (im->trueColor) {
2585                 p = gdImageGetTrueColorPixel(im->tile, srcx, srcy);
2586 -               gdImageSetPixel(im, x, y, p);
2587 +               if (p != gdImageGetTransparent (im->tile)) {
2588 +                       gdImageSetPixel(im, x, y, p);
2589 +               }
2590         } else {
2591                 p = gdImageGetPixel(im->tile, srcx, srcy);
2592                 /* Allow for transparency */
2593 @@ -903,8 +925,8 @@
2594         float p_dist, p_alpha;
2595         unsigned char opacity;
2596  
2597 -       /* 
2598 -        * Find the perpendicular distance from point C (px, py) to the line 
2599 +       /*
2600 +        * Find the perpendicular distance from point C (px, py) to the line
2601          * segment AB that is being drawn.  (Adapted from an algorithm from the
2602          * comp.graphics.algorithms FAQ.)
2603          */
2604 @@ -918,7 +940,7 @@
2605         int By_Cy = im->AAL_y2 - py;
2606  
2607         /* 2.0.13: bounds check! AA_opacity is just as capable of
2608 -        * overflowing as the main pixel array. Arne Jorgensen. 
2609 +        * overflowing as the main pixel array. Arne Jorgensen.
2610          * 2.0.14: typo fixed. 2.0.15: moved down below declarations
2611          * to satisfy non-C++ compilers.
2612          */
2613 @@ -931,12 +953,12 @@
2614         LBC_2 = (Bx_Cx * Bx_Cx) + (By_Cy * By_Cy);
2615  
2616         if (((im->AAL_LAB_2 + LAC_2) >= LBC_2) && ((im->AAL_LAB_2 + LBC_2) >= LAC_2)) {
2617 -               /* The two angles are acute.  The point lies inside the portion of the 
2618 +               /* The two angles are acute.  The point lies inside the portion of the
2619                  * plane spanned by the line segment.
2620                  */
2621                 p_dist = fabs ((float) ((Ay_Cy * im->AAL_Bx_Ax) - (Ax_Cx * im->AAL_By_Ay)) / im->AAL_LAB);
2622         } else {
2623 -               /* The point is past an end of the line segment.  It's length from the 
2624 +               /* The point is past an end of the line segment.  It's length from the
2625                  * segment is the shorter of the lengths from the endpoints, but call
2626                  * the distance -1, so as not to compute the alpha nor draw the pixel.
2627                  */
2628 @@ -1017,6 +1039,43 @@
2629         }
2630  }
2631  
2632 +static void gdImageHLine(gdImagePtr im, int y, int x1, int x2, int col)
2633 +{
2634 +       if (im->thick > 1) {
2635 +               int thickhalf = im->thick >> 1;
2636 +               gdImageFilledRectangle(im, x1, y - thickhalf, x2, y + im->thick - thickhalf - 1, col);
2637 +       } else {
2638 +               if (x2 < x1) {
2639 +                       int t = x2;
2640 +                       x2 = x1;
2641 +                       x1 = t;
2642 +               }
2643 +
2644 +               for (;x1 <= x2; x1++) {
2645 +                       gdImageSetPixel(im, x1, y, col);
2646 +               }
2647 +       }
2648 +       return;
2649 +}
2650 +
2651 +static void gdImageVLine(gdImagePtr im, int x, int y1, int y2, int col)
2652 +{
2653 +       if (im->thick > 1) {
2654 +               int thickhalf = im->thick >> 1;
2655 +               gdImageFilledRectangle(im, x - thickhalf, y1, x + im->thick - thickhalf - 1, y2, col);
2656 +       } else {
2657 +               if (y2 < y1) {
2658 +                       int t = y1;
2659 +                       y1 = y2;
2660 +                       y2 = t;
2661 +               }
2662 +
2663 +               for (;y1 <= y2; y1++) {
2664 +                       gdImageSetPixel(im, x, y1, col);
2665 +               }
2666 +       }
2667 +       return;
2668 +}
2669  
2670  /* Bresenham as presented in Foley & Van Dam */
2671  void gdImageLine (gdImagePtr im, int x1, int y1, int x2, int y2, int color)
2672 @@ -1026,39 +1085,47 @@
2673         int w, wstart;
2674         int thick = im->thick;
2675  
2676 +       if (color == gdAntiAliased)
2677 +       {
2678 +               /* 
2679 +                  gdAntiAliased passed as color: use the much faster, much cheaper
2680 +                  and equally attractive gdImageAALine implementation. That
2681 +                  clips too, so don't clip twice.
2682 +                  */
2683 +               gdImageAALine(im, x1, y1, x2, y2, im->AA_color); 
2684 +               return;
2685 +       }
2686 +
2687         /* 2.0.10: Nick Atty: clip to edges of drawing rectangle, return if no points need to be drawn */
2688         if (!clip_1d(&x1,&y1,&x2,&y2,gdImageSX(im)) || !clip_1d(&y1,&x1,&y2,&x2,gdImageSY(im))) {
2689                 return;
2690         }
2691  
2692 -       /* gdAntiAliased passed as color: set anti-aliased line (AAL) global vars. */
2693 -       if (color == gdAntiAliased) {
2694 -               im->AAL_x1 = x1;
2695 -               im->AAL_y1 = y1;
2696 -               im->AAL_x2 = x2;
2697 -               im->AAL_y2 = y2;
2698 -
2699 -               /* Compute what we can for point-to-line distance calculation later. */
2700 -               im->AAL_Bx_Ax = x2 - x1;
2701 -               im->AAL_By_Ay = y2 - y1;
2702 -               im->AAL_LAB_2 = (im->AAL_Bx_Ax * im->AAL_Bx_Ax) + (im->AAL_By_Ay * im->AAL_By_Ay);
2703 -               im->AAL_LAB = sqrt (im->AAL_LAB_2);
2704 +       dx = abs (x2 - x1);
2705 +       dy = abs (y2 - y1);
2706  
2707 -               /* For AA, we must draw pixels outside the width of the line.  Keep in
2708 -                * mind that this will be curtailed by cos/sin of theta later. 
2709 -                */
2710 -               thick += 4;
2711 +       if (dx == 0) {
2712 +               gdImageVLine(im, x1, y1, y2, color);
2713 +               return;
2714 +       } else if (dy == 0) {
2715 +               gdImageHLine(im, y1, x1, x2, color);
2716 +               return;
2717         }
2718 -       
2719 -       dx = abs(x2 - x1);
2720 -       dy = abs(y2 - y1);
2721 +
2722         if (dy <= dx) {
2723                 /* More-or-less horizontal. use wid for vertical stroke */
2724                 /* Doug Claar: watch out for NaN in atan2 (2.0.5) */
2725                 if ((dx == 0) && (dy == 0)) {
2726                         wid = 1;
2727                 } else {
2728 -                       wid = (int)(thick * cos (atan2 (dy, dx)));
2729 +                       /* 2.0.12: Michael Schwartz: divide rather than multiply;
2730 +TBB: but watch out for /0! */
2731 +                       double ac = cos (atan2 (dy, dx));
2732 +                       if (ac != 0) {
2733 +                               wid = thick / ac;
2734 +                       } else {
2735 +                               wid = 1;
2736 +                       }
2737                         if (wid == 0) {
2738                                 wid = 1;
2739                         }
2740 @@ -1115,16 +1182,17 @@
2741                 }
2742         } else {
2743                 /* More-or-less vertical. use wid for horizontal stroke */
2744 -               /* 2.0.12: Michael Schwartz: divide rather than multiply; 
2745 -                  TBB: but watch out for /0! */
2746 -               double as = sin(atan2(dy, dx));
2747 +               /* 2.0.12: Michael Schwartz: divide rather than multiply;
2748 +           TBB: but watch out for /0! */
2749 +               double as = sin (atan2 (dy, dx));
2750                 if (as != 0) {
2751 -                       if (!(wid = thick / as)) {
2752 -                               wid = 1;
2753 -                       }
2754 +                       wid = thick / as;
2755                 } else {
2756                         wid = 1;
2757                 }
2758 +               if (wid == 0) {
2759 +                       wid = 1;
2760 +               }
2761  
2762                 d = 2 * dx - dy;
2763                 incr1 = 2 * dx;
2764 @@ -1177,11 +1245,6 @@
2765                         }
2766                 }
2767         }
2768 -
2769 -       /* If this is the only line we are drawing, go ahead and blend. */
2770 -       if (color == gdAntiAliased && !im->AA_polygon) {
2771 -               gdImageAABlend(im);
2772 -       }
2773  }
2774  
2775  
2776 @@ -1207,7 +1270,7 @@
2777         BLEND_COLOR(t, dg, g, dg);
2778         BLEND_COLOR(t, db, b, db);
2779         im->tpixels[y][x]=gdTrueColorAlpha(dr, dg, db,  gdAlphaOpaque);
2780 -}  
2781 +}
2782  
2783  /*
2784   * Added on 2003/12 by Pierre-Alain Joye (pajoye@pearfr.org)
2785 @@ -1586,9 +1649,9 @@
2786  
2787  /* s and e are integers modulo 360 (degrees), with 0 degrees
2788     being the rightmost extreme and degrees changing clockwise.
2789 -   cx and cy are the center in pixels; w and h are the horizontal 
2790 +   cx and cy are the center in pixels; w and h are the horizontal
2791     and vertical diameter in pixels. Nice interface, but slow.
2792 -   See gd_arc_f_buggy.c for a better version that doesn't 
2793 +   See gd_arc_f_buggy.c for a better version that doesn't
2794     seem to be bug-free yet. */
2795  
2796  void gdImageArc (gdImagePtr im, int cx, int cy, int w, int h, int s, int e, int color)
2797 @@ -1607,29 +1670,30 @@
2798         int lx = 0, ly = 0;
2799         int fx = 0, fy = 0;
2800  
2801 -       if ((s % 360)  == (e % 360)) {
2802 +
2803 +    if ((s % 360)  == (e % 360)) {
2804                 s = 0; e = 360;
2805 -       } else {
2806 -               if (s > 360) {
2807 -                       s = s % 360;
2808 -               }
2809 +    } else {
2810 +        if (s > 360) {
2811 +            s = s % 360;
2812 +        }
2813  
2814 -               if (e > 360) {
2815 -                       e = e % 360;
2816 -               }
2817 +        if (e > 360) {
2818 +            e = e % 360;
2819 +        }
2820  
2821 -               while (s < 0) {
2822 -                       s += 360;
2823 -               }
2824 +        while (s < 0) {
2825 +            s += 360;
2826 +        }
2827  
2828 -               while (e < s) {
2829 -                       e += 360;
2830 -               }
2831 +        while (e < s) {
2832 +            e += 360;
2833 +        }
2834  
2835 -               if (s == e) {
2836 +        if (s == e) {
2837                         s = 0; e = 360;
2838 -               }
2839 -       }
2840 +        }
2841 +    }
2842  
2843         for (i = s; i <= e; i++) {
2844                 int x, y;
2845 @@ -1787,17 +1851,15 @@
2846         int lastBorder;
2847         /* Seek left */
2848         int leftLimit = -1, rightLimit;
2849 -       int i, restoreAlphaBleding=0;
2850 +       int i, restoreAlphaBlending = 0;
2851  
2852         if (border < 0) {
2853                 /* Refuse to fill to a non-solid border */
2854                 return;
2855         }
2856  
2857 -       if (im->alphaBlendingFlag) {
2858 -               restoreAlphaBleding = 1;
2859 -               im->alphaBlendingFlag = 0;
2860 -       }
2861 +       restoreAlphaBlending = im->alphaBlendingFlag;
2862 +       im->alphaBlendingFlag = 0;
2863  
2864         if (x >= im->sx) {
2865                 x = im->sx - 1;
2866 @@ -1814,9 +1876,7 @@
2867                 leftLimit = i;
2868         }
2869         if (leftLimit == -1) {
2870 -               if (restoreAlphaBleding) {
2871 -                       im->alphaBlendingFlag = 1;
2872 -               }
2873 +               im->alphaBlendingFlag = restoreAlphaBlending;
2874                 return;
2875         }
2876         /* Seek right */
2877 @@ -1844,6 +1904,7 @@
2878                         }
2879                 }
2880         }
2881 +
2882         /* Below */
2883         if (y < ((im->sy) - 1)) {
2884                 lastBorder = 1;
2885 @@ -1860,12 +1921,9 @@
2886                         }
2887                 }
2888         }
2889 -       if (restoreAlphaBleding) {
2890 -               im->alphaBlendingFlag = 1;
2891 -       }
2892 +       im->alphaBlendingFlag = restoreAlphaBlending;
2893  }
2894  
2895 -
2896  /*
2897   * set the pixel at (x,y) and its 4-connected neighbors
2898   * with the same pixel value to the new pixel value nc (new color).
2899 @@ -1888,25 +1946,31 @@
2900  #define FILL_POP(Y, XL, XR, DY) \
2901      {sp--; Y = sp->y+(DY = sp->dy); XL = sp->xl; XR = sp->xr;}
2902  
2903 -void _gdImageFillTiled(gdImagePtr im, int x, int y, int nc);
2904 +static void _gdImageFillTiled(gdImagePtr im, int x, int y, int nc);
2905  
2906  void gdImageFill(gdImagePtr im, int x, int y, int nc)
2907  {
2908         int l, x1, x2, dy;
2909         int oc;   /* old pixel value */
2910         int wx2,wy2;
2911 +
2912         int alphablending_bak;
2913 +
2914         /* stack of filled segments */
2915         /* struct seg stack[FILL_MAX],*sp = stack;; */
2916 -       struct seg *stack;
2917 +       struct seg *stack = NULL;
2918         struct seg *sp;
2919  
2920 +       if (!im->trueColor && nc > (im->colorsTotal -1)) {
2921 +               return;
2922 +       }
2923 +
2924         alphablending_bak = im->alphaBlendingFlag;      
2925         im->alphaBlendingFlag = 0;
2926  
2927         if (nc==gdTiled){
2928                 _gdImageFillTiled(im,x,y,nc);
2929 -               im->alphaBlendingFlag = alphablending_bak;      
2930 +               im->alphaBlendingFlag = alphablending_bak;
2931                 return;
2932         }
2933  
2934 @@ -1916,8 +1980,31 @@
2935                 im->alphaBlendingFlag = alphablending_bak;      
2936                 return;
2937         }
2938
2939 -       stack = (struct seg *)emalloc(sizeof(struct seg) * ((int)(im->sy*im->sx)/4)+1);
2940 +
2941 +       /* Do not use the 4 neighbors implementation with
2942 +   * small images
2943 +   */
2944 +       if (im->sx < 4) {
2945 +               int ix = x, iy = y, c;
2946 +               do {
2947 +                       c = gdImageGetPixel(im, ix, iy);
2948 +                       if (c != oc) {
2949 +                               goto done;
2950 +                       }
2951 +                       gdImageSetPixel(im, ix, iy, nc);
2952 +               } while(ix++ < (im->sx -1));
2953 +               ix = x; iy = y + 1;
2954 +               do {
2955 +                       c = gdImageGetPixel(im, ix, iy);
2956 +                       if (c != oc) {
2957 +                               goto done;
2958 +                       }
2959 +                       gdImageSetPixel(im, ix, iy, nc);
2960 +               } while(ix++ < (im->sx -1));
2961 +               goto done;
2962 +       }
2963 +
2964 +       stack = (struct seg *)safe_emalloc(sizeof(struct seg), ((int)(im->sy*im->sx)/4), 1);
2965         sp = stack;
2966  
2967         /* required! */
2968 @@ -1954,22 +2041,25 @@
2969                         l = x;
2970                 } while (x<=x2);
2971         }
2972 +
2973         efree(stack);
2974 +
2975 +done:
2976         im->alphaBlendingFlag = alphablending_bak;      
2977  }
2978  
2979 -void _gdImageFillTiled(gdImagePtr im, int x, int y, int nc)
2980 +static void _gdImageFillTiled(gdImagePtr im, int x, int y, int nc)
2981  {
2982 -       int i,l, x1, x2, dy;
2983 +       int i, l, x1, x2, dy;
2984         int oc;   /* old pixel value */
2985         int tiled;
2986         int wx2,wy2;
2987         /* stack of filled segments */
2988         struct seg *stack;
2989         struct seg *sp;
2990 +       char **pts;
2991  
2992 -       int **pts;
2993 -       if(!im->tile){
2994 +       if (!im->tile) {
2995                 return;
2996         }
2997  
2998 @@ -1977,30 +2067,26 @@
2999         tiled = nc==gdTiled;
3000  
3001         nc =  gdImageTileGet(im,x,y);
3002 -       pts = (int **) ecalloc(sizeof(int *) * im->sy, sizeof(int));
3003  
3004 -       for (i=0; i<im->sy;i++) {
3005 -               pts[i] = (int *) ecalloc(im->sx, sizeof(int));
3006 +       pts = (char **) ecalloc(im->sy + 1, sizeof(char *));
3007 +       for (i = 0; i < im->sy + 1; i++) {
3008 +               pts[i] = (char *) ecalloc(im->sx + 1, sizeof(char));
3009         }
3010  
3011 -       stack = (struct seg *)emalloc(sizeof(struct seg) * ((int)(im->sy*im->sx)/4)+1);
3012 +       stack = (struct seg *)safe_emalloc(sizeof(struct seg), ((int)(im->sy*im->sx)/4), 1);
3013         sp = stack;
3014  
3015         oc = gdImageGetPixel(im, x, y);
3016  
3017 -       /* required! */
3018 +/* required! */
3019         FILL_PUSH(y,x,x,1);
3020         /* seed segment (popped 1st) */
3021         FILL_PUSH(y+1, x, x, -1);
3022         while (sp>stack) {
3023                 FILL_POP(y, x1, x2, dy);
3024                 for (x=x1; x>=0 && (!pts[y][x] && gdImageGetPixel(im,x,y)==oc); x--) {
3025 -                       if (pts[y][x]){
3026 -                               /* we should never be here */
3027 -                               break;
3028 -                       }
3029                         nc = gdImageTileGet(im,x,y);
3030 -                       pts[y][x]=1;
3031 +                       pts[y][x] = 1;
3032                         gdImageSetPixel(im,x, y, nc);
3033                 }
3034                 if (x>=x1) {
3035 @@ -2014,13 +2100,9 @@
3036                 }
3037                 x = x1+1;
3038                 do {
3039 -                       for (; x<=wx2 && (!pts[y][x] && gdImageGetPixel(im,x, y)==oc) ; x++) {
3040 -                               if (pts[y][x]){
3041 -                                       /* we should never be here */
3042 -                                       break;
3043 -                               }
3044 +                       for(; x<wx2 && (!pts[y][x] && gdImageGetPixel(im,x, y)==oc); x++) {
3045                                 nc = gdImageTileGet(im,x,y);
3046 -                               pts[y][x]=1;
3047 +                               pts[y][x] = 1;
3048                                 gdImageSetPixel(im, x, y, nc);
3049                         }
3050                         FILL_PUSH(y, l, x-1, dy);
3051 @@ -2028,13 +2110,15 @@
3052                         if (x>x2+1) {
3053                                 FILL_PUSH(y, x2+1, x-1, -dy);
3054                         }
3055 -skip:                  for (x++; x<=x2 && (pts[y][x] || gdImageGetPixel(im,x, y)!=oc); x++);
3056 +skip:          for(x++; x<=x2 && (pts[y][x] || gdImageGetPixel(im,x, y)!=oc); x++);
3057                         l = x;
3058                 } while (x<=x2);
3059         }
3060 -       for (i=0; i<im->sy;i++) {
3061 +
3062 +       for(i = 0; i < im->sy + 1; i++) {
3063                 efree(pts[i]);
3064         }
3065 +
3066         efree(pts);
3067         efree(stack);
3068  }
3069 @@ -2048,6 +2132,11 @@
3070         int half1 = 1;
3071         int t;
3072  
3073 +       if (x1 == x2 && y1 == y2 && thick == 1) {
3074 +               gdImageSetPixel(im, x1, y1, color);
3075 +               return;
3076 +       }
3077 +
3078         if (y2 < y1) {
3079                 t=y1;
3080                 y1 = y2;
3081 @@ -2110,16 +2199,15 @@
3082                 gdImageLine(im, x1v, y1v, x1v, y2v, color);
3083                 gdImageLine(im, x2v, y1v, x2v, y2v, color);
3084         }
3085 -
3086  }
3087  
3088  void gdImageFilledRectangle (gdImagePtr im, int x1, int y1, int x2, int y2, int color)
3089  {
3090         int x, y;
3091 -  
3092 +
3093         /* Nick Atty: limit the points at the edge.  Note that this also
3094          * nicely kills any plotting for rectangles completely outside the
3095 -        * window as it makes the tests in the for loops fail 
3096 +        * window as it makes the tests in the for loops fail
3097          */
3098         if (x1 < 0) {
3099                 x1 = 0;
3100 @@ -2133,15 +2221,15 @@
3101         if (y1 > gdImageSY(im)) {
3102                 y1 = gdImageSY(im);
3103         }
3104 -       if (y2 < y1) {
3105 -               int t;
3106 -               t=y1;
3107 -               y1 = y2;
3108 -               y2 = t;
3109 -
3110 -               t = x1;
3111 +       if (x1 > x2) {
3112 +               x = x1;
3113                 x1 = x2;
3114 -               x2 = t;
3115 +               x2 = x;
3116 +       }
3117 +       if (y1 > y2) {
3118 +               y = y1;
3119 +               y1 = y2;
3120 +               y2 = y;
3121         }
3122  
3123         for (y = y1; (y <= y2); y++) {
3124 @@ -2162,9 +2250,9 @@
3125         if (dst->trueColor) {
3126                 /* 2.0: much easier when the destination is truecolor. */
3127                 /* 2.0.10: needs a transparent-index check that is still valid if
3128 -                * the source is not truecolor. Thanks to Frank Warmerdam. 
3129 +                * the source is not truecolor. Thanks to Frank Warmerdam.
3130                  */
3131 -               
3132 +
3133                 if (src->trueColor) {
3134                         for (y = 0; (y < h); y++) {
3135                                 for (x = 0; (x < w); x++) {
3136 @@ -2178,7 +2266,7 @@
3137                                 for (x = 0; (x < w); x++) {
3138                                         int c = gdImageGetPixel (src, srcX + x, srcY + y);
3139                                         if (c != src->transparent) {
3140 -                                               gdImageSetPixel (dst, dstX + x, dstY + y, gdTrueColor(src->red[c], src->green[c], src->blue[c]));
3141 +                                               gdImageSetPixel(dst, dstX + x, dstY + y, gdTrueColorAlpha(src->red[c], src->green[c], src->blue[c], src->alpha[c]));
3142                                         }
3143                                 }
3144                         }
3145 @@ -2225,7 +2313,7 @@
3146                         /* Have we established a mapping for this color? */
3147                         if (src->trueColor) {
3148                                 /* 2.05: remap to the palette available in the destination image. This is slow and
3149 -                                * works badly, but it beats crashing! Thanks to Padhrig McCarthy. 
3150 +                                * works badly, but it beats crashing! Thanks to Padhrig McCarthy.
3151                                  */
3152                                 mapTo = gdImageColorResolveAlpha (dst, gdTrueColorGetRed (c), gdTrueColorGetGreen (c), gdTrueColorGetBlue (c), gdTrueColorGetAlpha (c));
3153                         } else if (colorMap[c] == (-1)) {
3154 @@ -2237,9 +2325,9 @@
3155                                         nc = gdImageColorResolveAlpha (dst, src->red[c], src->green[c], src->blue[c], src->alpha[c]);
3156                                 }
3157                                 colorMap[c] = nc;
3158 -                               mapTo = colorMap[c];  
3159 +                               mapTo = colorMap[c];
3160                         } else {
3161 -                               mapTo = colorMap[c];  
3162 +                               mapTo = colorMap[c];
3163                         }
3164                         gdImageSetPixel (dst, tox, toy, mapTo);
3165                         tox++;
3166 @@ -2257,7 +2345,7 @@
3167         int tox, toy;
3168         int ncR, ncG, ncB;
3169         toy = dstY;
3170 -
3171 +       
3172         for (y = srcY; y < (srcY + h); y++) {
3173                 tox = dstX;
3174                 for (x = srcX; x < (srcX + w); x++) {
3175 @@ -2304,15 +2392,17 @@
3176                 for (x = srcX; (x < (srcX + w)); x++) {
3177                         int nc;
3178                         c = gdImageGetPixel (src, x, y);
3179 +
3180                         /* Added 7/24/95: support transparent copies */
3181                         if (gdImageGetTransparent(src) == c) {
3182                                 tox++;
3183                                 continue;
3184                         }
3185 -                       /* 
3186 -                        * If it's the same image, mapping is NOT trivial since we 
3187 -                        * merge with greyscale target, but if pct is 100, the grey 
3188 -                        * value is not used, so it becomes trivial. pjw 2.0.12. 
3189 +
3190 +                       /*
3191 +                        * If it's the same image, mapping is NOT trivial since we
3192 +                        * merge with greyscale target, but if pct is 100, the grey
3193 +                        * value is not used, so it becomes trivial. pjw 2.0.12.
3194                          */
3195                         if (dst == src && pct == 100) {
3196                                 nc = c;
3197 @@ -2320,9 +2410,10 @@
3198                                 dc = gdImageGetPixel(dst, tox, toy);
3199                                 g = (0.29900f * gdImageRed(dst, dc)) + (0.58700f * gdImageGreen(dst, dc)) + (0.11400f * gdImageBlue(dst, dc));
3200  
3201 -                                ncR = (int)(gdImageRed (src, c) * (pct / 100.0f) + g * ((100 - pct) / 100.0));
3202 -                                ncG = (int)(gdImageGreen (src, c) * (pct / 100.0f) + g * ((100 - pct) / 100.0));
3203 -                                ncB = (int)(gdImageBlue (src, c) * (pct / 100.0f) + g * ((100 - pct) / 100.0));
3204 +                               ncR = (int)(gdImageRed (src, c) * (pct / 100.0f) + g * ((100 - pct) / 100.0));
3205 +                               ncG = (int)(gdImageGreen (src, c) * (pct / 100.0f) + g * ((100 - pct) / 100.0));
3206 +                               ncB = (int)(gdImageBlue (src, c) * (pct / 100.0f) + g * ((100 - pct) / 100.0));
3207 +
3208  
3209                                 /* First look for an exact match */
3210                                 nc = gdImageColorExact(dst, ncR, ncG, ncB);
3211 @@ -2354,10 +2445,18 @@
3212         int *stx, *sty;
3213         /* We only need to use floating point to determine the correct stretch vector for one line's worth. */
3214         double accum;
3215 -       stx = (int *) safe_emalloc(sizeof(int), srcW, 0);
3216 -       sty = (int *) safe_emalloc(sizeof(int), srcH, 0);
3217 -       accum = 0;
3218         
3219 +       if (overflow2(sizeof(int), srcW)) {
3220 +               return;
3221 +       }
3222 +       if (overflow2(sizeof(int), srcH)) {
3223 +               return;
3224 +       }
3225 +
3226 +       stx = (int *) gdMalloc (sizeof (int) * srcW);
3227 +       sty = (int *) gdMalloc (sizeof (int) * srcH);
3228 +       accum = 0;
3229 +
3230         /* Fixed by Mao Morimoto 2.0.16 */
3231         for (i = 0; (i < srcW); i++) {
3232                 stx[i] = dstW * (i+1) / srcW - dstW * i / srcW ;
3233 @@ -2387,7 +2486,7 @@
3234                                                         /* 2.0.21, TK: not tox++ */
3235                                                         tox += stx[x - srcX];
3236                                                         continue;
3237 -                                               }       
3238 +                                               }
3239                                         } else {
3240                                                 /* TK: old code follows */
3241                                                 mapTo = gdImageGetTrueColorPixel (src, x, y);
3242 @@ -2397,7 +2496,7 @@
3243                                                         tox += stx[x - srcX];
3244                                                         continue;
3245                                                 }
3246 -                                       }       
3247 +                                       }
3248                                 } else {
3249                                         c = gdImageGetPixel (src, x, y);
3250                                         /* Added 7/24/95: support transparent copies */
3251 @@ -2451,6 +2550,7 @@
3252  {
3253         int x, y;
3254         double sy1, sy2, sx1, sx2;
3255 +
3256         if (!dst->trueColor) {
3257                 gdImageCopyResized (dst, src, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH);
3258                 return;
3259 @@ -2497,7 +2597,7 @@
3260                                         }
3261                                         pcontribution = xportion * yportion;
3262                                         p = gdImageGetTrueColorPixel(src, (int) sx + srcX, (int) sy + srcY);
3263 -                                       
3264 +
3265                                         alpha_factor = ((gdAlphaMax - gdTrueColorGetAlpha(p))) * pcontribution;
3266                                         red += gdTrueColorGetRed (p) * alpha_factor;
3267                                         green += gdTrueColorGetGreen (p) * alpha_factor;
3268 @@ -2509,12 +2609,12 @@
3269                                         sx += 1.0f;
3270                                 }
3271                                 while (sx < sx2);
3272 -                       
3273 +
3274                                 sy += 1.0f;
3275                         }
3276 -                       
3277 +
3278                         while (sy < sy2);
3279 -                       
3280 +
3281                         if (spixels != 0.0f) {
3282                                 red /= spixels;
3283                                 green /= spixels;
3284 @@ -2524,7 +2624,7 @@
3285                         if ( alpha_sum != 0.0f) {
3286                                 if( contrib_sum != 0.0f) {
3287                                         alpha_sum /= contrib_sum;
3288 -                               }       
3289 +                               }
3290                                 red /= alpha_sum;
3291                                 green /= alpha_sum;
3292                                 blue /= alpha_sum;
3293 @@ -2549,7 +2649,7 @@
3294  
3295  
3296  /*
3297 - * Rotate function Added on 2003/12 
3298 + * Rotate function Added on 2003/12
3299   * by Pierre-Alain Joye (pajoye@pearfr.org)
3300   **/
3301  /* Begin rotate function */
3302 @@ -2558,7 +2658,7 @@
3303  #endif /* ROTATE_PI */
3304  
3305  #define ROTATE_DEG2RAD  3.1415926535897932384626433832795/180
3306 -void gdImageSkewX (gdImagePtr dst, gdImagePtr src, int uRow, int iOffset, double dWeight, int clrBack)
3307 +void gdImageSkewX (gdImagePtr dst, gdImagePtr src, int uRow, int iOffset, double dWeight, int clrBack, int ignoretransparent)
3308  {
3309         typedef int (*FuncPtr)(gdImagePtr, int, int);
3310         int i, r, g, b, a, clrBackR, clrBackG, clrBackB, clrBackA;
3311 @@ -2623,10 +2723,14 @@
3312                         a = 127;
3313                 }
3314  
3315 -               pxlSrc = gdImageColorAllocateAlpha(dst, r, g, b, a);
3316 +               if (ignoretransparent && pxlSrc == dst->transparent) {
3317 +                       pxlSrc = dst->transparent;
3318 +               } else {
3319 +                       pxlSrc = gdImageColorAllocateAlpha(dst, r, g, b, a);
3320  
3321 -               if (pxlSrc == -1) {
3322 -                       pxlSrc = gdImageColorClosestAlpha(dst, r, g, b, a);
3323 +                       if (pxlSrc == -1) {
3324 +                               pxlSrc = gdImageColorClosestAlpha(dst, r, g, b, a);
3325 +                       }
3326                 }
3327  
3328                 if ((i + iOffset >= 0) && (i + iOffset < dst->sx)) {
3329 @@ -2651,7 +2755,7 @@
3330         }
3331  }
3332  
3333 -void gdImageSkewY (gdImagePtr dst, gdImagePtr src, int uCol, int iOffset, double dWeight, int clrBack)
3334 +void gdImageSkewY (gdImagePtr dst, gdImagePtr src, int uCol, int iOffset, double dWeight, int clrBack, int ignoretransparent)
3335  {
3336         typedef int (*FuncPtr)(gdImagePtr, int, int);
3337         int i, iYPos=0, r, g, b, a;
3338 @@ -2710,10 +2814,14 @@
3339                         a = 127;
3340                 }
3341  
3342 -               pxlSrc = gdImageColorAllocateAlpha(dst, r, g, b, a);
3343 +               if (ignoretransparent && pxlSrc == dst->transparent) {
3344 +                       pxlSrc = dst->transparent;
3345 +               } else {
3346 +                       pxlSrc = gdImageColorAllocateAlpha(dst, r, g, b, a);
3347  
3348 -               if (pxlSrc == -1) {
3349 -                       pxlSrc = gdImageColorClosestAlpha(dst, r, g, b, a);
3350 +                       if (pxlSrc == -1) {
3351 +                               pxlSrc = gdImageColorClosestAlpha(dst, r, g, b, a);
3352 +                       }
3353                 }
3354  
3355                 if ((iYPos >= 0) && (iYPos < dst->sy)) {
3356 @@ -2735,10 +2843,10 @@
3357  }
3358  
3359  /* Rotates an image by 90 degrees (counter clockwise) */
3360 -gdImagePtr gdImageRotate90 (gdImagePtr src)
3361 +gdImagePtr gdImageRotate90 (gdImagePtr src, int ignoretransparent)
3362  {
3363         int uY, uX;
3364 -       int c, r,g,b,a;
3365 +       int c,r,g,b,a;
3366         gdImagePtr dst;
3367         typedef int (*FuncPtr)(gdImagePtr, int, int);
3368         FuncPtr f;
3369 @@ -2749,8 +2857,12 @@
3370                 f = gdImageGetPixel;
3371         }
3372         dst = gdImageCreateTrueColor(src->sy, src->sx);
3373 +       dst->transparent = src->transparent;
3374  
3375         if (dst != NULL) {
3376 +               int old_blendmode = dst->alphaBlendingFlag;
3377 +               dst->alphaBlendingFlag = 0;
3378 +
3379                 gdImagePaletteCopy (dst, src);
3380  
3381                 for (uY = 0; uY<src->sy; uY++) {
3382 @@ -2763,16 +2875,21 @@
3383                                         a = gdImageAlpha(src,c);
3384                                         c = gdTrueColorAlpha(r, g, b, a);
3385                                 }
3386 -                               gdImageSetPixel(dst, uY, (dst->sy - uX - 1), c);
3387 +                               if (ignoretransparent && c == dst->transparent) {
3388 +                                       gdImageSetPixel(dst, uY, (dst->sy - uX - 1), dst->transparent);
3389 +                               } else {
3390 +                                       gdImageSetPixel(dst, uY, (dst->sy - uX - 1), c);
3391 +                               }
3392                         }
3393                 }
3394 +               dst->alphaBlendingFlag = old_blendmode;
3395         }
3396  
3397         return dst;
3398  }
3399  
3400  /* Rotates an image by 180 degrees (counter clockwise) */
3401 -gdImagePtr gdImageRotate180 (gdImagePtr src)
3402 +gdImagePtr gdImageRotate180 (gdImagePtr src, int ignoretransparent)
3403  {
3404         int uY, uX;
3405         int c,r,g,b,a;
3406 @@ -2786,8 +2903,12 @@
3407                 f = gdImageGetPixel;
3408         }
3409         dst = gdImageCreateTrueColor(src->sx, src->sy);
3410 +       dst->transparent = src->transparent;
3411  
3412         if (dst != NULL) {
3413 +               int old_blendmode = dst->alphaBlendingFlag;
3414 +               dst->alphaBlendingFlag = 0;
3415 +
3416                 gdImagePaletteCopy (dst, src);
3417  
3418                 for (uY = 0; uY<src->sy; uY++) {
3419 @@ -2800,16 +2921,22 @@
3420                                         a = gdImageAlpha(src,c);
3421                                         c = gdTrueColorAlpha(r, g, b, a);
3422                                 }
3423 -                               gdImageSetPixel(dst, (dst->sx - uX - 1), (dst->sy - uY - 1), c);
3424 +
3425 +                               if (ignoretransparent && c == dst->transparent) {
3426 +                                       gdImageSetPixel(dst, (dst->sx - uX - 1), (dst->sy - uY - 1), dst->transparent);
3427 +                               } else {
3428 +                                       gdImageSetPixel(dst, (dst->sx - uX - 1), (dst->sy - uY - 1), c);
3429 +                               }
3430                         }
3431                 }
3432 +               dst->alphaBlendingFlag = old_blendmode;
3433         }
3434  
3435         return dst;
3436  }
3437  
3438  /* Rotates an image by 270 degrees (counter clockwise) */
3439 -gdImagePtr gdImageRotate270 ( gdImagePtr src )
3440 +gdImagePtr gdImageRotate270 (gdImagePtr src, int ignoretransparent)
3441  {
3442         int uY, uX;
3443         int c,r,g,b,a;
3444 @@ -2822,9 +2949,13 @@
3445         } else {
3446                 f = gdImageGetPixel;
3447         }
3448 -       dst = gdImageCreateTrueColor(src->sy, src->sx);
3449 +       dst = gdImageCreateTrueColor (src->sy, src->sx);
3450 +       dst->transparent = src->transparent;
3451  
3452         if (dst != NULL) {
3453 +               int old_blendmode = dst->alphaBlendingFlag;
3454 +               dst->alphaBlendingFlag = 0;
3455 +
3456                 gdImagePaletteCopy (dst, src);
3457  
3458                 for (uY = 0; uY<src->sy; uY++) {
3459 @@ -2837,15 +2968,21 @@
3460                                         a = gdImageAlpha(src,c);
3461                                         c = gdTrueColorAlpha(r, g, b, a);
3462                                 }
3463 -                               gdImageSetPixel(dst, (dst->sx - uY - 1), uX, c);
3464 +
3465 +                               if (ignoretransparent && c == dst->transparent) {
3466 +                                       gdImageSetPixel(dst, (dst->sx - uY - 1), uX, dst->transparent);
3467 +                               } else {
3468 +                                       gdImageSetPixel(dst, (dst->sx - uY - 1), uX, c);
3469 +                               }
3470                         }
3471                 }
3472 +               dst->alphaBlendingFlag = old_blendmode;
3473         }
3474  
3475         return dst;
3476  }
3477  
3478 -gdImagePtr gdImageRotate45 (gdImagePtr src, double dAngle, int clrBack)
3479 +gdImagePtr gdImageRotate45 (gdImagePtr src, double dAngle, int clrBack, int ignoretransparent)
3480  {
3481         typedef int (*FuncPtr)(gdImagePtr, int, int);
3482         gdImagePtr dst1,dst2,dst3;
3483 @@ -2869,8 +3006,8 @@
3484         } else {
3485                 f = gdImageGetPixel;
3486         }
3487 -       dst1 = gdImageCreateTrueColor(newx, newy);
3488  
3489 +       dst1 = gdImageCreateTrueColor(newx, newy);
3490         /******* Perform 1st shear (horizontal) ******/
3491         if (dst1 == NULL) {
3492                 return NULL;
3493 @@ -2885,6 +3022,15 @@
3494  
3495         gdImagePaletteCopy (dst1, src);
3496  
3497 +       if (ignoretransparent) {
3498 +               if (gdImageTrueColor(src)) {
3499 +                       dst1->transparent = src->transparent;
3500 +               } else {
3501 +
3502 +                       dst1->transparent = gdTrueColorAlpha(gdImageRed(src, src->transparent), gdImageBlue(src, src->transparent), gdImageGreen(src, src->transparent), 127);
3503 +               }
3504 +       }
3505 +
3506         dRadAngle = dAngle * ROTATE_DEG2RAD; /* Angle in radians */
3507         dSinE = sin (dRadAngle);
3508         dTan = tan (dRadAngle / 2.0);
3509 @@ -2897,7 +3043,7 @@
3510                 }
3511  
3512                 iShear = (int)floor(dShear);
3513 -               gdImageSkewX(dst1, src, u, iShear, (dShear - iShear), clrBack);
3514 +               gdImageSkewX(dst1, src, u, iShear, (dShear - iShear), clrBack, ignoretransparent);
3515         }
3516  
3517         /*
3518 @@ -2933,10 +3079,13 @@
3519                 return NULL;
3520         }
3521         dst2->alphaBlendingFlag = gdEffectReplace;
3522 +       if (ignoretransparent) {
3523 +               dst2->transparent = dst1->transparent;
3524 +       }
3525  
3526         for (u = 0; u < dst2->sx; u++, dOffset -= dSinE) {
3527                 iShear = (int)floor (dOffset);
3528 -               gdImageSkewY(dst2, dst1, u, iShear, (dOffset - (double)iShear), clrBack);
3529 +               gdImageSkewY(dst2, dst1, u, iShear, (dOffset - (double)iShear), clrBack, ignoretransparent);
3530         }
3531  
3532         /* 3rd shear */
3533 @@ -2955,6 +3104,12 @@
3534                 gdImageDestroy(dst2);
3535                 return NULL;
3536         }
3537 +
3538 +       dst3->alphaBlendingFlag = gdEffectReplace;
3539 +       if (ignoretransparent) {
3540 +               dst3->transparent = dst2->transparent;
3541 +       }
3542 +
3543         if (dSinE >= 0.0) {
3544                 dOffset = (double)(src->sx - 1) * dSinE * -dTan;
3545         } else {
3546 @@ -2962,8 +3117,8 @@
3547         }
3548  
3549         for (u = 0; u < dst3->sy; u++, dOffset += dTan) {
3550 -               int iShear = (int)floor(dOffset);
3551 -               gdImageSkewX(dst3, dst2, u, iShear, (dOffset - iShear), clrBack);
3552 +               int iShear = (int)floor(dOffset);
3553 +               gdImageSkewX(dst3, dst2, u, iShear, (dOffset - iShear), clrBack, ignoretransparent);
3554         }
3555  
3556         gdImageDestroy(dst2);
3557 @@ -2971,11 +3126,11 @@
3558         return dst3;
3559  }
3560  
3561 -gdImagePtr gdImageRotate (gdImagePtr src, double dAngle, int clrBack)
3562 +gdImagePtr gdImageRotate (gdImagePtr src, double dAngle, int clrBack, int ignoretransparent)
3563  {
3564         gdImagePtr pMidImg;
3565         gdImagePtr rotatedImg;
3566 -       int r,g,b,a;
3567 +
3568         if (src == NULL) {
3569                 return NULL;
3570         }
3571 @@ -2993,41 +3148,33 @@
3572         }
3573  
3574         if (dAngle == 90.00) {
3575 -               return gdImageRotate90(src);
3576 +               return gdImageRotate90(src, ignoretransparent);
3577         }
3578         if (dAngle == 180.00) {
3579 -               return gdImageRotate180(src);
3580 +               return gdImageRotate180(src, ignoretransparent);
3581         }
3582         if(dAngle == 270.00) {
3583 -               return gdImageRotate270 ( src);
3584 +               return gdImageRotate270 (src, ignoretransparent);
3585         }
3586  
3587         if ((dAngle > 45.0) && (dAngle <= 135.0)) {
3588 -               pMidImg = gdImageRotate90 (src);
3589 +               pMidImg = gdImageRotate90 (src, ignoretransparent);
3590                 dAngle -= 90.0;
3591         } else if ((dAngle > 135.0) && (dAngle <= 225.0)) {
3592 -               pMidImg = gdImageRotate180 (src);
3593 +               pMidImg = gdImageRotate180 (src, ignoretransparent);
3594                 dAngle -= 180.0;
3595         } else if ((dAngle > 225.0) && (dAngle <= 315.0)) {
3596 -               pMidImg = gdImageRotate270 (src);
3597 +               pMidImg = gdImageRotate270 (src, ignoretransparent);
3598                 dAngle -= 270.0;
3599         } else {
3600 -               return gdImageRotate45 (src, dAngle, clrBack);
3601 +               return gdImageRotate45 (src, dAngle, clrBack, ignoretransparent);
3602         }
3603  
3604         if (pMidImg == NULL) {
3605                 return NULL;
3606         }
3607  
3608 -       if(!src->trueColor) {
3609 -               r = gdImageRed(src, clrBack);
3610 -               g = gdImageGreen(src, clrBack);
3611 -               b = gdImageBlue(src, clrBack);
3612 -               a = gdImageAlpha(src, clrBack);
3613 -               clrBack =  gdTrueColorAlpha(r,g,b,a);
3614 -       }
3615 -
3616 -       rotatedImg = gdImageRotate45 (pMidImg, dAngle, clrBack);
3617 +       rotatedImg = gdImageRotate45 (pMidImg, dAngle, clrBack, ignoretransparent);
3618         gdImageDestroy(pMidImg);
3619  
3620         return rotatedImg;
3621 @@ -3098,20 +3245,27 @@
3622                 return;
3623         }
3624  
3625 +       if (overflow2(sizeof(int), n)) {
3626 +               return;
3627 +       }
3628 +
3629         if (c == gdAntiAliased) {
3630                 fill_color = im->AA_color;
3631         } else {
3632                 fill_color = c;
3633         }
3634 -       
3635 +
3636         if (!im->polyAllocated) {
3637 -               im->polyInts = (int *) safe_emalloc(sizeof(int), n, 0);
3638 +               im->polyInts = (int *) gdMalloc(sizeof(int) * n);
3639                 im->polyAllocated = n;
3640         }
3641         if (im->polyAllocated < n) {
3642                 while (im->polyAllocated < n) {
3643                         im->polyAllocated *= 2;
3644                 }
3645 +               if (overflow2(sizeof(int), im->polyAllocated)) {
3646 +                       return;
3647 +               }
3648                 im->polyInts = (int *) gdRealloc(im->polyInts, sizeof(int) * im->polyAllocated);
3649         }
3650         miny = p[0].y;
3651 @@ -3131,7 +3285,7 @@
3652         }
3653         if (maxy >= gdImageSY(im)) {
3654                 maxy = gdImageSY(im) - 1;
3655 -       } 
3656 +       }
3657  
3658         /* Fix in 1.3: count a vertex only once */
3659         for (y = miny; y <= maxy; y++) {
3660 @@ -3193,7 +3347,7 @@
3661         if (im->style) {
3662                 gdFree(im->style);
3663         }
3664 -       im->style = (int *) safe_emalloc(sizeof(int), noOfPixels, 0);
3665 +       im->style = (int *) gdMalloc(sizeof(int) * noOfPixels);
3666         memcpy(im->style, style, sizeof(int) * noOfPixels);
3667         im->styleLength = noOfPixels;
3668         im->stylePos = 0;
3669 @@ -3323,9 +3477,9 @@
3670  }
3671  
3672  int
3673 -gdAlphaBlend (int dst, int src)
3674 +gdAlphaBlendOld (int dst, int src)
3675  {
3676 -       /* 2.0.12: TBB: alpha in the destination should be a 
3677 +       /* 2.0.12: TBB: alpha in the destination should be a
3678          * component of the result. Thanks to Frank Warmerdam for
3679          * pointing out the issue.
3680          */
3681 @@ -3345,6 +3499,51 @@
3682             gdTrueColorGetBlue (dst)) / gdAlphaMax));
3683  }
3684  
3685 +int gdAlphaBlend (int dst, int src) {
3686 +    int src_alpha = gdTrueColorGetAlpha(src);
3687 +    int dst_alpha, alpha, red, green, blue;
3688 +    int src_weight, dst_weight, tot_weight;
3689 +
3690 +/* -------------------------------------------------------------------- */
3691 +/*      Simple cases we want to handle fast.                            */
3692 +/* -------------------------------------------------------------------- */
3693 +    if( src_alpha == gdAlphaOpaque )
3694 +        return src;
3695 +
3696 +    dst_alpha = gdTrueColorGetAlpha(dst);
3697 +    if( src_alpha == gdAlphaTransparent )
3698 +        return dst;
3699 +    if( dst_alpha == gdAlphaTransparent )
3700 +        return src;
3701 +
3702 +/* -------------------------------------------------------------------- */
3703 +/*      What will the source and destination alphas be?  Note that      */
3704 +/*      the destination weighting is substantially reduced as the       */
3705 +/*      overlay becomes quite opaque.                                   */
3706 +/* -------------------------------------------------------------------- */
3707 +    src_weight = gdAlphaTransparent - src_alpha;
3708 +    dst_weight = (gdAlphaTransparent - dst_alpha) * src_alpha / gdAlphaMax;
3709 +    tot_weight = src_weight + dst_weight;
3710 +    
3711 +/* -------------------------------------------------------------------- */
3712 +/*      What red, green and blue result values will we use?             */
3713 +/* -------------------------------------------------------------------- */
3714 +    alpha = src_alpha * dst_alpha / gdAlphaMax;
3715 +
3716 +    red = (gdTrueColorGetRed(src) * src_weight
3717 +           + gdTrueColorGetRed(dst) * dst_weight) / tot_weight;
3718 +    green = (gdTrueColorGetGreen(src) * src_weight
3719 +           + gdTrueColorGetGreen(dst) * dst_weight) / tot_weight;
3720 +    blue = (gdTrueColorGetBlue(src) * src_weight
3721 +           + gdTrueColorGetBlue(dst) * dst_weight) / tot_weight;
3722 +
3723 +/* -------------------------------------------------------------------- */
3724 +/*      Return merged result.                                           */
3725 +/* -------------------------------------------------------------------- */
3726 +    return ((alpha << 24) + (red << 16) + (green << 8) + blue);
3727 +
3728 +}
3729 +
3730  void gdImageAlphaBlending (gdImagePtr im, int alphaBlendingArg)
3731  {
3732         im->alphaBlendingFlag = alphaBlendingArg;
3733 @@ -3362,44 +3561,6 @@
3734         im->saveAlphaFlag = saveAlphaArg;
3735  }
3736  
3737 -static int gdFullAlphaBlend (int dst, int src)
3738 -{
3739 -       int a1, a2;
3740 -       a1 = gdAlphaTransparent - gdTrueColorGetAlpha(src);
3741 -       a2 = gdAlphaTransparent - gdTrueColorGetAlpha(dst);
3742 -
3743 -       return ( ((gdAlphaTransparent - ((a1+a2)-(a1*a2/gdAlphaMax))) << 24) +
3744 -               (gdAlphaBlendColor( gdTrueColorGetRed(src), gdTrueColorGetRed(dst), a1, a2 ) << 16) +
3745 -               (gdAlphaBlendColor( gdTrueColorGetGreen(src), gdTrueColorGetGreen(dst), a1, a2 ) << 8) +
3746 -               (gdAlphaBlendColor( gdTrueColorGetBlue(src), gdTrueColorGetBlue(dst), a1, a2 ))
3747 -               );
3748 -}
3749 -
3750 -static int gdAlphaBlendColor( int b1, int b2, int a1, int a2 )
3751 -{
3752 -       int c;
3753 -       int w;
3754 -
3755 -       /* deal with special cases */
3756 -
3757 -       if( (gdAlphaMax == a1) || (0 == a2) ) {
3758 -               /* the back pixel can't be seen */
3759 -               return b1;
3760 -       } else if(0 == a1) {
3761 -               /* the front pixel can't be seen */
3762 -               return b2;
3763 -       } else if(gdAlphaMax == a2) {
3764 -               /* the back pixel is opaque */
3765 -               return ( a1 * b1 + ( gdAlphaMax - a1 ) * b2 ) / gdAlphaMax;
3766 -       }
3767 -
3768 -       /* the general case */
3769 -       w = ( a1 * ( gdAlphaMax - a2 ) / ( gdAlphaMax - a1 * a2 / gdAlphaMax ) * b1 + \
3770 -                 a2 * ( gdAlphaMax - a1 ) / ( gdAlphaMax - a1 * a2 / gdAlphaMax ) * b2 ) / gdAlphaMax;
3771 -       c = (a2 * b2  +  ( gdAlphaMax - a2 ) * w ) / gdAlphaMax;
3772 -       return ( a1 * b1 + ( gdAlphaMax - a1 ) * c ) / gdAlphaMax;
3773 -}
3774 -
3775  static int gdLayerOverlay (int dst, int src)
3776  {
3777         int a1, a2;
3778 @@ -3415,12 +3576,12 @@
3779  static int gdAlphaOverlayColor (int src, int dst, int max )
3780  {
3781         /* this function implements the algorithm
3782 -        * 
3783 +        *
3784          * for dst[rgb] < 0.5,
3785          *   c[rgb] = 2.src[rgb].dst[rgb]
3786          * and for dst[rgb] > 0.5,
3787          *   c[rgb] = -2.src[rgb].dst[rgb] + 2.dst[rgb] + 2.src[rgb] - 1
3788 -        *   
3789 +        *
3790          */
3791  
3792         dst = dst << 1;
3793 @@ -3472,3 +3633,457 @@
3794         *x2P = im->cx2;
3795         *y2P = im->cy2;
3796  }
3797 +
3798 +
3799 +/* Filters function added on 2003/12
3800 + * by Pierre-Alain Joye (pajoye@pearfr.org)
3801 + **/
3802 +/* Begin filters function */
3803 +#ifndef HAVE_GET_TRUE_COLOR
3804 +#define GET_PIXEL_FUNCTION(src)(src->trueColor?gdImageGetTrueColorPixel:gdImageGetPixel)
3805 +#endif
3806 +
3807 +/* invert src image */
3808 +int gdImageNegate(gdImagePtr src)
3809 +{
3810 +       int x, y;
3811 +       int r,g,b,a;
3812 +       int new_pxl, pxl;
3813 +       typedef int (*FuncPtr)(gdImagePtr, int, int);
3814 +       FuncPtr f;
3815 +
3816 +       if (src==NULL) {
3817 +               return 0;
3818 +       }
3819 +
3820 +       f = GET_PIXEL_FUNCTION(src);
3821 +
3822 +       for (y=0; y<src->sy; ++y) {
3823 +               for (x=0; x<src->sx; ++x) {
3824 +                       pxl = f (src, x, y);
3825 +                       r = gdImageRed(src, pxl);
3826 +                       g = gdImageGreen(src, pxl);
3827 +                       b = gdImageBlue(src, pxl);
3828 +                       a = gdImageAlpha(src, pxl);
3829 +
3830 +                       new_pxl = gdImageColorAllocateAlpha(src, 255-r, 255-g, 255-b, a);
3831 +                       if (new_pxl == -1) {
3832 +                               new_pxl = gdImageColorClosestAlpha(src, 255-r, 255-g, 255-b, a);
3833 +                       }
3834 +                       gdImageSetPixel (src, x, y, new_pxl);
3835 +               }
3836 +       }
3837 +       return 1;
3838 +}
3839 +
3840 +/* Convert the image src to a grayscale image */
3841 +int gdImageGrayScale(gdImagePtr src)
3842 +{
3843 +       int x, y;
3844 +       int r,g,b,a;
3845 +       int new_pxl, pxl;
3846 +       typedef int (*FuncPtr)(gdImagePtr, int, int);
3847 +       FuncPtr f;
3848 +       f = GET_PIXEL_FUNCTION(src);
3849 +
3850 +       if (src==NULL) {
3851 +               return 0;
3852 +       }
3853 +
3854 +       for (y=0; y<src->sy; ++y) {
3855 +               for (x=0; x<src->sx; ++x) {
3856 +                       pxl = f (src, x, y);
3857 +                       r = gdImageRed(src, pxl);
3858 +                       g = gdImageGreen(src, pxl);
3859 +                       b = gdImageBlue(src, pxl);
3860 +                       a = gdImageAlpha(src, pxl);
3861 +                       r = g = b = (int) (.299 * r + .587 * g + .114 * b);
3862 +
3863 +                       new_pxl = gdImageColorAllocateAlpha(src, r, g, b, a);
3864 +                       if (new_pxl == -1) {
3865 +                               new_pxl = gdImageColorClosestAlpha(src, r, g, b, a);
3866 +                       }
3867 +                       gdImageSetPixel (src, x, y, new_pxl);
3868 +               }
3869 +       }
3870 +       return 1;
3871 +}
3872 +
3873 +/* Set the brightness level <level> for the image src */
3874 +int gdImageBrightness(gdImagePtr src, int brightness)
3875 +{
3876 +       int x, y;
3877 +       int r,g,b,a;
3878 +       int new_pxl, pxl;
3879 +       typedef int (*FuncPtr)(gdImagePtr, int, int);
3880 +       FuncPtr f;
3881 +       f = GET_PIXEL_FUNCTION(src);
3882 +
3883 +       if (src==NULL || (brightness < -255 || brightness>255)) {
3884 +               return 0;
3885 +       }
3886 +
3887 +       if (brightness==0) {
3888 +               return 1;
3889 +       }
3890 +
3891 +       for (y=0; y<src->sy; ++y) {
3892 +               for (x=0; x<src->sx; ++x) {
3893 +                       pxl = f (src, x, y);
3894 +
3895 +                       r = gdImageRed(src, pxl);
3896 +                       g = gdImageGreen(src, pxl);
3897 +                       b = gdImageBlue(src, pxl);
3898 +                       a = gdImageAlpha(src, pxl);
3899 +
3900 +                       r = r + brightness;
3901 +                       g = g + brightness;
3902 +                       b = b + brightness;
3903 +
3904 +                       r = (r > 255)? 255 : ((r < 0)? 0:r);
3905 +                       g = (g > 255)? 255 : ((g < 0)? 0:g);
3906 +                       b = (b > 255)? 255 : ((b < 0)? 0:b);
3907 +
3908 +                       new_pxl = gdImageColorAllocateAlpha(src, (int)r, (int)g, (int)b, a);
3909 +                       if (new_pxl == -1) {
3910 +                               new_pxl = gdImageColorClosestAlpha(src, (int)r, (int)g, (int)b, a);
3911 +                       }
3912 +                       gdImageSetPixel (src, x, y, new_pxl);
3913 +               }
3914 +       }
3915 +       return 1;
3916 +}
3917 +
3918 +
3919 +int gdImageContrast(gdImagePtr src, double contrast)
3920 +{
3921 +       int x, y;
3922 +       int r,g,b,a;
3923 +       double rf,gf,bf;
3924 +       int new_pxl, pxl;
3925 +       typedef int (*FuncPtr)(gdImagePtr, int, int);
3926 +
3927 +       FuncPtr f;
3928 +       f = GET_PIXEL_FUNCTION(src);
3929 +
3930 +       if (src==NULL) {
3931 +               return 0;
3932 +       }
3933 +
3934 +       contrast = (double)(100.0-contrast)/100.0;
3935 +       contrast = contrast*contrast;
3936 +
3937 +       for (y=0; y<src->sy; ++y) {
3938 +               for (x=0; x<src->sx; ++x) {
3939 +                       pxl = f(src, x, y);
3940 +
3941 +                       r = gdImageRed(src, pxl);
3942 +                       g = gdImageGreen(src, pxl);
3943 +                       b = gdImageBlue(src, pxl);
3944 +                       a = gdImageAlpha(src, pxl);
3945 +
3946 +                       rf = (double)r/255.0;
3947 +                       rf = rf-0.5;
3948 +                       rf = rf*contrast;
3949 +                       rf = rf+0.5;
3950 +                       rf = rf*255.0;
3951 +
3952 +                       bf = (double)b/255.0;
3953 +                       bf = bf-0.5;
3954 +                       bf = bf*contrast;
3955 +                       bf = bf+0.5;
3956 +                       bf = bf*255.0;
3957 +
3958 +                       gf = (double)g/255.0;
3959 +                       gf = gf-0.5;
3960 +                       gf = gf*contrast;
3961 +                       gf = gf+0.5;
3962 +                       gf = gf*255.0;
3963 +
3964 +                       rf = (rf > 255.0)? 255.0 : ((rf < 0.0)? 0.0:rf);
3965 +                       gf = (gf > 255.0)? 255.0 : ((gf < 0.0)? 0.0:gf);
3966 +                       bf = (bf > 255.0)? 255.0 : ((bf < 0.0)? 0.0:bf);
3967 +
3968 +                       new_pxl = gdImageColorAllocateAlpha(src, (int)rf, (int)gf, (int)bf, a);
3969 +                       if (new_pxl == -1) {
3970 +                               new_pxl = gdImageColorClosestAlpha(src, (int)rf, (int)gf, (int)bf, a);
3971 +                       }
3972 +                       gdImageSetPixel (src, x, y, new_pxl);
3973 +               }
3974 +       }
3975 +       return 1;
3976 +}
3977 +
3978 +
3979 +int gdImageColor(gdImagePtr src, const int red, const int green, const int blue, const int alpha)
3980 +{
3981 +       int x, y;
3982 +       int new_pxl, pxl;
3983 +       typedef int (*FuncPtr)(gdImagePtr, int, int);
3984 +       FuncPtr f;
3985 +
3986 +       if (src == NULL) {
3987 +               return 0;
3988 +       }
3989 +
3990 +       f = GET_PIXEL_FUNCTION(src);
3991 +
3992 +       for (y=0; y<src->sy; ++y) {
3993 +               for (x=0; x<src->sx; ++x) {
3994 +                       int r,g,b,a;
3995 +
3996 +                       pxl = f(src, x, y);
3997 +                       r = gdImageRed(src, pxl);
3998 +                       g = gdImageGreen(src, pxl);
3999 +                       b = gdImageBlue(src, pxl);
4000 +                       a = gdImageAlpha(src, pxl);
4001 +
4002 +                       r = r + red;
4003 +                       g = g + green;
4004 +                       b = b + blue;
4005 +                       a = a + alpha;
4006 +
4007 +                       r = (r > 255)? 255 : ((r < 0)? 0 : r);
4008 +                       g = (g > 255)? 255 : ((g < 0)? 0 : g);
4009 +                       b = (b > 255)? 255 : ((b < 0)? 0 : b);
4010 +                       a = (a > 127)? 127 : ((a < 0)? 0 : a);
4011 +
4012 +                       new_pxl = gdImageColorAllocateAlpha(src, r, g, b, a);
4013 +                       if (new_pxl == -1) {
4014 +                               new_pxl = gdImageColorClosestAlpha(src, r, g, b, a);
4015 +                       }
4016 +                       gdImageSetPixel (src, x, y, new_pxl);
4017 +               }
4018 +       }
4019 +       return 1;
4020 +}
4021 +
4022 +int gdImageConvolution(gdImagePtr src, float filter[3][3], float filter_div, float offset)
4023 +{
4024 +       int         x, y, i, j, new_a;
4025 +       float       new_r, new_g, new_b;
4026 +       int         new_pxl, pxl=0;
4027 +       gdImagePtr  srcback;
4028 +       typedef int (*FuncPtr)(gdImagePtr, int, int);
4029 +       FuncPtr f;
4030 +
4031 +       if (src==NULL) {
4032 +               return 0;
4033 +       }
4034 +
4035 +       /* We need the orinal image with each safe neoghb. pixel */
4036 +       srcback = gdImageCreateTrueColor (src->sx, src->sy);
4037 +       gdImageCopy(srcback, src,0,0,0,0,src->sx,src->sy);
4038 +
4039 +       if (srcback==NULL) {
4040 +               return 0;
4041 +       }
4042 +
4043 +       f = GET_PIXEL_FUNCTION(src);
4044 +
4045 +       for ( y=0; y<src->sy; y++) {
4046 +               for(x=0; x<src->sx; x++) {
4047 +                       new_r = new_g = new_b = 0;
4048 +                       new_a = gdImageAlpha(srcback, pxl);
4049 +
4050 +                       for (j=0; j<3; j++) {
4051 +                               int yv = MIN(MAX(y - 1 + j, 0), src->sy - 1);
4052 +                               for (i=0; i<3; i++) {
4053 +                                       pxl = f(srcback, MIN(MAX(x - 1 + i, 0), src->sx - 1), yv);
4054 +                                       new_r += (float)gdImageRed(srcback, pxl) * filter[j][i];
4055 +                                       new_g += (float)gdImageGreen(srcback, pxl) * filter[j][i];
4056 +                                       new_b += (float)gdImageBlue(srcback, pxl) * filter[j][i];
4057 +                               }
4058 +                       }
4059 +
4060 +                       new_r = (new_r/filter_div)+offset;
4061 +                       new_g = (new_g/filter_div)+offset;
4062 +                       new_b = (new_b/filter_div)+offset;
4063 +
4064 +                       new_r = (new_r > 255.0f)? 255.0f : ((new_r < 0.0f)? 0.0f:new_r);
4065 +                       new_g = (new_g > 255.0f)? 255.0f : ((new_g < 0.0f)? 0.0f:new_g);
4066 +                       new_b = (new_b > 255.0f)? 255.0f : ((new_b < 0.0f)? 0.0f:new_b);
4067 +
4068 +                       new_pxl = gdImageColorAllocateAlpha(src, (int)new_r, (int)new_g, (int)new_b, new_a);
4069 +                       if (new_pxl == -1) {
4070 +                               new_pxl = gdImageColorClosestAlpha(src, (int)new_r, (int)new_g, (int)new_b, new_a);
4071 +                       }
4072 +                       gdImageSetPixel (src, x, y, new_pxl);
4073 +               }
4074 +       }
4075 +       gdImageDestroy(srcback);
4076 +       return 1;
4077 +}
4078 +
4079 +int gdImageSelectiveBlur( gdImagePtr src)
4080 +{
4081 +       int         x, y, i, j;
4082 +       float       new_r, new_g, new_b;
4083 +       int         new_pxl, cpxl, pxl, new_a=0;
4084 +       float flt_r [3][3];
4085 +       float flt_g [3][3];
4086 +       float flt_b [3][3];
4087 +       float flt_r_sum, flt_g_sum, flt_b_sum;
4088 +
4089 +       gdImagePtr srcback;
4090 +       typedef int (*FuncPtr)(gdImagePtr, int, int);
4091 +       FuncPtr f;
4092 +
4093 +       if (src==NULL) {
4094 +               return 0;
4095 +       }
4096 +
4097 +       /* We need the orinal image with each safe neoghb. pixel */
4098 +       srcback = gdImageCreateTrueColor (src->sx, src->sy);
4099 +       gdImageCopy(srcback, src,0,0,0,0,src->sx,src->sy);
4100 +
4101 +       if (srcback==NULL) {
4102 +               return 0;
4103 +       }
4104 +
4105 +       f = GET_PIXEL_FUNCTION(src);
4106 +
4107 +       for(y = 0; y<src->sy; y++) {
4108 +               for (x=0; x<src->sx; x++) {
4109 +                     flt_r_sum = flt_g_sum = flt_b_sum = 0.0;
4110 +                       cpxl = f(src, x, y);
4111 +
4112 +                       for (j=0; j<3; j++) {
4113 +                               for (i=0; i<3; i++) {
4114 +                                       if ((j == 1) && (i == 1)) {
4115 +                                               flt_r[1][1] = flt_g[1][1] = flt_b[1][1] = 0.5;
4116 +                                       } else {
4117 +                                               pxl = f(src, x-(3>>1)+i, y-(3>>1)+j);
4118 +                                               new_a = gdImageAlpha(srcback, pxl);
4119 +
4120 +                                               new_r = ((float)gdImageRed(srcback, cpxl)) - ((float)gdImageRed (srcback, pxl));
4121 +
4122 +                                               if (new_r < 0.0f) {
4123 +                                                       new_r = -new_r;
4124 +                                               }
4125 +                                               if (new_r != 0) {
4126 +                                                       flt_r[j][i] = 1.0f/new_r;
4127 +                                               } else {
4128 +                                                       flt_r[j][i] = 1.0f;
4129 +                                               }
4130 +
4131 +                                               new_g = ((float)gdImageGreen(srcback, cpxl)) - ((float)gdImageGreen(srcback, pxl));
4132 +
4133 +                                               if (new_g < 0.0f) {
4134 +                                                       new_g = -new_g;
4135 +                                               }
4136 +                                               if (new_g != 0) {
4137 +                                                       flt_g[j][i] = 1.0f/new_g;
4138 +                                               } else {
4139 +                                                       flt_g[j][i] = 1.0f;
4140 +                                               }
4141 +
4142 +                                               new_b = ((float)gdImageBlue(srcback, cpxl)) - ((float)gdImageBlue(srcback, pxl));
4143 +
4144 +                                               if (new_b < 0.0f) {
4145 +                                                       new_b = -new_b;
4146 +                                               }
4147 +                                               if (new_b != 0) {
4148 +                                                       flt_b[j][i] = 1.0f/new_b;
4149 +                                               } else {
4150 +                                                       flt_b[j][i] = 1.0f;
4151 +                                               }
4152 +                                       }
4153 +
4154 +                                       flt_r_sum += flt_r[j][i];
4155 +                                       flt_g_sum += flt_g[j][i];
4156 +                                       flt_b_sum += flt_b [j][i];
4157 +                               }
4158 +                       }
4159 +
4160 +                       for (j=0; j<3; j++) {
4161 +                               for (i=0; i<3; i++) {
4162 +                                       if (flt_r_sum != 0.0) {
4163 +                                               flt_r[j][i] /= flt_r_sum;
4164 +                                       }
4165 +                                       if (flt_g_sum != 0.0) {
4166 +                                               flt_g[j][i] /= flt_g_sum;
4167 +                                       }
4168 +                                       if (flt_b_sum != 0.0) {
4169 +                                               flt_b [j][i] /= flt_b_sum;
4170 +                                       }
4171 +                               }
4172 +                       }
4173 +
4174 +                       new_r = new_g = new_b = 0.0;
4175 +
4176 +                       for (j=0; j<3; j++) {
4177 +                               for (i=0; i<3; i++) {
4178 +                                       pxl = f(src, x-(3>>1)+i, y-(3>>1)+j);
4179 +                                       new_r += (float)gdImageRed(srcback, pxl) * flt_r[j][i];
4180 +                                       new_g += (float)gdImageGreen(srcback, pxl) * flt_g[j][i];
4181 +                                       new_b += (float)gdImageBlue(srcback, pxl) * flt_b[j][i];
4182 +                               }
4183 +                       }
4184 +
4185 +                       new_r = (new_r > 255.0f)? 255.0f : ((new_r < 0.0f)? 0.0f:new_r);
4186 +                       new_g = (new_g > 255.0f)? 255.0f : ((new_g < 0.0f)? 0.0f:new_g);
4187 +                       new_b = (new_b > 255.0f)? 255.0f : ((new_b < 0.0f)? 0.0f:new_b);
4188 +                       new_pxl = gdImageColorAllocateAlpha(src, (int)new_r, (int)new_g, (int)new_b, new_a);
4189 +                       if (new_pxl == -1) {
4190 +                               new_pxl = gdImageColorClosestAlpha(src, (int)new_r, (int)new_g, (int)new_b, new_a);
4191 +                       }
4192 +                       gdImageSetPixel (src, x, y, new_pxl);
4193 +               }
4194 +       }
4195 +       gdImageDestroy(srcback);
4196 +       return 1;
4197 +}
4198 +
4199 +int gdImageEdgeDetectQuick(gdImagePtr src)
4200 +{
4201 +       float filter[3][3] =    {{-1.0,0.0,-1.0},
4202 +                               {0.0,4.0,0.0},
4203 +                               {-1.0,0.0,-1.0}};
4204 +
4205 +       return gdImageConvolution(src, filter, 1, 127);
4206 +}
4207 +
4208 +int gdImageGaussianBlur(gdImagePtr im)
4209 +{
4210 +       float filter[3][3] =    {{1.0,2.0,1.0},
4211 +                               {2.0,4.0,2.0},
4212 +                               {1.0,2.0,1.0}};
4213 +
4214 +       return gdImageConvolution(im, filter, 16, 0);
4215 +}
4216 +
4217 +int gdImageEmboss(gdImagePtr im)
4218 +{
4219 +/*
4220 +       float filter[3][3] =    {{1.0,1.0,1.0},
4221 +                               {0.0,0.0,0.0},
4222 +                               {-1.0,-1.0,-1.0}};
4223 +*/
4224 +       float filter[3][3] =    {{ 1.5, 0.0, 0.0},
4225 +                                { 0.0, 0.0, 0.0},
4226 +                                { 0.0, 0.0,-1.5}};
4227 +
4228 +       return gdImageConvolution(im, filter, 1, 127);
4229 +}
4230 +
4231 +int gdImageMeanRemoval(gdImagePtr im)
4232 +{
4233 +       float filter[3][3] =    {{-1.0,-1.0,-1.0},
4234 +                               {-1.0,9.0,-1.0},
4235 +                               {-1.0,-1.0,-1.0}};
4236 +
4237 +       return gdImageConvolution(im, filter, 1, 0);
4238 +}
4239 +
4240 +int gdImageSmooth(gdImagePtr im, float weight)
4241 +{
4242 +       float filter[3][3] =    {{1.0,1.0,1.0},
4243 +                               {1.0,0.0,1.0},
4244 +                               {1.0,1.0,1.0}};
4245 +
4246 +       filter[1][1] = weight;
4247 +
4248 +       return gdImageConvolution(im, filter, weight+8, 0);
4249 +}
4250 +/* End filters function */
4251 diff -urN php-4.4.8.org/ext/gd/libgd/gdcache.c php-4.4.8/ext/gd/libgd/gdcache.c
4252 --- php-4.4.8.org/ext/gd/libgd/gdcache.c        2003-04-05 19:24:16.000000000 +0200
4253 +++ php-4.4.8/ext/gd/libgd/gdcache.c    2003-12-28 21:11:08.000000000 +0100
4254 @@ -11,11 +11,11 @@
4255  
4256  #ifdef NEED_CACHE
4257  
4258 -/* 
4259 +/*
4260   * gdcache.c
4261   *
4262 - * Caches of pointers to user structs in which the least-recently-used 
4263 - * element is replaced in the event of a cache miss after the cache has 
4264 + * Caches of pointers to user structs in which the least-recently-used
4265 + * element is replaced in the event of a cache miss after the cache has
4266   * reached a given size.
4267   *
4268   * John Ellson  (ellson@graphviz.org)  Oct 31, 1997
4269 @@ -30,17 +30,17 @@
4270   * The head structure has a pointer to the most-recently-used
4271   * element, and elements are moved to this position in the list each
4272   * time they are used.  The head also contains pointers to three
4273 - * user defined functions: 
4274 - *              - a function to test if a cached userdata matches some keydata 
4275 - *              - a function to provide a new userdata struct to the cache 
4276 + * user defined functions:
4277 + *              - a function to test if a cached userdata matches some keydata
4278 + *              - a function to provide a new userdata struct to the cache
4279   *          if there has been a cache miss.
4280   *              - a function to release a userdata struct when it is
4281   *          no longer being managed by the cache
4282   *
4283   * In the event of a cache miss the cache is allowed to grow up to
4284   * a specified maximum size.  After the maximum size is reached then
4285 - * the least-recently-used element is discarded to make room for the 
4286 - * new.  The most-recently-returned value is always left at the 
4287 + * the least-recently-used element is discarded to make room for the
4288 + * new.  The most-recently-returned value is always left at the
4289   * beginning of the list after retrieval.
4290   *
4291   * In the current implementation the cache is traversed by a linear
4292 diff -urN php-4.4.8.org/ext/gd/libgd/gdcache.h php-4.4.8/ext/gd/libgd/gdcache.h
4293 --- php-4.4.8.org/ext/gd/libgd/gdcache.h        2003-04-05 19:24:16.000000000 +0200
4294 +++ php-4.4.8/ext/gd/libgd/gdcache.h    2003-12-28 21:11:08.000000000 +0100
4295 @@ -1,8 +1,8 @@
4296 -/* 
4297 +/*
4298   * gdcache.h
4299   *
4300 - * Caches of pointers to user structs in which the least-recently-used 
4301 - * element is replaced in the event of a cache miss after the cache has 
4302 + * Caches of pointers to user structs in which the least-recently-used
4303 + * element is replaced in the event of a cache miss after the cache has
4304   * reached a given size.
4305   *
4306   * John Ellson  (ellson@graphviz.org)  Oct 31, 1997
4307 @@ -17,17 +17,17 @@
4308   * The head structure has a pointer to the most-recently-used
4309   * element, and elements are moved to this position in the list each
4310   * time they are used.  The head also contains pointers to three
4311 - * user defined functions: 
4312 - *             - a function to test if a cached userdata matches some keydata 
4313 - *             - a function to provide a new userdata struct to the cache 
4314 + * user defined functions:
4315 + *             - a function to test if a cached userdata matches some keydata
4316 + *             - a function to provide a new userdata struct to the cache
4317   *          if there has been a cache miss.
4318   *             - a function to release a userdata struct when it is
4319   *          no longer being managed by the cache
4320   *
4321   * In the event of a cache miss the cache is allowed to grow up to
4322   * a specified maximum size.  After the maximum size is reached then
4323 - * the least-recently-used element is discarded to make room for the 
4324 - * new.  The most-recently-returned value is always left at the 
4325 + * the least-recently-used element is discarded to make room for the
4326 + * new.  The most-recently-returned value is always left at the
4327   * beginning of the list after retrieval.
4328   *
4329   * In the current implementation the cache is traversed by a linear
4330 diff -urN php-4.4.8.org/ext/gd/libgd/gdfontg.c php-4.4.8/ext/gd/libgd/gdfontg.c
4331 --- php-4.4.8.org/ext/gd/libgd/gdfontg.c        2004-03-29 20:21:00.000000000 +0200
4332 +++ php-4.4.8/ext/gd/libgd/gdfontg.c    2006-09-16 21:07:45.000000000 +0200
4333 @@ -13,7 +13,7 @@
4334  
4335  #include "gdfontg.h"
4336  
4337 -char gdFontGiantData[] =
4338 +static const char gdFontGiantData[] =
4339  {
4340  /* Char 0 */
4341    0, 0, 0, 0, 0, 0, 0, 0, 0,
4342 @@ -4376,7 +4376,7 @@
4343    0,
4344    9,
4345    15,
4346 -  gdFontGiantData
4347 +  (char*)gdFontGiantData
4348  };
4349  
4350  gdFontPtr gdFontGiant = &gdFontGiantRep;
4351 diff -urN php-4.4.8.org/ext/gd/libgd/gdfontl.c php-4.4.8/ext/gd/libgd/gdfontl.c
4352 --- php-4.4.8.org/ext/gd/libgd/gdfontl.c        2004-03-29 20:21:00.000000000 +0200
4353 +++ php-4.4.8/ext/gd/libgd/gdfontl.c    2006-09-16 21:07:45.000000000 +0200
4354 @@ -14,7 +14,7 @@
4355  
4356  #include "gdfontl.h"
4357  
4358 -char gdFontLargeData[] =
4359 +static const char gdFontLargeData[] =
4360  {
4361  /* Char 0 */
4362    0, 0, 0, 0, 0, 0, 0, 0,
4363 @@ -4633,7 +4633,7 @@
4364    0,
4365    8,
4366    16,
4367 -  gdFontLargeData
4368 +  (char*)gdFontLargeData
4369  };
4370  
4371  gdFontPtr gdFontLarge = &gdFontLargeRep;
4372 diff -urN php-4.4.8.org/ext/gd/libgd/gdfontmb.c php-4.4.8/ext/gd/libgd/gdfontmb.c
4373 --- php-4.4.8.org/ext/gd/libgd/gdfontmb.c       2004-03-29 20:21:00.000000000 +0200
4374 +++ php-4.4.8/ext/gd/libgd/gdfontmb.c   2006-09-16 21:07:46.000000000 +0200
4375 @@ -12,7 +12,7 @@
4376  
4377  #include "gdfontmb.h"
4378  
4379 -char gdFontMediumBoldData[] =
4380 +static const char gdFontMediumBoldData[] =
4381  {
4382  /* Char 0 */
4383    0, 0, 0, 0, 0, 0, 0,
4384 @@ -3863,7 +3863,7 @@
4385    0,
4386    7,
4387    13,
4388 -  gdFontMediumBoldData
4389 +  (char*)gdFontMediumBoldData
4390  };
4391  
4392  gdFontPtr gdFontMediumBold = &gdFontMediumBoldRep;
4393 diff -urN php-4.4.8.org/ext/gd/libgd/gdfonts.c php-4.4.8/ext/gd/libgd/gdfonts.c
4394 --- php-4.4.8.org/ext/gd/libgd/gdfonts.c        2004-03-29 20:21:00.000000000 +0200
4395 +++ php-4.4.8/ext/gd/libgd/gdfonts.c    2006-09-16 21:07:46.000000000 +0200
4396 @@ -12,7 +12,7 @@
4397  
4398  #include "gdfonts.h"
4399  
4400 -char gdFontSmallData[] =
4401 +static const char gdFontSmallData[] =
4402  {
4403  /* Char 0 */
4404    0, 0, 0, 0, 0, 0,
4405 @@ -3863,7 +3863,7 @@
4406    0,
4407    6,
4408    13,
4409 -  gdFontSmallData
4410 +  (char*)gdFontSmallData
4411  };
4412  
4413  gdFontPtr gdFontSmall = &gdFontSmallRep;
4414 diff -urN php-4.4.8.org/ext/gd/libgd/gdfontt.c php-4.4.8/ext/gd/libgd/gdfontt.c
4415 --- php-4.4.8.org/ext/gd/libgd/gdfontt.c        2004-03-29 20:21:00.000000000 +0200
4416 +++ php-4.4.8/ext/gd/libgd/gdfontt.c    2006-09-16 21:07:46.000000000 +0200
4417 @@ -13,7 +13,7 @@
4418  
4419  #include "gdfontt.h"
4420  
4421 -char gdFontTinyData[] =
4422 +static const char gdFontTinyData[] =
4423  {
4424  /* Char 0 */
4425    0, 0, 0, 0, 0,
4426 @@ -2584,7 +2584,7 @@
4427    0,
4428    5,
4429    8,
4430 -  gdFontTinyData
4431 +  (char*)gdFontTinyData
4432  };
4433  
4434  gdFontPtr gdFontTiny = &gdFontTinyRep;
4435 diff -urN php-4.4.8.org/ext/gd/libgd/gdft.c php-4.4.8/ext/gd/libgd/gdft.c
4436 --- php-4.4.8.org/ext/gd/libgd/gdft.c   2007-03-10 13:51:07.000000000 +0100
4437 +++ php-4.4.8/ext/gd/libgd/gdft.c       2007-04-23 17:17:47.000000000 +0200
4438 @@ -16,7 +16,9 @@
4439  #include <unistd.h>
4440  #else
4441  #include <io.h>
4442 -#define R_OK 04                        /* Needed in Windows */
4443 +#ifndef R_OK
4444 +# define R_OK 04                       /* Needed in Windows */
4445 +#endif
4446  #endif
4447  
4448  #ifdef WIN32
4449 @@ -37,9 +39,8 @@
4450  gdImageStringTTF (gdImage * im, int *brect, int fg, char *fontlist,
4451                   double ptsize, double angle, int x, int y, char *string)
4452  {
4453 -  /* 2.0.6: valid return */ 
4454 -  return gdImageStringFT (im, brect, fg, fontlist, ptsize,
4455 -                  angle, x, y, string);
4456 +       /* 2.0.6: valid return */
4457 +       return gdImageStringFT (im, brect, fg, fontlist, ptsize, angle, x, y, string);
4458  }
4459  
4460  #ifndef HAVE_LIBFREETYPE
4461 @@ -48,14 +49,14 @@
4462                  double ptsize, double angle, int x, int y, char *string,
4463                  gdFTStringExtraPtr strex)
4464  {
4465 -  return "libgd was not built with FreeType font support\n";
4466 +       return "libgd was not built with FreeType font support\n";
4467  }
4468  
4469  char *
4470  gdImageStringFT (gdImage * im, int *brect, int fg, char *fontlist,
4471                  double ptsize, double angle, int x, int y, char *string)
4472  {
4473 -  return "libgd was not built with FreeType font support\n";
4474 +       return "libgd was not built with FreeType font support\n";
4475  }
4476  #else
4477  
4478 @@ -71,8 +72,8 @@
4479  #define TWEENCOLORCACHESIZE 32
4480  
4481  /*
4482 - * Line separation as a factor of font height.  
4483 - *      No space between if LINESPACE = 1.00 
4484 + * Line separation as a factor of font height.
4485 + *      No space between if LINESPACE = 1.00
4486   *      Line separation will be rounded up to next pixel row.
4487   */
4488  #define LINESPACE 1.05
4489 @@ -115,40 +116,35 @@
4490  
4491  typedef struct
4492  {
4493 -  char *fontlist;              /* key */
4494 -  FT_Library *library;
4495 -  FT_Face face;
4496 -  FT_Bool have_char_map_unicode, have_char_map_big5, have_char_map_sjis,
4497 -    have_char_map_apple_roman;
4498 -  gdCache_head_t *glyphCache;
4499 -}
4500 -font_t;
4501 +       char *fontlist;         /* key */
4502 +       FT_Library *library;
4503 +       FT_Face face;
4504 +       FT_Bool have_char_map_unicode, have_char_map_big5, have_char_map_sjis, have_char_map_apple_roman;
4505 +       gdCache_head_t *glyphCache;
4506 +} font_t;
4507  
4508  typedef struct
4509 -  {
4510 -    char *fontlist;            /* key */
4511 -    FT_Library *library;
4512 -  }
4513 -fontkey_t;
4514 +{
4515 +       char *fontlist;         /* key */
4516 +       FT_Library *library;
4517 +} fontkey_t;
4518  
4519  typedef struct
4520 -  {
4521 -    int pixel;                 /* key */
4522 -    int bgcolor;               /* key */
4523 -    int fgcolor;               /* key *//* -ve means no antialias */
4524 -    gdImagePtr im;             /* key */
4525 -    int tweencolor;
4526 -  }
4527 -tweencolor_t;
4528 +{
4529 +       int pixel;              /* key */
4530 +       int bgcolor;            /* key */
4531 +       int fgcolor;            /* key *//* -ve means no antialias */
4532 +       gdImagePtr im;          /* key */
4533 +       int tweencolor;
4534 +} tweencolor_t;
4535  
4536  typedef struct
4537 -  {
4538 -    int pixel;                 /* key */
4539 -    int bgcolor;               /* key */
4540 -    int fgcolor;               /* key *//* -ve means no antialias */
4541 -    gdImagePtr im;             /* key */
4542 -  }
4543 -tweencolorkey_t;
4544 +{
4545 +       int pixel;              /* key */
4546 +       int bgcolor;            /* key */
4547 +       int fgcolor;            /* key *//* -ve means no antialias */
4548 +       gdImagePtr im;          /* key */
4549 +} tweencolorkey_t;
4550  
4551  /********************************************************************
4552   * gdTcl_UtfToUniChar is borrowed from Tcl ...
4553 @@ -208,160 +204,141 @@
4554  
4555  #define Tcl_UniChar int
4556  #define TCL_UTF_MAX 3
4557 -static int
4558 -gdTcl_UtfToUniChar (char *str, Tcl_UniChar * chPtr)
4559 +static int gdTcl_UtfToUniChar (char *str, Tcl_UniChar * chPtr)
4560  /* str is the UTF8 next character pointer */
4561  /* chPtr is the int for the result */
4562  {
4563 -  int byte;
4564 +       int byte;
4565 +
4566 +       /* HTML4.0 entities in decimal form, e.g. &#197; */
4567 +       byte = *((unsigned char *) str);
4568 +       if (byte == '&') {
4569 +               int i, n = 0;
4570 +
4571 +               byte = *((unsigned char *) (str + 1));
4572 +               if (byte == '#') {
4573 +                       byte = *((unsigned char *) (str + 2));
4574 +                       if (byte == 'x' || byte == 'X') {
4575 +                               for (i = 3; i < 8; i++) {
4576 +                                       byte = *((unsigned char *) (str + i));
4577 +                                       if (byte >= 'A' && byte <= 'F')
4578 +                                               byte = byte - 'A' + 10;
4579 +                                       else if (byte >= 'a' && byte <= 'f')
4580 +                                               byte = byte - 'a' + 10;
4581 +                                       else if (byte >= '0' && byte <= '9')
4582 +                                               byte = byte - '0';
4583 +                                       else
4584 +                                               break;
4585 +                                       n = (n * 16) + byte;
4586 +                               }
4587 +                       } else {
4588 +                               for (i = 2; i < 8; i++) {
4589 +                                       byte = *((unsigned char *) (str + i));
4590 +                                       if (byte >= '0' && byte <= '9') {
4591 +                                               n = (n * 10) + (byte - '0');
4592 +                                       } else {
4593 +                                               break;
4594 +                                       }
4595 +                               }
4596 +                       }
4597 +                       if (byte == ';') {
4598 +                               *chPtr = (Tcl_UniChar) n;
4599 +                               return ++i;
4600 +                       }
4601 +               }
4602 +       }
4603  
4604 -  /* HTML4.0 entities in decimal form, e.g. &#197; */
4605 -  byte = *((unsigned char *) str);
4606 -  if (byte == '&')
4607 -    {
4608 -      int i, n = 0;
4609 -
4610 -      byte = *((unsigned char *) (str + 1));
4611 -      if (byte == '#')
4612 -       {
4613 -         for (i = 2; i < 8; i++)
4614 -           {
4615 -             byte = *((unsigned char *) (str + i));
4616 -             if (byte >= '0' && byte <= '9')
4617 -               {
4618 -                 n = (n * 10) + (byte - '0');
4619 -               }
4620 -             else
4621 -               break;
4622 -           }
4623 -         if (byte == ';')
4624 -           {
4625 -             *chPtr = (Tcl_UniChar) n;
4626 -             return ++i;
4627 -           }
4628 -       }
4629 -    }
4630 -
4631 -  /*
4632 -   * Unroll 1 to 3 byte UTF-8 sequences, use loop to handle longer ones.
4633 -   */
4634 +       /* Unroll 1 to 3 byte UTF-8 sequences, use loop to handle longer ones. */
4635  
4636 -  byte = *((unsigned char *) str);
4637 +       byte = *((unsigned char *) str);
4638  #ifdef JISX0208
4639 -  if (0xA1 <= byte && byte <= 0xFE)
4640 -    {
4641 -      int ku, ten;
4642 -
4643 -      ku = (byte & 0x7F) - 0x20;
4644 -      ten = (str[1] & 0x7F) - 0x20;
4645 -      if ((ku < 1 || ku > 92) || (ten < 1 || ten > 94))
4646 -       {
4647 -         *chPtr = (Tcl_UniChar) byte;
4648 -         return 1;
4649 -       }
4650 -
4651 -      *chPtr = (Tcl_UniChar) UnicodeTbl[ku - 1][ten - 1];
4652 -      return 2;
4653 -    }
4654 -  else
4655 +       if (0xA1 <= byte && byte <= 0xFE) {
4656 +               int ku, ten;
4657 +
4658 +               ku = (byte & 0x7F) - 0x20;
4659 +               ten = (str[1] & 0x7F) - 0x20;
4660 +               if ((ku < 1 || ku > 92) || (ten < 1 || ten > 94)) {
4661 +                       *chPtr = (Tcl_UniChar) byte;
4662 +                       return 1;
4663 +               }
4664 +
4665 +               *chPtr = (Tcl_UniChar) UnicodeTbl[ku - 1][ten - 1];
4666 +               return 2;
4667 +       } else
4668  #endif /* JISX0208 */
4669 -  if (byte < 0xC0)
4670 -    {
4671 -      /*
4672 -       * Handles properly formed UTF-8 characters between
4673 -       * 0x01 and 0x7F.  Also treats \0 and naked trail
4674 -       * bytes 0x80 to 0xBF as valid characters representing
4675 -       * themselves.
4676 -       */
4677 -
4678 -      *chPtr = (Tcl_UniChar) byte;
4679 -      return 1;
4680 -    }
4681 -  else if (byte < 0xE0)
4682 -    {
4683 -      if ((str[1] & 0xC0) == 0x80)
4684 -       {
4685 -         /*
4686 -          * Two-byte-character lead-byte followed
4687 -          * by a trail-byte.
4688 -          */
4689 -
4690 -         *chPtr = (Tcl_UniChar) (((byte & 0x1F) << 6)
4691 -                                 | (str[1] & 0x3F));
4692 -         return 2;
4693 -       }
4694 -      /*
4695 -       * A two-byte-character lead-byte not followed by trail-byte
4696 -       * represents itself.
4697 -       */
4698 -
4699 -      *chPtr = (Tcl_UniChar) byte;
4700 -      return 1;
4701 -    }
4702 -  else if (byte < 0xF0)
4703 -    {
4704 -      if (((str[1] & 0xC0) == 0x80) && ((str[2] & 0xC0) == 0x80))
4705 -       {
4706 -         /*
4707 -          * Three-byte-character lead byte followed by
4708 -          * two trail bytes.
4709 -          */
4710 -
4711 -         *chPtr = (Tcl_UniChar) (((byte & 0x0F) << 12)
4712 -                               | ((str[1] & 0x3F) << 6) | (str[2] & 0x3F));
4713 -         return 3;
4714 -       }
4715 -      /*
4716 -       * A three-byte-character lead-byte not followed by
4717 -       * two trail-bytes represents itself.
4718 -       */
4719 -
4720 -      *chPtr = (Tcl_UniChar) byte;
4721 -      return 1;
4722 -    }
4723 +       if (byte < 0xC0) {
4724 +               /* Handles properly formed UTF-8 characters between
4725 +                * 0x01 and 0x7F.  Also treats \0 and naked trail
4726 +                * bytes 0x80 to 0xBF as valid characters representing
4727 +                * themselves.
4728 +                */
4729 +
4730 +               *chPtr = (Tcl_UniChar) byte;
4731 +               return 1;
4732 +       } else if (byte < 0xE0) {
4733 +               if ((str[1] & 0xC0) == 0x80) {
4734 +                       /* Two-byte-character lead-byte followed by a trail-byte. */
4735 +
4736 +                       *chPtr = (Tcl_UniChar) (((byte & 0x1F) << 6) | (str[1] & 0x3F));
4737 +                       return 2;
4738 +               }
4739 +               /*
4740 +                * A two-byte-character lead-byte not followed by trail-byte
4741 +                * represents itself.
4742 +                */
4743 +
4744 +               *chPtr = (Tcl_UniChar) byte;
4745 +               return 1;
4746 +       } else if (byte < 0xF0) {
4747 +               if (((str[1] & 0xC0) == 0x80) && ((str[2] & 0xC0) == 0x80)) {
4748 +                       /* Three-byte-character lead byte followed by two trail bytes. */
4749 +
4750 +                       *chPtr = (Tcl_UniChar) (((byte & 0x0F) << 12) | ((str[1] & 0x3F) << 6) | (str[2] & 0x3F));
4751 +                       return 3;
4752 +               }
4753 +               /* A three-byte-character lead-byte not followed by two trail-bytes represents itself. */
4754 +
4755 +               *chPtr = (Tcl_UniChar) byte;
4756 +               return 1;
4757 +       }
4758  #if TCL_UTF_MAX > 3
4759 -  else
4760 -    {
4761 -      int ch, total, trail;
4762 -
4763 -      total = totalBytes[byte];
4764 -      trail = total - 1;
4765 -      if (trail > 0)
4766 -       {
4767 -         ch = byte & (0x3F >> trail);
4768 -         do
4769 -           {
4770 -             str++;
4771 -             if ((*str & 0xC0) != 0x80)
4772 -               {
4773 -                 *chPtr = byte;
4774 -                 return 1;
4775 -               }
4776 -             ch <<= 6;
4777 -             ch |= (*str & 0x3F);
4778 -             trail--;
4779 -           }
4780 -         while (trail > 0);
4781 -         *chPtr = ch;
4782 -         return total;
4783 +       else {
4784 +               int ch, total, trail;
4785 +
4786 +               total = totalBytes[byte];
4787 +               trail = total - 1;
4788 +
4789 +               if (trail > 0) {
4790 +                       ch = byte & (0x3F >> trail);
4791 +                       do {
4792 +                               str++;
4793 +                               if ((*str & 0xC0) != 0x80) {
4794 +                                       *chPtr = byte;
4795 +                                       return 1;
4796 +                               }
4797 +                               ch <<= 6;
4798 +                               ch |= (*str & 0x3F);
4799 +                               trail--;
4800 +                       } while (trail > 0);
4801 +                       *chPtr = ch;
4802 +                       return total;
4803 +               }
4804         }
4805 -    }
4806  #endif
4807  
4808 -  *chPtr = (Tcl_UniChar) byte;
4809 -  return 1;
4810 +       *chPtr = (Tcl_UniChar) byte;
4811 +       return 1;
4812  }
4813  
4814  /********************************************************************/
4815  /* font cache functions                                             */
4816  
4817 -static int
4818 -fontTest (void *element, void *key)
4819 +static int fontTest (void *element, void *key)
4820  {
4821 -  font_t *a = (font_t *) element;
4822 -  fontkey_t *b = (fontkey_t *) key;
4823 +       font_t *a = (font_t *) element;
4824 +       fontkey_t *b = (fontkey_t *) key;
4825  
4826 -  return (strcmp (a->fontlist, b->fontlist) == 0);
4827 +       return (strcmp (a->fontlist, b->fontlist) == 0);
4828  }
4829  
4830  static void *fontFetch (char **error, void *key)
4831 @@ -389,7 +366,7 @@
4832         fontsearchpath = getenv ("GDFONTPATH");
4833         if (!fontsearchpath) {
4834                 fontsearchpath = DEFAULT_FONTPATH;
4835 -       }       
4836 +       }
4837         fontlist = gdEstrdup(a->fontlist);
4838  
4839         /*
4840 @@ -399,8 +376,12 @@
4841                 /* make a fresh copy each time - strtok corrupts it. */
4842                 path = gdEstrdup (fontsearchpath);
4843  
4844 -               /* if name is an absolute filename then test directly */ 
4845 +               /* if name is an absolute filename then test directly */
4846 +#ifdef NETWARE
4847 +               if (*name == '/' || (name[0] != 0 && strstr(name, ":/"))) {
4848 +#else
4849                 if (*name == '/' || (name[0] != 0 && name[1] == ':' && (name[2] == '/' || name[2] == '\\'))) {
4850 +#endif
4851                         snprintf(fullname, sizeof(fullname) - 1, "%s", name);
4852                         if (access(fullname, R_OK) == 0) {
4853                                 font_found++;
4854 @@ -437,15 +418,15 @@
4855                 path = NULL;
4856                 if (font_found) {
4857                         break;
4858 -               }       
4859 +               }
4860         }
4861 -  
4862 +
4863         if (path) {
4864                 gdFree(path);
4865         }
4866 -  
4867 +
4868         gdFree(fontlist);
4869 -  
4870 +
4871         if (!font_found) {
4872                 gdPFree(a->fontlist);
4873                 gdPFree(a);
4874 @@ -556,257 +537,234 @@
4875   * does the work so that text can be alpha blended across a complex
4876   * background (TBB; and for real in 2.0.2).
4877   */
4878 -static void *
4879 -tweenColorFetch (char **error, void *key)
4880 +static void * tweenColorFetch (char **error, void *key)
4881  {
4882 -  tweencolor_t *a;
4883 -  tweencolorkey_t *b = (tweencolorkey_t *) key;
4884 -  int pixel, npixel, bg, fg;
4885 -  gdImagePtr im;
4886 -
4887 -  a = (tweencolor_t *) gdMalloc (sizeof (tweencolor_t));
4888 -  pixel = a->pixel = b->pixel;
4889 -  bg = a->bgcolor = b->bgcolor;
4890 -  fg = a->fgcolor = b->fgcolor;
4891 -  im = b->im;
4892 -
4893 -  /* if fg is specified by a negative color idx, then don't antialias */
4894 -  if (fg < 0)
4895 -    {
4896 -      if ((pixel + pixel) >= NUMCOLORS)
4897 -      a->tweencolor = -fg;
4898 -      else
4899 -         a->tweencolor = bg;
4900 -    }
4901 -  else
4902 -    {
4903 -      npixel = NUMCOLORS - pixel;
4904 -      if (im->trueColor)
4905 -       {
4906 -         /* 2.0.1: use gdImageSetPixel to do the alpha blending work,
4907 -            or to just store the alpha level. All we have to do here
4908 -            is incorporate our knowledge of the percentage of this
4909 -            pixel that is really "lit" by pushing the alpha value
4910 -            up toward transparency in edge regions. */
4911 -         a->tweencolor = gdTrueColorAlpha (
4912 -                                            gdTrueColorGetRed (fg),
4913 -                                            gdTrueColorGetGreen (fg),
4914 -                                            gdTrueColorGetBlue (fg),
4915 -              gdAlphaMax - (gdTrueColorGetAlpha (fg) * pixel / NUMCOLORS));
4916 -       }
4917 -      else
4918 -       {
4919 -         a->tweencolor = gdImageColorResolve (im,
4920 -                  (pixel * im->red[fg] + npixel * im->red[bg]) / NUMCOLORS,
4921 -              (pixel * im->green[fg] + npixel * im->green[bg]) / NUMCOLORS,
4922 -               (pixel * im->blue[fg] + npixel * im->blue[bg]) / NUMCOLORS);
4923 -    }
4924 -    }
4925 -  return (void *) a;
4926 +       tweencolor_t *a;
4927 +       tweencolorkey_t *b = (tweencolorkey_t *) key;
4928 +       int pixel, npixel, bg, fg;
4929 +       gdImagePtr im;
4930 +
4931 +       a = (tweencolor_t *) gdMalloc (sizeof (tweencolor_t));
4932 +       pixel = a->pixel = b->pixel;
4933 +       bg = a->bgcolor = b->bgcolor;
4934 +       fg = a->fgcolor = b->fgcolor;
4935 +       im = a->im = b->im;
4936 +
4937 +       /* if fg is specified by a negative color idx, then don't antialias */
4938 +       if (fg < 0) {
4939 +               if ((pixel + pixel) >= NUMCOLORS) {
4940 +                       a->tweencolor = -fg;
4941 +               } else {
4942 +                       a->tweencolor = bg;
4943 +               }
4944 +       } else {
4945 +               npixel = NUMCOLORS - pixel;
4946 +               if (im->trueColor) {
4947 +                       /* 2.0.1: use gdImageSetPixel to do the alpha blending work,
4948 +                        * or to just store the alpha level. All we have to do here
4949 +                        * is incorporate our knowledge of the percentage of this
4950 +                        * pixel that is really "lit" by pushing the alpha value
4951 +                        * up toward transparency in edge regions.
4952 +                        */
4953 +                       a->tweencolor = gdTrueColorAlpha(
4954 +                                               gdTrueColorGetRed(fg),
4955 +                                               gdTrueColorGetGreen(fg),
4956 +                                               gdTrueColorGetBlue(fg),
4957 +                                               gdAlphaMax - (gdTrueColorGetAlpha (fg) * pixel / NUMCOLORS));
4958 +               } else {
4959 +                       a->tweencolor = gdImageColorResolve(im,
4960 +                                               (pixel * im->red[fg] + npixel * im->red[bg]) / NUMCOLORS,
4961 +                                               (pixel * im->green[fg] + npixel * im->green[bg]) / NUMCOLORS,
4962 +                                               (pixel * im->blue[fg] + npixel * im->blue[bg]) / NUMCOLORS);
4963 +               }
4964 +       }
4965 +       return (void *) a;
4966  }
4967  
4968 -static void
4969 -tweenColorRelease (void *element)
4970 +static void tweenColorRelease (void *element)
4971  {
4972 -  gdFree ((char *) element);
4973 +       gdFree((char *) element);
4974  }
4975  
4976  /* draw_bitmap - transfers glyph bitmap to GD image */
4977 -static char *
4978 -gdft_draw_bitmap (gdCache_head_t *tc_cache, gdImage * im, int fg, FT_Bitmap bitmap, int pen_x, int pen_y)
4979 +static char * gdft_draw_bitmap (gdCache_head_t *tc_cache, gdImage * im, int fg, FT_Bitmap bitmap, int pen_x, int pen_y)
4980  {
4981 -  unsigned char *pixel = NULL;
4982 -  int *tpixel = NULL;
4983 -  int x, y, row, col, pc, pcr;
4984 -
4985 -  tweencolor_t *tc_elem;
4986 -  tweencolorkey_t tc_key;
4987 -
4988 -  /* copy to image, mapping colors */
4989 -  tc_key.fgcolor = fg;
4990 -  tc_key.im = im;
4991 -  /* Truecolor version; does not require the cache */
4992 -  if (im->trueColor) 
4993 -    {
4994 -    for (row = 0; row < bitmap.rows; row++)
4995 -      {
4996 -        pc = row * bitmap.pitch;
4997 -        pcr = pc;
4998 -        y = pen_y + row;
4999 -        /* clip if out of bounds */
5000 -        /* 2.0.16: clipping rectangle, not image bounds */             
5001 -       if ((y > im->cy2) || (y < im->cy1))
5002 -         continue;
5003 -      for (col = 0; col < bitmap.width; col++, pc++)
5004 -       {
5005 -          int level;  
5006 -         if (bitmap.pixel_mode == ft_pixel_mode_grays)
5007 -           {
5008 -             /*
5009 -              * Scale to 128 levels of alpha for gd use.
5010 -               * alpha 0 is opacity, so be sure to invert at the end
5011 -              */
5012 -             level = (bitmap.buffer[pc] * gdAlphaMax /
5013 -                (bitmap.num_grays - 1)); 
5014 -            }
5015 -          else if (bitmap.pixel_mode == ft_pixel_mode_mono)
5016 -            {
5017 -              /* 2.0.5: mode_mono fix from Giuliano Pochini */
5018 -              level = ((bitmap.buffer[(col>>3)+pcr]) & (1<<(~col&0x07)))
5019 -               ? gdAlphaTransparent :
5020 -                gdAlphaOpaque;
5021 -            }  
5022 -          else 
5023 -           {
5024 -             return "Unsupported ft_pixel_mode";
5025 -           }
5026 -          if ((fg >= 0) && (im->trueColor)) {
5027 -            /* Consider alpha in the foreground color itself to be an
5028 -              upper bound on how opaque things get, when truecolor is
5029 -              available. Without truecolor this results in far too many
5030 -              color indexes. */ 
5031 -            level = level * (gdAlphaMax - gdTrueColorGetAlpha(fg)) / gdAlphaMax;
5032 -          }
5033 -          level = gdAlphaMax - level;  
5034 -          x = pen_x + col;
5035 -             /* clip if out of bounds */
5036 -             /* 2.0.16: clip to clipping rectangle, Matt McNabb */
5037 -               if ((x > im->cx2) || (x < im->cx1))
5038 -               continue;
5039 -             /* get pixel location in gd buffer */
5040 -           tpixel = &im->tpixels[y][x];
5041 -            if (fg < 0) {
5042 -              if (level < (gdAlphaMax / 2)) {
5043 -                *tpixel = -fg;
5044 -              }
5045 -            } else {
5046 -              if (im->alphaBlendingFlag) { 
5047 -                *tpixel = gdAlphaBlend(*tpixel, (level << 24) + (fg & 0xFFFFFF));
5048 -              } else {    
5049 -                *tpixel = (level << 24) + (fg & 0xFFFFFF);
5050 -              }
5051 -            }
5052 -         } 
5053 -        }
5054 -      return (char *) NULL;
5055 -    }
5056 -    /* Non-truecolor case, restored to its more or less original form */ 
5057 -  for (row = 0; row < bitmap.rows; row++)
5058 -    {
5059 -      int pcr;
5060 -      pc = row * bitmap.pitch;
5061 -      pcr = pc;
5062 -      if(bitmap.pixel_mode==ft_pixel_mode_mono)
5063 -             pc *= 8;    /* pc is measured in bits for monochrome images */
5064 -
5065 -      y = pen_y + row;
5066 -
5067 -      /* clip if out of bounds */
5068 -      if (y >= im->sy || y < 0)
5069 -       continue;
5070 -
5071 -      for (col = 0; col < bitmap.width; col++, pc++)
5072 -       {
5073 -         if (bitmap.pixel_mode == ft_pixel_mode_grays)
5074 -           {
5075 -             /*
5076 -              * Round to NUMCOLORS levels of antialiasing for
5077 -              * index color images since only 256 colors are
5078 -              * available.
5079 -              */
5080 -             tc_key.pixel = ((bitmap.buffer[pc] * NUMCOLORS)
5081 -                             + bitmap.num_grays / 2)
5082 -               / (bitmap.num_grays - 1);
5083 -           }
5084 -         else if (bitmap.pixel_mode == ft_pixel_mode_mono)
5085 -           {
5086 -             tc_key.pixel = ((bitmap.buffer[pc / 8]
5087 -                              << (pc % 8)) & 128) ? NUMCOLORS : 0;
5088 -              /* 2.0.5: mode_mono fix from Giuliano Pochini */
5089 -              tc_key.pixel = ((bitmap.buffer[(col>>3)+pcr]) & (1<<(~col&0x07)))
5090 -               ? NUMCOLORS : 0;
5091 -           }
5092 -         else
5093 -           {
5094 -             return "Unsupported ft_pixel_mode";
5095 -           }
5096 -         if (tc_key.pixel > 0) /* if not background */
5097 -           {                   
5098 -             x = pen_x + col;
5099 -
5100 -             /* clip if out of bounds */
5101 -             if (x >= im->sx || x < 0)
5102 -               continue;
5103 -             /* get pixel location in gd buffer */
5104 -             pixel = &im->pixels[y][x];
5105 -             if (tc_key.pixel == NUMCOLORS)
5106 -               {
5107 -                 /* use fg color directly. gd 2.0.2: watch out for
5108 -                     negative indexes (thanks to David Marwood). */ 
5109 -                   *pixel = (fg < 0) ? -fg : fg;
5110 -               }
5111 -             else
5112 -               {
5113 -                 /* find antialised color */
5114 -       
5115 -                 tc_key.bgcolor = *pixel;
5116 -                 gdMutexLock(gdFontCacheMutex);
5117 -                 tc_elem = (tweencolor_t *) gdCacheGet (tc_cache, &tc_key);
5118 -                 *pixel = tc_elem->tweencolor;
5119 -                 gdMutexUnlock(gdFontCacheMutex);
5120 +       unsigned char *pixel = NULL;
5121 +       int *tpixel = NULL;
5122 +       int x, y, row, col, pc, pcr;
5123 +
5124 +       tweencolor_t *tc_elem;
5125 +       tweencolorkey_t tc_key;
5126 +
5127 +       /* copy to image, mapping colors */
5128 +       tc_key.fgcolor = fg;
5129 +       tc_key.im = im;
5130 +       /* Truecolor version; does not require the cache */
5131 +       if (im->trueColor) {
5132 +               for (row = 0; row < bitmap.rows; row++) {
5133 +                       pc = row * bitmap.pitch;
5134 +                       pcr = pc;
5135 +                       y = pen_y + row;
5136 +                       /* clip if out of bounds */
5137 +                       /* 2.0.16: clipping rectangle, not image bounds */
5138 +                       if ((y > im->cy2) || (y < im->cy1)) {
5139 +                               continue;
5140 +                       }
5141 +                       for (col = 0; col < bitmap.width; col++, pc++) {
5142 +                               int level;
5143 +                               if (bitmap.pixel_mode == ft_pixel_mode_grays) {
5144 +                                       /* Scale to 128 levels of alpha for gd use.
5145 +                                        * alpha 0 is opacity, so be sure to invert at the end
5146 +                                        */
5147 +                                       level = (bitmap.buffer[pc] * gdAlphaMax / (bitmap.num_grays - 1));
5148 +                               } else if (bitmap.pixel_mode == ft_pixel_mode_mono) {
5149 +                                       /* 2.0.5: mode_mono fix from Giuliano Pochini */
5150 +                                       level = ((bitmap.buffer[(col>>3)+pcr]) & (1<<(~col&0x07))) ? gdAlphaTransparent : gdAlphaOpaque;
5151 +                               } else {
5152 +                                       return "Unsupported ft_pixel_mode";
5153 +                               }
5154 +                               if ((fg >= 0) && (im->trueColor)) {
5155 +                                       /* Consider alpha in the foreground color itself to be an
5156 +                                        * upper bound on how opaque things get, when truecolor is
5157 +                                        * available. Without truecolor this results in far too many
5158 +                                        * color indexes.
5159 +                                        */
5160 +                                       level = level * (gdAlphaMax - gdTrueColorGetAlpha(fg)) / gdAlphaMax;
5161 +                               }
5162 +                               level = gdAlphaMax - level;
5163 +                               x = pen_x + col;
5164 +                               /* clip if out of bounds */
5165 +                               /* 2.0.16: clip to clipping rectangle, Matt McNabb */
5166 +                               if ((x > im->cx2) || (x < im->cx1)) {
5167 +                                       continue;
5168 +                               }
5169 +                               /* get pixel location in gd buffer */
5170 +                               tpixel = &im->tpixels[y][x];
5171 +                               if (fg < 0) {
5172 +                                       if (level < (gdAlphaMax / 2)) {
5173 +                                               *tpixel = -fg;
5174 +                                       }
5175 +                               } else {
5176 +                                       if (im->alphaBlendingFlag) {
5177 +                                               *tpixel = gdAlphaBlend(*tpixel, (level << 24) + (fg & 0xFFFFFF));
5178 +                                       } else {
5179 +                                               *tpixel = (level << 24) + (fg & 0xFFFFFF);
5180 +                                       }
5181 +                               }
5182 +                       }
5183 +               }
5184 +               return (char *) NULL;
5185 +       }
5186 +       /* Non-truecolor case, restored to its more or less original form */
5187 +       for (row = 0; row < bitmap.rows; row++) {
5188 +               int pcr;
5189 +               pc = row * bitmap.pitch;
5190 +               pcr = pc;
5191 +               if (bitmap.pixel_mode==ft_pixel_mode_mono) {
5192 +                       pc *= 8;    /* pc is measured in bits for monochrome images */
5193 +               }
5194 +               y = pen_y + row;
5195 +
5196 +               /* clip if out of bounds */
5197 +               if (y >= im->sy || y < 0) {
5198 +                       continue;
5199 +               }
5200 +
5201 +               for (col = 0; col < bitmap.width; col++, pc++) {
5202 +                       if (bitmap.pixel_mode == ft_pixel_mode_grays) {
5203 +                               /*
5204 +                                * Round to NUMCOLORS levels of antialiasing for
5205 +                                * index color images since only 256 colors are
5206 +                                * available.
5207 +                                */
5208 +                               tc_key.pixel = ((bitmap.buffer[pc] * NUMCOLORS) + bitmap.num_grays / 2) / (bitmap.num_grays - 1);
5209 +                       } else if (bitmap.pixel_mode == ft_pixel_mode_mono) {
5210 +                               tc_key.pixel = ((bitmap.buffer[pc / 8] << (pc % 8)) & 128) ? NUMCOLORS : 0;
5211 +                               /* 2.0.5: mode_mono fix from Giuliano Pochini */
5212 +                               tc_key.pixel = ((bitmap.buffer[(col>>3)+pcr]) & (1<<(~col&0x07))) ? NUMCOLORS : 0;
5213 +                       } else {
5214 +                               return "Unsupported ft_pixel_mode";
5215 +                       }
5216 +                       if (tc_key.pixel > 0) { /* if not background */
5217 +                               x = pen_x + col;
5218 +
5219 +                               /* clip if out of bounds */
5220 +                               if (x >= im->sx || x < 0) {
5221 +                                       continue;
5222 +                               }
5223 +                               /* get pixel location in gd buffer */
5224 +                               pixel = &im->pixels[y][x];
5225 +                               if (tc_key.pixel == NUMCOLORS) {
5226 +                                       /* use fg color directly. gd 2.0.2: watch out for
5227 +                                        * negative indexes (thanks to David Marwood).
5228 +                                        */
5229 +                                       *pixel = (fg < 0) ? -fg : fg;
5230 +                               } else {
5231 +                                       /* find antialised color */
5232 +                                       tc_key.bgcolor = *pixel;
5233 +                                       tc_elem = (tweencolor_t *) gdCacheGet(tc_cache, &tc_key);
5234 +                                       *pixel = tc_elem->tweencolor;
5235 +                               }
5236 +                       }
5237                 }
5238 -           }
5239         }
5240 -    }
5241 -  return (char *) NULL;
5242 +       return (char *) NULL;
5243  }
5244  
5245  static int
5246  gdroundupdown (FT_F26Dot6 v1, int updown)
5247  {
5248 -  return (!updown)
5249 -    ? (v1 < 0 ? ((v1 - 63) >> 6) : v1 >> 6)
5250 -    : (v1 > 0 ? ((v1 + 63) >> 6) : v1 >> 6);
5251 +       return (!updown) ? (v1 < 0 ? ((v1 - 63) >> 6) : v1 >> 6) : (v1 > 0 ? ((v1 + 63) >> 6) : v1 >> 6);
5252  }
5253  
5254  void gdFontCacheShutdown()
5255  {
5256 +       gdMutexLock(gdFontCacheMutex);
5257 +
5258         if (fontCache) {
5259 -               gdMutexLock(gdFontCacheMutex);
5260                 gdCacheDelete(fontCache);
5261                 fontCache = NULL;
5262 -               gdMutexUnlock(gdFontCacheMutex);
5263 -               gdMutexShutdown(gdFontCacheMutex);
5264                 FT_Done_FreeType(library);
5265         }
5266 +
5267 +       gdMutexUnlock(gdFontCacheMutex);
5268  }
5269  
5270  void gdFreeFontCache()
5271  {
5272         gdFontCacheShutdown();
5273  }
5274 -  
5275 +
5276 +void gdFontCacheMutexSetup()
5277 +{
5278 +       gdMutexSetup(gdFontCacheMutex);
5279 +}
5280 +
5281 +void gdFontCacheMutexShutdown()
5282 +{
5283 +       gdMutexShutdown(gdFontCacheMutex);
5284 +}
5285 +
5286  int gdFontCacheSetup(void)
5287  {
5288         if (fontCache) {
5289                 /* Already set up */
5290                 return 0;
5291         }
5292 -       gdMutexSetup(gdFontCacheMutex);
5293         if (FT_Init_FreeType(&library)) {
5294 -               gdMutexShutdown(gdFontCacheMutex);
5295                 return -1;
5296         }
5297         fontCache = gdCacheCreate (FONTCACHESIZE, fontTest, fontFetch, fontRelease);
5298         return 0;
5299  }
5300  
5301 +
5302  /********************************************************************/
5303  /* gdImageStringFT -  render a utf8 string onto a gd image          */
5304  
5305  char *
5306 -gdImageStringFT (gdImage * im, int *brect, int fg, char *fontlist, 
5307 -                double ptsize, double angle, int x, int y, char *string)
5308 +gdImageStringFT (gdImage * im, int *brect, int fg, char *fontlist,
5309 +                double ptsize, double angle, int x, int y, char *string)
5310  {
5311         return gdImageStringFTEx(im, brect, fg, fontlist, ptsize, angle, x, y, string, 0);
5312  }
5313 @@ -856,15 +814,16 @@
5314  
5315         /***** initialize font library and font cache on first call ******/
5316  
5317 +       gdMutexLock(gdFontCacheMutex);
5318         if (!fontCache) {
5319                 if (gdFontCacheSetup() != 0) {
5320                         gdCacheDelete(tc_cache);
5321 +                       gdMutexUnlock(gdFontCacheMutex);
5322                         return "Failure to initialize font library";
5323                 }
5324         }
5325         /*****/
5326 -       
5327 -       gdMutexLock(gdFontCacheMutex);
5328 +
5329         /* get the font (via font cache) */
5330         fontkey.fontlist = fontlist;
5331         fontkey.library = &library;
5332 @@ -961,6 +920,7 @@
5333  
5334         while (*next) {
5335                 ch = *next;
5336 +
5337                 /* carriage returns */
5338                 if (ch == '\r') {
5339                         penf.x = 0;
5340 @@ -991,8 +951,9 @@
5341                         /* I do not know the significance of the constant 0xf000.
5342                          * It was determined by inspection of the character codes
5343                          * stored in Microsoft font symbol.
5344 +                        * Added by Pierre (pajoye@php.net):
5345 +                        * Convert to the Symbol glyph range only for a Symbol family member
5346                          */
5347 -                       /* Convert to the Symbol glyph range only for a Symbol family member */ 
5348                         len = gdTcl_UtfToUniChar (next, &ch);
5349                         ch |= 0xf000;
5350                         next += len;
5351 @@ -1008,7 +969,7 @@
5352                                         next += len;
5353                                 }
5354                                 break;
5355 -                       case gdFTEX_Shift_JIS: 
5356 +                       case gdFTEX_Shift_JIS:
5357                                 if (font->have_char_map_sjis) {
5358                                         unsigned char c;
5359                                         int jiscode;
5360 @@ -1063,7 +1024,7 @@
5361                 FT_Set_Transform(face, &matrix, NULL);
5362                 /* Convert character code to glyph index */
5363                 glyph_index = FT_Get_Char_Index(face, ch);
5364 -               
5365 +
5366                 /* retrieve kerning distance and move pen position */
5367                 if (use_kerning && previous && glyph_index) {
5368                         FT_Get_Kerning(face, previous, glyph_index, ft_kerning_default, &delta);
5369 diff -urN php-4.4.8.org/ext/gd/libgd/gd_gd2.c php-4.4.8/ext/gd/libgd/gd_gd2.c
5370 --- php-4.4.8.org/ext/gd/libgd/gd_gd2.c 2004-03-29 20:21:00.000000000 +0200
5371 +++ php-4.4.8/ext/gd/libgd/gd_gd2.c     2006-08-08 13:56:36.000000000 +0200
5372 @@ -26,7 +26,7 @@
5373  /* 2.11: not part of the API, as the save routine can figure it out
5374   *     from im->trueColor, and the load routine doesn't need to tell
5375   *     the end user the saved format. NOTE: adding 2 is assumed
5376 - *     to result in the correct format value for truecolor! 
5377 + *     to result in the correct format value for truecolor!
5378  */
5379  #define GD2_FMT_TRUECOLOR_RAW 3
5380  #define GD2_FMT_TRUECOLOR_COMPRESSED 4
5381 @@ -43,8 +43,7 @@
5382  {
5383         int offset;
5384         int size;
5385 -}
5386 -t_chunk_info;
5387 +} t_chunk_info;
5388  
5389  extern int _gdGetColors(gdIOCtx * in, gdImagePtr im, int gd2xFlag);
5390  extern void _gdPutColors(gdImagePtr im, gdIOCtx * out);
5391 @@ -61,7 +60,7 @@
5392         int sidx;
5393         int nc;
5394  
5395 -       GD2_DBG(php_gd_error("Reading gd2 header info\n"));
5396 +       GD2_DBG(php_gd_error("Reading gd2 header info"));
5397  
5398         for (i = 0; i < 4; i++) {
5399                 ch = gdGetC(in);
5400 @@ -72,11 +71,11 @@
5401         }
5402         id[4] = 0;
5403  
5404 -       GD2_DBG(php_gd_error("Got file code: %s\n", id));
5405 +       GD2_DBG(php_gd_error("Got file code: %s", id));
5406  
5407         /* Equiv. of 'magick'.  */
5408         if (strcmp(id, GD2_ID) != 0) {
5409 -               GD2_DBG(php_gd_error("Not a valid gd2 file\n"));
5410 +               GD2_DBG(php_gd_error("Not a valid gd2 file"));
5411                 goto fail1;
5412         }
5413  
5414 @@ -84,32 +83,32 @@
5415         if (gdGetWord(vers, in) != 1) {
5416                 goto fail1;
5417         }
5418 -       GD2_DBG(php_gd_error("Version: %d\n", *vers));
5419 +       GD2_DBG(php_gd_error("Version: %d", *vers));
5420  
5421         if ((*vers != 1) && (*vers != 2)) {
5422 -               GD2_DBG(php_gd_error("Bad version: %d\n", *vers));
5423 +               GD2_DBG(php_gd_error("Bad version: %d", *vers));
5424                 goto fail1;
5425         }
5426  
5427         /* Image Size */
5428         if (!gdGetWord(sx, in)) {
5429 -               GD2_DBG(php_gd_error("Could not get x-size\n"));
5430 +               GD2_DBG(php_gd_error("Could not get x-size"));
5431                 goto fail1;
5432         }
5433         if (!gdGetWord(sy, in)) {
5434 -               GD2_DBG(php_gd_error("Could not get y-size\n"));
5435 +               GD2_DBG(php_gd_error("Could not get y-size"));
5436                 goto fail1;
5437         }
5438 -       GD2_DBG(php_gd_error("Image is %dx%d\n", *sx, *sy));
5439 +       GD2_DBG(php_gd_error("Image is %dx%d", *sx, *sy));
5440  
5441         /* Chunk Size (pixels, not bytes!) */
5442         if (gdGetWord(cs, in) != 1) {
5443                 goto fail1;
5444         }
5445 -       GD2_DBG(php_gd_error("ChunkSize: %d\n", *cs));
5446 +       GD2_DBG(php_gd_error("ChunkSize: %d", *cs));
5447  
5448         if ((*cs < GD2_CHUNKSIZE_MIN) || (*cs > GD2_CHUNKSIZE_MAX)) {
5449 -               GD2_DBG(php_gd_error("Bad chunk size: %d\n", *cs));
5450 +               GD2_DBG(php_gd_error("Bad chunk size: %d", *cs));
5451                 goto fail1;
5452         }
5453  
5454 @@ -117,10 +116,10 @@
5455         if (gdGetWord(fmt, in) != 1) {
5456                 goto fail1;
5457         }
5458 -       GD2_DBG(php_gd_error("Format: %d\n", *fmt));
5459 +       GD2_DBG(php_gd_error("Format: %d", *fmt));
5460  
5461         if ((*fmt != GD2_FMT_RAW) && (*fmt != GD2_FMT_COMPRESSED) && (*fmt != GD2_FMT_TRUECOLOR_RAW) && (*fmt != GD2_FMT_TRUECOLOR_COMPRESSED)) {
5462 -               GD2_DBG(php_gd_error("Bad data format: %d\n", *fmt));
5463 +               GD2_DBG(php_gd_error("Bad data format: %d", *fmt));
5464                 goto fail1;
5465         }
5466  
5467 @@ -128,17 +127,17 @@
5468         if (gdGetWord(ncx, in) != 1) {
5469                 goto fail1;
5470         }
5471 -       GD2_DBG(php_gd_error("%d Chunks Wide\n", *ncx));
5472 +       GD2_DBG(php_gd_error("%d Chunks Wide", *ncx));
5473  
5474         /* # of chunks high */
5475         if (gdGetWord(ncy, in) != 1) {
5476                 goto fail1;
5477         }
5478 -       GD2_DBG(php_gd_error("%d Chunks vertically\n", *ncy));
5479 +       GD2_DBG(php_gd_error("%d Chunks vertically", *ncy));
5480  
5481         if (gd2_compressed(*fmt)) {
5482                 nc = (*ncx) * (*ncy);
5483 -               GD2_DBG(php_gd_error("Reading %d chunk index entries\n", nc));
5484 +               GD2_DBG(php_gd_error("Reading %d chunk index entries", nc));
5485                 sidx = sizeof(t_chunk_info) * nc;
5486                 if (sidx <= 0) {
5487                         goto fail1;
5488 @@ -155,7 +154,7 @@
5489                 *chunkIdx = cidx;
5490         }
5491  
5492 -       GD2_DBG(php_gd_error("gd2 header complete\n"));
5493 +       GD2_DBG(php_gd_error("gd2 header complete"));
5494  
5495         return 1;
5496  
5497 @@ -168,7 +167,7 @@
5498         gdImagePtr im;
5499  
5500         if (_gd2GetHeader (in, sx, sy, cs, vers, fmt, ncx, ncy, cidx) != 1) {
5501 -               GD2_DBG(php_gd_error("Bad GD2 header\n"));
5502 +               GD2_DBG(php_gd_error("Bad GD2 header"));
5503                 goto fail1;
5504         }
5505  
5506 @@ -178,15 +177,15 @@
5507                 im = gdImageCreate(*sx, *sy);
5508         }
5509         if (im == NULL) {
5510 -               GD2_DBG(php_gd_error("Could not create gdImage\n"));
5511 +               GD2_DBG(php_gd_error("Could not create gdImage"));
5512                 goto fail1;
5513         }
5514  
5515         if (!_gdGetColors(in, im, (*vers) == 2)) {
5516 -               GD2_DBG(php_gd_error("Could not read color palette\n"));
5517 +               GD2_DBG(php_gd_error("Could not read color palette"));
5518                 goto fail2;
5519         }
5520 -       GD2_DBG(php_gd_error("Image palette completed: %d colours\n", im->colorsTotal));
5521 +       GD2_DBG(php_gd_error("Image palette completed: %d colours", im->colorsTotal));
5522  
5523         return im;
5524  
5525 @@ -203,25 +202,25 @@
5526         int zerr;
5527  
5528         if (gdTell(in) != offset) {
5529 -               GD2_DBG(php_gd_error("Positioning in file to %d\n", offset));
5530 +               GD2_DBG(php_gd_error("Positioning in file to %d", offset));
5531                 gdSeek(in, offset);
5532         } else {
5533 -               GD2_DBG(php_gd_error("Already Positioned in file to %d\n", offset));
5534 +               GD2_DBG(php_gd_error("Already Positioned in file to %d", offset));
5535         }
5536  
5537         /* Read and uncompress an entire chunk. */
5538 -       GD2_DBG(php_gd_error("Reading file\n"));
5539 +       GD2_DBG(php_gd_error("Reading file"));
5540         if (gdGetBuf(compBuf, compSize, in) != compSize) {
5541                 return FALSE;
5542         }
5543 -       GD2_DBG(php_gd_error("Got %d bytes. Uncompressing into buffer of %d bytes\n", compSize, (int)*chunkLen));
5544 +       GD2_DBG(php_gd_error("Got %d bytes. Uncompressing into buffer of %d bytes", compSize, (int)*chunkLen));
5545         zerr = uncompress((unsigned char *) chunkBuf, chunkLen, (unsigned char *) compBuf, compSize);
5546         if (zerr != Z_OK) {
5547 -               GD2_DBG(php_gd_error("Error %d from uncompress\n", zerr));
5548 +               GD2_DBG(php_gd_error("Error %d from uncompress", zerr));
5549                 return FALSE;
5550         }
5551 -       GD2_DBG(php_gd_error("Got chunk\n"));
5552 -       
5553 +       GD2_DBG(php_gd_error("Got chunk"));
5554 +
5555         return TRUE;
5556  }
5557  
5558 @@ -291,8 +290,8 @@
5559                 }
5560                 chunkBuf = gdCalloc(chunkMax, 1);
5561                 compBuf = gdCalloc(compMax, 1);
5562 -               
5563 -               GD2_DBG(php_gd_error("Largest compressed chunk is %d bytes\n", compMax));
5564 +
5565 +               GD2_DBG(php_gd_error("Largest compressed chunk is %d bytes", compMax));
5566         }
5567  
5568         /* Read the data... */
5569 @@ -304,13 +303,13 @@
5570                                 yhi = im->sy;
5571                         }
5572  
5573 -                       GD2_DBG(php_gd_error("Processing Chunk %d (%d, %d), y from %d to %d\n", chunkNum, cx, cy, ylo, yhi));
5574 +                       GD2_DBG(php_gd_error("Processing Chunk %d (%d, %d), y from %d to %d", chunkNum, cx, cy, ylo, yhi));
5575  
5576                         if (gd2_compressed(fmt)) {
5577                                 chunkLen = chunkMax;
5578  
5579                                 if (!_gd2ReadChunk(chunkIdx[chunkNum].offset, compBuf, chunkIdx[chunkNum].size, (char *) chunkBuf, &chunkLen, in)) {
5580 -                                       GD2_DBG(php_gd_error("Error reading comproessed chunk\n"));
5581 +                                       GD2_DBG(php_gd_error("Error reading comproessed chunk"));
5582                                         goto fail2;
5583                                 }
5584  
5585 @@ -357,7 +356,7 @@
5586                 }
5587         }
5588  
5589 -       GD2_DBG(php_gd_error("Freeing memory\n"));
5590 +       GD2_DBG(php_gd_error("Freeing memory"));
5591  
5592         if (chunkBuf) {
5593                 gdFree(chunkBuf);
5594 @@ -369,7 +368,7 @@
5595                 gdFree(chunkIdx);
5596         }
5597  
5598 -       GD2_DBG(php_gd_error("Done\n"));
5599 +       GD2_DBG(php_gd_error("Done"));
5600  
5601         return im;
5602  
5603 @@ -398,7 +397,7 @@
5604         return im;
5605  }
5606  
5607 -gdImagePtr gdImageCreateFromGd2Part (FILE * inFile, int srcx, int srcy, int w, int h) 
5608 +gdImagePtr gdImageCreateFromGd2Part (FILE * inFile, int srcx, int srcy, int w, int h)
5609  {
5610         gdImagePtr im;
5611         gdIOCtx *in = gdNewFileCtx(inFile);
5612 @@ -431,6 +430,10 @@
5613  
5614         gdImagePtr im;
5615  
5616 +       if (w<1 || h <1) {
5617 +               return 0;
5618 +       }
5619 +
5620         /* The next few lines are basically copied from gd2CreateFromFile
5621          * we change the file size, so don't want to use the code directly.
5622          * but we do need to know the file size.
5623 @@ -439,7 +442,7 @@
5624                 goto fail1;
5625         }
5626  
5627 -       GD2_DBG(php_gd_error("File size is %dx%d\n", fsx, fsy));
5628 +       GD2_DBG(php_gd_error("File size is %dx%d", fsx, fsy));
5629  
5630         /* This is the difference - make a file based on size of chunks. */
5631         if (gd2_truecolor(fmt)) {
5632 @@ -454,7 +457,7 @@
5633         if (!_gdGetColors(in, im, vers == 2)) {
5634                 goto fail2;
5635         }
5636 -       GD2_DBG(php_gd_error("Image palette completed: %d colours\n", im->colorsTotal));
5637 +       GD2_DBG(php_gd_error("Image palette completed: %d colours", im->colorsTotal));
5638  
5639         /* Process the header info */
5640         nc = ncx * ncy;
5641 @@ -477,7 +480,7 @@
5642                 if (chunkMax <= 0) {
5643                         goto fail2;
5644                 }
5645 -               
5646 +
5647                 chunkBuf = gdCalloc(chunkMax, 1);
5648                 compBuf = gdCalloc(compMax, 1);
5649         }
5650 @@ -503,7 +506,7 @@
5651  
5652         /* Remember file position of image data. */
5653         dstart = gdTell(in);
5654 -       GD2_DBG(php_gd_error("Data starts at %d\n", dstart));
5655 +       GD2_DBG(php_gd_error("Data starts at %d", dstart));
5656  
5657         /* Loop through the chunks. */
5658         for (cy = scy; (cy <= ecy); cy++) {
5659 @@ -521,10 +524,10 @@
5660                                 xhi = fsx;
5661                         }
5662  
5663 -                       GD2_DBG(php_gd_error("Processing Chunk (%d, %d), from %d to %d\n", cx, cy, ylo, yhi));
5664 +                       GD2_DBG(php_gd_error("Processing Chunk (%d, %d), from %d to %d", cx, cy, ylo, yhi));
5665  
5666                         if (!gd2_compressed(fmt)) {
5667 -                               GD2_DBG(php_gd_error("Using raw format data\n"));
5668 +                               GD2_DBG(php_gd_error("Using raw format data"));
5669                                 if (im->trueColor) {
5670                                         dpos = (cy * (cs * fsx) * 4 + cx * cs * (yhi - ylo) * 4) + dstart;
5671                                 } else {
5672 @@ -533,29 +536,29 @@
5673  
5674                                 /* gd 2.0.11: gdSeek returns TRUE on success, not 0. Longstanding bug. 01/16/03 */
5675                                 if (!gdSeek(in, dpos)) {
5676 -                                       php_gd_error_ex(E_WARNING, "Error from seek: %d\n", errno);
5677 +                                       php_gd_error_ex(E_WARNING, "Error from seek: %d", errno);
5678                                         goto fail2;
5679                                 }
5680 -                               GD2_DBG(php_gd_error("Reading (%d, %d) from position %d\n", cx, cy, dpos - dstart));
5681 +                               GD2_DBG(php_gd_error("Reading (%d, %d) from position %d", cx, cy, dpos - dstart));
5682                         } else {
5683                                 chunkNum = cx + cy * ncx;
5684  
5685                                 chunkLen = chunkMax;
5686 -                               if (!_gd2ReadChunk (chunkIdx[chunkNum].offset, compBuf, chunkIdx[chunkNum].size, chunkBuf, &chunkLen, in)) {
5687 -                                       php_gd_error("Error reading comproessed chunk\n");
5688 +                               if (!_gd2ReadChunk (chunkIdx[chunkNum].offset, compBuf, chunkIdx[chunkNum].size, (char *)chunkBuf, &chunkLen, in)) {
5689 +                                       php_gd_error("Error reading comproessed chunk");
5690                                         goto fail2;
5691                                 }
5692                                 chunkPos = 0;
5693 -                               GD2_DBG(php_gd_error("Reading (%d, %d) from chunk %d\n", cx, cy, chunkNum));
5694 +                               GD2_DBG(php_gd_error("Reading (%d, %d) from chunk %d", cx, cy, chunkNum));
5695                         }
5696  
5697 -                       GD2_DBG(php_gd_error("   into (%d, %d) - (%d, %d)\n", xlo, ylo, xhi, yhi));
5698 +                       GD2_DBG(php_gd_error("   into (%d, %d) - (%d, %d)", xlo, ylo, xhi, yhi));
5699  
5700                         for (y = ylo; (y < yhi); y++) {
5701                                 for (x = xlo; x < xhi; x++) {
5702                                         if (!gd2_compressed(fmt)) {
5703                                                 if (im->trueColor) {
5704 -                                                       if (!gdGetInt(&ch, in)) {
5705 +                                                       if (!gdGetInt((int *)&ch, in)) {
5706                                                                 ch = 0;
5707                                                         }
5708                                                 } else {
5709 @@ -577,11 +580,11 @@
5710  
5711                                         /* Only use a point that is in the image. */
5712                                         if ((x >= srcx) && (x < (srcx + w)) && (x < fsx) && (x >= 0) && (y >= srcy) && (y < (srcy + h)) && (y < fsy) && (y >= 0)) {
5713 -                                               if (im->trueColor) {  
5714 +                                               if (im->trueColor) {
5715                                                         im->tpixels[y - srcy][x - srcx] = ch;
5716                                                 } else {
5717                                                         im->pixels[y - srcy][x - srcx] = ch;
5718 -                                               }   
5719 +                                               }
5720                                         }
5721                                 }
5722                         }
5723 @@ -597,7 +600,7 @@
5724         if (chunkIdx) {
5725                 gdFree(chunkIdx);
5726         }
5727 -       
5728 +
5729         return im;
5730  
5731  fail2:
5732 @@ -653,7 +656,7 @@
5733         int compMax = 0;
5734  
5735         /* Force fmt to a valid value since we don't return anything. */
5736 -       if ((fmt != GD2_FMT_RAW) && (fmt != GD2_FMT_COMPRESSED)) {      
5737 +       if ((fmt != GD2_FMT_RAW) && (fmt != GD2_FMT_COMPRESSED)) {
5738                 fmt = im->trueColor ? GD2_FMT_TRUECOLOR_COMPRESSED : GD2_FMT_COMPRESSED;
5739         }
5740         if (im->trueColor) {
5741 @@ -693,16 +696,16 @@
5742                 chunkData = safe_emalloc(cs * bytesPerPixel, cs, 0);
5743                 memset(chunkData, 0, cs * bytesPerPixel * cs);
5744                 if (compMax <= 0) {
5745 -                       goto fail;              
5746 +                       goto fail;
5747                 }
5748                 compData = gdCalloc(compMax, 1);
5749  
5750                 /* Save the file position of chunk index, and allocate enough space for
5751 -                * each chunk_info block . 
5752 +                * each chunk_info block .
5753                  */
5754                 idxPos = gdTell(out);
5755                 idxSize = ncx * ncy * sizeof(t_chunk_info);
5756 -               GD2_DBG(php_gd_error("Index size is %d\n", idxSize));
5757 +               GD2_DBG(php_gd_error("Index size is %d", idxSize));
5758                 gdSeek(out, idxPos + idxSize);
5759  
5760                 chunkIdx = safe_emalloc(idxSize, sizeof(t_chunk_info), 0);
5761 @@ -711,8 +714,8 @@
5762  
5763         _gdPutColors (im, out);
5764  
5765 -       GD2_DBG(php_gd_error("Size: %dx%d\n", im->sx, im->sy));
5766 -       GD2_DBG(php_gd_error("Chunks: %dx%d\n", ncx, ncy));
5767 +       GD2_DBG(php_gd_error("Size: %dx%d", im->sx, im->sy));
5768 +       GD2_DBG(php_gd_error("Chunks: %dx%d", ncx, ncy));
5769  
5770         for (cy = 0; (cy < ncy); cy++) {
5771                 for (cx = 0; (cx < ncx); cx++) {
5772 @@ -722,7 +725,7 @@
5773                                 yhi = im->sy;
5774                         }
5775  
5776 -                       GD2_DBG(php_gd_error("Processing Chunk (%dx%d), y from %d to %d\n", cx, cy, ylo, yhi));
5777 +                       GD2_DBG(php_gd_error("Processing Chunk (%dx%d), y from %d to %d", cx, cy, ylo, yhi));
5778                         chunkLen = 0;
5779                         for (y = ylo; (y < yhi); y++) {
5780                                 GD2_DBG(php_gd_error("y=%d: ",y));
5781 @@ -747,7 +750,7 @@
5782                                         }
5783                                 } else {
5784                                         for (x = xlo; x < xhi; x++) {
5785 -                                               GD2_DBG(php_gd_error("%d, ",x)); 
5786 +                                               GD2_DBG(php_gd_error("%d, ",x));
5787  
5788                                                 if (im->trueColor) {
5789                                                         gdPutInt(im->tpixels[y][x], out);
5790 @@ -756,21 +759,21 @@
5791                                                 }
5792                                         }
5793                                 }
5794 -                               GD2_DBG(php_gd_error("y=%d done.\n",y)); 
5795 +                               GD2_DBG(php_gd_error("y=%d done.",y));
5796                         }
5797  
5798                         if (gd2_compressed(fmt)) {
5799                                 compLen = compMax;
5800                                 if (compress((unsigned char *) &compData[0], &compLen, (unsigned char *) &chunkData[0], chunkLen) != Z_OK) {
5801 -                                       php_gd_error("Error from compressing\n");
5802 +                                       php_gd_error("Error from compressing");
5803                                 } else {
5804                                         chunkIdx[chunkNum].offset = gdTell(out);
5805                                         chunkIdx[chunkNum++].size = compLen;
5806 -                                       GD2_DBG(php_gd_error("Chunk %d size %d offset %d\n", chunkNum, chunkIdx[chunkNum - 1].size, chunkIdx[chunkNum - 1].offset));
5807 +                                       GD2_DBG(php_gd_error("Chunk %d size %d offset %d", chunkNum, chunkIdx[chunkNum - 1].size, chunkIdx[chunkNum - 1].offset));
5808  
5809                                         if (gdPutBuf (compData, compLen, out) <= 0) {
5810                                                 /* Any alternate suggestions for handling this? */
5811 -                                               php_gd_error_ex(E_WARNING, "Error %d on write\n", errno);
5812 +                                               php_gd_error_ex(E_WARNING, "Error %d on write", errno);
5813                                         }
5814                                 }
5815                         }
5816 @@ -779,29 +782,29 @@
5817  
5818         if (gd2_compressed(fmt)) {
5819                 /* Save the position, write the index, restore position (paranoia). */
5820 -               GD2_DBG(php_gd_error("Seeking %d to write index\n", idxPos));
5821 +               GD2_DBG(php_gd_error("Seeking %d to write index", idxPos));
5822                 posSave = gdTell(out);
5823                 gdSeek(out, idxPos);
5824 -               GD2_DBG(php_gd_error("Writing index\n"));
5825 +               GD2_DBG(php_gd_error("Writing index"));
5826                 for (x = 0; x < chunkNum; x++) {
5827 -                       GD2_DBG(php_gd_error("Chunk %d size %d offset %d\n", x, chunkIdx[x].size, chunkIdx[x].offset));
5828 +                       GD2_DBG(php_gd_error("Chunk %d size %d offset %d", x, chunkIdx[x].size, chunkIdx[x].offset));
5829                         gdPutInt(chunkIdx[x].offset, out);
5830                         gdPutInt(chunkIdx[x].size, out);
5831                 }
5832                 gdSeek(out, posSave);
5833         }
5834  fail:
5835 -       GD2_DBG(php_gd_error("Freeing memory\n"));
5836 +       GD2_DBG(php_gd_error("Freeing memory"));
5837         if (chunkData) {
5838                 gdFree(chunkData);
5839         }
5840 -       if (compData) { 
5841 +       if (compData) {
5842                 gdFree(compData);
5843         }
5844         if (chunkIdx) {
5845                 gdFree(chunkIdx);
5846         }
5847 -       GD2_DBG(php_gd_error("Done\n"));
5848 +       GD2_DBG(php_gd_error("Done"));
5849  }
5850  
5851  void gdImageGd2 (gdImagePtr im, FILE * outFile, int cs, int fmt)
5852 @@ -809,7 +812,7 @@
5853         gdIOCtx *out = gdNewFileCtx(outFile);
5854  
5855         _gdImageGd2(im, out, cs, fmt);
5856 -       
5857 +
5858         out->gd_free(out);
5859  }
5860  
5861 @@ -821,6 +824,6 @@
5862         _gdImageGd2(im, out, cs, fmt);
5863         rv = gdDPExtractData(out, size);
5864         out->gd_free(out);
5865 -       
5866 +
5867         return rv;
5868  }
5869 diff -urN php-4.4.8.org/ext/gd/libgd/gd_gd.c php-4.4.8/ext/gd/libgd/gd_gd.c
5870 --- php-4.4.8.org/ext/gd/libgd/gd_gd.c  2004-03-29 20:21:00.000000000 +0200
5871 +++ php-4.4.8/ext/gd/libgd/gd_gd.c      2007-08-09 16:21:38.000000000 +0200
5872 @@ -1,4 +1,3 @@
5873 -
5874  #include <stdio.h>
5875  #include <math.h>
5876  #include <string.h>
5877 @@ -58,7 +57,7 @@
5878         }
5879  
5880         GD2_DBG(printf("Pallette had %d colours (T=%d)\n", im->colorsTotal, im->transparent));
5881 -       
5882 +
5883         if (im->trueColor) {
5884                 return TRUE;
5885         }
5886 @@ -123,6 +122,9 @@
5887         } else {
5888                 im = gdImageCreate(*sx, *sy);
5889         }
5890 +       if(!im) {
5891 +               goto fail1;
5892 +       }
5893         if (!_gdGetColors(in, im, gd2xFlag)) {
5894                 goto fail2;
5895         }
5896 @@ -225,7 +227,7 @@
5897  
5898  static void _gdPutHeader (gdImagePtr im, gdIOCtx * out)
5899  {
5900 -       /* 65535 indicates this is a gd 2.x .gd file.  
5901 +       /* 65535 indicates this is a gd 2.x .gd file.
5902          * 2.0.12: 65534 indicates truecolor.
5903          */
5904         if (im->trueColor) {
5905 diff -urN php-4.4.8.org/ext/gd/libgd/gd_gif_in.c php-4.4.8/ext/gd/libgd/gd_gif_in.c
5906 --- php-4.4.8.org/ext/gd/libgd/gd_gif_in.c      2007-06-08 07:31:02.000000000 +0200
5907 +++ php-4.4.8/ext/gd/libgd/gd_gif_in.c  2007-06-07 23:07:33.000000000 +0200
5908 @@ -17,9 +17,9 @@
5909  
5910  static int set_verbose(void)
5911  {
5912 - verbose = !!getenv("GIF_VERBOSE");
5913 - verbose_set = 1;
5914 - return(verbose);
5915 +       verbose = !!getenv("GIF_VERBOSE");
5916 +       verbose_set = 1;
5917 +       return(verbose);
5918  }
5919  
5920  #else
5921 @@ -44,270 +44,313 @@
5922  #define LOCALCOLORMAP  0x80
5923  #define BitSet(byte, bit)      (((byte) & (bit)) == (bit))
5924  
5925 -#define        ReadOK(file,buffer,len) (gdGetBuf(buffer, len, file) != 0)
5926 +#define        ReadOK(file,buffer,len) (gdGetBuf(buffer, len, file) > 0)
5927  
5928  #define LM_to_uint(a,b)                        (((b)<<8)|(a))
5929  
5930  /* We may eventually want to use this information, but def it out for now */
5931  #if 0
5932  static struct {
5933 -       unsigned int    Width;
5934 -       unsigned int    Height;
5935 -       unsigned char   ColorMap[3][MAXCOLORMAPSIZE];
5936 -       unsigned int    BitPixel;
5937 -       unsigned int    ColorResolution;
5938 -       unsigned int    Background;
5939 -       unsigned int    AspectRatio;
5940 +       unsigned int    Width;
5941 +       unsigned int    Height;
5942 +       unsigned char   ColorMap[3][MAXCOLORMAPSIZE];
5943 +       unsigned int    BitPixel;
5944 +       unsigned int    ColorResolution;
5945 +       unsigned int    Background;
5946 +       unsigned int    AspectRatio;
5947  } GifScreen;
5948  #endif
5949  
5950 +#if 0
5951  static struct {
5952 -       int     transparent;
5953 -       int     delayTime;
5954 -       int     inputFlag;
5955 -       int     disposal;
5956 +       int     transparent;
5957 +       int     delayTime;
5958 +       int     inputFlag;
5959 +       int     disposal;
5960  } Gif89 = { -1, -1, -1, 0 };
5961 +#endif
5962  
5963 -static int ReadColorMap (gdIOCtx *fd, int number, unsigned char (*buffer)[256]);
5964 -static int DoExtension (gdIOCtx *fd, int label, int *Transparent);
5965 -static int GetDataBlock (gdIOCtx *fd, unsigned char *buf);
5966 -static int GetCode (gdIOCtx *fd, int code_size, int flag);
5967 -static int LWZReadByte (gdIOCtx *fd, int flag, int input_code_size);
5968 +#define STACK_SIZE ((1<<(MAX_LWZ_BITS))*2)
5969  
5970 -static void ReadImage (gdImagePtr im, gdIOCtx *fd, int len, int height, unsigned char (*cmap)[256], int interlace); /*1.4//, int ignore); */
5971 +typedef struct {
5972 +       unsigned char    buf[280];
5973 +       int              curbit, lastbit, done, last_byte;
5974 +} CODE_STATIC_DATA;
5975 +
5976 +typedef struct {
5977 +       int fresh;
5978 +       int code_size, set_code_size;
5979 +       int max_code, max_code_size;
5980 +       int firstcode, oldcode;
5981 +       int clear_code, end_code;
5982 +       int table[2][(1<< MAX_LWZ_BITS)];
5983 +       int stack[STACK_SIZE], *sp;
5984 +       CODE_STATIC_DATA scd;
5985 +} LZW_STATIC_DATA;
5986  
5987 -int ZeroDataBlock;
5988 +static int ReadColorMap (gdIOCtx *fd, int number, unsigned char (*buffer)[256]);
5989 +static int DoExtension (gdIOCtx *fd, int label, int *Transparent, int *ZeroDataBlockP);
5990 +static int GetDataBlock (gdIOCtx *fd, unsigned char *buf, int *ZeroDataBlockP);
5991 +static int GetCode (gdIOCtx *fd, CODE_STATIC_DATA *scd, int code_size, int flag, int *ZeroDataBlockP);
5992 +static int LWZReadByte (gdIOCtx *fd, LZW_STATIC_DATA *sd, char flag, int input_code_size, int *ZeroDataBlockP);
5993 +
5994 +static void ReadImage (gdImagePtr im, gdIOCtx *fd, int len, int height, unsigned char (*cmap)[256], int interlace, int *ZeroDataBlockP); /*1.4//, int ignore); */
5995  
5996 -gdImagePtr gdImageCreateFromGifSource(gdSourcePtr inSource)
5997 +gdImagePtr gdImageCreateFromGifSource(gdSourcePtr inSource) /* {{{ */
5998  {
5999 -        gdIOCtx         *in = gdNewSSCtx(inSource, NULL);
6000 -        gdImagePtr      im;
6001 +       gdIOCtx         *in = gdNewSSCtx(inSource, NULL);
6002 +       gdImagePtr      im;
6003  
6004 -        im = gdImageCreateFromGifCtx(in);
6005 +       im = gdImageCreateFromGifCtx(in);
6006  
6007 -        in->gd_free(in);
6008 +       in->gd_free(in);
6009  
6010 -        return im;
6011 +       return im;
6012  }
6013 +/* }}} */
6014  
6015 -gdImagePtr
6016 -gdImageCreateFromGif(FILE *fdFile)
6017 +gdImagePtr gdImageCreateFromGif(FILE *fdFile) /* {{{ */
6018  {
6019 -        gdIOCtx                *fd = gdNewFileCtx(fdFile);
6020 -        gdImagePtr     im = 0;
6021 +       gdIOCtx         *fd = gdNewFileCtx(fdFile);
6022 +       gdImagePtr      im = 0;
6023  
6024 -        im = gdImageCreateFromGifCtx(fd);
6025 +       im = gdImageCreateFromGifCtx(fd);
6026  
6027 -        fd->gd_free(fd);
6028 +       fd->gd_free(fd);
6029  
6030 -        return im;
6031 +       return im;
6032  }
6033 +/* }}} */
6034  
6035 -gdImagePtr
6036 -gdImageCreateFromGifCtx(gdIOCtxPtr fd)
6037 +gdImagePtr gdImageCreateFromGifCtx(gdIOCtxPtr fd) /* {{{ */
6038  {
6039 -/* 1.4       int imageNumber; */
6040 -       int BitPixel;
6041 -       int ColorResolution;
6042 -       int Background;
6043 -       int AspectRatio;
6044 -       int Transparent = (-1);
6045 -       unsigned char   buf[16];
6046 -       unsigned char   c;
6047 -       unsigned char   ColorMap[3][MAXCOLORMAPSIZE];
6048 -       unsigned char   localColorMap[3][MAXCOLORMAPSIZE];
6049 -       int             imw, imh;
6050 -       int             useGlobalColormap;
6051 -       int             bitPixel;
6052 -       int            i;
6053 -       /*1.4//int             imageCount = 0; */
6054 -       char            version[4];
6055 -
6056 -       gdImagePtr im = 0;
6057 -       ZeroDataBlock = FALSE;
6058 +       int BitPixel;
6059 +#if 0
6060 +       int ColorResolution;
6061 +       int Background;
6062 +       int AspectRatio;
6063 +#endif
6064 +       int Transparent = (-1);
6065 +       unsigned char   buf[16];
6066 +       unsigned char   c;
6067 +       unsigned char   ColorMap[3][MAXCOLORMAPSIZE];
6068 +       unsigned char   localColorMap[3][MAXCOLORMAPSIZE];
6069 +       int             imw, imh, screen_width, screen_height;
6070 +       int             gif87a, useGlobalColormap;
6071 +       int             bitPixel;
6072 +       int            i;
6073 +       /*1.4//int             imageCount = 0; */
6074 +
6075 +       int ZeroDataBlock = FALSE;
6076 +       int haveGlobalColormap;
6077 +       gdImagePtr im = 0;
6078  
6079 -       /*1.4//imageNumber = 1; */
6080 -       if (! ReadOK(fd,buf,6)) {
6081 +       /*1.4//imageNumber = 1; */
6082 +       if (! ReadOK(fd,buf,6)) {
6083                 return 0;
6084         }
6085 -       if (strncmp((char *)buf,"GIF",3) != 0) {
6086 +       if (strncmp((char *)buf,"GIF",3) != 0) {
6087                 return 0;
6088         }
6089 -       strncpy(version, (char *)buf + 3, 3);
6090 -       version[3] = '\0';
6091  
6092 -       if ((strcmp(version, "87a") != 0) && (strcmp(version, "89a") != 0)) {
6093 +       if (memcmp((char *)buf+3, "87a", 3) == 0) {
6094 +               gif87a = 1;
6095 +       } else if (memcmp((char *)buf+3, "89a", 3) == 0) {
6096 +               gif87a = 0;
6097 +       } else {
6098                 return 0;
6099         }
6100 -       if (! ReadOK(fd,buf,7)) {
6101 +
6102 +       if (! ReadOK(fd,buf,7)) {
6103                 return 0;
6104         }
6105 -       BitPixel        = 2<<(buf[4]&0x07);
6106 -       ColorResolution = (int) (((buf[4]&0x70)>>3)+1);
6107 -       Background      = buf[5];
6108 -       AspectRatio     = buf[6];
6109  
6110 -               imw = LM_to_uint(buf[0],buf[1]);
6111 -               imh = LM_to_uint(buf[2],buf[3]);
6112 +       BitPixel        = 2<<(buf[4]&0x07);
6113 +#if 0
6114 +       ColorResolution = (int) (((buf[4]&0x70)>>3)+1);
6115 +       Background      = buf[5];
6116 +       AspectRatio     = buf[6];
6117 +#endif
6118 +       screen_width = imw = LM_to_uint(buf[0],buf[1]);
6119 +       screen_height = imh = LM_to_uint(buf[2],buf[3]);
6120  
6121 -               if (BitSet(buf[4], LOCALCOLORMAP)) {    /* Global Colormap */
6122 -               if (ReadColorMap(fd, BitPixel, ColorMap)) {
6123 +       haveGlobalColormap = BitSet(buf[4], LOCALCOLORMAP);    /* Global Colormap */
6124 +       if (haveGlobalColormap) {
6125 +               if (ReadColorMap(fd, BitPixel, ColorMap)) {
6126                         return 0;
6127                 }
6128 -       }
6129 -       for (;;) {
6130 -               if (! ReadOK(fd,&c,1)) {
6131 -                       return 0;
6132 -               }
6133 -               if (c == ';') {         /* GIF terminator */
6134 +       }
6135 +
6136 +       for (;;) {
6137 +               int top, left;
6138 +               int width, height;
6139 +
6140 +               if (! ReadOK(fd,&c,1)) {
6141 +                       return 0;
6142 +               }
6143 +               if (c == ';') {         /* GIF terminator */
6144                         goto terminated;
6145 -              }
6146 +               }
6147 +
6148 +               if (c == '!') {         /* Extension */
6149 +                       if (! ReadOK(fd,&c,1)) {
6150 +                               return 0;
6151 +                       }
6152 +                       DoExtension(fd, c, &Transparent, &ZeroDataBlock);
6153 +                       continue;
6154 +               }
6155  
6156 -               if (c == '!') {         /* Extension */
6157 -                       if (! ReadOK(fd,&c,1)) {
6158 -                               return 0;
6159 -                       }
6160 -                       DoExtension(fd, c, &Transparent);
6161 -                       continue;
6162 -               }
6163 -
6164 -               if (c != ',') {         /* Not a valid start character */
6165 -                       continue;
6166 -               }
6167 -
6168 -               /*1.4//++imageCount; */
6169 -
6170 -               if (! ReadOK(fd,buf,9)) {
6171 -                      return 0;
6172 -               }
6173 -
6174 -               useGlobalColormap = ! BitSet(buf[8], LOCALCOLORMAP);
6175 -
6176 -               bitPixel = 1<<((buf[8]&0x07)+1);
6177 -
6178 -                          if (!useGlobalColormap) {
6179 -                                  if (ReadColorMap(fd, bitPixel, localColorMap)) {
6180 -                                          return 0;
6181 -                                  }
6182 -                          }
6183 -                          
6184 -              if (!(im = gdImageCreate(imw, imh))) {
6185 -                        return 0;
6186 -              }
6187 -               im->interlace = BitSet(buf[8], INTERLACE);
6188 -               if (! useGlobalColormap) {
6189 -                       ReadImage(im, fd, imw, imh, localColorMap, 
6190 -                                 BitSet(buf[8], INTERLACE)); 
6191 -                                 /*1.4//imageCount != imageNumber); */
6192 -               } else {
6193 -                       ReadImage(im, fd, imw, imh,
6194 -                                 ColorMap, 
6195 -                                 BitSet(buf[8], INTERLACE));
6196 -                                 /*1.4//imageCount != imageNumber); */
6197 -               }
6198 -               if (Transparent != (-1)) {
6199 -                       gdImageColorTransparent(im, Transparent);
6200 -               }          
6201 -              goto terminated;
6202 -       }
6203 +               if (c != ',') {         /* Not a valid start character */
6204 +                       continue;
6205 +               }
6206  
6207 -terminated:
6208 -       /* Terminator before any image was declared! */
6209 -       if (!im) {
6210 -               return 0;
6211 -       }
6212 +               /*1.4//++imageCount; */
6213  
6214 -               if (!im->colorsTotal) {
6215 -                       gdImageDestroy(im);
6216 +               if (! ReadOK(fd,buf,9)) {
6217                         return 0;
6218                 }
6219  
6220 -       /* Check for open colors at the end, so
6221 -          we can reduce colorsTotal and ultimately
6222 -          BitsPerPixel */
6223 -       for (i=((im->colorsTotal-1)); (i>=0); i--) {
6224 +               useGlobalColormap = ! BitSet(buf[8], LOCALCOLORMAP);
6225 +
6226 +               bitPixel = 1<<((buf[8]&0x07)+1);
6227 +               left = LM_to_uint(buf[0], buf[1]);
6228 +               top = LM_to_uint(buf[2], buf[3]);
6229 +               width = LM_to_uint(buf[4], buf[5]);
6230 +               height = LM_to_uint(buf[6], buf[7]);
6231 +
6232 +               if (left + width > screen_width || top + height > screen_height) {
6233 +                       if (VERBOSE) {
6234 +                               printf("Frame is not confined to screen dimension.\n");
6235 +                       }
6236 +                       return 0;
6237 +               }
6238 +
6239 +               if (!(im = gdImageCreate(width, height))) {
6240 +                       return 0;
6241 +               }
6242 +               im->interlace = BitSet(buf[8], INTERLACE);
6243 +               if (!useGlobalColormap) {
6244 +                       if (ReadColorMap(fd, bitPixel, localColorMap)) { 
6245 +                               gdImageDestroy(im);
6246 +                               return 0;
6247 +                       }
6248 +                       ReadImage(im, fd, width, height, localColorMap, 
6249 +                                       BitSet(buf[8], INTERLACE), &ZeroDataBlock);
6250 +               } else {
6251 +                       if (!haveGlobalColormap) {
6252 +                               gdImageDestroy(im);
6253 +                               return 0;
6254 +                       }
6255 +                       ReadImage(im, fd, width, height,
6256 +                                               ColorMap, 
6257 +                                               BitSet(buf[8], INTERLACE), &ZeroDataBlock);
6258 +               }
6259 +               if (Transparent != (-1)) {
6260 +                       gdImageColorTransparent(im, Transparent);
6261 +               }
6262 +               goto terminated;
6263 +       }
6264 +
6265 +terminated:
6266 +       /* Terminator before any image was declared! */
6267 +       if (!im) {
6268 +               return 0;
6269 +       }
6270 +       if (!im->colorsTotal) {
6271 +               gdImageDestroy(im);
6272 +               return 0;
6273 +       }
6274 +       /* Check for open colors at the end, so
6275 +          we can reduce colorsTotal and ultimately
6276 +          BitsPerPixel */
6277 +       for (i=((im->colorsTotal-1)); (i>=0); i--) {
6278                 if (im->open[i]) {
6279 -                       im->colorsTotal--;
6280 -                } else {
6281 -                       break;
6282 -                }
6283 -       } 
6284 -       return im;
6285 +                       im->colorsTotal--;
6286 +               } else {
6287 +                       break;
6288 +               }
6289 +       }
6290 +       return im;
6291  }
6292 +/* }}} */
6293  
6294 -static int
6295 -ReadColorMap(gdIOCtx *fd, int number, unsigned char (*buffer)[256])
6296 +static int ReadColorMap(gdIOCtx *fd, int number, unsigned char (*buffer)[256]) /* {{{ */
6297  {
6298 -       int             i;
6299 -       unsigned char   rgb[3];
6300 +       int             i;
6301 +       unsigned char   rgb[3];
6302  
6303  
6304 -       for (i = 0; i < number; ++i) {
6305 -               if (! ReadOK(fd, rgb, sizeof(rgb))) {
6306 -                       return TRUE;
6307 -               }
6308 -               buffer[CM_RED][i] = rgb[0] ;
6309 -               buffer[CM_GREEN][i] = rgb[1] ;
6310 -               buffer[CM_BLUE][i] = rgb[2] ;
6311 -       }
6312 +       for (i = 0; i < number; ++i) {
6313 +               if (! ReadOK(fd, rgb, sizeof(rgb))) {
6314 +                       return TRUE;
6315 +               }
6316 +               buffer[CM_RED][i] = rgb[0] ;
6317 +               buffer[CM_GREEN][i] = rgb[1] ;
6318 +               buffer[CM_BLUE][i] = rgb[2] ;
6319 +       }
6320  
6321  
6322 -       return FALSE;
6323 +       return FALSE;
6324  }
6325 +/* }}} */
6326  
6327  static int
6328 -DoExtension(gdIOCtx *fd, int label, int *Transparent)
6329 +DoExtension(gdIOCtx *fd, int label, int *Transparent, int *ZeroDataBlockP)
6330  {
6331 -       static unsigned char     buf[256];
6332 +       unsigned char buf[256];
6333 +
6334 +       switch (label) {
6335 +               case 0xf9:              /* Graphic Control Extension */
6336 +                       memset(buf, 0, 4); /* initialize a few bytes in the case the next function fails */
6337 +               (void) GetDataBlock(fd, (unsigned char*) buf, ZeroDataBlockP);
6338 +#if 0
6339 +                       Gif89.disposal    = (buf[0] >> 2) & 0x7;
6340 +                       Gif89.inputFlag   = (buf[0] >> 1) & 0x1;
6341 +                       Gif89.delayTime   = LM_to_uint(buf[1],buf[2]);
6342 +#endif
6343 +                       if ((buf[0] & 0x1) != 0)
6344 +                               *Transparent = buf[3];
6345  
6346 -       switch (label) {
6347 -       case 0xf9:              /* Graphic Control Extension */
6348 -               (void) GetDataBlock(fd, (unsigned char*) buf);
6349 -               Gif89.disposal    = (buf[0] >> 2) & 0x7;
6350 -               Gif89.inputFlag   = (buf[0] >> 1) & 0x1;
6351 -               Gif89.delayTime   = LM_to_uint(buf[1],buf[2]);
6352 -               if ((buf[0] & 0x1) != 0)
6353 -                       *Transparent = buf[3];
6354 -
6355 -               while (GetDataBlock(fd, (unsigned char*) buf) > 0)
6356 -                       ;
6357 -               return FALSE;
6358 -       default:
6359 -               break;
6360 -       }
6361 -       while (GetDataBlock(fd, (unsigned char*) buf) > 0)
6362 -               ;
6363 +                       while (GetDataBlock(fd, (unsigned char*) buf, ZeroDataBlockP) > 0);
6364 +                       return FALSE;
6365 +               default:
6366 +                       break;
6367 +       }
6368 +       while (GetDataBlock(fd, (unsigned char*) buf, ZeroDataBlockP) > 0)
6369 +               ;
6370  
6371 -       return FALSE;
6372 +       return FALSE;
6373  }
6374 +/* }}} */
6375  
6376  static int
6377 -GetDataBlock_(gdIOCtx *fd, unsigned char *buf)
6378 +GetDataBlock_(gdIOCtx *fd, unsigned char *buf, int *ZeroDataBlockP)
6379  {
6380 -       unsigned char   count;
6381 +       unsigned char   count;
6382  
6383 -       if (! ReadOK(fd,&count,1)) {
6384 -               return -1;
6385 -       }
6386 +       if (! ReadOK(fd,&count,1)) {
6387 +               return -1;
6388 +       }
6389  
6390 -       ZeroDataBlock = count == 0;
6391 +       *ZeroDataBlockP = count == 0;
6392  
6393 -       if ((count != 0) && (! ReadOK(fd, buf, count))) {
6394 -               return -1;
6395 -       }
6396 +       if ((count != 0) && (! ReadOK(fd, buf, count))) {
6397 +               return -1;
6398 +       }
6399  
6400 -       return count;
6401 +       return count;
6402  }
6403 +/* }}} */
6404  
6405  static int
6406 -GetDataBlock(gdIOCtx *fd, unsigned char *buf)
6407 +GetDataBlock(gdIOCtx *fd, unsigned char *buf, int *ZeroDataBlockP)
6408  {
6409         int rv;
6410         int i;
6411 -       char *tmp = NULL;
6412 -       
6413 -       rv = GetDataBlock_(fd,buf);
6414 +
6415 +       rv = GetDataBlock_(fd,buf, ZeroDataBlockP);
6416         if (VERBOSE) {
6417 +               char *tmp = NULL;
6418                 if (rv > 0) {
6419                         tmp = safe_emalloc(3 * rv, sizeof(char), 1);
6420                         for (i=0;i<rv;i++) {
6421 @@ -321,281 +364,275 @@
6422         }
6423         return(rv);
6424  }
6425 +/* }}} */
6426  
6427  static int
6428 -GetCode_(gdIOCtx *fd, int code_size, int flag)
6429 +GetCode_(gdIOCtx *fd, CODE_STATIC_DATA *scd, int code_size, int flag, int *ZeroDataBlockP)
6430  {
6431 -       static unsigned char    buf[280];
6432 -       static int              curbit, lastbit, done, last_byte;
6433 -       int                     i, j, ret;
6434 -       unsigned char           count;
6435 -
6436 -       if (flag) {
6437 -               curbit = 0;
6438 -               lastbit = 0;
6439 -               done = FALSE;
6440 -               return 0;
6441 -       }
6442 -
6443 -       if ( (curbit+code_size) >= lastbit) {
6444 -               if (done) {
6445 -                       if (curbit >= lastbit) {
6446 -                                /* Oh well */
6447 -                       }                        
6448 -                       return -1;
6449 -               }
6450 -               buf[0] = buf[last_byte-2];
6451 -               buf[1] = buf[last_byte-1];
6452 -
6453 -               if ((count = GetDataBlock(fd, &buf[2])) <= 0)
6454 -                       done = TRUE;
6455 -
6456 -               last_byte = 2 + count;
6457 -               curbit = (curbit - lastbit) + 16;
6458 -               lastbit = (2+count)*8 ;
6459 -       }
6460 -
6461 -       ret = 0;
6462 -       for (i = curbit, j = 0; j < code_size; ++i, ++j)
6463 -               ret |= ((buf[ i / 8 ] & (1 << (i % 8))) != 0) << j;
6464 +       int           i, j, ret;
6465 +       unsigned char count;
6466 +
6467 +       if (flag) {
6468 +               scd->curbit = 0;
6469 +               scd->lastbit = 0;
6470 +               scd->last_byte = 0;
6471 +               scd->done = FALSE;
6472 +               return 0;
6473 +       }
6474 +
6475 +       if ( (scd->curbit + code_size) >= scd->lastbit) {
6476 +               if (scd->done) {
6477 +                       if (scd->curbit >= scd->lastbit) {
6478 +                               /* Oh well */
6479 +                       }
6480 +                       return -1;
6481 +               }
6482 +               scd->buf[0] = scd->buf[scd->last_byte-2];
6483 +               scd->buf[1] = scd->buf[scd->last_byte-1];
6484 +
6485 +               if ((count = GetDataBlock(fd, &scd->buf[2], ZeroDataBlockP)) <= 0)
6486 +                       scd->done = TRUE;
6487 +
6488 +               scd->last_byte = 2 + count;
6489 +               scd->curbit = (scd->curbit - scd->lastbit) + 16;
6490 +               scd->lastbit = (2+count)*8 ;
6491 +       }
6492  
6493 -       curbit += code_size;
6494 -       return ret;
6495 +       ret = 0;
6496 +       for (i = scd->curbit, j = 0; j < code_size; ++i, ++j)
6497 +               ret |= ((scd->buf[ i / 8 ] & (1 << (i % 8))) != 0) << j;
6498 +
6499 +       scd->curbit += code_size;
6500 +       return ret;
6501  }
6502  
6503  static int
6504 -GetCode(gdIOCtx *fd, int code_size, int flag)
6505 +GetCode(gdIOCtx *fd, CODE_STATIC_DATA *scd, int code_size, int flag, int *ZeroDataBlockP)
6506  {
6507 - int rv;
6508 +       int rv;
6509  
6510 - rv = GetCode_(fd,code_size,flag);
6511 - if (VERBOSE) php_gd_error_ex(E_NOTICE, "[GetCode(,%d,%d) returning %d]\n",code_size,flag,rv);
6512 - return(rv);
6513 + rv = GetCode_(fd, scd, code_size,flag, ZeroDataBlockP);
6514 + if (VERBOSE) printf("[GetCode(,%d,%d) returning %d]\n",code_size,flag,rv);
6515 +       return(rv);
6516  }
6517 +/* }}} */
6518  
6519 -#define STACK_SIZE ((1<<(MAX_LWZ_BITS))*2)
6520  static int
6521 -LWZReadByte_(gdIOCtx *fd, int flag, int input_code_size)
6522 +LWZReadByte_(gdIOCtx *fd, LZW_STATIC_DATA *sd, char flag, int input_code_size, int *ZeroDataBlockP)
6523  {
6524 -       static int      fresh = FALSE;
6525 -       int             code, incode;
6526 -       static int      code_size, set_code_size;
6527 -       static int      max_code, max_code_size;
6528 -       static int      firstcode, oldcode;
6529 -       static int      clear_code, end_code;
6530 -       static int      table[2][(1<< MAX_LWZ_BITS)];
6531 -       static int      stack[STACK_SIZE], *sp;
6532 -       register int    i;
6533 -
6534 -       if (flag) {
6535 -               set_code_size = input_code_size;
6536 -               code_size = set_code_size+1;
6537 -               clear_code = 1 << set_code_size ;
6538 -               end_code = clear_code + 1;
6539 -               max_code_size = 2*clear_code;
6540 -               max_code = clear_code+2;
6541 -
6542 -               GetCode(fd, 0, TRUE);
6543 -               
6544 -               fresh = TRUE;
6545 -
6546 -               for (i = 0; i < clear_code; ++i) {
6547 -                       table[0][i] = 0;
6548 -                       table[1][i] = i;
6549 -               }
6550 -               for (; i < (1<<MAX_LWZ_BITS); ++i)
6551 -                       table[0][i] = table[1][0] = 0;
6552 -
6553 -               sp = stack;
6554 -
6555 -               return 0;
6556 -       } else if (fresh) {
6557 -               fresh = FALSE;
6558 -               do {
6559 -                       firstcode = oldcode =
6560 -                               GetCode(fd, code_size, FALSE);
6561 -               } while (firstcode == clear_code);
6562 -               return firstcode;
6563 -       }
6564 -
6565 -       if (sp > stack)
6566 -               return *--sp;
6567 -
6568 -       while ((code = GetCode(fd, code_size, FALSE)) >= 0) {
6569 -               if (code == clear_code) {
6570 -                       for (i = 0; i < clear_code; ++i) {
6571 -                               table[0][i] = 0;
6572 -                               table[1][i] = i;
6573 -                       }
6574 -                       for (; i < (1<<MAX_LWZ_BITS); ++i)
6575 -                               table[0][i] = table[1][i] = 0;
6576 -                       code_size = set_code_size+1;
6577 -                       max_code_size = 2*clear_code;
6578 -                       max_code = clear_code+2;
6579 -                       sp = stack;
6580 -                       firstcode = oldcode =
6581 -                                       GetCode(fd, code_size, FALSE);
6582 -                       return firstcode;
6583 -               } else if (code == end_code) {
6584 -                       int             count;
6585 -                       unsigned char   buf[260];
6586 -
6587 -                       if (ZeroDataBlock)
6588 -                               return -2;
6589 -
6590 -                       while ((count = GetDataBlock(fd, buf)) > 0)
6591 -                               ;
6592 -
6593 -                       if (count != 0)
6594 -                       return -2;
6595 -               }
6596 -
6597 -               incode = code;
6598 -
6599 -              if (sp == (stack + STACK_SIZE)) {
6600 -                      /* Bad compressed data stream */
6601 -                      return -1;
6602 -              }
6603 -
6604 -               if (code >= max_code) {
6605 -                       *sp++ = firstcode;
6606 -                       code = oldcode;
6607 -               }
6608 -
6609 -               while (code >= clear_code) {
6610 -                      if (sp == (stack + STACK_SIZE)) {
6611 -                              /* Bad compressed data stream */
6612 -                              return -1;
6613 -                      }
6614 -                       *sp++ = table[1][code];
6615 -                       if (code == table[0][code]) {
6616 -                               /* Oh well */
6617 -                       }
6618 -                       code = table[0][code];
6619 -               }
6620 -
6621 -               *sp++ = firstcode = table[1][code];
6622 -
6623 -               if ((code = max_code) <(1<<MAX_LWZ_BITS)) {
6624 -                       table[0][code] = oldcode;
6625 -                       table[1][code] = firstcode;
6626 -                       ++max_code;
6627 -                       if ((max_code >= max_code_size) &&
6628 -                               (max_code_size < (1<<MAX_LWZ_BITS))) {
6629 -                               max_code_size *= 2;
6630 -                               ++code_size;
6631 -                       }
6632 -               }
6633 -
6634 -               oldcode = incode;
6635 -
6636 -               if (sp > stack)
6637 -                       return *--sp;
6638 -       }
6639 -       return code;
6640 +       int code, incode, i;
6641 +
6642 +       if (flag) {
6643 +               sd->set_code_size = input_code_size;
6644 +               sd->code_size = sd->set_code_size+1;
6645 +               sd->clear_code = 1 << sd->set_code_size ;
6646 +               sd->end_code = sd->clear_code + 1;
6647 +               sd->max_code_size = 2*sd->clear_code;
6648 +               sd->max_code = sd->clear_code+2;
6649 +
6650 +               GetCode(fd, &sd->scd, 0, TRUE, ZeroDataBlockP);
6651 +
6652 +               sd->fresh = TRUE;
6653 +
6654 +               for (i = 0; i < sd->clear_code; ++i) {
6655 +                       sd->table[0][i] = 0;
6656 +                       sd->table[1][i] = i;
6657 +               }
6658 +               for (; i < (1<<MAX_LWZ_BITS); ++i)
6659 +                       sd->table[0][i] = sd->table[1][0] = 0;
6660 +
6661 +               sd->sp = sd->stack;
6662 +
6663 +               return 0;
6664 +       } else if (sd->fresh) {
6665 +               sd->fresh = FALSE;
6666 +               do {
6667 +                       sd->firstcode = sd->oldcode =
6668 +                       GetCode(fd, &sd->scd, sd->code_size, FALSE, ZeroDataBlockP);
6669 +               } while (sd->firstcode == sd->clear_code);
6670 +               return sd->firstcode;
6671 +       }
6672 +
6673 +       if (sd->sp > sd->stack)
6674 +               return *--sd->sp;
6675 +
6676 +               while ((code = GetCode(fd, &sd->scd, sd->code_size, FALSE, ZeroDataBlockP)) >= 0) {
6677 +               if (code == sd->clear_code) {
6678 +                       for (i = 0; i < sd->clear_code; ++i) {
6679 +                               sd->table[0][i] = 0;
6680 +                               sd->table[1][i] = i;
6681 +                       }
6682 +                       for (; i < (1<<MAX_LWZ_BITS); ++i)
6683 +                               sd->table[0][i] = sd->table[1][i] = 0;
6684 +                       sd->code_size = sd->set_code_size+1;
6685 +                       sd->max_code_size = 2*sd->clear_code;
6686 +                       sd->max_code = sd->clear_code+2;
6687 +                       sd->sp = sd->stack;
6688 +                       sd->firstcode = sd->oldcode =
6689 +                                                               GetCode(fd, &sd->scd, sd->code_size, FALSE, ZeroDataBlockP);
6690 +                       return sd->firstcode;
6691 +               } else if (code == sd->end_code) {
6692 +                       int             count;
6693 +                       unsigned char   buf[260];
6694 +
6695 +                       if (*ZeroDataBlockP)
6696 +                               return -2;
6697 +
6698 +                       while ((count = GetDataBlock(fd, buf, ZeroDataBlockP)) > 0)
6699 +                               ;
6700 +
6701 +                       if (count != 0)
6702 +                               return -2;
6703 +               }
6704 +
6705 +               incode = code;
6706 +
6707 +               if (sd->sp == (sd->stack + STACK_SIZE)) {
6708 +                       /* Bad compressed data stream */
6709 +                       return -1;
6710 +               }
6711 +
6712 +               if (code >= sd->max_code) {
6713 +                       *sd->sp++ = sd->firstcode;
6714 +                       code = sd->oldcode;
6715 +               }
6716 +
6717 +               while (code >= sd->clear_code) {
6718 +                       if (sd->sp == (sd->stack + STACK_SIZE)) {
6719 +                               /* Bad compressed data stream */
6720 +                               return -1;
6721 +                       }
6722 +                       *sd->sp++ = sd->table[1][code];
6723 +                       if (code == sd->table[0][code]) {
6724 +                               /* Oh well */
6725 +                       }
6726 +                       code = sd->table[0][code];
6727 +               }
6728 +
6729 +               *sd->sp++ = sd->firstcode = sd->table[1][code];
6730 +
6731 +               if ((code = sd->max_code) <(1<<MAX_LWZ_BITS)) {
6732 +                       sd->table[0][code] = sd->oldcode;
6733 +                       sd->table[1][code] = sd->firstcode;
6734 +                       ++sd->max_code;
6735 +                       if ((sd->max_code >= sd->max_code_size) &&
6736 +                                       (sd->max_code_size < (1<<MAX_LWZ_BITS))) {
6737 +                               sd->max_code_size *= 2;
6738 +                               ++sd->code_size;
6739 +                       }
6740 +               }
6741 +
6742 +               sd->oldcode = incode;
6743 +
6744 +               if (sd->sp > sd->stack)
6745 +                       return *--sd->sp;
6746 +       }
6747 +       return code;
6748  }
6749 +/* }}} */
6750  
6751  static int
6752 -LWZReadByte(gdIOCtx *fd, int flag, int input_code_size)
6753 +LWZReadByte(gdIOCtx *fd, LZW_STATIC_DATA *sd, char flag, int input_code_size, int *ZeroDataBlockP)
6754  {
6755 - int rv;
6756 +       int rv;
6757  
6758 - rv = LWZReadByte_(fd,flag,input_code_size);
6759 - if (VERBOSE) php_gd_error_ex(E_NOTICE, "[LWZReadByte(,%d,%d) returning %d]\n",flag,input_code_size,rv);
6760 - return(rv);
6761 + rv = LWZReadByte_(fd, sd, flag, input_code_size, ZeroDataBlockP);
6762 + if (VERBOSE) printf("[LWZReadByte(,%d,%d) returning %d]\n",flag,input_code_size,rv);
6763 +       return(rv);
6764  }
6765 +/* }}} */
6766  
6767  static void
6768 -ReadImage(gdImagePtr im, gdIOCtx *fd, int len, int height, unsigned char (*cmap)[256], int interlace) /*1.4//, int ignore) */
6769 +ReadImage(gdImagePtr im, gdIOCtx *fd, int len, int height, unsigned char (*cmap)[256], int interlace, int *ZeroDataBlockP) /*1.4//, int ignore) */
6770  {
6771 -       unsigned char   c;      
6772 -       int             v;
6773 -       int             xpos = 0, ypos = 0, pass = 0;
6774 -       int i;
6775 -
6776 -          /*
6777 -               **  Initialize the Compression routines
6778 -               */
6779 -          if (! ReadOK(fd,&c,1)) {
6780 -                  return;
6781 -          }
6782 -
6783 -          if (c > MAX_LWZ_BITS) {
6784 -                  return;      
6785 -          }
6786 -
6787 -
6788 -       /* Stash the color map into the image */
6789 -       for (i=0; (i<gdMaxColors); i++) {
6790 -               im->red[i] = cmap[CM_RED][i];   
6791 -               im->green[i] = cmap[CM_GREEN][i];       
6792 -               im->blue[i] = cmap[CM_BLUE][i]; 
6793 -               im->open[i] = 1;
6794 -       }
6795 -       /* Many (perhaps most) of these colors will remain marked open. */
6796 -       im->colorsTotal = gdMaxColors;
6797 -
6798 -       if (LWZReadByte(fd, TRUE, c) < 0) {
6799 -               return;
6800 -       }
6801 -
6802 -       /*
6803 -       **  If this is an "uninteresting picture" ignore it.
6804 -       **  REMOVED For 1.4
6805 -       */
6806 -       /*if (ignore) { */
6807 -       /*        while (LWZReadByte(fd, FALSE, c) >= 0) */
6808 -       /*                ; */
6809 -       /*        return; */
6810 -       /*} */
6811 -
6812 -       while ((v = LWZReadByte(fd,FALSE,c)) >= 0 ) {
6813 -                               if (v >= gdMaxColors) {
6814 -                                       v = 0;
6815 +       unsigned char   c;
6816 +       int             v;
6817 +       int             xpos = 0, ypos = 0, pass = 0;
6818 +       int i;
6819 +       LZW_STATIC_DATA sd;
6820 +
6821 +
6822 +       /*
6823 +        **  Initialize the Compression routines
6824 +        */
6825 +       if (! ReadOK(fd,&c,1)) {
6826 +               return;
6827 +       }
6828 +
6829 +       if (c > MAX_LWZ_BITS) {
6830 +               return; 
6831 +       }
6832 +
6833 +       /* Stash the color map into the image */
6834 +       for (i=0; (i<gdMaxColors); i++) {
6835 +               im->red[i] = cmap[CM_RED][i];
6836 +               im->green[i] = cmap[CM_GREEN][i];
6837 +               im->blue[i] = cmap[CM_BLUE][i];
6838 +               im->open[i] = 1;
6839 +       }
6840 +       /* Many (perhaps most) of these colors will remain marked open. */
6841 +       im->colorsTotal = gdMaxColors;
6842 +       if (LWZReadByte(fd, &sd, TRUE, c, ZeroDataBlockP) < 0) {
6843 +               return;
6844 +       }
6845 +
6846 +       /*
6847 +        **  If this is an "uninteresting picture" ignore it.
6848 +        **  REMOVED For 1.4
6849 +        */
6850 +       /*if (ignore) { */
6851 +       /*        while (LWZReadByte(fd, &sd, FALSE, c) >= 0) */
6852 +       /*                ; */
6853 +       /*        return; */
6854 +       /*} */
6855 +
6856 +       while ((v = LWZReadByte(fd, &sd, FALSE, c, ZeroDataBlockP)) >= 0) {
6857 +               if (v >= gdMaxColors) {
6858 +                       v = 0;
6859 +               }
6860 +               /* This how we recognize which colors are actually used. */
6861 +               if (im->open[v]) {
6862 +                       im->open[v] = 0;
6863 +               }
6864 +               gdImageSetPixel(im, xpos, ypos, v);
6865 +               ++xpos;
6866 +               if (xpos == len) {
6867 +                       xpos = 0;
6868 +                       if (interlace) {
6869 +                               switch (pass) {
6870 +                                       case 0:
6871 +                                       case 1:
6872 +                                               ypos += 8; break;
6873 +                                       case 2:
6874 +                                               ypos += 4; break;
6875 +                                       case 3:
6876 +                                               ypos += 2; break;
6877                                 }
6878 -               /* This how we recognize which colors are actually used. */
6879 -               if (im->open[v]) {
6880 -                       im->open[v] = 0;
6881 -               }
6882 -               gdImageSetPixel(im, xpos, ypos, v);
6883 -               ++xpos;
6884 -               if (xpos == len) {
6885 -                       xpos = 0;
6886 -                       if (interlace) {
6887 -                               switch (pass) {
6888 -                               case 0:
6889 -                               case 1:
6890 -                                       ypos += 8; break;
6891 -                               case 2:
6892 -                                       ypos += 4; break;
6893 -                               case 3:
6894 -                                       ypos += 2; break;
6895 -                               }
6896 -
6897 -                               if (ypos >= height) {
6898 -                                       ++pass;
6899 -                                       switch (pass) {
6900 -                                       case 1:
6901 -                                               ypos = 4; break;
6902 -                                       case 2:
6903 -                                               ypos = 2; break;
6904 -                                       case 3:
6905 -                                               ypos = 1; break;
6906 -                                       default:
6907 -                                               goto fini;
6908 -                                       }
6909 -                               }
6910 -                       } else {
6911 -                               ++ypos;
6912 -                       }
6913 -               }
6914 -               if (ypos >= height)
6915 -                       break;
6916 -       }
6917 +
6918 +                               if (ypos >= height) {
6919 +                                       ++pass;
6920 +                                       switch (pass) {
6921 +                                               case 1:
6922 +                                                       ypos = 4; break;
6923 +                                               case 2:
6924 +                                                       ypos = 2; break;
6925 +                                               case 3:
6926 +                                                       ypos = 1; break;
6927 +                                               default:
6928 +                                                       goto fini;
6929 +                                       }
6930 +                               }
6931 +                       } else {
6932 +                               ++ypos;
6933 +                       }
6934 +               }
6935 +               if (ypos >= height)
6936 +                       break;
6937 +       }
6938  
6939  fini:
6940 -       if (LWZReadByte(fd,FALSE,c)>=0) {
6941 -               /* Ignore extra */
6942 -       }
6943 +       if (LWZReadByte(fd, &sd, FALSE, c, ZeroDataBlockP) >=0) {
6944 +               /* Ignore extra */
6945 +       }
6946  }
6947 -
6948 +/* }}} */
6949 diff -urN php-4.4.8.org/ext/gd/libgd/gd_gif_out.c php-4.4.8/ext/gd/libgd/gd_gif_out.c
6950 --- php-4.4.8.org/ext/gd/libgd/gd_gif_out.c     2004-07-23 01:09:24.000000000 +0200
6951 +++ php-4.4.8/ext/gd/libgd/gd_gif_out.c 2006-07-26 12:03:09.000000000 +0200
6952 @@ -133,7 +133,7 @@
6953         BitsPerPixel = colorstobpp(tim->colorsTotal);
6954         /* All set, let's do it. */
6955         GIFEncode(
6956 -               out, tim->sx, tim->sy, interlace, 0, transparent, BitsPerPixel,
6957 +               out, tim->sx, tim->sy, tim->interlace, 0, tim->transparent, BitsPerPixel,
6958                 tim->red, tim->green, tim->blue, tim);
6959         if (pim) {
6960                 /* Destroy palette based temporary image. */
6961 @@ -264,10 +264,12 @@
6962          int ColorMapSize;
6963          int InitCodeSize;
6964          int i;
6965 -       GifCtx ctx;
6966 +               GifCtx ctx;
6967 +
6968 +               memset(&ctx, 0, sizeof(ctx));
6969          ctx.Interlace = GInterlace;
6970 -       ctx.in_count = 1;
6971 -       memset(&ctx, 0, sizeof(ctx));
6972 +               ctx.in_count = 1;
6973 +
6974          ColorMapSize = 1 << BitsPerPixel;
6975  
6976          RWidth = ctx.Width = GWidth;
6977 diff -urN php-4.4.8.org/ext/gd/libgd/gd.h php-4.4.8/ext/gd/libgd/gd.h
6978 --- php-4.4.8.org/ext/gd/libgd/gd.h     2004-07-23 01:09:24.000000000 +0200
6979 +++ php-4.4.8/ext/gd/libgd/gd.h 2007-09-12 01:34:25.000000000 +0200
6980 @@ -5,14 +5,30 @@
6981  extern "C" {
6982  #endif
6983  
6984 -#ifndef WIN32
6985 -/* default fontpath for unix systems */
6986 -#define DEFAULT_FONTPATH "/usr/X11R6/lib/X11/fonts/TrueType:/usr/X11R6/lib/X11/fonts/truetype:/usr/X11R6/lib/X11/fonts/TTF:/usr/share/fonts/TrueType:/usr/share/fonts/truetype:/usr/openwin/lib/X11/fonts/TrueType:/usr/X11R6/lib/X11/fonts/Type1:."
6987 -#define PATHSEPARATOR ":"
6988 -#else
6989 +#ifdef HAVE_CONFIG_H
6990 +#include "config.h"
6991 +#endif
6992 +
6993 +#include "php_compat.h"
6994 +
6995 +#define GD_MAJOR_VERSION 2
6996 +#define GD_MINOR_VERSION 0
6997 +#define GD_RELEASE_VERSION 35
6998 +#define GD_EXTRA_VERSION ""
6999 +#define GD_VERSION_STRING "2.0.35"
7000 +
7001 +#ifdef NETWARE
7002 +/* default fontpath for netware systems */
7003 +#define DEFAULT_FONTPATH "sys:/java/nwgfx/lib/x11/fonts/ttf;."
7004 +#define PATHSEPARATOR ";"
7005 +#elif defined(WIN32)
7006  /* default fontpath for windows systems */
7007  #define DEFAULT_FONTPATH "c:\\winnt\\fonts;c:\\windows\\fonts;."
7008  #define PATHSEPARATOR ";"
7009 +#else
7010 +/* default fontpath for unix systems */
7011 +#define DEFAULT_FONTPATH "/usr/X11R6/lib/X11/fonts/TrueType:/usr/X11R6/lib/X11/fonts/truetype:/usr/X11R6/lib/X11/fonts/TTF:/usr/share/fonts/TrueType:/usr/share/fonts/truetype:/usr/openwin/lib/X11/fonts/TrueType:/usr/X11R6/lib/X11/fonts/Type1:."
7012 +#define PATHSEPARATOR ":"
7013  #endif
7014  
7015  /* gd.h: declarations file for the graphic-draw module.
7016 @@ -231,8 +247,8 @@
7017  gdImagePtr gdImageCreateFromPngCtx(gdIOCtxPtr in);
7018  gdImagePtr gdImageCreateFromWBMP(FILE *inFile);
7019  gdImagePtr gdImageCreateFromWBMPCtx(gdIOCtx *infile);
7020 -gdImagePtr gdImageCreateFromJpeg(FILE *infile);
7021 -gdImagePtr gdImageCreateFromJpegCtx(gdIOCtx *infile);
7022 +gdImagePtr gdImageCreateFromJpeg(FILE *infile, int ignore_warning);
7023 +gdImagePtr gdImageCreateFromJpegCtx(gdIOCtx *infile, int ignore_warning);
7024  
7025  /* A custom data source. */
7026  /* The source function must return -1 on error, otherwise the number
7027 @@ -295,6 +311,14 @@
7028  void gdImageString16(gdImagePtr im, gdFontPtr f, int x, int y, unsigned short *s, int color);
7029  void gdImageStringUp16(gdImagePtr im, gdFontPtr f, int x, int y, unsigned short *s, int color);
7030  
7031 +/*
7032 + * The following functions are required to be called prior to the
7033 + * use of any sort of threads in a module load / shutdown function
7034 + * respectively.
7035 + */
7036 +void gdFontCacheMutexSetup();
7037 +void gdFontCacheMutexShutdown();
7038 +
7039  /* 2.0.16: for thread-safe use of gdImageStringFT and friends,
7040   * call this before allowing any thread to call gdImageStringFT.
7041   * Otherwise it is invoked by the first thread to invoke
7042 @@ -438,8 +462,8 @@
7043   * compression (smallest files) but takes a long time to compress, and
7044   * -1 selects the default compiled into the zlib library.
7045   */
7046 -void gdImagePngEx(gdImagePtr im, FILE * out, int level);
7047 -void gdImagePngCtxEx(gdImagePtr im, gdIOCtx * out, int level);
7048 +void gdImagePngEx(gdImagePtr im, FILE * out, int level, int basefilter);
7049 +void gdImagePngCtxEx(gdImagePtr im, gdIOCtx * out, int level, int basefilter);
7050  
7051  void gdImageWBMP(gdImagePtr image, int fg, FILE *out);
7052  void gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out);
7053 @@ -483,7 +507,7 @@
7054  
7055  /* Best to free this memory with gdFree(), not free() */
7056  void* gdImageGdPtr(gdImagePtr im, int *size);
7057 -void *gdImagePngPtrEx(gdImagePtr im, int *size, int level);
7058 +void *gdImagePngPtrEx(gdImagePtr im, int *size, int level, int basefilter);
7059  
7060  /* Best to free this memory with gdFree(), not free() */
7061  void* gdImageGd2Ptr(gdImagePtr im, int cs, int fmt, int *size);
7062 @@ -534,11 +558,11 @@
7063         substituted automatically. */
7064  void gdImageCopyResampled(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH);
7065  
7066 -gdImagePtr gdImageRotate90(gdImagePtr src);
7067 -gdImagePtr gdImageRotate180(gdImagePtr src);
7068 -gdImagePtr gdImageRotate270(gdImagePtr src);
7069 -gdImagePtr gdImageRotate45(gdImagePtr src, double dAngle, int clrBack);
7070 -gdImagePtr gdImageRotate (gdImagePtr src, double dAngle, int clrBack);
7071 +gdImagePtr gdImageRotate90(gdImagePtr src, int ignoretransparent);
7072 +gdImagePtr gdImageRotate180(gdImagePtr src, int ignoretransparent);
7073 +gdImagePtr gdImageRotate270(gdImagePtr src, int ignoretransparent);
7074 +gdImagePtr gdImageRotate45(gdImagePtr src, double dAngle, int clrBack, int ignoretransparent);
7075 +gdImagePtr gdImageRotate (gdImagePtr src, double dAngle, int clrBack, int ignoretransparent);
7076  
7077  void gdImageSetBrush(gdImagePtr im, gdImagePtr brush);
7078  void gdImageSetTile(gdImagePtr im, gdImagePtr tile);
7079 @@ -619,7 +643,7 @@
7080  int gdImageContrast(gdImagePtr src, double contrast);
7081  
7082  /* Simply adds or substracts respectively red, green or blue to a pixel */
7083 -int gdImageColor(gdImagePtr src, int red, int green, int blue);
7084 +int gdImageColor(gdImagePtr src, const int red, const int green, const int blue, const int alpha);
7085  
7086  /* Image convolution by a 3x3 custom matrix */
7087  int gdImageConvolution(gdImagePtr src, float ft[3][3], float filter_div, float offset);
7088 diff -urN php-4.4.8.org/ext/gd/libgd/gdhelpers.h php-4.4.8/ext/gd/libgd/gdhelpers.h
7089 --- php-4.4.8.org/ext/gd/libgd/gdhelpers.h      2007-03-10 14:06:37.000000000 +0100
7090 +++ php-4.4.8/ext/gd/libgd/gdhelpers.h  2007-03-10 13:18:36.000000000 +0100
7091 @@ -1,4 +1,4 @@
7092 -#ifndef GDHELPERS_H 
7093 +#ifndef GDHELPERS_H
7094  #define GDHELPERS_H 1
7095  
7096  #include <sys/types.h>
7097 diff -urN php-4.4.8.org/ext/gd/libgd/gd_io.c php-4.4.8/ext/gd/libgd/gd_io.c
7098 --- php-4.4.8.org/ext/gd/libgd/gd_io.c  2003-12-25 23:33:03.000000000 +0100
7099 +++ php-4.4.8/ext/gd/libgd/gd_io.c      2005-08-18 14:54:43.000000000 +0200
7100 @@ -23,153 +23,124 @@
7101  #define IO_DBG(s)
7102  
7103  
7104 +#define GD_IO_EOF_CHK(r)       \
7105 +       if (r == EOF) {         \
7106 +               return 0;       \
7107 +       }                       \
7108 +
7109  /*
7110   * Write out a word to the I/O context pointer
7111   */
7112 -void
7113 -Putword (int w, gdIOCtx * ctx)
7114 +void Putword (int w, gdIOCtx * ctx)
7115  {
7116 -  unsigned char buf[2];
7117 -  buf[0] = w & 0xff;
7118 -  buf[1] = (w / 256) & 0xff;
7119 -  (ctx->putBuf) (ctx, (char *) buf, 2);
7120 +       unsigned char buf[2];
7121 +
7122 +       buf[0] = w & 0xff;
7123 +       buf[1] = (w / 256) & 0xff;
7124 +       (ctx->putBuf) (ctx, (char *) buf, 2);
7125  }
7126  
7127 -void
7128 -Putchar (int c, gdIOCtx * ctx)
7129 +void Putchar (int c, gdIOCtx * ctx)
7130  {
7131 -  (ctx->putC) (ctx, c & 0xff);
7132 +       (ctx->putC) (ctx, c & 0xff);
7133  }
7134  
7135 -void
7136 -gdPutC (const unsigned char c, gdIOCtx * ctx)
7137 +void gdPutC (const unsigned char c, gdIOCtx * ctx)
7138  {
7139 -  (ctx->putC) (ctx, c);
7140 +       (ctx->putC) (ctx, c);
7141  }
7142  
7143 -void
7144 -gdPutWord (int w, gdIOCtx * ctx)
7145 +void gdPutWord (int w, gdIOCtx * ctx)
7146  {
7147 -  IO_DBG (printf ("Putting word...\n"));
7148 -  (ctx->putC) (ctx, (unsigned char) (w >> 8));
7149 -  (ctx->putC) (ctx, (unsigned char) (w & 0xFF));
7150 -  IO_DBG (printf ("put.\n"));
7151 +       IO_DBG (php_gd_error("Putting word..."));
7152 +       (ctx->putC) (ctx, (unsigned char) (w >> 8));
7153 +       (ctx->putC) (ctx, (unsigned char) (w & 0xFF));
7154 +       IO_DBG (php_gd_error("put."));
7155  }
7156  
7157 -void
7158 -gdPutInt (int w, gdIOCtx * ctx)
7159 +void gdPutInt (int w, gdIOCtx * ctx)
7160  {
7161 -  IO_DBG (printf ("Putting int...\n"));
7162 -  (ctx->putC) (ctx, (unsigned char) (w >> 24));
7163 -  (ctx->putC) (ctx, (unsigned char) ((w >> 16) & 0xFF));
7164 -  (ctx->putC) (ctx, (unsigned char) ((w >> 8) & 0xFF));
7165 -  (ctx->putC) (ctx, (unsigned char) (w & 0xFF));
7166 -  IO_DBG (printf ("put.\n"));
7167 +       IO_DBG (php_gd_error("Putting int..."));
7168 +       (ctx->putC) (ctx, (unsigned char) (w >> 24));
7169 +       (ctx->putC) (ctx, (unsigned char) ((w >> 16) & 0xFF));
7170 +       (ctx->putC) (ctx, (unsigned char) ((w >> 8) & 0xFF));
7171 +       (ctx->putC) (ctx, (unsigned char) (w & 0xFF));
7172 +       IO_DBG (php_gd_error("put."));
7173  }
7174  
7175 -int
7176 -gdGetC (gdIOCtx * ctx)
7177 +int gdGetC (gdIOCtx * ctx)
7178  {
7179 -  return ((ctx->getC) (ctx));
7180 +       return ((ctx->getC) (ctx));
7181  }
7182  
7183 -
7184 -
7185 -int
7186 -gdGetByte (int *result, gdIOCtx * ctx)
7187 +int gdGetByte (int *result, gdIOCtx * ctx)
7188  {
7189 -  int r;
7190 -  r = (ctx->getC) (ctx);
7191 -  if (r == EOF)
7192 -    {
7193 -      return 0;
7194 -    }
7195 -  *result = r;
7196 -  return 1;
7197 +       int r;
7198 +       r = (ctx->getC) (ctx);
7199 +       GD_IO_EOF_CHK(r);
7200 +       *result = r;
7201 +       return 1;
7202  }
7203  
7204 -int
7205 -gdGetWord (int *result, gdIOCtx * ctx)
7206 +int gdGetWord (int *result, gdIOCtx * ctx)
7207  {
7208 -  int r;
7209 -  r = (ctx->getC) (ctx);
7210 -  if (r == EOF)
7211 -    {
7212 -      return 0;
7213 -    }
7214 -  *result = r << 8;
7215 -  r = (ctx->getC) (ctx);
7216 -  if (r == EOF)
7217 -    {
7218 -      return 0;
7219 -    }
7220 -  *result += r;
7221 -  return 1;
7222 +       int r;
7223 +       r = (ctx->getC) (ctx);
7224 +       GD_IO_EOF_CHK(r);
7225 +       *result = r << 8;
7226 +       r = (ctx->getC) (ctx);
7227 +       GD_IO_EOF_CHK(r);
7228 +       *result += r;
7229 +       return 1;
7230  }
7231  
7232  
7233 -int
7234 -gdGetInt (int *result, gdIOCtx * ctx)
7235 +int gdGetInt (int *result, gdIOCtx * ctx)
7236  {
7237 -  int r;
7238 -  r = (ctx->getC) (ctx);
7239 -  if (r == EOF)
7240 -    {
7241 -      return 0;
7242 -    }
7243 -  *result = r << 24;
7244 +       int r;
7245 +       r = (ctx->getC) (ctx);
7246 +       GD_IO_EOF_CHK(r);
7247 +       *result = r << 24;
7248  
7249 -  r = (ctx->getC) (ctx);
7250 -  if (r == EOF)
7251 -    {
7252 -      return 0;
7253 -    }
7254 -  *result += r << 16;
7255 +       r = (ctx->getC) (ctx);
7256 +       GD_IO_EOF_CHK(r);
7257 +       *result += r << 16;
7258  
7259 -  r = (ctx->getC) (ctx);
7260 -  if (r == EOF)
7261 -    {
7262 -      return 0;
7263 -    }
7264 -  *result += r << 8;
7265 +       r = (ctx->getC) (ctx);
7266 +       if (r == EOF) {
7267 +               return 0;
7268 +       }
7269 +       *result += r << 8;
7270  
7271 -  r = (ctx->getC) (ctx);
7272 -  if (r == EOF)
7273 -    {
7274 -      return 0;
7275 -    }
7276 -  *result += r;
7277 +       r = (ctx->getC) (ctx);
7278 +       GD_IO_EOF_CHK(r);
7279 +       *result += r;
7280  
7281 -  return 1;
7282 +       return 1;
7283  }
7284  
7285 -int
7286 -gdPutBuf (const void *buf, int size, gdIOCtx * ctx)
7287 +int gdPutBuf (const void *buf, int size, gdIOCtx * ctx)
7288  {
7289 -  IO_DBG (printf ("Putting buf...\n"));
7290 -  return (ctx->putBuf) (ctx, buf, size);
7291 -  IO_DBG (printf ("put.\n"));
7292 +       IO_DBG (php_gd_error("Putting buf..."));
7293 +       return (ctx->putBuf) (ctx, buf, size);
7294 +       IO_DBG (php_gd_error("put."));
7295  }
7296  
7297 -int
7298 -gdGetBuf (void *buf, int size, gdIOCtx * ctx)
7299 +int gdGetBuf (void *buf, int size, gdIOCtx * ctx)
7300  {
7301 -  return (ctx->getBuf) (ctx, buf, size);
7302 +       return (ctx->getBuf) (ctx, buf, size);
7303  }
7304  
7305 -
7306 -int
7307 -gdSeek (gdIOCtx * ctx, const int pos)
7308 +int gdSeek (gdIOCtx * ctx, const int pos)
7309  {
7310 -  IO_DBG (printf ("Seeking...\n"));
7311 -  return ((ctx->seek) (ctx, pos));
7312 -  IO_DBG (printf ("Done.\n"));
7313 +       IO_DBG (php_gd_error("Seeking..."));
7314 +       return ((ctx->seek) (ctx, pos));
7315 +       IO_DBG (php_gd_error("Done."));
7316  }
7317  
7318 -long
7319 -gdTell (gdIOCtx * ctx)
7320 +long gdTell (gdIOCtx * ctx)
7321  {
7322 -  IO_DBG (printf ("Telling...\n"));
7323 -  return ((ctx->tell) (ctx));
7324 -  IO_DBG (printf ("told.\n"));
7325 +       IO_DBG (php_gd_error("Telling..."));
7326 +       return ((ctx->tell) (ctx));
7327 +       IO_DBG (php_gd_error ("told."));
7328  }
7329 diff -urN php-4.4.8.org/ext/gd/libgd/gd_io_dp.c php-4.4.8/ext/gd/libgd/gd_io_dp.c
7330 --- php-4.4.8.org/ext/gd/libgd/gd_io_dp.c       2004-03-29 20:21:00.000000000 +0200
7331 +++ php-4.4.8/ext/gd/libgd/gd_io_dp.c   2004-03-29 20:20:33.000000000 +0200
7332 @@ -27,26 +27,23 @@
7333  
7334  /* this is used for creating images in main memory */
7335  typedef struct dpStruct
7336 -  {
7337 -    void *data;
7338 -    int logicalSize;
7339 -    int realSize;
7340 -    int dataGood;
7341 -    int pos;
7342 -    int freeOK;
7343 -  }
7344 -dynamicPtr;
7345 +{
7346 +       void *data;
7347 +       int logicalSize;
7348 +       int realSize;
7349 +       int dataGood;
7350 +       int pos;
7351 +       int freeOK;
7352 +} dynamicPtr;
7353  
7354  typedef struct dpIOCtx
7355 -  {
7356 -    gdIOCtx ctx;
7357 -    dynamicPtr *dp;
7358 -  }
7359 -dpIOCtx;
7360 +{
7361 +       gdIOCtx ctx;
7362 +       dynamicPtr *dp;
7363 +} dpIOCtx;
7364  
7365  typedef struct dpIOCtx *dpIOCtxPtr;
7366  
7367 -
7368  /* these functions operate on in-memory dynamic pointers */
7369  static int allocDynamic (dynamicPtr * dp, int initialSize, void *data);
7370  static int appendDynamic (dynamicPtr * dp, const void *src, int size);
7371 @@ -65,166 +62,136 @@
7372  static long dynamicTell (struct gdIOCtx *);
7373  
7374  /* return data as a dynamic pointer */
7375 -gdIOCtx *
7376 -gdNewDynamicCtx (int initialSize, void *data)
7377 +gdIOCtx * gdNewDynamicCtx (int initialSize, void *data)
7378  {
7379 -       return gdNewDynamicCtxEx(initialSize, data, 1);
7380 +       return gdNewDynamicCtxEx(initialSize, data, 1);
7381  }
7382
7383 +
7384  gdIOCtx * gdNewDynamicCtxEx (int initialSize, void *data, int freeOKFlag)
7385  {
7386 -  dpIOCtx *ctx;
7387 -  dynamicPtr *dp;
7388 +       dpIOCtx *ctx;
7389 +       dynamicPtr *dp;
7390  
7391 -  ctx = (dpIOCtx *) gdMalloc (sizeof (dpIOCtx));
7392 -  if (ctx == NULL)
7393 -    {
7394 -      return NULL;
7395 -    }
7396 +       ctx = (dpIOCtx *) gdMalloc (sizeof (dpIOCtx));
7397  
7398 -  dp = newDynamic (initialSize, data, freeOKFlag);
7399 -  if (!dp)
7400 -    {
7401 -      gdFree (ctx);
7402 -      return NULL;
7403 -    };
7404 +       dp = newDynamic(initialSize, data, freeOKFlag);
7405  
7406 -  ctx->dp = dp;
7407 +       ctx->dp = dp;
7408  
7409 -  ctx->ctx.getC = dynamicGetchar;
7410 -  ctx->ctx.putC = dynamicPutchar;
7411 +       ctx->ctx.getC = dynamicGetchar;
7412 +       ctx->ctx.putC = dynamicPutchar;
7413  
7414 -  ctx->ctx.getBuf = dynamicGetbuf;
7415 -  ctx->ctx.putBuf = dynamicPutbuf;
7416 +       ctx->ctx.getBuf = dynamicGetbuf;
7417 +       ctx->ctx.putBuf = dynamicPutbuf;
7418  
7419 -  ctx->ctx.seek = dynamicSeek;
7420 -  ctx->ctx.tell = dynamicTell;
7421 +       ctx->ctx.seek = dynamicSeek;
7422 +       ctx->ctx.tell = dynamicTell;
7423  
7424 -  ctx->ctx.gd_free = gdFreeDynamicCtx;
7425 +       ctx->ctx.gd_free = gdFreeDynamicCtx;
7426  
7427 -  return (gdIOCtx *) ctx;
7428 +       return (gdIOCtx *) ctx;
7429  }
7430  
7431 -void *
7432 -gdDPExtractData (struct gdIOCtx *ctx, int *size)
7433 +void * gdDPExtractData (struct gdIOCtx *ctx, int *size)
7434  {
7435 -  dynamicPtr *dp;
7436 -  dpIOCtx *dctx;
7437 -  void *data;
7438 +       dynamicPtr *dp;
7439 +       dpIOCtx *dctx;
7440 +       void *data;
7441  
7442 -  dctx = (dpIOCtx *) ctx;
7443 -  dp = dctx->dp;
7444 +       dctx = (dpIOCtx *) ctx;
7445 +       dp = dctx->dp;
7446  
7447 -  /* clean up the data block and return it */
7448 -  if (dp->dataGood)
7449 -    {
7450 -      trimDynamic (dp);
7451 -      *size = dp->logicalSize;
7452 -      data = dp->data;
7453 -    }
7454 -  else
7455 -    {
7456 -      *size = 0;
7457 -      data = NULL;
7458 -      if (dp->data != NULL && dp->freeOK)
7459 -       {
7460 -         gdFree (dp->data);
7461 +       /* clean up the data block and return it */
7462 +       if (dp->dataGood) {
7463 +               trimDynamic (dp);
7464 +               *size = dp->logicalSize;
7465 +               data = dp->data;
7466 +       } else {
7467 +               *size = 0;
7468 +               data = NULL;
7469 +               if (dp->data != NULL && dp->freeOK) {
7470 +                       gdFree(dp->data);
7471 +               }
7472         }
7473 -    }
7474  
7475 -  dp->data = NULL;
7476 -  dp->realSize = 0;
7477 -  dp->logicalSize = 0;
7478 +       dp->data = NULL;
7479 +       dp->realSize = 0;
7480 +       dp->logicalSize = 0;
7481  
7482 -  return data;
7483 +       return data;
7484  }
7485  
7486 -static
7487 -void
7488 -gdFreeDynamicCtx (struct gdIOCtx *ctx)
7489 +static void gdFreeDynamicCtx (struct gdIOCtx *ctx)
7490  {
7491 -  dynamicPtr *dp;
7492 -  dpIOCtx *dctx;
7493 -
7494 -  dctx = (dpIOCtx *) ctx;
7495 -  dp = dctx->dp;
7496 +       dynamicPtr *dp;
7497 +       dpIOCtx *dctx;
7498  
7499 -  gdFree (ctx);
7500 +       dctx = (dpIOCtx *) ctx;
7501 +       dp = dctx->dp;
7502  
7503 -  dp->realSize = 0;
7504 -  dp->logicalSize = 0;
7505 +       gdFree(ctx);
7506  
7507 -  gdFree (dp);
7508 +       dp->realSize = 0;
7509 +       dp->logicalSize = 0;
7510  
7511 +       gdFree(dp);
7512  }
7513  
7514 -static long
7515 -dynamicTell (struct gdIOCtx *ctx)
7516 +static long dynamicTell (struct gdIOCtx *ctx)
7517  {
7518 -  dpIOCtx *dctx;
7519 +       dpIOCtx *dctx;
7520  
7521 -  dctx = (dpIOCtx *) ctx;
7522 -  return (dctx->dp->pos);
7523 +       dctx = (dpIOCtx *) ctx;
7524 +
7525 +       return (dctx->dp->pos);
7526  }
7527  
7528 -static int
7529 -dynamicSeek (struct gdIOCtx *ctx, const int pos)
7530 +static int dynamicSeek (struct gdIOCtx *ctx, const int pos)
7531  {
7532 -  int bytesNeeded;
7533 -  dynamicPtr *dp;
7534 -  dpIOCtx *dctx;
7535 +       int bytesNeeded;
7536 +       dynamicPtr *dp;
7537 +       dpIOCtx *dctx;
7538  
7539 -  dctx = (dpIOCtx *) ctx;
7540 -  dp = dctx->dp;
7541 +       dctx = (dpIOCtx *) ctx;
7542 +       dp = dctx->dp;
7543  
7544 -  if (!dp->dataGood)
7545 -    return FALSE;
7546 +       if (!dp->dataGood) {
7547 +               return FALSE;
7548 +       }
7549  
7550 -  bytesNeeded = pos;
7551 -  if (bytesNeeded > dp->realSize)
7552 -    {
7553 -       /* 2.0.21 */
7554 -       if (!dp->freeOK) {
7555 -               return FALSE;
7556 -       }
7557 -      if (!gdReallocDynamic (dp, dp->realSize * 2))
7558 -       {
7559 -         dp->dataGood = FALSE;
7560 -         return FALSE;
7561 +       bytesNeeded = pos;
7562 +       if (bytesNeeded > dp->realSize) {
7563 +               /* 2.0.21 */
7564 +               if (!dp->freeOK) {
7565 +                       return FALSE;
7566 +               }
7567 +               gdReallocDynamic (dp, dp->realSize * 2);
7568         }
7569 -    }
7570  
7571 -  /* if we get here, we can be sure that we have enough bytes
7572 -     to copy safely */
7573 +       /* if we get here, we can be sure that we have enough bytes to copy safely */
7574  
7575 -  /* Extend the logical size if we seek beyond EOF. */
7576 -  if (pos > dp->logicalSize)
7577 -    {
7578 -      dp->logicalSize = pos;
7579 -    };
7580 +       /* Extend the logical size if we seek beyond EOF. */
7581 +       if (pos > dp->logicalSize) {
7582 +               dp->logicalSize = pos;
7583 +       }
7584  
7585 -  dp->pos = pos;
7586 +       dp->pos = pos;
7587  
7588 -  return TRUE;
7589 +       return TRUE;
7590  }
7591  
7592  /* return data as a dynamic pointer */
7593  static dynamicPtr * newDynamic (int initialSize, void *data, int freeOKFlag)
7594  {
7595 -  dynamicPtr *dp;
7596 -  dp = (dynamicPtr *) gdMalloc (sizeof (dynamicPtr));
7597 -  if (dp == NULL)
7598 -    {
7599 -      return NULL;
7600 -    }
7601 +       dynamicPtr *dp;
7602 +       dp = (dynamicPtr *) gdMalloc (sizeof (dynamicPtr));
7603  
7604 -  if (!allocDynamic (dp, initialSize, data))
7605 -    return NULL;
7606 +       allocDynamic (dp, initialSize, data);
7607  
7608 -  dp->pos = 0;
7609 -  dp->freeOK = freeOKFlag;
7610 +       dp->pos = 0;
7611 +       dp->freeOK = freeOKFlag;
7612  
7613 -  return dp;
7614 +       return dp;
7615  }
7616  
7617  static int
7618 @@ -246,64 +213,53 @@
7619  
7620  }
7621  
7622 -static void
7623 -dynamicPutchar (struct gdIOCtx *ctx, int a)
7624 +static void dynamicPutchar (struct gdIOCtx *ctx, int a)
7625  {
7626 -  unsigned char b;
7627 -  dpIOCtxPtr dctx;
7628 +       unsigned char b;
7629 +       dpIOCtxPtr dctx;
7630  
7631 -  b = a;
7632 -  dctx = (dpIOCtxPtr) ctx;
7633 +       b = a;
7634 +       dctx = (dpIOCtxPtr) ctx;
7635  
7636 -  appendDynamic (dctx->dp, &b, 1);
7637 +       appendDynamic(dctx->dp, &b, 1);
7638  }
7639  
7640 -static int
7641 -dynamicGetbuf (gdIOCtxPtr ctx, void *buf, int len)
7642 +static int dynamicGetbuf (gdIOCtxPtr ctx, void *buf, int len)
7643  {
7644 -  int rlen, remain;
7645 -  dpIOCtxPtr dctx;
7646 -  dynamicPtr *dp;
7647 +       int rlen, remain;
7648 +       dpIOCtxPtr dctx;
7649 +       dynamicPtr *dp;
7650  
7651 -  dctx = (dpIOCtxPtr) ctx;
7652 -  dp = dctx->dp;
7653 +       dctx = (dpIOCtxPtr) ctx;
7654 +       dp = dctx->dp;
7655  
7656 -  remain = dp->logicalSize - dp->pos;
7657 -  if (remain >= len)
7658 -    {
7659 -      rlen = len;
7660 -    }
7661 -  else
7662 -    {
7663 -      if (remain == 0)
7664 -       {
7665 -         return EOF;
7666 +       remain = dp->logicalSize - dp->pos;
7667 +       if (remain >= len) {
7668 +               rlen = len;
7669 +       } else {
7670 +               if (remain == 0) {
7671 +                       return EOF;
7672 +               }
7673 +               rlen = remain;
7674         }
7675 -      rlen = remain;
7676 -    }
7677  
7678 -  memcpy (buf, (void *) ((char *) dp->data + dp->pos), rlen);
7679 -  dp->pos += rlen;
7680 +       memcpy(buf, (void *) ((char *) dp->data + dp->pos), rlen);
7681 +       dp->pos += rlen;
7682  
7683 -  return rlen;
7684 +       return rlen;
7685  }
7686  
7687 -static int
7688 -dynamicGetchar (gdIOCtxPtr ctx)
7689 +static int dynamicGetchar (gdIOCtxPtr ctx)
7690  {
7691 -  unsigned char b;
7692 -  int rv;
7693 -
7694 -  rv = dynamicGetbuf (ctx, &b, 1);
7695 +       unsigned char b;
7696 +       int rv;
7697  
7698 -  if (rv != 1)
7699 -    {
7700 -      return EOF;
7701 -    }
7702 -  else
7703 -    {
7704 -      return b;                        /* (b & 0xff); */
7705 -    }
7706 +       rv = dynamicGetbuf (ctx, &b, 1);
7707 +       if (rv != 1) {
7708 +               return EOF;
7709 +       } else {
7710 +               return b;               /* (b & 0xff); */
7711 +       }
7712  }
7713  
7714  /* *********************************************************************
7715 @@ -316,114 +272,89 @@
7716  allocDynamic (dynamicPtr * dp, int initialSize, void *data)
7717  {
7718  
7719 -  if (data == NULL)
7720 -    {
7721 -      dp->logicalSize = 0;
7722 -      dp->dataGood = FALSE;
7723 -      dp->data = gdMalloc (initialSize);
7724 -    }
7725 -  else
7726 -    {
7727 -      dp->logicalSize = initialSize;
7728 -      dp->dataGood = TRUE;
7729 -      dp->data = data;
7730 -    }
7731 +       if (data == NULL) {
7732 +               dp->logicalSize = 0;
7733 +               dp->dataGood = FALSE;
7734 +               dp->data = gdMalloc(initialSize);
7735 +       } else {
7736 +               dp->logicalSize = initialSize;
7737 +               dp->dataGood = TRUE;
7738 +               dp->data = data;
7739 +       }
7740  
7741 -  if (dp->data != NULL)
7742 -    {
7743 -      dp->realSize = initialSize;
7744 -      dp->dataGood = TRUE;
7745 -      dp->pos = 0;
7746 -      return TRUE;
7747 -    }
7748 -  else
7749 -    {
7750 -      dp->realSize = 0;
7751 -      return FALSE;
7752 -    }
7753 +       dp->realSize = initialSize;
7754 +       dp->dataGood = TRUE;
7755 +       dp->pos = 0;
7756 +
7757 +       return TRUE;
7758  }
7759  
7760  /* append bytes to the end of a dynamic pointer */
7761 -static int
7762 -appendDynamic (dynamicPtr * dp, const void *src, int size)
7763 +static int appendDynamic (dynamicPtr * dp, const void *src, int size)
7764  {
7765 -  int bytesNeeded;
7766 -  char *tmp;
7767 +       int bytesNeeded;
7768 +       char *tmp;
7769  
7770 -  if (!dp->dataGood)
7771 -    return FALSE;
7772 +       if (!dp->dataGood) {
7773 +               return FALSE;
7774 +       }
7775  
7776 -/*  bytesNeeded = dp->logicalSize + size; */
7777 -  bytesNeeded = dp->pos + size;
7778 +       /*  bytesNeeded = dp->logicalSize + size; */
7779 +       bytesNeeded = dp->pos + size;
7780  
7781 -  if (bytesNeeded > dp->realSize)
7782 -    {
7783 +       if (bytesNeeded > dp->realSize) {
7784                 /* 2.0.21 */
7785                 if (!dp->freeOK) {
7786                         return FALSE;
7787                 }
7788 -      if (!gdReallocDynamic (dp, bytesNeeded * 2))
7789 -       {
7790 -         dp->dataGood = FALSE;
7791 -         return FALSE;
7792 +               gdReallocDynamic(dp, bytesNeeded * 2);
7793         }
7794 -    }
7795  
7796 -  /* if we get here, we can be sure that we have enough bytes
7797 -     to copy safely */
7798 -  /*printf("Mem OK Size: %d, Pos: %d\n", dp->realSize, dp->pos); */
7799 -
7800 -  tmp = (char *) dp->data;
7801 -  memcpy ((void *) (tmp + (dp->pos)), src, size);
7802 -  dp->pos += size;
7803 +       /* if we get here, we can be sure that we have enough bytes to copy safely */
7804 +       /*printf("Mem OK Size: %d, Pos: %d\n", dp->realSize, dp->pos); */
7805  
7806 -  if (dp->pos > dp->logicalSize)
7807 -    {
7808 -      dp->logicalSize = dp->pos;
7809 -    };
7810 +       tmp = (char *) dp->data;
7811 +       memcpy((void *) (tmp + (dp->pos)), src, size);
7812 +       dp->pos += size;
7813 +
7814 +       if (dp->pos > dp->logicalSize) {
7815 +               dp->logicalSize = dp->pos;
7816 +       }
7817  
7818 -  return TRUE;
7819 +       return TRUE;
7820  }
7821  
7822  /* grow (or shrink) dynamic pointer */
7823 -static int
7824 -gdReallocDynamic (dynamicPtr * dp, int required)
7825 +static int gdReallocDynamic (dynamicPtr * dp, int required)
7826  {
7827 -  void *newPtr;
7828 +       void *newPtr;
7829  
7830 -  /* First try gdRealloc().  If that doesn't work, make a new
7831 -     memory block and copy. */
7832 -  if ((newPtr = gdRealloc (dp->data, required)))
7833 -    {
7834 -      dp->realSize = required;
7835 -      dp->data = newPtr;
7836 -      return TRUE;
7837 -    }
7838 +       /* First try gdRealloc(). If that doesn't work, make a new memory block and copy. */
7839 +       if ((newPtr = gdRealloc(dp->data, required))) {
7840 +               dp->realSize = required;
7841 +               dp->data = newPtr;
7842 +               return TRUE;
7843 +       }
7844  
7845 -  /* create a new pointer */
7846 -  newPtr = gdMalloc (required);
7847 -  if (!newPtr)
7848 -    {
7849 -      dp->dataGood = FALSE;
7850 -      return FALSE;
7851 -    }
7852 +       /* create a new pointer */
7853 +       newPtr = gdMalloc(required);
7854 +
7855 +       /* copy the old data into it */
7856 +       memcpy(newPtr, dp->data, dp->logicalSize);
7857 +       gdFree(dp->data);
7858 +       dp->data = newPtr;
7859  
7860 -  /* copy the old data into it */
7861 -  memcpy (newPtr, dp->data, dp->logicalSize);
7862 -  gdFree (dp->data);
7863 -  dp->data = newPtr;
7864 +       dp->realSize = required;
7865  
7866 -  dp->realSize = required;
7867 -  return TRUE;
7868 +       return TRUE;
7869  }
7870  
7871  /* trim pointer so that its real and logical sizes match */
7872 -static int
7873 -trimDynamic (dynamicPtr * dp)
7874 +static int trimDynamic (dynamicPtr * dp)
7875  {
7876 -       /* 2.0.21: we don't reallocate memory we don't own */
7877 -       if (!dp->freeOK) {
7878 -               return FALSE;
7879 -       }
7880 -       return gdReallocDynamic (dp, dp->logicalSize);
7881 +       /* 2.0.21: we don't reallocate memory we don't own */
7882 +       if (!dp->freeOK) {
7883 +               return FALSE;
7884 +       }
7885 +       return gdReallocDynamic(dp, dp->logicalSize);
7886  }
7887 diff -urN php-4.4.8.org/ext/gd/libgd/gd_io_file.c php-4.4.8/ext/gd/libgd/gd_io_file.c
7888 --- php-4.4.8.org/ext/gd/libgd/gd_io_file.c     2003-12-25 23:33:03.000000000 +0100
7889 +++ php-4.4.8/ext/gd/libgd/gd_io_file.c 2003-12-25 23:12:12.000000000 +0100
7890 @@ -29,11 +29,10 @@
7891  /* this is used for creating images in main memory */
7892  
7893  typedef struct fileIOCtx
7894 -  {
7895 -    gdIOCtx ctx;
7896 -    FILE *f;
7897 -  }
7898 -fileIOCtx;
7899 +{
7900 +       gdIOCtx ctx;
7901 +       FILE *f;
7902 +} fileIOCtx;
7903  
7904  gdIOCtx *newFileCtx (FILE * f);
7905  
7906 @@ -47,97 +46,83 @@
7907  static void gdFreeFileCtx (gdIOCtx * ctx);
7908  
7909  /* return data as a dynamic pointer */
7910 -gdIOCtx *
7911 -gdNewFileCtx (FILE * f)
7912 +gdIOCtx * gdNewFileCtx (FILE * f)
7913  {
7914 -  fileIOCtx *ctx;
7915 +       fileIOCtx *ctx;
7916  
7917 -  ctx = (fileIOCtx *) gdMalloc (sizeof (fileIOCtx));
7918 -  if (ctx == NULL)
7919 -    {
7920 -      return NULL;
7921 -    }
7922 +       ctx = (fileIOCtx *) gdMalloc(sizeof (fileIOCtx));
7923  
7924 -  ctx->f = f;
7925 +       ctx->f = f;
7926  
7927 -  ctx->ctx.getC = fileGetchar;
7928 -  ctx->ctx.putC = filePutchar;
7929 +       ctx->ctx.getC = fileGetchar;
7930 +       ctx->ctx.putC = filePutchar;
7931  
7932 -  ctx->ctx.getBuf = fileGetbuf;
7933 -  ctx->ctx.putBuf = filePutbuf;
7934 +       ctx->ctx.getBuf = fileGetbuf;
7935 +       ctx->ctx.putBuf = filePutbuf;
7936  
7937 -  ctx->ctx.tell = fileTell;
7938 -  ctx->ctx.seek = fileSeek;
7939 +       ctx->ctx.tell = fileTell;
7940 +       ctx->ctx.seek = fileSeek;
7941  
7942 -  ctx->ctx.gd_free = gdFreeFileCtx;
7943 +       ctx->ctx.gd_free = gdFreeFileCtx;
7944  
7945 -  return (gdIOCtx *) ctx;
7946 +       return (gdIOCtx *) ctx;
7947  }
7948  
7949 -static
7950 -void
7951 -gdFreeFileCtx (gdIOCtx * ctx)
7952 +static void gdFreeFileCtx (gdIOCtx * ctx)
7953  {
7954 -  gdFree (ctx);
7955 +       gdFree(ctx);
7956  }
7957  
7958  
7959 -static int
7960 -filePutbuf (gdIOCtx * ctx, const void *buf, int size)
7961 +static int filePutbuf (gdIOCtx * ctx, const void *buf, int size)
7962  {
7963 -  fileIOCtx *fctx;
7964 -  fctx = (fileIOCtx *) ctx;
7965 +       fileIOCtx *fctx;
7966 +       fctx = (fileIOCtx *) ctx;
7967  
7968 -  return fwrite (buf, 1, size, fctx->f);
7969 +       return fwrite(buf, 1, size, fctx->f);
7970  
7971  }
7972  
7973 -static int
7974 -fileGetbuf (gdIOCtx * ctx, void *buf, int size)
7975 +static int fileGetbuf (gdIOCtx * ctx, void *buf, int size)
7976  {
7977 -  fileIOCtx *fctx;
7978 -  fctx = (fileIOCtx *) ctx;
7979 -
7980 -  return (fread (buf, 1, size, fctx->f));
7981 +       fileIOCtx *fctx;
7982 +       fctx = (fileIOCtx *) ctx;
7983  
7984 +       return fread(buf, 1, size, fctx->f);
7985  }
7986  
7987 -static void
7988 -filePutchar (gdIOCtx * ctx, int a)
7989 +static void filePutchar (gdIOCtx * ctx, int a)
7990  {
7991 -  unsigned char b;
7992 -  fileIOCtx *fctx;
7993 -  fctx = (fileIOCtx *) ctx;
7994 +       unsigned char b;
7995 +       fileIOCtx *fctx;
7996 +       fctx = (fileIOCtx *) ctx;
7997  
7998 -  b = a;
7999 +       b = a;
8000  
8001 -  putc (b, fctx->f);
8002 +       putc (b, fctx->f);
8003  }
8004  
8005 -static int
8006 -fileGetchar (gdIOCtx * ctx)
8007 +static int fileGetchar (gdIOCtx * ctx)
8008  {
8009 -  fileIOCtx *fctx;
8010 -  fctx = (fileIOCtx *) ctx;
8011 +       fileIOCtx *fctx;
8012 +       fctx = (fileIOCtx *) ctx;
8013  
8014 -  return getc (fctx->f);
8015 +       return getc (fctx->f);
8016  }
8017  
8018  
8019 -static int
8020 -fileSeek (struct gdIOCtx *ctx, const int pos)
8021 +static int fileSeek (struct gdIOCtx *ctx, const int pos)
8022  {
8023 -  fileIOCtx *fctx;
8024 -  fctx = (fileIOCtx *) ctx;
8025 +       fileIOCtx *fctx;
8026 +       fctx = (fileIOCtx *) ctx;
8027  
8028 -  return (fseek (fctx->f, pos, SEEK_SET) == 0);
8029 +       return (fseek (fctx->f, pos, SEEK_SET) == 0);
8030  }
8031  
8032 -static long
8033 -fileTell (struct gdIOCtx *ctx)
8034 +static long fileTell (struct gdIOCtx *ctx)
8035  {
8036 -  fileIOCtx *fctx;
8037 -  fctx = (fileIOCtx *) ctx;
8038 +       fileIOCtx *fctx;
8039 +       fctx = (fileIOCtx *) ctx;
8040  
8041 -  return ftell (fctx->f);
8042 +       return ftell (fctx->f);
8043  }
8044 diff -urN php-4.4.8.org/ext/gd/libgd/gd_io.h php-4.4.8/ext/gd/libgd/gd_io.h
8045 --- php-4.4.8.org/ext/gd/libgd/gd_io.h  2003-04-05 19:24:16.000000000 +0200
8046 +++ php-4.4.8/ext/gd/libgd/gd_io.h      2003-12-28 21:11:08.000000000 +0100
8047 @@ -6,18 +6,18 @@
8048  #ifdef VMS
8049  #define Putchar gdPutchar
8050  #endif
8051
8052 +
8053  typedef struct gdIOCtx {
8054         int     (*getC)(struct gdIOCtx*);
8055         int     (*getBuf)(struct gdIOCtx*, void*, int);
8056  
8057 -        void     (*putC)(struct gdIOCtx*, int);
8058 +       void    (*putC)(struct gdIOCtx*, int);
8059         int     (*putBuf)(struct gdIOCtx*, const void*, int);
8060  
8061         int     (*seek)(struct gdIOCtx*, const int);
8062         long    (*tell)(struct gdIOCtx*);
8063  
8064 -       void    (*gd_free)(struct gdIOCtx*);
8065 +       void    (*gd_free)(struct gdIOCtx*);
8066  
8067  } gdIOCtx;
8068  
8069 diff -urN php-4.4.8.org/ext/gd/libgd/gd_io_ss.c php-4.4.8/ext/gd/libgd/gd_io_ss.c
8070 --- php-4.4.8.org/ext/gd/libgd/gd_io_ss.c       2002-10-30 00:08:01.000000000 +0100
8071 +++ php-4.4.8/ext/gd/libgd/gd_io_ss.c   2003-12-28 21:11:08.000000000 +0100
8072 @@ -12,7 +12,7 @@
8073     * used internally until it settles down a bit.
8074     *
8075     * This module just layers the Source/Sink interface on top of the IOCtx; no
8076 -   * support is provided for tell/seek, so GD2 writing is not possible, and 
8077 +   * support is provided for tell/seek, so GD2 writing is not possible, and
8078     * retrieving parts of GD2 files is also not possible.
8079     *
8080     * A new SS context does not need to be created with both a Source and a Sink.
8081 @@ -30,12 +30,11 @@
8082  /* this is used for creating images in main memory */
8083  
8084  typedef struct ssIOCtx
8085 -  {
8086 -    gdIOCtx ctx;
8087 -    gdSourcePtr src;
8088 -    gdSinkPtr snk;
8089 -  }
8090 -ssIOCtx;
8091 +{
8092 +       gdIOCtx ctx;
8093 +       gdSourcePtr src;
8094 +       gdSinkPtr snk;
8095 +} ssIOCtx;
8096  
8097  typedef struct ssIOCtx *ssIOCtxPtr;
8098  
8099 @@ -48,118 +47,92 @@
8100  static void gdFreeSsCtx (gdIOCtx * ctx);
8101  
8102  /* return data as a dynamic pointer */
8103 -gdIOCtx *
8104 -gdNewSSCtx (gdSourcePtr src, gdSinkPtr snk)
8105 +gdIOCtx * gdNewSSCtx (gdSourcePtr src, gdSinkPtr snk)
8106  {
8107 -  ssIOCtxPtr ctx;
8108 +       ssIOCtxPtr ctx;
8109  
8110 -  ctx = (ssIOCtxPtr) gdMalloc (sizeof (ssIOCtx));
8111 -  if (ctx == NULL)
8112 -    {
8113 -      return NULL;
8114 -    }
8115 +       ctx = (ssIOCtxPtr) gdMalloc (sizeof (ssIOCtx));
8116  
8117 -  ctx->src = src;
8118 -  ctx->snk = snk;
8119 +       ctx->src = src;
8120 +       ctx->snk = snk;
8121  
8122 -  ctx->ctx.getC = sourceGetchar;
8123 -  ctx->ctx.getBuf = sourceGetbuf;
8124 +       ctx->ctx.getC = sourceGetchar;
8125 +       ctx->ctx.getBuf = sourceGetbuf;
8126  
8127 -  ctx->ctx.putC = sinkPutchar;
8128 -  ctx->ctx.putBuf = sinkPutbuf;
8129 +       ctx->ctx.putC = sinkPutchar;
8130 +       ctx->ctx.putBuf = sinkPutbuf;
8131  
8132 -  ctx->ctx.tell = NULL;
8133 -  ctx->ctx.seek = NULL;
8134 +       ctx->ctx.tell = NULL;
8135 +       ctx->ctx.seek = NULL;
8136  
8137 -  ctx->ctx.gd_free = gdFreeSsCtx;
8138 +       ctx->ctx.gd_free = gdFreeSsCtx;
8139  
8140 -  return (gdIOCtx *) ctx;
8141 +       return (gdIOCtx *) ctx;
8142  }
8143  
8144 -static
8145 -void
8146 -gdFreeSsCtx (gdIOCtx * ctx)
8147 +static void gdFreeSsCtx (gdIOCtx * ctx)
8148  {
8149 -  gdFree (ctx);
8150 +       gdFree(ctx);
8151  }
8152  
8153  
8154 -static int
8155 -sourceGetbuf (gdIOCtx * ctx, void *buf, int size)
8156 +static int sourceGetbuf (gdIOCtx * ctx, void *buf, int size)
8157  {
8158 -  ssIOCtx *lctx;
8159 -  int res;
8160 -
8161 -  lctx = (ssIOCtx *) ctx;
8162 +       ssIOCtx *lctx;
8163 +       int res;
8164  
8165 -  res = ((lctx->src->source) (lctx->src->context, buf, size));
8166 +       lctx = (ssIOCtx *) ctx;
8167  
8168 -/*
8169 -   ** Translate the return values from the Source object: 
8170 -   ** 0 is EOF, -1 is error
8171 - */
8172 +       res = ((lctx->src->source) (lctx->src->context, buf, size));
8173  
8174 -  if (res == 0)
8175 -    {
8176 -      return EOF;
8177 -    }
8178 -  else if (res < 0)
8179 -    {
8180 -      return 0;
8181 -    }
8182 -  else
8183 -    {
8184 -      return res;
8185 -    };
8186 +       /*
8187 +        * Translate the return values from the Source object:
8188 +        * 0 is EOF, -1 is error
8189 +        */
8190  
8191 +       if (res == 0) {
8192 +               return EOF;
8193 +       } else if (res < 0) {
8194 +               return 0;
8195 +       } else {
8196 +               return res;
8197 +       }
8198  }
8199  
8200 -static int
8201 -sourceGetchar (gdIOCtx * ctx)
8202 +static int sourceGetchar (gdIOCtx * ctx)
8203  {
8204 -  int res;
8205 -  unsigned char buf;
8206 +       int res;
8207 +       unsigned char buf;
8208  
8209 -  res = sourceGetbuf (ctx, &buf, 1);
8210 -
8211 -  if (res == 1)
8212 -    {
8213 -      return buf;
8214 -    }
8215 -  else
8216 -    {
8217 -      return EOF;
8218 -    };
8219 +       res = sourceGetbuf (ctx, &buf, 1);
8220  
8221 +       if (res == 1) {
8222 +               return buf;
8223 +       } else {
8224 +               return EOF;
8225 +       }
8226  }
8227  
8228 -static int
8229 -sinkPutbuf (gdIOCtx * ctx, const void *buf, int size)
8230 +static int sinkPutbuf (gdIOCtx * ctx, const void *buf, int size)
8231  {
8232 -  ssIOCtxPtr lctx;
8233 -  int res;
8234 -
8235 -  lctx = (ssIOCtx *) ctx;
8236 +       ssIOCtxPtr lctx;
8237 +       int res;
8238  
8239 -  res = (lctx->snk->sink) (lctx->snk->context, buf, size);
8240 +       lctx = (ssIOCtx *) ctx;
8241  
8242 -  if (res <= 0)
8243 -    {
8244 -      return 0;
8245 -    }
8246 -  else
8247 -    {
8248 -      return res;
8249 -    };
8250 +       res = (lctx->snk->sink) (lctx->snk->context, buf, size);
8251  
8252 +       if (res <= 0) {
8253 +               return 0;
8254 +       } else {
8255 +               return res;
8256 +       }
8257  }
8258  
8259 -static void
8260 -sinkPutchar (gdIOCtx * ctx, int a)
8261 +static void sinkPutchar (gdIOCtx * ctx, int a)
8262  {
8263 -  unsigned char b;
8264 -
8265 -  b = a;
8266 -  sinkPutbuf (ctx, &b, 1);
8267 +       unsigned char b;
8268  
8269 +       b = a;
8270 +       sinkPutbuf (ctx, &b, 1);
8271  }
8272 diff -urN php-4.4.8.org/ext/gd/libgd/gd_jpeg.c php-4.4.8/ext/gd/libgd/gd_jpeg.c
8273 --- php-4.4.8.org/ext/gd/libgd/gd_jpeg.c        2004-03-29 20:21:00.000000000 +0200
8274 +++ php-4.4.8/ext/gd/libgd/gd_jpeg.c    2006-02-05 16:53:58.000000000 +0100
8275 @@ -1,7 +1,7 @@
8276  /*
8277   * gd_jpeg.c: Read and write JPEG (JFIF) format image files using the
8278   * gd graphics library (http://www.boutell.com/gd/).
8279 - * 
8280 + *
8281   * This software is based in part on the work of the Independent JPEG
8282   * Group.  For more information on the IJG JPEG software (and JPEG
8283   * documentation, etc.), see ftp://ftp.uu.net/graphics/jpeg/.
8284 @@ -18,13 +18,9 @@
8285   * major CGI brain damage
8286   *
8287   * 2.0.10: more efficient gdImageCreateFromJpegCtx, thanks to
8288 - * Christian Aberger 
8289 + * Christian Aberger
8290   */
8291  
8292 -#if PHP_WIN32 && !defined(ssize_t)
8293 -typedef int ssize_t;
8294 -#endif
8295 -
8296  #include <stdio.h>
8297  #include <stdlib.h>
8298  #include <setjmp.h>
8299 @@ -47,8 +43,44 @@
8300  typedef struct _jmpbuf_wrapper
8301  {
8302         jmp_buf jmpbuf;
8303 +       int ignore_warning;
8304  } jmpbuf_wrapper;
8305  
8306 +static long php_jpeg_emit_message(j_common_ptr jpeg_info, int level)
8307 +{
8308 +       char message[JMSG_LENGTH_MAX];
8309 +       jmpbuf_wrapper *jmpbufw;
8310 +       int ignore_warning = 0;
8311 +       
8312 +    jmpbufw = (jmpbuf_wrapper *) jpeg_info->client_data;
8313 +
8314 +       if (jmpbufw != 0) {
8315 +               ignore_warning = jmpbufw->ignore_warning;
8316 +       }
8317 +
8318 +       (jpeg_info->err->format_message)(jpeg_info,message);
8319 +
8320 +       /* It is a warning message */
8321 +       if (level < 0) {
8322 +               /* display only the 1st warning, as would do a default libjpeg
8323 +                * unless strace_level >= 3
8324 +                */
8325 +               if ((jpeg_info->err->num_warnings == 0) || (jpeg_info->err->trace_level >= 3)) {
8326 +                       php_gd_error_ex(ignore_warning ? E_NOTICE : E_WARNING, "gd-jpeg, libjpeg: recoverable error: %s\n", message);
8327 +               }
8328 +
8329 +               jpeg_info->err->num_warnings++;
8330 +       } else {
8331 +               /* strace msg, Show it if trace_level >= level. */
8332 +               if (jpeg_info->err->trace_level >= level) {
8333 +                       php_gd_error_ex(E_NOTICE, "gd-jpeg, libjpeg: strace message: %s\n", message);
8334 +               }
8335 +       }
8336 +       return 1;
8337 +}
8338 +
8339 +
8340 +
8341  /* Called by the IJG JPEG library upon encountering a fatal error */
8342  static void fatal_jpeg_error (j_common_ptr cinfo)
8343  {
8344 @@ -174,7 +206,7 @@
8345  
8346                         nlines = jpeg_write_scanlines (&cinfo, rowptr, 1);
8347                         if (nlines != 1) {
8348 -                               php_gd_error_ex(E_WARNING, "gd_jpeg: warning: jpeg_write_scanlines returns %u -- expected 1\n", nlines);
8349 +                               php_gd_error_ex(E_WARNING, "gd_jpeg: warning: jpeg_write_scanlines returns %u -- expected 1", nlines);
8350                         }
8351                 }
8352         } else {
8353 @@ -201,7 +233,7 @@
8354  
8355                         nlines = jpeg_write_scanlines (&cinfo, rowptr, 1);
8356                         if (nlines != 1) {
8357 -                               php_gd_error_ex(E_WARNING, "gd_jpeg: warning: jpeg_write_scanlines returns %u -- expected 1\n", nlines);
8358 +                               php_gd_error_ex(E_WARNING, "gd_jpeg: warning: jpeg_write_scanlines returns %u -- expected 1", nlines);
8359                         }
8360                 }
8361         }
8362 @@ -211,21 +243,21 @@
8363         gdFree (row);
8364  }
8365  
8366 -gdImagePtr gdImageCreateFromJpeg (FILE * inFile)
8367 +gdImagePtr gdImageCreateFromJpeg (FILE * inFile, int ignore_warning)
8368  {
8369         gdImagePtr im;
8370         gdIOCtx *in = gdNewFileCtx(inFile);
8371 -       im = gdImageCreateFromJpegCtx(in);
8372 +       im = gdImageCreateFromJpegCtx(in, ignore_warning);
8373         in->gd_free (in);
8374  
8375         return im;
8376  }
8377  
8378 -gdImagePtr gdImageCreateFromJpegPtr (int size, void *data)
8379 +gdImagePtr gdImageCreateFromJpegPtr (int size, void *data, int ignore_warning)
8380  {
8381         gdImagePtr im;
8382         gdIOCtx *in = gdNewDynamicCtxEx(size, data, 0);
8383 -       im = gdImageCreateFromJpegCtx(in);
8384 +       im = gdImageCreateFromJpegCtx(in, ignore_warning);
8385         in->gd_free(in);
8386  
8387         return im;
8388 @@ -235,11 +267,12 @@
8389  
8390  static int CMYKToRGB(int c, int m, int y, int k, int inverted);
8391  
8392 -/* 
8393 +
8394 +/*
8395   * Create a gd-format image from the JPEG-format INFILE.  Returns the
8396   * image, or NULL upon error.
8397   */
8398 -gdImagePtr gdImageCreateFromJpegCtx (gdIOCtx * infile)
8399 +gdImagePtr gdImageCreateFromJpegCtx (gdIOCtx * infile, int ignore_warning)
8400  {
8401         struct jpeg_decompress_struct cinfo;
8402         struct jpeg_error_mgr jerr;
8403 @@ -257,8 +290,13 @@
8404         memset (&cinfo, 0, sizeof (cinfo));
8405         memset (&jerr, 0, sizeof (jerr));
8406  
8407 +       jmpbufw.ignore_warning = ignore_warning;
8408 +
8409         cinfo.err = jpeg_std_error (&jerr);
8410         cinfo.client_data = &jmpbufw;
8411 +
8412 +       cinfo.err->emit_message = (void (*)(j_common_ptr,int)) php_jpeg_emit_message;
8413 +
8414         if (setjmp (jmpbufw.jmpbuf) != 0) {
8415                 /* we're here courtesy of longjmp */
8416                 if (row) {
8417 @@ -280,7 +318,7 @@
8418         jpeg_save_markers(&cinfo, JPEG_APP0 + 14, 256);
8419  
8420         retval = jpeg_read_header (&cinfo, TRUE);
8421 -       if (retval != JPEG_HEADER_OK) { 
8422 +       if (retval != JPEG_HEADER_OK) {
8423                 php_gd_error_ex(E_WARNING, "gd-jpeg: warning: jpeg_read_header returned %d, expected %d", retval, JPEG_HEADER_OK);
8424         }
8425  
8426 @@ -316,9 +354,9 @@
8427          * latest libjpeg, replaced by something else. Unfortunately
8428          * there is still no right way to find out if the file was
8429          * progressive or not; just declare your intent before you
8430 -        * write one by calling gdImageInterlace(im, 1) yourself. 
8431 +        * write one by calling gdImageInterlace(im, 1) yourself.
8432          * After all, we're not really supposed to rework JPEGs and
8433 -        * write them out again anyway. Lossy compression, remember? 
8434 +        * write them out again anyway. Lossy compression, remember?
8435          */
8436  #if 0
8437    gdImageInterlace (im, cinfo.progressive_mode != 0);
8438 @@ -390,12 +428,12 @@
8439         if (jpeg_finish_decompress (&cinfo) != TRUE) {
8440                 php_gd_error("gd-jpeg: warning: jpeg_finish_decompress reports suspended data source");
8441         }
8442 -
8443 -       /* Thanks to Truxton Fulton */
8444 -       if (cinfo.err->num_warnings > 0) {
8445 -               goto error;
8446 +       if (!ignore_warning) {
8447 +               if (cinfo.err->num_warnings > 0) {
8448 +                       goto error;
8449 +               }
8450         }
8451 -
8452 +       
8453         jpeg_destroy_decompress (&cinfo);
8454         gdFree (row);
8455  
8456 @@ -524,7 +562,7 @@
8457                 int got = gdGetBuf(src->buffer + nbytes, INPUT_BUF_SIZE - nbytes, src->infile);
8458  
8459                 if (got == EOF || got == 0) {
8460 -                       /* EOF or error. If we got any data, don't worry about it. If we didn't, then this is unexpected. */ 
8461 +                       /* EOF or error. If we got any data, don't worry about it. If we didn't, then this is unexpected. */
8462                         if (!nbytes) {
8463                                 nbytes = -1;
8464                         }
8465 @@ -532,7 +570,7 @@
8466                 }
8467                 nbytes += got;
8468         }
8469 -  
8470 +
8471         if (nbytes <= 0) {
8472                 if (src->start_of_file) { /* Treat empty input file as fatal error */
8473                         ERREXIT (cinfo, JERR_INPUT_EMPTY);
8474 @@ -634,7 +672,7 @@
8475                 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof (my_source_mgr));
8476                 src = (my_src_ptr) cinfo->src;
8477                 src->buffer = (unsigned char *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, INPUT_BUF_SIZE * sizeof (unsigned char));
8478 -      
8479 +
8480         }
8481  
8482         src = (my_src_ptr) cinfo->src;
8483 diff -urN php-4.4.8.org/ext/gd/libgd/gdkanji.c php-4.4.8/ext/gd/libgd/gdkanji.c
8484 --- php-4.4.8.org/ext/gd/libgd/gdkanji.c        2003-04-05 19:24:16.000000000 +0200
8485 +++ php-4.4.8/ext/gd/libgd/gdkanji.c    2003-12-28 21:11:08.000000000 +0100
8486 @@ -75,7 +75,7 @@
8487         va_list args;
8488         char *tmp;
8489         TSRMLS_FETCH();
8490 -       
8491 +
8492         va_start(args, format);
8493         vspprintf(&tmp, 0, format, args);
8494         va_end(args);
8495 diff -urN php-4.4.8.org/ext/gd/libgd/gd_png.c php-4.4.8/ext/gd/libgd/gd_png.c
8496 --- php-4.4.8.org/ext/gd/libgd/gd_png.c 2007-05-17 00:54:11.000000000 +0200
8497 +++ php-4.4.8/ext/gd/libgd/gd_png.c     2007-05-17 00:19:08.000000000 +0200
8498 @@ -23,7 +23,7 @@
8499      out all fprintf() statements to disable that).
8500  
8501      GD 2.0 supports RGBA truecolor and will read and write truecolor PNGs.
8502 -    GD 2.0 supports 8 bits of color resolution per channel and 
8503 +    GD 2.0 supports 8 bits of color resolution per channel and
8504      7 bits of alpha channel resolution. Images with more than 8 bits
8505      per channel are reduced to 8 bits. Images with an alpha channel are
8506      only able to resolve down to '1/128th opaque' instead of '1/256th',
8507 @@ -58,11 +58,11 @@
8508          * been defined.
8509          */
8510  
8511 -       php_gd_error_ex(E_ERROR, "gd-png:  fatal libpng error: %s\n", msg);
8512 +       php_gd_error_ex(E_WARNING, "gd-png:  fatal libpng error: %s", msg);
8513  
8514         jmpbuf_ptr = png_get_error_ptr (png_ptr);
8515         if (jmpbuf_ptr == NULL) { /* we are completely hosed now */
8516 -               php_gd_error_ex(E_ERROR, "gd-png:  EXTREMELY fatal error: jmpbuf unrecoverable; terminating.\n");
8517 +               php_gd_error_ex(E_ERROR, "gd-png:  EXTREMELY fatal error: jmpbuf unrecoverable; terminating.");
8518         }
8519  
8520         longjmp (jmpbuf_ptr->jmpbuf, 1);
8521 @@ -130,12 +130,15 @@
8522  
8523         /* Make sure the signature can't match by dumb luck -- TBB */
8524         /* GRR: isn't sizeof(infile) equal to the size of the pointer? */
8525 -       memset (infile, 0, sizeof(infile));
8526 +       memset (sig, 0, sizeof(sig));
8527  
8528           /* first do a quick check that the file really is a PNG image; could
8529 -          * have used slightly more general png_sig_cmp() function instead 
8530 +          * have used slightly more general png_sig_cmp() function instead
8531            */
8532 -       gdGetBuf(sig, 8, infile);
8533 +       if (gdGetBuf(sig, 8, infile) < 8) {
8534 +               return NULL;
8535 +       }
8536 +
8537         if (!png_check_sig (sig, 8)) { /* bad signature */
8538                 return NULL;
8539         }
8540 @@ -146,13 +149,13 @@
8541         png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
8542  #endif
8543         if (png_ptr == NULL) {
8544 -               php_gd_error("gd-png error: cannot allocate libpng main struct\n");
8545 +               php_gd_error("gd-png error: cannot allocate libpng main struct");
8546                 return NULL;
8547         }
8548  
8549         info_ptr = png_create_info_struct(png_ptr);
8550         if (info_ptr == NULL) {
8551 -               php_gd_error("gd-png error: cannot allocate libpng info struct\n");
8552 +               php_gd_error("gd-png error: cannot allocate libpng info struct");
8553                 png_destroy_read_struct (&png_ptr, NULL, NULL);
8554  
8555                 return NULL;
8556 @@ -160,7 +163,7 @@
8557  
8558         /* we could create a second info struct here (end_info), but it's only
8559          * useful if we want to keep pre- and post-IDAT chunk info separated
8560 -        * (mainly for PNG-aware image editors and converters) 
8561 +        * (mainly for PNG-aware image editors and converters)
8562          */
8563  
8564         /* setjmp() must be called in every non-callback function that calls a
8565 @@ -168,7 +171,7 @@
8566          */
8567  #ifndef PNG_SETJMP_NOT_SUPPORTED
8568         if (setjmp(gdPngJmpbufStruct.jmpbuf)) {
8569 -               php_gd_error("gd-png error: setjmp returns error condition\n");
8570 +               php_gd_error("gd-png error: setjmp returns error condition");
8571                 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
8572  
8573                 return NULL;
8574 @@ -187,7 +190,7 @@
8575                 im = gdImageCreate((int) width, (int) height);
8576         }
8577         if (im == NULL) {
8578 -               php_gd_error("gd-png error: cannot allocate gdImage struct\n");
8579 +               php_gd_error("gd-png error: cannot allocate gdImage struct");
8580                 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
8581                 gdFree(image_data);
8582                 gdFree(row_pointers);
8583 @@ -201,15 +204,32 @@
8584                 png_set_packing (png_ptr); /* expand to 1 byte per pixel */
8585         }
8586  
8587 +       /* setjmp() must be called in every non-callback function that calls a
8588 +        * PNG-reading libpng function
8589 +        */
8590 +#ifndef PNG_SETJMP_NOT_SUPPORTED
8591 +       if (setjmp(gdPngJmpbufStruct.jmpbuf)) {
8592 +               php_gd_error("gd-png error: setjmp returns error condition");
8593 +               png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
8594 +               gdFree(image_data);
8595 +               gdFree(row_pointers);
8596 +               if (im) {
8597 +                       gdImageDestroy(im);
8598 +               }
8599 +               return NULL;
8600 +       }
8601 +#endif
8602 +
8603 +
8604         switch (color_type) {
8605                 case PNG_COLOR_TYPE_PALETTE:
8606                         png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
8607  #ifdef DEBUG
8608 -                       php_gd_error("gd-png color_type is palette, colors: %d\n", num_palette);
8609 +                       php_gd_error("gd-png color_type is palette, colors: %d", num_palette);
8610  #endif /* DEBUG */
8611                         if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS)) {
8612                                 /* gd 2.0: we support this rather thoroughly now. Grab the
8613 -                                * first fully transparent entry, if any, as the value of 
8614 +                                * first fully transparent entry, if any, as the value of
8615                                  * the simple-transparency index, mostly for backwards
8616                                  * binary compatibility. The alpha channel is where it's
8617                                  * really at these days.
8618 @@ -228,8 +248,8 @@
8619                 case PNG_COLOR_TYPE_GRAY:
8620                 case PNG_COLOR_TYPE_GRAY_ALPHA:
8621                         /* create a fake palette and check for single-shade transparency */
8622 -                       if ((palette = (png_colorp) safe_emalloc(256, sizeof(png_color), 0)) == NULL) {
8623 -                               php_gd_error("gd-png error: cannot allocate gray palette\n");
8624 +                       if ((palette = (png_colorp) gdMalloc (256 * sizeof (png_color))) == NULL) {
8625 +                               php_gd_error("gd-png error: cannot allocate gray palette");
8626                                 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
8627  
8628                                 return NULL;
8629 @@ -263,7 +283,7 @@
8630                                  * custom 16-bit code to handle the case where there are gdFree
8631                                  * palette entries.  This error will be extremely rare in
8632                                  * general, though.  (Quite possibly there is only one such
8633 -                                * image in existence.) 
8634 +                                * image in existence.)
8635                                  */
8636                         }
8637                         break;
8638 @@ -272,16 +292,16 @@
8639                         case PNG_COLOR_TYPE_RGB_ALPHA:
8640                                 /* gd 2.0: we now support truecolor. See the comment above
8641                                  * for a rare situation in which the transparent pixel may not
8642 -                                * work properly with 16-bit channels. 
8643 +                                * work properly with 16-bit channels.
8644                                  */
8645                                 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
8646                                         png_get_tRNS(png_ptr, info_ptr, NULL, NULL, &trans_color_rgb);
8647                                         if (bit_depth == 16) { /* png_set_strip_16() not yet in effect */
8648 -                                               transparent = gdTrueColor(trans_color_rgb->red >> 8, 
8649 +                                               transparent = gdTrueColor(trans_color_rgb->red >> 8,
8650                                                                         trans_color_rgb->green >> 8,
8651                                                                         trans_color_rgb->blue >> 8);
8652                                         } else {
8653 -                                               transparent = gdTrueColor(trans_color_rgb->red, 
8654 +                                               transparent = gdTrueColor(trans_color_rgb->red,
8655                                                                         trans_color_rgb->green,
8656                                                                         trans_color_rgb->blue);
8657                                         }
8658 @@ -295,7 +315,7 @@
8659         rowbytes = png_get_rowbytes(png_ptr, info_ptr);
8660         image_data = (png_bytep) safe_emalloc(rowbytes, height, 0);
8661  
8662 -       row_pointers = (png_bytepp) safe_emalloc(height, sizeof (png_bytep), 0);
8663 +       row_pointers = (png_bytepp) safe_emalloc(height, sizeof(png_bytep), 0);
8664  
8665         /* set the individual row_pointers to point at the correct offsets */
8666         for (h = 0; h < height; ++h) {
8667 @@ -349,7 +369,7 @@
8668                                         register png_byte b = row_pointers[h][boffset++];
8669  
8670                                         /* gd has only 7 bits of alpha channel resolution, and
8671 -                                        * 127 is transparent, 0 opaque. A moment of convenience, 
8672 +                                        * 127 is transparent, 0 opaque. A moment of convenience,
8673                                          *  a lifetime of compatibility.
8674                                          */
8675  
8676 @@ -373,7 +393,7 @@
8677         if (!im->trueColor) {
8678                 for (i = num_palette; i < gdMaxColors; ++i) {
8679                         if (!open[i]) {
8680 -                               php_gd_error("gd-png warning: image data references out-of-range color index (%d)\n", i);
8681 +                               php_gd_error("gd-png warning: image data references out-of-range color index (%d)", i);
8682                         }
8683                 }
8684         }
8685 @@ -388,17 +408,17 @@
8686         return im;
8687  }
8688  
8689 -void gdImagePngEx (gdImagePtr im, FILE * outFile, int level)
8690 +void gdImagePngEx (gdImagePtr im, FILE * outFile, int level, int basefilter)
8691  {
8692         gdIOCtx *out = gdNewFileCtx(outFile);
8693 -       gdImagePngCtxEx(im, out, level);
8694 +       gdImagePngCtxEx(im, out, level, basefilter);
8695         out->gd_free(out);
8696  }
8697  
8698  void gdImagePng (gdImagePtr im, FILE * outFile)
8699  {
8700         gdIOCtx *out = gdNewFileCtx(outFile);
8701 -       gdImagePngCtxEx(im, out, -1);
8702 +       gdImagePngCtxEx(im, out, -1, -1);
8703         out->gd_free(out);
8704  }
8705  
8706 @@ -406,18 +426,18 @@
8707  {
8708         void *rv;
8709         gdIOCtx *out = gdNewDynamicCtx(2048, NULL);
8710 -       gdImagePngCtxEx(im, out, -1);
8711 +       gdImagePngCtxEx(im, out, -1, -1);
8712         rv = gdDPExtractData(out, size);
8713         out->gd_free(out);
8714  
8715         return rv;
8716  }
8717  
8718 -void * gdImagePngPtrEx (gdImagePtr im, int *size, int level)
8719 +void * gdImagePngPtrEx (gdImagePtr im, int *size, int level, int basefilter)
8720  {
8721         void *rv;
8722         gdIOCtx *out = gdNewDynamicCtx(2048, NULL);
8723 -       gdImagePngCtxEx(im, out, level);
8724 +       gdImagePngCtxEx(im, out, level, basefilter);
8725         rv = gdDPExtractData(out, size);
8726         out->gd_free(out);
8727         return rv;
8728 @@ -425,14 +445,14 @@
8729  
8730  void gdImagePngCtx (gdImagePtr im, gdIOCtx * outfile)
8731  {
8732 -       gdImagePngCtxEx(im, outfile, -1);
8733 +       gdImagePngCtxEx(im, outfile, -1, -1);
8734  }
8735  
8736  /* This routine is based in part on code from Dale Lutz (Safe Software Inc.)
8737   *  and in part on demo code from Chapter 15 of "PNG: The Definitive Guide"
8738   *  (http://www.cdrom.com/pub/png/pngbook.html).
8739   */
8740 -void gdImagePngCtxEx (gdImagePtr im, gdIOCtx * outfile, int level)
8741 +void gdImagePngCtxEx (gdImagePtr im, gdIOCtx * outfile, int level, int basefilter)
8742  {
8743         int i, j, bit_depth = 0, interlace_type;
8744         int width = im->sx;
8745 @@ -454,13 +474,13 @@
8746         png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
8747  #endif
8748         if (png_ptr == NULL) {
8749 -               php_gd_error("gd-png error: cannot allocate libpng main struct\n");
8750 +               php_gd_error("gd-png error: cannot allocate libpng main struct");
8751                 return;
8752         }
8753  
8754         info_ptr = png_create_info_struct(png_ptr);
8755         if (info_ptr == NULL) {
8756 -               php_gd_error("gd-png error: cannot allocate libpng info struct\n");
8757 +               php_gd_error("gd-png error: cannot allocate libpng info struct");
8758                 png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
8759  
8760                 return;
8761 @@ -468,7 +488,7 @@
8762  
8763  #ifndef PNG_SETJMP_NOT_SUPPORTED
8764         if (setjmp (gdPngJmpbufStruct.jmpbuf)) {
8765 -               php_gd_error("gd-png error: setjmp returns error condition\n");
8766 +               php_gd_error("gd-png error: setjmp returns error condition");
8767                 png_destroy_write_struct (&png_ptr, &info_ptr);
8768  
8769                 return;
8770 @@ -483,14 +503,17 @@
8771          * gd is intentionally imperfect and doesn't spend a lot of time
8772          * fussing with such things.
8773          */
8774 -       
8775 +
8776         /*  png_set_filter(png_ptr, 0, PNG_FILTER_NONE);  */
8777  
8778         /* 2.0.12: this is finally a parameter */
8779         png_set_compression_level(png_ptr, level);
8780 +       if (basefilter >= 0) {
8781 +               png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, basefilter);
8782 +       }
8783  
8784         /* can set this to a smaller value without compromising compression if all
8785 -        * image data is 16K or less; will save some decoder memory [min == 8] 
8786 +        * image data is 16K or less; will save some decoder memory [min == 8]
8787          */
8788  
8789         /*  png_set_compression_window_bits(png_ptr, 15);  */
8790 @@ -519,13 +542,13 @@
8791                         bit_depth = 1;
8792                 } else if (colors <= 4) {
8793                         bit_depth = 2;
8794 -               } else if (colors <= 16) { 
8795 +               } else if (colors <= 16) {
8796                         bit_depth = 4;
8797                 } else {
8798                         bit_depth = 8;
8799                 }
8800         }
8801 -       
8802 +
8803         interlace_type = im->interlace ? PNG_INTERLACE_ADAM7 : PNG_INTERLACE_NONE;
8804  
8805         if (im->trueColor) {
8806 @@ -552,9 +575,9 @@
8807         if (!im->trueColor) {
8808                 /* Oy veh. Remap the PNG palette to put the entries with interesting alpha channel
8809                  * values first. This minimizes the size of the tRNS chunk and thus the size
8810 -                * of the PNG file as a whole. 
8811 +                * of the PNG file as a whole.
8812                  */
8813 -               
8814 +
8815                 int tc = 0;
8816                 int i;
8817                 int j;
8818 @@ -585,7 +608,7 @@
8819                         for (i = 0; i < im->colorsTotal; i++) {
8820                                 if (!im->open[i]) {
8821                                         if (im->alpha[i] != gdAlphaOpaque) {
8822 -                                               /* Andrew Hull: >> 6, not >> 7! (gd 2.0.5) */ 
8823 +                                               /* Andrew Hull: >> 6, not >> 7! (gd 2.0.5) */
8824                                                 trans_values[j] = 255 - ((im->alpha[i] << 1) + (im->alpha[i] >> 6));
8825                                                 mapping[i] = j++;
8826                                         } else {
8827 @@ -665,7 +688,7 @@
8828                                          * PNG's convention in which 255 is opaque.
8829                                          */
8830                                         a = gdTrueColorGetAlpha(thisPixel);
8831 -                                       /* Andrew Hull: >> 6, not >> 7! (gd 2.0.5) */ 
8832 +                                       /* Andrew Hull: >> 6, not >> 7! (gd 2.0.5) */
8833                                         *pOutputRow++ = 255 - ((a << 1) + (a >> 6));
8834                                 }
8835                         }
8836 @@ -677,12 +700,12 @@
8837                 for (j = 0; j < height; ++j) {
8838                         gdFree(row_pointers[j]);
8839                 }
8840 -       
8841 +
8842                 gdFree(row_pointers);
8843         } else {
8844                 if (remap) {
8845                         png_bytep *row_pointers;
8846 -                       row_pointers = safe_emalloc(sizeof(png_bytep), height, 0);
8847 +                       row_pointers = safe_emalloc(height, sizeof(png_bytep), 0);
8848                         for (j = 0; j < height; ++j) {
8849                                 row_pointers[j] = (png_bytep) gdMalloc(width);
8850                                 for (i = 0; i < width; ++i) {
8851 diff -urN php-4.4.8.org/ext/gd/libgd/gd_security.c php-4.4.8/ext/gd/libgd/gd_security.c
8852 --- php-4.4.8.org/ext/gd/libgd/gd_security.c    2007-03-10 14:06:37.000000000 +0100
8853 +++ php-4.4.8/ext/gd/libgd/gd_security.c        2007-10-23 03:58:08.000000000 +0200
8854 @@ -19,12 +19,10 @@
8855  
8856  int overflow2(int a, int b)
8857  {
8858 -       if(a < 0 || b < 0) {
8859 -               php_gd_error("gd warning: one parameter to a memory allocation multiplication is negative, failing operation gracefully\n");
8860 +       if(a <= 0 || b <= 0) {
8861 +               php_gd_error("gd warning: one parameter to a memory allocation multiplication is negative or zero, failing operation gracefully\n");
8862                 return 1;
8863         }
8864 -       if(b == 0)
8865 -               return 0;
8866         if(a > INT_MAX / b) {
8867                 php_gd_error("gd warning: product of memory allocation multiplication would exceed INT_MAX, failing operation gracefully\n");
8868                 return 1;
8869 diff -urN php-4.4.8.org/ext/gd/libgd/gd_ss.c php-4.4.8/ext/gd/libgd/gd_ss.c
8870 --- php-4.4.8.org/ext/gd/libgd/gd_ss.c  2003-03-05 17:04:20.000000000 +0100
8871 +++ php-4.4.8/ext/gd/libgd/gd_ss.c      2005-08-18 14:54:44.000000000 +0200
8872 @@ -17,37 +17,33 @@
8873  #define GD_SS_DBG(s)
8874  
8875  #ifdef HAVE_LIBPNG
8876 -void
8877 -gdImagePngToSink (gdImagePtr im, gdSinkPtr outSink)
8878 +void gdImagePngToSink (gdImagePtr im, gdSinkPtr outSink)
8879  {
8880 -  gdIOCtx *out = gdNewSSCtx (NULL, outSink);
8881 -  gdImagePngCtx (im, out);
8882 -  out->gd_free (out);
8883 +       gdIOCtx *out = gdNewSSCtx(NULL, outSink);
8884 +       gdImagePngCtx(im, out);
8885 +       out->gd_free(out);
8886  }
8887  
8888 -gdImagePtr
8889 -gdImageCreateFromPngSource (gdSourcePtr inSource)
8890 +gdImagePtr gdImageCreateFromPngSource (gdSourcePtr inSource)
8891  {
8892 -  gdIOCtx *in = gdNewSSCtx (inSource, NULL);
8893 -  gdImagePtr im;
8894 +       gdIOCtx *in = gdNewSSCtx(inSource, NULL);
8895 +       gdImagePtr im;
8896  
8897 -  im = gdImageCreateFromPngCtx (in);
8898 +       im = gdImageCreateFromPngCtx(in);
8899  
8900 -  in->gd_free (in);
8901 +       in->gd_free(in);
8902  
8903 -  return im;
8904 +       return im;
8905  }
8906  #else /* no HAVE_LIBPNG */
8907 -void
8908 -gdImagePngToSink (gdImagePtr im, gdSinkPtr outSink)
8909 +void gdImagePngToSink (gdImagePtr im, gdSinkPtr outSink)
8910  {
8911 -  php_gd_error("PNG support is not available\n");
8912 +       php_gd_error("PNG support is not available");
8913  }
8914 -gdImagePtr
8915 -gdImageCreateFromPngSource (gdSourcePtr inSource)
8916 +gdImagePtr gdImageCreateFromPngSource (gdSourcePtr inSource)
8917  {
8918 -  php_gd_error("PNG support is not available\n");
8919 -  return NULL;
8920 +       php_gd_error("PNG support is not available");
8921 +       return NULL;
8922  }
8923  #endif /* HAVE_LIBPNG */
8924  
8925 diff -urN php-4.4.8.org/ext/gd/libgd/gdtables.c php-4.4.8/ext/gd/libgd/gdtables.c
8926 --- php-4.4.8.org/ext/gd/libgd/gdtables.c       2002-04-13 04:03:09.000000000 +0200
8927 +++ php-4.4.8/ext/gd/libgd/gdtables.c   2006-09-15 17:11:54.000000000 +0200
8928 @@ -1,5 +1,11 @@
8929  
8930 -int gdCosT[] =
8931 +#ifdef HAVE_CONFIG_H
8932 +#include "config.h"
8933 +#endif
8934 +
8935 +#include "php_compat.h"
8936 +
8937 +const int gdCosT[] =
8938  {
8939    1024,
8940    1023,
8941 @@ -363,7 +369,7 @@
8942    1023
8943  };
8944  
8945 -int gdSinT[] =
8946 +const int gdSinT[] =
8947  {
8948    0,
8949    17,
8950 diff -urN php-4.4.8.org/ext/gd/libgd/gdtest.c php-4.4.8/ext/gd/libgd/gdtest.c
8951 --- php-4.4.8.org/ext/gd/libgd/gdtest.c 2002-10-30 00:08:01.000000000 +0100
8952 +++ php-4.4.8/ext/gd/libgd/gdtest.c     2007-02-24 03:17:24.000000000 +0100
8953 @@ -56,7 +56,7 @@
8954    /* */
8955    /* Send to PNG File then Ptr */
8956    /* */
8957 -  sprintf (of, "%s.png", argv[1]);
8958 +  snprintf (of, sizeof(of), "%s.png", argv[1]);
8959    out = fopen (of, "wb");
8960    gdImagePng (im, out);
8961    fclose (out);
8962 @@ -88,7 +88,7 @@
8963    /* */
8964    /* Send to GD2 File then Ptr */
8965    /* */
8966 -  sprintf (of, "%s.gd2", argv[1]);
8967 +  snprintf (of, sizeof(of), "%s.gd2", argv[1]);
8968    out = fopen (of, "wb");
8969    gdImageGd2 (im, out, 128, 2);
8970    fclose (out);
8971 @@ -123,7 +123,7 @@
8972    /* */
8973    /* Send to GD File then Ptr */
8974    /* */
8975 -  sprintf (of, "%s.gd", argv[1]);
8976 +  snprintf (of, sizeof(of), "%s.gd", argv[1]);
8977    out = fopen (of, "wb");
8978    gdImageGd (im, out);
8979    fclose (out);
8980 @@ -180,7 +180,7 @@
8981       ** Test gdImagePngToSink'
8982       * */
8983  
8984 -  sprintf (of, "%s.snk", argv[1]);
8985 +  snprintf (of, sizeof(of), "%s.snk", argv[1]);
8986    out = fopen (of, "wb");
8987    imgsnk.sink = fwriteWrapper;
8988    imgsnk.context = out;
8989 diff -urN php-4.4.8.org/ext/gd/libgd/gd_topal.c php-4.4.8/ext/gd/libgd/gd_topal.c
8990 --- php-4.4.8.org/ext/gd/libgd/gd_topal.c       2004-08-12 01:25:54.000000000 +0200
8991 +++ php-4.4.8/ext/gd/libgd/gd_topal.c   2007-04-04 02:30:18.000000000 +0200
8992 @@ -772,6 +772,7 @@
8993        nim->green[icolor] = 255;
8994        nim->blue[icolor] = 255;
8995      }
8996 +               nim->open[icolor] = 0;
8997  #endif
8998  }
8999  
9000 @@ -2086,6 +2087,9 @@
9001         if( (im1->sx != im2->sx) || (im1->sy != im2->sy) ) {
9002                 return -3; /* the images are meant to be the same dimensions */
9003         }
9004 +       if (im2->colorsTotal<1) {
9005 +               return -4; /* At least 1 color must be allocated */
9006 +       }
9007  
9008         buf = (unsigned long *)safe_emalloc(sizeof(unsigned long), 5 * im2->colorsTotal, 0);
9009         memset( buf, 0, sizeof(unsigned long) * 5 * im2->colorsTotal );
9010 diff -urN php-4.4.8.org/ext/gd/libgd/gd_wbmp.c php-4.4.8/ext/gd/libgd/gd_wbmp.c
9011 --- php-4.4.8.org/ext/gd/libgd/gd_wbmp.c        2004-03-29 20:21:00.000000000 +0200
9012 +++ php-4.4.8/ext/gd/libgd/gd_wbmp.c    2005-08-18 14:54:44.000000000 +0200
9013 @@ -1,10 +1,8 @@
9014 -
9015 -
9016  /*
9017     WBMP: Wireless Bitmap Type 0: B/W, Uncompressed Bitmap
9018 -   Specification of the WBMP format can be found in the file: 
9019 +   Specification of the WBMP format can be found in the file:
9020     SPEC-WAESpec-19990524.pdf
9021 -   You can download the WAP specification on: http://www.wapforum.com/ 
9022 +   You can download the WAP specification on: http://www.wapforum.com/
9023  
9024     gd_wbmp.c
9025  
9026 @@ -16,7 +14,7 @@
9027     (wbmp library included, but you can find the latest distribution
9028     at http://www.vandenbrande.com/wbmp)
9029  
9030 -   Implemented: gdImageCreateFromWBMPCtx, gdImageCreateFromWBMP 
9031 +   Implemented: gdImageCreateFromWBMPCtx, gdImageCreateFromWBMP
9032  
9033     ---------------------------------------------------------------------------
9034  
9035 @@ -34,7 +32,7 @@
9036     ** implied warranty.
9037  
9038     ---------------------------------------------------------------------------
9039 -   Parts od this code are inspired by  'pbmtowbmp.c' and 'wbmptopbm.c' by 
9040 +   Parts od this code are inspired by  'pbmtowbmp.c' and 'wbmptopbm.c' by
9041     Terje Sannum <terje@looplab.com>.
9042     **
9043     ** Permission to use, copy, modify, and distribute this software and its
9044 @@ -67,10 +65,9 @@
9045     ** Wrapper around gdPutC for use with writewbmp
9046     **
9047   */
9048 -void
9049 -gd_putout (int i, void *out)
9050 +void gd_putout (int i, void *out)
9051  {
9052 -  gdPutC (i, (gdIOCtx *) out);
9053 +       gdPutC(i, (gdIOCtx *) out);
9054  }
9055  
9056  
9057 @@ -79,10 +76,9 @@
9058     ** Wrapper around gdGetC for use with readwbmp
9059     **
9060   */
9061 -int
9062 -gd_getin (void *in)
9063 +int gd_getin (void *in)
9064  {
9065 -  return (gdGetC ((gdIOCtx *) in));
9066 +       return (gdGetC((gdIOCtx *) in));
9067  }
9068  
9069  
9070 @@ -91,105 +87,93 @@
9071     **  Write the image as a wbmp file
9072     **  Parameters are:
9073     **  image:  gd image structure;
9074 -   **  fg:     the index of the foreground color. any other value will be 
9075 +   **  fg:     the index of the foreground color. any other value will be
9076     **          considered as background and will not be written
9077     **  out:    the stream where to write
9078   */
9079 -void
9080 -gdImageWBMPCtx (gdImagePtr image, int fg, gdIOCtx * out)
9081 +void gdImageWBMPCtx (gdImagePtr image, int fg, gdIOCtx * out)
9082  {
9083 +       int x, y, pos;
9084 +       Wbmp *wbmp;
9085  
9086 -  int x, y, pos;
9087 -  Wbmp *wbmp;
9088 +       /* create the WBMP */
9089 +       if ((wbmp = createwbmp (gdImageSX (image), gdImageSY (image), WBMP_WHITE)) == NULL) {
9090 +               php_gd_error("Could not create WBMP");
9091 +       }
9092  
9093 +       /* fill up the WBMP structure */
9094 +       pos = 0;
9095 +       for (y = 0; y < gdImageSY(image); y++) {
9096 +               for (x = 0; x < gdImageSX(image); x++) {
9097 +                       if (gdImageGetPixel (image, x, y) == fg) {
9098 +                               wbmp->bitmap[pos] = WBMP_BLACK;
9099 +                       }
9100 +                       pos++;
9101 +               }
9102 +       }
9103  
9104 -  /* create the WBMP */
9105 -  if ((wbmp = createwbmp (gdImageSX (image), gdImageSY (image), WBMP_WHITE)) == NULL)
9106 -    php_gd_error("Could not create WBMP\n");
9107 -
9108 -  /* fill up the WBMP structure */
9109 -  pos = 0;
9110 -  for (y = 0; y < gdImageSY (image); y++)
9111 -    {
9112 -      for (x = 0; x < gdImageSX (image); x++)
9113 -       {
9114 -         if (gdImageGetPixel (image, x, y) == fg)
9115 -           {
9116 -             wbmp->bitmap[pos] = WBMP_BLACK;
9117 -           }
9118 -         pos++;
9119 -       }
9120 -    }
9121 -
9122 -  /* write the WBMP to a gd file descriptor */
9123 -  if (writewbmp (wbmp, &gd_putout, out))
9124 -    php_gd_error("Could not save WBMP\n");
9125 -  /* des submitted this bugfix: gdFree the memory. */
9126 -  freewbmp (wbmp);
9127 +       /* write the WBMP to a gd file descriptor */
9128 +       if (writewbmp (wbmp, &gd_putout, out)) {
9129 +               php_gd_error("Could not save WBMP");
9130 +       }
9131 +       /* des submitted this bugfix: gdFree the memory. */
9132 +       freewbmp(wbmp);
9133  }
9134  
9135 -
9136  /* gdImageCreateFromWBMPCtx
9137     ** ------------------------
9138     ** Create a gdImage from a WBMP file input from an gdIOCtx
9139   */
9140 -gdImagePtr
9141 -gdImageCreateFromWBMPCtx (gdIOCtx * infile)
9142 +gdImagePtr gdImageCreateFromWBMPCtx (gdIOCtx * infile)
9143  {
9144 -  /* FILE *wbmp_file; */
9145 -  Wbmp *wbmp;
9146 -  gdImagePtr im = NULL;
9147 -  int black, white;
9148 -  int col, row, pos;
9149 -
9150 -  if (readwbmp (&gd_getin, infile, &wbmp))
9151 -    return (NULL);
9152 -
9153 -  if (!(im = gdImageCreate (wbmp->width, wbmp->height)))
9154 -    {
9155 -      freewbmp (wbmp);
9156 -      return (NULL);
9157 -    }
9158 -
9159 -  /* create the background color */
9160 -  white = gdImageColorAllocate (im, 255, 255, 255);
9161 -  /* create foreground color */
9162 -  black = gdImageColorAllocate (im, 0, 0, 0);
9163 -
9164 -  /* fill in image (in a wbmp 1 = white/ 0 = black) */
9165 -  pos = 0;
9166 -  for (row = 0; row < wbmp->height; row++)
9167 -    {
9168 -      for (col = 0; col < wbmp->width; col++)
9169 -       {
9170 -         if (wbmp->bitmap[pos++] == WBMP_WHITE)
9171 -           {
9172 -             gdImageSetPixel (im, col, row, white);
9173 -           }
9174 -         else
9175 -           {
9176 -             gdImageSetPixel (im, col, row, black);
9177 -           }
9178 +       /* FILE *wbmp_file; */
9179 +       Wbmp *wbmp;
9180 +       gdImagePtr im = NULL;
9181 +       int black, white;
9182 +       int col, row, pos;
9183 +
9184 +       if (readwbmp (&gd_getin, infile, &wbmp)) {
9185 +               return NULL;
9186         }
9187 -    }
9188  
9189 -  freewbmp (wbmp);
9190 +       if (!(im = gdImageCreate (wbmp->width, wbmp->height))) {
9191 +               freewbmp (wbmp);
9192 +               return NULL;
9193 +       }
9194  
9195 -  return (im);
9196 -}
9197 +       /* create the background color */
9198 +       white = gdImageColorAllocate(im, 255, 255, 255);
9199 +       /* create foreground color */
9200 +       black = gdImageColorAllocate(im, 0, 0, 0);
9201 +
9202 +       /* fill in image (in a wbmp 1 = white/ 0 = black) */
9203 +       pos = 0;
9204 +       for (row = 0; row < wbmp->height; row++) {
9205 +               for (col = 0; col < wbmp->width; col++) {
9206 +                       if (wbmp->bitmap[pos++] == WBMP_WHITE) {
9207 +                               gdImageSetPixel(im, col, row, white);
9208 +                       } else {
9209 +                               gdImageSetPixel(im, col, row, black);
9210 +                       }
9211 +               }
9212 +       }
9213  
9214 +       freewbmp(wbmp);
9215 +
9216 +       return im;
9217 +}
9218  
9219  /* gdImageCreateFromWBMP
9220     ** ---------------------
9221   */
9222 -gdImagePtr
9223 -gdImageCreateFromWBMP (FILE * inFile)
9224 +gdImagePtr gdImageCreateFromWBMP (FILE * inFile)
9225  {
9226 -  gdImagePtr im;
9227 -  gdIOCtx *in = gdNewFileCtx (inFile);
9228 -  im = gdImageCreateFromWBMPCtx (in);
9229 -  in->gd_free (in);
9230 -  return (im);
9231 +       gdImagePtr im;
9232 +       gdIOCtx *in = gdNewFileCtx(inFile);
9233 +       im = gdImageCreateFromWBMPCtx(in);
9234 +       in->gd_free(in);
9235 +
9236 +       return im;
9237  }
9238  
9239  gdImagePtr gdImageCreateFromWBMPPtr (int size, void *data)
9240 @@ -204,24 +188,23 @@
9241  /* gdImageWBMP
9242     ** -----------
9243   */
9244 -void
9245 -gdImageWBMP (gdImagePtr im, int fg, FILE * outFile)
9246 +void gdImageWBMP (gdImagePtr im, int fg, FILE * outFile)
9247  {
9248 -  gdIOCtx *out = gdNewFileCtx (outFile);
9249 -  gdImageWBMPCtx (im, fg, out);
9250 -  out->gd_free (out);
9251 +       gdIOCtx *out = gdNewFileCtx(outFile);
9252 +       gdImageWBMPCtx(im, fg, out);
9253 +       out->gd_free(out);
9254  }
9255  
9256  /* gdImageWBMPPtr
9257     ** --------------
9258   */
9259 -void *
9260 -gdImageWBMPPtr (gdImagePtr im, int *size, int fg)
9261 +void * gdImageWBMPPtr (gdImagePtr im, int *size, int fg)
9262  {
9263 -  void *rv;
9264 -  gdIOCtx *out = gdNewDynamicCtx (2048, NULL);
9265 -  gdImageWBMPCtx (im, fg, out);
9266 -  rv = gdDPExtractData (out, size);
9267 -  out->gd_free (out);
9268 -  return rv;
9269 +       void *rv;
9270 +       gdIOCtx *out = gdNewDynamicCtx(2048, NULL);
9271 +       gdImageWBMPCtx(im, fg, out);
9272 +       rv = gdDPExtractData(out, size);
9273 +       out->gd_free(out);
9274 +
9275 +       return rv;
9276  }
9277 diff -urN php-4.4.8.org/ext/gd/libgd/gdxpm.c php-4.4.8/ext/gd/libgd/gdxpm.c
9278 --- php-4.4.8.org/ext/gd/libgd/gdxpm.c  2003-06-04 01:42:49.000000000 +0200
9279 +++ php-4.4.8/ext/gd/libgd/gdxpm.c      2005-08-18 14:54:44.000000000 +0200
9280 @@ -1,8 +1,8 @@
9281  
9282 -/* 
9283 +/*
9284     add ability to load xpm files to gd, requires the xpm
9285     library.
9286 -   Caolan.McNamara@ul.ie 
9287 +   Caolan.McNamara@ul.ie
9288     http://www.csn.ul.ie/~caolan
9289   */
9290  #include <stdio.h>
9291 @@ -15,123 +15,125 @@
9292  
9293  #include <X11/xpm.h>
9294  
9295 -gdImagePtr
9296 -gdImageCreateFromXpm (char *filename)
9297 +gdImagePtr gdImageCreateFromXpm (char *filename)
9298  {
9299 -  XpmInfo info;
9300 -  XpmImage image;
9301 -  int i, j, k, number;
9302 -  char buf[5];
9303 -  gdImagePtr im = 0;
9304 -  char *apixel;
9305 -  int *pointer;
9306 -  int red = 0, green = 0, blue = 0;
9307 -  int *colors;
9308 -  int ret;
9309 -
9310 -  ret = XpmReadFileToXpmImage (filename, &image, &info);
9311 -  if (ret != XpmSuccess)
9312 -    return 0;
9313 -
9314 -  if (!(im = gdImageCreate (image.width, image.height)))
9315 -    return 0;
9316 -
9317 -  number = image.ncolors;
9318 -  colors = (int *) safe_emalloc(number, sizeof(int), 0);
9319 -  for (i = 0; i < number; i++)
9320 -    {
9321 -      switch (strlen (image.colorTable[i].c_color))
9322 -       {
9323 -       case 4:
9324 -         buf[1] = '\0';
9325 -         buf[0] = image.colorTable[i].c_color[1];
9326 -         red = strtol (buf, NULL, 16);
9327 -
9328 -         buf[0] = image.colorTable[i].c_color[3];
9329 -         green = strtol (buf, NULL, 16);
9330 -
9331 -         buf[0] = image.colorTable[i].c_color[5];
9332 -         blue = strtol (buf, NULL, 16);
9333 -         break;
9334 -       case 7:
9335 -         buf[2] = '\0';
9336 -         buf[0] = image.colorTable[i].c_color[1];
9337 -         buf[1] = image.colorTable[i].c_color[2];
9338 -         red = strtol (buf, NULL, 16);
9339 -
9340 -         buf[0] = image.colorTable[i].c_color[3];
9341 -         buf[1] = image.colorTable[i].c_color[4];
9342 -         green = strtol (buf, NULL, 16);
9343 -
9344 -         buf[0] = image.colorTable[i].c_color[5];
9345 -         buf[1] = image.colorTable[i].c_color[6];
9346 -         blue = strtol (buf, NULL, 16);
9347 -         break;
9348 -       case 10:
9349 -         buf[3] = '\0';
9350 -         buf[0] = image.colorTable[i].c_color[1];
9351 -         buf[1] = image.colorTable[i].c_color[2];
9352 -         buf[2] = image.colorTable[i].c_color[3];
9353 -         red = strtol (buf, NULL, 16);
9354 -         red /= 64;
9355 -
9356 -         buf[0] = image.colorTable[i].c_color[4];
9357 -         buf[1] = image.colorTable[i].c_color[5];
9358 -         buf[2] = image.colorTable[i].c_color[6];
9359 -         green = strtol (buf, NULL, 16);
9360 -         green /= 64;
9361 -
9362 -         buf[0] = image.colorTable[i].c_color[7];
9363 -         buf[1] = image.colorTable[i].c_color[8];
9364 -         buf[2] = image.colorTable[i].c_color[9];
9365 -         blue = strtol (buf, NULL, 16);
9366 -         blue /= 64;
9367 -         break;
9368 -       case 13:
9369 -         buf[4] = '\0';
9370 -         buf[0] = image.colorTable[i].c_color[1];
9371 -         buf[1] = image.colorTable[i].c_color[2];
9372 -         buf[2] = image.colorTable[i].c_color[3];
9373 -         buf[3] = image.colorTable[i].c_color[4];
9374 -         red = strtol (buf, NULL, 16);
9375 -         red /= 256;
9376 -
9377 -         buf[0] = image.colorTable[i].c_color[5];
9378 -         buf[1] = image.colorTable[i].c_color[6];
9379 -         buf[2] = image.colorTable[i].c_color[7];
9380 -         buf[3] = image.colorTable[i].c_color[8];
9381 -         green = strtol (buf, NULL, 16);
9382 -         green /= 256;
9383 -
9384 -         buf[0] = image.colorTable[i].c_color[9];
9385 -         buf[1] = image.colorTable[i].c_color[10];
9386 -         buf[2] = image.colorTable[i].c_color[11];
9387 -         buf[3] = image.colorTable[i].c_color[12];
9388 -         blue = strtol (buf, NULL, 16);
9389 -         blue /= 256;
9390 -         break;
9391 +       XpmInfo info;
9392 +       XpmImage image;
9393 +       int i, j, k, number;
9394 +       char buf[5];
9395 +       gdImagePtr im = 0;
9396 +       char *apixel;
9397 +       int *pointer;
9398 +       int red = 0, green = 0, blue = 0;
9399 +       int *colors;
9400 +       int ret;
9401 +
9402 +       ret = XpmReadFileToXpmImage(filename, &image, &info);
9403 +       if (ret != XpmSuccess) {
9404 +               return 0;
9405         }
9406  
9407 +       if (!(im = gdImageCreate(image.width, image.height))) {
9408 +               return 0;
9409 +       }
9410  
9411 -      colors[i] = gdImageColorResolve (im, red, green, blue);
9412 -      if (colors[i] == -1)
9413 -       php_gd_error("ARRRGH\n");
9414 -    }
9415 -
9416 -  apixel = (char *) gdMalloc (image.cpp + 1);
9417 -  apixel[image.cpp] = '\0';
9418 -
9419 -  pointer = (int *) image.data;
9420 -  for (i = 0; i < image.height; i++)
9421 -    {
9422 -      for (j = 0; j < image.width; j++)
9423 -       {
9424 -         k = *pointer++;
9425 -         gdImageSetPixel (im, j, i, colors[k]);
9426 +       number = image.ncolors;
9427 +       colors = (int *) safe_emalloc(number, sizeof(int), 0);
9428 +       for (i = 0; i < number; i++) {
9429 +               switch (strlen (image.colorTable[i].c_color)) {
9430 +                       case 4:
9431 +                               buf[1] = '\0';
9432 +                               buf[0] = image.colorTable[i].c_color[1];
9433 +                               red = strtol(buf, NULL, 16);
9434 +
9435 +                               buf[0] = image.colorTable[i].c_color[2];
9436 +                               green = strtol(buf, NULL, 16);
9437 +
9438 +                               buf[0] = image.colorTable[i].c_color[3];
9439 +                               blue = strtol(buf, NULL, 16);
9440 +                               break;
9441 +
9442 +                       case 7:
9443 +                               buf[2] = '\0';
9444 +                               buf[0] = image.colorTable[i].c_color[1];
9445 +                               buf[1] = image.colorTable[i].c_color[2];
9446 +                               red = strtol(buf, NULL, 16);
9447 +
9448 +                               buf[0] = image.colorTable[i].c_color[3];
9449 +                               buf[1] = image.colorTable[i].c_color[4];
9450 +                               green = strtol(buf, NULL, 16);
9451 +
9452 +                               buf[0] = image.colorTable[i].c_color[5];
9453 +                               buf[1] = image.colorTable[i].c_color[6];
9454 +                               blue = strtol(buf, NULL, 16);
9455 +                               break;
9456 +
9457 +                       case 10:
9458 +                               buf[3] = '\0';
9459 +                               buf[0] = image.colorTable[i].c_color[1];
9460 +                               buf[1] = image.colorTable[i].c_color[2];
9461 +                               buf[2] = image.colorTable[i].c_color[3];
9462 +                               red = strtol(buf, NULL, 16);
9463 +                               red /= 64;
9464 +
9465 +                               buf[0] = image.colorTable[i].c_color[4];
9466 +                               buf[1] = image.colorTable[i].c_color[5];
9467 +                               buf[2] = image.colorTable[i].c_color[6];
9468 +                               green = strtol(buf, NULL, 16);
9469 +                               green /= 64;
9470 +
9471 +                               buf[0] = image.colorTable[i].c_color[7];
9472 +                               buf[1] = image.colorTable[i].c_color[8];
9473 +                               buf[2] = image.colorTable[i].c_color[9];
9474 +                               blue = strtol(buf, NULL, 16);
9475 +                               blue /= 64;
9476 +                               break;
9477 +
9478 +                       case 13:
9479 +                               buf[4] = '\0';
9480 +                               buf[0] = image.colorTable[i].c_color[1];
9481 +                               buf[1] = image.colorTable[i].c_color[2];
9482 +                               buf[2] = image.colorTable[i].c_color[3];
9483 +                               buf[3] = image.colorTable[i].c_color[4];
9484 +                               red = strtol(buf, NULL, 16);
9485 +                               red /= 256;
9486 +
9487 +                               buf[0] = image.colorTable[i].c_color[5];
9488 +                               buf[1] = image.colorTable[i].c_color[6];
9489 +                               buf[2] = image.colorTable[i].c_color[7];
9490 +                               buf[3] = image.colorTable[i].c_color[8];
9491 +                               green = strtol(buf, NULL, 16);
9492 +                               green /= 256;
9493 +
9494 +                               buf[0] = image.colorTable[i].c_color[9];
9495 +                               buf[1] = image.colorTable[i].c_color[10];
9496 +                               buf[2] = image.colorTable[i].c_color[11];
9497 +                               buf[3] = image.colorTable[i].c_color[12];
9498 +                               blue = strtol(buf, NULL, 16);
9499 +                               blue /= 256;
9500 +                               break;
9501 +               }
9502 +
9503 +
9504 +               colors[i] = gdImageColorResolve(im, red, green, blue);
9505 +               if (colors[i] == -1) {
9506 +                       php_gd_error("ARRRGH");
9507 +               }
9508         }
9509 -    }
9510 -  gdFree (apixel);
9511 -  gdFree (colors);
9512 -  return (im);
9513 +
9514 +       apixel = (char *) gdMalloc(image.cpp + 1);
9515 +       apixel[image.cpp] = '\0';
9516 +
9517 +       pointer = (int *) image.data;
9518 +       for (i = 0; i < image.height; i++) {
9519 +               for (j = 0; j < image.width; j++) {
9520 +                       k = *pointer++;
9521 +                       gdImageSetPixel(im, j, i, colors[k]);
9522 +               }
9523 +       }
9524 +
9525 +       gdFree(apixel);
9526 +       gdFree(colors);
9527 +       return im;
9528  }
9529  #endif
9530 diff -urN php-4.4.8.org/ext/gd/libgd/testac.c php-4.4.8/ext/gd/libgd/testac.c
9531 --- php-4.4.8.org/ext/gd/libgd/testac.c 2002-04-13 04:03:09.000000000 +0200
9532 +++ php-4.4.8/ext/gd/libgd/testac.c     2003-12-28 21:11:08.000000000 +0100
9533 @@ -33,7 +33,7 @@
9534      }
9535    /* Load original PNG, which should contain alpha channel
9536       information. We will use it in two ways: preserving it
9537 -     literally, for use with compatible browsers, and 
9538 +     literally, for use with compatible browsers, and
9539       compositing it ourselves against a background of our
9540       choosing (alpha blending). We'll change its size
9541       and try creating palette versions of it. */
9542 @@ -80,7 +80,7 @@
9543    /* Create output image. */
9544    im_out = gdImageCreateTrueColor ((int) (gdImageSX (im_in) * scale),
9545                                    (int) (gdImageSY (im_in) * scale));
9546 -  /*      
9547 +  /*
9548       Request alpha blending. This causes future
9549       drawing operations to perform alpha channel blending
9550       with the background, resulting in an opaque image.
9551 @@ -111,7 +111,7 @@
9552  
9553    /* If this image is the result of alpha channel blending,
9554       it will not contain an interesting alpha channel itself.
9555 -     Save a little file size by not saving the alpha channel. 
9556 +     Save a little file size by not saving the alpha channel.
9557       Otherwise the file would typically be slightly larger. */
9558    gdImageSaveAlpha (im_out, !blending);
9559  
9560 diff -urN php-4.4.8.org/ext/gd/libgd/wbmp.c php-4.4.8/ext/gd/libgd/wbmp.c
9561 --- php-4.4.8.org/ext/gd/libgd/wbmp.c   2007-03-10 14:06:37.000000000 +0100
9562 +++ php-4.4.8/ext/gd/libgd/wbmp.c       2007-03-10 13:18:36.000000000 +0100
9563 @@ -28,7 +28,7 @@
9564  
9565  /* getmbi
9566     ** ------
9567 -   ** Get a multibyte integer from a generic getin function 
9568 +   ** Get a multibyte integer from a generic getin function
9569     ** 'getin' can be getc, with in = NULL
9570     ** you can find getin as a function just above the main function
9571     ** This way you gain a lot of flexibilty about how this package
9572 @@ -125,7 +125,7 @@
9573      return NULL;
9574    }
9575  
9576 -  if ((wbmp->bitmap = (int *) safe_emalloc(sizeof(int), (width * height), 0)) == NULL)
9577 +  if ((wbmp->bitmap = (int *) safe_emalloc(sizeof(int), width * height, 0)) == NULL)
9578      {
9579        gdFree (wbmp);
9580        return (NULL);
9581 @@ -192,7 +192,7 @@
9582        return (-1);
9583      }
9584  
9585 -  if ((wbmp->bitmap = (int *) safe_emalloc(sizeof(int), (wbmp->width * wbmp->height), 0)) == NULL)
9586 +  if ((wbmp->bitmap = (int *) safe_emalloc((size_t)wbmp->width * wbmp->height, sizeof(int), 0)) == NULL)
9587      {
9588        gdFree (wbmp);
9589        return (-1);
9590 @@ -240,7 +240,7 @@
9591     ** Why not just giving a filedescriptor to this function?
9592     ** Well, the incentive to write this function was the complete
9593     ** integration in gd library from www.boutell.com. They use
9594 -   ** their own io functions, so the passing of a function seemed to be 
9595 +   ** their own io functions, so the passing of a function seemed to be
9596     ** a logic(?) decision ...
9597     **
9598   */
9599 @@ -335,7 +335,7 @@
9600    return (putc (c, (FILE *) out));
9601  }
9602  
9603 -/* getin from file descriptor 
9604 +/* getin from file descriptor
9605     ** --------------------------
9606   */
9607  int
9608 diff -urN php-4.4.8.org/ext/gd/libgd/wbmp.h php-4.4.8/ext/gd/libgd/wbmp.h
9609 --- php-4.4.8.org/ext/gd/libgd/wbmp.h   2002-04-13 04:03:09.000000000 +0200
9610 +++ php-4.4.8/ext/gd/libgd/wbmp.h       2005-10-09 14:06:27.000000000 +0200
9611 @@ -8,17 +8,22 @@
9612  ** (c) 2000 Johan Van den Brande <johan@vandenbrande.com>
9613  **
9614  ** Header file
9615 -*/        
9616 +*/
9617  #ifndef __WBMP_H
9618  #define __WBMP_H       1
9619  
9620 +#ifdef HAVE_CONFIG_H
9621 +#include "config.h"
9622 +#endif
9623 +
9624 +#include "php_compat.h"
9625  
9626  /* WBMP struct
9627  ** -----------
9628  ** A Wireless bitmap structure
9629  **
9630  */
9631
9632 +
9633  typedef struct Wbmp_
9634  {
9635      int type;           /* type of the wbmp */
9636 @@ -26,22 +31,22 @@
9637      int height;         /* height of the image */
9638      int *bitmap;        /* pointer to data: 0 = WHITE , 1 = BLACK */
9639  } Wbmp;
9640
9641 +
9642  #define WBMP_WHITE  1
9643  #define WBMP_BLACK  0
9644
9645 +
9646  
9647  /* Proto's
9648  ** -------
9649  **
9650  */
9651 -void           putmbi( int i, void (*putout)(int c, void *out), void *out); 
9652 +void           putmbi( int i, void (*putout)(int c, void *out), void *out);
9653  int    getmbi ( int (*getin)(void *in), void *in );
9654  int     skipheader( int (*getin)(void *in), void *in );
9655  Wbmp   *createwbmp( int width, int height, int color );
9656  int     readwbmp( int (*getin)(void *in), void *in, Wbmp **wbmp );
9657  int            writewbmp( Wbmp *wbmp, void (*putout)( int c, void *out), void *out);
9658  void    freewbmp( Wbmp *wbmp );
9659 -void    printwbmp( Wbmp *wbmp );  
9660 +void    printwbmp( Wbmp *wbmp );
9661  
9662  #endif
9663 diff -urN php-4.4.8.org/ext/gd/libgd/webpng.c php-4.4.8/ext/gd/libgd/webpng.c
9664 --- php-4.4.8.org/ext/gd/libgd/webpng.c 2002-04-21 15:48:22.000000000 +0200
9665 +++ php-4.4.8/ext/gd/libgd/webpng.c     2007-02-24 03:17:24.000000000 +0100
9666 @@ -193,7 +193,7 @@
9667                         maxy = gdImageSY(im);
9668  
9669                         printf("alpha channel information:\n");
9670 -               
9671 +
9672                         if (im->trueColor)      {
9673                                 for (y = 0; y < maxy; y++)      {
9674                                         for (x = 0; x < maxx; x++)      {
9675 @@ -215,9 +215,9 @@
9676                         }
9677                         else
9678                                 printf("NOT a true color image\n");
9679 -                       no = 0; 
9680 +                       no = 0;
9681                         printf("%d alpha channels\n", nalpha);
9682 -                       
9683 +
9684                 }
9685        else
9686         {
9687 @@ -252,7 +252,7 @@
9688           /* Open a temporary file. */
9689  
9690           /* "temp.tmp" is not good temporary filename. */
9691 -         sprintf (outFn, "webpng.tmp%d", getpid ());
9692 +         snprintf (outFn, sizeof(outFn), "webpng.tmp%d", getpid ());
9693           out = fopen (outFn, "wb");
9694  
9695           if (!out)
9696 diff -urN php-4.4.8.org/ext/gd/libgd/xbm.c php-4.4.8/ext/gd/libgd/xbm.c
9697 --- php-4.4.8.org/ext/gd/libgd/xbm.c    2007-12-31 08:22:47.000000000 +0100
9698 +++ php-4.4.8/ext/gd/libgd/xbm.c        2007-08-09 14:08:29.000000000 +0200
9699 @@ -1,8 +1,8 @@
9700  /*
9701     +----------------------------------------------------------------------+
9702 -   | PHP Version 4                                                        |
9703 +   | PHP Version 5                                                        |
9704     +----------------------------------------------------------------------+
9705 -   | Copyright (c) 1997-2008 The PHP Group                                |
9706 +   | Copyright (c) 1997-2007 The PHP Group                                |
9707     +----------------------------------------------------------------------+
9708     | This source file is subject to version 3.01 of the PHP license,      |
9709     | that is bundled with this package in the file LICENSE, and is        |
9710 @@ -16,7 +16,7 @@
9711     +----------------------------------------------------------------------+
9712   */
9713  
9714 -/* $Id$ */
9715 +/* $Id$ */
9716  
9717  #include <stdio.h>
9718  #include <math.h>
9719 @@ -29,8 +29,8 @@
9720  
9721  #define MAX_XBM_LINE_SIZE 255
9722  
9723 -gdImagePtr
9724 -gdImageCreateFromXbm (FILE * fd)
9725 +/* {{{ gdImagePtr gdImageCreateFromXbm */
9726 +gdImagePtr gdImageCreateFromXbm(FILE * fd)
9727  {
9728         char fline[MAX_XBM_LINE_SIZE];
9729         char iname[MAX_XBM_LINE_SIZE];
9730 @@ -46,7 +46,7 @@
9731         int ch;
9732         char h[8];
9733         unsigned int b;
9734 -       
9735 +
9736         rewind(fd);
9737         while (fgets(fline, MAX_XBM_LINE_SIZE, fd)) {
9738                 fline[MAX_XBM_LINE_SIZE-1] = '\0';
9739 @@ -59,7 +59,7 @@
9740                         } else {
9741                                 type++;
9742                         }
9743 -       
9744 +
9745                         if (!strcmp("width", type)) {
9746                                 width = (unsigned int) value;
9747                         }
9748 @@ -96,7 +96,9 @@
9749                 return 0;
9750         }
9751  
9752 -       im = gdImageCreate(width, height);
9753 +       if(!(im = gdImageCreate(width, height))) {
9754 +               return 0;
9755 +       }
9756         gdImageColorAllocate(im, 255, 255, 255);
9757         gdImageColorAllocate(im, 0, 0, 0);
9758         h[2] = '\0';
9759 @@ -147,7 +149,93 @@
9760                 }
9761         }
9762  
9763 -       php_gd_error("EOF before image was complete\n");
9764 +       php_gd_error("EOF before image was complete");
9765         gdImageDestroy(im);
9766         return 0;
9767  }
9768 +/* }}} */
9769 +
9770 +/* {{{ gdCtxPrintf */
9771 +void gdCtxPrintf(gdIOCtx * out, const char *format, ...)
9772 +{
9773 +       char *buf;
9774 +       int len;
9775 +       va_list args;
9776 +
9777 +       va_start(args, format);
9778 +       len = vspprintf(&buf, 0, format, args);
9779 +       va_end(args);
9780 +       out->putBuf(out, buf, len);
9781 +       efree(buf);
9782 +}
9783 +/* }}} */
9784 +
9785 +/* {{{ gdImageXbmCtx */
9786 +void gdImageXbmCtx(gdImagePtr image, char* file_name, int fg, gdIOCtx * out)
9787 +{
9788 +       int x, y, c, b, sx, sy, p;
9789 +       char *name, *f;
9790 +       size_t i, l;
9791 +
9792 +       name = file_name;
9793 +       if ((f = strrchr(name, '/')) != NULL) name = f+1;
9794 +       if ((f = strrchr(name, '\\')) != NULL) name = f+1;
9795 +       name = estrdup(name);
9796 +       if ((f = strrchr(name, '.')) != NULL && !strcasecmp(f, ".XBM")) *f = '\0';
9797 +       if ((l = strlen(name)) == 0) {
9798 +               efree(name);
9799 +               name = estrdup("image");
9800 +       } else {
9801 +               for (i=0; i<l; i++) {
9802 +                       /* only in C-locale isalnum() would work */
9803 +                       if (!isupper(name[i]) && !islower(name[i]) && !isdigit(name[i])) {
9804 +                               name[i] = '_';
9805 +                       }
9806 +               }
9807 +       }
9808 +
9809 +       gdCtxPrintf(out, "#define %s_width %d\n", name, gdImageSX(image));
9810 +       gdCtxPrintf(out, "#define %s_height %d\n", name, gdImageSY(image));
9811 +       gdCtxPrintf(out, "static unsigned char %s_bits[] = {\n  ", name);
9812 +
9813 +       efree(name);
9814 +
9815 +       b = 1;
9816 +       p = 0;
9817 +       c = 0;
9818 +       sx = gdImageSX(image);
9819 +       sy = gdImageSY(image);
9820 +       for (y = 0; y < sy; y++) {
9821 +               for (x = 0; x < sx; x++) {
9822 +                       if (gdImageGetPixel(image, x, y) == fg) {
9823 +                               c |= b;
9824 +                       }
9825 +                       if ((b == 128) || (x == sx && y == sy)) {
9826 +                               b = 1;
9827 +                               if (p) {
9828 +                                       gdCtxPrintf(out, ", ");
9829 +                                       if (!(p%12)) {
9830 +                                               gdCtxPrintf(out, "\n  ");
9831 +                                               p = 12;
9832 +                                       }
9833 +                               }
9834 +                               p++;
9835 +                               gdCtxPrintf(out, "0x%02X", c);
9836 +                               c = 0;
9837 +                       } else {
9838 +                               b <<= 1;
9839 +                       }
9840 +               }
9841 +       }
9842 +       gdCtxPrintf(out, "};\n");
9843 +}
9844 +/* }}} */
9845 +
9846 +/*
9847 + * Local variables:
9848 + * tab-width: 4
9849 + * c-basic-offset: 4
9850 + * End:
9851 + * vim600: sw=4 ts=4 fdm=marker
9852 + * vim<600: sw=4 ts=4
9853 + */
9854 diff -urN php-4.4.8.org/ext/gd/php_gd.h php-4.4.8/ext/gd/php_gd.h
9855 --- php-4.4.8.org/ext/gd/php_gd.h       2007-12-31 08:22:47.000000000 +0100
9856 +++ php-4.4.8/ext/gd/php_gd.h   2008-01-22 22:38:05.897151947 +0100
9857 @@ -1,8 +1,8 @@
9858  /*
9859     +----------------------------------------------------------------------+
9860 -   | PHP Version 4                                                        |
9861 +   | PHP Version 5                                                        |
9862     +----------------------------------------------------------------------+
9863 -   | Copyright (c) 1997-2008 The PHP Group                                |
9864 +   | Copyright (c) 1997-2007 The PHP Group                                |
9865     +----------------------------------------------------------------------+
9866     | This source file is subject to version 3.01 of the PHP license,      |
9867     | that is bundled with this package in the file LICENSE, and is        |
9868 @@ -13,11 +13,11 @@
9869     | license@php.net so we can mail you a copy immediately.               |
9870     +----------------------------------------------------------------------+
9871     | Authors: Rasmus Lerdorf <rasmus@php.net>                             |
9872 -   |          Stig Bakken <ssb@fast.no>                                   |
9873 +   |          Stig Bakken <ssb@php.net>                                   |
9874     +----------------------------------------------------------------------+
9875  */
9876  
9877 -/* $Id$ */
9878 +/* $Id$ */
9879  
9880  #ifndef PHP_GD_H
9881  #define PHP_GD_H
9882 @@ -32,7 +32,7 @@
9883  
9884  /* open_basedir and safe_mode checks */
9885  #define PHP_GD_CHECK_OPEN_BASEDIR(filename, errormsg)                                   \
9886 -       if (!filename || filename == empty_string || php_check_open_basedir(filename TSRMLS_CC) || \
9887 +       if (!filename || php_check_open_basedir(filename TSRMLS_CC) ||                      \
9888                 (PG(safe_mode) && !php_checkuid(filename, NULL, CHECKUID_CHECK_FILE_AND_DIR))   \
9889         ) {                                                                                 \
9890                 php_error_docref(NULL TSRMLS_CC, E_WARNING, errormsg);                          \
9891 @@ -66,7 +66,9 @@
9892  /* gd.c functions */
9893  PHP_MINFO_FUNCTION(gd);
9894  PHP_MINIT_FUNCTION(gd);
9895 +#if HAVE_LIBT1 || HAVE_GD_FONTMUTEX
9896  PHP_MSHUTDOWN_FUNCTION(gd);
9897 +#endif
9898  #if HAVE_LIBGD20 && HAVE_GD_STRINGFT
9899  PHP_RSHUTDOWN_FUNCTION(gd);
9900  #endif
9901 @@ -112,6 +114,11 @@
9902  PHP_FUNCTION(imagecopyresampled);
9903  #endif
9904  
9905 +#ifdef PHP_WIN32
9906 +PHP_FUNCTION(imagegrabwindow);
9907 +PHP_FUNCTION(imagegrabscreen);
9908 +#endif
9909 +
9910  #ifdef HAVE_GD_BUNDLED
9911  PHP_FUNCTION(imagerotate);
9912  PHP_FUNCTION(imageantialias);
9913 @@ -184,6 +191,8 @@
9914  PHP_FUNCTION(imagelayereffect);
9915  PHP_FUNCTION(imagecolormatch);
9916  PHP_FUNCTION(imagefilter);
9917 +PHP_FUNCTION(imageconvolution);
9918 +PHP_FUNCTION(imagexbm);
9919  #endif
9920  
9921  PHP_GD_API int phpi_get_le_gd(void);
This page took 7.595966 seconds and 4 git commands to generate.