-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathepeg.c
132 lines (119 loc) · 2.89 KB
/
epeg.c
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
#include <ruby.h>
#include <ruby/intern.h>
#include <Epeg.h>
VALUE cEpeg;
ID idsize;
ID idclosed;
static VALUE rb_epeg_thumbnail(klass, fname, x, y)
VALUE klass, fname, x, y;
{
Epeg_Image *image = NULL;
unsigned char *data = NULL;
int size = 0;
VALUE retval;
Check_Type(fname, T_STRING);
if(StringValueCStr(fname)[0] != '/') {
fname = rb_file_s_expand_path(1, &fname);
}
image = epeg_file_open(StringValueCStr(fname));
if(!image) {
rb_raise(rb_eStandardError, "Can't open image");
}
epeg_decode_size_set(image, NUM2INT(x), NUM2INT(y));
epeg_memory_output_set(image, &data, &size);
if(epeg_encode(image) != 0) {
rb_raise(rb_eStandardError, "Can't encode image");
}
epeg_close(image);
retval = rb_str_new(data, size);
free(data);
return retval;
}
static VALUE
rb_epeg_new(klass, fname)
VALUE klass, fname;
{
VALUE retval;
Epeg_Image *image;
image = epeg_file_open(StringValueCStr(fname));
if(!image) {
rb_raise(rb_eStandardError, "Can't open image");
}
retval = Data_Wrap_Struct(klass, NULL, NULL, image);
rb_obj_call_init(retval, 0, NULL);
return retval;
}
static VALUE
rb_epeg_get_size(obj)
VALUE obj;
{
int x, y;
Epeg_Image *image;
VALUE retval;
retval = rb_ivar_get(obj, idsize);
if(RTEST(retval))
return retval;
Data_Get_Struct(obj, Epeg_Image, image);
epeg_size_get(image, &x, &y);
retval = rb_ary_new3(2, INT2NUM(x), INT2NUM(y));
rb_ivar_set(obj, idsize, retval);
return retval;
}
static VALUE
rb_epeg_set_output_size(obj, x, y)
VALUE obj, x, y;
{
Epeg_Image *image;
VALUE retval;
if(OBJ_FROZEN(obj)) {
rb_raise(rb_eStandardError, "can't set size now");
}
Data_Get_Struct(obj, Epeg_Image, image);
epeg_decode_size_set(image, NUM2INT(x), NUM2INT(y));
return Qnil;
}
static VALUE
rb_epeg_finish(argc, args, obj)
int argc;
VALUE *args;
VALUE obj;
{
Epeg_Image *image;
VALUE retval;
unsigned char *data;
int size;
if(OBJ_FROZEN(obj)) {
rb_raise(rb_eStandardError, "Epeg finished");
}
Data_Get_Struct(obj, Epeg_Image, image);
if(argc == 0) { // Return as string
epeg_memory_output_set(image, &data, &size);
} else if(argc == 1) { // Filename
Check_Type(args[0], T_STRING);
epeg_file_output_set(image, RSTRING_PTR(args[0]));
} else {
rb_raise(rb_eRuntimeError, "invalid arguments");
}
if(epeg_encode(image) != 0) {
rb_raise(rb_eStandardError, "Can't encode image");
}
epeg_close(image);
OBJ_FREEZE(obj);
if(argc == 0) {
retval = rb_str_new(data, size);
free(data);
return retval;
} else {
return Qnil;
}
}
void Init_epeg () {
idclosed = rb_intern("closed");
idsize = rb_intern("size");
cEpeg = rb_define_class("Epeg", rb_cObject);
rb_define_singleton_method(cEpeg, "thumbnail", rb_epeg_thumbnail, 3);
rb_define_singleton_method(cEpeg, "new", rb_epeg_new, 1);
rb_define_method(cEpeg, "size", rb_epeg_get_size, 0);
rb_define_method(cEpeg, "set_output_size", rb_epeg_set_output_size, 2);
rb_define_method(cEpeg, "finish", rb_epeg_finish, -1);
}