lilimg 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/lilimg/lilimg.c +49 -38
- data/lilimg.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bdc6fc2f84a1003b4cd672530e0b9c0aefb50c8970c09bb0c0779f6d0a095a07
|
4
|
+
data.tar.gz: 630a7f496e536c17bcd1de5165e9d3981ac8a00e41e9491116399ef5fd530c7d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b7211d65cf200f9754e24b7f08b7e187041c4f0256dc5cff50935add105cb8d619ddbea292a4b7c9f73c516c997f1943018ecad8de770fcfd3bf17ac2acac70a
|
7
|
+
data.tar.gz: 7b9cec47e6e248bda6617eff400da2b7e43ef5c912d6faa33fbb78750b24a6910fbe9a80ae1062e3ce76ef02c168e91c85627d2ae3bc4d86dc573321c603cb0f
|
data/ext/lilimg/lilimg.c
CHANGED
@@ -7,60 +7,71 @@
|
|
7
7
|
#include "jo_gif.h"
|
8
8
|
|
9
9
|
unsigned char * rgb_to_rgba(unsigned char * buf, int w, int h) {
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
10
|
+
int newsize = 4 * w * h;
|
11
|
+
unsigned char * dest;
|
12
|
+
dest = malloc(newsize);
|
13
|
+
int s = 0, d = 0;
|
14
|
+
while (d < newsize) {
|
15
|
+
dest[d++] = buf[s++];
|
16
|
+
dest[d++] = buf[s++];
|
17
|
+
dest[d++] = buf[s++];
|
18
|
+
dest[d++] = 255; // fully opaque
|
19
|
+
}
|
20
|
+
|
21
|
+
return dest;
|
22
22
|
}
|
23
23
|
|
24
24
|
static VALUE jpeg_to_gif(VALUE self, VALUE buf) {
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
rb_check_type(buf, T_STRING);
|
26
|
+
|
27
|
+
if (buf[0] != 0xFF || buf[1] != 0xD8) {
|
28
|
+
rb_raise(rb_eRuntimeError, "Not a JPEG image!");
|
29
|
+
return Qnil;
|
30
|
+
}
|
28
31
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
rb_raise(rb_eRuntimeError, "Error decoding input file (size %d)", size);
|
33
|
-
return Qnil;
|
34
|
-
}
|
32
|
+
int size = RSTRING_LEN(buf);
|
33
|
+
char * jpeg = StringValuePtr(buf);
|
34
|
+
njInit();
|
35
35
|
|
36
|
+
if (njDecode(jpeg, size)) {
|
36
37
|
free((void*)jpeg);
|
38
|
+
rb_raise(rb_eRuntimeError, "Error decoding input file (size %d)", size);
|
39
|
+
return Qnil;
|
40
|
+
}
|
41
|
+
|
42
|
+
free((void*)jpeg);
|
43
|
+
|
44
|
+
unsigned char * pixels = njGetImage();
|
45
|
+
if (!pixels) {
|
46
|
+
rb_raise(rb_eRuntimeError, "Couldn't decode image.");
|
47
|
+
return Qnil;
|
48
|
+
}
|
37
49
|
|
38
|
-
|
50
|
+
char * out;
|
39
51
|
size_t bufsize;
|
40
52
|
FILE * fp = open_memstream(&out, &bufsize);
|
41
53
|
// FILE * fp = fopen("output.gif", "wb");
|
42
54
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
55
|
+
if (!fp) {
|
56
|
+
rb_raise(rb_eRuntimeError, "Cannot initialize memstream");
|
57
|
+
return Qnil;
|
58
|
+
}
|
47
59
|
|
48
|
-
|
49
|
-
|
50
|
-
int maxColors = 8;
|
60
|
+
unsigned char * rgba = rgb_to_rgba(pixels, njGetWidth(), njGetHeight());
|
61
|
+
int maxColors = 8;
|
51
62
|
|
52
|
-
|
53
|
-
|
54
|
-
|
63
|
+
jo_gif_t gif = jo_gif_start(fp, njGetWidth(), njGetHeight(), 0, maxColors);
|
64
|
+
jo_gif_frame(&gif, rgba, 0, 0);
|
65
|
+
jo_gif_end(&gif);
|
55
66
|
|
56
|
-
|
57
|
-
|
67
|
+
VALUE rstr;
|
68
|
+
rstr = rb_str_new(out, bufsize);
|
58
69
|
|
59
|
-
|
60
|
-
|
61
|
-
|
70
|
+
free(rgba);
|
71
|
+
free(out);
|
72
|
+
njDone();
|
62
73
|
|
63
|
-
|
74
|
+
return rstr;
|
64
75
|
}
|
65
76
|
|
66
77
|
VALUE Lilimg = Qnil;
|
data/lilimg.gemspec
CHANGED