jpeg 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a38f85970789b149b8328172ba08dcd4ace974bf
4
- data.tar.gz: a92cf8cd4a996d755cdd69c913f3ffcce965421e
3
+ metadata.gz: 17514bf49f64437e58f047e73f51f8aa910d419f
4
+ data.tar.gz: bad9d9450380e4d9f31c0ba2dbc3e1a6a434bbf9
5
5
  SHA512:
6
- metadata.gz: 2e54c57d3c1bb861f3ac3788ec9e37720c995ac9ee33d710c09478574a3bf493c823d7ae1b9e8015889da2b7e8c716dc2bd1dab035d5c523eda65beb3eaf0394
7
- data.tar.gz: 17fc49472ca3c05554c875bb67df6ae9e5ba7bbdc9342fe462aa1c7ab57b46bf1071a06af1e0193c1d52be8c6809cc31ffef20b5e5730c5ded8128e0884a5650
6
+ metadata.gz: e64f1099daaea7eee6c8b2e4ea3158550015a0117e5d804881a249c73ca207024b5ef094d9b72d46fec9fceff04b2dc982f02fb3444113907dc81ac2f86e944f
7
+ data.tar.gz: 241da4820a95b3feaa7820526c8ea62d5fa71c27efc302ac7aa669f07884f23196f8f61cc102d83b4f03529d648f4c5a7f1e08c210412c991deb5b6c7b4df902
data/ext/jpeg/jpeg.c CHANGED
@@ -1,8 +1,12 @@
1
1
  #include "jpeg.h"
2
- #include "jpeg_jpeg.h"
2
+ #include "jpeg_image.h"
3
3
  #include "jpeg_error.h"
4
4
 
