jpeg 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5d53a185f8b43438f663a8e6515c32e76913c618
4
- data.tar.gz: 1ae8dc89c3d59e39daa72326ec5d1baabb0545c0
3
+ metadata.gz: 2b512a8178edabb2a12690e9a276ad5606e0a80b
4
+ data.tar.gz: aaba7c6ba93a72a70523022f8ec9755520773172
5
5
  SHA512:
6
- metadata.gz: 293cb450233a0c2b2d0a5b915031853f0633ce95ac8f21d5f65fad003489ce214af197a7b3bc48eff4d06e70164f3d3152e4b5fe5deacb92e0a47447445bba10
7
- data.tar.gz: 8dd0300b215a2094ac3b4f812ec09a1d0671b313b99eb658ce11d7c3ca5909bc4ea165178ec5139085d38a49118cc0f758fac5f998e38bcce34676bf77fd0c59
6
+ metadata.gz: d161cfb5425cd64037ae7f264e374a97a1a232dfc9215cb36f65b26c29601e5e5e713c88813b9594c72db7dda9f934e33e81f0807c7da854d8fe604046e5cfc6
7
+ data.tar.gz: 25122f30d0b14743f57007162fc4c223e507f2f257120c3d36cc55d7ba4b4b65160e90bd5f215ae790281ab5843de58b7c9937ace6b46102052f3b9d033af697
@@ -7,13 +7,11 @@ extern VALUE rb_mJpeg;
7
7
  VALUE rb_cJpegImage;
8
8
 
9
9
  static VALUE jpeg_image_s_open(int argc, VALUE *argv, VALUE sef);
10
+ static VALUE jpeg_image_s_open_buffer(int argc, VALUE *argv, VALUE sef);
10
11
  static VALUE jpeg_image_alloc(VALUE klass);
11
12
  static void jpeg_image_mark(struct rb_jpeg_image *p);
12
13
  static void jpeg_image_free(struct rb_jpeg_image *p);
13
- static VALUE jpeg_image_width(VALUE self);
14
- static VALUE jpeg_image_height(VALUE self);
15
14
  static VALUE jpeg_image_size(VALUE self);
16
- static VALUE jpeg_image_color_info(VALUE self);
17
15
  static VALUE jpeg_image_raw_data(VALUE self);
18
16
 
19
17
  extern VALUE rb_eJpegError;
@@ -22,10 +20,12 @@ void Init_jpeg_image(void) {
22
20
  rb_cJpegImage = rb_define_class_under(rb_mJpeg, "Image", rb_cObject);
23
21
  rb_define_alloc_func(rb_cJpegImage, jpeg_image_alloc);
24
22
  rb_define_singleton_method(rb_cJpegImage, "open", jpeg_image_s_open, -1);
25
- rb_define_method(rb_cJpegImage, "width", jpeg_image_width, 0);
26
- rb_define_method(rb_cJpegImage, "height", jpeg_image_height, 0);
23
+ rb_define_singleton_method(rb_cJpegImage, "open_buffer", jpeg_image_s_open_buffer, -1);
24
+ rb_attr(rb_cJpegImage, rb_intern("width"), 1, 0, 0);
25
+ rb_attr(rb_cJpegImage, rb_intern("height"), 1, 0, 0);
26
+ rb_attr(rb_cJpegImage, rb_intern("color_info"), 1, 0, 0);
27
+
27
28
  rb_define_method(rb_cJpegImage, "size", jpeg_image_size, 0);
28
- rb_define_method(rb_cJpegImage, "color_info", jpeg_image_color_info, 0);
29
29
  rb_define_method(rb_cJpegImage, "raw_data", jpeg_image_raw_data, 0);
30
30
  }
31
31
 
@@ -43,7 +43,6 @@ static VALUE jpeg_image_alloc(VALUE klass) {
43
43
  jpeg->error = (void *)ALLOC(struct jpeg_error_mgr);
44
44
  jpeg->read->err = jpeg_std_error(jpeg->error);
45
45
  jpeg->error->error_exit = jpeg_image_exit;
46
- jpeg_create_decompress(jpeg->read);
47
46
 
48
47
  return Data_Wrap_Struct(klass, jpeg_image_mark, jpeg_image_free, jpeg);
49
48
  }
