jpeg 0.5.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/ext/jpeg/jpeg_image.c +66 -11
- data/ext/jpeg/jpeg_image.h +1 -0
- data/lib/jpeg/version.rb +1 -1
- data/spec/jpeg_spec.rb +8 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d53a185f8b43438f663a8e6515c32e76913c618
|
4
|
+
data.tar.gz: 1ae8dc89c3d59e39daa72326ec5d1baabb0545c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 293cb450233a0c2b2d0a5b915031853f0633ce95ac8f21d5f65fad003489ce214af197a7b3bc48eff4d06e70164f3d3152e4b5fe5deacb92e0a47447445bba10
|
7
|
+
data.tar.gz: 8dd0300b215a2094ac3b4f812ec09a1d0671b313b99eb658ce11d7c3ca5909bc4ea165178ec5139085d38a49118cc0f758fac5f998e38bcce34676bf77fd0c59
|
data/.travis.yml
CHANGED
data/ext/jpeg/jpeg_image.c
CHANGED
@@ -14,6 +14,7 @@ static VALUE jpeg_image_width(VALUE self);
|
|
14
14
|
static VALUE jpeg_image_height(VALUE self);
|
15
15
|
static VALUE jpeg_image_size(VALUE self);
|
16
16
|
static VALUE jpeg_image_color_info(VALUE self);
|
17
|
+
static VALUE jpeg_image_raw_data(VALUE self);
|
17
18
|
|
18
19
|
extern VALUE rb_eJpegError;
|
19
20
|
|
@@ -25,17 +26,19 @@ void Init_jpeg_image(void) {
|
|
25
26
|
rb_define_method(rb_cJpegImage, "height", jpeg_image_height, 0);
|
26
27
|
rb_define_method(rb_cJpegImage, "size", jpeg_image_size, 0);
|
27
28
|
rb_define_method(rb_cJpegImage, "color_info", jpeg_image_color_info, 0);
|
29
|
+
rb_define_method(rb_cJpegImage, "raw_data", jpeg_image_raw_data, 0);
|
28
30
|
}
|
29
31
|
|
30
32
|
void jpeg_image_exit(j_common_ptr jpeg) {
|
31
33
|
char buffer[JMSG_LENGTH_MAX];
|
32
34
|
jpeg->err->format_message(jpeg, buffer);
|
33
|
-
rb_raise(rb_eJpegError, buffer);
|
35
|
+
rb_raise(rb_eJpegError, "%s", buffer);
|
34
36
|
}
|
35
37
|
|
36
38
|
static VALUE jpeg_image_alloc(VALUE klass) {
|
37
39
|
struct rb_jpeg_image *jpeg = ALLOC(struct rb_jpeg_image);
|
38
40
|
|
41
|
+
jpeg->fp = NULL;
|
39
42
|
jpeg->read = (void *)ALLOC(struct jpeg_decompress_struct);
|
40
43
|
jpeg->error = (void *)ALLOC(struct jpeg_error_mgr);
|
41
44
|
jpeg->read->err = jpeg_std_error(jpeg->error);
|
@@ -57,6 +60,9 @@ static void jpeg_image_free(struct rb_jpeg_image *p) {
|
|
57
60
|
if (p->error) {
|
58
61
|
xfree(p->error);
|
59
62
|
}
|
63
|
+
if (p->fp) {
|
64
|
+
fclose(p->fp);
|
65
|
+
}
|
60
66
|
xfree(p);
|
61
67
|
}
|
62
68
|
|
@@ -64,7 +70,6 @@ static VALUE jpeg_image_s_open(int argc, VALUE *argv, VALUE self) {
|
|
64
70
|
VALUE path;
|
65
71
|
VALUE jpeg;
|
66
72
|
struct rb_jpeg_image *p_jpeg;
|
67
|
-
FILE *fp;
|
68
73
|
char *filename;
|
69
74
|
rb_scan_args(argc, argv, "1", &path);
|
70
75
|
Check_Type(path, T_STRING);
|
@@ -73,27 +78,25 @@ static VALUE jpeg_image_s_open(int argc, VALUE *argv, VALUE self) {
|
|
73
78
|
Data_Get_Struct(jpeg, struct rb_jpeg_image, p_jpeg);
|
74
79
|
filename = StringValuePtr(path);
|
75
80
|
|
76
|
-
if ((fp = fopen(filename, "rb")) == NULL) {
|
81
|
+
if ((p_jpeg->fp = fopen(filename, "rb")) == NULL) {
|
77
82
|
rb_raise(rb_eJpegError, "Open file failed: %s", filename);
|
78
83
|
}
|
79
|
-
jpeg_stdio_src(p_jpeg->read, fp);
|
84
|
+
jpeg_stdio_src(p_jpeg->read, p_jpeg->fp);
|
80
85
|
|
81
86
|
jpeg_read_header(p_jpeg->read, TRUE);
|
82
|
-
jpeg_start_decompress(p_jpeg->read);
|
83
|
-
fclose(fp);
|
84
87
|
return jpeg;
|
85
88
|
}
|
86
89
|
|
87
90
|
static VALUE jpeg_image_width(VALUE self) {
|
88
91
|
struct rb_jpeg_image *p_jpeg;
|
89
92
|
Data_Get_Struct(self, struct rb_jpeg_image, p_jpeg);
|
90
|
-
return rb_int_new(p_jpeg->read->
|
93
|
+
return rb_int_new(p_jpeg->read->image_width);
|
91
94
|
}
|
92
95
|
|
93
96
|
static VALUE jpeg_image_height(VALUE self) {
|
94
97
|
struct rb_jpeg_image *p_jpeg;
|
95
98
|
Data_Get_Struct(self, struct rb_jpeg_image, p_jpeg);
|
96
|
-
return rb_int_new(p_jpeg->read->
|
99
|
+
return rb_int_new(p_jpeg->read->image_height);
|
97
100
|
}
|
98
101
|
|
99
102
|
static VALUE jpeg_image_size(VALUE self) {
|
@@ -102,8 +105,8 @@ static VALUE jpeg_image_size(VALUE self) {
|
|
102
105
|
Data_Get_Struct(self, struct rb_jpeg_image, p_jpeg);
|
103
106
|
|
104
107
|
array = rb_ary_new();
|
105
|
-
rb_ary_push(array, rb_int_new(p_jpeg->read->
|
106
|
-
rb_ary_push(array, rb_int_new(p_jpeg->read->
|
108
|
+
rb_ary_push(array, rb_int_new(p_jpeg->read->image_width));
|
109
|
+
rb_ary_push(array, rb_int_new(p_jpeg->read->image_height));
|
107
110
|
|
108
111
|
return array;
|
109
112
|
}
|
@@ -112,5 +115,57 @@ static VALUE jpeg_image_color_info(VALUE self) {
|
|
112
115
|
struct rb_jpeg_image *p_jpeg;
|
113
116
|
|
114
117
|
Data_Get_Struct(self, struct rb_jpeg_image, p_jpeg);
|
115
|
-
return ID2SYM(rb_intern( p_jpeg->read->
|
118
|
+
return ID2SYM(rb_intern( p_jpeg->read->out_color_space == JCS_GRAYSCALE ? "gray" : "rgb" ));
|
119
|
+
}
|
120
|
+
|
121
|
+
static VALUE jpeg_image_raw_data(VALUE self) {
|
122
|
+
struct rb_jpeg_image *p_jpeg;
|
123
|
+
VALUE matrix;
|
124
|
+
VALUE line;
|
125
|
+
VALUE point;
|
126
|
+
int64_t line_size;
|
127
|
+
int64_t i;
|
128
|
+
JSAMPARRAY buffer;
|
129
|
+
|
130
|
+
matrix = rb_iv_get(self, "@raw_data");
|
131
|
+
|
132
|
+
if (RTEST(matrix)) {
|
133
|
+
return matrix;
|
134
|
+
}
|
135
|
+
|
136
|
+
Data_Get_Struct(self, struct rb_jpeg_image, p_jpeg);
|
137
|
+
|
138
|
+
jpeg_start_decompress(p_jpeg->read);
|
139
|
+
|
140
|
+
line_size = p_jpeg->read->output_width * p_jpeg->read->out_color_components;
|
141
|
+
if ((buffer = (*p_jpeg->read->mem->alloc_sarray)((j_common_ptr) p_jpeg->read, JPOOL_IMAGE, line_size, 1)) == NULL) {
|
142
|
+
rb_raise(rb_eJpegError, "Could not allocate memory (%ld bytes) to decode the image", line_size);
|
143
|
+
}
|
144
|
+
|
145
|
+
matrix = rb_ary_new();
|
146
|
+
while (p_jpeg->read->output_scanline < p_jpeg->read->output_height) {
|
147
|
+
jpeg_read_scanlines(p_jpeg->read, buffer , 1);
|
148
|
+
|
149
|
+
line = rb_ary_new();
|
150
|
+
if (p_jpeg->read->out_color_components == 3) {
|
151
|
+
for (i = 0; i < line_size; i += 3) {
|
152
|
+
point = rb_ary_new();
|
153
|
+
rb_ary_push(point, rb_int_new(buffer[0][i]));
|
154
|
+
rb_ary_push(point, rb_int_new(buffer[0][i + 1]));
|
155
|
+
rb_ary_push(point, rb_int_new(buffer[0][i + 2]));
|
156
|
+
rb_ary_push(line, point);
|
157
|
+
}
|
158
|
+
} else {
|
159
|
+
for (i = 0; i < line_size; i++) {
|
160
|
+
rb_ary_push(line, rb_int_new(buffer[0][i]));
|
161
|
+
}
|
162
|
+
}
|
163
|
+
|
164
|
+
rb_ary_push(matrix, line);
|
165
|
+
}
|
166
|
+
jpeg_finish_decompress(p_jpeg->read);
|
167
|
+
|
168
|
+
rb_iv_set(self, "@raw_data", matrix);
|
169
|
+
|
170
|
+
return matrix;
|
116
171
|
}
|
data/ext/jpeg/jpeg_image.h
CHANGED
data/lib/jpeg/version.rb
CHANGED
data/spec/jpeg_spec.rb
CHANGED
@@ -16,6 +16,14 @@ describe "Jpeg" do
|
|
16
16
|
it { expect(subject.color_info).to eq :rgb }
|
17
17
|
it { expect(subject).to be_rgb }
|
18
18
|
it { expect(subject).not_to be_gray }
|
19
|
+
it "should export the decoded data" do
|
20
|
+
decoded = subject.raw_data
|
21
|
+
expect(decoded.count).to eq 112
|
22
|
+
expect(decoded[0].count).to eq 112
|
23
|
+
expect(decoded[0][0]).to eq [255, 255, 253]
|
24
|
+
expect(decoded[60][50]).to eq [16, 27, 207]
|
25
|
+
expect(decoded[111][111]).to eq [255, 255, 255]
|
26
|
+
end
|
19
27
|
end
|
20
28
|
|
21
29
|
context "non-exists file" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jpeg
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- masarakki
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-03-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|