5
- void Init_jpeg() {
6
- Init_jpeg_jpeg();
5
+ VALUE rb_mJpeg;
6
+
7
+ void
8
+ Init_jpeg(void) {
9
+ rb_mJpeg = rb_define_module("Jpeg");
10
+ Init_jpeg_image();
7
11
  Init_jpeg_error();
8
12
  }
data/ext/jpeg/jpeg.h CHANGED
@@ -1,8 +1,6 @@
1
- #ifndef __JPEG_IMAGE_
2
- #define __JPEG_IMAGE_
1
+ #ifndef RB_JPEG_H
2
+ #define RB_JPEG_H 1
3
3
 
4
4
  #include "ruby.h"
5
5
 
6
- void Init_jpeg();
7
-
8
6
  #endif
@@ -2,9 +2,9 @@
2
2
  #include "jpeg_error.h"
3
3
  #include "ruby.h"
4
4
 
5
- extern VALUE Jpeg;
6
- VALUE Jpeg_Error;
5
+ extern VALUE rb_mJpeg;
6
+ VALUE rb_eJpegError;
7
7
 
8
8
  void Init_jpeg_error() {
9
- Jpeg_Error = rb_define_class_under(Jpeg, "Error", rb_eStandardError);
9
+ rb_eJpegError = rb_define_class_under(rb_mJpeg, "Error", rb_eStandardError);
10
10
  }
@@ -1,5 +1,5 @@
1
- #ifndef __JPEG_ERROR_H__
2
- #define __JPEG_ERROR_H__
1
+ #ifndef RB_JPEG_ERROR
2
+ #define RB_JPEG_ERROR_H 1
3
3
 
4
4
  void Init_jpeg_error();
5
5
 
@@ -0,0 +1,116 @@
1
+ #include <stdio.h>
2
+ #include <jpeglib.h>
3
+ #include "jpeg_image.h"
4
+ #include "jpeg_error.h"
5
+
6
+ extern VALUE rb_mJpeg;
7
+ VALUE rb_cJpegImage;
8
+
9
+ static VALUE jpeg_image_s_open(int argc, VALUE *argv, VALUE sef);
10
+ static VALUE jpeg_image_alloc(VALUE klass);
11
+ static void jpeg_image_mark(struct rb_jpeg_image *p);
12
+ 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
+ static VALUE jpeg_image_size(VALUE self);
16
+ static VALUE jpeg_image_color_info(VALUE self);
17
+
18
+ extern VALUE rb_eJpegError;
19
+
20
+ void Init_jpeg_image(void) {
21
+ rb_cJpegImage = rb_define_class_under(rb_mJpeg, "Image", rb_cObject);
22
+ rb_define_alloc_func(rb_cJpegImage, jpeg_image_alloc);
23
+ rb_define_singleton_method(rb_cJpegImage, "open", jpeg_image_s_open, -1);
24
+ rb_define_method(rb_cJpegImage, "width", jpeg_image_width, 0);
25
+ rb_define_method(rb_cJpegImage, "height", jpeg_image_height, 0);
26
+ rb_define_method(rb_cJpegImage, "size", jpeg_image_size, 0);
27
+ rb_define_method(rb_cJpegImage, "color_info", jpeg_image_color_info, 0);
28
+ }
29
+
30
+ void jpeg_image_exit(j_common_ptr jpeg) {
31
+ char buffer[JMSG_LENGTH_MAX];
32
+ jpeg->err->format_message(jpeg, buffer);
33
+ rb_raise(rb_eJpegError, buffer);
34
+ }
35
+
36
+ static VALUE jpeg_image_alloc(VALUE klass) {
37
+ struct rb_jpeg_image *jpeg = ALLOC(struct rb_jpeg_image);
38
+
39
+ jpeg->read = (void *)ALLOC(struct jpeg_decompress_struct);
40
+ jpeg->error = (void *)ALLOC(struct jpeg_error_mgr);
41
+ jpeg->read->err = jpeg_std_error(jpeg->error);
42
+ jpeg->error->error_exit = jpeg_image_exit;
43
+ jpeg_create_decompress(jpeg->read);
44
+
45
+ return Data_Wrap_Struct(klass, jpeg_image_mark, jpeg_image_free, jpeg);
46
+ }
47
+
48
+ static void jpeg_image_mark(struct rb_jpeg_image *p) {
49
+
50
+ }
51
+
52
+ static void jpeg_image_free(struct rb_jpeg_image *p) {
53
+ if (p->read) {
54
+ jpeg_destroy_decompress(p->read);
55
+ xfree(p->read);
56
+ }
57
+ if (p->error) {
58
+ xfree(p->error);
59
+ }
60
+ xfree(p);
61
+ }
62
+
63
+ static VALUE jpeg_image_s_open(int argc, VALUE *argv, VALUE self) {
64
+ VALUE path;
65
+ VALUE jpeg;
66
+ struct rb_jpeg_image *p_jpeg;
67
+ FILE *fp;
68
+ char *filename;
69
+ rb_scan_args(argc, argv, "1", &path);
70
+ Check_Type(path, T_STRING);
71
+
72
+ jpeg = rb_funcall(rb_cJpegImage, rb_intern("new"), 0);
73
+ Data_Get_Struct(jpeg, struct rb_jpeg_image, p_jpeg);
74
+ filename = StringValuePtr(path);
75
+
76
+ if ((fp = fopen(filename, "rb")) == NULL) {
77
+ rb_raise(rb_eJpegError, "Open file failed: %s", filename);
78
+ }
79
+ jpeg_stdio_src(p_jpeg->read, fp);
80
+
81
+ jpeg_read_header(p_jpeg->read, TRUE);
82
+ jpeg_start_decompress(p_jpeg->read);
83
+ fclose(fp);
84
+ return jpeg;
85
+ }
86
+
87
+ static VALUE jpeg_image_width(VALUE self) {
88
+ struct rb_jpeg_image *p_jpeg;
89
+ Data_Get_Struct(self, struct rb_jpeg_image, p_jpeg);
90
+ return rb_int_new(p_jpeg->read->output_width);
91
+ }
92
+
93
+ static VALUE jpeg_image_height(VALUE self) {
94
+ struct rb_jpeg_image *p_jpeg;
95
+ Data_Get_Struct(self, struct rb_jpeg_image, p_jpeg);
96
+ return rb_int_new(p_jpeg->read->output_height);
97
+ }
98
+
99
+ static VALUE jpeg_image_size(VALUE self) {
100
+ struct rb_jpeg_image *p_jpeg;
101
+ VALUE array;
102
+ Data_Get_Struct(self, struct rb_jpeg_image, p_jpeg);
103
+
104
+ array = rb_ary_new();
105
+ rb_ary_push(array, rb_int_new(p_jpeg->read->output_width));
106
+ rb_ary_push(array, rb_int_new(p_jpeg->read->output_height));
107
+
108
+ return array;
109
+ }
110
+
111
+ static VALUE jpeg_image_color_info(VALUE self) {
112
+ struct rb_jpeg_image *p_jpeg;
113
+
114
+ Data_Get_Struct(self, struct rb_jpeg_image, p_jpeg);
115
+ return ID2SYM(rb_intern( p_jpeg->read->out_color_components == 3 ? "rgb" : "gray" ));
116
+ }
@@ -1,14 +1,14 @@
1
- #ifndef __JPEG_JPEG_
2
- #define __JPEG_JPEG_
1
+ #ifndef RB_JPEG_JPEG_H
2
+ #define RB_JPEG_JPEG_H 1
3
3
  #include <jpeglib.h>
4
4
  #include "jpeg.h"
5
5
  #include "ruby.h"
6
6
 
7
- struct jpeg_jpeg {
7
+ struct rb_jpeg_image {
8
8
  struct jpeg_decompress_struct *read;
9
9
  struct jpeg_error_mgr *error;
10
10
  };
11
11
 
12
- void Init_jpeg_jpeg();
12
+ void Init_jpeg_image();
13
13
 
14
14
  #endif
data/jpeg.gemspec CHANGED
@@ -22,5 +22,6 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "bundler", "~> 1.6"
23
23
  spec.add_development_dependency "rake"
24
24
  spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "pry"
25
26
  spec.add_development_dependency "rake-compiler"
26
27
  end
data/lib/jpeg.rb CHANGED
@@ -1,22 +1,20 @@
1
1
  require 'tempfile'
2
+ require 'jpeg/version'
2
3
  require 'jpeg/jpeg'
4
+ require 'jpeg/image'
3
5
 
4
- class Jpeg
6
+ module Jpeg
5
7
  def self.open_buffer(str)
6
8
  tmp = Tempfile::new("ruby-jpeg")
7
9
  tmp.set_encoding Encoding::BINARY if tmp.respond_to?(:set_encoding)
8
10
  tmp.puts str
9
11
  tmp.rewind
10
- jpeg = Jpeg.open(tmp.path)
12
+ jpeg = Jpeg::Image.open(tmp.path)
11
13
  tmp.close
12
14
  jpeg
13
15
  end
14
16
 
15
- def rgb?
16
- color_info == :rgb
17
- end
18
-
19
- def gray?
20
- color_info == :gray
17
+ def self.open(file)
18
+ Jpeg::Image.open(file)
21
19
  end
22
20
  end
data/lib/jpeg/image.rb ADDED
@@ -0,0 +1,11 @@
1
+ module Jpeg
2
+ class Image
3
+ def rgb?
4
+ color_info == :rgb
5
+ end
6
+
7
+ def gray?
8
+ color_info == :gray
9
+ end
10
+ end
11
+ end
data/lib/jpeg/version.rb CHANGED
@@ -1,3 +1,3 @@
1
- class Jpeg
2
- VERSION = "0.4.0"
1
+ module Jpeg
2
+ VERSION = "0.5.0"
3
3
  end
data/spec/jpeg_spec.rb CHANGED
@@ -9,31 +9,25 @@ describe "Jpeg" do
9
9
  describe :open do
10
10
  context "valid jpeg" do
11
11
  before { @jpeg = Jpeg.open(sample_file_path("sample.jpg")) }
12
- its(:size) { should == [112, 112] }
13
- its(:width) { should == 112 }
14
- its(:height) { should == 112 }
15
- its(:color_info) { should == :rgb }
16
- its(:rgb?) { should be_true }
17
- its(:gray?) { should be_false }
12
+ it { expect(subject).to be_a Jpeg::Image }
13
+ it { expect(subject.size).to eq [112, 112] }
14
+ it { expect(subject.width).to eq 112 }
15
+ it { expect(subject.height).to eq 112 }
16
+ it { expect(subject.color_info).to eq :rgb }
17
+ it { expect(subject).to be_rgb }
18
+ it { expect(subject).not_to be_gray }
18
19
  end
20
+
19
21
  context "non-exists file" do
20
- it {
21
- lambda {
22
- Jpeg.open(sample_file_path("nonexists.jpg"))
23
- }.should raise_error
24
- }
22
+ it { expect { Jpeg.open(sample_file_path("nonexists.jpg")) }.to raise_error(Jpeg::Error) }
25
23
  end
26
24
 
27
25
  context "not a correct jpeg file" do
28
- it {
29
- lambda {
30
- Jpeg.open(sample_file_path("sample.png"))
31
- }.should raise_error
32
- }
26
+ it { expect { Jpeg.open(sample_file_path("sample.png")) }.to raise_error(Jpeg::Error) }
33
27
  end
34
28
  end
35
29
  describe :from_string do
36
30
  before { @jpeg = Jpeg.open_buffer(File.open(sample_file_path("sample.jpg")).read) }
37
- its(:size) { should == [112, 112] }
31
+ it { expect(subject.size).to eq [112, 112] }
38
32
  end
39
33
  end
data/spec/spec_helper.rb CHANGED
@@ -1,12 +1,3 @@
1
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- $LOAD_PATH.unshift(File.dirname(__FILE__))
3
- require 'rspec'
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
4
2
  require 'jpeg'
5
-
6
- # Requires supporting files with custom matchers and macros, etc,
7
- # in ./support/ and its subdirectories.
8
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
-
10
- RSpec.configure do |config|
11
-
12
- end
3
+ require 'pry'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jpeg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - masarakki
@@ -14,56 +14,70 @@ dependencies:
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.6'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.6'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: rake-compiler
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
- - - '>='
73
+ - - ">="
60
74
  - !ruby/object:Gem::Version
61
75
  version: '0'
62
76
  type: :development
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
- - - '>='
80
+ - - ">="
67
81
  - !ruby/object:Gem::Version
68
82
  version: '0'
69
83
  description: libjpeg wrapper for ruby
@@ -74,10 +88,10 @@ extensions:
74
88
  - ext/jpeg/extconf.rb
75
89
  extra_rdoc_files: []
76
90
  files:
77
- - .document
78
- - .gitignore
79
- - .rspec
80
- - .travis.yml
91
+ - ".document"
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - ".travis.yml"
81
95
  - Gemfile
82
96
  - LICENSE.txt
83
97
  - README.md
@@ -88,10 +102,11 @@ files:
88
102
  - ext/jpeg/jpeg.h
89
103
  - ext/jpeg/jpeg_error.c
90
104
  - ext/jpeg/jpeg_error.h
91
- - ext/jpeg/jpeg_jpeg.c
92
- - ext/jpeg/jpeg_jpeg.h
105
+ - ext/jpeg/jpeg_image.c
106
+ - ext/jpeg/jpeg_image.h
93
107
  - jpeg.gemspec
94
108
  - lib/jpeg.rb
109
+ - lib/jpeg/image.rb
95
110
  - lib/jpeg/version.rb
96
111
  - spec/jpeg_spec.rb
97
112
  - spec/samples/sample.jpg
@@ -107,12 +122,12 @@ require_paths:
107
122
  - lib
108
123
  required_ruby_version: !ruby/object:Gem::Requirement
109
124
  requirements:
110
- - - '>='
125
+ - - ">="
111
126
  - !ruby/object:Gem::Version
112
127
  version: '0'
113
128
  required_rubygems_version: !ruby/object:Gem::Requirement
114
129
  requirements:
115
- - - '>='
130
+ - - ">="
116
131
  - !ruby/object:Gem::Version
117
132
  version: '0'
118
133
  requirements: []
data/ext/jpeg/jpeg_jpeg.c DELETED
@@ -1,114 +0,0 @@
1
- #include <stdio.h>
2
- #include <jpeglib.h>
3
- #include "jpeg_jpeg.h"
4
- #include "jpeg_error.h"
5
-
6
- VALUE Jpeg;
7
-
8
- static VALUE jpeg_jpeg_s_open(int argc, VALUE *argv, VALUE sef);
9
- static VALUE jpeg_jpeg_alloc(VALUE klass);
10
- static void jpeg_jpeg_mark(struct jpeg_jpeg *p);
11
- static void jpeg_jpeg_free(struct jpeg_jpeg *p);
12
- static VALUE jpeg_jpeg_width(VALUE self);
13
- static VALUE jpeg_jpeg_height(VALUE self);
14
- static VALUE jpeg_jpeg_size(VALUE self);
15
- static VALUE jpeg_jpeg_color_info(VALUE self);
16
-
17
- extern VALUE Jpeg_Error;
18
-
19
- void Init_jpeg_jpeg() {
20
- Jpeg = rb_define_class("Jpeg", rb_cObject);
21
- rb_define_alloc_func(Jpeg, jpeg_jpeg_alloc);
22
- rb_define_singleton_method(Jpeg, "open", jpeg_jpeg_s_open, -1);
23
- rb_define_method(Jpeg, "width", jpeg_jpeg_width, 0);
24
- rb_define_method(Jpeg, "height", jpeg_jpeg_height, 0);
25
- rb_define_method(Jpeg, "size", jpeg_jpeg_size, 0);
26
- rb_define_method(Jpeg, "color_info", jpeg_jpeg_color_info, 0);
27
- }
28
-
29
- void jpeg_jpeg_exit(j_common_ptr jpeg) {
30
- char buffer[JMSG_LENGTH_MAX];
31
- jpeg->err->format_message(jpeg, buffer);
32
- rb_raise(Jpeg_Error, buffer);
33
- }
34
-
35
- static VALUE jpeg_jpeg_alloc(VALUE klass) {
36
- struct jpeg_jpeg *jpeg = ALLOC(struct jpeg_jpeg);
37
-
38
- jpeg->read = (void *)ALLOC(struct jpeg_decompress_struct);
39
- jpeg->error = (void *)ALLOC(struct jpeg_error_mgr);
40
- jpeg->read->err = jpeg_std_error(jpeg->error);
41
- jpeg->error->error_exit = jpeg_jpeg_exit;
42
- jpeg_create_decompress(jpeg->read);
43
-
44
- return Data_Wrap_Struct(klass, jpeg_jpeg_mark, jpeg_jpeg_free, jpeg);
45
- }
46
-
47
- static void jpeg_jpeg_mark(struct jpeg_jpeg *p) {
48
-
49
- }
50
-
51
- static void jpeg_jpeg_free(struct jpeg_jpeg *p) {
52
- if (p->read) {
53
- jpeg_destroy_decompress(p->read);
54
- xfree(p->read);
55
- }
56
- if (p->error) {
57
- xfree(p->error);
58
- }
59
- xfree(p);
60
- }
61
-
62
- static VALUE jpeg_jpeg_s_open(int argc, VALUE *argv, VALUE self) {
63
- VALUE path;
64
- VALUE jpeg;
65
- struct jpeg_jpeg *p_jpeg;
66
- int errorp;
67
- FILE *fp;
68
- rb_scan_args(argc, argv, "1", &path);
69
- Check_Type(path, T_STRING);
70
-
71
- jpeg = rb_funcall(Jpeg, rb_intern("new"), 0);
72
- Data_Get_Struct(jpeg, struct jpeg_jpeg, p_jpeg);
73
-
74
- if ((fp = fopen(RSTRING_PTR(path), "rb")) == NULL) {
75
- rb_raise(rb_eRuntimeError, "Open file failed");
76
- }
77
- jpeg_stdio_src(p_jpeg->read, fp);
78
-
79
- jpeg_read_header(p_jpeg->read, TRUE);
80
- jpeg_start_decompress(p_jpeg->read);
81
- fclose(fp);
82
- return jpeg;
83
- }
84
-
85
- static VALUE jpeg_jpeg_width(VALUE self) {
86
- struct jpeg_jpeg *p_jpeg;
87
- Data_Get_Struct(self, struct jpeg_jpeg, p_jpeg);
88
- return rb_int_new(p_jpeg->read->output_width);
89
- }
90
-
91
- static VALUE jpeg_jpeg_height(VALUE self) {
92
- struct jpeg_jpeg *p_jpeg;
93
- Data_Get_Struct(self, struct jpeg_jpeg, p_jpeg);
94
- return rb_int_new(p_jpeg->read->output_height);
95
- }
96
-
97
- static VALUE jpeg_jpeg_size(VALUE self) {
98
- struct jpeg_jpeg *p_jpeg;
99
- VALUE array;
100
- Data_Get_Struct(self, struct jpeg_jpeg, p_jpeg);
101
-
102
- array = rb_ary_new();
103
- rb_ary_push(array, rb_int_new(p_jpeg->read->output_width));
104
- rb_ary_push(array, rb_int_new(p_jpeg->read->output_height));
105
-
106
- return array;
107
- }
108
-
109
- static VALUE jpeg_jpeg_color_info(VALUE self) {
110
- struct jpeg_jpeg *p_jpeg;
111
-
112
- Data_Get_Struct(self, struct jpeg_jpeg, p_jpeg);
113
- return ID2SYM(rb_intern( p_jpeg->read->out_color_components == 3 ? "rgb" : "gray" ));
114
- }