@@ -54,7 +53,6 @@ static void jpeg_image_mark(struct rb_jpeg_image *p) {
54
53
 
55
54
  static void jpeg_image_free(struct rb_jpeg_image *p) {
56
55
  if (p->read) {
57
- jpeg_destroy_decompress(p->read);
58
56
  xfree(p->read);
59
57
  }
60
58
  if (p->error) {
@@ -66,56 +64,91 @@ static void jpeg_image_free(struct rb_jpeg_image *p) {
66
64
  xfree(p);
67
65
  }
68
66
 
69
- static VALUE jpeg_image_s_open(int argc, VALUE *argv, VALUE self) {
70
- VALUE path;
67
+ static void jpeg_image_s_set_read_source(struct rb_jpeg_image *p_jpeg) {
68
+
69
+ if (p_jpeg->filename) {
70
+
71
+ if ((p_jpeg->fp = fopen(p_jpeg->filename, "rb")) == NULL) {
72
+ rb_raise(rb_eJpegError, "Open file failed: %s", p_jpeg->filename);
73
+ }
74
+
75
+ } else {
76
+
77
+ #ifdef jpeg_mem_src
78
+ jpeg_mem_src(p_jpeg->read, (unsigned char *) p_jpeg->buffer, p_jpeg->buffer_len);
79
+ #else
80
+ if ((p_jpeg->fp = fmemopen(p_jpeg->buffer, p_jpeg->buffer_len, "rb")) == NULL) {
81
+ rb_raise(rb_eJpegError, "Could not read jpeg from buffer");
82
+ }
83
+ #endif
84
+
85
+ }
86
+
87
+ if (p_jpeg->fp) {
88
+ jpeg_stdio_src(p_jpeg->read, p_jpeg->fp);
89
+ }
90
+ }
91
+
92
+ static VALUE jpeg_image_s_get_image_values(char *filename, char *buffer, int64_t buffer_len, VALUE self) {
71
93
  VALUE jpeg;
72
94
  struct rb_jpeg_image *p_jpeg;
73
- char *filename;
74
- rb_scan_args(argc, argv, "1", &path);
75
- Check_Type(path, T_STRING);
76
95
 
77
- jpeg = rb_funcall(rb_cJpegImage, rb_intern("new"), 0);
96
+ jpeg = rb_obj_alloc(rb_cJpegImage);
97
+ rb_obj_call_init(jpeg, 0, NULL);
98
+
78
99
  Data_Get_Struct(jpeg, struct rb_jpeg_image, p_jpeg);
79
- filename = StringValuePtr(path);
80
100
 
81
- if ((p_jpeg->fp = fopen(filename, "rb")) == NULL) {
82
- rb_raise(rb_eJpegError, "Open file failed: %s", filename);
83
- }
84
- jpeg_stdio_src(p_jpeg->read, p_jpeg->fp);
101
+ p_jpeg->filename = filename;
102
+ p_jpeg->buffer = buffer;
103
+ p_jpeg->buffer_len = buffer_len;
104
+
105
+ jpeg_create_decompress(p_jpeg->read);
106
+
107
+ jpeg_image_s_set_read_source(p_jpeg);
85
108
 
86
109
  jpeg_read_header(p_jpeg->read, TRUE);
110
+
111
+ rb_iv_set(jpeg, "@width", LONG2NUM(p_jpeg->read->image_width));
112
+ rb_iv_set(jpeg, "@height", LONG2NUM(p_jpeg->read->image_height));
113
+ rb_iv_set(jpeg, "@color_info", ID2SYM(rb_intern( p_jpeg->read->out_color_space == JCS_GRAYSCALE ? "gray" : "rgb" )));
114
+
115
+ jpeg_destroy_decompress(p_jpeg->read);
116
+
117
+ if (p_jpeg->fp) {
118
+ fclose(p_jpeg->fp);
119
+ p_jpeg->fp = NULL;
120
+ }
121
+
87
122
  return jpeg;
88
123
  }
89
124
 
90
- static VALUE jpeg_image_width(VALUE self) {
91
- struct rb_jpeg_image *p_jpeg;
92
- Data_Get_Struct(self, struct rb_jpeg_image, p_jpeg);
93
- return rb_int_new(p_jpeg->read->image_width);
125
+ static VALUE jpeg_image_s_open(int argc, VALUE *argv, VALUE self) {
126
+ VALUE path;
127
+
128
+ rb_scan_args(argc, argv, "1", &path);
129
+ Check_Type(path, T_STRING);
130
+
131
+ return jpeg_image_s_get_image_values(StringValuePtr(path), NULL, 0, self);
94
132
  }
95
133
 
96
- static VALUE jpeg_image_height(VALUE self) {
97
- struct rb_jpeg_image *p_jpeg;
98
- Data_Get_Struct(self, struct rb_jpeg_image, p_jpeg);
99
- return rb_int_new(p_jpeg->read->image_height);
134
+ static VALUE jpeg_image_s_open_buffer(int argc, VALUE *argv, VALUE self) {
135
+ VALUE str;
136
+
137
+ rb_scan_args(argc, argv, "1", &str);
138
+ Check_Type(str, T_STRING);
139
+
140
+ return jpeg_image_s_get_image_values(NULL, StringValuePtr(str), RSTRING_LEN(str), self);
100
141
  }
101
142
 
102
143
  static VALUE jpeg_image_size(VALUE self) {
103
- struct rb_jpeg_image *p_jpeg;
104
144
  VALUE array;
105
- Data_Get_Struct(self, struct rb_jpeg_image, p_jpeg);
106
145
 
107
146
  array = rb_ary_new();
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));
110
147
 
111
- return array;
112
- }
113
-
114
- static VALUE jpeg_image_color_info(VALUE self) {
115
- struct rb_jpeg_image *p_jpeg;
148
+ rb_ary_push(array, rb_iv_get(self, "@width"));
149
+ rb_ary_push(array, rb_iv_get(self, "@height"));
116
150
 
117
- Data_Get_Struct(self, struct rb_jpeg_image, p_jpeg);
118
- return ID2SYM(rb_intern( p_jpeg->read->out_color_space == JCS_GRAYSCALE ? "gray" : "rgb" ));
151
+ return array;
119
152
  }
120
153
 
121
154
  static VALUE jpeg_image_raw_data(VALUE self) {
@@ -135,6 +168,12 @@ static VALUE jpeg_image_raw_data(VALUE self) {
135
168
 
136
169
  Data_Get_Struct(self, struct rb_jpeg_image, p_jpeg);
137
170
 
171
+ jpeg_create_decompress(p_jpeg->read);
172
+
173
+ jpeg_image_s_set_read_source(p_jpeg);
174
+
175
+ jpeg_read_header(p_jpeg->read, TRUE);
176
+
138
177
  jpeg_start_decompress(p_jpeg->read);
139
178
 
140
179
  line_size = p_jpeg->read->output_width * p_jpeg->read->out_color_components;
@@ -165,6 +204,13 @@ static VALUE jpeg_image_raw_data(VALUE self) {
165
204
  }
166
205
  jpeg_finish_decompress(p_jpeg->read);
167
206
 
207
+ jpeg_destroy_decompress(p_jpeg->read);
208
+
209
+ if (p_jpeg->fp) {
210
+ fclose(p_jpeg->fp);
211
+ p_jpeg->fp = NULL;
212
+ }
213
+
168
214
  rb_iv_set(self, "@raw_data", matrix);
169
215
 
170
216
  return matrix;
@@ -8,6 +8,9 @@ struct rb_jpeg_image {
8
8
  struct jpeg_decompress_struct *read;
9
9
  struct jpeg_error_mgr *error;
10
10
  FILE *fp;
11
+ char *filename;
12
+ char *buffer;
13
+ int64_t buffer_len;
11
14
  };
12
15
 
13
16
  void Init_jpeg_image();
@@ -5,13 +5,7 @@ require 'jpeg/image'
5
5
 
6
6
  module Jpeg
7
7
  def self.open_buffer(str)
8
- tmp = Tempfile::new("ruby-jpeg")
9
- tmp.set_encoding Encoding::BINARY if tmp.respond_to?(:set_encoding)
10
- tmp.puts str
11
- tmp.rewind
12
- jpeg = Jpeg::Image.open(tmp.path)
13
- tmp.close
14
- jpeg
8
+ Jpeg::Image.open_buffer(str)
15
9
  end
16
10
 
17
11
  def self.open(file)
@@ -1,3 +1,3 @@
1
1
  module Jpeg
2
- VERSION = '0.6.0'
2
+ VERSION = '0.7.0'
3
3
  end
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift File.expand_path('../lib', File.dirname(__FILE__))
4
+ require 'jpeg'
5
+
6
+ puts Process.pid
7
+ file = File.expand_path('samples/sample.jpg', File.dirname(__FILE__))
8
+
9
+ [10, 100, 1000].each do |n|
10
+ n.times do
11
+ @jpeg = Jpeg.open(file).raw_data
12
+ end
13
+ @jpeg = nil
14
+ GC.start
15
+ puts "#{n} times"
16
+ puts `pmap -x #{Process.pid} | tail -n 1`
17
+ # puts `lsof -p #{Process.pid}`
18
+ end
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.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - masarakki
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-01 00:00:00.000000000 Z
11
+ date: 2015-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -88,7 +88,6 @@ extensions:
88
88
  - ext/jpeg/extconf.rb
89
89
  extra_rdoc_files: []
90
90
  files:
91
- - ".document"
92
91
  - ".gitignore"
93
92
  - ".rspec"
94
93
  - ".travis.yml"
@@ -96,7 +95,6 @@ files:
96
95
  - LICENSE.txt
97
96
  - README.md
98
97
  - Rakefile
99
- - VERSION
100
98
  - ext/jpeg/extconf.rb
101
99
  - ext/jpeg/jpeg.c
102
100
  - ext/jpeg/jpeg.h
@@ -109,6 +107,7 @@ files:
109
107
  - lib/jpeg/image.rb
110
108
  - lib/jpeg/version.rb
111
109
  - spec/jpeg_spec.rb
110
+ - spec/leak_test.rb
112
111
  - spec/samples/sample.jpg
113
112
  - spec/samples/sample.png
114
113
  - spec/spec_helper.rb
@@ -138,6 +137,7 @@ specification_version: 4
138
137
  summary: libjpeg wrapper for ruby
139
138
  test_files:
140
139
  - spec/jpeg_spec.rb
140
+ - spec/leak_test.rb
141
141
  - spec/samples/sample.jpg
142
142
  - spec/samples/sample.png
143
143
  - spec/spec_helper.rb
data/.document DELETED
@@ -1,5 +0,0 @@
1
- lib/**/*.rb
2
- bin/*
3
- -
4
- features/**/*.feature
5
- LICENSE.txt
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.3.3