psd_native 0.1.0 → 0.2.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: 3455db8f83d189130541ead18b691098dd5c63b9
4
- data.tar.gz: 0a606b3f924bfa86ccfd5dc0a0f81a66868339e8
3
+ metadata.gz: ea6a18568c0167e52edc9898e46cf1c3ca7fa191
4
+ data.tar.gz: c1cb23a65ca6bb18cd23b87d6ed21f224b1ccba4
5
5
  SHA512:
6
- metadata.gz: e9d211703ff9633bb3d74f4fe9e54dfd802d7a183308e23369363d1de83183ec88d8d4de4565fae75e8c13ab9328c48522145a4bbd4a3926ef11ed963b8f4a60
7
- data.tar.gz: f10ac6b1d616c6745a9d99bbb65fd7808747b8a7bc8e80a784344e4dc4a4b698ac2233a49ae32389982053260a840a6ccf01dd5217abc6917dc153c86203a8db
6
+ metadata.gz: a13bc0db5675474519811a44b298bb94b68ae72f32fa300aa36864e973d35c56c3f6489ccd3153418aaccb3bd09e4d56243107d8b6fd51c98c4f1b9198224111
7
+ data.tar.gz: 8106d269a32bd68cb67aa8980018036dd68b2eed9946adf18c67902ace34e760a19c71bb8885a5fcc864041dfed3c47bec4bad7cd5d0ad24b1f120f389c5a903
data/.gitignore CHANGED
@@ -19,4 +19,5 @@ Makefile
19
19
  *.o
20
20
  conftest.dSYM
21
21
  .DS_Store
22
- *.bundle
22
+ *.bundle
23
+ examples/
data/Guardfile ADDED
@@ -0,0 +1,6 @@
1
+ guard 'rspec', cli: '--color' do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { "spec" }
4
+ watch(%r{^ext/(.+)$}) { "spec" }
5
+ watch('spec/spec_helper.rb') { "spec" }
6
+ end
data/README.md CHANGED
@@ -1,11 +1,11 @@
1
1
  # PSDNative
2
2
 
3
- A gem that includes multiple mixins that speed up [PSD.rb](https://github.com/layervault/psd.rb) by delegating certain parts of the library to native C code. This library allows for PSD.rb to function as a pure Ruby library, while allowing for optional native code speed improvements. It overwrites specific methods within PSD.rb transparently, so you can use PSD.rb as you normally would.
3
+ A gem that includes multiple mixins that speed up [PSD.rb](https://github.com/layervault/psd.rb) by delegating certain parts of the library to native C code. This library allows for PSD.rb to function as a pure Ruby library, while allowing for optional native code speed improvements. It overwrites specific methods within PSD.rb transparently, so you can use PSD.rb like normal.
4
4
 
5
5
  Currently, PSDNative replaces these sections of PSD.rb with native code:
6
6
 
7
7
  * RGB processing
8
- * RLE decoding (disabled; needs optimization)
8
+ * RLE decoding
9
9
 
10
10
  ## Installation
11
11
 
@@ -23,6 +23,8 @@ Or install it yourself as:
23
23
 
24
24
  ## Usage
25
25
 
26
+ Simply require `psd_native` instead of `psd`, and you're good to go.
27
+
26
28
  ``` ruby
27
29
  require 'psd_native'
28
30
 
@@ -1,56 +1,51 @@
1
1
  #include "psd_native_ext.h"
2
2
 
3
3
  VALUE psd_native_decode_rle_channel(VALUE self) {
4
+ int bytes, len, val;
5
+ int i, j, k;
6
+
4
7
  int height = FIX2INT(rb_funcall(self, rb_intern("height"), 0));
5
8
  VALUE channel_data = rb_iv_get(self, "@channel_data");
6
- int bytes, finish, len, val;
9
+ VALUE* byte_counts = RARRAY_PTR(rb_iv_get(self, "@byte_counts"));
10
+ int line_index = FIX2INT(rb_iv_get(self, "@line_index"));
11
+ int chan_pos = FIX2INT(rb_iv_get(self, "@chan_pos"));
7
12
 
8
- for (int i = 0; i < height; i++) {
9
- bytes = psd_byte_count(self, i);
10
- finish = psd_file_tell(self) + bytes;
13
+ for (i = 0; i < height; i++) {
14
+ bytes = FIX2INT(byte_counts[line_index + i]);
11
15
 
12
- while (psd_file_tell(self) < finish) {
16
+ for (j = 0; j < bytes;) {
13
17
  len = FIX2INT(psd_file_read_byte(self));
18
+ j++;
14
19
 
15
20
  if (len < 128) {
16
21
  len++;
17
- for (int j = psd_chan_pos(self); j < psd_chan_pos(self) + len; j++) {
18
- rb_ary_store(channel_data, j, psd_file_read_byte(self));
22
+ for (k = chan_pos; k < chan_pos + len; k++) {
23
+ rb_ary_store(channel_data, k, psd_file_read_byte(self));
19
24
  }
20
25
 
21
- rb_iv_set(self, "@chan_pos", INT2FIX(psd_chan_pos(self) + len));
26
+ chan_pos += len;
27
+ j += len;
22
28
  } else if (len > 128) {
23
29
  len ^= 0xff;
24
30
  len += 2;
25
31
 
26
32
  val = psd_file_read_byte(self);
27
- for (int j = psd_chan_pos(self); j < psd_chan_pos(self) + len; j++) {
28
- rb_ary_store(channel_data, j, val);
33
+ for (k = chan_pos; k < chan_pos + len; k++) {
34
+ rb_ary_store(channel_data, k, val);
29
35
  }
30
36
 
31
- rb_iv_set(self, "@chan_pos", INT2FIX(psd_chan_pos(self) + len));
37
+ chan_pos += len;
38
+ j++;
32
39
  }
33
40
  }
34
41
  }
35
42
 
43
+ rb_iv_set(self, "@chan_pos", INT2FIX(chan_pos));
36
44
  return Qnil;
37
45
  }
38
46
 
39
- int psd_byte_count(VALUE self, int line) {
40
- return FIX2INT(
41
- rb_ary_entry(
42
- rb_iv_get(self, "@byte_counts"),
43
- FIX2INT(rb_iv_get(self, "@line_index")) + line
44
- )
45
- );
46
- }
47
-
48
47
  VALUE psd_file_read_byte(VALUE self) {
48
+ // @file.read(1).bytes[0]
49
49
  VALUE data = rb_funcall(psd_file(self), rb_intern("read"), 1, INT2FIX(1));
50
- VALUE bytes = rb_funcall(data, rb_intern("bytes"), 0);
51
- return rb_ary_entry(rb_funcall(bytes, rb_intern("to_a"), 0), 0);
52
- }
53
-
54
- int psd_chan_pos(VALUE self) {
55
- return FIX2INT(rb_iv_get(self, "@chan_pos"));
50
+ return RARRAY_PTR(rb_funcall(data, rb_intern("bytes"), 0))[0];
56
51
  }
@@ -2,8 +2,6 @@
2
2
  #define PSD_NATIVE_RLE_DECODING
3
3
 
4
4
  VALUE psd_native_decode_rle_channel(VALUE self);
5
- int psd_byte_count(VALUE self, int line);
6
5
  VALUE psd_file_read_byte(VALUE self);
7
- int psd_chan_pos(VALUE self);
8
6
 
9
7
  #endif
@@ -1,3 +1,3 @@
1
1
  module PSDNative
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/psd_native.rb CHANGED
@@ -7,7 +7,7 @@ module PSDNative
7
7
  base::Image.send(:include, PSDNative::ImageMode::RGB)
8
8
 
9
9
  # Disabled until optimized
10
- # base::Image.send(:include, PSDNative::ImageFormat::RLE)
10
+ base::Image.send(:include, PSDNative::ImageFormat::RLE)
11
11
  end
12
12
  end
13
13
 
data/psd_native.gemspec CHANGED
@@ -25,4 +25,13 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency "bundler", "~> 1.3"
26
26
  spec.add_development_dependency "rake"
27
27
  spec.add_development_dependency "rake-compiler"
28
+
29
+ spec.test_files = Dir.glob("spec/**/*")
30
+ spec.add_development_dependency 'rspec'
31
+ spec.add_development_dependency 'guard'
32
+ spec.add_development_dependency 'guard-rspec'
33
+
34
+ if RUBY_PLATFORM =~ /darwin/
35
+ spec.add_development_dependency 'rb-fsevent', '~> 0.9'
36
+ end
28
37
  end
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Image Exporting' do
4
+ before(:each) do
5
+ class PSD::Image
6
+ attr_accessor :pixel_data
7
+ end
8
+
9
+ @psd = PSD.new('spec/files/pixel.psd')
10
+ end
11
+
12
+ describe "the full preview image" do
13
+ it "should successfully parse the image data" do
14
+ @psd.parse!
15
+ expect(@psd).to be_parsed
16
+ expect(@psd.image).to_not be_nil
17
+ expect(@psd.image.width).to eq(1)
18
+ expect(@psd.image.height).to eq(1)
19
+ expect(@psd.image.pixel_data).to eq([ChunkyPNG::Color.rgba(0, 100, 200, 255)])
20
+ end
21
+
22
+ it "should be able to skip to the image" do
23
+ expect(@psd).to_not be_parsed
24
+ expect(@psd.image.width).to eq(1)
25
+ expect(@psd.image.height).to eq(1)
26
+ expect(@psd.image.pixel_data).to eq([ChunkyPNG::Color.rgba(0, 100, 200, 255)])
27
+ end
28
+
29
+ describe "as PNG" do
30
+ it "should produce a valid PNG object" do
31
+ expect(@psd.image.to_png).to be_an_instance_of(ChunkyPNG::Image)
32
+
33
+ expect(@psd.image.to_png.width).to eq(1)
34
+ expect(@psd.image.to_png.height).to eq(1)
35
+ expect(
36
+ ChunkyPNG::Color.to_truecolor_alpha_bytes(@psd.image.to_png[0,0])
37
+ ).to eq([0, 100, 200, 255])
38
+ end
39
+ end
40
+ end
41
+
42
+ describe "layer images" do
43
+ it "should successfully parse the image data" do
44
+ @psd.options[:parse_layer_images] = true
45
+ @psd.parse!
46
+
47
+ image = @psd.tree.children.first.image
48
+ expect(image).to be_an_instance_of(PSD::ChannelImage)
49
+ expect(image.width).to eq(1)
50
+ expect(image.height).to eq(1)
51
+
52
+ expect(image.pixel_data).to eq([ChunkyPNG::Color.rgba(0, 100, 200, 255)])
53
+ end
54
+
55
+ describe "as PNG" do
56
+ it "should produce a valid PNG object" do
57
+ @psd.options[:parse_layer_images] = true
58
+ @psd.parse!
59
+
60
+ png = @psd.tree.children.first.image.to_png
61
+ expect(png).to be_an_instance_of(ChunkyPNG::Image)
62
+ expect(png.width).to eq(1)
63
+ expect(png.height).to eq(1)
64
+ expect(
65
+ ChunkyPNG::Color.to_truecolor_alpha_bytes(png[0,0])
66
+ ).to eq([0, 100, 200, 255])
67
+ end
68
+ end
69
+ end
70
+ end
data/spec/psd_spec.rb ADDED
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'PSD' do
4
+ let(:filename) { 'spec/files/example.psd' }
5
+
6
+ it 'should open a file without a block' do
7
+ psd = PSD.open(filename)
8
+ expect(psd).to be_parsed
9
+ expect(psd).to be_an_instance_of(PSD)
10
+ end
11
+
12
+ it 'should refuse to open a bad filename' do
13
+ expect { PSD.open('') }.to raise_error
14
+ end
15
+
16
+ it 'should open a file and feed it to a block' do
17
+ PSD.open(filename) do |psd|
18
+ expect(psd).to be_parsed
19
+ expect(psd).to be_an_instance_of(PSD)
20
+ end
21
+ end
22
+
23
+ # We have to use #should syntax here because the DSL binds
24
+ # the block to the PSD instance.
25
+ it 'should open a file and feed it to a block DSL style' do
26
+ PSD.open(filename) do
27
+ parsed?.should == true
28
+ is_a?(PSD).should == true
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,13 @@
1
+ require 'psd_native'
2
+
3
+ class PSD
4
+ attr_reader :file
5
+ end
6
+
7
+ RSpec.configure do |config|
8
+ unless ENV['CIRCLECI']
9
+ config.filter_run :focus => true
10
+ end
11
+ config.treat_symbols_as_metadata_keys_with_true_values = true
12
+ config.run_all_when_everything_filtered = true
13
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: psd_native
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan LeFevre
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-22 00:00:00.000000000 Z
11
+ date: 2013-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: psd
@@ -66,6 +66,62 @@ dependencies:
66
66
  - - '>='
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rb-fsevent
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: '0.9'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ~>
123
+ - !ruby/object:Gem::Version
124
+ version: '0.9'
69
125
  description: Native mixins to speed up PSD.rb
70
126
  email:
71
127
  - ryan@layervault.com
@@ -76,6 +132,7 @@ extra_rdoc_files: []
76
132
  files:
77
133
  - .gitignore
78
134
  - Gemfile
135
+ - Guardfile
79
136
  - LICENSE.txt
80
137
  - README.md
81
138
  - Rakefile
@@ -89,6 +146,15 @@ files:
89
146
  - lib/psd_native.rb
90
147
  - lib/psd_native/version.rb
91
148
  - psd_native.gemspec
149
+ - spec/files/example.psd
150
+ - spec/files/one_layer.psd
151
+ - spec/files/path.psd
152
+ - spec/files/pixel.psd
153
+ - spec/files/simplest.psd
154
+ - spec/files/text.psd
155
+ - spec/image_spec.rb
156
+ - spec/psd_spec.rb
157
+ - spec/spec_helper.rb
92
158
  homepage: http://cosmos.layervault.com/psdrb.html
93
159
  licenses:
94
160
  - MIT
@@ -114,4 +180,13 @@ rubygems_version: 2.0.8
114
180
  signing_key:
115
181
  specification_version: 4
116
182
  summary: Native C mixins to speed up the slowest parts of PSD.rb
117
- test_files: []
183
+ test_files:
184
+ - spec/files/example.psd
185
+ - spec/files/one_layer.psd
186
+ - spec/files/path.psd
187
+ - spec/files/pixel.psd
188
+ - spec/files/simplest.psd
189
+ - spec/files/text.psd
190
+ - spec/image_spec.rb
191
+ - spec/psd_spec.rb
192
+ - spec/spec_helper.rb