mineskin 0.0.1 → 0.0.2

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: f2ad5a0b0f013880ad8da88118d4fc97f2e2c5cf
4
- data.tar.gz: ed9dedbc558433fbd8a8ff74d477fe6ebd49c4cf
3
+ metadata.gz: 79a3caa2954d3207de917a57e2898b5c204e291d
4
+ data.tar.gz: b63fd32460ca646080ecdab1544b9204d034bf56
5
5
  SHA512:
6
- metadata.gz: b1d186e77ac6a4ef8fa2f3f3d70f75dd28fe903182f4edbb2da927dedde18a4210711b8c24abd6c37bcbdca0d191cd152ee74a89c7882fd37d17e80be8308346
7
- data.tar.gz: 7c60577555742245618811e2baadf037d71e3fd2e25c2533253e76a11de71faae7755a42fe12e274edfd9a692662878348b5b8aec3ae2d2dfd227ccb411b113e
6
+ metadata.gz: 05f9b4ed7af32aa228707867bf4a43259769c34e8cd3e02a535bde3fb9ff3ce0e0b77d2eba50251c311c58a47522f3b37d108b4f28b17901a0b213b6c8194eda
7
+ data.tar.gz: 6d42c92f63f97c1fdee0dd697f15f88c1b2a37b8defaba4aa077d6304e2376c4640fd63bfcdd47f023615b7c7ee39690e4d5d5fe251b37e51dda810941cf4c50
@@ -1,3 +1,4 @@
1
1
  require 'mineskin/version'
2
2
  require 'mineskin/preview'
3
3
  require 'mineskin/skin_data'
