psd_native 0.2.0 → 0.3.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: ea6a18568c0167e52edc9898e46cf1c3ca7fa191
4
- data.tar.gz: c1cb23a65ca6bb18cd23b87d6ed21f224b1ccba4
3
+ metadata.gz: 6a097861634857452e319eac2dc9b273f5152995
4
+ data.tar.gz: cdaa91f6470f0403cf4c551fbc6d7bae936604e7
5
5
  SHA512:
6
- metadata.gz: a13bc0db5675474519811a44b298bb94b68ae72f32fa300aa36864e973d35c56c3f6489ccd3153418aaccb3bd09e4d56243107d8b6fd51c98c4f1b9198224111
7
- data.tar.gz: 8106d269a32bd68cb67aa8980018036dd68b2eed9946adf18c67902ace34e760a19c71bb8885a5fcc864041dfed3c47bec4bad7cd5d0ad24b1f120f389c5a903
6
+ metadata.gz: 9aeb904cc861e840673e1ce5e5a47d55e1dbcf50a3af58d05ae827fef0a7f5681489fe234e100644c1aa33a39dd208f5e2c6b1627f85f5179882c1ea5296c242
7
+ data.tar.gz: 05ad35fb96d4b7a85a113e2927ee948fb33e75fa1d5c9699f93a1a8aeecf518c06a575ee922aa2ea17c88d2826504b5d2eeaf8710242c0b834e24e6347205d3b
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
data/README.md CHANGED
@@ -1,11 +1,15 @@
1
1
  # PSDNative
2
2
 
