File: image.h

package info (click to toggle)
libimage-scale-perl 0.14%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,828 kB
  • sloc: ansic: 4,760; perl: 313; makefile: 3
file content (223 lines) | stat: -rw-r--r-- 5,282 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifdef HAVE_JPEG
#include <jpeglib.h>
#endif
#ifdef HAVE_GIF
#include <gif_lib.h>
#endif

#define BUFFER_SIZE 4096

#define DEFAULT_JPEG_QUALITY 90

#define COL(red, green, blue) (((red) << 24) | ((green) << 16) | ((blue) << 8) | 0xFF)
#define COL_FULL(red, green, blue, alpha) (((red) << 24) | ((green) << 16) | ((blue) << 8) | (alpha))
#define COL_RED(col)   ((col >> 24) & 0xFF)
#define COL_GREEN(col) ((col >> 16) & 0xFF)
#define COL_BLUE(col)  ((col >> 8) & 0xFF)
#define COL_ALPHA(col) (col & 0xFF)

#ifndef MAX
#define MAX(x,y) (x) > (y) ? (x) : (y)
#endif
#ifndef MIN
#define MIN(x,y) (x) < (y) ? (x) : (y)
#endif
#ifndef ABS
#define ABS(x)  ((x) < 0 ? -(x) : (x))
#endif

// MSVC does not have lrintf
#ifdef _MSC_VER
static inline int lrintf(float f) {
#ifdef _M_X64
  return (int)((f > 0.0f) ? (f + 0.5f) : (f -0.5f));
#else
  int i;
  _asm {
    fld f
    fistp i
  };

  return i;
#endif
}
#endif

#define ROUND_FLOAT_TO_INT(x) lrintf(x)

#define EPSILON 1.0e-12
#define PI      3.14159265358979323846264338327950288419716939937510

#if defined(__GNUC__)
#define ARGUNUSED(arg) arg __attribute__((unused))
#else
#define ARGUNUSED(arg) arg
#endif

typedef uint32_t pix;

enum image_type {
  UNKNOWN = 0,
  JPEG,
  GIF,
  PNG,
  BMP
};

// Resize algorithms
enum resize_type {
  IMAGE_SCALE_TYPE_GD = 0,
  IMAGE_SCALE_TYPE_GD_FIXED,
  IMAGE_SCALE_TYPE_GM,
  IMAGE_SCALE_TYPE_GM_FIXED
};

// Exif Orientation
enum orientation {
  ORIENTATION_NORMAL = 1,
  ORIENTATION_MIRROR_HORIZ,
  ORIENTATION_180,
  ORIENTATION_MIRROR_VERT,
  ORIENTATION_MIRROR_HORIZ_270_CCW,
  ORIENTATION_90_CCW,
  ORIENTATION_MIRROR_HORIZ_90_CCW,
  ORIENTATION_270_CCW
};

// BMP Compression methods
enum bmp_compression {
  BMP_BI_RGB = 0,
  BMP_BI_RLE8,
  BMP_BI_RLE4,
  BMP_BI_BITFIELDS,
  BMP_BI_JPEG,
  BMP_BI_PNG
};

typedef struct {
  int colors[256];
} palette;

typedef struct {
  Buffer  *buf;
  SV      *path;
  PerlIO  *fh;
  SV      *sv_data;
  int32_t sv_offset;
  int32_t image_offset;
  int32_t image_length;
  int32_t type;
  int32_t width;
  int32_t height;
  int32_t width_padding;  // empty padding pixels to leave to maintain aspect
  int32_t width_inner;    // width of inner area when maintaining aspect
  int32_t height_padding;
  int32_t height_inner;
  int32_t flipped;
  int32_t bpp;
  int32_t compression;
  int32_t channels;
  int32_t has_alpha;
  int32_t orientation;
  int32_t orientation_orig;
  int32_t memory_used;
  int32_t outbuf_size;
  int32_t used; // How many times the object has been used to resize
  pix     *pixbuf; // Source image
  pix     *outbuf; // Resized image
  pix     *tmpbuf; // Temporary intermediate image
  palette *palette;

  // Resize options
  int32_t memory_limit;
  int32_t target_width;
  int32_t target_height;
  int32_t keep_aspect;
  int32_t resize_type;
  int32_t filter;
  int32_t bgcolor;

#ifdef HAVE_JPEG
  struct jpeg_decompress_struct *cinfo;
  struct jpeg_error_mgr *jpeg_error_pub;
#endif

#ifdef HAVE_PNG
  png_structp png_ptr;
  png_infop info_ptr;
#endif

#ifdef HAVE_GIF
  GifFileType *gif;
#endif
} image;

static inline pix
get_pix(image *im, int32_t x, int32_t y)
{
	return (im->pixbuf[(y * im->width) + x]);
}

static inline void
put_pix(image *im, int32_t x, int32_t y, pix col)
{
	im->outbuf[(y * im->target_width) + x] = col;
}

static inline void
put_pix_rotated(image *im, int32_t x, int32_t y, int32_t rotated_width, pix col)
{
  im->outbuf[(y * rotated_width) + x] = col;
}

int image_init(HV *self, image *im);
int image_resize(image *im);
void image_downsize_gd(image *im);
void image_downsize_gd_fixed_point(image *im);
void image_downsize_gm(image *im);
void image_alloc(image *im, int width, int height);
void image_bgcolor_fill(pix *buf, int size, int bgcolor);
void image_finish(image *im);
inline void image_get_rotated_coords(image *im, int x, int y, int *ox, int *oy);

#ifdef HAVE_JPEG
int image_jpeg_read_header(image *im);
int image_jpeg_load(image *im);
void image_jpeg_save(image *im, const char *path, int quality);
void image_jpeg_to_sv(image *im, int quality, SV *sv_buf);
void image_jpeg_finish(image *im);
#endif

int image_bmp_read_header(image *im);
int image_bmp_load(image *im);
void image_bmp_finish(image *im);

#ifdef HAVE_GIF
int image_gif_read_header(image *im);
int image_gif_load(image *im);
void image_gif_finish(image *im);
#endif

#ifdef HAVE_PNG
int image_png_read_header(image *im);
int image_png_load(image *im);
void image_png_save(image *im, const char *path);
void image_png_to_sv(image *im, SV *sv_buf);
void image_png_finish(image *im);
#endif