Debian Codegraph @

src/image.c

raw symbols SCIP
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16
17 #include "image.h"
18 #include "fixed.h"
19
20 #include "bmp.c"
21 #ifdef HAVE_JPEG
22 #include "jpeg.c"
23 #endif
24 #ifdef HAVE_PNG
25 #include "png.c"
26 #endif
27 #ifdef HAVE_GIF
28 #include "gif.c"
29 #endif
30
31 // GD algorithm
32 #include "gd.c"
33
34 // Algorithms from GraphicsMagick
35 #include "magick.c"
36 #include "magick_fixed.c"
37
38 int
39 image_init(HV *self, image *im)
40 {
41 unsigned char *bptr;
42 char *file = NULL;
43 int ret = 1;
44
45 if (my_hv_exists(self, "file")) {
46 // Input from file
47 SV *path = *(my_hv_fetch(self, "file"));
48 file = SvPVX(path);
49 im->fh = IoIFP(sv_2io(*(my_hv_fetch(self, "_fh"))));
50 im->path = newSVsv(path);
51 }
52 else {
53 // Input from scalar ref
54 im->fh = NULL;
55 im->path = newSVpv("(data)", 0);
56 im->sv_data = *(my_hv_fetch(self, "data"));
57 if (SvROK(im->sv_data))
58 im->sv_data = SvRV(im->sv_data);
59 else
60 croak("data is not a scalar ref\n");
61 }
62
63 im->pixbuf = NULL;
64 im->outbuf = NULL;
65 im->outbuf_size = 0;
66 im->type = UNKNOWN;
67 im->sv_offset = 0;
68 im->image_offset = 0;
69 im->image_length = 0;
70 im->width = 0;
71 im->height = 0;
72 im->width_padding = 0;
73 im->width_inner = 0;
74 im->height_padding = 0;
75 im->height_inner = 0;
76 im->flipped = 0;
77 im->bpp = 0;
78 im->channels = 0;
79 im->has_alpha = 0;
80 im->orientation = ORIENTATION_NORMAL;
81 im->orientation_orig = ORIENTATION_NORMAL;
82 im->memory_limit = 0;
83 im->target_width = 0;
84 im->target_height = 0;
85 im->keep_aspect = 0;
86 im->resize_type = IMAGE_SCALE_TYPE_GD_FIXED;
87 im->filter = 0;
88 im->bgcolor = 0;
89 im->used = 0;
90 im->palette = NULL;
91
92 #ifdef HAVE_JPEG
93 im->cinfo = NULL;
94 #endif
95 #ifdef HAVE_PNG
96 im->png_ptr = NULL;
97 im->info_ptr = NULL;
98 #endif
99 #ifdef HAVE_GIF
100 im->gif = NULL;
101 #endif
102
103 // Read new() options
104 if (my_hv_exists(self, "offset")) {
105 im->image_offset = SvIV(*(my_hv_fetch(self, "offset")));
106 if (im->fh != NULL)
107 PerlIO_seek(im->fh, im->image_offset, SEEK_SET);
108 }
109
110 if (my_hv_exists(self, "length"))
111 im->image_length = SvIV(*(my_hv_fetch(self, "length")));
112
113 Newz(0, im->buf, sizeof(Buffer), Buffer);
114 buffer_init(im->buf, BUFFER_SIZE);
115 im->memory_used = BUFFER_SIZE;
116
117 // Determine type of file from magic bytes
118 if (im->fh != NULL) {
119 if ( !_check_buf(im->fh, im->buf, 8, BUFFER_SIZE) ) {
120 image_finish(im);
121 croak("Unable to read image header for %s\n", file);
122 }
123 }
124 else {
125 im->sv_offset = MIN(sv_len(im->sv_data) - im->image_offset, BUFFER_SIZE);
126 buffer_append(im->buf, SvPVX(im->sv_data) + im->image_offset, im->sv_offset);
127 }
128
129 bptr = buffer_ptr(im->buf);
130
131 switch (bptr[0]) {
132 case 0xff:
133 if (bptr[1] == 0xd8 && bptr[2] == 0xff) {
134 #ifdef HAVE_JPEG
135 im->type = JPEG;
136 #else
137 image_finish(im);
138 croak("Image::Scale was not built with JPEG support\n");
139 #endif
140 }
141 break;
142 case 0x89:
143 if (bptr[1] == 'P' && bptr[2] == 'N' && bptr[3] == 'G'
144 && bptr[4] == 0x0d && bptr[5] == 0x0a && bptr[6] == 0x1a && bptr[7] == 0x0a) {
145 #ifdef HAVE_PNG
146 im->type = PNG;
147 #else
148 image_finish(im);
149 croak("Image::Scale was not built with PNG support\n");
150 #endif
151 }
152 break;
153 case 'G':
154 if (bptr[1] == 'I' && bptr[2] == 'F' && bptr[3] == '8'
155 && (bptr[4] == '7' || bptr[4] == '9') && bptr[5] == 'a') {
156 #ifdef HAVE_GIF
157 im->type = GIF;
158 #else
159 image_finish(im);
160 croak("Image::Scale was not built with GIF support\n");
161 #endif
162 }
163 break;
164 case 'B':
165 if (bptr[1] == 'M') {
166 im->type = BMP;
167 }
168 break;
169 }
170
171 DEBUG_TRACE("Image type: %d\n", im->type);
172
173 // Read image header via type-specific function to determine dimensions
174 switch (im->type) {
175 #ifdef HAVE_JPEG
176 case JPEG:
177 if ( !image_jpeg_read_header(im) ) {
178 ret = 0;
179 goto out;
180 }
181 break;
182 #endif
183 #ifdef HAVE_PNG
184 case PNG:
185 if ( !image_png_read_header(im) ) {
186 ret = 0;
187 goto out;
188 }
189 break;
190 #endif
191 #ifdef HAVE_GIF
192 case GIF:
193 if ( !image_gif_read_header(im) ) {
194 ret = 0;
195 goto out;
196 }
197 break;
198 #endif
199 case BMP:
200 image_bmp_read_header(im);
201 break;
202 case UNKNOWN:
203 warn("Image::Scale unknown file type (%s), first 8 bytes were: %02x %02x %02x %02x %02x %02x %02x %02x\n",
204 SvPVX(im->path), bptr[0], bptr[1], bptr[2], bptr[3], bptr[4], bptr[5], bptr[6], bptr[7]);
205 ret = 0;
206 break;
207 }
208
209 DEBUG_TRACE("Image dimenensions: %d x %d, channels %d\n", im->width, im->height, im->channels);
210
211 out:
212 if (ret == 0)
213 image_finish(im);
214
215 return ret;
216 }
217
218 void
219 image_alloc(image *im, int width, int height)
220 {
221 int size = width * height * sizeof(pix);
222
223 if (im->memory_limit && im->memory_limit < im->memory_used + size) {
224 image_finish(im);
225 croak("Image::Scale memory_limit exceeded (wanted to allocate %d bytes)\n", im->memory_used + size);
226 }
227
228 DEBUG_TRACE("Allocating %d bytes for decompressed image\n", size);
229
230 New(0, im->pixbuf, size, pix);
231 im->memory_used += size;
232 }
233
234 void
235 image_bgcolor_fill(pix *buf, int size, int bgcolor)
236 {
237 int alloc_size = size * sizeof(pix);
238 int i;
239
240 if (bgcolor != 0) {
241 for (i = 0; i < alloc_size; i += sizeof(pix))
242 memcpy( ((char *)buf) + i, &bgcolor, sizeof(pix) );
243 }
244 else {
245 Zero(buf, size, pix);
246 }
247 }
248
249 int
250 image_resize(image *im)
251 {
252 int size;
253 int ret = 1;
254
255 // Check if we have already resized an image with this object,
256 // if so, clear everything we've already done
257 if (im->used) {
258 DEBUG_TRACE("Object already used for a resize, resetting\n");
259 if (im->outbuf != NULL) {
260 Safefree(im->outbuf);
261 im->outbuf = NULL;
262 im->memory_used -= im->outbuf_size;
263 }
264
265 #ifdef HAVE_JPEG
266 // For a JPEG we have to reset the scaled size in case we're resizing larger than before
267 if (im->type == JPEG) {
268 im->width = im->cinfo->image_width;
269 im->height = im->cinfo->image_height;
270
271 DEBUG_TRACE("JPEG dimensions set back to original %d x %d\n", im->width, im->height);
272 }
273 #endif
274 }
275
276 // Load the source image into memory
277 switch (im->type) {
278 #ifdef HAVE_JPEG
279 case JPEG:
280 if ( !image_jpeg_load(im) ) {
281 ret = 0;
282 goto out;
283 }
284 break;
285 #endif
286 #ifdef HAVE_PNG
287 case PNG:
288 if ( !image_png_load(im) ) {
289 ret = 0;
290 goto out;
291 }
292 break;
293 #endif
294 #ifdef HAVE_GIF
295 case GIF:
296 if ( !image_gif_load(im) ) {
297 ret = 0;
298 goto out;
299 }
300 break;
301 #endif
302 case BMP:
303 if ( !image_bmp_load(im) ) {
304 ret = 0;
305 goto out;
306 }
307 break;
308 }
309
310 // Special case for equal size without resizing
311 if (im->width == im->target_width && im->height == im->target_height) {
312 im->outbuf = im->pixbuf;
313 goto out;
314 }
315
316 // Allocate space for the resized image
317 size = im->target_width * im->target_height;
318 im->outbuf_size = size * sizeof(pix);
319
320 if (im->memory_limit && im->memory_limit < im->memory_used + im->outbuf_size) {
321 image_finish(im);
322 croak("Image::Scale memory_limit exceeded (wanted to allocate %d bytes)\n", im->memory_used + im->outbuf_size);
323 }
324
325 DEBUG_TRACE("Allocating %d bytes for resized image of size %d x %d\n",
326 im->outbuf_size, im->target_width, im->target_height);
327 New(0, im->outbuf, size, pix);
328 im->memory_used += im->outbuf_size;
329
330 // Determine padding if necessary
331 if (im->keep_aspect) {
332 float source_ar = 1.0 * im->width / im->height;
333 float dest_ar = 1.0 * im->target_width / im->target_height;
334
335 if (source_ar >= dest_ar) {
336 im->height_padding = (int)((im->target_height - (im->target_width / source_ar)) / 2);
337 im->height_inner = (int)(im->target_width / source_ar);
338 if (im->height_inner < 1) // Avoid divide by 0
339 im->height_inner = 1;
340 }
341 else {
342 im->width_padding = (int)((im->target_width - (im->target_height * source_ar)) / 2);
343 im->width_inner = (int)(im->target_height * source_ar);
344 if (im->width_inner < 1) // Avoid divide by 0
345 im->width_inner = 1;
346 }
347
348 // Fill new space with the bgcolor or zeros
349 image_bgcolor_fill(im->outbuf, size, im->bgcolor);
350
351 DEBUG_TRACE("Using width padding %d, inner width %d, height padding %d, inner height %d, bgcolor %x\n",
352 im->width_padding, im->width_inner, im->height_padding, im->height_inner, im->bgcolor);
353 }
354
355 // Resize
356 switch (im->resize_type) {
357 case IMAGE_SCALE_TYPE_GD:
358 image_downsize_gd(im);
359 break;
360 case IMAGE_SCALE_TYPE_GD_FIXED:
361 image_downsize_gd_fixed_point(im);
362 break;
363 case IMAGE_SCALE_TYPE_GM:
364 image_downsize_gm(im);
365 break;
366 case IMAGE_SCALE_TYPE_GM_FIXED:
367 image_downsize_gm_fixed_point(im);
368 break;
369 default:
370 image_finish(im);
371 croak("Image::Scale unknown resize type %d\n", im->resize_type);
372 }
373
374 // If the image was rotated, swap the width/height if necessary
375 // This is needed for the save_*() functions to output the correct size
376 if (im->orientation >= 5) {
377 int tmp = im->target_height;
378 im->target_height = im->target_width;
379 im->target_width = tmp;
380
381 DEBUG_TRACE("Image was rotated, output now %d x %d\n", im->target_width, im->target_height);
382 }
383
384 // After resizing we can release the source image memory
385 Safefree(im->pixbuf);
386 im->pixbuf = NULL;
387
388 out:
389 im->used++;
390
391 return ret;
392 }
393
394 void
395 image_finish(image *im)
396 {
397 // Called at DESTROY-time to release all memory if needed.
398 // Items here may be freed elsewhere so must check that they aren't NULL
399
400 DEBUG_TRACE("image_finish\n");
401
402 switch (im->type) {
403 #ifdef HAVE_JPEG
404 case JPEG:
405 image_jpeg_finish(im);
406 break;
407 #endif
408 #ifdef HAVE_PNG
409 case PNG:
410 image_png_finish(im);
411 break;
412 #endif
413 #ifdef HAVE_GIF
414 case GIF:
415 image_gif_finish(im);
416 break;
417 #endif
418 case BMP:
419 image_bmp_finish(im);
420 break;
421 }
422
423 if (im->buf != NULL) {
424 buffer_free(im->buf);
425 Safefree(im->buf);
426 im->buf = NULL;
427 }
428
429 if (im->pixbuf != NULL && im->pixbuf != im->outbuf) { // pixbuf = outbuf if resizing to same dimensions
430 Safefree(im->pixbuf);
431 im->pixbuf = NULL;
432 }
433
434 if (im->outbuf != NULL) {
435 Safefree(im->outbuf);
436 im->outbuf = NULL;
437 im->outbuf_size = 0;
438 }
439
440 if (im->path != NULL) {
441 SvREFCNT_dec(im->path);
442 im->path = NULL;
443 }
444
445 DEBUG_TRACE("Freed all memory, total used: %d\n", im->memory_used);
446 im->memory_used = 0;
447 }
448
449 inline void
450 image_get_rotated_coords(image *im, int x, int y, int *ox, int *oy)
451 {
452 switch (im->orientation) {
453 case ORIENTATION_MIRROR_HORIZ: // 2
454 *ox = im->target_width - 1 - x;
455 *oy = y;
456 break;
457 case ORIENTATION_180: // 3
458 *ox = im->target_width - 1 - x;
459 *oy = im->target_height - 1 - y;
460 break;
461 case ORIENTATION_MIRROR_VERT: // 4
462 *ox = x;
463 *oy = im->target_height - 1 - y;
464 break;
465 case ORIENTATION_MIRROR_HORIZ_270_CCW: // 5
466 *ox = y;
467 *oy = x;
468 break;
469 case ORIENTATION_90_CCW: // 6
470 *ox = im->target_height - 1 - y;
471 *oy = x;
472 break;
473 case ORIENTATION_MIRROR_HORIZ_90_CCW: // 7
474 *ox = im->target_height - 1 - y;
475 *oy = im->target_width - 1 - x;
476 break;
477 case ORIENTATION_270_CCW: // 8
478 *ox = y;
479 *oy = im->target_width - 1 - x;
480 break;
481 default:
482 if (x == 0 && y == 0 && im->orientation != 0) // An invalid orientation of 0 is often seen in non-rotated images
483 warn("Image::Scale cannot rotate, unknown orientation value: %d (%s)\n", im->orientation, SvPVX(im->path));
484 *ox = x;
485 *oy = y;
486 break;
487 }
488 }