3
+ [![Travis CI](https://travis-ci.org/layervault/psd_native.png)](https://travis-ci.org/layervault/psd_native)
4
+
3
5
  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
6
 
5
7
  Currently, PSDNative replaces these sections of PSD.rb with native code:
6
8
 
7
9
  * RGB processing
10
+ * CMYK processing
8
11
  * RLE decoding
12
+ * Some color conversions
9
13
 
10
14
  ## Installation
11
15
 
data/Rakefile CHANGED
@@ -1,7 +1,13 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rake/extensiontask"
3
+ require 'rspec/core/rake_task'
3
4
 
4
5
  Rake::ExtensionTask.new('psd_native') do |ext|
5
6
  ext.lib_dir = File.join('lib', 'psd_native')
6
7
  ext.config_options = '--with-cflags="-std=c99"'
7
8
  end
9
+
10
+ Rake::Task['spec'].prerequisites << :compile
11
+
12
+ RSpec::Core::RakeTask.new(:spec)
13
+ task :default => :spec
@@ -0,0 +1,20 @@
1
+ #include "psd_native_ext.h"
2
+
3
+ VALUE psd_native_cmyk_to_rgb(VALUE self, VALUE c, VALUE m, VALUE y, VALUE k) {
4
+ int r, g, b;
5
+
6
+ r = psd_clamp_int((65535 - (FIX2INT(c) * (255 - FIX2INT(k)) + (FIX2INT(k) << 8))) >> 8, 0, 255);
7
+ g = psd_clamp_int((65535 - (FIX2INT(m) * (255 - FIX2INT(k)) + (FIX2INT(k) << 8))) >> 8, 0, 255);
8
+ b = psd_clamp_int((65535 - (FIX2INT(y) * (255 - FIX2INT(k)) + (FIX2INT(k) << 8))) >> 8, 0, 255);
9
+
10
+ VALUE result = rb_hash_new();
11
+ rb_hash_aset(result, ID2SYM(rb_intern("r")), INT2FIX(r));
12
+ rb_hash_aset(result, ID2SYM(rb_intern("g")), INT2FIX(g));
13
+ rb_hash_aset(result, ID2SYM(rb_intern("b")), INT2FIX(b));
14
+
15
+ return result;
16
+ }
17
+
18
+ int psd_clamp_int(int n, int low, int high) {
19
+ return n < low ? low : (n > high ? high : n);
20
+ }
@@ -0,0 +1,7 @@
1
+ #ifndef PSD_NATIVE_COLOR
2
+ #define PSD_NATIVE_COLOR
3
+
4
+ VALUE psd_native_cmyk_to_rgb(VALUE self, VALUE c, VALUE m, VALUE y, VALUE k);
5
+ int psd_clamp_int(int n, int low, int high);
6
+
7
+ #endif
@@ -0,0 +1,50 @@
1
+ #include "psd_native_ext.h"
2
+
3
+ VALUE psd_native_combine_cmyk_channel(VALUE self) {
4
+ psd_logger("debug", "Beginning CMYK processing");
5
+
6
+ uint32_t num_pixels = FIX2UINT(rb_iv_get(self, "@num_pixels"));
7
+ uint32_t pixel_step = FIX2UINT(rb_funcall(self, rb_intern("pixel_step"), 0));
8
+
9
+ VALUE* channel_data = RARRAY_PTR(rb_iv_get(self, "@channel_data"));
10
+ uint32_t channel_length = FIX2UINT(rb_iv_get(self, "@channel_length"));
11
+ uint32_t channel_count = FIX2UINT(rb_funcall(self, rb_intern("channels"), 0));
12
+
13
+ int i;
14
+ uint32_t r, g, b;
15
+ VALUE a, c, m, y, k, rgb;
16
+
17
+ // Loop through every pixel in the image
18
+ for (i = 0; i < num_pixels; i += pixel_step) {
19
+ if (channel_count == 5) {
20
+ a = channel_data[i];
21
+ c = channel_data[i + channel_length];
22
+ m = channel_data[i + channel_length * 2];
23
+ y = channel_data[i + channel_length * 3];
24
+ k = channel_data[i + channel_length * 4];
25
+ } else {
26
+ a = 255;
27
+ c = channel_data[i];
28
+ m = channel_data[i + channel_length];
29
+ y = channel_data[i + channel_length * 2];
30
+ k = channel_data[i + channel_length * 3];
31
+ }
32
+
33
+ rgb = psd_native_cmyk_to_rgb(
34
+ self,
35
+ INT2FIX(255 - FIX2INT(c)),
36
+ INT2FIX(255 - FIX2INT(m)),
37
+ INT2FIX(255 - FIX2INT(y)),
38
+ INT2FIX(255 - FIX2INT(k))
39
+ );
40
+
41
+
42
+ r = FIX2UINT(rb_hash_aref(rgb, ID2SYM(rb_intern("r"))));
43
+ g = FIX2UINT(rb_hash_aref(rgb, ID2SYM(rb_intern("g"))));
44
+ b = FIX2UINT(rb_hash_aref(rgb, ID2SYM(rb_intern("b"))));
45
+
46
+ rb_ary_push(rb_iv_get(self, "@pixel_data"), INT2FIX(BUILD_PIXEL(r, g, b, a)));
47
+ }
48
+
49
+ return Qnil;
50
+ }
@@ -0,0 +1,6 @@
1
+ #ifndef PSD_NATIVE_CMYK
2
+ #define PSD_NATIVE_CMYK
3
+
4
+ VALUE psd_native_combine_cmyk_channel(VALUE self);
5
+
6
+ #endif
@@ -6,10 +6,10 @@ VALUE psd_native_combine_rgb_channel(VALUE self) {
6
6
  uint32_t num_pixels = FIX2UINT(rb_iv_get(self, "@num_pixels"));
7
7
  uint32_t pixel_step = FIX2UINT(rb_funcall(self, rb_intern("pixel_step"), 0));
8
8
 
9
- VALUE channels_info = rb_iv_get(self, "@channels_info");
10
- VALUE channel_data = rb_iv_get(self, "@channel_data");
9
+ VALUE* channels_info = RARRAY_PTR(rb_iv_get(self, "@channels_info"));
10
+ VALUE* channel_data = RARRAY_PTR(rb_iv_get(self, "@channel_data"));
11
11
  uint32_t channel_length = FIX2UINT(rb_iv_get(self, "@channel_length"));
12
- uint32_t channel_count = FIX2UINT(rb_funcall(channels_info, rb_intern("length"), 0));
12
+ int channel_count = RARRAY_LENINT(rb_iv_get(self, "@channels_info"));
13
13
 
14
14
  int i, j;
15
15
  uint32_t val, r, g, b, a;
@@ -21,10 +21,10 @@ VALUE psd_native_combine_rgb_channel(VALUE self) {
21
21
 
22
22
  // And every channel for every pixel
23
23
  for (j = 0; j < channel_count; j++) {
24
- val = FIX2UINT(rb_ary_entry(channel_data, i + (channel_length * j)));
24
+ val = FIX2UINT(channel_data[i + (channel_length * j)]);
25
25
 
26
26
  // Get the hash containing channel info
27
- switch (FIX2INT(rb_hash_aref(rb_ary_entry(channels_info, j), ID2SYM(rb_intern("id"))))) {
27
+ switch (FIX2INT(rb_hash_aref(channels_info[j], ID2SYM(rb_intern("id"))))) {
28
28
  case -1: a = val; break;
29
29
  case 0: r = val; break;
30
30
  case 1: g = val; break;
@@ -1,8 +1,6 @@
1
1
  #ifndef PSD_NATIVE_RGB
2
2
  #define PSD_NATIVE_RGB
3
3
 
4
- #define BUILD_PIXEL(r, g, b, a) (((PIXEL) (r) << 24) + ((PIXEL) (g) << 16) + ((PIXEL) (b) << 8) + (PIXEL) (a))
5
-
6
4
  VALUE psd_native_combine_rgb_channel(VALUE self);
7
5
 
8
6
  #endif
@@ -8,11 +8,19 @@ void Init_psd_native() {
8
8
  VALUE ImageMode_RGB = rb_define_module_under(ImageMode, "RGB");
9
9
  rb_define_private_method(ImageMode_RGB, "combine_rgb_channel", psd_native_combine_rgb_channel, 0);
10
10
 
11
+ // CMYK Processing
12
+ VALUE ImageMode_CMYK = rb_define_module_under(ImageMode, "CMYK");
13
+ rb_define_private_method(ImageMode_CMYK, "combine_cmyk_channel", psd_native_combine_cmyk_channel, 0);
14
+
11
15
  // RLE decoding
12
16
  VALUE ImageFormat = rb_define_module_under(PSDNative, "ImageFormat");
13
17
  VALUE RLE = rb_define_module_under(ImageFormat, "RLE");
14
18
  rb_define_private_method(RLE, "decode_rle_channel", psd_native_decode_rle_channel, 0);
15
19
 
20
+ // Color functions
21
+ VALUE Color = rb_define_module_under(PSDNative, "Color");
22
+ rb_define_singleton_method(Color, "cmyk_to_rgb", psd_native_cmyk_to_rgb, 4);
23
+
16
24
  psd_logger("info", "PSD native mixins enabled!");
17
25
  }
18
26
 
@@ -8,10 +8,13 @@
8
8
  // Pixels use 32 bits unsigned integers
9
9
  // We borrow this from OilyPNG
10
10
  typedef uint32_t PIXEL;
11
+ #define BUILD_PIXEL(r, g, b, a) (((PIXEL) (r) << 24) + ((PIXEL) (g) << 16) + ((PIXEL) (b) << 8) + (PIXEL) (a))
11
12
 
12
13
  // Our native mixins
13
14
  #include "image_mode_rgb.h"
15
+ #include "image_mode_cmyk.h"
14
16
  #include "rle_decoding.h"
17
+ #include "color.h"
15
18
 
16
19
  void Init_psd_native();
17
20
  VALUE psd_class();
@@ -0,0 +1,13 @@
1
+ require 'psd'
2
+ require 'psd_native/psd_native'
3
+
4
+ class PSD
5
+ class Color
6
+ instance_eval do
7
+ alias :old_cmyk_to_rgb :cmyk_to_rgb
8
+ def cmyk_to_rgb(*args)
9
+ PSDNative::Color.cmyk_to_rgb(*args)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module PSDNative
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/psd_native.rb CHANGED
@@ -1,14 +1,15 @@
1
1
  require "psd"
2
- require "psd_native/version"
3
- require "psd_native/psd_native"
4
2
 
5
3
  module PSDNative
6
4
  def self.included(base)
7
5
  base::Image.send(:include, PSDNative::ImageMode::RGB)
8
-
9
- # Disabled until optimized
6
+ base::Image.send(:include, PSDNative::ImageMode::CMYK)
10
7
  base::Image.send(:include, PSDNative::ImageFormat::RLE)
11
8
  end
12
9
  end
13
10
 
11
+ require "psd_native/version"
12
+ require "psd_native/psd_native"
13
+ require "psd_native/color"
14
+
14
15
  PSD.send :include, PSDNative
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: psd_native
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan LeFevre
@@ -131,12 +131,17 @@ extensions:
131
131
  extra_rdoc_files: []
132
132
  files:
133
133
  - .gitignore
134
+ - .travis.yml
134
135
  - Gemfile
135
136
  - Guardfile
136
137
  - LICENSE.txt
137
138
  - README.md
138
139
  - Rakefile
140
+ - ext/psd_native/color.c
141
+ - ext/psd_native/color.h
139
142
  - ext/psd_native/extconf.rb
143
+ - ext/psd_native/image_mode_cmyk.c
144
+ - ext/psd_native/image_mode_cmyk.h
140
145
  - ext/psd_native/image_mode_rgb.c
141
146
  - ext/psd_native/image_mode_rgb.h
142
147
  - ext/psd_native/psd_native_ext.c
@@ -144,6 +149,7 @@ files:
144
149
  - ext/psd_native/rle_decoding.c
145
150
  - ext/psd_native/rle_decoding.h
146
151
  - lib/psd_native.rb
152
+ - lib/psd_native/color.rb
147
153
  - lib/psd_native/version.rb
148
154
  - psd_native.gemspec
149
155
  - spec/files/example.psd