From 423c9393917a9d1233fe163a45a0791da306b109 Mon Sep 17 00:00:00 2001 From: Pierre Joye Date: Thu, 4 Apr 2013 10:48:25 +0200 Subject: [PATCH] add new interpolation method, affine methods, scale and rotation --- src/gd.h | 1545 +++++++++++++----------- src/gd_interpolation.c | 2567 ++++++++++++++++++++++++++++++++++++++++ src/gd_matrix.c | 334 ++++++ src/gd_rotate.c | 565 +++++++++ windows/Makefile.w32 | 29 +- 5 files changed, 4344 insertions(+), 696 deletions(-) create mode 100644 src/gd_interpolation.c create mode 100644 src/gd_matrix.c create mode 100644 src/gd_rotate.c diff --git a/src/gd.h b/src/gd.h index ea39d33bd..5502b17f4 100644 --- a/src/gd.h +++ b/src/gd.h @@ -61,63 +61,66 @@ extern "C" { { #endif - /* some might want to set DEFAULT_FONTPATH from configure in config.h */ +#ifdef WIN32 +#define inline __inline +#endif +/* some might want to set DEFAULT_FONTPATH from configure in config.h */ #ifdef NETWARE - /* default fontpath for netware systems, could probably be handled in configure for 2.1 */ +/* default fontpath for netware systems, could probably be handled in configure for 2.1 */ #define DEFAULT_FONTPATH "sys:/java/nwgfx/lib/x11/fonts/ttf;." #define PATHSEPARATOR ";" #endif - /* 2.0.23: more Type 1 fonts */ +/* 2.0.23: more Type 1 fonts */ #ifndef DEFAULT_FONTPATH - /* default fontpath for unix systems - whatever happened to standards ! */ +/* default fontpath for unix systems - whatever happened to standards ! */ #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:/usr/lib/X11/fonts/Type1:/usr/openwin/lib/X11/fonts/Type1" #endif #ifndef PATHSEPARATOR #define PATHSEPARATOR ":" #endif - /* gd.h: declarations file for the graphic-draw module. - * Permission to use, copy, modify, and distribute this software and its - * documentation for any purpose and without fee is hereby granted, provided - * that the above copyright notice appear in all copies and that both that - * copyright notice and this permission notice appear in supporting - * documentation. This software is provided "AS IS." Thomas Boutell and - * Boutell.Com, Inc. disclaim all warranties, either express or implied, - * including but not limited to implied warranties of merchantability and - * fitness for a particular purpose, with respect to this code and accompanying - * documentation. */ - - /* stdio is needed for file I/O. */ +/* gd.h: declarations file for the graphic-draw module. + * Permission to use, copy, modify, and distribute this software and its + * documentation for any purpose and without fee is hereby granted, provided + * that the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation. This software is provided "AS IS." Thomas Boutell and + * Boutell.Com, Inc. disclaim all warranties, either express or implied, + * including but not limited to implied warranties of merchantability and + * fitness for a particular purpose, with respect to this code and accompanying + * documentation. */ + +/* stdio is needed for file I/O. */ #include #include "gd_io.h" - /* The maximum number of palette entries in palette-based images. - In the wonderful new world of gd 2.0, you can of course have - many more colors when using truecolor mode. */ +/* The maximum number of palette entries in palette-based images. + In the wonderful new world of gd 2.0, you can of course have + many more colors when using truecolor mode. */ #define gdMaxColors 256 - /* Image type. See functions below; you will not need to change - the elements directly. Use the provided macros to - access sx, sy, the color table, and colorsTotal for - read-only purposes. */ +/* Image type. See functions below; you will not need to change + the elements directly. Use the provided macros to + access sx, sy, the color table, and colorsTotal for + read-only purposes. */ - /* If 'truecolor' is set true, the image is truecolor; - pixels are represented by integers, which - must be 32 bits wide or more. +/* If 'truecolor' is set true, the image is truecolor; + pixels are represented by integers, which + must be 32 bits wide or more. - True colors are repsented as follows: + True colors are repsented as follows: - ARGB + ARGB - Where 'A' (alpha channel) occupies only the - LOWER 7 BITS of the MSB. This very small - loss of alpha channel resolution allows gd 2.x - to keep backwards compatibility by allowing - signed integers to be used to represent colors, - and negative numbers to represent special cases, - just as in gd 1.x. */ + Where 'A' (alpha channel) occupies only the + LOWER 7 BITS of the MSB. This very small + loss of alpha channel resolution allows gd 2.x + to keep backwards compatibility by allowing + signed integers to be used to represent colors, + and negative numbers to represent special cases, + just as in gd 1.x. */ #define gdAlphaMax 127 #define gdAlphaOpaque 0 @@ -130,147 +133,234 @@ extern "C" { #define gdTrueColorGetGreen(c) (((c) & 0x00FF00) >> 8) #define gdTrueColorGetBlue(c) ((c) & 0x0000FF) - enum gdCropMode { - GD_CROP_DEFAULT = 0, - GD_CROP_TRANSPARENT, - GD_CROP_BLACK, - GD_CROP_WHITE, - GD_CROP_SIDES - }; - - enum gdPaletteQuantizationMethod { - GD_QUANT_DEFAULT = 0, - GD_QUANT_JQUANT = 1, /* libjpeg's old median cut. Fast, but only uses 16-bit color. */ - GD_QUANT_NEUQUANT = 2, /* neuquant - approximation using kohonen neural network. */ - GD_QUANT_LIQ = 3 /* combination of algorithms used in libimagequant/pngquant2 aiming for highest quality at cost of speed */ - }; - - /* This function accepts truecolor pixel values only. The - source color is composited with the destination color - based on the alpha channel value of the source color. - The resulting color is opaque. */ - - BGD_DECLARE(int) gdAlphaBlend (int dest, int src); - - typedef struct gdImageStruct { - /* Palette-based image pixels */ - unsigned char **pixels; - int sx; - int sy; - /* These are valid in palette images only. See also - 'alpha', which appears later in the structure to - preserve binary backwards compatibility */ - int colorsTotal; - int red[gdMaxColors]; - int green[gdMaxColors]; - int blue[gdMaxColors]; - int open[gdMaxColors]; - /* For backwards compatibility, this is set to the - first palette entry with 100% transparency, - and is also set and reset by the - gdImageColorTransparent function. Newer - applications can allocate palette entries - with any desired level of transparency; however, - bear in mind that many viewers, notably - many web browsers, fail to implement - full alpha channel for PNG and provide - support for full opacity or transparency only. */ - int transparent; - int *polyInts; - int polyAllocated; - struct gdImageStruct *brush; - struct gdImageStruct *tile; - int brushColorMap[gdMaxColors]; - int tileColorMap[gdMaxColors]; - int styleLength; - int stylePos; - int *style; - int interlace; - /* New in 2.0: thickness of line. Initialized to 1. */ - int thick; - /* New in 2.0: alpha channel for palettes. Note that only - Macintosh Internet Explorer and (possibly) Netscape 6 - really support multiple levels of transparency in - palettes, to my knowledge, as of 2/15/01. Most - common browsers will display 100% opaque and - 100% transparent correctly, and do something - unpredictable and/or undesirable for levels - in between. TBB */ - int alpha[gdMaxColors]; - /* Truecolor flag and pixels. New 2.0 fields appear here at the - end to minimize breakage of existing object code. */ - int trueColor; - int **tpixels; - /* Should alpha channel be copied, or applied, each time a - pixel is drawn? This applies to truecolor images only. - No attempt is made to alpha-blend in palette images, - even if semitransparent palette entries exist. - To do that, build your image as a truecolor image, - then quantize down to 8 bits. */ - int alphaBlendingFlag; - /* Should the alpha channel of the image be saved? This affects - PNG at the moment; other future formats may also - have that capability. JPEG doesn't. */ - int saveAlphaFlag; - - /* There should NEVER BE ACCESSOR MACROS FOR ITEMS BELOW HERE, so this - part of the structure can be safely changed in new releases. */ - - /* 2.0.12: anti-aliased globals. 2.0.26: just a few vestiges after - switching to the fast, memory-cheap implementation from PHP-gd. */ - int AA; - int AA_color; - int AA_dont_blend; - - /* 2.0.12: simple clipping rectangle. These values - must be checked for safety when set; please use - gdImageSetClip */ - int cx1; - int cy1; - int cx2; - int cy2; - - /* 2.1.0: allows to specify resolution in dpi */ - unsigned int res_x; - unsigned int res_y; - - /* Selects quantization method, see gdImageTrueColorToPaletteSetMethod() and gdPaletteQuantizationMethod enum. */ - int paletteQuantizationMethod; - /* speed/quality trade-off. 1 = best quality, 10 = best speed. 0 = method-specific default. - Applicable to GD_QUANT_LIQ and GD_QUANT_NEUQUANT. */ - int paletteQuantizationSpeed; - /* Image will remain true-color if conversion to palette cannot achieve given quality. - Value from 1 to 100, 1 = ugly, 100 = perfect. Applicable to GD_QUANT_LIQ.*/ - int paletteQuantizationMinQuality; - /* Image will use minimum number of palette colors needed to achieve given quality. Must be higher than paletteQuantizationMinQuality - Value from 1 to 100, 1 = ugly, 100 = perfect. Applicable to GD_QUANT_LIQ.*/ - int paletteQuantizationMaxQuality; - } - gdImage; - - typedef gdImage *gdImagePtr; - - typedef struct { - /* # of characters in font */ - int nchars; - /* First character is numbered... (usually 32 = space) */ - int offset; - /* Character width and height */ - int w; - int h; - /* Font data; array of characters, one row after another. - Easily included in code, also easily loaded from - data files. */ - char *data; - } - gdFont; - - /* Text functions take these. */ - typedef gdFont *gdFontPtr; - - /* For backwards compatibility only. Use gdImageSetStyle() - for MUCH more flexible line drawing. Also see - gdImageSetBrush(). */ +#define GD_TRUE 1 +#define GD_FALSE 0 + +#define GD_EPSILON 1e-6 +#ifndef M_PI +# define M_PI 3.14159265358979323846 +#endif + +/* This function accepts truecolor pixel values only. The + source color is composited with the destination color + based on the alpha channel value of the source color. + The resulting color is opaque. */ + +BGD_DECLARE(int) gdAlphaBlend (int dest, int src); + +enum gdPaletteQuantizationMethod { + GD_QUANT_DEFAULT = 0, + GD_QUANT_JQUANT = 1, /* libjpeg's old median cut. Fast, but only uses 16-bit color. */ + GD_QUANT_NEUQUANT = 2, /* neuquant - approximation using kohonen neural network. */ + GD_QUANT_LIQ = 3 /* combination of algorithms used in libimagequant/pngquant2 aiming for highest quality at cost of speed */ +}; + +/** + * Group: Transform + * + * Constants: gdInterpolationMethod + + * GD_BELL - Bell + * GD_BESSEL - Bessel + * GD_BILINEAR_FIXED - fixed point bilinear + * GD_BICUBIC - Bicubic + * GD_BICUBIC_FIXED - fixed point bicubic integer + * GD_BLACKMAN - Blackman + * GD_BOX - Box + * GD_BSPLINE - BSpline + * GD_CATMULLROM - Catmullrom + * GD_GAUSSIAN - Gaussian + * GD_GENERALIZED_CUBIC - Generalized cubic + * GD_HERMITE - Hermite + * GD_HAMMING - Hamming + * GD_HANNING - Hannig + * GD_MITCHELL - Mitchell + * GD_NEAREST_NEIGHBOUR - Nearest neighbour interpolation + * GD_POWER - Power + * GD_QUADRATIC - Quadratic + * GD_SINC - Sinc + * GD_TRIANGLE - Triangle + * GD_WEIGHTED4 - 4 pixels weighted bilinear interpolation + * + * See also: + * + **/ +typedef enum { + GD_DEFAULT = 0, + GD_BELL, + GD_BESSEL, + GD_BILINEAR_FIXED, + GD_BICUBIC, + GD_BICUBIC_FIXED, + GD_BLACKMAN, + GD_BOX, + GD_BSPLINE, + GD_CATMULLROM, + GD_GAUSSIAN, + GD_GENERALIZED_CUBIC, + GD_HERMITE, + GD_HAMMING, + GD_HANNING, + GD_MITCHELL, + GD_NEAREST_NEIGHBOUR, + GD_POWER, + GD_QUADRATIC, + GD_SINC, + GD_TRIANGLE, + GD_WEIGHTED4, + GD_METHOD_COUNT = 21 +} gdInterpolationMethod; + +/* define struct with name and func ptr and add it to gdImageStruct gdInterpolationMethod interpolation; */ + +/* Interpolation function ptr */ +typedef double (* interpolation_method )(double); + +typedef struct gdImageStruct { + /* Palette-based image pixels */ + unsigned char **pixels; + int sx; + int sy; + /* These are valid in palette images only. See also + 'alpha', which appears later in the structure to + preserve binary backwards compatibility */ + int colorsTotal; + int red[gdMaxColors]; + int green[gdMaxColors]; + int blue[gdMaxColors]; + int open[gdMaxColors]; + /* For backwards compatibility, this is set to the + first palette entry with 100% transparency, + and is also set and reset by the + gdImageColorTransparent function. Newer + applications can allocate palette entries + with any desired level of transparency; however, + bear in mind that many viewers, notably + many web browsers, fail to implement + full alpha channel for PNG and provide + support for full opacity or transparency only. */ + int transparent; + int *polyInts; + int polyAllocated; + struct gdImageStruct *brush; + struct gdImageStruct *tile; + int brushColorMap[gdMaxColors]; + int tileColorMap[gdMaxColors]; + int styleLength; + int stylePos; + int *style; + int interlace; + /* New in 2.0: thickness of line. Initialized to 1. */ + int thick; + /* New in 2.0: alpha channel for palettes. Note that only + Macintosh Internet Explorer and (possibly) Netscape 6 + really support multiple levels of transparency in + palettes, to my knowledge, as of 2/15/01. Most + common browsers will display 100% opaque and + 100% transparent correctly, and do something + unpredictable and/or undesirable for levels + in between. TBB */ + int alpha[gdMaxColors]; + /* Truecolor flag and pixels. New 2.0 fields appear here at the + end to minimize breakage of existing object code. */ + int trueColor; + int **tpixels; + /* Should alpha channel be copied, or applied, each time a + pixel is drawn? This applies to truecolor images only. + No attempt is made to alpha-blend in palette images, + even if semitransparent palette entries exist. + To do that, build your image as a truecolor image, + then quantize down to 8 bits. */ + int alphaBlendingFlag; + /* Should the alpha channel of the image be saved? This affects + PNG at the moment; other future formats may also + have that capability. JPEG doesn't. */ + int saveAlphaFlag; + + /* There should NEVER BE ACCESSOR MACROS FOR ITEMS BELOW HERE, so this + part of the structure can be safely changed in new releases. */ + + /* 2.0.12: anti-aliased globals. 2.0.26: just a few vestiges after + switching to the fast, memory-cheap implementation from PHP-gd. */ + int AA; + int AA_color; + int AA_dont_blend; + + /* 2.0.12: simple clipping rectangle. These values + must be checked for safety when set; please use + gdImageSetClip */ + int cx1; + int cy1; + int cx2; + int cy2; + + /* 2.1.0: allows to specify resolution in dpi */ + unsigned int res_x; + unsigned int res_y; + + /* Selects quantization method, see gdImageTrueColorToPaletteSetMethod() and gdPaletteQuantizationMethod enum. */ + int paletteQuantizationMethod; + /* speed/quality trade-off. 1 = best quality, 10 = best speed. 0 = method-specific default. + Applicable to GD_QUANT_LIQ and GD_QUANT_NEUQUANT. */ + int paletteQuantizationSpeed; + /* Image will remain true-color if conversion to palette cannot achieve given quality. + Value from 1 to 100, 1 = ugly, 100 = perfect. Applicable to GD_QUANT_LIQ.*/ + int paletteQuantizationMinQuality; + /* Image will use minimum number of palette colors needed to achieve given quality. Must be higher than paletteQuantizationMinQuality + Value from 1 to 100, 1 = ugly, 100 = perfect. Applicable to GD_QUANT_LIQ.*/ + int paletteQuantizationMaxQuality; + gdInterpolationMethod interpolation_id; + interpolation_method interpolation; +} +gdImage; + +typedef gdImage *gdImagePtr; + + +/* Point type for use in polygon drawing. */ + +/** + * Group: Types + * + * typedef: gdPointF + * Defines a point in a 2D coordinate system using floating point + * values. + * x - Floating point position (increase from left to right) + * y - Floating point Row position (increase from top to bottom) + * + * typedef: gdPointFPtr + * Pointer to a + * + * See also: + * , , + **/ +typedef struct +{ + double x, y; +} +gdPointF, *gdPointFPtr; + +typedef struct { + /* # of characters in font */ + int nchars; + /* First character is numbered... (usually 32 = space) */ + int offset; + /* Character width and height */ + int w; + int h; + /* Font data; array of characters, one row after another. + Easily included in code, also easily loaded from + data files. */ + char *data; +} +gdFont; + +/* Text functions take these. */ +typedef gdFont *gdFontPtr; + +/* For backwards compatibility only. Use gdImageSetStyle() + for MUCH more flexible line drawing. Also see + gdImageSetBrush(). */ #define gdDashSize 4 /* Special colors. */ @@ -297,178 +387,178 @@ extern "C" { /* Creates a truecolor image (millions of colors). */ BGD_DECLARE(gdImagePtr) gdImageCreateTrueColor (int sx, int sy); - /* Creates an image from various file types. These functions - return a palette or truecolor image based on the - nature of the file being loaded. Truecolor PNG - stays truecolor; palette PNG stays palette-based; - JPEG is always truecolor. */ - BGD_DECLARE(gdImagePtr) gdImageCreateFromPng (FILE * fd); - BGD_DECLARE(gdImagePtr) gdImageCreateFromPngCtx (gdIOCtxPtr in); - BGD_DECLARE(gdImagePtr) gdImageCreateFromPngPtr (int size, void *data); - - /* These read the first frame only */ - BGD_DECLARE(gdImagePtr) gdImageCreateFromGif (FILE * fd); - BGD_DECLARE(gdImagePtr) gdImageCreateFromGifCtx (gdIOCtxPtr in); - BGD_DECLARE(gdImagePtr) gdImageCreateFromGifPtr (int size, void *data); - BGD_DECLARE(gdImagePtr) gdImageCreateFromWBMP (FILE * inFile); - BGD_DECLARE(gdImagePtr) gdImageCreateFromWBMPCtx (gdIOCtx * infile); - BGD_DECLARE(gdImagePtr) gdImageCreateFromWBMPPtr (int size, void *data); - BGD_DECLARE(gdImagePtr) gdImageCreateFromJpeg (FILE * infile); - BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegCtx (gdIOCtx * infile); - BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegPtr (int size, void *data); - - BGD_DECLARE(gdImagePtr) gdImageCreateFromTiff(FILE *inFile); - BGD_DECLARE(gdImagePtr) gdImageCreateFromTiffCtx(gdIOCtx *infile); - BGD_DECLARE(gdImagePtr) gdImageCreateFromTiffPtr(int size, void *data); - - BGD_DECLARE(gdImagePtr) gdImageCreateFromTga( FILE * fp ); - BGD_DECLARE(gdImagePtr) gdImageCreateFromTgaCtx(gdIOCtx* ctx); - BGD_DECLARE(gdImagePtr) gdImageCreateFromTgaPtr(int size, void *data); - - BGD_DECLARE(gdImagePtr) gdImageCreateFromBmp (FILE * inFile); - BGD_DECLARE(gdImagePtr) gdImageCreateFromBmpPtr (int size, void *data); - BGD_DECLARE(gdImagePtr) gdImageCreateFromBmpCtx (gdIOCtxPtr infile); - - /* A custom data source. */ - /* The source function must return -1 on error, otherwise the number - of bytes fetched. 0 is EOF, not an error! */ - /* context will be passed to your source function. */ - - typedef struct { - int (*source) (void *context, char *buffer, int len); - void *context; - } - gdSource, *gdSourcePtr; - - /* Deprecated in favor of gdImageCreateFromPngCtx */ - BGD_DECLARE(gdImagePtr) gdImageCreateFromPngSource (gdSourcePtr in); - - BGD_DECLARE(gdImagePtr) gdImageCreateFromGd (FILE * in); - BGD_DECLARE(gdImagePtr) gdImageCreateFromGdCtx (gdIOCtxPtr in); - BGD_DECLARE(gdImagePtr) gdImageCreateFromGdPtr (int size, void *data); - - BGD_DECLARE(gdImagePtr) gdImageCreateFromGd2 (FILE * in); - BGD_DECLARE(gdImagePtr) gdImageCreateFromGd2Ctx (gdIOCtxPtr in); - BGD_DECLARE(gdImagePtr) gdImageCreateFromGd2Ptr (int size, void *data); - - BGD_DECLARE(gdImagePtr) gdImageCreateFromGd2Part (FILE * in, int srcx, int srcy, int w, - int h); - BGD_DECLARE(gdImagePtr) gdImageCreateFromGd2PartCtx (gdIOCtxPtr in, int srcx, int srcy, - int w, int h); - BGD_DECLARE(gdImagePtr) gdImageCreateFromGd2PartPtr (int size, void *data, int srcx, int srcy, - int w, int h); - /* 2.0.10: prototype was missing */ - BGD_DECLARE(gdImagePtr) gdImageCreateFromXbm (FILE * in); - - /* NOTE: filename, not FILE */ - BGD_DECLARE(gdImagePtr) gdImageCreateFromXpm (char *filename); - - BGD_DECLARE(void) gdImageDestroy (gdImagePtr im); - - /* Replaces or blends with the background depending on the - most recent call to gdImageAlphaBlending and the - alpha channel value of 'color'; default is to overwrite. - Tiling and line styling are also implemented - here. All other gd drawing functions pass through this call, - allowing for many useful effects. */ - - BGD_DECLARE(void) gdImageSetPixel (gdImagePtr im, int x, int y, int color); - /* FreeType 2 text output with hook to extra flags */ - - BGD_DECLARE(int) gdImageGetPixel (gdImagePtr im, int x, int y); - BGD_DECLARE(int) gdImageGetTrueColorPixel (gdImagePtr im, int x, int y); - - BGD_DECLARE(void) gdImageAABlend (gdImagePtr im); - - BGD_DECLARE(void) gdImageLine (gdImagePtr im, int x1, int y1, int x2, int y2, int color); - - /* For backwards compatibility only. Use gdImageSetStyle() - for much more flexible line drawing. */ - BGD_DECLARE(void) gdImageDashedLine (gdImagePtr im, int x1, int y1, int x2, int y2, - int color); - /* Corners specified (not width and height). Upper left first, lower right - second. */ - BGD_DECLARE(void) gdImageRectangle (gdImagePtr im, int x1, int y1, int x2, int y2, - int color); - /* Solid bar. Upper left corner first, lower right corner second. */ - BGD_DECLARE(void) gdImageFilledRectangle (gdImagePtr im, int x1, int y1, int x2, int y2, - int color); - BGD_DECLARE(void) gdImageSetClip(gdImagePtr im, int x1, int y1, int x2, int y2); - BGD_DECLARE(void) gdImageGetClip(gdImagePtr im, int *x1P, int *y1P, int *x2P, int *y2P); - BGD_DECLARE(void) gdImageSetResolution(gdImagePtr im, const unsigned int res_x, const unsigned int res_y); - BGD_DECLARE(int) gdImageBoundsSafe (gdImagePtr im, int x, int y); - BGD_DECLARE(void) gdImageChar (gdImagePtr im, gdFontPtr f, int x, int y, int c, - int color); - BGD_DECLARE(void) gdImageCharUp (gdImagePtr im, gdFontPtr f, int x, int y, int c, - int color); - BGD_DECLARE(void) gdImageString (gdImagePtr im, gdFontPtr f, int x, int y, - unsigned char *s, int color); - BGD_DECLARE(void) gdImageStringUp (gdImagePtr im, gdFontPtr f, int x, int y, - unsigned char *s, int color); - BGD_DECLARE(void) gdImageString16 (gdImagePtr im, gdFontPtr f, int x, int y, - unsigned short *s, int color); - BGD_DECLARE(void) gdImageStringUp16 (gdImagePtr im, gdFontPtr f, int x, int y, - unsigned short *s, int color); - - /* 2.0.16: for thread-safe use of gdImageStringFT and friends, - call this before allowing any thread to call gdImageStringFT. - Otherwise it is invoked by the first thread to invoke - gdImageStringFT, with a very small but real risk of a race condition. - Return 0 on success, nonzero on failure to initialize freetype. */ - BGD_DECLARE(int) gdFontCacheSetup (void); - - /* Optional: clean up after application is done using fonts in - BGD_DECLARE( ) - gdImageStringFT(). */ - BGD_DECLARE(void) gdFontCacheShutdown (void); - /* 2.0.20: for backwards compatibility. A few applications did start calling - this function when it first appeared although it was never documented. - Simply invokes gdFontCacheShutdown. */ - BGD_DECLARE(void) gdFreeFontCache (void); - - /* Calls gdImageStringFT. Provided for backwards compatibility only. */ - BGD_DECLARE(char *) gdImageStringTTF (gdImage * im, int *brect, int fg, char *fontlist, - double ptsize, double angle, int x, int y, - char *string); - - /* FreeType 2 text output */ - BGD_DECLARE(char *) gdImageStringFT (gdImage * im, int *brect, int fg, char *fontlist, - double ptsize, double angle, int x, int y, - char *string); - - /* 2.0.5: provides an extensible way to pass additional parameters. - Thanks to Wez Furlong, sorry for the delay. */ - - typedef struct { - int flags; /* Logical OR of gdFTEX_ values */ - double linespacing; /* fine tune line spacing for '\n' */ - int charmap; /* TBB: 2.0.12: may be gdFTEX_Unicode, - gdFTEX_Shift_JIS, gdFTEX_Big5, - or gdFTEX_Adobe_Custom; - when not specified, maps are searched - for in the above order. */ - int hdpi; /* if (flags & gdFTEX_RESOLUTION) */ - int vdpi; /* if (flags & gdFTEX_RESOLUTION) */ - char *xshow; /* if (flags & gdFTEX_XSHOW) - then, on return, xshow is a malloc'ed - string containing xshow position data for - the last string. - - NB. The caller is responsible for gdFree'ing - the xshow string. - */ - char *fontpath; /* if (flags & gdFTEX_RETURNFONTPATHNAME) - then, on return, fontpath is a malloc'ed - string containing the actual font file path name - used, which can be interesting when fontconfig - is in use. - - The caller is responsible for gdFree'ing the - fontpath string. - */ - - } - gdFTStringExtra, *gdFTStringExtraPtr; +/* Creates an image from various file types. These functions + return a palette or truecolor image based on the + nature of the file being loaded. Truecolor PNG + stays truecolor; palette PNG stays palette-based; + JPEG is always truecolor. */ +BGD_DECLARE(gdImagePtr) gdImageCreateFromPng (FILE * fd); +BGD_DECLARE(gdImagePtr) gdImageCreateFromPngCtx (gdIOCtxPtr in); +BGD_DECLARE(gdImagePtr) gdImageCreateFromPngPtr (int size, void *data); + +/* These read the first frame only */ +BGD_DECLARE(gdImagePtr) gdImageCreateFromGif (FILE * fd); +BGD_DECLARE(gdImagePtr) gdImageCreateFromGifCtx (gdIOCtxPtr in); +BGD_DECLARE(gdImagePtr) gdImageCreateFromGifPtr (int size, void *data); +BGD_DECLARE(gdImagePtr) gdImageCreateFromWBMP (FILE * inFile); +BGD_DECLARE(gdImagePtr) gdImageCreateFromWBMPCtx (gdIOCtx * infile); +BGD_DECLARE(gdImagePtr) gdImageCreateFromWBMPPtr (int size, void *data); +BGD_DECLARE(gdImagePtr) gdImageCreateFromJpeg (FILE * infile); +BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegCtx (gdIOCtx * infile); +BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegPtr (int size, void *data); + +BGD_DECLARE(gdImagePtr) gdImageCreateFromTiff(FILE *inFile); +BGD_DECLARE(gdImagePtr) gdImageCreateFromTiffCtx(gdIOCtx *infile); +BGD_DECLARE(gdImagePtr) gdImageCreateFromTiffPtr(int size, void *data); + +BGD_DECLARE(gdImagePtr) gdImageCreateFromTga( FILE * fp ); +BGD_DECLARE(gdImagePtr) gdImageCreateFromTgaCtx(gdIOCtx* ctx); +BGD_DECLARE(gdImagePtr) gdImageCreateFromTgaPtr(int size, void *data); + +BGD_DECLARE(gdImagePtr) gdImageCreateFromBmp (FILE * inFile); +BGD_DECLARE(gdImagePtr) gdImageCreateFromBmpPtr (int size, void *data); +BGD_DECLARE(gdImagePtr) gdImageCreateFromBmpCtx (gdIOCtxPtr infile); + +/* A custom data source. */ +/* The source function must return -1 on error, otherwise the number + of bytes fetched. 0 is EOF, not an error! */ +/* context will be passed to your source function. */ + +typedef struct { + int (*source) (void *context, char *buffer, int len); + void *context; +} +gdSource, *gdSourcePtr; + +/* Deprecated in favor of gdImageCreateFromPngCtx */ +BGD_DECLARE(gdImagePtr) gdImageCreateFromPngSource (gdSourcePtr in); + +BGD_DECLARE(gdImagePtr) gdImageCreateFromGd (FILE * in); +BGD_DECLARE(gdImagePtr) gdImageCreateFromGdCtx (gdIOCtxPtr in); +BGD_DECLARE(gdImagePtr) gdImageCreateFromGdPtr (int size, void *data); + +BGD_DECLARE(gdImagePtr) gdImageCreateFromGd2 (FILE * in); +BGD_DECLARE(gdImagePtr) gdImageCreateFromGd2Ctx (gdIOCtxPtr in); +BGD_DECLARE(gdImagePtr) gdImageCreateFromGd2Ptr (int size, void *data); + +BGD_DECLARE(gdImagePtr) gdImageCreateFromGd2Part (FILE * in, int srcx, int srcy, int w, + int h); +BGD_DECLARE(gdImagePtr) gdImageCreateFromGd2PartCtx (gdIOCtxPtr in, int srcx, int srcy, + int w, int h); +BGD_DECLARE(gdImagePtr) gdImageCreateFromGd2PartPtr (int size, void *data, int srcx, int srcy, + int w, int h); +/* 2.0.10: prototype was missing */ +BGD_DECLARE(gdImagePtr) gdImageCreateFromXbm (FILE * in); + +/* NOTE: filename, not FILE */ +BGD_DECLARE(gdImagePtr) gdImageCreateFromXpm (char *filename); + +BGD_DECLARE(void) gdImageDestroy (gdImagePtr im); + +/* Replaces or blends with the background depending on the + most recent call to gdImageAlphaBlending and the + alpha channel value of 'color'; default is to overwrite. + Tiling and line styling are also implemented + here. All other gd drawing functions pass through this call, + allowing for many useful effects. */ + +BGD_DECLARE(void) gdImageSetPixel (gdImagePtr im, int x, int y, int color); +/* FreeType 2 text output with hook to extra flags */ + +BGD_DECLARE(int) gdImageGetPixel (gdImagePtr im, int x, int y); +BGD_DECLARE(int) gdImageGetTrueColorPixel (gdImagePtr im, int x, int y); + +BGD_DECLARE(void) gdImageAABlend (gdImagePtr im); + +BGD_DECLARE(void) gdImageLine (gdImagePtr im, int x1, int y1, int x2, int y2, int color); + +/* For backwards compatibility only. Use gdImageSetStyle() + for much more flexible line drawing. */ +BGD_DECLARE(void) gdImageDashedLine (gdImagePtr im, int x1, int y1, int x2, int y2, + int color); +/* Corners specified (not width and height). Upper left first, lower right + second. */ +BGD_DECLARE(void) gdImageRectangle (gdImagePtr im, int x1, int y1, int x2, int y2, + int color); +/* Solid bar. Upper left corner first, lower right corner second. */ +BGD_DECLARE(void) gdImageFilledRectangle (gdImagePtr im, int x1, int y1, int x2, int y2, + int color); +BGD_DECLARE(void) gdImageSetClip(gdImagePtr im, int x1, int y1, int x2, int y2); +BGD_DECLARE(void) gdImageGetClip(gdImagePtr im, int *x1P, int *y1P, int *x2P, int *y2P); +BGD_DECLARE(void) gdImageSetResolution(gdImagePtr im, const unsigned int res_x, const unsigned int res_y); +BGD_DECLARE(int) gdImageBoundsSafe (gdImagePtr im, int x, int y); +BGD_DECLARE(void) gdImageChar (gdImagePtr im, gdFontPtr f, int x, int y, int c, + int color); +BGD_DECLARE(void) gdImageCharUp (gdImagePtr im, gdFontPtr f, int x, int y, int c, + int color); +BGD_DECLARE(void) gdImageString (gdImagePtr im, gdFontPtr f, int x, int y, + unsigned char *s, int color); +BGD_DECLARE(void) gdImageStringUp (gdImagePtr im, gdFontPtr f, int x, int y, + unsigned char *s, int color); +BGD_DECLARE(void) gdImageString16 (gdImagePtr im, gdFontPtr f, int x, int y, + unsigned short *s, int color); +BGD_DECLARE(void) gdImageStringUp16 (gdImagePtr im, gdFontPtr f, int x, int y, + unsigned short *s, int color); + +/* 2.0.16: for thread-safe use of gdImageStringFT and friends, + call this before allowing any thread to call gdImageStringFT. + Otherwise it is invoked by the first thread to invoke + gdImageStringFT, with a very small but real risk of a race condition. + Return 0 on success, nonzero on failure to initialize freetype. */ +BGD_DECLARE(int) gdFontCacheSetup (void); + +/* Optional: clean up after application is done using fonts in +BGD_DECLARE( ) + gdImageStringFT(). */ +BGD_DECLARE(void) gdFontCacheShutdown (void); +/* 2.0.20: for backwards compatibility. A few applications did start calling + this function when it first appeared although it was never documented. + Simply invokes gdFontCacheShutdown. */ +BGD_DECLARE(void) gdFreeFontCache (void); + +/* Calls gdImageStringFT. Provided for backwards compatibility only. */ +BGD_DECLARE(char *) gdImageStringTTF (gdImage * im, int *brect, int fg, char *fontlist, + double ptsize, double angle, int x, int y, + char *string); + +/* FreeType 2 text output */ +BGD_DECLARE(char *) gdImageStringFT (gdImage * im, int *brect, int fg, char *fontlist, + double ptsize, double angle, int x, int y, + char *string); + +/* 2.0.5: provides an extensible way to pass additional parameters. + Thanks to Wez Furlong, sorry for the delay. */ + +typedef struct { + int flags; /* Logical OR of gdFTEX_ values */ + double linespacing; /* fine tune line spacing for '\n' */ + int charmap; /* TBB: 2.0.12: may be gdFTEX_Unicode, + gdFTEX_Shift_JIS, gdFTEX_Big5, + or gdFTEX_Adobe_Custom; + when not specified, maps are searched + for in the above order. */ + int hdpi; /* if (flags & gdFTEX_RESOLUTION) */ + int vdpi; /* if (flags & gdFTEX_RESOLUTION) */ + char *xshow; /* if (flags & gdFTEX_XSHOW) + then, on return, xshow is a malloc'ed + string containing xshow position data for + the last string. + + NB. The caller is responsible for gdFree'ing + the xshow string. + */ + char *fontpath; /* if (flags & gdFTEX_RETURNFONTPATHNAME) + then, on return, fontpath is a malloc'ed + string containing the actual font file path name + used, which can be interesting when fontconfig + is in use. + + The caller is responsible for gdFree'ing the + fontpath string. + */ + +} +gdFTStringExtra, *gdFTStringExtraPtr; #define gdFTEX_LINESPACE 1 #define gdFTEX_CHARMAP 2 @@ -502,52 +592,52 @@ extern "C" { #define gdFTEX_Big5 2 #define gdFTEX_Adobe_Custom 3 - BGD_DECLARE(char *) gdImageStringFTEx (gdImage * im, int *brect, int fg, char *fontlist, - double ptsize, double angle, int x, int y, - char *string, gdFTStringExtraPtr strex); - - /* Point type for use in polygon drawing. */ - typedef struct { - int x, y; - } - gdPoint, *gdPointPtr; - - typedef struct { - int x, y; - int width, height; - } - gdRect, *gdRectPtr; - - - BGD_DECLARE(void) gdImagePolygon (gdImagePtr im, gdPointPtr p, int n, int c); - BGD_DECLARE(void) gdImageOpenPolygon (gdImagePtr im, gdPointPtr p, int n, int c); - BGD_DECLARE(void) gdImageFilledPolygon (gdImagePtr im, gdPointPtr p, int n, int c); - - /* These functions still work with truecolor images, - for which they never return error. */ - BGD_DECLARE(int) gdImageColorAllocate (gdImagePtr im, int r, int g, int b); - /* gd 2.0: palette entries with non-opaque transparency are permitted. */ - BGD_DECLARE(int) gdImageColorAllocateAlpha (gdImagePtr im, int r, int g, int b, int a); - /* Assumes opaque is the preferred alpha channel value */ - BGD_DECLARE(int) gdImageColorClosest (gdImagePtr im, int r, int g, int b); - /* Closest match taking all four parameters into account. - A slightly different color with the same transparency - beats the exact same color with radically different - transparency */ - BGD_DECLARE(int) gdImageColorClosestAlpha (gdImagePtr im, int r, int g, int b, int a); - /* An alternate method */ - BGD_DECLARE(int) gdImageColorClosestHWB (gdImagePtr im, int r, int g, int b); - /* Returns exact, 100% opaque matches only */ - BGD_DECLARE(int) gdImageColorExact (gdImagePtr im, int r, int g, int b); - /* Returns an exact match only, including alpha */ - BGD_DECLARE(int) gdImageColorExactAlpha (gdImagePtr im, int r, int g, int b, int a); - /* Opaque only */ - BGD_DECLARE(int) gdImageColorResolve (gdImagePtr im, int r, int g, int b); - /* Based on gdImageColorExactAlpha and gdImageColorClosestAlpha */ - BGD_DECLARE(int) gdImageColorResolveAlpha (gdImagePtr im, int r, int g, int b, int a); - - /* A simpler way to obtain an opaque truecolor value for drawing on a - truecolor image. Not for use with palette images! */ +BGD_DECLARE(char *) gdImageStringFTEx (gdImage * im, int *brect, int fg, char *fontlist, + double ptsize, double angle, int x, int y, + char *string, gdFTStringExtraPtr strex); + +/* Point type for use in polygon drawing. */ +typedef struct { + int x, y; +} +gdPoint, *gdPointPtr; + +typedef struct { + int x, y; + int width, height; +} +gdRect, *gdRectPtr; + + +BGD_DECLARE(void) gdImagePolygon (gdImagePtr im, gdPointPtr p, int n, int c); +BGD_DECLARE(void) gdImageOpenPolygon (gdImagePtr im, gdPointPtr p, int n, int c); +BGD_DECLARE(void) gdImageFilledPolygon (gdImagePtr im, gdPointPtr p, int n, int c); + +/* These functions still work with truecolor images, + for which they never return error. */ +BGD_DECLARE(int) gdImageColorAllocate (gdImagePtr im, int r, int g, int b); +/* gd 2.0: palette entries with non-opaque transparency are permitted. */ +BGD_DECLARE(int) gdImageColorAllocateAlpha (gdImagePtr im, int r, int g, int b, int a); +/* Assumes opaque is the preferred alpha channel value */ +BGD_DECLARE(int) gdImageColorClosest (gdImagePtr im, int r, int g, int b); +/* Closest match taking all four parameters into account. + A slightly different color with the same transparency + beats the exact same color with radically different + transparency */ +BGD_DECLARE(int) gdImageColorClosestAlpha (gdImagePtr im, int r, int g, int b, int a); +/* An alternate method */ +BGD_DECLARE(int) gdImageColorClosestHWB (gdImagePtr im, int r, int g, int b); +/* Returns exact, 100% opaque matches only */ +BGD_DECLARE(int) gdImageColorExact (gdImagePtr im, int r, int g, int b); +/* Returns an exact match only, including alpha */ +BGD_DECLARE(int) gdImageColorExactAlpha (gdImagePtr im, int r, int g, int b, int a); +/* Opaque only */ +BGD_DECLARE(int) gdImageColorResolve (gdImagePtr im, int r, int g, int b); +/* Based on gdImageColorExactAlpha and gdImageColorClosestAlpha */ +BGD_DECLARE(int) gdImageColorResolveAlpha (gdImagePtr im, int r, int g, int b, int a); + +/* A simpler way to obtain an opaque truecolor value for drawing on a + truecolor image. Not for use with palette images! */ #define gdTrueColor(r, g, b) (((r) << 16) + \ ((g) << 8) + \ @@ -562,279 +652,269 @@ extern "C" { ((g) << 8) + \ (b)) - BGD_DECLARE(void) gdImageColorDeallocate (gdImagePtr im, int color); - - /* Converts a truecolor image to a palette-based image, - using a high-quality two-pass quantization routine - which attempts to preserve alpha channel information - as well as R/G/B color information when creating - a palette. If ditherFlag is set, the image will be - dithered to approximate colors better, at the expense - of some obvious "speckling." colorsWanted can be - anything up to 256. If the original source image - includes photographic information or anything that - came out of a JPEG, 256 is strongly recommended. - - Better yet, don't use these function -- write real - truecolor PNGs and JPEGs. The disk space gain of - conversion to palette is not great (for small images - it can be negative) and the quality loss is ugly. - - DIFFERENCES: gdImageCreatePaletteFromTrueColor creates and - returns a new image. gdImageTrueColorToPalette modifies - an existing image, and the truecolor pixels are discarded. - - gdImageTrueColorToPalette() returns TRUE on success, FALSE on failure. - - */ - - BGD_DECLARE(gdImagePtr) gdImageCreatePaletteFromTrueColor (gdImagePtr im, int ditherFlag, - int colorsWanted); - - BGD_DECLARE(int) gdImageTrueColorToPalette (gdImagePtr im, int ditherFlag, - int colorsWanted); - - /* - Selects quantization method used for subsequent gdImageTrueColorToPalette calls. - See gdPaletteQuantizationMethod enum (e.g. GD_QUANT_NEUQUANT, GD_QUANT_LIQ). - Speed is from 1 (highest quality) to 10 (fastest). - Speed 0 selects method-specific default (recommended). - - Returns FALSE if the given method is invalid or not available. - */ - BGD_DECLARE(int) gdImageTrueColorToPaletteSetMethod (gdImagePtr im, int method, int speed); - - /* - Chooses quality range that subsequent call to gdImageTrueColorToPalette will aim for. - Min and max quality is in range 1-100 (1 = ugly, 100 = perfect). Max must be higher than min. - If palette cannot represent image with at least min_quality, then image will remain true-color. - If palette can represent image with quality better than max_quality, then lower number of colors will be used. - This function has effect only when GD_QUANT_LIQ method has been selected and the source image is true-color. - */ - BGD_DECLARE(void) gdImageTrueColorToPaletteSetQuality (gdImagePtr im, int min_quality, int max_quality); - - /* Specifies a color index (if a palette image) or an - RGB color (if a truecolor image) which should be - considered 100% transparent. FOR TRUECOLOR IMAGES, - THIS IS IGNORED IF AN ALPHA CHANNEL IS BEING - SAVED. Use gdImageSaveAlpha(im, 0); to - turn off the saving of a full alpha channel in - a truecolor image. Note that gdImageColorTransparent - is usually compatible with older browsers that - do not understand full alpha channels well. TBB */ - BGD_DECLARE(void) gdImageColorTransparent (gdImagePtr im, int color); - - BGD_DECLARE(void) gdImagePaletteCopy (gdImagePtr dst, gdImagePtr src); - - typedef int (*gdCallbackImageColor)(gdImagePtr im, int src); - - BGD_DECLARE(int) gdImageColorReplace(gdImagePtr im, int src, int dst); - BGD_DECLARE(int) gdImageColorReplaceThreshold(gdImagePtr im, int src, int dst, float threshold); - BGD_DECLARE(int) gdImageColorReplaceArray(gdImagePtr im, int len, int *src, int *dst); - BGD_DECLARE(int) gdImageColorReplaceCallback(gdImagePtr im, gdCallbackImageColor callback); - - BGD_DECLARE(void) gdImageGif (gdImagePtr im, FILE * out); - BGD_DECLARE(void) gdImagePng (gdImagePtr im, FILE * out); - BGD_DECLARE(void) gdImagePngCtx (gdImagePtr im, gdIOCtx * out); - BGD_DECLARE(void) gdImageGifCtx (gdImagePtr im, gdIOCtx * out); - BGD_DECLARE(void) gdImageTiff(gdImagePtr im, FILE *outFile); - BGD_DECLARE(void *) gdImageTiffPtr(gdImagePtr im, int *size); - BGD_DECLARE(void) gdImageTiffCtx(gdImagePtr image, gdIOCtx *out); - - BGD_DECLARE(void *) gdImageBmpPtr(gdImagePtr im, int *size, int compression); - BGD_DECLARE(void) gdImageBmp(gdImagePtr im, FILE *outFile, int compression); - BGD_DECLARE(void) gdImageBmpCtx(gdImagePtr im, gdIOCtxPtr out, int compression); - - /* 2.0.12: Compression level: 0-9 or -1, where 0 is NO COMPRESSION at all, - 1 is FASTEST but produces larger files, 9 provides the best - compression (smallest files) but takes a long time to compress, and - -1 selects the default compiled into the zlib library. */ - BGD_DECLARE(void) gdImagePngEx (gdImagePtr im, FILE * out, int level); - BGD_DECLARE(void) gdImagePngCtxEx (gdImagePtr im, gdIOCtx * out, int level); - - BGD_DECLARE(void) gdImageWBMP (gdImagePtr image, int fg, FILE * out); - BGD_DECLARE(void) gdImageWBMPCtx (gdImagePtr image, int fg, gdIOCtx * out); - - /* Guaranteed to correctly free memory returned - by the gdImage*Ptr functions */ - BGD_DECLARE(void) gdFree (void *m); - - /* Best to free this memory with gdFree(), not free() */ - BGD_DECLARE(void *) gdImageWBMPPtr (gdImagePtr im, int *size, int fg); - - /* 100 is highest quality (there is always a little loss with JPEG). - 0 is lowest. 10 is about the lowest useful setting. */ - BGD_DECLARE(void) gdImageJpeg (gdImagePtr im, FILE * out, int quality); - BGD_DECLARE(void) gdImageJpegCtx (gdImagePtr im, gdIOCtx * out, int quality); - - /* Best to free this memory with gdFree(), not free() */ - BGD_DECLARE(void *) gdImageJpegPtr (gdImagePtr im, int *size, int quality); - - /* Legal values for Disposal. gdDisposalNone is always used by - the built-in optimizer if previm is passed. */ - - enum { - gdDisposalUnknown, - gdDisposalNone, - gdDisposalRestoreBackground, - gdDisposalRestorePrevious - }; - - BGD_DECLARE(void) gdImageGifAnimBegin(gdImagePtr im, FILE *outFile, int GlobalCM, int Loops); - BGD_DECLARE(void) gdImageGifAnimAdd(gdImagePtr im, FILE *outFile, int LocalCM, int LeftOfs, int TopOfs, int Delay, int Disposal, gdImagePtr previm); - BGD_DECLARE(void) gdImageGifAnimEnd(FILE *outFile); - BGD_DECLARE(void) gdImageGifAnimBeginCtx(gdImagePtr im, gdIOCtx *out, int GlobalCM, int Loops); - BGD_DECLARE(void) gdImageGifAnimAddCtx(gdImagePtr im, gdIOCtx *out, int LocalCM, int LeftOfs, int TopOfs, int Delay, int Disposal, gdImagePtr previm); - BGD_DECLARE(void) gdImageGifAnimEndCtx(gdIOCtx *out); - BGD_DECLARE(void *) gdImageGifAnimBeginPtr(gdImagePtr im, int *size, int GlobalCM, int Loops); - BGD_DECLARE(void *) gdImageGifAnimAddPtr(gdImagePtr im, int *size, int LocalCM, int LeftOfs, int TopOfs, int Delay, int Disposal, gdImagePtr previm); - BGD_DECLARE(void *) gdImageGifAnimEndPtr(int *size); - - /* A custom data sink. For backwards compatibility. Use - gdIOCtx instead. */ - /* The sink function must return -1 on error, otherwise the number - of bytes written, which must be equal to len. */ - /* context will be passed to your sink function. */ - typedef struct { - int (*sink) (void *context, const char *buffer, int len); - void *context; - } - gdSink, *gdSinkPtr; - - BGD_DECLARE(void) gdImagePngToSink (gdImagePtr im, gdSinkPtr out); - - BGD_DECLARE(void) gdImageGd (gdImagePtr im, FILE * out); - BGD_DECLARE(void) gdImageGd2 (gdImagePtr im, FILE * out, int cs, int fmt); - - /* Best to free this memory with gdFree(), not free() */ - BGD_DECLARE(void *) gdImageGifPtr (gdImagePtr im, int *size); - - /* Best to free this memory with gdFree(), not free() */ - BGD_DECLARE(void *) gdImagePngPtr (gdImagePtr im, int *size); - BGD_DECLARE(void *) gdImagePngPtrEx (gdImagePtr im, int *size, int level); - - /* Best to free this memory with gdFree(), not free() */ - BGD_DECLARE(void *) gdImageGdPtr (gdImagePtr im, int *size); - - /* Best to free this memory with gdFree(), not free() */ - BGD_DECLARE(void *) gdImageGd2Ptr (gdImagePtr im, int cs, int fmt, int *size); - - /* Style is a bitwise OR ( | operator ) of these. - gdArc and gdChord are mutually exclusive; - gdChord just connects the starting and ending - angles with a straight line, while gdArc produces - a rounded edge. gdPie is a synonym for gdArc. - gdNoFill indicates that the arc or chord should be - outlined, not filled. gdEdged, used together with - gdNoFill, indicates that the beginning and ending - angles should be connected to the center; this is - a good way to outline (rather than fill) a - 'pie slice'. */ +BGD_DECLARE(void) gdImageColorDeallocate (gdImagePtr im, int color); + +/* Converts a truecolor image to a palette-based image, + using a high-quality two-pass quantization routine + which attempts to preserve alpha channel information + as well as R/G/B color information when creating + a palette. If ditherFlag is set, the image will be + dithered to approximate colors better, at the expense + of some obvious "speckling." colorsWanted can be + anything up to 256. If the original source image + includes photographic information or anything that + came out of a JPEG, 256 is strongly recommended. + + Better yet, don't use these function -- write real + truecolor PNGs and JPEGs. The disk space gain of + conversion to palette is not great (for small images + it can be negative) and the quality loss is ugly. + + DIFFERENCES: gdImageCreatePaletteFromTrueColor creates and + returns a new image. gdImageTrueColorToPalette modifies + an existing image, and the truecolor pixels are discarded. + + gdImageTrueColorToPalette() returns TRUE on success, FALSE on failure. + + */ + +BGD_DECLARE(gdImagePtr) gdImageCreatePaletteFromTrueColor (gdImagePtr im, int ditherFlag, + int colorsWanted); + +BGD_DECLARE(int) gdImageTrueColorToPalette (gdImagePtr im, int ditherFlag, + int colorsWanted); + +/* + Selects quantization method used for subsequent gdImageTrueColorToPalette calls. + See gdPaletteQuantizationMethod enum (e.g. GD_QUANT_NEUQUANT, GD_QUANT_LIQ). + Speed is from 1 (highest quality) to 10 (fastest). + Speed 0 selects method-specific default (recommended). + + Returns FALSE if the given method is invalid or not available. +*/ +BGD_DECLARE(int) gdImageTrueColorToPaletteSetMethod (gdImagePtr im, int method, int speed); + +/* + Chooses quality range that subsequent call to gdImageTrueColorToPalette will aim for. + Min and max quality is in range 1-100 (1 = ugly, 100 = perfect). Max must be higher than min. + If palette cannot represent image with at least min_quality, then image will remain true-color. + If palette can represent image with quality better than max_quality, then lower number of colors will be used. + This function has effect only when GD_QUANT_LIQ method has been selected and the source image is true-color. +*/ +BGD_DECLARE(void) gdImageTrueColorToPaletteSetQuality (gdImagePtr im, int min_quality, int max_quality); + +/* Specifies a color index (if a palette image) or an + RGB color (if a truecolor image) which should be + considered 100% transparent. FOR TRUECOLOR IMAGES, + THIS IS IGNORED IF AN ALPHA CHANNEL IS BEING + SAVED. Use gdImageSaveAlpha(im, 0); to + turn off the saving of a full alpha channel in + a truecolor image. Note that gdImageColorTransparent + is usually compatible with older browsers that + do not understand full alpha channels well. TBB */ +BGD_DECLARE(void) gdImageColorTransparent (gdImagePtr im, int color); + +BGD_DECLARE(void) gdImagePaletteCopy (gdImagePtr dst, gdImagePtr src); + +typedef int (*gdCallbackImageColor)(gdImagePtr im, int src); + +BGD_DECLARE(int) gdImageColorReplace(gdImagePtr im, int src, int dst); +BGD_DECLARE(int) gdImageColorReplaceThreshold(gdImagePtr im, int src, int dst, float threshold); +BGD_DECLARE(int) gdImageColorReplaceArray(gdImagePtr im, int len, int *src, int *dst); +BGD_DECLARE(int) gdImageColorReplaceCallback(gdImagePtr im, gdCallbackImageColor callback); + +BGD_DECLARE(void) gdImageGif (gdImagePtr im, FILE * out); +BGD_DECLARE(void) gdImagePng (gdImagePtr im, FILE * out); +BGD_DECLARE(void) gdImagePngCtx (gdImagePtr im, gdIOCtx * out); +BGD_DECLARE(void) gdImageGifCtx (gdImagePtr im, gdIOCtx * out); +BGD_DECLARE(void) gdImageTiff(gdImagePtr im, FILE *outFile); +BGD_DECLARE(void *) gdImageTiffPtr(gdImagePtr im, int *size); +BGD_DECLARE(void) gdImageTiffCtx(gdImagePtr image, gdIOCtx *out); + +BGD_DECLARE(void *) gdImageBmpPtr(gdImagePtr im, int *size, int compression); +BGD_DECLARE(void) gdImageBmp(gdImagePtr im, FILE *outFile, int compression); +BGD_DECLARE(void) gdImageBmpCtx(gdImagePtr im, gdIOCtxPtr out, int compression); + +/* 2.0.12: Compression level: 0-9 or -1, where 0 is NO COMPRESSION at all, + 1 is FASTEST but produces larger files, 9 provides the best + compression (smallest files) but takes a long time to compress, and + -1 selects the default compiled into the zlib library. */ +BGD_DECLARE(void) gdImagePngEx (gdImagePtr im, FILE * out, int level); +BGD_DECLARE(void) gdImagePngCtxEx (gdImagePtr im, gdIOCtx * out, int level); + +BGD_DECLARE(void) gdImageWBMP (gdImagePtr image, int fg, FILE * out); +BGD_DECLARE(void) gdImageWBMPCtx (gdImagePtr image, int fg, gdIOCtx * out); + +/* Guaranteed to correctly free memory returned + by the gdImage*Ptr functions */ +BGD_DECLARE(void) gdFree (void *m); + +/* Best to free this memory with gdFree(), not free() */ +BGD_DECLARE(void *) gdImageWBMPPtr (gdImagePtr im, int *size, int fg); + +/* 100 is highest quality (there is always a little loss with JPEG). + 0 is lowest. 10 is about the lowest useful setting. */ +BGD_DECLARE(void) gdImageJpeg (gdImagePtr im, FILE * out, int quality); +BGD_DECLARE(void) gdImageJpegCtx (gdImagePtr im, gdIOCtx * out, int quality); + +/* Best to free this memory with gdFree(), not free() */ +BGD_DECLARE(void *) gdImageJpegPtr (gdImagePtr im, int *size, int quality); + +/* Legal values for Disposal. gdDisposalNone is always used by + the built-in optimizer if previm is passed. */ + +enum { + gdDisposalUnknown, + gdDisposalNone, + gdDisposalRestoreBackground, + gdDisposalRestorePrevious +}; + +BGD_DECLARE(void) gdImageGifAnimBegin(gdImagePtr im, FILE *outFile, int GlobalCM, int Loops); +BGD_DECLARE(void) gdImageGifAnimAdd(gdImagePtr im, FILE *outFile, int LocalCM, int LeftOfs, int TopOfs, int Delay, int Disposal, gdImagePtr previm); +BGD_DECLARE(void) gdImageGifAnimEnd(FILE *outFile); +BGD_DECLARE(void) gdImageGifAnimBeginCtx(gdImagePtr im, gdIOCtx *out, int GlobalCM, int Loops); +BGD_DECLARE(void) gdImageGifAnimAddCtx(gdImagePtr im, gdIOCtx *out, int LocalCM, int LeftOfs, int TopOfs, int Delay, int Disposal, gdImagePtr previm); +BGD_DECLARE(void) gdImageGifAnimEndCtx(gdIOCtx *out); +BGD_DECLARE(void *) gdImageGifAnimBeginPtr(gdImagePtr im, int *size, int GlobalCM, int Loops); +BGD_DECLARE(void *) gdImageGifAnimAddPtr(gdImagePtr im, int *size, int LocalCM, int LeftOfs, int TopOfs, int Delay, int Disposal, gdImagePtr previm); +BGD_DECLARE(void *) gdImageGifAnimEndPtr(int *size); + +/* A custom data sink. For backwards compatibility. Use + gdIOCtx instead. */ +/* The sink function must return -1 on error, otherwise the number + of bytes written, which must be equal to len. */ +/* context will be passed to your sink function. */ +typedef struct { + int (*sink) (void *context, const char *buffer, int len); + void *context; +} +gdSink, *gdSinkPtr; + +BGD_DECLARE(void) gdImagePngToSink (gdImagePtr im, gdSinkPtr out); + +BGD_DECLARE(void) gdImageGd (gdImagePtr im, FILE * out); +BGD_DECLARE(void) gdImageGd2 (gdImagePtr im, FILE * out, int cs, int fmt); + +/* Best to free this memory with gdFree(), not free() */ +BGD_DECLARE(void *) gdImageGifPtr (gdImagePtr im, int *size); + +/* Best to free this memory with gdFree(), not free() */ +BGD_DECLARE(void *) gdImagePngPtr (gdImagePtr im, int *size); +BGD_DECLARE(void *) gdImagePngPtrEx (gdImagePtr im, int *size, int level); + +/* Best to free this memory with gdFree(), not free() */ +BGD_DECLARE(void *) gdImageGdPtr (gdImagePtr im, int *size); + +/* Best to free this memory with gdFree(), not free() */ +BGD_DECLARE(void *) gdImageGd2Ptr (gdImagePtr im, int cs, int fmt, int *size); + +/* Style is a bitwise OR ( | operator ) of these. + gdArc and gdChord are mutually exclusive; + gdChord just connects the starting and ending + angles with a straight line, while gdArc produces + a rounded edge. gdPie is a synonym for gdArc. + gdNoFill indicates that the arc or chord should be + outlined, not filled. gdEdged, used together with + gdNoFill, indicates that the beginning and ending + angles should be connected to the center; this is + a good way to outline (rather than fill) a + 'pie slice'. */ #define gdArc 0 #define gdPie gdArc #define gdChord 1 #define gdNoFill 2 #define gdEdged 4 - BGD_DECLARE(void) gdImageFilledArc (gdImagePtr im, int cx, int cy, int w, int h, int s, - int e, int color, int style); - BGD_DECLARE(void) gdImageArc (gdImagePtr im, int cx, int cy, int w, int h, int s, int e, - int color); - BGD_DECLARE(void) gdImageEllipse(gdImagePtr im, int cx, int cy, int w, int h, int color); - BGD_DECLARE(void) gdImageFilledEllipse (gdImagePtr im, int cx, int cy, int w, int h, - int color); - BGD_DECLARE(void) gdImageFillToBorder (gdImagePtr im, int x, int y, int border, - int color); - BGD_DECLARE(void) gdImageFill (gdImagePtr im, int x, int y, int color); - BGD_DECLARE(void) gdImageCopy (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, - int srcX, int srcY, int w, int h); - BGD_DECLARE(void) gdImageCopyMerge (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, - int srcX, int srcY, int w, int h, int pct); - BGD_DECLARE(void) gdImageCopyMergeGray (gdImagePtr dst, gdImagePtr src, int dstX, - int dstY, int srcX, int srcY, int w, int h, - int pct); - - /* Stretches or shrinks to fit, as needed. Does NOT attempt - to average the entire set of source pixels that scale down onto the - destination pixel. */ - BGD_DECLARE(void) gdImageCopyResized (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, - int srcX, int srcY, int dstW, int dstH, int srcW, - int srcH); - - /* gd 2.0: stretches or shrinks to fit, as needed. When called with a - truecolor destination image, this function averages the - entire set of source pixels that scale down onto the - destination pixel, taking into account what portion of the - destination pixel each source pixel represents. This is a - floating point operation, but this is not a performance issue - on modern hardware, except for some embedded devices. If the - destination is a palette image, gdImageCopyResized is - substituted automatically. */ - BGD_DECLARE(void) gdImageCopyResampled (gdImagePtr dst, gdImagePtr src, int dstX, - int dstY, int srcX, int srcY, int dstW, int dstH, - int srcW, int srcH); - - /* gd 2.0.8: gdImageCopyRotated is added. Source - is a rectangle, with its upper left corner at - srcX and srcY. Destination is the *center* of - the rotated copy. Angle is in degrees, same as - gdImageArc. Floating point destination center - coordinates allow accurate rotation of - objects of odd-numbered width or height. */ - BGD_DECLARE(void) gdImageCopyRotated (gdImagePtr dst, - gdImagePtr src, - double dstX, double dstY, - int srcX, int srcY, - int srcWidth, int srcHeight, int angle); - - BGD_DECLARE(void) gdImageSetBrush (gdImagePtr im, gdImagePtr brush); - BGD_DECLARE(void) gdImageSetTile (gdImagePtr im, gdImagePtr tile); - BGD_DECLARE(void) gdImageSetAntiAliased (gdImagePtr im, int c); - BGD_DECLARE(void) gdImageSetAntiAliasedDontBlend (gdImagePtr im, int c, int dont_blend); - BGD_DECLARE(void) gdImageSetStyle (gdImagePtr im, int *style, int noOfPixels); - /* Line thickness (defaults to 1). Affects lines, ellipses, - rectangles, polygons and so forth. */ - BGD_DECLARE(void) gdImageSetThickness (gdImagePtr im, int thickness); - /* On or off (1 or 0) for all three of these. */ - BGD_DECLARE(void) gdImageInterlace (gdImagePtr im, int interlaceArg); - BGD_DECLARE(void) gdImageAlphaBlending (gdImagePtr im, int alphaBlendingArg); - BGD_DECLARE(void) gdImageSaveAlpha (gdImagePtr im, int saveAlphaArg); - - - BGD_DECLARE(gdImagePtr) gdImageCrop(gdImagePtr src, const gdRect *crop); - BGD_DECLARE(gdImagePtr) gdImageAutoCrop(gdImagePtr im, const unsigned int mode); - BGD_DECLARE(gdImagePtr) gdImageThresholdCrop(gdImagePtr im, const unsigned int color, const float threshold); - - BGD_DECLARE(void) gdImageFlipHorizontal(gdImagePtr im); - BGD_DECLARE(void) gdImageFlipVertical(gdImagePtr im); - BGD_DECLARE(void) gdImageFlipBoth(gdImagePtr im); - - BGD_DECLARE(gdImagePtr) gdImageNeuQuant(gdImagePtr im, const int max_color, int sample_factor); - - enum gdPixelateMode { - GD_PIXELATE_UPPERLEFT, - GD_PIXELATE_AVERAGE - }; - - BGD_DECLARE(int) gdImagePixelate(gdImagePtr im, int block_size, const unsigned int mode); - - typedef struct { - int sub; - int plus; - unsigned int num_colors; - int *colors; - unsigned int seed; - } gdScatter, *gdScatterPtr; - - BGD_DECLARE(int) gdImageScatter(gdImagePtr im, int sub, int plus); - BGD_DECLARE(int) gdImageScatterColor(gdImagePtr im, int sub, int plus, int colors[], unsigned int num_colors); - BGD_DECLARE(int) gdImageScatterEx(gdImagePtr im, gdScatterPtr s); - - /* Macros to access information about images. */ - - /* Returns nonzero if the image is a truecolor image, - zero for a palette image. */ - +BGD_DECLARE(void) gdImageFilledArc (gdImagePtr im, int cx, int cy, int w, int h, int s, + int e, int color, int style); +BGD_DECLARE(void) gdImageArc (gdImagePtr im, int cx, int cy, int w, int h, int s, int e, + int color); +BGD_DECLARE(void) gdImageEllipse(gdImagePtr im, int cx, int cy, int w, int h, int color); +BGD_DECLARE(void) gdImageFilledEllipse (gdImagePtr im, int cx, int cy, int w, int h, + int color); +BGD_DECLARE(void) gdImageFillToBorder (gdImagePtr im, int x, int y, int border, + int color); +BGD_DECLARE(void) gdImageFill (gdImagePtr im, int x, int y, int color); +BGD_DECLARE(void) gdImageCopy (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, + int srcX, int srcY, int w, int h); +BGD_DECLARE(void) gdImageCopyMerge (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, + int srcX, int srcY, int w, int h, int pct); +BGD_DECLARE(void) gdImageCopyMergeGray (gdImagePtr dst, gdImagePtr src, int dstX, + int dstY, int srcX, int srcY, int w, int h, + int pct); + +/* Stretches or shrinks to fit, as needed. Does NOT attempt + to average the entire set of source pixels that scale down onto the + destination pixel. */ +BGD_DECLARE(void) gdImageCopyResized (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, + int srcX, int srcY, int dstW, int dstH, int srcW, + int srcH); + +/* gd 2.0: stretches or shrinks to fit, as needed. When called with a + truecolor destination image, this function averages the + entire set of source pixels that scale down onto the + destination pixel, taking into account what portion of the + destination pixel each source pixel represents. This is a + floating point operation, but this is not a performance issue + on modern hardware, except for some embedded devices. If the + destination is a palette image, gdImageCopyResized is + substituted automatically. */ +BGD_DECLARE(void) gdImageCopyResampled (gdImagePtr dst, gdImagePtr src, int dstX, + int dstY, int srcX, int srcY, int dstW, int dstH, + int srcW, int srcH); + +/* gd 2.0.8: gdImageCopyRotated is added. Source + is a rectangle, with its upper left corner at + srcX and srcY. Destination is the *center* of + the rotated copy. Angle is in degrees, same as + gdImageArc. Floating point destination center + coordinates allow accurate rotation of + objects of odd-numbered width or height. */ +BGD_DECLARE(void) gdImageCopyRotated (gdImagePtr dst, + gdImagePtr src, + double dstX, double dstY, + int srcX, int srcY, + int srcWidth, int srcHeight, int angle); + +BGD_DECLARE(void) gdImageSetBrush (gdImagePtr im, gdImagePtr brush); +BGD_DECLARE(void) gdImageSetTile (gdImagePtr im, gdImagePtr tile); +BGD_DECLARE(void) gdImageSetAntiAliased (gdImagePtr im, int c); +BGD_DECLARE(void) gdImageSetAntiAliasedDontBlend (gdImagePtr im, int c, int dont_blend); +BGD_DECLARE(void) gdImageSetStyle (gdImagePtr im, int *style, int noOfPixels); +/* Line thickness (defaults to 1). Affects lines, ellipses, + rectangles, polygons and so forth. */ +BGD_DECLARE(void) gdImageSetThickness (gdImagePtr im, int thickness); +/* On or off (1 or 0) for all three of these. */ +BGD_DECLARE(void) gdImageInterlace (gdImagePtr im, int interlaceArg); +BGD_DECLARE(void) gdImageAlphaBlending (gdImagePtr im, int alphaBlendingArg); +BGD_DECLARE(void) gdImageSaveAlpha (gdImagePtr im, int saveAlphaArg); + +BGD_DECLARE(gdImagePtr) gdImageNeuQuant(gdImagePtr im, const int max_color, int sample_factor); + +enum gdPixelateMode { + GD_PIXELATE_UPPERLEFT, + GD_PIXELATE_AVERAGE +}; + +BGD_DECLARE(int) gdImagePixelate(gdImagePtr im, int block_size, const unsigned int mode); + +typedef struct { + int sub; + int plus; + unsigned int num_colors; + int *colors; + unsigned int seed; +} gdScatter, *gdScatterPtr; + +BGD_DECLARE(int) gdImageScatter(gdImagePtr im, int sub, int plus); +BGD_DECLARE(int) gdImageScatterColor(gdImagePtr im, int sub, int plus, int colors[], unsigned int num_colors); +BGD_DECLARE(int) gdImageScatterEx(gdImagePtr im, gdScatterPtr s); + +/* Macros to access information about images. */ + +/* Returns nonzero if the image is a truecolor image, + zero for a palette image. */ #define gdImageTrueColor(im) ((im)->trueColor) #define gdImageSX(im) ((im)->sx) @@ -851,38 +931,38 @@ extern "C" { #define gdImageGetTransparent(im) ((im)->transparent) #define gdImageGetInterlaced(im) ((im)->interlace) - /* These macros provide direct access to pixels in - palette-based and truecolor images, respectively. - If you use these macros, you must perform your own - bounds checking. Use of the macro for the correct type - of image is also your responsibility. */ +/* These macros provide direct access to pixels in + palette-based and truecolor images, respectively. + If you use these macros, you must perform your own + bounds checking. Use of the macro for the correct type + of image is also your responsibility. */ #define gdImagePalettePixel(im, x, y) (im)->pixels[(y)][(x)] #define gdImageTrueColorPixel(im, x, y) (im)->tpixels[(y)][(x)] #define gdImageResolutionX(im) (im)->res_x #define gdImageResolutionY(im) (im)->res_y - /* I/O Support routines. */ - - BGD_DECLARE(gdIOCtx *) gdNewFileCtx (FILE *); - /* If data is null, size is ignored and an initial data buffer is - allocated automatically. NOTE: this function assumes gd has the right - to free or reallocate "data" at will! Also note that gd will free - "data" when the IO context is freed. If data is not null, it must point - to memory allocated with gdMalloc, or by a call to gdImage[something]Ptr. - If not, see gdNewDynamicCtxEx for an alternative. */ - BGD_DECLARE(gdIOCtx *) gdNewDynamicCtx (int size, void *data); - /* 2.0.21: if freeFlag is nonzero, gd will free and/or reallocate "data" as - needed as described above. If freeFlag is zero, gd will never free - or reallocate "data", which means that the context should only be used - for *reading* an image from a memory buffer, or writing an image to a - memory buffer which is already large enough. If the memory buffer is - not large enough and an image write is attempted, the write operation - will fail. Those wishing to write an image to a buffer in memory have - a much simpler alternative in the gdImage[something]Ptr functions. */ - BGD_DECLARE(gdIOCtx *) gdNewDynamicCtxEx (int size, void *data, int freeFlag); - BGD_DECLARE(gdIOCtx *) gdNewSSCtx (gdSourcePtr in, gdSinkPtr out); - BGD_DECLARE(void *) gdDPExtractData (struct gdIOCtx *ctx, int *size); +/* I/O Support routines. */ + +BGD_DECLARE(gdIOCtx *) gdNewFileCtx (FILE *); +/* If data is null, size is ignored and an initial data buffer is + allocated automatically. NOTE: this function assumes gd has the right + to free or reallocate "data" at will! Also note that gd will free + "data" when the IO context is freed. If data is not null, it must point + to memory allocated with gdMalloc, or by a call to gdImage[something]Ptr. + If not, see gdNewDynamicCtxEx for an alternative. */ +BGD_DECLARE(gdIOCtx *) gdNewDynamicCtx (int size, void *data); +/* 2.0.21: if freeFlag is nonzero, gd will free and/or reallocate "data" as + needed as described above. If freeFlag is zero, gd will never free + or reallocate "data", which means that the context should only be used + for *reading* an image from a memory buffer, or writing an image to a + memory buffer which is already large enough. If the memory buffer is + not large enough and an image write is attempted, the write operation + will fail. Those wishing to write an image to a buffer in memory have + a much simpler alternative in the gdImage[something]Ptr functions. */ +BGD_DECLARE(gdIOCtx *) gdNewDynamicCtxEx (int size, void *data, int freeFlag); +BGD_DECLARE(gdIOCtx *) gdNewSSCtx (gdSourcePtr in, gdSinkPtr out); +BGD_DECLARE(void *) gdDPExtractData (struct gdIOCtx *ctx, int *size); #define GD2_CHUNKSIZE 128 #define GD2_CHUNKSIZE_MIN 64 @@ -894,8 +974,88 @@ extern "C" { #define GD2_FMT_RAW 1 #define GD2_FMT_COMPRESSED 2 - /* Image comparison definitions */ - BGD_DECLARE(int) gdImageCompare (gdImagePtr im1, gdImagePtr im2); +/* Image comparison definitions */ +BGD_DECLARE(int) gdImageCompare (gdImagePtr im1, gdImagePtr im2); + +BGD_DECLARE(void) gdImageFlipHorizontal(gdImagePtr im); +BGD_DECLARE(void) gdImageFlipVertical(gdImagePtr im); +BGD_DECLARE(void) gdImageFlipBoth(gdImagePtr im); + +#define GD_FLIP_HORINZONTAL 1 +#define GD_FLIP_VERTICAL 2 +#define GD_FLIP_BOTH 3 + +/** + * Group: Crop + * + * Constants: gdCropMode + * GD_CROP_DEFAULT - Default crop mode (4 corners or background) + * GD_CROP_TRANSPARENT - Crop using the transparent color + * GD_CROP_BLACK - Crop black borders + * GD_CROP_WHITE - Crop white borders + * GD_CROP_SIDES - Crop using colors of the 4 corners + * + * See also: + * + **/ +enum gdCropMode { + GD_CROP_DEFAULT = 0, + GD_CROP_TRANSPARENT, + GD_CROP_BLACK, + GD_CROP_WHITE, + GD_CROP_SIDES, + GD_CROP_THRESHOLD +}; + +BGD_DECLARE(gdImagePtr) gdImageCrop(gdImagePtr src, const gdRectPtr crop); +BGD_DECLARE(gdImagePtr) gdImageCropAuto(gdImagePtr im, const unsigned int mode); +BGD_DECLARE(gdImagePtr) gdImageCropThreshold(gdImagePtr im, const unsigned int color, const float threshold); + +int gdImageSetInterpolationMethod(gdImagePtr im, gdInterpolationMethod id); + +gdImagePtr gdImageScaleBilinear(gdImagePtr im, const unsigned int new_width, const unsigned int new_height); +gdImagePtr gdImageScaleBicubic(gdImagePtr src_img, const unsigned int new_width, const unsigned int new_height); +gdImagePtr gdImageScaleBicubicFixed(gdImagePtr src, const unsigned int width, const unsigned int height); +gdImagePtr gdImageScaleNearestNeighbour(gdImagePtr im, const unsigned int width, const unsigned int height); +gdImagePtr gdImageScaleTwoPass(const gdImagePtr pOrigImage, const unsigned int uOrigWidth, const unsigned int uOrigHeight, const unsigned int uNewWidth, const unsigned int uNewHeight); +gdImagePtr gdImageScale(const gdImagePtr src, const unsigned int new_width, const unsigned int new_height); + +gdImagePtr gdImageRotateNearestNeighbour(gdImagePtr src, const float degrees, const int bgColor); +gdImagePtr gdImageRotateBilinear(gdImagePtr src, const float degrees, const int bgColor); +gdImagePtr gdImageRotateBicubicFixed(gdImagePtr src, const float degrees, const int bgColor); +gdImagePtr gdImageRotateGeneric(gdImagePtr src, const float degrees, const int bgColor); +gdImagePtr gdImageRotateInterpolated(const gdImagePtr src, const float angle, int bgcolor); + +typedef enum { + GD_AFFINE_TRANSLATE = 0, + GD_AFFINE_SCALE, + GD_AFFINE_ROTATE, + GD_AFFINE_SHEAR_HORIZONTAL, + GD_AFFINE_SHEAR_VERTICAL, +} gdAffineStandardMatrix; + +int gdAffineApplyToPointF (gdPointFPtr dst, const gdPointFPtr src, const double affine[6]); +int gdAffineInvert (double dst[6], const double src[6]); +int gdAffineFlip (double dst_affine[6], const double src_affine[6], const int flip_h, const int flip_v); +int gdAffineConcat (double dst[6], const double m1[6], const double m2[6]); + +int gdAffineIdentity (double dst[6]); +int gdAffineScale (double dst[6], const double scale_x, const double scale_y); +int gdAffineRotate (double dst[6], const double angle); +int gdAffineShearHorizontal (double dst[6], const double angle); +int gdAffineShearVertical(double dst[6], const double angle); +int gdAffineTranslate (double dst[6], const double offset_x, const double offset_y); +double gdAffineExpansion (const double src[6]); +int gdAffineRectilinear (const double src[6]); +int gdAffineEqual (const double matrix1[6], const double matrix2[6]); +int gdTransformAffineGetImage(gdImagePtr *dst, const gdImagePtr src, gdRectPtr src_area, const double affine[6]); +int gdTransformAffineCopy(gdImagePtr dst, int dst_x, int dst_y, const gdImagePtr src, gdRectPtr src_region, const double affine[6]); +/* +gdTransformAffineCopy(gdImagePtr dst, int x0, int y0, int x1, int y1, + const gdImagePtr src, int src_width, int src_height, + const double affine[6]); +*/ +int gdTransformAffineBoundingBox(gdRectPtr src, const double affine[6], gdRectPtr bbox); #define GD_CMP_IMAGE 1 /* Actual image IS different */ #define GD_CMP_NUM_COLORS 2 /* Number of Colours in pallette differ */ @@ -907,13 +1067,14 @@ extern "C" { #define GD_CMP_INTERLACE 128 /* Interlaced setting */ #define GD_CMP_TRUECOLOR 256 /* Truecolor vs palette differs */ -#define GD_RESOLUTION 96 /* dots per inch */ +/* resolution affects ttf font rendering, particularly hinting */ +#define GD_RESOLUTION 96 /* pixels per inch */ #ifdef __cplusplus - } +} #endif - /* newfangled special effects */ +/* newfangled special effects */ #include "gdfx.h" #endif /* GD_H */ diff --git a/src/gd_interpolation.c b/src/gd_interpolation.c new file mode 100644 index 000000000..47ba5e336 --- /dev/null +++ b/src/gd_interpolation.c @@ -0,0 +1,2567 @@ +/* + * The two pass scaling function is based on: + * Filtered Image Rescaling + * Based on Gems III + * - Schumacher general filtered image rescaling + * (pp. 414-424) + * by Dale Schumacher + * + * Additional changes by Ray Gardener, Daylon Graphics Ltd. + * December 4, 1999 + * + * Ported to libgd by Pierre Joye. Support for multiple channels + * added (argb for now). + * + * Initial sources code is avaibable in the Gems Source Code Packages: + * http://www.acm.org/pubs/tog/GraphicsGems/GGemsIII.tar.gz + * + */ + +/* + Summary: + + - Horizontal filter contributions are calculated on the fly, + as each column is mapped from src to dst image. This lets + us omit having to allocate a temporary full horizontal stretch + of the src image. + + - If none of the src pixels within a sampling region differ, + then the output pixel is forced to equal (any of) the source pixel. + This ensures that filters do not corrupt areas of constant color. + + - Filter weight contribution results, after summing, are + rounded to the nearest pixel color value instead of + being casted to ILubyte (usually an int or char). Otherwise, + artifacting occurs. + +*/ + +/* + Additional functions are available for simple rotation or up/downscaling. + downscaling using the fixed point implementations are usually much faster + than the existing gdImageCopyResampled while having a similar or better + quality. + + For image rotations, the optimized versions have a lazy antialiasing for + the edges of the images. For a much better antialiased result, the affine + function is recommended. +*/ + +/* +TODO: + - Optimize pixel accesses and loops once we have continuous buffer + - Add scale support for a portion only of an image (equivalent of copyresized/resampled) + */ + +#include +#include +#include +#include + +#include +#include "gdhelpers.h" + +#ifdef _MSC_VER +# pragma optimize("t", on) +# include +#endif + +#ifndef MIN +#define MIN(a,b) ((a)<(b)?(a):(b)) +#endif +#define MIN3(a,b,c) ((a)<(b)?(MIN(a,c)):(MIN(b,c))) +#ifndef MAX +#define MAX(a,b) ((a)<(b)?(b):(a)) +#endif +#define MAX3(a,b,c) ((a)<(b)?(MAX(b,c)):(MAX(a,c))) + +#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x))) + +/* only used here, let do a generic fixed point integers later if required by other + part of GD */ +typedef long gdFixed; +/* Integer to fixed point */ +#define gd_itofx(x) ((x) << 8) + +/* Float to fixed point */ +#define gd_ftofx(x) (long)((x) * 256) + +/* Double to fixed point */ +#define gd_dtofx(x) (long)((x) * 256) + +/* Fixed point to integer */ +#define gd_fxtoi(x) ((x) >> 8) + +/* Fixed point to float */ +# define gd_fxtof(x) ((float)(x) / 256) + +/* Fixed point to double */ +#define gd_fxtod(x) ((double)(x) / 256) + +/* Multiply a fixed by a fixed */ +#define gd_mulfx(x,y) (((x) * (y)) >> 8) + +/* Divide a fixed by a fixed */ +#define gd_divfx(x,y) (((x) << 8) / (y)) + +typedef struct +{ + double *Weights; /* Normalized weights of neighboring pixels */ + int Left,Right; /* Bounds of source pixels window */ +} ContributionType; /* Contirbution information for a single pixel */ + +typedef struct +{ + ContributionType *ContribRow; /* Row (or column) of contribution weights */ + unsigned int WindowSize, /* Filter window size (of affecting source pixels) */ + LineLength; /* Length of line (no. or rows / cols) */ +} LineContribType; + +/* Each core filter has its own radius */ +#define DEFAULT_FILTER_BICUBIC 3.0 +#define DEFAULT_FILTER_BOX 0.5 +#define DEFAULT_FILTER_GENERALIZED_CUBIC 0.5 +#define DEFAULT_FILTER_RADIUS 1.0 +#define DEFAULT_LANCZOS8_RADIUS 8.0 +#define DEFAULT_LANCZOS3_RADIUS 3.0 +#define DEFAULT_HERMITE_RADIUS 1.0 +#define DEFAULT_BOX_RADIUS 0.5 +#define DEFAULT_TRIANGLE_RADIUS 1.0 +#define DEFAULT_BELL_RADIUS 1.5 +#define DEFAULT_CUBICSPLINE_RADIUS 2.0 +#define DEFAULT_MITCHELL_RADIUS 2.0 +#define DEFAULT_COSINE_RADIUS 1.0 +#define DEFAULT_CATMULLROM_RADIUS 2.0 +#define DEFAULT_QUADRATIC_RADIUS 1.5 +#define DEFAULT_QUADRATICBSPLINE_RADIUS 1.5 +#define DEFAULT_CUBICCONVOLUTION_RADIUS 3.0 +#define DEFAULT_GAUSSIAN_RADIUS 1.0 +#define DEFAULT_HANNING_RADIUS 1.0 +#define DEFAULT_HAMMING_RADIUS 1.0 +#define DEFAULT_SINC_RADIUS 1.0 +#define DEFAULT_WELSH_RADIUS 1.0 + +enum GD_RESIZE_FILTER_TYPE{ + FILTER_DEFAULT = 0, + FILTER_BELL, + FILTER_BESSEL, + FILTER_BLACKMAN, + FILTER_BOX, + FILTER_BSPLINE, + FILTER_CATMULLROM, + FILTER_COSINE, + FILTER_CUBICCONVOLUTION, + FILTER_CUBICSPLINE, + FILTER_HERMITE, + FILTER_LANCZOS3, + FILTER_LANCZOS8, + FILTER_MITCHELL, + FILTER_QUADRATIC, + FILTER_QUADRATICBSPLINE, + FILTER_TRIANGLE, + FILTER_GAUSSIAN, + FILTER_HANNING, + FILTER_HAMMING, + FILTER_SINC, + FILTER_WELSH, + + FILTER_CALLBACK = 999 +}; + +typedef enum GD_RESIZE_FILTER_TYPE gdResizeFilterType; + +static double KernelBessel_J1(const double x) +{ + double p, q; + + register long i; + + static const double + Pone[] = + { + 0.581199354001606143928050809e+21, + -0.6672106568924916298020941484e+20, + 0.2316433580634002297931815435e+19, + -0.3588817569910106050743641413e+17, + 0.2908795263834775409737601689e+15, + -0.1322983480332126453125473247e+13, + 0.3413234182301700539091292655e+10, + -0.4695753530642995859767162166e+7, + 0.270112271089232341485679099e+4 + }, + Qone[] = + { + 0.11623987080032122878585294e+22, + 0.1185770712190320999837113348e+20, + 0.6092061398917521746105196863e+17, + 0.2081661221307607351240184229e+15, + 0.5243710262167649715406728642e+12, + 0.1013863514358673989967045588e+10, + 0.1501793594998585505921097578e+7, + 0.1606931573481487801970916749e+4, + 0.1e+1 + }; + + p = Pone[8]; + q = Qone[8]; + for (i=7; i >= 0; i--) + { + p = p*x*x+Pone[i]; + q = q*x*x+Qone[i]; + } + return (double)(p/q); +} + +static double KernelBessel_P1(const double x) +{ + double p, q; + + register long i; + + static const double + Pone[] = + { + 0.352246649133679798341724373e+5, + 0.62758845247161281269005675e+5, + 0.313539631109159574238669888e+5, + 0.49854832060594338434500455e+4, + 0.2111529182853962382105718e+3, + 0.12571716929145341558495e+1 + }, + Qone[] = + { + 0.352246649133679798068390431e+5, + 0.626943469593560511888833731e+5, + 0.312404063819041039923015703e+5, + 0.4930396490181088979386097e+4, + 0.2030775189134759322293574e+3, + 0.1e+1 + }; + + p = Pone[5]; + q = Qone[5]; + for (i=4; i >= 0; i--) + { + p = p*(8.0/x)*(8.0/x)+Pone[i]; + q = q*(8.0/x)*(8.0/x)+Qone[i]; + } + return (double)(p/q); +} + +static double KernelBessel_Q1(const double x) +{ + double p, q; + + register long i; + + static const double + Pone[] = + { + 0.3511751914303552822533318e+3, + 0.7210391804904475039280863e+3, + 0.4259873011654442389886993e+3, + 0.831898957673850827325226e+2, + 0.45681716295512267064405e+1, + 0.3532840052740123642735e-1 + }, + Qone[] = + { + 0.74917374171809127714519505e+4, + 0.154141773392650970499848051e+5, + 0.91522317015169922705904727e+4, + 0.18111867005523513506724158e+4, + 0.1038187585462133728776636e+3, + 0.1e+1 + }; + + p = Pone[5]; + q = Qone[5]; + for (i=4; i >= 0; i--) + { + p = p*(8.0/x)*(8.0/x)+Pone[i]; + q = q*(8.0/x)*(8.0/x)+Qone[i]; + } + return (double)(p/q); +} + +static double KernelBessel_Order1(double x) +{ + double p, q; + + if (x == 0.0) + return (0.0f); + p = x; + if (x < 0.0) + x=(-x); + if (x < 8.0) + return (p*KernelBessel_J1(x)); + q = (double)sqrt(2.0f/(M_PI*x))*(double)(KernelBessel_P1(x)*(1.0f/sqrt(2.0f)*(sin(x)-cos(x)))-8.0f/x*KernelBessel_Q1(x)* + (-1.0f/sqrt(2.0f)*(sin(x)+cos(x)))); + if (p < 0.0f) + q = (-q); + return (q); +} + +static double filter_bessel(const double x) +{ + if (x == 0.0f) + return (double)(M_PI/4.0f); + return (KernelBessel_Order1((double)M_PI*x)/(2.0f*x)); +} + + +static double filter_blackman(const double x) +{ + return (0.42f+0.5f*(double)cos(M_PI*x)+0.08f*(double)cos(2.0f*M_PI*x)); +} + +/** + * Bicubic interpolation kernel (a=-1): + \verbatim + / + | 1-2|t|**2+|t|**3 , if |t| < 1 + h(t) = | 4-8|t|+5|t|**2-|t|**3 , if 1<=|t|<2 + | 0 , otherwise + \ + \endverbatim + * ***bd*** 2.2004 + */ +static double filter_bicubic(const double t) +{ + const double abs_t = (double)fabs(t); + const double abs_t_sq = abs_t * abs_t; + if (abs_t<1) return 1-2*abs_t_sq+abs_t_sq*abs_t; + if (abs_t<2) return 4 - 8*abs_t +5*abs_t_sq - abs_t_sq*abs_t; + return 0; +} + +/** + * Generalized cubic kernel (for a=-1 it is the same as BicubicKernel): + \verbatim + / + | (a+2)|t|**3 - (a+3)|t|**2 + 1 , |t| <= 1 + h(t) = | a|t|**3 - 5a|t|**2 + 8a|t| - 4a , 1 < |t| <= 2 + | 0 , otherwise + \ + \endverbatim + * Often used values for a are -1 and -1/2. + */ +static double filter_generalized_cubic(const double t) +{ + const double a = -DEFAULT_FILTER_GENERALIZED_CUBIC; + double abs_t = (double)fabs(t); + double abs_t_sq = abs_t * abs_t; + if (abs_t < 1) return (a + 2) * abs_t_sq * abs_t - (a + 3) * abs_t_sq + 1; + if (abs_t < 2) return a * abs_t_sq * abs_t - 5 * a * abs_t_sq + 8 * a * abs_t - 4 * a; + return 0; +} + +/* CubicSpline filter, default radius 2 */ +static double filter_cubic_spline(const double x1) +{ + const double x = x1 < 0.0 ? -x1 : x1; + + if (x < 1.0 ) { + const double x2 = x*x; + + return (0.5 * x2 * x - x2 + 2.0 / 3.0); + } + if (x < 2.0) { + return (pow(2.0 - x, 3.0)/6.0); + } + return 0; +} + +/* CubicConvolution filter, default radius 3 */ +static double filter_cubic_convolution(const double x1) +{ + const double x = x1 < 0.0 ? -x1 : x1; + const double x2 = x1 * x1; + const double x2_x = x2 * x; + + if (x <= 1.0) return ((4.0 / 3.0)* x2_x - (7.0 / 3.0) * x2 + 1.0); + if (x <= 2.0) return (- (7.0 / 12.0) * x2_x + 3 * x2 - (59.0 / 12.0) * x + 2.5); + if (x <= 3.0) return ( (1.0/12.0) * x2_x - (2.0 / 3.0) * x2 + 1.75 * x - 1.5); + return 0; +} + +static double filter_box(double x) { + if (x < - DEFAULT_FILTER_BOX) + return 0.0f; + if (x < DEFAULT_FILTER_BOX) + return 1.0f; + return 0.0f; +} + +static double filter_catmullrom(const double x) +{ + if (x < -2.0) + return(0.0f); + if (x < -1.0) + return(0.5f*(4.0f+x*(8.0f+x*(5.0f+x)))); + if (x < 0.0) + return(0.5f*(2.0f+x*x*(-5.0f-3.0f*x))); + if (x < 1.0) + return(0.5f*(2.0f+x*x*(-5.0f+3.0f*x))); + if (x < 2.0) + return(0.5f*(4.0f+x*(-8.0f+x*(5.0f-x)))); + return(0.0f); +} + +static double filter_filter(double t) +{ + /* f(t) = 2|t|^3 - 3|t|^2 + 1, -1 <= t <= 1 */ + if(t < 0.0) t = -t; + if(t < 1.0) return((2.0 * t - 3.0) * t * t + 1.0); + return(0.0); +} + + +/* Lanczos8 filter, default radius 8 */ +static double filter_lanczos8(const double x1) +{ + const double x = x1 < 0.0 ? -x1 : x1; +#define R DEFAULT_LANCZOS8_RADIUS + + if ( x == 0.0) return 1; + + if ( x < R) { + return R * sin(x*M_PI) * sin(x * M_PI/ R) / (x * M_PI * x * M_PI); + } + return 0.0; +#undef R +} + + +/* Lanczos3 filter, default radius 3 */ +static double filter_lanczos3(const double x1) +{ + const double x = x1 < 0.0 ? -x1 : x1; +#define R DEFAULT_LANCZOS3_RADIUS + + if ( x == 0.0) return 1; + + if ( x < R) + { + return R * sin(x*M_PI) * sin(x * M_PI / R) / (x * M_PI * x * M_PI); + } + return 0.0; +#undef R +} + +/* Hermite filter, default radius 1 */ +static double filter_hermite(const double x1) +{ + const double x = x1 < 0.0 ? -x1 : x1; + + if (x < 1.0) return ((2.0 * x - 3) * x * x + 1.0 ); + + return 0.0; +} + +/* Trangle filter, default radius 1 */ +static double filter_triangle(const double x1) +{ + const double x = x1 < 0.0 ? -x1 : x1; + if (x < 1.0) return (1.0 - x); + return 0.0; +} + +/* Bell filter, default radius 1.5 */ +static double filter_bell(const double x1) +{ + const double x = x1 < 0.0 ? -x1 : x1; + + if (x < 0.5) return (0.75 - x*x); + if (x < 1.5) return (0.5 * pow(x - 1.5, 2.0)); + return 0.0; +} + +/* Mitchell filter, default radius 2.0 */ +static double filter_mitchell(const double x) +{ +#define KM_B (1.0f/3.0f) +#define KM_C (1.0f/3.0f) +#define KM_P0 (( 6.0f - 2.0f * KM_B ) / 6.0f) +#define KM_P2 ((-18.0f + 12.0f * KM_B + 6.0f * KM_C) / 6.0f) +#define KM_P3 (( 12.0f - 9.0f * KM_B - 6.0f * KM_C) / 6.0f) +#define KM_Q0 (( 8.0f * KM_B + 24.0f * KM_C) / 6.0f) +#define KM_Q1 ((-12.0f * KM_B - 48.0f * KM_C) / 6.0f) +#define KM_Q2 (( 6.0f * KM_B + 30.0f * KM_C) / 6.0f) +#define KM_Q3 (( -1.0f * KM_B - 6.0f * KM_C) / 6.0f) + + if (x < -2.0) + return(0.0f); + if (x < -1.0) + return(KM_Q0-x*(KM_Q1-x*(KM_Q2-x*KM_Q3))); + if (x < 0.0f) + return(KM_P0+x*x*(KM_P2-x*KM_P3)); + if (x < 1.0f) + return(KM_P0+x*x*(KM_P2+x*KM_P3)); + if (x < 2.0f) + return(KM_Q0+x*(KM_Q1+x*(KM_Q2+x*KM_Q3))); + return(0.0f); +} + + + +/* Cosine filter, default radius 1 */ +static double filter_cosine(const double x) +{ + if ((x >= -1.0) && (x <= 1.0)) return ((cos(x * M_PI) + 1.0)/2.0); + + return 0; +} + +/* Quadratic filter, default radius 1.5 */ +static double filter_quadratic(const double x1) +{ + const double x = x1 < 0.0 ? -x1 : x1; + + if (x <= 0.5) return (- 2.0 * x * x + 1); + if (x <= 1.5) return (x * x - 2.5* x + 1.5); + return 0.0; +} + +static double filter_bspline(const double x) +{ + if (x>2.0f) { + return 0.0f; + } else { + double a, b, c, d; + /* Was calculated anyway cause the "if((x-1.0f) < 0)" */ + const double xm1 = x - 1.0f; + const double xp1 = x + 1.0f; + const double xp2 = x + 2.0f; + + if ((xp2) <= 0.0f) a = 0.0f; else a = xp2*xp2*xp2; + if ((xp1) <= 0.0f) b = 0.0f; else b = xp1*xp1*xp1; + if (x <= 0) c = 0.0f; else c = x*x*x; + if ((xm1) <= 0.0f) d = 0.0f; else d = xm1*xm1*xm1; + + return (0.16666666666666666667f * (a - (4.0f * b) + (6.0f * c) - (4.0f * d))); + } +} + +/* QuadraticBSpline filter, default radius 1.5 */ +static double filter_quadratic_bspline(const double x1) +{ + const double x = x1 < 0.0 ? -x1 : x1; + + if (x <= 0.5) return (- x * x + 0.75); + if (x <= 1.5) return (0.5 * x * x - 1.5 * x + 1.125); + return 0.0; +} + +static double filter_gaussian(const double x) +{ + /* return(exp((double) (-2.0 * x * x)) * sqrt(2.0 / M_PI)); */ + return (double)(exp(-2.0f * x * x) * 0.79788456080287f); +} + +static double filter_hanning(const double x) +{ + /* A Cosine windowing function */ + return(0.5 + 0.5 * cos(M_PI * x)); +} + +static double filter_hamming(const double x) +{ + /* should be + (0.54+0.46*cos(M_PI*(double) x)); + but this approximation is sufficient */ + if (x < -1.0f) + return 0.0f; + if (x < 0.0f) + return 0.92f*(-2.0f*x-3.0f)*x*x+1.0f; + if (x < 1.0f) + return 0.92f*(2.0f*x-3.0f)*x*x+1.0f; + return 0.0f; +} + +static double filter_power(const double x) +{ + const double a = 2.0f; + if (fabs(x)>1) return 0.0f; + return (1.0f - (double)fabs(pow(x,a))); +} + +static double filter_sinc(const double x) +{ + /* X-scaled Sinc(x) function. */ + if (x == 0.0) return(1.0); + return (sin(M_PI * (double) x) / (M_PI * (double) x)); +} + +static double filter_welsh(const double x) +{ + /* Welsh parabolic windowing filter */ + if (x < 1.0) + return(1 - x*x); + return(0.0); +} + + +/* Copied from upstream's libgd */ +static inline int _color_blend (const int dst, const int src) +{ + const int src_alpha = gdTrueColorGetAlpha(src); + + if( src_alpha == gdAlphaOpaque ) { + return src; + } else { + const int dst_alpha = gdTrueColorGetAlpha(dst); + + if( src_alpha == gdAlphaTransparent ) return dst; + if( dst_alpha == gdAlphaTransparent ) { + return src; + } else { + register int alpha, red, green, blue; + const int src_weight = gdAlphaTransparent - src_alpha; + const int dst_weight = (gdAlphaTransparent - dst_alpha) * src_alpha / gdAlphaMax; + const int tot_weight = src_weight + dst_weight; + + alpha = src_alpha * dst_alpha / gdAlphaMax; + + red = (gdTrueColorGetRed(src) * src_weight + + gdTrueColorGetRed(dst) * dst_weight) / tot_weight; + green = (gdTrueColorGetGreen(src) * src_weight + + gdTrueColorGetGreen(dst) * dst_weight) / tot_weight; + blue = (gdTrueColorGetBlue(src) * src_weight + + gdTrueColorGetBlue(dst) * dst_weight) / tot_weight; + + return ((alpha << 24) + (red << 16) + (green << 8) + blue); + } + } +} + +static inline int _setEdgePixel(const gdImagePtr src, unsigned int x, unsigned int y, gdFixed coverage, const int bgColor) +{ + const gdFixed f_127 = gd_itofx(127); + register int c = src->tpixels[y][x]; + c = c | (( (int) (gd_fxtof(gd_mulfx(coverage, f_127)) + 50.5f)) << 24); + return _color_blend(bgColor, c); +} + +static inline int getPixelOverflowTC(gdImagePtr im, const int x, const int y, const int bgColor) +{ + if (gdImageBoundsSafe(im, x, y)) { + const int c = im->tpixels[y][x]; + if (c == im->transparent) { + return bgColor == -1 ? gdTrueColorAlpha(0, 0, 0, 127) : bgColor; + } + return c; + } else { + register int border; + + if (y < im->cy1) { + border = im->tpixels[0][im->cx1]; + goto processborder; + } + + if (y < im->cy1) { + border = im->tpixels[0][im->cx1]; + goto processborder; + } + + if (y > im->cy2) { + if (x >= im->cx1 && x <= im->cx1) { + border = im->tpixels[im->cy2][x]; + goto processborder; + } else { + return gdTrueColorAlpha(0, 0, 0, 127); + } + } + + /* y is bound safe at this point */ + if (x < im->cx1) { + border = im->tpixels[y][im->cx1]; + goto processborder; + } + + if (x > im->cx2) { + border = im->tpixels[y][im->cx2]; + } + +processborder: + if (border == im->transparent) { + return gdTrueColorAlpha(0, 0, 0, 127); + } else{ + return gdTrueColorAlpha(gdTrueColorGetRed(border), gdTrueColorGetGreen(border), gdTrueColorGetBlue(border), 127); + } + } +} + +#define colorIndex2RGBA(c) gdTrueColorAlpha(im->red[(c)], im->green[(c)], im->blue[(c)], im->alpha[(c)]) +#define colorIndex2RGBcustomA(c, a) gdTrueColorAlpha(im->red[(c)], im->green[(c)], im->blue[(c)], im->alpha[(a)]) +static inline int getPixelOverflowPalette(gdImagePtr im, const int x, const int y, const int bgColor) +{ + if (gdImageBoundsSafe(im, x, y)) { + const int c = im->pixels[y][x]; + if (c == im->transparent) { + return bgColor == -1 ? gdTrueColorAlpha(0, 0, 0, 127) : bgColor; + } + return colorIndex2RGBA(c); + } else { + register int border; + if (y < im->cy1) { + border = gdImageGetPixel(im, im->cx1, 0); + goto processborder; + } + + if (y < im->cy1) { + border = gdImageGetPixel(im, im->cx1, 0); + goto processborder; + } + + if (y > im->cy2) { + if (x >= im->cx1 && x <= im->cx1) { + border = gdImageGetPixel(im, x, im->cy2); + goto processborder; + } else { + return gdTrueColorAlpha(0, 0, 0, 127); + } + } + + /* y is bound safe at this point */ + if (x < im->cx1) { + border = gdImageGetPixel(im, im->cx1, y); + goto processborder; + } + + if (x > im->cx2) { + border = gdImageGetPixel(im, im->cx2, y); + } + +processborder: + if (border == im->transparent) { + return gdTrueColorAlpha(0, 0, 0, 127); + } else{ + return colorIndex2RGBcustomA(border, 127); + } + } +} + +static int getPixelInterpolateWeight(gdImagePtr im, const double x, const double y, const int bgColor) +{ + /* Closest pixel <= (xf,yf) */ + int sx = (int)(x); + int sy = (int)(y); + const double xf = x - (double)sx; + const double yf = y - (double)sy; + const double nxf = (double) 1.0 - xf; + const double nyf = (double) 1.0 - yf; + const double m1 = xf * yf; + const double m2 = nxf * yf; + const double m3 = xf * nyf; + const double m4 = nxf * nyf; + + /* get color values of neighbouring pixels */ + const int c1 = im->trueColor == 1 ? getPixelOverflowTC(im, sx, sy, bgColor) : getPixelOverflowPalette(im, sx, sy, bgColor); + const int c2 = im->trueColor == 1 ? getPixelOverflowTC(im, sx - 1, sy, bgColor) : getPixelOverflowPalette(im, sx - 1, sy, bgColor); + const int c3 = im->trueColor == 1 ? getPixelOverflowTC(im, sx, sy - 1, bgColor) : getPixelOverflowPalette(im, sx, sy - 1, bgColor); + const int c4 = im->trueColor == 1 ? getPixelOverflowTC(im, sx - 1, sy - 1, bgColor) : getPixelOverflowPalette(im, sx, sy - 1, bgColor); + int r, g, b, a; + + if (x < 0) sx--; + if (y < 0) sy--; + + /* component-wise summing-up of color values */ + if (im->trueColor) { + r = (int)(m1*gdTrueColorGetRed(c1) + m2*gdTrueColorGetRed(c2) + m3*gdTrueColorGetRed(c3) + m4*gdTrueColorGetRed(c4)); + g = (int)(m1*gdTrueColorGetGreen(c1) + m2*gdTrueColorGetGreen(c2) + m3*gdTrueColorGetGreen(c3) + m4*gdTrueColorGetGreen(c4)); + b = (int)(m1*gdTrueColorGetBlue(c1) + m2*gdTrueColorGetBlue(c2) + m3*gdTrueColorGetBlue(c3) + m4*gdTrueColorGetBlue(c4)); + a = (int)(m1*gdTrueColorGetAlpha(c1) + m2*gdTrueColorGetAlpha(c2) + m3*gdTrueColorGetAlpha(c3) + m4*gdTrueColorGetAlpha(c4)); + } else { + r = (int)(m1*im->red[(c1)] + m2*im->red[(c2)] + m3*im->red[(c3)] + m4*im->red[(c4)]); + g = (int)(m1*im->green[(c1)] + m2*im->green[(c2)] + m3*im->green[(c3)] + m4*im->green[(c4)]); + b = (int)(m1*im->blue[(c1)] + m2*im->blue[(c2)] + m3*im->blue[(c3)] + m4*im->blue[(c4)]); + a = (int)(m1*im->alpha[(c1)] + m2*im->alpha[(c2)] + m3*im->alpha[(c3)] + m4*im->alpha[(c4)]); + } + + r = CLAMP(r, 0, 255); + g = CLAMP(g, 0, 255); + b = CLAMP(b, 0, 255); + a = CLAMP(a, 0, gdAlphaMax); + return gdTrueColorAlpha(r, g, b, a); +} + +/** + * Function: getPixelInterpolated + * Returns the interpolated color value using the default interpolation + * method. The returned color is always in the ARGB format (truecolor). + * + * Parameters: + * im - Image to set the default interpolation method + * y - X value of the ideal position + * y - Y value of the ideal position + * method - Interpolation method + * + * Returns: + * GD_TRUE if the affine is rectilinear or GD_FALSE + * + * See also: + * + */ +int getPixelInterpolated(gdImagePtr im, const double x, const double y, const int bgColor) +{ + const int xi=(int)((x) < 0 ? x - 1: x); + const int yi=(int)((y) < 0 ? y - 1: y); + int yii; + int i; + double kernel, kernel_cache_y; + double kernel_x[12], kernel_y[4]; + double new_r = 0.0f, new_g = 0.0f, new_b = 0.0f, new_a = 0.0f; + + /* These methods use special implementations */ + if (im->interpolation_id == GD_BILINEAR_FIXED || im->interpolation_id == GD_BICUBIC_FIXED || im->interpolation_id == GD_NEAREST_NEIGHBOUR) { + return -1; + } + + if (im->interpolation_id == GD_WEIGHTED4) { + return getPixelInterpolateWeight(im, x, y, bgColor); + } + + if (im->interpolation_id == GD_NEAREST_NEIGHBOUR) { + if (im->trueColor == 1) { + return getPixelOverflowTC(im, xi, yi, bgColor); + } else { + return getPixelOverflowPalette(im, xi, yi, bgColor); + } + } + if (im->interpolation) { + for (i=0; i<4; i++) { + kernel_x[i] = (double) im->interpolation((double)(xi+i-1-x)); + kernel_y[i] = (double) im->interpolation((double)(yi+i-1-y)); + } + } else { + return -1; + } + + /* + * TODO: use the known fast rgba multiplication implementation once + * the new formats are in place + */ + for (yii = yi-1; yii < yi+3; yii++) { + int xii; + kernel_cache_y = kernel_y[yii-(yi-1)]; + if (im->trueColor) { + for (xii=xi-1; xiiWindowSize = windows_size; + res->LineLength = line_length; + res->ContribRow = (ContributionType *) gdMalloc(line_length * sizeof(ContributionType)); + + for (u = 0 ; u < line_length ; u++) { + res->ContribRow[u].Weights = (double *) gdMalloc(windows_size * sizeof(double)); + } + return res; +} + +static inline _gdContributionsFree(LineContribType * p) +{ + unsigned int u; + for (u = 0; u < p->LineLength; u++) { + gdFree(p->ContribRow[u].Weights); + } + gdFree(p->ContribRow); + gdFree(p); +} + +static inline LineContribType *_gdContributionsCalc(unsigned int line_size, unsigned int src_size, double scale_d, const interpolation_method pFilter) +{ + double width_d; + double scale_f_d = 1.0; + const double filter_width_d = DEFAULT_BOX_RADIUS; + int windows_size; + unsigned int u; + LineContribType *res; + + if (scale_d < 1.0) { + width_d = filter_width_d / scale_d; + scale_f_d = scale_d; + } else { + width_d= filter_width_d; + } + + windows_size = 2 * (int)ceil(width_d) + 1; + res = _gdContributionsAlloc(line_size, windows_size); + + for (u = 0; u < line_size; u++) { + const double dCenter = (double)u / scale_d; + /* get the significant edge points affecting the pixel */ + register int iLeft = MAX(0, (int)floor (dCenter - width_d)); + int iRight = MIN((int)ceil(dCenter + width_d), (int)src_size - 1); + double dTotalWeight = 0.0; + int iSrc; + + res->ContribRow[u].Left = iLeft; + res->ContribRow[u].Right = iRight; + + /* Cut edge points to fit in filter window in case of spill-off */ + if (iRight - iLeft + 1 > windows_size) { + if (iLeft < ((int)src_size - 1 / 2)) { + iLeft++; + } else { + iRight--; + } + } + + for (iSrc = iLeft; iSrc <= iRight; iSrc++) { + dTotalWeight += (res->ContribRow[u].Weights[iSrc-iLeft] = scale_f_d * (*pFilter)(scale_f_d * (dCenter - (double)iSrc))); + } + + if (dTotalWeight < 0.0) { + _gdContributionsFree(res); + return NULL; + } + + if (dTotalWeight > 0.0) { + for (iSrc = iLeft; iSrc <= iRight; iSrc++) { + res->ContribRow[u].Weights[iSrc-iLeft] /= dTotalWeight; + } + } + } + return res; +} + +static inline void _gdScaleRow(gdImagePtr pSrc, unsigned int src_width, gdImagePtr dst, unsigned int dst_width, unsigned int row, LineContribType *contrib) +{ + int *p_src_row = pSrc->tpixels[row]; + int *p_dst_row = dst->tpixels[row]; + unsigned int x; + + for (x = 0; x < dst_width - 1; x++) { + register unsigned char r = 0, g = 0, b = 0, a = 0; + const int left = contrib->ContribRow[x].Left; + const int right = contrib->ContribRow[x].Right; + int i; + + /* Accumulate each channel */ + for (i = left; i <= right; i++) { + const left_channel = i - left; + r += (unsigned char)(contrib->ContribRow[x].Weights[left_channel] * (double)(gdTrueColorGetRed(p_src_row[i]))); + g += (unsigned char)(contrib->ContribRow[x].Weights[left_channel] * (double)(gdTrueColorGetGreen(p_src_row[i]))); + b += (unsigned char)(contrib->ContribRow[x].Weights[left_channel] * (double)(gdTrueColorGetBlue(p_src_row[i]))); + a += (unsigned char)(contrib->ContribRow[x].Weights[left_channel] * (double)(gdTrueColorGetAlpha(p_src_row[i]))); + } + p_dst_row[x] = gdTrueColorAlpha(r, g, b, a); + } +} + +static inline void _gdScaleHoriz(gdImagePtr pSrc, unsigned int src_width, unsigned int src_height, gdImagePtr pDst, unsigned int dst_width, unsigned int dst_height) +{ + unsigned int u; + LineContribType * contrib; + + /* same width, just copy it */ + if (dst_width == src_width) { + unsigned int y; + for (y = 0; y < src_height - 1; ++y) { + memcpy(pDst->tpixels[y], pSrc->tpixels[y], src_width); + } + } + + contrib = _gdContributionsCalc(dst_width, src_width, (double)dst_width / (double)src_width, pSrc->interpolation); + if (contrib == NULL) { + return; + } + /* Scale each row */ + for (u = 0; u < dst_height - 1; u++) { + _gdScaleRow(pSrc, src_width, pDst, dst_width, u, contrib); + } + _gdContributionsFree (contrib); +} + +static inline _gdScaleCol (gdImagePtr pSrc, unsigned int src_width, gdImagePtr pRes, unsigned int dst_width, unsigned int dst_height, unsigned int uCol, LineContribType *contrib) +{ + unsigned int y; + for (y = 0; y < dst_height - 1; y++) { + register unsigned char r = 0, g = 0, b = 0, a = 0; + const int iLeft = contrib->ContribRow[y].Left; + const int iRight = contrib->ContribRow[y].Right; + int i; + int *row = pRes->tpixels[y]; + + /* Accumulate each channel */ + for (i = iLeft; i <= iRight; i++) { + const int pCurSrc = pSrc->tpixels[i][uCol]; + const int i_iLeft = i - iLeft; + r += (unsigned char)(contrib->ContribRow[y].Weights[i_iLeft] * (double)(gdTrueColorGetRed(pCurSrc))); + g += (unsigned char)(contrib->ContribRow[y].Weights[i_iLeft] * (double)(gdTrueColorGetGreen(pCurSrc))); + b += (unsigned char)(contrib->ContribRow[y].Weights[i_iLeft] * (double)(gdTrueColorGetBlue(pCurSrc))); + a += (unsigned char)(contrib->ContribRow[y].Weights[i_iLeft] * (double)(gdTrueColorGetAlpha(pCurSrc))); + } + pRes->tpixels[y][uCol] = gdTrueColorAlpha(r, g, b, a); + } +} + +static inline _gdScaleVert (const gdImagePtr pSrc, const unsigned int src_width, const unsigned int src_height, const gdImagePtr pDst, const unsigned int dst_width, const unsigned int dst_height) +{ + unsigned int u; + LineContribType * contrib; + + /* same height, copy it */ + if (src_height == dst_height) { + unsigned int y; + for (y = 0; y < src_height - 1; ++y) { + memcpy(pDst->tpixels[y], pSrc->tpixels[y], src_width); + } + } + + contrib = _gdContributionsCalc(dst_height, src_height, (double)(dst_height) / (double)(src_height), pSrc->interpolation); + /* scale each column */ + for (u = 0; u < dst_width - 1; u++) { + _gdScaleCol(pSrc, src_width, pDst, dst_width, dst_height, u, contrib); + } + _gdContributionsFree(contrib); +} + +gdImagePtr gdImageScaleTwoPass(const gdImagePtr src, const unsigned int src_width, const unsigned int src_height, const unsigned int new_width, const unsigned int new_height) +{ + gdImagePtr tmp_im; + gdImagePtr dst; + + tmp_im = gdImageCreateTrueColor(new_width, src_height); + if (tmp_im == NULL) { + return NULL; + } + _gdScaleHoriz (src, src_width, src_height, tmp_im, new_width, src_height); + + dst = gdImageCreateTrueColor(new_width, new_height); + if (dst == NULL) { + return NULL; + } + _gdScaleVert(tmp_im, new_width, src_height, dst, new_width, new_height); + gdFree(tmp_im); + + + return dst; +} + +gdImagePtr Scale(const gdImagePtr src, const unsigned int src_width, const unsigned int src_height, const gdImagePtr dst, const unsigned int new_width, const unsigned int new_height) +{ + gdImagePtr tmp_im; + + tmp_im = gdImageCreateTrueColor(new_width, src_height); + if (tmp_im == NULL) { + return NULL; + } + _gdScaleHoriz(src, src_width, src_height, tmp_im, new_width, src_height); + + _gdScaleVert(tmp_im, new_width, src_height, dst, new_width, new_height); + + gdFree(tmp_im); + return dst; +} + +/* + BilinearFixed, BicubicFixed and nearest implementations are rewamped versions of the implementation in CBitmapEx + http://www.codeproject.com/Articles/29121/CBitmapEx-Free-C-Bitmap-Manipulation-Class + Integer only implementation, good to have for common usages like pre scale very large + images before using another interpolation methods for the last step. +*/ +gdImagePtr gdImageScaleNearestNeighbour(gdImagePtr im, const unsigned int width, const unsigned int height) +{ + const unsigned long new_width = MAX(1, width); + const unsigned long new_height = MAX(1, height); + const float dx = (float)im->sx / (float)new_width; + const float dy = (float)im->sy / (float)new_height; + const gdFixed f_dx = gd_ftofx(dx); + const gdFixed f_dy = gd_ftofx(dy); + + gdImagePtr dst_img; + unsigned long dst_offset_x; + unsigned long dst_offset_y = 0; + unsigned int i; + + dst_img = gdImageCreateTrueColor(new_width, new_height); + + if (dst_img == NULL) { + return NULL; + } + + for (i=0; itrueColor) { + for (j=0; jtpixels[dst_offset_y][dst_offset_x++] = im->tpixels[m][n]; + } + } else { + for (j=0; jtpixels[dst_offset_y][dst_offset_x++] = colorIndex2RGBA(im->pixels[m][n]); + } + } + dst_offset_y++; + } + return dst_img; +} + +static inline int getPixelOverflowColorTC(gdImagePtr im, const int x, const int y, const int color) +{ + if (gdImageBoundsSafe(im, x, y)) { + const int c = im->tpixels[y][x]; + if (c == im->transparent) { + return gdTrueColorAlpha(0, 0, 0, 127); + } + return c; + } else { + register int border; + if (y < im->cy1) { + border = im->tpixels[0][im->cx1]; + goto processborder; + } + + if (y < im->cy1) { + border = im->tpixels[0][im->cx1]; + goto processborder; + } + + if (y > im->cy2) { + if (x >= im->cx1 && x <= im->cx1) { + border = im->tpixels[im->cy2][x]; + goto processborder; + } else { + return gdTrueColorAlpha(0, 0, 0, 127); + } + } + + /* y is bound safe at this point */ + if (x < im->cx1) { + border = im->tpixels[y][im->cx1]; + goto processborder; + } + + if (x > im->cx2) { + border = im->tpixels[y][im->cx2]; + } + +processborder: + if (border == im->transparent) { + return gdTrueColorAlpha(0, 0, 0, 127); + } else{ + return gdTrueColorAlpha(gdTrueColorGetRed(border), gdTrueColorGetGreen(border), gdTrueColorGetBlue(border), 127); + } + } +} + +static gdImagePtr gdImageScaleBilinearPalette(gdImagePtr im, const unsigned int new_width, const unsigned int new_height) +{ + long _width = MAX(1, new_width); + long _height = MAX(1, new_height); + float dx = (float)gdImageSX(im) / (float)_width; + float dy = (float)gdImageSY(im) / (float)_height; + gdFixed f_dx = gd_ftofx(dx); + gdFixed f_dy = gd_ftofx(dy); + gdFixed f_1 = gd_itofx(1); + + int dst_offset_h; + int dst_offset_v = 0; + long i; + gdImagePtr new_img; + const int transparent = im->transparent; + + new_img = gdImageCreateTrueColor(new_width, new_height); + if (new_img == NULL) { + return NULL; + } + new_img->transparent = gdTrueColorAlpha(im->red[transparent], im->green[transparent], im->blue[transparent], im->alpha[transparent]); + + for (i=0; i < _height; i++) { + long j; + const gdFixed f_i = gd_itofx(i); + const gdFixed f_a = gd_mulfx(f_i, f_dy); + register long m = gd_fxtoi(f_a); + + dst_offset_h = 0; + + for (j=0; j < _width; j++) { + /* Update bitmap */ + gdFixed f_j = gd_itofx(j); + gdFixed f_b = gd_mulfx(f_j, f_dx); + + const long n = gd_fxtoi(f_b); + gdFixed f_f = f_a - gd_itofx(m); + gdFixed f_g = f_b - gd_itofx(n); + + const gdFixed f_w1 = gd_mulfx(f_1-f_f, f_1-f_g); + const gdFixed f_w2 = gd_mulfx(f_1-f_f, f_g); + const gdFixed f_w3 = gd_mulfx(f_f, f_1-f_g); + const gdFixed f_w4 = gd_mulfx(f_f, f_g); + unsigned int pixel1; + unsigned int pixel2; + unsigned int pixel3; + unsigned int pixel4; + register gdFixed f_r1, f_r2, f_r3, f_r4, + f_g1, f_g2, f_g3, f_g4, + f_b1, f_b2, f_b3, f_b4, + f_a1, f_a2, f_a3, f_a4; + + /* zero for the background color, nothig gets outside anyway */ + pixel1 = getPixelOverflowPalette(im, n, m, 0); + pixel2 = getPixelOverflowPalette(im, n + 1, m, 0); + pixel3 = getPixelOverflowPalette(im, n, m + 1, 0); + pixel4 = getPixelOverflowPalette(im, n + 1, m + 1, 0); + + f_r1 = gd_itofx(gdTrueColorGetRed(pixel1)); + f_r2 = gd_itofx(gdTrueColorGetRed(pixel2)); + f_r3 = gd_itofx(gdTrueColorGetRed(pixel3)); + f_r4 = gd_itofx(gdTrueColorGetRed(pixel4)); + f_g1 = gd_itofx(gdTrueColorGetGreen(pixel1)); + f_g2 = gd_itofx(gdTrueColorGetGreen(pixel2)); + f_g3 = gd_itofx(gdTrueColorGetGreen(pixel3)); + f_g4 = gd_itofx(gdTrueColorGetGreen(pixel4)); + f_b1 = gd_itofx(gdTrueColorGetBlue(pixel1)); + f_b2 = gd_itofx(gdTrueColorGetBlue(pixel2)); + f_b3 = gd_itofx(gdTrueColorGetBlue(pixel3)); + f_b4 = gd_itofx(gdTrueColorGetBlue(pixel4)); + f_a1 = gd_itofx(gdTrueColorGetAlpha(pixel1)); + f_a2 = gd_itofx(gdTrueColorGetAlpha(pixel2)); + f_a3 = gd_itofx(gdTrueColorGetAlpha(pixel3)); + f_a4 = gd_itofx(gdTrueColorGetAlpha(pixel4)); + + { + const char red = (char) gd_fxtoi(gd_mulfx(f_w1, f_r1) + gd_mulfx(f_w2, f_r2) + gd_mulfx(f_w3, f_r3) + gd_mulfx(f_w4, f_r4)); + const char green = (char) gd_fxtoi(gd_mulfx(f_w1, f_g1) + gd_mulfx(f_w2, f_g2) + gd_mulfx(f_w3, f_g3) + gd_mulfx(f_w4, f_g4)); + const char blue = (char) gd_fxtoi(gd_mulfx(f_w1, f_b1) + gd_mulfx(f_w2, f_b2) + gd_mulfx(f_w3, f_b3) + gd_mulfx(f_w4, f_b4)); + const char alpha = (char) gd_fxtoi(gd_mulfx(f_w1, f_a1) + gd_mulfx(f_w2, f_a2) + gd_mulfx(f_w3, f_a3) + gd_mulfx(f_w4, f_a4)); + + new_img->tpixels[dst_offset_v][dst_offset_h] = gdTrueColorAlpha(red, green, blue, alpha); + } + + dst_offset_h++; + } + + dst_offset_v++; + } + return new_img; +} + +static gdImagePtr gdImageScaleBilinearTC(gdImagePtr im, const unsigned int new_width, const unsigned int new_height) +{ + long dst_w = MAX(1, new_width); + long dst_h = MAX(1, new_height); + float dx = (float)gdImageSX(im) / (float)dst_w; + float dy = (float)gdImageSY(im) / (float)dst_h; + gdFixed f_dx = gd_ftofx(dx); + gdFixed f_dy = gd_ftofx(dy); + gdFixed f_1 = gd_itofx(1); + + int dst_offset_h; + int dst_offset_v = 0; + int dwSrcTotalOffset; + long i; + gdImagePtr new_img; + + new_img = gdImageCreateTrueColor(new_width, new_height); + if (!new_img){ + return NULL; + } + + for (i=0; i < dst_h; i++) { + long j; + dst_offset_h = 0; + for (j=0; j < dst_w; j++) { + /* Update bitmap */ + gdFixed f_i = gd_itofx(i); + gdFixed f_j = gd_itofx(j); + gdFixed f_a = gd_mulfx(f_i, f_dy); + gdFixed f_b = gd_mulfx(f_j, f_dx); + const long m = gd_fxtoi(f_a); + const long n = gd_fxtoi(f_b); + gdFixed f_f = f_a - gd_itofx(m); + gdFixed f_g = f_b - gd_itofx(n); + + const gdFixed f_w1 = gd_mulfx(f_1-f_f, f_1-f_g); + const gdFixed f_w2 = gd_mulfx(f_1-f_f, f_g); + const gdFixed f_w3 = gd_mulfx(f_f, f_1-f_g); + const gdFixed f_w4 = gd_mulfx(f_f, f_g); + unsigned int pixel1; + unsigned int pixel2; + unsigned int pixel3; + unsigned int pixel4; + register gdFixed f_r1, f_r2, f_r3, f_r4, + f_g1, f_g2, f_g3, f_g4, + f_b1, f_b2, f_b3, f_b4, + f_a1, f_a2, f_a3, f_a4; + dwSrcTotalOffset = m + n; + /* 0 for bgColor, nothing gets outside anyway */ + pixel1 = getPixelOverflowTC(im, n, m, 0); + pixel2 = getPixelOverflowTC(im, n + 1, m, 0); + pixel3 = getPixelOverflowTC(im, n, m + 1, 0); + pixel4 = getPixelOverflowTC(im, n + 1, m + 1, 0); + + f_r1 = gd_itofx(gdTrueColorGetRed(pixel1)); + f_r2 = gd_itofx(gdTrueColorGetRed(pixel2)); + f_r3 = gd_itofx(gdTrueColorGetRed(pixel3)); + f_r4 = gd_itofx(gdTrueColorGetRed(pixel4)); + f_g1 = gd_itofx(gdTrueColorGetGreen(pixel1)); + f_g2 = gd_itofx(gdTrueColorGetGreen(pixel2)); + f_g3 = gd_itofx(gdTrueColorGetGreen(pixel3)); + f_g4 = gd_itofx(gdTrueColorGetGreen(pixel4)); + f_b1 = gd_itofx(gdTrueColorGetBlue(pixel1)); + f_b2 = gd_itofx(gdTrueColorGetBlue(pixel2)); + f_b3 = gd_itofx(gdTrueColorGetBlue(pixel3)); + f_b4 = gd_itofx(gdTrueColorGetBlue(pixel4)); + f_a1 = gd_itofx(gdTrueColorGetAlpha(pixel1)); + f_a2 = gd_itofx(gdTrueColorGetAlpha(pixel2)); + f_a3 = gd_itofx(gdTrueColorGetAlpha(pixel3)); + f_a4 = gd_itofx(gdTrueColorGetAlpha(pixel4)); + { + const char red = (char) gd_fxtoi(gd_mulfx(f_w1, f_r1) + gd_mulfx(f_w2, f_r2) + gd_mulfx(f_w3, f_r3) + gd_mulfx(f_w4, f_r4)); + const char green = (char) gd_fxtoi(gd_mulfx(f_w1, f_g1) + gd_mulfx(f_w2, f_g2) + gd_mulfx(f_w3, f_g3) + gd_mulfx(f_w4, f_g4)); + const char blue = (char) gd_fxtoi(gd_mulfx(f_w1, f_b1) + gd_mulfx(f_w2, f_b2) + gd_mulfx(f_w3, f_b3) + gd_mulfx(f_w4, f_b4)); + const char alpha = (char) gd_fxtoi(gd_mulfx(f_w1, f_a1) + gd_mulfx(f_w2, f_a2) + gd_mulfx(f_w3, f_a3) + gd_mulfx(f_w4, f_a4)); + + new_img->tpixels[dst_offset_v][dst_offset_h] = gdTrueColorAlpha(red, green, blue, alpha); + } + + dst_offset_h++; + } + + dst_offset_v++; + } + return new_img; +} + +gdImagePtr gdImageScaleBilinear(gdImagePtr im, const unsigned int new_width, const unsigned int new_height) +{ + if (im->trueColor) { + return gdImageScaleBilinearTC(im, new_width, new_height); + } else { + return gdImageScaleBilinearPalette(im, new_width, new_height); + } +} + +gdImagePtr gdImageScaleBicubicFixed(gdImagePtr src, const unsigned int width, const unsigned int height) +{ + const long new_width = MAX(1, width); + const long new_height = MAX(1, height); + const int src_w = gdImageSX(src); + const int src_h = gdImageSY(src); + const gdFixed f_dx = gd_ftofx((float)src_w / (float)new_width); + const gdFixed f_dy = gd_ftofx((float)src_h / (float)new_height); + const gdFixed f_1 = gd_itofx(1); + const gdFixed f_2 = gd_itofx(2); + const gdFixed f_4 = gd_itofx(4); + const gdFixed f_6 = gd_itofx(6); + const gdFixed f_gamma = gd_ftofx(1.04f); + gdImagePtr dst; + + unsigned int dst_offset_x; + unsigned int dst_offset_y = 0; + long i; + + /* impact perf a bit, but not that much. Implementation for palette + images can be done at a later point. + */ + if (src->trueColor == 0) { + gdImagePaletteToTrueColor(src); + } + + dst = gdImageCreateTrueColor(new_width, new_height); + if (!dst) { + return NULL; + } + + dst->saveAlphaFlag = 1; + + for (i=0; i < new_height; i++) { + long j; + dst_offset_x = 0; + + for (j=0; j < new_width; j++) { + const gdFixed f_a = gd_mulfx(gd_itofx(i), f_dy); + const gdFixed f_b = gd_mulfx(gd_itofx(j), f_dx); + const long m = gd_fxtoi(f_a); + const long n = gd_fxtoi(f_b); + const gdFixed f_f = f_a - gd_itofx(m); + const gdFixed f_g = f_b - gd_itofx(n); + unsigned int src_offset_x[16], src_offset_y[16]; + long k; + register gdFixed f_red = 0, f_green = 0, f_blue = 0, f_alpha = 0; + unsigned char red, green, blue, alpha = 0; + int *dst_row = dst->tpixels[dst_offset_y]; + + if ((m < 1) || (n < 1)) { + src_offset_x[0] = n; + src_offset_y[0] = m; + } else { + src_offset_x[0] = n - 1; + src_offset_y[0] = m; + } + + if (m < 1) { + src_offset_x[1] = n; + src_offset_y[1] = m; + } else { + src_offset_x[1] = n; + src_offset_y[1] = m; + } + + if ((m < 1) || (n >= src_w - 1)) { + src_offset_x[2] = n; + src_offset_y[2] = m; + } else { + src_offset_x[2] = n + 1; + src_offset_y[2] = m; + } + + if ((m < 1) || (n >= src_w - 2)) { + src_offset_x[3] = n; + src_offset_y[3] = m; + } else { + src_offset_x[3] = n + 1 + 1; + src_offset_y[3] = m; + } + + if (n < 1) { + src_offset_x[4] = n; + src_offset_y[4] = m; + } else { + src_offset_x[4] = n - 1; + src_offset_y[4] = m; + } + + src_offset_x[5] = n; + src_offset_y[5] = m; + if (n >= src_w-1) { + src_offset_x[6] = n; + src_offset_y[6] = m; + } else { + src_offset_x[6] = n + 1; + src_offset_y[6] = m; + } + + if (n >= src_w - 2) { + src_offset_x[7] = n; + src_offset_y[7] = m; + } else { + src_offset_x[7] = n + 1 + 1; + src_offset_y[7] = m; + } + + if ((m >= src_h - 1) || (n < 1)) { + src_offset_x[8] = n; + src_offset_y[8] = m; + } else { + src_offset_x[8] = n - 1; + src_offset_y[8] = m; + } + + if (m >= src_h - 1) { + src_offset_x[8] = n; + src_offset_y[8] = m; + } else { + src_offset_x[9] = n; + src_offset_y[9] = m; + } + + if ((m >= src_h-1) || (n >= src_w-1)) { + src_offset_x[10] = n; + src_offset_y[10] = m; + } else { + src_offset_x[10] = n + 1; + src_offset_y[10] = m; + } + + if ((m >= src_h - 1) || (n >= src_w - 2)) { + src_offset_x[11] = n; + src_offset_y[11] = m; + } else { + src_offset_x[11] = n + 1 + 1; + src_offset_y[11] = m; + } + + if ((m >= src_h - 2) || (n < 1)) { + src_offset_x[12] = n; + src_offset_y[12] = m; + } else { + src_offset_x[12] = n - 1; + src_offset_y[12] = m; + } + + if (m >= src_h - 2) { + src_offset_x[13] = n; + src_offset_y[13] = m; + } else { + src_offset_x[13] = n; + src_offset_y[13] = m; + } + + if ((m >= src_h - 2) || (n >= src_w - 1)) { + src_offset_x[14] = n; + src_offset_y[14] = m; + } else { + src_offset_x[14] = n + 1; + src_offset_y[14] = m; + } + + if ((m >= src_h - 2) || (n >= src_w - 2)) { + src_offset_x[15] = n; + src_offset_y[15] = m; + } else { + src_offset_x[15] = n + 1 + 1; + src_offset_y[15] = m; + } + + for (k = -1; k < 3; k++) { + const gdFixed f = gd_itofx(k)-f_f; + const gdFixed f_fm1 = f - f_1; + const gdFixed f_fp1 = f + f_1; + const gdFixed f_fp2 = f + f_2; + register gdFixed f_a = 0, f_b = 0, f_d = 0, f_c = 0; + register gdFixed f_RY; + int l; + + if (f_fp2 > 0) f_a = gd_mulfx(f_fp2, gd_mulfx(f_fp2,f_fp2)); + if (f_fp1 > 0) f_b = gd_mulfx(f_fp1, gd_mulfx(f_fp1,f_fp1)); + if (f > 0) f_c = gd_mulfx(f, gd_mulfx(f,f)); + if (f_fm1 > 0) f_d = gd_mulfx(f_fm1, gd_mulfx(f_fm1,f_fm1)); + + f_RY = gd_divfx((f_a - gd_mulfx(f_4,f_b) + gd_mulfx(f_6,f_c) - gd_mulfx(f_4,f_d)),f_6); + + for (l = -1; l < 3; l++) { + const gdFixed f = gd_itofx(l) - f_g; + const gdFixed f_fm1 = f - f_1; + const gdFixed f_fp1 = f + f_1; + const gdFixed f_fp2 = f + f_2; + register gdFixed f_a = 0, f_b = 0, f_c = 0, f_d = 0; + register gdFixed f_RX, f_R, f_rs, f_gs, f_bs, f_ba; + register int c; + const int _k = ((k+1)*4) + (l+1); + + if (f_fp2 > 0) f_a = gd_mulfx(f_fp2,gd_mulfx(f_fp2,f_fp2)); + + if (f_fp1 > 0) f_b = gd_mulfx(f_fp1,gd_mulfx(f_fp1,f_fp1)); + + if (f > 0) f_c = gd_mulfx(f,gd_mulfx(f,f)); + + if (f_fm1 > 0) f_d = gd_mulfx(f_fm1,gd_mulfx(f_fm1,f_fm1)); + + f_RX = gd_divfx((f_a-gd_mulfx(f_4,f_b)+gd_mulfx(f_6,f_c)-gd_mulfx(f_4,f_d)),f_6); + f_R = gd_mulfx(f_RY,f_RX); + + c = src->tpixels[*(src_offset_y + _k)][*(src_offset_x + _k)]; + f_rs = gd_itofx(gdTrueColorGetRed(c)); + f_gs = gd_itofx(gdTrueColorGetGreen(c)); + f_bs = gd_itofx(gdTrueColorGetBlue(c)); + f_ba = gd_itofx(gdTrueColorGetAlpha(c)); + + f_red += gd_mulfx(f_rs,f_R); + f_green += gd_mulfx(f_gs,f_R); + f_blue += gd_mulfx(f_bs,f_R); + f_alpha += gd_mulfx(f_ba,f_R); + } + } + + red = (unsigned char) CLAMP(gd_fxtoi(gd_mulfx(f_red, f_gamma)), 0, 255); + green = (unsigned char) CLAMP(gd_fxtoi(gd_mulfx(f_green, f_gamma)), 0, 255); + blue = (unsigned char) CLAMP(gd_fxtoi(gd_mulfx(f_blue, f_gamma)), 0, 255); + alpha = (unsigned char) CLAMP(gd_fxtoi(gd_mulfx(f_alpha, f_gamma)), 0, 127); + + *(dst_row + dst_offset_x) = gdTrueColorAlpha(red, green, blue, alpha); + + dst_offset_x++; + } + dst_offset_y++; + } + return dst; +} + +gdImagePtr gdImageScale(const gdImagePtr src, const unsigned int new_width, const unsigned int new_height) +{ + gdImagePtr im_scaled = NULL; + + if (src == NULL || src->interpolation_id < 0 || src->interpolation_id > GD_METHOD_COUNT) { + return 0; + } + + switch (src->interpolation_id) { + /*Special cases, optimized implementations */ + case GD_NEAREST_NEIGHBOUR: + im_scaled = gdImageScaleNearestNeighbour(src, new_width, new_height); + break; + + case GD_BILINEAR_FIXED: + im_scaled = gdImageScaleBilinear(src, new_width, new_height); + break; + + case GD_BICUBIC_FIXED: + im_scaled = gdImageScaleBicubicFixed(src, new_width, new_height); + break; + + /* generic */ + default: + if (src->interpolation == NULL) { + return NULL; + } + im_scaled = gdImageScaleTwoPass(src, src->sx, src->sy, new_width, new_height); + break; + } + return im_scaled; +} + +gdImagePtr gdImageRotateNearestNeighbour(gdImagePtr src, const float degrees, const int bgColor) +{ + float _angle = ((float) (-degrees / 180.0f) * (float)M_PI); + const int src_w = gdImageSX(src); + const int src_h = gdImageSY(src); + const unsigned int new_width = (unsigned int)(abs((int)(src_w * cos(_angle))) + abs((int)(src_h * sin(_angle))) + 0.5f); + const unsigned int new_height = (unsigned int)(abs((int)(src_w * sin(_angle))) + abs((int)(src_h * cos(_angle))) + 0.5f); + const gdFixed f_0_5 = gd_ftofx(0.5f); + const gdFixed f_H = gd_itofx(src_h/2); + const gdFixed f_W = gd_itofx(src_w/2); + const gdFixed f_cos = gd_ftofx(cos(-_angle)); + const gdFixed f_sin = gd_ftofx(sin(-_angle)); + + unsigned int dst_offset_x; + unsigned int dst_offset_y = 0; + unsigned int i; + gdImagePtr dst; + + /* impact perf a bit, but not that much. Implementation for palette + images can be done at a later point. + */ + if (src->trueColor == 0) { + gdImagePaletteToTrueColor(src); + } + + dst = gdImageCreateTrueColor(new_width, new_height); + if (!dst) { + return NULL; + } + dst->saveAlphaFlag = 1; + for (i = 0; i < new_height; i++) { + unsigned int j; + dst_offset_x = 0; + for (j = 0; j < new_width; j++) { + gdFixed f_i = gd_itofx(i - new_height/2); + gdFixed f_j = gd_itofx(j-new_width/2); + gdFixed f_m = gd_mulfx(f_j,f_sin) + gd_mulfx(f_i,f_cos) + f_0_5 + f_H; + gdFixed f_n = gd_mulfx(f_j,f_cos) - gd_mulfx(f_i,f_sin) + f_0_5 + f_W; + long m = gd_fxtoi(f_m); + long n = gd_fxtoi(f_n); + + if ((m > 0) && (m < src_h-1) && (n > 0) && (n < src_w-1)) { + if (dst_offset_y < new_height) { + dst->tpixels[dst_offset_y][dst_offset_x++] = src->tpixels[m][n]; + } + } else { + if (dst_offset_y < new_height) { + dst->tpixels[dst_offset_y][dst_offset_x++] = bgColor; + } + } + } + dst_offset_y++; + } + return dst; +} + +gdImagePtr gdImageRotateGeneric(gdImagePtr src, const float degrees, const int bgColor) +{ + float _angle = ((float) (-degrees / 180.0f) * (float)M_PI); + const int angle_rounded = (int)floor(degrees * 100); + const int src_w = gdImageSX(src); + const int src_h = gdImageSY(src); + const unsigned int new_width = (unsigned int)(abs((int)(src_w * cos(_angle))) + abs((int)(src_h * sin(_angle))) + 0.5f); + const unsigned int new_height = (unsigned int)(abs((int)(src_w * sin(_angle))) + abs((int)(src_h * cos(_angle))) + 0.5f); + const gdFixed f_0_5 = gd_ftofx(0.5f); + const gdFixed f_H = gd_itofx(src_h/2); + const gdFixed f_W = gd_itofx(src_w/2); + const gdFixed f_cos = gd_ftofx(cos(-_angle)); + const gdFixed f_sin = gd_ftofx(sin(-_angle)); + + unsigned int dst_offset_x; + unsigned int dst_offset_y = 0; + unsigned int i; + gdImagePtr dst; + + const gdFixed f_slop_y = f_sin; + const gdFixed f_slop_x = f_cos; + const gdFixed f_slop = f_slop_x > 0 && f_slop_x > 0 ? + f_slop_x > f_slop_y ? gd_divfx(f_slop_y, f_slop_x) : gd_divfx(f_slop_x, f_slop_y) + : 0; + + if (bgColor < 0) { + return NULL; + } + + /* impact perf a bit, but not that much. Implementation for palette + images can be done at a later point. + */ + if (src->trueColor == 0) { + gdImagePaletteToTrueColor(src); + } + + dst = gdImageCreateTrueColor(new_width, new_height); + if (!dst) { + return NULL; + } + dst->saveAlphaFlag = 1; + + for (i = 0; i < new_height; i++) { + unsigned int j; + dst_offset_x = 0; + for (j = 0; j < new_width; j++) { + gdFixed f_i = gd_itofx(i - new_height/ 2); + gdFixed f_j = gd_itofx(j -new_width / 2); + gdFixed f_m = gd_mulfx(f_j,f_sin) + gd_mulfx(f_i,f_cos) + f_0_5 + f_H; + gdFixed f_n = gd_mulfx(f_j,f_cos) - gd_mulfx(f_i,f_sin) + f_0_5 + f_W; + long m = gd_fxtoi(f_m); + long n = gd_fxtoi(f_n); + + if ((n <= 0) || (m <= 0) || (m >= src_h) || (n >= src_w)) { + dst->tpixels[dst_offset_y][dst_offset_x++] = bgColor; + } else if ((n <= 1) || (m <= 1) || (m >= src_h - 1) || (n >= src_w - 1)) { + gdFixed f_127 = gd_itofx(127); + register int c = getPixelInterpolated(src, n, m, bgColor); + c = c | (( gdTrueColorGetAlpha(c) + ((int)(127* gd_fxtof(f_slop)))) << 24); + + dst->tpixels[dst_offset_y][dst_offset_x++] = _color_blend(bgColor, c); + } else { + dst->tpixels[dst_offset_y][dst_offset_x++] = getPixelInterpolated(src, n, m, bgColor); + } + } + dst_offset_y++; + } + return dst; +} + +gdImagePtr gdImageRotateBilinear(gdImagePtr src, const float degrees, const int bgColor) +{ + float _angle = (float)((- degrees / 180.0f) * M_PI); + const unsigned int src_w = gdImageSX(src); + const unsigned int src_h = gdImageSY(src); + unsigned int new_width = abs((int)(src_w*cos(_angle))) + abs((int)(src_h*sin(_angle) + 0.5f)); + unsigned int new_height = abs((int)(src_w*sin(_angle))) + abs((int)(src_h*cos(_angle) + 0.5f)); + const gdFixed f_0_5 = gd_ftofx(0.5f); + const gdFixed f_H = gd_itofx(src_h/2); + const gdFixed f_W = gd_itofx(src_w/2); + const gdFixed f_cos = gd_ftofx(cos(-_angle)); + const gdFixed f_sin = gd_ftofx(sin(-_angle)); + const gdFixed f_1 = gd_itofx(1); + unsigned int i; + unsigned int dst_offset_x; + unsigned int dst_offset_y = 0; + unsigned int src_offset_x, src_offset_y; + gdImagePtr dst; + + /* impact perf a bit, but not that much. Implementation for palette + images can be done at a later point. + */ + if (src->trueColor == 0) { + gdImagePaletteToTrueColor(src); + } + + dst = gdImageCreateTrueColor(new_width, new_height); + if (dst == NULL) { + return NULL; + } + dst->saveAlphaFlag = 1; + + for (i = 0; i < new_height; i++) { + unsigned int j; + dst_offset_x = 0; + + for (j=0; j < new_width; j++) { + const gdFixed f_i = gd_itofx(i-new_height/2); + const gdFixed f_j = gd_itofx(j-new_width/2); + const gdFixed f_m = gd_mulfx(f_j,f_sin) + gd_mulfx(f_i,f_cos) + f_0_5 + f_H; + const gdFixed f_n = gd_mulfx(f_j,f_cos) - gd_mulfx(f_i,f_sin) + f_0_5 + f_W; + const unsigned int m = gd_fxtoi(f_m); + const unsigned int n = gd_fxtoi(f_n); + + if ((m > 0) && (m < src_h - 1) && (n > 0) && (n < src_w - 1)) { + const gdFixed f_f = f_m - gd_itofx(m); + const gdFixed f_g = f_n - gd_itofx(n); + const gdFixed f_w1 = gd_mulfx(f_1-f_f, f_1-f_g); + const gdFixed f_w2 = gd_mulfx(f_1-f_f, f_g); + const gdFixed f_w3 = gd_mulfx(f_f, f_1-f_g); + const gdFixed f_w4 = gd_mulfx(f_f, f_g); + + if (n < src_w - 1) { + src_offset_x = m + 1; + src_offset_y = n; + } + + if (m < src_h-1) { + src_offset_x = m; + src_offset_y = n + 1; + } + + if (!((n >= src_w-1) || (m >= src_h-1))) { + src_offset_x = m + 1; + src_offset_y = n + 1; + } + { + const int pixel1 = src->tpixels[src_offset_y][src_offset_x]; + register int pixel2, pixel3, pixel4; + + if (src_offset_y + 1 >= src_h) { + pixel2 = bgColor; + pixel3 = bgColor; + pixel4 = bgColor; + } else if (src_offset_x + 1 >= src_w) { + pixel2 = bgColor; + pixel3 = bgColor; + pixel4 = bgColor; + } else { + pixel2 = src->tpixels[src_offset_y][src_offset_x + 1]; + pixel3 = src->tpixels[src_offset_y + 1][src_offset_x]; + pixel4 = src->tpixels[src_offset_y + 1][src_offset_x + 1]; + } + { + const gdFixed f_r1 = gd_itofx(gdTrueColorGetRed(pixel1)); + const gdFixed f_r2 = gd_itofx(gdTrueColorGetRed(pixel2)); + const gdFixed f_r3 = gd_itofx(gdTrueColorGetRed(pixel3)); + const gdFixed f_r4 = gd_itofx(gdTrueColorGetRed(pixel4)); + const gdFixed f_g1 = gd_itofx(gdTrueColorGetGreen(pixel1)); + const gdFixed f_g2 = gd_itofx(gdTrueColorGetGreen(pixel2)); + const gdFixed f_g3 = gd_itofx(gdTrueColorGetGreen(pixel3)); + const gdFixed f_g4 = gd_itofx(gdTrueColorGetGreen(pixel4)); + const gdFixed f_b1 = gd_itofx(gdTrueColorGetBlue(pixel1)); + const gdFixed f_b2 = gd_itofx(gdTrueColorGetBlue(pixel2)); + const gdFixed f_b3 = gd_itofx(gdTrueColorGetBlue(pixel3)); + const gdFixed f_b4 = gd_itofx(gdTrueColorGetBlue(pixel4)); + const gdFixed f_a1 = gd_itofx(gdTrueColorGetAlpha(pixel1)); + const gdFixed f_a2 = gd_itofx(gdTrueColorGetAlpha(pixel2)); + const gdFixed f_a3 = gd_itofx(gdTrueColorGetAlpha(pixel3)); + const gdFixed f_a4 = gd_itofx(gdTrueColorGetAlpha(pixel4)); + const gdFixed f_red = gd_mulfx(f_w1, f_r1) + gd_mulfx(f_w2, f_r2) + gd_mulfx(f_w3, f_r3) + gd_mulfx(f_w4, f_r4); + const gdFixed f_green = gd_mulfx(f_w1, f_g1) + gd_mulfx(f_w2, f_g2) + gd_mulfx(f_w3, f_g3) + gd_mulfx(f_w4, f_g4); + const gdFixed f_blue = gd_mulfx(f_w1, f_b1) + gd_mulfx(f_w2, f_b2) + gd_mulfx(f_w3, f_b3) + gd_mulfx(f_w4, f_b4); + const gdFixed f_alpha = gd_mulfx(f_w1, f_a1) + gd_mulfx(f_w2, f_a2) + gd_mulfx(f_w3, f_a3) + gd_mulfx(f_w4, f_a4); + + const unsigned char red = (unsigned char) CLAMP(gd_fxtoi(f_red), 0, 255); + const unsigned char green = (unsigned char) CLAMP(gd_fxtoi(f_green), 0, 255); + const unsigned char blue = (unsigned char) CLAMP(gd_fxtoi(f_blue), 0, 255); + const unsigned char alpha = (unsigned char) CLAMP(gd_fxtoi(f_alpha), 0, 127); + + dst->tpixels[dst_offset_y][dst_offset_x++] = gdTrueColorAlpha(red, green, blue, alpha); + } + } + } else { + dst->tpixels[dst_offset_y][dst_offset_x++] = bgColor; + } + } + dst_offset_y++; + } + return dst; +} + +gdImagePtr gdImageRotateBicubicFixed(gdImagePtr src, const float degrees, const int bgColor) +{ + const float _angle = (float)((- degrees / 180.0f) * M_PI); + const int src_w = gdImageSX(src); + const int src_h = gdImageSY(src); + const unsigned int new_width = abs((int)(src_w*cos(_angle))) + abs((int)(src_h*sin(_angle) + 0.5f)); + const unsigned int new_height = abs((int)(src_w*sin(_angle))) + abs((int)(src_h*cos(_angle) + 0.5f)); + const gdFixed f_0_5 = gd_ftofx(0.5f); + const gdFixed f_H = gd_itofx(src_h/2); + const gdFixed f_W = gd_itofx(src_w/2); + const gdFixed f_cos = gd_ftofx(cos(-_angle)); + const gdFixed f_sin = gd_ftofx(sin(-_angle)); + const gdFixed f_1 = gd_itofx(1); + const gdFixed f_2 = gd_itofx(2); + const gdFixed f_4 = gd_itofx(4); + const gdFixed f_6 = gd_itofx(6); + const gdFixed f_gama = gd_ftofx(1.04f); + + unsigned int dst_offset_x; + unsigned int dst_offset_y = 0; + unsigned int i; + gdImagePtr dst; + + /* impact perf a bit, but not that much. Implementation for palette + images can be done at a later point. + */ + if (src->trueColor == 0) { + gdImagePaletteToTrueColor(src); + } + + dst = gdImageCreateTrueColor(new_width, new_height); + + if (dst == NULL) { + return NULL; + } + dst->saveAlphaFlag = 1; + + for (i=0; i < new_height; i++) { + unsigned int j; + dst_offset_x = 0; + + for (j=0; j < new_width; j++) { + const gdFixed f_i = gd_itofx(i-new_height/2); + const gdFixed f_j = gd_itofx(j-new_width/2); + const gdFixed f_m = gd_mulfx(f_j,f_sin) + gd_mulfx(f_i,f_cos) + f_0_5 + f_H; + const gdFixed f_n = gd_mulfx(f_j,f_cos) - gd_mulfx(f_i,f_sin) + f_0_5 + f_W; + const int m = gd_fxtoi(f_m); + const int n = gd_fxtoi(f_n); + + if ((m > 0) && (m < src_h - 1) && (n > 0) && (n < src_w-1)) { + const gdFixed f_f = f_m - gd_itofx(m); + const gdFixed f_g = f_n - gd_itofx(n); + unsigned int src_offset_x[16], src_offset_y[16]; + unsigned char red, green, blue, alpha; + gdFixed f_red=0, f_green=0, f_blue=0, f_alpha=0; + int k; + + if ((m < 1) || (n < 1)) { + src_offset_x[0] = n; + src_offset_y[0] = m; + } else { + src_offset_x[0] = n - 1; + src_offset_y[0] = m; + } + + if (m < 1) { + src_offset_x[1] = n; + src_offset_y[1] = m; + } else { + src_offset_x[1] = n; + src_offset_y[1] = m ; + } + + if ((m < 1) || (n >= src_w-1)) { + src_offset_x[2] = - 1; + src_offset_y[2] = - 1; + } else { + src_offset_x[2] = n + 1; + src_offset_y[2] = m ; + } + + if ((m < 1) || (n >= src_w-2)) { + src_offset_x[3] = - 1; + src_offset_y[3] = - 1; + } else { + src_offset_x[3] = n + 1 + 1; + src_offset_y[3] = m ; + } + + if (n < 1) { + src_offset_x[4] = - 1; + src_offset_y[4] = - 1; + } else { + src_offset_x[4] = n - 1; + src_offset_y[4] = m; + } + + src_offset_x[5] = n; + src_offset_y[5] = m; + if (n >= src_w-1) { + src_offset_x[6] = - 1; + src_offset_y[6] = - 1; + } else { + src_offset_x[6] = n + 1; + src_offset_y[6] = m; + } + + if (n >= src_w-2) { + src_offset_x[7] = - 1; + src_offset_y[7] = - 1; + } else { + src_offset_x[7] = n + 1 + 1; + src_offset_y[7] = m; + } + + if ((m >= src_h-1) || (n < 1)) { + src_offset_x[8] = - 1; + src_offset_y[8] = - 1; + } else { + src_offset_x[8] = n - 1; + src_offset_y[8] = m; + } + + if (m >= src_h-1) { + src_offset_x[8] = - 1; + src_offset_y[8] = - 1; + } else { + src_offset_x[9] = n; + src_offset_y[9] = m; + } + + if ((m >= src_h-1) || (n >= src_w-1)) { + src_offset_x[10] = - 1; + src_offset_y[10] = - 1; + } else { + src_offset_x[10] = n + 1; + src_offset_y[10] = m; + } + + if ((m >= src_h-1) || (n >= src_w-2)) { + src_offset_x[11] = - 1; + src_offset_y[11] = - 1; + } else { + src_offset_x[11] = n + 1 + 1; + src_offset_y[11] = m; + } + + if ((m >= src_h-2) || (n < 1)) { + src_offset_x[12] = - 1; + src_offset_y[12] = - 1; + } else { + src_offset_x[12] = n - 1; + src_offset_y[12] = m; + } + + if (m >= src_h-2) { + src_offset_x[13] = - 1; + src_offset_y[13] = - 1; + } else { + src_offset_x[13] = n; + src_offset_y[13] = m; + } + + if ((m >= src_h-2) || (n >= src_w - 1)) { + src_offset_x[14] = - 1; + src_offset_y[14] = - 1; + } else { + src_offset_x[14] = n + 1; + src_offset_y[14] = m; + } + + if ((m >= src_h-2) || (n >= src_w-2)) { + src_offset_x[15] = - 1; + src_offset_y[15] = - 1; + } else { + src_offset_x[15] = n + 1 + 1; + src_offset_y[15] = m; + } + + for (k=-1; k<3; k++) { + const gdFixed f = gd_itofx(k)-f_f; + const gdFixed f_fm1 = f - f_1; + const gdFixed f_fp1 = f + f_1; + const gdFixed f_fp2 = f + f_2; + gdFixed f_a = 0, f_b = 0,f_c = 0, f_d = 0; + gdFixed f_RY; + int l; + + if (f_fp2 > 0) { + f_a = gd_mulfx(f_fp2,gd_mulfx(f_fp2,f_fp2)); + } + + if (f_fp1 > 0) { + f_b = gd_mulfx(f_fp1,gd_mulfx(f_fp1,f_fp1)); + } + + if (f > 0) { + f_c = gd_mulfx(f,gd_mulfx(f,f)); + } + + if (f_fm1 > 0) { + f_d = gd_mulfx(f_fm1,gd_mulfx(f_fm1,f_fm1)); + } + f_RY = gd_divfx((f_a-gd_mulfx(f_4,f_b)+gd_mulfx(f_6,f_c)-gd_mulfx(f_4,f_d)),f_6); + + for (l=-1; l< 3; l++) { + const gdFixed f = gd_itofx(l) - f_g; + const gdFixed f_fm1 = f - f_1; + const gdFixed f_fp1 = f + f_1; + const gdFixed f_fp2 = f + f_2; + gdFixed f_a = 0, f_b = 0, f_c = 0, f_d = 0; + gdFixed f_RX, f_R; + const int _k = ((k + 1) * 4) + (l + 1); + register gdFixed f_rs, f_gs, f_bs, f_as; + register int c; + + if (f_fp2 > 0) { + f_a = gd_mulfx(f_fp2,gd_mulfx(f_fp2,f_fp2)); + } + + if (f_fp1 > 0) { + f_b = gd_mulfx(f_fp1,gd_mulfx(f_fp1,f_fp1)); + } + + if (f > 0) { + f_c = gd_mulfx(f,gd_mulfx(f,f)); + } + + if (f_fm1 > 0) { + f_d = gd_mulfx(f_fm1,gd_mulfx(f_fm1,f_fm1)); + } + + f_RX = gd_divfx((f_a - gd_mulfx(f_4, f_b) + gd_mulfx(f_6, f_c) - gd_mulfx(f_4, f_d)), f_6); + f_R = gd_mulfx(f_RY, f_RX); + + if ((src_offset_x[_k] <= 0) || (src_offset_y[_k] <= 0) || (src_offset_y[_k] >= src_h) || (src_offset_x[_k] >= src_w)) { + c = bgColor; + } else if ((src_offset_x[_k] <= 1) || (src_offset_y[_k] <= 1) || (src_offset_y[_k] >= (int)src_h - 1) || (src_offset_x[_k] >= (int)src_w - 1)) { + gdFixed f_127 = gd_itofx(127); + c = src->tpixels[src_offset_y[_k]][src_offset_x[_k]]; + c = c | (( (int) (gd_fxtof(gd_mulfx(f_R, f_127)) + 50.5f)) << 24); + c = _color_blend(bgColor, c); + } else { + c = src->tpixels[src_offset_y[_k]][src_offset_x[_k]]; + } + + f_rs = gd_itofx(gdTrueColorGetRed(c)); + f_gs = gd_itofx(gdTrueColorGetGreen(c)); + f_bs = gd_itofx(gdTrueColorGetBlue(c)); + f_as = gd_itofx(gdTrueColorGetAlpha(c)); + + f_red += gd_mulfx(f_rs, f_R); + f_green += gd_mulfx(f_gs, f_R); + f_blue += gd_mulfx(f_bs, f_R); + f_alpha += gd_mulfx(f_as, f_R); + } + } + + red = (unsigned char) CLAMP(gd_fxtoi(gd_mulfx(f_red, f_gama)), 0, 255); + green = (unsigned char) CLAMP(gd_fxtoi(gd_mulfx(f_green, f_gama)), 0, 255); + blue = (unsigned char) CLAMP(gd_fxtoi(gd_mulfx(f_blue, f_gama)), 0, 255); + alpha = (unsigned char) CLAMP(gd_fxtoi(gd_mulfx(f_alpha, f_gama)), 0, 127); + + dst->tpixels[dst_offset_y][dst_offset_x] = gdTrueColorAlpha(red, green, blue, alpha); + } else { + dst->tpixels[dst_offset_y][dst_offset_x] = bgColor; + } + dst_offset_x++; + } + + dst_offset_y++; + } + return dst; +} + +gdImagePtr gdImageRotateInterpolated(const gdImagePtr src, const float angle, int bgcolor) +{ + const int angle_rounded = (int)floor(angle * 100); + + if (bgcolor < 0) { + return NULL; + } + + /* no interpolation needed here */ + switch (angle_rounded) { + case 9000: + return gdImageRotate90(src, 0); + case 18000: + return gdImageRotate180(src, 0); + case 27000: + return gdImageRotate270(src, 0); + } + + if (src == NULL || src->interpolation_id < 1 || src->interpolation_id > GD_METHOD_COUNT) { + return NULL; + } + + switch (src->interpolation_id) { + case GD_NEAREST_NEIGHBOUR: + return gdImageRotateNearestNeighbour(src, angle, bgcolor); + break; + + case GD_BILINEAR_FIXED: + return gdImageRotateBilinear(src, angle, bgcolor); + break; + + case GD_BICUBIC_FIXED: + return gdImageRotateBicubicFixed(src, angle, bgcolor); + break; + + default: + return gdImageRotateGeneric(src, angle, bgcolor); + } + return NULL; +} + +/** + * Title: Affine transformation + **/ + +/** + * Group: Transform + **/ + + static void gdImageClipRectangle(gdImagePtr im, gdRectPtr r) +{ + int c1x, c1y, c2x, c2y; + int x1,y1; + + gdImageGetClip(im, &c1x, &c1y, &c2x, &c2y); + x1 = r->x + r->width - 1; + y1 = r->y + r->height - 1; + r->x = CLAMP(r->x, c1x, c2x); + r->y = CLAMP(r->y, c1y, c2y); + r->width = CLAMP(x1, c1x, c2x) - r->x + 1; + r->height = CLAMP(y1, c1y, c2y) - r->y + 1; +} + +void gdDumpRect(const char *msg, gdRectPtr r) +{ + printf("%s (%i, %i) (%i, %i)\n", msg, r->x, r->y, r->width, r->height); +} + +/** + * Function: gdTransformAffineGetImage + * Applies an affine transformation to a region and return an image + * containing the complete transformation. + * + * Parameters: + * dst - Pointer to a gdImagePtr to store the created image, NULL when + * the creation or the transformation failed + * src - Source image + * src_area - rectangle defining the source region to transform + * dstY - Y position in the destination image + * affine - The desired affine transformation + * + * Returns: + * GD_TRUE if the affine is rectilinear or GD_FALSE + */ +int gdTransformAffineGetImage(gdImagePtr *dst, + const gdImagePtr src, + gdRectPtr src_area, + const double affine[6]) +{ + int res; + double m[6]; + gdRect bbox; + gdRect area_full; + + if (src_area == NULL) { + area_full.x = 0; + area_full.y = 0; + area_full.width = gdImageSX(src); + area_full.height = gdImageSY(src); + src_area = &area_full; + } + + gdTransformAffineBoundingBox(src_area, affine, &bbox); + + *dst = gdImageCreateTrueColor(bbox.width, bbox.height); + if (*dst == NULL) { + return GD_FALSE; + } + (*dst)->saveAlphaFlag = 1; + + if (!src->trueColor) { + gdImagePaletteToTrueColor(src); + } + + /* Translate to dst origin (0,0) */ + gdAffineTranslate(m, -bbox.x, -bbox.y); + gdAffineConcat(m, affine, m); + + gdImageAlphaBlending(*dst, 0); + + res = gdTransformAffineCopy(*dst, + 0,0, + src, + src_area, + m); + + if (res != GD_TRUE) { + gdImageDestroy(*dst); + dst = NULL; + return GD_FALSE; + } else { + return GD_TRUE; + } +} + +/** + * Function: gdTransformAffineCopy + * Applies an affine transformation to a region and copy the result + * in a destination to the given position. + * + * Parameters: + * dst - Image to draw the transformed image + * src - Source image + * dstX - X position in the destination image + * dstY - Y position in the destination image + * src_area - Rectangular region to rotate in the src image + * + * Returns: + * GD_TRUE if the affine is rectilinear or GD_FALSE + */ +int gdTransformAffineCopy(gdImagePtr dst, + int dst_x, int dst_y, + const gdImagePtr src, + gdRectPtr src_region, + const double affine[6]) +{ + int c1x,c1y,c2x,c2y; + int backclip = 0; + int backup_clipx1, backup_clipy1, backup_clipx2, backup_clipy2; + register int x, y, src_offset_x, src_offset_y; + double inv[6]; + int *dst_p; + gdPointF pt, src_pt; + gdRect bbox; + int end_x, end_y; + gdInterpolationMethod interpolotion_id_bak; + interpolation_method interpolation_bak; + + /* These methods use special implementations */ + if (src->interpolation_id == GD_BILINEAR_FIXED || src->interpolation_id == GD_BICUBIC_FIXED || src->interpolation_id == GD_NEAREST_NEIGHBOUR) { + interpolotion_id_bak = src->interpolation_id; + interpolation_bak = src->interpolation; + + gdImageSetInterpolationMethod(src, GD_BICUBIC); + } + + + gdImageClipRectangle(src, src_region); + + if (src_region->x > 0 || src_region->y > 0 + || src_region->width < gdImageSX(src) + || src_region->height < gdImageSY(src)) { + backclip = 1; + + gdImageGetClip(src, &backup_clipx1, &backup_clipy1, + &backup_clipx2, &backup_clipy2); + + gdImageSetClip(src, src_region->x, src_region->y, + src_region->x + src_region->width - 1, + src_region->y + src_region->height - 1); + } + + if (!gdTransformAffineBoundingBox(src_region, affine, &bbox)) { + if (backclip) { + gdImageSetClip(src, backup_clipx1, backup_clipy1, + backup_clipx2, backup_clipy2); + } + gdImageSetInterpolationMethod(src, interpolotion_id_bak); + return GD_FALSE; + } + + gdImageGetClip(dst, &c1x, &c1y, &c2x, &c2y); + + end_x = bbox.width + (int) fabs(bbox.x); + end_y = bbox.height + (int) fabs(bbox.y); + + /* Get inverse affine to let us work with destination -> source */ + gdAffineInvert(inv, affine); + + src_offset_x = src_region->x; + src_offset_y = src_region->y; + + if (dst->alphaBlendingFlag) { + for (y = bbox.y; y <= end_y; y++) { + pt.y = y + 0.5; + for (x = 0; x <= end_x; x++) { + pt.x = x + 0.5; + gdAffineApplyToPointF(&src_pt, &pt, inv); + gdImageSetPixel(dst, dst_x + x, dst_y + y, getPixelInterpolated(src, src_offset_x + src_pt.x, src_offset_y + src_pt.y, 0)); + } + } + } else { + for (y = 0; y <= end_y; y++) { + pt.y = y + 0.5 + bbox.y; + if ((dst_y + y) < 0 || ((dst_y + y) > gdImageSY(dst) -1)) { + continue; + } + dst_p = dst->tpixels[dst_y + y] + dst_x; + + for (x = 0; x <= end_x; x++) { + pt.x = x + 0.5 + bbox.x; + gdAffineApplyToPointF(&src_pt, &pt, inv); + + if ((dst_x + x) < 0 || (dst_x + x) > (gdImageSX(dst) - 1)) { + break; + } + *(dst_p++) = getPixelInterpolated(src, src_offset_x + src_pt.x, src_offset_y + src_pt.y, -1); + } + } + } + + /* Restore clip if required */ + if (backclip) { + gdImageSetClip(src, backup_clipx1, backup_clipy1, + backup_clipx2, backup_clipy2); + } + + gdImageSetInterpolationMethod(src, interpolotion_id_bak); + return GD_TRUE; +} + +/** + * Function: gdTransformAffineBoundingBox + * Returns the bounding box of an affine transformation applied to a + * rectangular area + * + * Parameters: + * src - Rectangular source area for the affine transformation + * affine - the affine transformation + * bbox - the resulting bounding box + * + * Returns: + * GD_TRUE if the affine is rectilinear or GD_FALSE + */ +int gdTransformAffineBoundingBox(gdRectPtr src, const double affine[6], gdRectPtr bbox) +{ + gdPointF extent[4], min, max, point; + int i; + + extent[0].x=0.0; + extent[0].y=0.0; + extent[1].x=(double) src->width; + extent[1].y=0.0; + extent[2].x=(double) src->width; + extent[2].y=(double) src->height; + extent[3].x=0.0; + extent[3].y=(double) src->height; + + for (i=0; i < 4; i++) { + point=extent[i]; + if (gdAffineApplyToPointF(&extent[i], &point, affine) != GD_TRUE) { + return GD_FALSE; + } + } + min=extent[0]; + max=extent[0]; + + for (i=1; i < 4; i++) { + if (min.x > extent[i].x) + min.x=extent[i].x; + if (min.y > extent[i].y) + min.y=extent[i].y; + if (max.x < extent[i].x) + max.x=extent[i].x; + if (max.y < extent[i].y) + max.y=extent[i].y; + } + bbox->x = (int) min.x; + bbox->y = (int) min.y; + bbox->width = (int) floor(max.x - min.x) - 1; + bbox->height = (int) floor(max.y - min.y); + return GD_TRUE; +} + +int gdImageSetInterpolationMethod(gdImagePtr im, gdInterpolationMethod id) +{ + if (im == NULL || id < 0 || id > GD_METHOD_COUNT) { + return 0; + } + + switch (id) { + case GD_DEFAULT: + im->interpolation_id = GD_BILINEAR_FIXED; + im->interpolation = NULL; + break; + + /* Optimized versions */ + case GD_BILINEAR_FIXED: + case GD_BICUBIC_FIXED: + case GD_NEAREST_NEIGHBOUR: + case GD_WEIGHTED4: + im->interpolation = NULL; + break; + + /* generic versions*/ + case GD_BELL: + im->interpolation = filter_bell; + break; + case GD_BESSEL: + im->interpolation = filter_bessel; + break; + case GD_BICUBIC: + im->interpolation = filter_bicubic; + break; + case GD_BLACKMAN: + im->interpolation = filter_blackman; + break; + case GD_BOX: + im->interpolation = filter_box; + break; + case GD_BSPLINE: + im->interpolation = filter_bspline; + break; + case GD_CATMULLROM: + im->interpolation = filter_catmullrom; + break; + case GD_GAUSSIAN: + im->interpolation = filter_gaussian; + break; + case GD_GENERALIZED_CUBIC: + im->interpolation = filter_generalized_cubic; + break; + case GD_HERMITE: + im->interpolation = filter_hermite; + break; + case GD_HAMMING: + im->interpolation = filter_hamming; + break; + case GD_HANNING: + im->interpolation = filter_hanning; + break; + case GD_MITCHELL: + im->interpolation = filter_mitchell; + break; + case GD_POWER: + im->interpolation = filter_power; + break; + case GD_QUADRATIC: + im->interpolation = filter_quadratic; + break; + case GD_SINC: + im->interpolation = filter_sinc; + break; + case GD_TRIANGLE: + im->interpolation = filter_triangle; + break; + + default: + return 0; + break; + } + im->interpolation_id = id; + return 1; +} + +#ifdef _MSC_VER +# pragma optimize("", on) +#endif diff --git a/src/gd_matrix.c b/src/gd_matrix.c new file mode 100644 index 000000000..83438bdbe --- /dev/null +++ b/src/gd_matrix.c @@ -0,0 +1,334 @@ +#include "gd.h" +#include + +#ifndef M_PI +# define M_PI 3.14159265358979323846 +#endif + +/** + * Title: Matrix + * Group: Affine Matrix + */ + +/** + * Function: gdAffineApplyToPointF + * Applies an affine transformation to a point (floating point + * gdPointF) + * + * + * Parameters: + * dst - Where to store the resulting point + * affine - Source Point + * flip_horz - affine matrix + * + * Returns: + * GD_TRUE if the affine is rectilinear or GD_FALSE + */ +int gdAffineApplyToPointF (gdPointFPtr dst, const gdPointFPtr src, + const double affine[6]) +{ + double x = src->x; + double y = src->y; + x = src->x; + y = src->y; + dst->x = x * affine[0] + y * affine[2] + affine[4]; + dst->y = x * affine[1] + y * affine[3] + affine[5]; + return GD_TRUE; +} + +/** + * Function: gdAffineInvert + * Find the inverse of an affine transformation. + * + * All non-degenerate affine transforms are invertible. Applying the + * inverted matrix will restore the original values. Multiplying + * by (commutative) will return the identity affine (rounding + * error possible). + * + * Parameters: + * dst - Where to store the resulting affine transform + * src_affine - Original affine matrix + * flip_horz - Whether or not to flip horizontally + * flip_vert - Whether or not to flip vertically + * + * See also: + * + * + * Returns: + * GD_TRUE if the affine is rectilinear or GD_FALSE + */ +int gdAffineInvert (double dst[6], const double src[6]) +{ + double r_det = (src[0] * src[3] - src[1] * src[2]); + + if (r_det <= 0.0) { + return GD_FALSE; + } + + r_det = 1.0 / r_det; + dst[0] = src[3] * r_det; + dst[1] = -src[1] * r_det; + dst[2] = -src[2] * r_det; + dst[3] = src[0] * r_det; + dst[4] = -src[4] * dst[0] - src[5] * dst[2]; + dst[5] = -src[4] * dst[1] - src[5] * dst[3]; + return GD_TRUE; +} + +/** + * Function: gdAffineFlip + * Flip an affine transformation horizontally or vertically. + * + * Flips the affine transform, giving GD_FALSE for and + * will clone the affine matrix. GD_TRUE for both will + * copy a 180° rotation. + * + * Parameters: + * dst - Where to store the resulting affine transform + * src_affine - Original affine matrix + * flip_h - Whether or not to flip horizontally + * flip_v - Whether or not to flip vertically + * + * Returns: + * GD_SUCCESS on success or GD_FAILURE + */ +int gdAffineFlip (double dst[6], const double src[6], const int flip_h, const int flip_v) +{ + dst[0] = flip_h ? - src[0] : src[0]; + dst[1] = flip_h ? - src[1] : src[1]; + dst[2] = flip_v ? - src[2] : src[2]; + dst[3] = flip_v ? - src[3] : src[3]; + dst[4] = flip_h ? - src[4] : src[4]; + dst[5] = flip_v ? - src[5] : src[5]; + return GD_TRUE; +} + +/** + * Function: gdAffineConcat + * Concat (Multiply) two affine transformation matrices. + * + * Concats two affine transforms together, i.e. the result + * will be the equivalent of doing first the transformation m1 and then + * m2. All parameters can be the same matrix (safe to call using + * the same array for all three arguments). + * + * Parameters: + * dst - Where to store the resulting affine transform + * m1 - First affine matrix + * m2 - Second affine matrix + * + * Returns: + * GD_SUCCESS on success or GD_FAILURE + */ +int gdAffineConcat (double dst[6], const double m1[6], const double m2[6]) +{ + double dst0, dst1, dst2, dst3, dst4, dst5; + + dst0 = m1[0] * m2[0] + m1[1] * m2[2]; + dst1 = m1[0] * m2[1] + m1[1] * m2[3]; + dst2 = m1[2] * m2[0] + m1[3] * m2[2]; + dst3 = m1[2] * m2[1] + m1[3] * m2[3]; + dst4 = m1[4] * m2[0] + m1[5] * m2[2] + m2[4]; + dst5 = m1[4] * m2[1] + m1[5] * m2[3] + m2[5]; + dst[0] = dst0; + dst[1] = dst1; + dst[2] = dst2; + dst[3] = dst3; + dst[4] = dst4; + dst[5] = dst5; + return GD_TRUE; +} + +/** + * Function: gdAffineIdentity + * Set up the identity matrix. + * + * Parameters: + * dst - Where to store the resulting affine transform + * + * Returns: + * GD_SUCCESS on success or GD_FAILURE + */ +int gdAffineIdentity (double dst[6]) +{ + dst[0] = 1; + dst[1] = 0; + dst[2] = 0; + dst[3] = 1; + dst[4] = 0; + dst[5] = 0; + return GD_TRUE; +} + +/** + * Function: gdAffineScale + * Set up a scaling matrix. + * + * Parameters: + * scale_x - X scale factor + * scale_y - Y scale factor + * + * Returns: + * GD_SUCCESS on success or GD_FAILURE + */ +int gdAffineScale (double dst[6], const double scale_x, const double scale_y) +{ + dst[0] = scale_x; + dst[1] = 0; + dst[2] = 0; + dst[3] = scale_y; + dst[4] = 0; + dst[5] = 0; + return GD_TRUE; +} + +/** + * Function: gdAffineRotate + * Set up a rotation affine transform. + * + * Like the other angle in libGD, in which increasing y moves + * downward, this is a counterclockwise rotation. + * + * Parameters: + * dst - Where to store the resulting affine transform + * angle - Rotation angle in degrees + * + * Returns: + * GD_SUCCESS on success or GD_FAILURE + */ +int gdAffineRotate (double dst[6], const double angle) +{ + const double sin_t = sin (angle * M_PI / 180.0); + const double cos_t = cos (angle * M_PI / 180.0); + + dst[0] = cos_t; + dst[1] = sin_t; + dst[2] = -sin_t; + dst[3] = cos_t; + dst[4] = 0; + dst[5] = 0; + return GD_TRUE; +} + +/** + * Function: gdAffineShearHorizontal + * Set up a horizontal shearing matrix || becomes \\. + * + * Parameters: + * dst - Where to store the resulting affine transform + * angle - Shear angle in degrees + * + * Returns: + * GD_SUCCESS on success or GD_FAILURE + */ +int gdAffineShearHorizontal(double dst[6], const double angle) +{ + dst[0] = 1; + dst[1] = 0; + dst[2] = tan(angle * M_PI / 180.0); + dst[3] = 1; + dst[4] = 0; + dst[5] = 0; + return GD_TRUE; +} + +/** + * Function: gdAffineShearVertical + * Set up a vertical shearing matrix, columns are untouched. + * + * Parameters: + * dst - Where to store the resulting affine transform + * angle - Shear angle in degrees + * + * Returns: + * GD_SUCCESS on success or GD_FAILURE + */ +int gdAffineShearVertical(double dst[6], const double angle) +{ + dst[0] = 1; + dst[1] = tan(angle * M_PI / 180.0);; + dst[2] = 0; + dst[3] = 1; + dst[4] = 0; + dst[5] = 0; + return GD_TRUE; +} + +/** + * Function: gdAffineTranslate + * Set up a translation matrix. + * + * Parameters: + * dst - Where to store the resulting affine transform + * offset_x - Horizontal translation amount + * offset_y - Vertical translation amount + * + * Returns: + * GD_SUCCESS on success or GD_FAILURE + */ +int gdAffineTranslate (double dst[6], const double offset_x, const double offset_y) +{ + dst[0] = 1; + dst[1] = 0; + dst[2] = 0; + dst[3] = 1; + dst[4] = offset_x; + dst[5] = offset_y; + return GD_TRUE; +} + +/** + * gdAffineexpansion: Find the affine's expansion factor. + * @src: The affine transformation. + * + * Finds the expansion factor, i.e. the square root of the factor + * by which the affine transform affects area. In an affine transform + * composed of scaling, rotation, shearing, and translation, returns + * the amount of scaling. + * + * GD_SUCCESS on success or GD_FAILURE + **/ +double gdAffineExpansion (const double src[6]) +{ + return sqrt (fabs (src[0] * src[3] - src[1] * src[2])); +} + +/** + * Function: gdAffineRectilinear + * Determines whether the affine transformation is axis aligned. A + * tolerance has been implemented using GD_EPSILON. + * + * Parameters: + * m - The affine transformation + * + * Returns: + * GD_TRUE if the affine is rectilinear or GD_FALSE + */ +int gdAffineRectilinear (const double m[6]) +{ + return ((fabs (m[1]) < GD_EPSILON && fabs (m[2]) < GD_EPSILON) || + (fabs (m[0]) < GD_EPSILON && fabs (m[3]) < GD_EPSILON)); +} + +/** + * Function: gdAffineEqual + * Determines whether two affine transformations are equal. A tolerance + * has been implemented using GD_EPSILON. + * + * Parameters: + * m1 - The first affine transformation + * m2 - The first affine transformation + * + * Returns: + * GD_SUCCESS on success or GD_FAILURE + */ +int gdAffineEqual (const double m1[6], const double m2[6]) +{ + return (fabs (m1[0] - m2[0]) < GD_EPSILON && + fabs (m1[1] - m2[1]) < GD_EPSILON && + fabs (m1[2] - m2[2]) < GD_EPSILON && + fabs (m1[3] - m2[3]) < GD_EPSILON && + fabs (m1[4] - m2[4]) < GD_EPSILON && + fabs (m1[5] - m2[5]) < GD_EPSILON); +} + diff --git a/src/gd_rotate.c b/src/gd_rotate.c new file mode 100644 index 000000000..fd6bc8ec7 --- /dev/null +++ b/src/gd_rotate.c @@ -0,0 +1,565 @@ +#if HAVE_GD_BUNDLED +# include "gd.h" +#else +# include +#endif + +#include "gd_intern.h" +#include + +/* + * Rotate function Added on 2003/12 + * by Pierre-Alain Joye (pierre@php.net) + **/ +/* Begin rotate function */ +#ifdef ROTATE_PI +#undef ROTATE_PI +#endif /* ROTATE_PI */ + +#ifdef NONDLL +#define BGD_DECLARE(rt) extern rt +#else +#ifdef BGDWIN32 +typedef int (__stdcall *FuncPtr)(gdImagePtr, int, int); +/* +#define BGD_DECLARE(rt) __declspec(dllexport) rt __stdcall +*/ +#else +typedef int (__stdcall *FuncPtr)(gdImagePtr, int, int); +/* #define BGD_DECLARE(rt) __declspec(dllimport) rt _stdcall */ +#endif /* BGDWIN32 */ +#endif /* NONDLL */ + +#define ROTATE_DEG2RAD 3.1415926535897932384626433832795/180 +void gdImageSkewX (gdImagePtr dst, gdImagePtr src, int uRow, int iOffset, double dWeight, int clrBack, int ignoretransparent) +{ + int i, r, g, b, a, clrBackR, clrBackG, clrBackB, clrBackA; + FuncPtr f; + + int pxlOldLeft, pxlLeft=0, pxlSrc; + + /* Keep clrBack as color index if required */ + if (src->trueColor) { + pxlOldLeft = clrBack; + f = gdImageGetTrueColorPixel; + } else { + pxlOldLeft = clrBack; + clrBackR = gdImageRed(src, clrBack); + clrBackG = gdImageGreen(src, clrBack); + clrBackB = gdImageBlue(src, clrBack); + clrBackA = gdImageAlpha(src, clrBack); + clrBack = gdTrueColorAlpha(clrBackR, clrBackG, clrBackB, clrBackA); + f = gdImageGetPixel; + } + + for (i = 0; i < iOffset; i++) { + gdImageSetPixel (dst, i, uRow, clrBack); + } + + if (i < dst->sx) { + gdImageSetPixel (dst, i, uRow, clrBack); + } + + for (i = 0; i < src->sx; i++) { + pxlSrc = f (src,i,uRow); + + r = (int)(gdImageRed(src,pxlSrc) * dWeight); + g = (int)(gdImageGreen(src,pxlSrc) * dWeight); + b = (int)(gdImageBlue(src,pxlSrc) * dWeight); + a = (int)(gdImageAlpha(src,pxlSrc) * dWeight); + + pxlLeft = gdImageColorAllocateAlpha(src, r, g, b, a); + + if (pxlLeft == -1) { + pxlLeft = gdImageColorClosestAlpha(src, r, g, b, a); + } + + r = gdImageRed(src,pxlSrc) - (gdImageRed(src,pxlLeft) - gdImageRed(src,pxlOldLeft)); + g = gdImageGreen(src,pxlSrc) - (gdImageGreen(src,pxlLeft) - gdImageGreen(src,pxlOldLeft)); + b = gdImageBlue(src,pxlSrc) - (gdImageBlue(src,pxlLeft) - gdImageBlue(src,pxlOldLeft)); + a = gdImageAlpha(src,pxlSrc) - (gdImageAlpha(src,pxlLeft) - gdImageAlpha(src,pxlOldLeft)); + + if (r>255) { + r = 255; + } + + if (g>255) { + g = 255; + } + + if (b>255) { + b = 255; + } + + if (a>127) { + a = 127; + } + + if (ignoretransparent && pxlSrc == dst->transparent) { + pxlSrc = dst->transparent; + } else { + pxlSrc = gdImageColorAllocateAlpha(dst, r, g, b, a); + + if (pxlSrc == -1) { + pxlSrc = gdImageColorClosestAlpha(dst, r, g, b, a); + } + } + + if ((i + iOffset >= 0) && (i + iOffset < dst->sx)) { + gdImageSetPixel (dst, i+iOffset, uRow, pxlSrc); + } + + pxlOldLeft = pxlLeft; + } + + i += iOffset; + + if (i < dst->sx) { + gdImageSetPixel (dst, i, uRow, pxlLeft); + } + + gdImageSetPixel (dst, iOffset, uRow, clrBack); + + i--; + + while (++i < dst->sx) { + gdImageSetPixel (dst, i, uRow, clrBack); + } +} + +void gdImageSkewY (gdImagePtr dst, gdImagePtr src, int uCol, int iOffset, double dWeight, int clrBack, int ignoretransparent) +{ + int i, iYPos=0, r, g, b, a; + FuncPtr f; + int pxlOldLeft, pxlLeft=0, pxlSrc; + + if (src->trueColor) { + f = gdImageGetTrueColorPixel; + } else { + f = gdImageGetPixel; + } + + for (i = 0; i<=iOffset; i++) { + gdImageSetPixel (dst, uCol, i, clrBack); + } + r = (int)((double)gdImageRed(src,clrBack) * dWeight); + g = (int)((double)gdImageGreen(src,clrBack) * dWeight); + b = (int)((double)gdImageBlue(src,clrBack) * dWeight); + a = (int)((double)gdImageAlpha(src,clrBack) * dWeight); + + pxlOldLeft = gdImageColorAllocateAlpha(dst, r, g, b, a); + + for (i = 0; i < src->sy; i++) { + pxlSrc = f (src, uCol, i); + iYPos = i + iOffset; + + r = (int)((double)gdImageRed(src,pxlSrc) * dWeight); + g = (int)((double)gdImageGreen(src,pxlSrc) * dWeight); + b = (int)((double)gdImageBlue(src,pxlSrc) * dWeight); + a = (int)((double)gdImageAlpha(src,pxlSrc) * dWeight); + + pxlLeft = gdImageColorAllocateAlpha(src, r, g, b, a); + + if (pxlLeft == -1) { + pxlLeft = gdImageColorClosestAlpha(src, r, g, b, a); + } + + r = gdImageRed(src,pxlSrc) - (gdImageRed(src,pxlLeft) - gdImageRed(src,pxlOldLeft)); + g = gdImageGreen(src,pxlSrc) - (gdImageGreen(src,pxlLeft) - gdImageGreen(src,pxlOldLeft)); + b = gdImageBlue(src,pxlSrc) - (gdImageBlue(src,pxlLeft) - gdImageBlue(src,pxlOldLeft)); + a = gdImageAlpha(src,pxlSrc) - (gdImageAlpha(src,pxlLeft) - gdImageAlpha(src,pxlOldLeft)); + + if (r>255) { + r = 255; + } + + if (g>255) { + g = 255; + } + + if (b>255) { + b = 255; + } + + if (a>127) { + a = 127; + } + + if (ignoretransparent && pxlSrc == dst->transparent) { + pxlSrc = dst->transparent; + } else { + pxlSrc = gdImageColorAllocateAlpha(dst, r, g, b, a); + + if (pxlSrc == -1) { + pxlSrc = gdImageColorClosestAlpha(dst, r, g, b, a); + } + } + + if ((iYPos >= 0) && (iYPos < dst->sy)) { + gdImageSetPixel (dst, uCol, iYPos, pxlSrc); + } + + pxlOldLeft = pxlLeft; + } + + i = iYPos; + if (i < dst->sy) { + gdImageSetPixel (dst, uCol, i, pxlLeft); + } + + i--; + while (++i < dst->sy) { + gdImageSetPixel (dst, uCol, i, clrBack); + } +} + +/* Rotates an image by 90 degrees (counter clockwise) */ +gdImagePtr gdImageRotate90 (gdImagePtr src, int ignoretransparent) +{ + int uY, uX; + int c,r,g,b,a; + gdImagePtr dst; + FuncPtr f; + + if (src->trueColor) { + f = gdImageGetTrueColorPixel; + } else { + f = gdImageGetPixel; + } + dst = gdImageCreateTrueColor(src->sy, src->sx); + dst->transparent = src->transparent; + + if (dst != NULL) { + int old_blendmode = dst->alphaBlendingFlag; + dst->alphaBlendingFlag = 0; + + gdImagePaletteCopy (dst, src); + + for (uY = 0; uYsy; uY++) { + for (uX = 0; uXsx; uX++) { + c = f (src, uX, uY); + if (!src->trueColor) { + r = gdImageRed(src,c); + g = gdImageGreen(src,c); + b = gdImageBlue(src,c); + a = gdImageAlpha(src,c); + c = gdTrueColorAlpha(r, g, b, a); + } + if (ignoretransparent && c == dst->transparent) { + gdImageSetPixel(dst, uY, (dst->sy - uX - 1), dst->transparent); + } else { + gdImageSetPixel(dst, uY, (dst->sy - uX - 1), c); + } + } + } + dst->alphaBlendingFlag = old_blendmode; + } + + return dst; +} + +/* Rotates an image by 180 degrees (counter clockwise) */ +gdImagePtr gdImageRotate180 (gdImagePtr src, int ignoretransparent) +{ + int uY, uX; + int c,r,g,b,a; + gdImagePtr dst; + FuncPtr f; + + if (src->trueColor) { + f = gdImageGetTrueColorPixel; + } else { + f = gdImageGetPixel; + } + dst = gdImageCreateTrueColor(src->sx, src->sy); + dst->transparent = src->transparent; + + if (dst != NULL) { + int old_blendmode = dst->alphaBlendingFlag; + dst->alphaBlendingFlag = 0; + + gdImagePaletteCopy (dst, src); + + for (uY = 0; uYsy; uY++) { + for (uX = 0; uXsx; uX++) { + c = f (src, uX, uY); + if (!src->trueColor) { + r = gdImageRed(src,c); + g = gdImageGreen(src,c); + b = gdImageBlue(src,c); + a = gdImageAlpha(src,c); + c = gdTrueColorAlpha(r, g, b, a); + } + + if (ignoretransparent && c == dst->transparent) { + gdImageSetPixel(dst, (dst->sx - uX - 1), (dst->sy - uY - 1), dst->transparent); + } else { + gdImageSetPixel(dst, (dst->sx - uX - 1), (dst->sy - uY - 1), c); + } + } + } + dst->alphaBlendingFlag = old_blendmode; + } + + return dst; +} + +/* Rotates an image by 270 degrees (counter clockwise) */ +gdImagePtr gdImageRotate270 (gdImagePtr src, int ignoretransparent) +{ + int uY, uX; + int c,r,g,b,a; + gdImagePtr dst; + FuncPtr f; + + if (src->trueColor) { + f = gdImageGetTrueColorPixel; + } else { + f = gdImageGetPixel; + } + dst = gdImageCreateTrueColor (src->sy, src->sx); + dst->transparent = src->transparent; + + if (dst != NULL) { + int old_blendmode = dst->alphaBlendingFlag; + dst->alphaBlendingFlag = 0; + + gdImagePaletteCopy (dst, src); + + for (uY = 0; uYsy; uY++) { + for (uX = 0; uXsx; uX++) { + c = f (src, uX, uY); + if (!src->trueColor) { + r = gdImageRed(src,c); + g = gdImageGreen(src,c); + b = gdImageBlue(src,c); + a = gdImageAlpha(src,c); + c = gdTrueColorAlpha(r, g, b, a); + } + + if (ignoretransparent && c == dst->transparent) { + gdImageSetPixel(dst, (dst->sx - uY - 1), uX, dst->transparent); + } else { + gdImageSetPixel(dst, (dst->sx - uY - 1), uX, c); + } + } + } + dst->alphaBlendingFlag = old_blendmode; + } + + return dst; +} + +gdImagePtr gdImageRotate45 (gdImagePtr src, double dAngle, int clrBack, int ignoretransparent) +{ + gdImagePtr dst1,dst2,dst3; + FuncPtr f; + double dRadAngle, dSinE, dTan, dShear; + double dOffset; /* Variable skew offset */ + int u, iShear, newx, newy; + int clrBackR, clrBackG, clrBackB, clrBackA; + + /* See GEMS I for the algorithm details */ + dRadAngle = dAngle * ROTATE_DEG2RAD; /* Angle in radians */ + dSinE = sin (dRadAngle); + dTan = tan (dRadAngle / 2.0); + + newx = (int)(src->sx + src->sy * fabs(dTan)); + newy = src->sy; + + /* 1st shear */ + if (src->trueColor) { + f = gdImageGetTrueColorPixel; + } else { + f = gdImageGetPixel; + } + + dst1 = gdImageCreateTrueColor(newx, newy); + /******* Perform 1st shear (horizontal) ******/ + if (dst1 == NULL) { + return NULL; + } +#ifdef HAVE_GD_BUNDLED + dst1->alphaBlendingFlag = gdEffectReplace; +#else + gdImageAlphaBlending(dst1, 0); +#endif + if (dAngle == 0.0) { + /* Returns copy of src */ + gdImageCopy (dst1, src,0,0,0,0,src->sx,src->sy); + return dst1; + } + + gdImagePaletteCopy (dst1, src); + + if (ignoretransparent) { + if (gdImageTrueColor(src)) { + dst1->transparent = src->transparent; + } else { + + dst1->transparent = gdTrueColorAlpha(gdImageRed(src, src->transparent), gdImageBlue(src, src->transparent), gdImageGreen(src, src->transparent), 127); + } + } + + dRadAngle = dAngle * ROTATE_DEG2RAD; /* Angle in radians */ + dSinE = sin (dRadAngle); + dTan = tan (dRadAngle / 2.0); + + for (u = 0; u < dst1->sy; u++) { + if (dTan >= 0.0) { + dShear = ((double)(u + 0.5)) * dTan; + } else { + dShear = ((double)(u - dst1->sy) + 0.5) * dTan; + } + + iShear = (int)floor(dShear); + gdImageSkewX(dst1, src, u, iShear, (dShear - iShear), clrBack, ignoretransparent); + } + + /* + The 1st shear may use the original clrBack as color index + Convert it once here + */ + if(!src->trueColor) { + clrBackR = gdImageRed(src, clrBack); + clrBackG = gdImageGreen(src, clrBack); + clrBackB = gdImageBlue(src, clrBack); + clrBackA = gdImageAlpha(src, clrBack); + clrBack = gdTrueColorAlpha(clrBackR, clrBackG, clrBackB, clrBackA); + } + /* 2nd shear */ + newx = dst1->sx; + + if (dSinE > 0.0) { + dOffset = (src->sx-1) * dSinE; + } else { + dOffset = -dSinE * (src->sx - newx); + } + + newy = (int) ((double) src->sx * fabs( dSinE ) + (double) src->sy * cos (dRadAngle))+1; + + if (src->trueColor) { + f = gdImageGetTrueColorPixel; + } else { + f = gdImageGetPixel; + } + dst2 = gdImageCreateTrueColor(newx, newy); + if (dst2 == NULL) { + gdImageDestroy(dst1); + return NULL; + } + +#ifdef HAVE_GD_BUNDLED + dst2->alphaBlendingFlag = gdEffectReplace; +#else + gdImageAlphaBlending(dst2, 0); +#endif + + if (ignoretransparent) { + dst2->transparent = dst1->transparent; + } + + for (u = 0; u < dst2->sx; u++, dOffset -= dSinE) { + iShear = (int)floor (dOffset); + gdImageSkewY(dst2, dst1, u, iShear, (dOffset - (double)iShear), clrBack, ignoretransparent); + } + + /* 3rd shear */ + gdImageDestroy(dst1); + + newx = (int) ((double)src->sy * fabs (dSinE) + (double)src->sx * cos (dRadAngle)) + 1; + newy = dst2->sy; + + if (src->trueColor) { + f = gdImageGetTrueColorPixel; + } else { + f = gdImageGetPixel; + } + dst3 = gdImageCreateTrueColor(newx, newy); + if (dst3 == NULL) { + gdImageDestroy(dst2); + return NULL; + } + +#ifdef HAVE_GD_BUNDLED + dst3->alphaBlendingFlag = gdEffectReplace; +#else + gdImageAlphaBlending(dst3, 0); +#endif + + if (ignoretransparent) { + dst3->transparent = dst2->transparent; + } + + if (dSinE >= 0.0) { + dOffset = (double)(src->sx - 1) * dSinE * -dTan; + } else { + dOffset = dTan * ((double)(src->sx - 1) * -dSinE + (double)(1 - newy)); + } + + for (u = 0; u < dst3->sy; u++, dOffset += dTan) { + int iShear = (int)floor(dOffset); + gdImageSkewX(dst3, dst2, u, iShear, (dOffset - iShear), clrBack, ignoretransparent); + } + + gdImageDestroy(dst2); + + return dst3; +} + +gdImagePtr gdImageRotate (gdImagePtr src, double dAngle, int clrBack, int ignoretransparent) +{ + gdImagePtr pMidImg; + gdImagePtr rotatedImg; + + if (src == NULL) { + return NULL; + } + + if (!gdImageTrueColor(src) && (clrBack < 0 || clrBack>=gdImageColorsTotal(src))) { + return NULL; + } + + while (dAngle >= 360.0) { + dAngle -= 360.0; + } + + while (dAngle < 0) { + dAngle += 360.0; + } + + if (dAngle == 90.00) { + return gdImageRotate90(src, ignoretransparent); + } + if (dAngle == 180.00) { + return gdImageRotate180(src, ignoretransparent); + } + if(dAngle == 270.00) { + return gdImageRotate270 (src, ignoretransparent); + } + + if ((dAngle > 45.0) && (dAngle <= 135.0)) { + pMidImg = gdImageRotate90 (src, ignoretransparent); + dAngle -= 90.0; + } else if ((dAngle > 135.0) && (dAngle <= 225.0)) { + pMidImg = gdImageRotate180 (src, ignoretransparent); + dAngle -= 180.0; + } else if ((dAngle > 225.0) && (dAngle <= 315.0)) { + pMidImg = gdImageRotate270 (src, ignoretransparent); + dAngle -= 270.0; + } else { + return gdImageRotate45 (src, dAngle, clrBack, ignoretransparent); + } + + if (pMidImg == NULL) { + return NULL; + } + + rotatedImg = gdImageRotate45 (pMidImg, dAngle, clrBack, ignoretransparent); + gdImageDestroy(pMidImg); + + return rotatedImg; +} +/* End Rotate function */ + + diff --git a/windows/Makefile.w32 b/windows/Makefile.w32 index 194d55b0c..777b06f57 100755 --- a/windows/Makefile.w32 +++ b/windows/Makefile.w32 @@ -7,6 +7,7 @@ WITH_DEVEL = ../../deps !IFNDEF WITH_BUILD WITH_BUILD = ..\..\gdbuild !ENDIF + !IFDEF WITH_PNG !IF "$(WITH_PNG)"=="dll" PNG_LIBS = libpng.lib @@ -21,6 +22,20 @@ USE_LIBPNG = false !ENDIF !ENDIF +!IFDEF WITH_JPEG +!IF "$(WITH_JPEG)"=="dll" +JPEG_LIBS = libjpeg.lib +USE_LIBJPEG = true +PNG = dll +!ELSEIF "$(WITH_JPEG)"=="static" +JPEG_LIBS = libjpeg_a.lib +USE_LIBJPEG = true +PNG = static +!ELSE +USE_LIBJPEG = false +!ENDIF +!ENDIF + EXTRA_INCLUDE=$(WITH_DEVEL)/include EXTRA_LIBS=/libpath:$(WITH_DEVEL)/lib @@ -31,6 +46,7 @@ BUILD=Release LIBGD_SRC_DIR=..\src LIBGD_OBJ_DIR=$(WITH_BUILD) +LIBGD_BIN_DIR=$(WITH_BUILD)\bin !MESSAGE library source: $(LIBGD_SRC_DIR) !MESSAGE library objects: $(LIBGD_OBJ_DIR) @@ -40,7 +56,6 @@ LIBGDCFLAGS= \ /D DEFAULT_FONTPATH="\"C:\\WINDOWS\\FONTS;C:\\WINNT\\FONTS\"" \ /D HAVE_FT2BUILD_H \ /D HAVE_LIBZ \ - /D HAVE_GD_BUNDLED=1 \ /D HAVE_GD_GIF_READ=1 \ /D HAVE_GD_GIF_CREATE=1 \ /D HAVE_GD_IMAGESETBRUSH=1 \ @@ -116,7 +131,10 @@ LIB_OBJS= \ $(LIBGD_OBJ_DIR)\gd_transform.obj \ $(LIBGD_OBJ_DIR)\gd_wbmp.obj \ $(LIBGD_OBJ_DIR)\gdxpm.obj \ - $(LIBGD_OBJ_DIR)\wbmp.obj + $(LIBGD_OBJ_DIR)\wbmp.obj \ + $(LIBGD_OBJ_DIR)\gd_interpolation.obj \ + $(LIBGD_OBJ_DIR)\gd_matrix.obj \ + $(LIBGD_OBJ_DIR)\gd_rotate.obj EXE_OBJS= \ annotate.obj \ @@ -143,7 +161,10 @@ EXE_OBJS= \ $(LIBGD_OBJ_DIR): @if not exist $(LIBGD_OBJ_DIR) mkdir $(LIBGD_OBJ_DIR) -all: $(LIBGD_OBJ_DIR) $(LIB_OBJS) $(STATICLIB) +$(LIBGD_BIN_DIR): + @if not exist $(LIBGD_BIN_DIR) mkdir $(LIBGD_BIN_DIR) + +all: $(LIBGD_OBJ_DIR) $(LIBGD_BIN_DIR) $(LIB_OBJS) $(STATICLIB) .SUFFIXES: .c .obj .res @@ -151,7 +172,7 @@ all: $(LIBGD_OBJ_DIR) $(LIB_OBJS) $(STATICLIB) $(CC) $(CFLAGS) /Fo"$@" $< $(STATICLIB): $(LIB_OBJS) - $(LD) /lib /out:$(LIBGD_OBJ_DIR)/$(STATICLIB) $(LIB_OBJS) + $(LD) /lib /out:$(LIBGD_BIN_DIR)/$(STATICLIB) $(LIB_OBJS) dist: all -rmdir /s /q distro