4
+ require 'mineskin/cape_data'
@@ -0,0 +1,38 @@
1
+ require 'rmagick'
2
+ require 'mineskin/unit'
3
+ require 'mineskin/extractor'
4
+ require 'mineskin/types'
5
+
6
+ module MineSkin
7
+ class CapeData
8
+ include Unit
9
+ include Extractor
10
+
11
+ attr_accessor :cape
12
+ attr_reader :unit
13
+
14
+ def initialize(filename)
15
+ @image = Magick::Image.read(filename).first
16
+ # rubocop:disable ConditionalAssignment
17
+ if (@image.columns.to_f / @image.rows.to_f) == (22.0 / 17.0)
18
+ @unit = (@image.columns / 22.0).ceil
19
+ else
20
+ @unit = (@image.columns / 64.0).ceil
21
+ end
22
+ extract_cape!
23
+ end
24
+
25
+ protected
26
+
27
+ def extract_cape!
28
+ @cape = extract(
29
+ top: { texture: [1, 0, 10, 1], overlay: nil },
30
+ bottom: { texture: [11, 0, 10, 1], overlay: nil },
31
+ right: { texture: [0, 1, 1, 16], overlay: nil },
32
+ left: { texture: [11, 1, 1, 16], overlay: nil },
33
+ front: { texture: [12, 1, 10, 16], overlay: nil },
34
+ back: { texture: [1, 1, 10, 16], overlay: nil }
35
+ )
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,25 @@
1
+ module MineSkin
2
+ # Composition tools
3
+ module Compositor
4
+ def resize(img, old_unit)
5
+ img.sample(@unit.to_f / old_unit.to_f)
6
+ end
7
+
8
+ # rubocop:disable MethodLength
9
+ def composite_texture!(tex, x, y, old_unit, op: Magick::SrcOverCompositeOp)
10
+ @image.composite!(
11
+ resize(tex.texture, old_unit),
12
+ x * @unit,
13
+ y * @unit,
14
+ op
15
+ )
16
+ @image.composite!(
17
+ resize(tex.overlay, old_unit),
18
+ x * @unit,
19
+ y * @unit,
20
+ op
21
+ ) if tex.overlay
22
+ end
23
+ # rubocop:enable MethodLength
24
+ end
25
+ end
@@ -0,0 +1,62 @@
1
+ require 'mineskin/types'
2
+
3
+ module MineSkin
4
+ module Extractor
5
+ # Extracts texture
6
+ # @param [Hash] texture Texture coordinates (x,y,width,height)
7
+ # @return [Magick::Image] Image part
8
+ def crop(texture)
9
+ @image.crop(
10
+ texture[:x] * @unit,
11
+ texture[:y] * @unit,
12
+ texture[:width] * @unit,
13
+ texture[:height] * @unit
14
+ )
15
+ end
16
+
17
+ # Constructs a Texture object from given regions
18
+ # @param [Hash,Array] texture Texture coordinates in array or hash
19
+ # @param [Hash, Array, nil] overlay Optional overlay coords
20
+ # @return [MineSkin::Texture] Texture object
21
+ def part(texture: { x: 0, y: 0, width: 0, height: 0 }, overlay: nil)
22
+ tex = crop coords_to_h texture
23
+
24
+ part = Texture.new(tex, nil)
25
+ if overlay
26
+ over = crop coords_to_h overlay
27
+ part.overlay = over
28
+ end
29
+
30
+ part
31
+ end
32
+
33
+ # Constructs a Cuboid from given regions
34
+ # @param [Hash] hash Region data
35
+ # @return [MineSkin::Cuboid] Cuboid object
36
+ def extract(hash)
37
+ Cuboid.new(
38
+ part(hash[:top]),
39
+ part(hash[:bottom]),
40
+ part(hash[:left]),
41
+ part(hash[:right]),
42
+ part(hash[:front]),
43
+ part(hash[:back])
44
+ )
45
+ end
46
+
47
+ protected
48
+
49
+ def coords_to_h(c)
50
+ if c.is_a? Array
51
+ {
52
+ x: c[0],
53
+ y: c[1],
54
+ width: c[2],
55
+ height: c[3]
56
+ }
57
+ else
58
+ c
59
+ end
60
+ end
61
+ end
62
+ end
@@ -1 +1,2 @@
1
- require 'mineskin/preview/2d'
1
+ require 'mineskin/preview/skin2d'
2
+ require 'mineskin/preview/cape2d'
@@ -0,0 +1,31 @@
1
+ require 'rmagick'
2
+
3
+ module MineSkin
4
+ module Preview
5
+ class Cape2D
6
+ include Unit
7
+ include Compositor
8
+
9
+ def initialize(cape_data)
10
+ @cape_data = cape_data
11
+ end
12
+
13
+ def render(width, background: 'white')
14
+ @unit = image_unit size: width, count: 40
15
+ @image = Magick::Image.new(width, 65 * width / 100) do
16
+ self.background_color = background
17
+ end
18
+ render_cape!
19
+ @image
20
+ end
21
+
22
+ protected
23
+
24
+ def render_cape!
25
+ composite_texture! @cape_data.cape.front, 5, 5, @cape_data.unit
26
+ composite_texture! @cape_data.cape.back, 25, 5, @cape_data.unit
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,60 @@
1
+ require 'rmagick'
2
+ require 'mineskin/unit'
3
+ require 'mineskin/compositor'
4
+
5
+ module MineSkin
6
+ module Preview
7
+ # 2D preview of skin
8
+ class Skin2D
9
+ include Unit
10
+ include Compositor
11
+ # Initializes object with skin data
12
+ # @param [MineSkin::SkinData] skin_data Skin Data
13
+ def initialize(skin_data)
14
+ @skin_data = skin_data
15
+ end
16
+
17
+ # Renders preview
18
+ # @param width Width of preview
19
+ # @param [String] background Optional background color (default white)
20
+ # @return [Magick::Image] Preview
21
+ def render(width, background: 'white')
22
+ @unit = image_unit size: width, count: 12
23
+ @image = Magick::Image.new(width, 5 * width / 6) do
24
+ self.background_color = background
25
+ end
26
+ render_head!
27
+ render_body!
28
+ render_legs!
29
+ render_arms!
30
+ @image
31
+ end
32
+
33
+ protected
34
+
35
+ def render_head!
36
+ composite_texture! @skin_data.head.front, 2, 1, @skin_data.unit
37
+ composite_texture! @skin_data.head.back, 8, 1, @skin_data.unit
38
+ end
39
+
40
+ def render_body!
41
+ composite_texture! @skin_data.body.front, 2, 3, @skin_data.unit
42
+ composite_texture! @skin_data.body.back, 8, 3, @skin_data.unit
43
+ end
44
+
45
+ def render_legs!
46
+ composite_texture! @skin_data.left_leg.front, 3, 6, @skin_data.unit
47
+ composite_texture! @skin_data.right_leg.front, 2, 6, @skin_data.unit
48
+ composite_texture! @skin_data.left_leg.back, 8, 6, @skin_data.unit
49
+ composite_texture! @skin_data.right_leg.back, 9, 6, @skin_data.unit
50
+ end
51
+
52
+ def render_arms!
53
+ composite_texture! @skin_data.left_arm.front, 4, 3, @skin_data.unit
54
+ composite_texture! @skin_data.right_arm.front, 1, 3, @skin_data.unit
55
+ composite_texture! @skin_data.left_arm.back, 7, 3, @skin_data.unit
56
+ composite_texture! @skin_data.right_arm.back, 10, 3, @skin_data.unit
57
+ end
58
+ end
59
+ end
60
+ end
@@ -1,14 +1,13 @@
1
1
  require 'rmagick'
2
2
  require 'mineskin/unit'
3
+ require 'mineskin/types'
4
+ require 'mineskin/extractor'
3
5
 
4
6
  module MineSkin
5
- Cuboid = Struct.new(:top, :bottom, :left, :right, :front, :back)
6
- Texture = Struct.new(:texture, :overlay)
7
-
8
- # rubocop:disable ClassLength
9
7
  # Skin Data class
10
8
  class SkinData
11
9
  include Unit
10
+ include Extractor
12
11
 
13
12
  attr_accessor(
14
13
  :head,
@@ -19,12 +18,13 @@ module MineSkin
19
18
  :right_leg
20
19
  )
21
20
  attr_reader :unit
22
-
23
21
  def new_format?
24
22
  @new_format
25
23
  end
26
24
 
27
25
  # rubocop:disable AbcSize
