This repository has been archived by the owner on Jan 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepeg.py
119 lines (106 loc) · 4.4 KB
/
epeg.py
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
import os.path
import sys
from site import USER_SITE
from cffi import FFI
ffi = FFI()
try:
lib = ffi.dlopen(next(os.path.join(x, '_epeg.so') for x in sys.path
if os.path.exists(os.path.join(x, '_epeg.so'))))
except StopIteration:
raise Exception("Could not find _epeg.so!")
ffi.cdef("""
typedef enum
{
EPEG_GRAY8,
EPEG_YUV8,
EPEG_RGB8,
EPEG_BGR8,
EPEG_RGBA8,
EPEG_BGRA8,
EPEG_ARGB32,
EPEG_CMYK
} Epeg_Colorspace;
typedef struct
{
char *uri;
unsigned long long int mtime;
int w, h;
char *mimetype;
} Epeg_Thumbnail_Info;
typedef struct
{
struct _epeg_error_mgr jerr;
struct stat stat_info;
unsigned char *pixels;
unsigned char **lines;
char scaled : 1;
int error;
Epeg_Colorspace color_space;
struct {
char *file;
struct {
unsigned char **data;
int size;
} mem;
int w, h;
char *comment;
FILE *f;
struct jpeg_decompress_struct jinfo;
struct {
char *uri;
unsigned long long int mtime;
int w, h;
char *mime;
} thumb_info;
} in;
struct {
char *file;
struct {
unsigned char **data;
int *size;
} mem;
int x, y;
int w, h;
char *comment;
FILE *f;
struct jpeg_compress_struct jinfo;
int quality;
char thumbnail_info : 1;
} out;
} Epeg_Image;
Epeg_Image *epeg_file_open (const char *file);
Epeg_Image *epeg_memory_open (unsigned char *data, int size);
void epeg_size_get (Epeg_Image *im, int *w, int *h);
void epeg_decode_size_set (Epeg_Image *im, int w, int h);
void epeg_colorspace_get (Epeg_Image *im, int *space);
void epeg_decode_colorspace_set (Epeg_Image *im, Epeg_Colorspace colorspace);
const void *epeg_pixels_get (Epeg_Image *im, int x, int y, int w, int h);
void epeg_pixels_free (Epeg_Image *im, const void *data);
const char *epeg_comment_get (Epeg_Image *im);
void epeg_thumbnail_comments_get (Epeg_Image *im, Epeg_Thumbnail_Info *info);
void epeg_comment_set (Epeg_Image *im, const char *comment);
void epeg_quality_set (Epeg_Image *im, int quality);
void epeg_thumbnail_comments_enable (Epeg_Image *im, int onoff);
void epeg_file_output_set (Epeg_Image *im, const char *file);
void epeg_memory_output_set (Epeg_Image *im, unsigned char **data, int *size);
int epeg_encode (Epeg_Image *im);
int epeg_trim (Epeg_Image *im);
void epeg_close (Epeg_Image *im);
""")
def scale_image(fname, width, height, quality=75):
try:
img = lib.epeg_file_open(fname)
except TypeError:
# FIXME: The first call to epeg_file_open will always throw a
# TypeError, and I have no clue why...
# Subsequent calls always work fine, so we currently have
# this ugly workaround.
img = lib.epeg_file_open(fname)
lib.epeg_decode_size_set(img, width, height)
lib.epeg_quality_set(img, quality)
pdata = ffi.new("unsigned char **")
psize = ffi.new("int*")
lib.epeg_memory_output_set(img, pdata, psize)
lib.epeg_encode(img)
lib.epeg_close(img)
return ffi.buffer(pdata[0], psize[0])[:]