psd 2.1.0 → 2.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/psd.rb +2 -2
- data/lib/psd/renderer/mask.rb +17 -40
- data/lib/psd/version.rb +1 -1
- data/spec/psd_spec.rb +8 -2
- 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: b59b96e20719a364948a8b25311fbe7ce9dfa6ba
|
4
|
+
data.tar.gz: fae553661c48a2caaee9212dd14e33589dd4c865
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a8629e8bca3c0e42bef10e2ab7b645c27b05a7e985fab315c3eadf878e6818b57fa32705ed679431cb60ebae2b558a08498ee1e134413830f0cf80add147ec67
|
7
|
+
data.tar.gz: c8e49f5511f1e9cfba8169e45853cd2c4e7c6b22da8e659d0573cfabbe745d85422e12a0bbef73092b3595d85a4f9078bf3a54d96dde53c99ad339ed56a328f6
|
data/lib/psd.rb
CHANGED
@@ -32,11 +32,11 @@ class PSD
|
|
32
32
|
# @param filename [String] the name of the file to open
|
33
33
|
# @return [PSD] the {PSD} object if no block was given, otherwise the value of the block
|
34
34
|
def self.open(filename, opts={}, &block)
|
35
|
+
raise "Must supply a block. Otherwise, use PSD.new." unless block_given?
|
36
|
+
|
35
37
|
psd = PSD.new(filename, opts)
|
36
38
|
psd.parse!
|
37
39
|
|
38
|
-
return psd unless block_given?
|
39
|
-
|
40
40
|
if 0 == block.arity
|
41
41
|
psd.instance_eval(&block)
|
42
42
|
else
|
data/lib/psd/renderer/mask.rb
CHANGED
@@ -1,67 +1,44 @@
|
|
1
1
|
class PSD
|
2
2
|
class Renderer
|
3
3
|
class Mask
|
4
|
-
attr_accessor :
|
4
|
+
attr_accessor :mask_data
|
5
5
|
|
6
|
-
def initialize(canvas
|
6
|
+
def initialize(canvas)
|
7
7
|
@canvas = canvas
|
8
8
|
@layer = canvas.node
|
9
|
-
@options = options
|
10
9
|
|
11
|
-
@pixel_data = @canvas.pixels
|
12
10
|
@mask_data = @layer.image.mask_data
|
13
11
|
|
14
|
-
@
|
15
|
-
@
|
12
|
+
@doc_width = @layer.header.width.to_i
|
13
|
+
@doc_height = @layer.header.height.to_i
|
16
14
|
end
|
17
15
|
|
18
16
|
def apply!
|
19
17
|
PSD.logger.debug "Applying mask to #{@layer.name}"
|
20
18
|
|
21
|
-
# We generate the preview at the document size instead to make applying the mask
|
22
|
-
# significantly easier.
|
23
|
-
width = @layer.header.width.to_i
|
24
|
-
height = @layer.header.height.to_i
|
25
|
-
png = ChunkyPNG::Canvas.new(width, height, ChunkyPNG::Color::TRANSPARENT)
|
26
|
-
|
27
|
-
i = 0
|
28
|
-
@layer_height.times do |y|
|
29
|
-
@layer_width.times do |x|
|
30
|
-
offset_x = x + @layer.left
|
31
|
-
offset_y = y + @layer.top
|
32
|
-
|
33
|
-
i +=1 and next if offset_x < 0 || offset_y < 0 || offset_x >= png.width || offset_y >= png.height
|
34
|
-
|
35
|
-
png[offset_x, offset_y] = @pixel_data[i]
|
36
|
-
i += 1
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
19
|
# Now we apply the mask
|
41
20
|
i = 0
|
42
21
|
@layer.mask.height.times do |y|
|
43
22
|
@layer.mask.width.times do |x|
|
44
|
-
|
45
|
-
|
23
|
+
doc_x = @layer.mask.left + x
|
24
|
+
doc_y = @layer.mask.top + y
|
25
|
+
|
26
|
+
layer_x = doc_x - @layer.left
|
27
|
+
layer_y = doc_y - @layer.top
|
46
28
|
|
47
|
-
|
29
|
+
color = ChunkyPNG::Color.to_truecolor_alpha_bytes(@canvas[layer_x, layer_y])
|
48
30
|
|
49
|
-
|
50
|
-
|
31
|
+
# We're off the document canvas. Crop.
|
32
|
+
if doc_x < 0 || doc_x >= @doc_width || doc_y < 0 || doc_y > @doc_height
|
33
|
+
color[3] = 0
|
34
|
+
else
|
35
|
+
color[3] = color[3] * @mask_data[i] / 255
|
36
|
+
end
|
51
37
|
|
52
|
-
|
38
|
+
@canvas[layer_x, layer_y] = ChunkyPNG::Color.rgba(*color)
|
53
39
|
i += 1
|
54
40
|
end
|
55
41
|
end
|
56
|
-
|
57
|
-
crop_left = PSD::Util.clamp(@layer.left, 0, png.width)
|
58
|
-
crop_top = PSD::Util.clamp(@layer.top, 0, png.height)
|
59
|
-
crop_width = PSD::Util.clamp(@layer_width, 0, png.width - crop_left)
|
60
|
-
crop_height = PSD::Util.clamp(@layer_height, 0, png.height - crop_top)
|
61
|
-
|
62
|
-
png.crop!(crop_left, crop_top, crop_width, crop_height)
|
63
|
-
|
64
|
-
@canvas.canvas = png
|
65
42
|
end
|
66
43
|
end
|
67
44
|
end
|
data/lib/psd/version.rb
CHANGED
data/spec/psd_spec.rb
CHANGED
@@ -4,11 +4,17 @@ describe 'PSD' do
|
|
4
4
|
let(:filename) { 'spec/files/example.psd' }
|
5
5
|
|
6
6
|
it 'should open a file without a block' do
|
7
|
-
psd = PSD.
|
8
|
-
expect(psd).
|
7
|
+
psd = PSD.new(filename)
|
8
|
+
expect(psd).to_not be_parsed
|
9
9
|
expect(psd).to be_an_instance_of(PSD)
|
10
10
|
end
|
11
11
|
|
12
|
+
it 'should raise an exception if using open without a block' do
|
13
|
+
expect {
|
14
|
+
PSD.open(filename)
|
15
|
+
}.to raise_error
|
16
|
+
end
|
17
|
+
|
12
18
|
it 'should refuse to open a bad filename' do
|
13
19
|
expect { PSD.open('') }.to raise_error
|
14
20
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: psd
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan LeFevre
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-03-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|