-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrokenjpeg.c
More file actions
59 lines (57 loc) · 1.51 KB
/
brokenjpeg.c
File metadata and controls
59 lines (57 loc) · 1.51 KB
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gd.h>
#include "fallbacks.h"
#include "commons.h"
int resize_brokenjpeg(char *outfile, char *file, int tw, int th, int scale) {
FILE *out, *in;
gdImagePtr im;
gdImagePtr im_new;
/* initialise storage for gd original and resized gifs */
in = fopen (file, "rb");
out = fopen(outfile, "wb");
if (!out) {
fprintf(stderr,
"Unable to write to output file -- exiting\n");
exit(1);
}
im=gdImageCreateFromJpeg(in);
if (scale==NONE) {
th=gdImageSY(im);
tw=gdImageSX(im);
}
if (scale==SHRINK) {
if ( (tw>=gdImageSX(im)) && (th>=gdImageSY(im))) {
th=gdImageSY(im);
tw=gdImageSX(im);
} else {
scale = ASPECT;
}
}
if (scale==FIT) {
if ((float)gdImageSX(im)/(float)gdImageSY(im)*(float)th > tw)
tw=(float)gdImageSX(im)/(float)gdImageSY(im)*(float)th;
else
th=(float)gdImageSY(im)/(float)gdImageSX(im)*(float)tw;
}
if (scale == ASPECT) {
if ((float)gdImageSX(im)/(float)gdImageSY(im)*(float)th < tw)
tw=(float)gdImageSX(im)/(float)gdImageSY(im)*(float)th;
else
th=(float)gdImageSY(im)/(float)gdImageSX(im)*(float)tw;
}
im_new=gdImageCreateTrueColor(tw, th);
/*scale by copying*/
gdImageCopyResampled(im_new, im, 0, 0, 0, 0, tw, th, gdImageSX(im), gdImageSY(im));
/* try to find closet transparency index without extending the palette */
gdImageJpeg(im_new, out, -1);
if (im)
gdImageDestroy(im);
if (im_new)
gdImageDestroy(im_new);
fclose(in);
fclose(out);
/* All's well that ends well. */
return 0;
}