psd 2.1.0 → 2.1.1

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: d9b96175c4675aceb277aa64cf381aa9d7f20561
4
- data.tar.gz: 1c4d5d7d68921e1a1dbd222f8022d3f02e3b9d2c
3
+ metadata.gz: b59b96e20719a364948a8b25311fbe7ce9dfa6ba
4
+ data.tar.gz: fae553661c48a2caaee9212dd14e33589dd4c865
5
5
  SHA512:
6
- metadata.gz: 9b86b19b017831e85e3c8b28a0d045a3858cc96a7c03e6c501c61225d32c7018f58e03dc4fbfd5252e2016ef67e593435493960cb9eabf76fcfba2cb85487666
7
- data.tar.gz: 65e4d1d26be0132d00e4215803dbb709a4af2c39f8ca604ac470daedc5dce5b7ecc5cbadaa134f0a392986c7b8c4f3cbb5fbc322fc660a9fc4649a8f93a634bd
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
@@ -1,67 +1,44 @@
1
1
  class PSD
2
2
  class Renderer
3
3
  class Mask
4
- attr_accessor :pixel_data, :mask_data, :layer_width, :layer_height
4
+ attr_accessor :mask_data
5
5
 
6
- def initialize(canvas, options = {})
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
- @layer_width = (@layer.folder? ? @layer.mask.width : @layer.width).to_i
15
- @layer_height = (@layer.folder? ? @layer.mask.height : @layer.height).to_i
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
- offset_x = @layer.mask.left + x
45
- offset_y = @layer.mask.top + y
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
- i += 1 and next if offset_x < 0 || offset_y < 0 || offset_x >= png.width || offset_y >= png.height
29
+ color = ChunkyPNG::Color.to_truecolor_alpha_bytes(@canvas[layer_x, layer_y])
48
30
 
49
- color = ChunkyPNG::Color.to_truecolor_alpha_bytes(png[offset_x, offset_y])
50
- color[3] = color[3] * @mask_data[i] / 255
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
- png[offset_x, offset_y] = ChunkyPNG::Color.rgba(*color)
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
@@ -1,3 +1,3 @@
1
1
  class PSD
2
- VERSION = "2.1.0"
2
+ VERSION = "2.1.1"
3
3
  end
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.open(filename)
8
- expect(psd).to be_parsed
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.0
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-02-21 00:00:00.000000000 Z
12
+ date: 2014-03-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake