| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#ifdef HAVE_JPEG |
| 18 |
#include <jpeglib.h> |
| 19 |
#endif |
| 20 |
#ifdef HAVE_GIF |
| 21 |
#include <gif_lib.h> |
| 22 |
#endif |
| 23 |
|
| 24 |
#define BUFFER_SIZE 4096 |
| 25 |
|
| 26 |
#define DEFAULT_JPEG_QUALITY 90 |
| 27 |
|
| 28 |
#define COL(red, green, blue) (((red) << 24) | ((green) << 16) | ((blue) << 8) | 0xFF) |
| 29 |
#define COL_FULL(red, green, blue, alpha) (((red) << 24) | ((green) << 16) | ((blue) << 8) | (alpha)) |
| 30 |
#define COL_RED(col) ((col >> 24) & 0xFF) |
| 31 |
#define COL_GREEN(col) ((col >> 16) & 0xFF) |
| 32 |
#define COL_BLUE(col) ((col >> 8) & 0xFF) |
| 33 |
#define COL_ALPHA(col) (col & 0xFF) |
| 34 |
|
| 35 |
#ifndef MAX |
| 36 |
#define MAX(x,y) (x) > (y) ? (x) : (y) |
| 37 |
#endif |
| 38 |
#ifndef MIN |
| 39 |
#define MIN(x,y) (x) < (y) ? (x) : (y) |
| 40 |
#endif |
| 41 |
#ifndef ABS |
| 42 |
#define ABS(x) ((x) < 0 ? -(x) : (x)) |
| 43 |
#endif |
| 44 |
|
| 45 |
|
| 46 |
#ifdef _MSC_VER |
| 47 |
static inline int lrintf(float f) { |
| 48 |
#ifdef _M_X64 |
| 49 |
return (int)((f > 0.0f) ? (f + 0.5f) : (f -0.5f)); |
| 50 |
#else |
| 51 |
int i; |
| 52 |
_asm { |
| 53 |
fld f |
| 54 |
fistp i |
| 55 |
}; |
| 56 |
|
| 57 |
return i; |
| 58 |
#endif |
| 59 |
} |
| 60 |
#endif |
| 61 |
|
| 62 |
#define ROUND_FLOAT_TO_INT(x) lrintf(x) |
| 63 |
|
| 64 |
#define EPSILON 1.0e-12 |
| 65 |
#define PI 3.14159265358979323846264338327950288419716939937510 |
| 66 |
|
| 67 |
#if defined(__GNUC__) |
| 68 |
#define ARGUNUSED(arg) arg __attribute__((unused)) |
| 69 |
#else |
| 70 |
#define ARGUNUSED(arg) arg |
| 71 |
#endif |
| 72 |
|
| 73 |
typedef uint32_t pix; |
| 74 |
|
| 75 |
enum image_type { |
| 76 |
UNKNOWN = 0, |
| 77 |
JPEG, |
| 78 |
GIF, |
| 79 |
PNG, |
| 80 |
BMP |
| 81 |
}; |
| 82 |
|
| 83 |
|
| 84 |
enum resize_type { |
| 85 |
IMAGE_SCALE_TYPE_GD = 0, |
| 86 |
IMAGE_SCALE_TYPE_GD_FIXED, |
| 87 |
IMAGE_SCALE_TYPE_GM, |
| 88 |
IMAGE_SCALE_TYPE_GM_FIXED |
| 89 |
}; |
| 90 |
|
| 91 |
|
| 92 |
enum orientation { |
| 93 |
ORIENTATION_NORMAL = 1, |
| 94 |
ORIENTATION_MIRROR_HORIZ, |
| 95 |
ORIENTATION_180, |
| 96 |
ORIENTATION_MIRROR_VERT, |
| 97 |
ORIENTATION_MIRROR_HORIZ_270_CCW, |
| 98 |
ORIENTATION_90_CCW, |
| 99 |
ORIENTATION_MIRROR_HORIZ_90_CCW, |
| 100 |
ORIENTATION_270_CCW |
| 101 |
}; |
| 102 |
|
| 103 |
|
| 104 |
enum bmp_compression { |
| 105 |
BMP_BI_RGB = 0, |
| 106 |
BMP_BI_RLE8, |
| 107 |
BMP_BI_RLE4, |
| 108 |
BMP_BI_BITFIELDS, |
| 109 |
BMP_BI_JPEG, |
| 110 |
BMP_BI_PNG |
| 111 |
}; |
| 112 |
|
| 113 |
typedef struct { |
| 114 |
int colors[256]; |
| 115 |
} palette; |
| 116 |
|
| 117 |
typedef struct { |
| 118 |
Buffer *buf; |
| 119 |
SV *path; |
| 120 |
PerlIO *fh; |
| 121 |
SV *sv_data; |
| 122 |
int32_t sv_offset; |
| 123 |
int32_t image_offset; |
| 124 |
int32_t image_length; |
| 125 |
int32_t type; |
| 126 |
int32_t width; |
| 127 |
int32_t height; |
| 128 |
int32_t width_padding; |
| 129 |
int32_t width_inner; |
| 130 |
int32_t height_padding; |
| 131 |
int32_t height_inner; |
| 132 |
int32_t flipped; |
| 133 |
int32_t bpp; |
| 134 |
int32_t compression; |
| 135 |
int32_t channels; |
| 136 |
int32_t has_alpha; |
| 137 |
int32_t orientation; |
| 138 |
int32_t orientation_orig; |
| 139 |
int32_t memory_used; |
| 140 |
int32_t outbuf_size; |
| 141 |
int32_t used; |
| 142 |
pix *pixbuf; |
| 143 |
pix *outbuf; |
| 144 |
pix *tmpbuf; |
| 145 |
palette *palette; |
| 146 |
|
| 147 |
|
| 148 |
int32_t memory_limit; |
| 149 |
int32_t target_width; |
| 150 |
int32_t target_height; |
| 151 |
int32_t keep_aspect; |
| 152 |
int32_t resize_type; |
| 153 |
int32_t filter; |
| 154 |
int32_t bgcolor; |
| 155 |
|
| 156 |
#ifdef HAVE_JPEG |
| 157 |
struct jpeg_decompress_struct *cinfo; |
| 158 |
struct jpeg_error_mgr *jpeg_error_pub; |
| 159 |
#endif |
| 160 |
|
| 161 |
#ifdef HAVE_PNG |
| 162 |
png_structp png_ptr; |
| 163 |
png_infop info_ptr; |
| 164 |
#endif |
| 165 |
|
| 166 |
#ifdef HAVE_GIF |
| 167 |
GifFileType *gif; |
| 168 |
#endif |
| 169 |
} image; |
| 170 |
|
| 171 |
static inline pix |
| 172 |
get_pix(image *im, int32_t x, int32_t y) |
| 173 |
{ |
| 174 |
return (im->pixbuf[(y * im->width) + x]); |
| 175 |
} |
| 176 |
|
| 177 |
static inline void |
| 178 |
put_pix(image *im, int32_t x, int32_t y, pix col) |
| 179 |
{ |
| 180 |
im->outbuf[(y * im->target_width) + x] = col; |
| 181 |
} |
| 182 |
|
| 183 |
static inline void |
| 184 |
put_pix_rotated(image *im, int32_t x, int32_t y, int32_t rotated_width, pix col) |
| 185 |
{ |
| 186 |
im->outbuf[(y * rotated_width) + x] = col; |
| 187 |
} |
| 188 |
|
| 189 |
int image_init(HV *self, image *im); |
| 190 |
int image_resize(image *im); |
| 191 |
void image_downsize_gd(image *im); |
| 192 |
void image_downsize_gd_fixed_point(image *im); |
| 193 |
void image_downsize_gm(image *im); |
| 194 |
void image_alloc(image *im, int width, int height); |
| 195 |
void image_bgcolor_fill(pix *buf, int size, int bgcolor); |
| 196 |
void image_finish(image *im); |
| 197 |
inline void image_get_rotated_coords(image *im, int x, int y, int *ox, int *oy); |
| 198 |
|
| 199 |
#ifdef HAVE_JPEG |
| 200 |
int image_jpeg_read_header(image *im); |
| 201 |
int image_jpeg_load(image *im); |
| 202 |
void image_jpeg_save(image *im, const char *path, int quality); |
| 203 |
void image_jpeg_to_sv(image *im, int quality, SV *sv_buf); |
| 204 |
void image_jpeg_finish(image *im); |
| 205 |
#endif |
| 206 |
|
| 207 |
int image_bmp_read_header(image *im); |
| 208 |
int image_bmp_load(image *im); |
| 209 |
void image_bmp_finish(image *im); |
| 210 |
|
| 211 |
#ifdef HAVE_GIF |
| 212 |
int image_gif_read_header(image *im); |
| 213 |
int image_gif_load(image *im); |
| 214 |
void image_gif_finish(image *im); |
| 215 |
#endif |
| 216 |
|
| 217 |
#ifdef HAVE_PNG |
| 218 |
int image_png_read_header(image *im); |
| 219 |
int image_png_load(image *im); |
| 220 |
void image_png_save(image *im, const char *path); |
| 221 |
void image_png_to_sv(image *im, SV *sv_buf); |
| 222 |
void image_png_finish(image *im); |
| 223 |
#endif |