26
+ # Initializes new instance of SkinData
27
+ # @param [String] filename Path to input file
28
28
  def initialize(filename)
29
29
  @image = Magick::Image.read(filename).first
30
30
  @new_format = @image.columns == @image.rows
@@ -35,53 +35,8 @@ module MineSkin
35
35
  @left_arm, @right_arm = extract_arms
36
36
  end
37
37
 
38
- def crop(texture)
39
- @image.crop(
40
- texture[:x] * @unit,
41
- texture[:y] * @unit,
42
- texture[:width] * @unit,
43
- texture[:height] * @unit
44
- )
45
- end
46
-
47
- def part(texture: { x: 0, y: 0, width: 0, height: 0 }, overlay: nil)
48
- tex = crop coords_to_h texture
49
-
50
- part = Texture.new(tex, nil)
51
- if overlay
52
- over = crop coords_to_h overlay
53
- part.overlay = over
54
- end
55
-
56
- part
57
- end
58
-
59
- def extract(hash)
60
- Cuboid.new(
61
- part(hash[:top]),
62
- part(hash[:bottom]),
63
- part(hash[:left]),
64
- part(hash[:right]),
65
- part(hash[:front]),
66
- part(hash[:back])
67
- )
68
- end
69
-
70
38
  protected
71
39
 
72
- def coords_to_h(c)
73
- if c.is_a? Array
74
- {
75
- x: c[0],
76
- y: c[1],
77
- width: c[2],
78
- height: c[3]
79
- }
80
- else
81
- c
82
- end
83
- end
84
-
85
40
  def new_only(*x)
86
41
  if new_format?
87
42
  return x.first if x.first.is_a? Array
@@ -0,0 +1,4 @@
1
+ module MineSkin
2
+ Cuboid = Struct.new(:top, :bottom, :left, :right, :front, :back)
3
+ Texture = Struct.new(:texture, :overlay)
4
+ end
@@ -1,3 +1,3 @@
1
1
  module MineSkin
2
- VERSION = '0.0.1'.freeze
2
+ VERSION = '0.0.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mineskin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stepan Melnikov
@@ -31,9 +31,14 @@ extensions: []
31
31
  extra_rdoc_files: []
32
32
  files:
33
33
  - lib/mineskin.rb
34
+ - lib/mineskin/cape_data.rb
35
+ - lib/mineskin/compositor.rb
36
+ - lib/mineskin/extractor.rb
34
37
  - lib/mineskin/preview.rb
35
- - lib/mineskin/preview/2d.rb
38
+ - lib/mineskin/preview/cape2d.rb
39
+ - lib/mineskin/preview/skin2d.rb
36
40
  - lib/mineskin/skin_data.rb
41
+ - lib/mineskin/types.rb
37
42
  - lib/mineskin/unit.rb
38
43
  - lib/mineskin/version.rb
39
44
  homepage: https://github.com/unn4m3d/mineskin
@@ -1,66 +0,0 @@
1
- require 'rmagick'
2
- require 'mineskin/unit'
3
- module MineSkin
4
- module Preview
5
- # 2D preview
6
- class Preview2D
7
- include Unit
8
-
9
- def initialize(skin_data)
10
- @skin_data = skin_data
11
- end
12
-
13
- def render(width, background: 'white')
14
- @unit = image_unit size: width, count: 12
15
- @image = Magick::Image.new(width, 5 * width / 6) do
16
- self.background_color = background
17
- end
18
- render_head!
19
- render_body!
20
- render_legs!
21
- render_arms!
22
- @image
23
- end
24
-
25
- protected
26
-
27
- def resize(img)
28
- img.sample(@unit.to_f / @skin_data.unit.to_f)
29
- end
30
-
31
- def composite_texture!(tex, x, y, op: Magick::SrcOverCompositeOp)
32
- @image.composite!(resize(tex.texture), x * @unit, y * @unit, op)
33
- @image.composite!(
34
- resize(tex.overlay),
35
- x * @unit,
36
- y * @unit,
37
- op
38
- ) if tex.overlay
39
- end
40
-
41
- def render_head!
42
- composite_texture! @skin_data.head.front, 2, 1
43
- composite_texture! @skin_data.head.back, 8, 1
44
- end
45
-
46
- def render_body!
47
- composite_texture! @skin_data.body.front, 2, 3
48
- composite_texture! @skin_data.body.back, 8, 3
49
- end
50
-
51
- def render_legs!
52
- composite_texture! @skin_data.left_leg.front, 3, 6
53
- composite_texture! @skin_data.right_leg.front, 2, 6
54
- composite_texture! @skin_data.left_leg.back, 8, 6
55
- composite_texture! @skin_data.right_leg.back, 9, 6
56
- end
57
-
58
- def render_arms!
59
- composite_texture! @skin_data.left_arm.front, 4, 3
60
- composite_texture! @skin_data.right_arm.front, 1, 3
61
- composite_texture! @skin_data.left_arm.back, 7, 3
62
- composite_texture! @skin_data.right_arm.back, 10, 3
63
- end
64
- end
65
- end
66
- end