mc_dot_art_maker 0.2.0 → 0.3.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: 4ada9f71a0b53191792fa771b9ecb3439d8994a2
4
- data.tar.gz: 0e1c14b21b56675f8e13cc47442aa2bc5975d217
3
+ metadata.gz: 36ae0027b41b20d19212440a241e7ad9dc1c133d
4
+ data.tar.gz: 34777d07dadd29b03deeaac615c279963383fd9e
5
5
  SHA512:
6
- metadata.gz: e3557db8a6ffba7eda172948ca1f1931cd2862ca0d95c8a82d67aee59a2f8acf828f8d64f92b2689d6da809ede0723af938c934946cd8340c81a1dcf414f18ea
7
- data.tar.gz: 6c4c01fd6277c184cedd8d8e38f6dc9f207662e1cf9a075eb9ba6a703dd5286a9571d7360adce6994f3fb8ed8c9bb4f1fb57c3d7678a1e0375bb4ed2bb75afee
6
+ metadata.gz: 251d52749d1b30b3b5191abc1b47ed3b41f74e6f17e8837afa9549cc1a39240d929a3e9017e38c32c1de22eacc8d880e8582da6a9ccd4080eae73fb851b884e7
7
+ data.tar.gz: f46451b5794615b3fd6162c3d104d2c5c38671daba2a5413270ca1b215bfe5744e292729233d84c48873bed6852dfd9517b837eddb9b9e7631db74391df52edb
data/README.md CHANGED
@@ -1,3 +1,20 @@
1
1
  # MCDotArtMaker
2
+ This gem will help you to create dot-art in Minecraft world.
2
3
 
3
- We are creating a Web Service so that you can try this gem easily.
4
+ # Function
5
+ - Converting images(.png .jpg ...) to dot-art using Minecraft texture.
6
+ - Also you can maker .schematic files so that you can paste dot-art to Minecraft world easily.
7
+
8
+ # Install
9
+ This gem uses RMagick which requires ImageMagick. So first of all,you need to install ImageMagick.
10
+ Then run `gem install mc_dot_art_maker`
11
+ # Usage
12
+ See examples.
13
+
14
+ First, load image files.
15
+ `maker = MCDotArtMaker::Maker.new("test_image.jpg")`
16
+ You can resize images like this: `maker.resize_to_fit(50,50)`
17
+ This means the output image's width/height is less than 50 blocks.
18
+
19
+ Run this command to write dot-art: `maker.texture_image.write "test_image_texture.png"`
20
+ Also you can write dot-art without using Minecraft textures: `maker.mosaic_image.write "test_image_mosaic.png"`
@@ -1,5 +1,5 @@
1
1
  module MCDotArtMaker
2
- #= マイクラドット絵のpixelにあたるもの。ブロック1つで表現する
2
+ #=
3
3
  class Cell
4
4
  attr_reader :color
5
5
  attr_accessor :block
@@ -80,6 +80,9 @@ module MCDotArtMaker
80
80
  load_image unless image_loaded?
81
81
  calc_nearest_block unless calculation_done?
82
82
  end
83
+ def reset
84
+ load_image
85
+ end
83
86
 
84
87
  def texture_image(size = TEXTURE_SIZE)
85
88
  # 近似ブロック色でのモザイク画像を返す
@@ -108,9 +111,7 @@ module MCDotArtMaker
108
111
  @schematic_helper.length = @height
109
112
  @schematic_helper.write filename
110
113
  end
111
- def image_loaded?
112
- !@cells.nil?
113
- end
114
+
114
115
  private
115
116
  def calc_nearest_block
116
117
  MCDotArtMaker.puts "Calc nearest block...."
@@ -156,12 +157,15 @@ module MCDotArtMaker
156
157
  (@width*@height).times do |i|
157
158
  x = i % @width #列番号・横方向: x
158
159
  y = i / @width #行番号・縦方向: y
159
- @cells << Cell.new(@image,x,y)
160
+ # @cells << Cell.new(@image,x,y)
161
+ @cells << Dot.new(@image,x,y)
160
162
  end
161
163
  MCDotArtMaker.puts "Done!"
162
164
 
163
165
  @calculation_count = 0
164
166
  end
165
-
167
+ def image_loaded?
168
+ !@cells.nil?
169
+ end
166
170
  end
167
171
  end
@@ -1,11 +1,21 @@
1
1
  require_relative '../../lib/mc_dot_art_maker'
2
2
 
3
3
  Dir::entries("src").reject{|e| e[0]=='.'}.each do |entry|
4
- table = MCDotArtMaker::Table.new("src/#{entry}")
4
+ # table = MCDotArtMaker::Table.new("src/#{entry}")
5
+ # base = entry.split('.').first
6
+ #
7
+ # table.resize_to_fit(500,500)
8
+ # table.texture_image.write "output/texture/#{base}.png"
9
+ # table.mosaic_image.write "output/mosaic/#{base}.png"
10
+ # table.write_schematic "output/schematic/#{base}.schematic"
11
+
12
+
13
+
14
+ maker = MCDotArtMaker::Maker.new("src/#{entry}")
5
15
  base = entry.split('.').first
6
16
 
7
- table.resize_to_fit(500,500)
8
- table.texture_image.write "output/texture/#{base}.png"
9
- table.mosaic_image.write "output/mosaic/#{base}.png"
10
- table.write_schematic "output/schematic/#{base}.schematic"
17
+ maker.resize_to_fit(500,500)
18
+ maker.texture_image.write "output/texture/#{base}.png"
19
+ maker.mosaic_image.write "output/mosaic/#{base}.png"
20
+ maker.write_schematic "output/schematic/#{base}.schematic"
11
21
  end
@@ -14,15 +14,16 @@ module MCDotArtMaker
14
14
  @texture = tx.resize(TEXTURE_SIZE,TEXTURE_SIZE)
15
15
  end
16
16
  def texture(size=TEXTURE_SIZE)
17
+ # resize if texture size is not match
17
18
  if size != TEXTURE_SIZE
18
19
  @texture.resize(size,size)
19
20
  else
20
21
  @texture
21
22
  end
22
23
  end
23
- def to_lab
24
- @color.to_lab
25
- end
24
+ # def to_lab
25
+ # @color.to_lab
26
+ # end
26
27
  def to_rmagic_color
27
28
  # p "calc color :#{@color.r} #{@color.g} #{@color.b}"
28
29
  Magick::Pixel.new(@color.r*257, @color.g*257, @color.b*257)
@@ -3,7 +3,7 @@
3
3
  #
4
4
 
5
5
  module MCDotArtMaker
6
- DEFAULT_DOT_SIZE = 5
6
+ # DEFAULT_DOT_SIZE = 5
7
7
  TEXTURE_SIZE = 16
8
8
 
9
9
  DEBUG = false
@@ -0,0 +1,30 @@
1
+ module MCDotArtMaker
2
+ class Dot
3
+ attr_reader :color
4
+ attr_accessor :block
5
+ def initialize(image,x,y)
6
+ pixel = image.pixel_color(x,y)
7
+ r = pixel.red / 257
8
+ g = pixel.green / 257
9
+ b = pixel.blue / 257
10
+ @color = Color::RGB.new(r, g, b)
11
+ end
12
+ # def set_color(r,g,b)
13
+ # @color = Color::RGB.new(r, g, b)
14
+ # # p "set color #{r} #{g} #{b}"
15
+ # end
16
+ # def to_lab
17
+ # @color.to_lab
18
+ # end
19
+ def to_rmagic_color
20
+ # p "calc color :#{@color.r} #{@color.g} #{@color.b}"
21
+ Magick::Pixel.new(@color.r*257, @color.g*257, @color.b*257)
22
+ end
23
+ def comparitor
24
+ Color::Comparison.new(@color)
25
+ end
26
+ # def image=(other)
27
+ # @image = other.resize(16,16)
28
+ # end
29
+ end
30
+ end
@@ -0,0 +1,171 @@
1
+ module MCDotArtMaker
2
+ class Maker
3
+ attr_reader :width, :height, :cells
4
+ # include Enumerable
5
+
6
+ def initialize(filename,dither_method = RiemersmaDitherMethod)
7
+ # モザイクにしたい元画像をセット
8
+ if filename.instance_of? String
9
+ @image = Magick::ImageList.new(filename).first
10
+ elsif filename.instance_of? Magick::Image
11
+ @image = filename
12
+ end
13
+ @dither_method = dither_method
14
+
15
+ @schematic_helper = MCDotArtMaker::SchematicHelper.instance
16
+ @schematic_helper.read File.expand_path('../seed.schematic', __FILE__)
17
+ @block_list = MCDotArtMaker::BlockList.instance
18
+
19
+ @calculation_count = 0 # 近似ブロック計算が終わったらインクリメント
20
+ end
21
+ def resize_to_fit(width, height)
22
+ @image = @image.resize_to_fit(width, height)
23
+ end
24
+ def [](row,col)
25
+ load_image unless image_loaded?
26
+ @cells[row*@width + col]
27
+ end
28
+ def []=(row,col,newValue)
29
+ load_image unless image_loaded?
30
+ @cells[row*@width + col] = newValue
31
+ end
32
+ def each(size = 1)
33
+ load_image unless image_loaded?
34
+ @cells.each_with_index do |c,i|
35
+ x = (i % @width) * size #列番号・横方向: x
36
+ y = (i/@width) * size #行番号・縦方向: y
37
+ yield c,x,y
38
+ # cell,cellの左上のx座標 ,cellの左上のy座標
39
+ end
40
+ end
41
+ def each_with_index(size = 1)
42
+ load_image unless image_loaded?
43
+ @cells.each_with_index do |c,i|
44
+ x = (i % @width) * size #列番号・横方向: x
45
+ y = (i/@width) * size #行番号・縦方向: y
46
+ yield c,i,x,y
47
+ # cell,cellの左上のx座標 ,cellの左上のy座標
48
+ end
49
+ end
50
+ def block_ids
51
+ load_image unless image_loaded?
52
+ tmp = []
53
+ each do |c|
54
+ tmp << c.block.id
55
+ end
56
+ tmp
57
+ end
58
+ def block_data
59
+ load_image unless image_loaded?
60
+ tmp = []
61
+ each do |c|
62
+ tmp << c.block.data
63
+ end
64
+ tmp
65
+ end
66
+ def mosaic_image(size = 1)
67
+ # モザイク化した画像を返す
68
+ calculate
69
+
70
+ new_image = Magick::Image.new(@width*size, @height*size){ self.background_color = "white" }
71
+ each(size) do |c,x,y|
72
+ idr = Magick::Draw.new
73
+ idr.fill = c.block.to_rmagic_color
74
+ idr.rectangle(x, y, x + size, y + size)
75
+ idr.draw(new_image)
76
+ end
77
+ new_image
78
+ end
79
+ def calculate
80
+ load_image unless image_loaded?
81
+ calc_nearest_block unless calculation_done?
82
+ end
83
+ def reset
84
+ load_image
85
+ end
86
+
87
+ def texture_image(size = TEXTURE_SIZE)
88
+ # 近似ブロック色でのモザイク画像を返す
89
+ calculate
90
+
91
+ new_image = Magick::Image.new(@width*size, @height*size){ self.background_color = "white" }
92
+ each(size) do |c,x,y|
93
+ new_image.composite!(c.block.texture(size), x, y, Magick::OverCompositeOp)
94
+ end
95
+ new_image
96
+ end
97
+
98
+ def write_schematic(filename)
99
+ calculate
100
+
101
+ @schematic_helper.blocks = block_ids
102
+ @schematic_helper.data = block_data
103
+ @schematic_helper.weOffsetX = 0
104
+ @schematic_helper.weOffsetY = 0
105
+ @schematic_helper.weOffsetZ = 0
106
+ @schematic_helper.weOriginX = 1
107
+ @schematic_helper.weOriginY = 0
108
+ @schematic_helper.weOriginZ = 0
109
+ @schematic_helper.height = 1
110
+ @schematic_helper.width = @width
111
+ @schematic_helper.length = @height
112
+ @schematic_helper.write filename
113
+ end
114
+
115
+ private
116
+ def calc_nearest_block
117
+ MCDotArtMaker.puts "Calc nearest block...."
118
+ @nearest_color_cash ||= {}
119
+
120
+ each_with_index do |cell,index|
121
+ puts "Calculating #{index+1} of #{@cells.size}" if ((index+1) % 10000 == 0)|| (index+1 == @cells.size)
122
+ if @nearest_color_cash.include?(cell.color)
123
+ cell.block = @nearest_color_cash[cell.color]
124
+ else
125
+ comparitor = cell.comparitor
126
+ block_distance_list = {}
127
+ @block_list.each do |block|
128
+ block_distance_list[block] = comparitor.compare(block.color)
129
+ end
130
+ nearest_block = block_distance_list.min{ |x, y| x[1] <=> y[1] }[0] #[0]: key [1]: value
131
+ @nearest_color_cash[cell.color] = nearest_block
132
+ cell.block = nearest_block
133
+
134
+ MCDotArtMaker.puts "Cashed new color #{@nearest_color_cash.size} of #{@block_list.size}"
135
+ end
136
+ @calculation_count += 1
137
+ end
138
+ MCDotArtMaker.puts "Done!"
139
+ end
140
+ def calculation_done?
141
+ @cells.size == @calculation_count
142
+ end
143
+ def load_image
144
+ # - width - 絵の横方向の大きさ
145
+ # - height - 絵の縦方向の大きさ
146
+ # 端っこは切り捨てる
147
+ @width = @image.columns
148
+ @height = @image.rows
149
+ @cells = []
150
+
151
+
152
+ MCDotArtMaker.puts "remapping..."
153
+ @image = @image.remap(@block_list.to_remap_image,@dither_method)
154
+ MCDotArtMaker.puts "Done!"
155
+
156
+ MCDotArtMaker.puts "Registering cells..."
157
+ (@width*@height).times do |i|
158
+ x = i % @width #列番号・横方向: x
159
+ y = i / @width #行番号・縦方向: y
160
+ # @cells << Cell.new(@image,x,y)
161
+ @cells << Dot.new(@image,x,y)
162
+ end
163
+ MCDotArtMaker.puts "Done!"
164
+
165
+ @calculation_count = 0
166
+ end
167
+ def image_loaded?
168
+ !@cells.nil?
169
+ end
170
+ end
171
+ end
@@ -77,7 +77,7 @@ module MCDotArtMaker
77
77
  @modified = true
78
78
  end
79
79
  def write(name)
80
- raise(IllegalBlockSizeError,"Blocks or Data size doesn't match length*width*height") unless verify
80
+ raise(IllegalBlockSizeError,"Blocks or Data size doesn't match length*width*height: Block size #{@blocks.size}, width #{@width}, height #{@height}, length #{@length}") unless verify
81
81
 
82
82
  @tag.find_tag("Blocks").payload = to_nbt_block
83
83
  @tag.find_tag("Data").payload = to_nbt_data
@@ -8,7 +8,9 @@ require File.expand_path('../mc_dot_art_maker/constants.rb', __FILE__)
8
8
  require File.expand_path('../mc_dot_art_maker/extern_lib_extension.rb', __FILE__)
9
9
  require File.expand_path('../mc_dot_art_maker/block.rb', __FILE__)
10
10
  require File.expand_path('../mc_dot_art_maker/block_list.rb', __FILE__)
11
- require File.expand_path('../mc_dot_art_maker/cell.rb', __FILE__)
12
- require File.expand_path('../mc_dot_art_maker/table.rb', __FILE__)
11
+ # require File.expand_path('../mc_dot_art_maker/cell.rb', __FILE__)
12
+ require File.expand_path('../mc_dot_art_maker/dot.rb', __FILE__)
13
+ require File.expand_path('../mc_dot_art_maker/maker.rb', __FILE__)
14
+ # require File.expand_path('../mc_dot_art_maker/table.rb', __FILE__)
13
15
  require File.expand_path('../mc_dot_art_maker/schematic_helper.rb', __FILE__)
14
16
  require File.expand_path('../mc_dot_art_maker/utils.rb', __FILE__)
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'mc_dot_art_maker'
3
- s.version = '0.2.0'
3
+ s.version = '0.3.0'
4
4
  s.licenses = ['MIT']
5
5
  s.summary = 'Dot Art Maker for Minecraft'
6
6
  s.description = 'Converting images to mosaic arts using Minecraft textures and .schematic files.'
data/test/palette.png CHANGED
Binary file
@@ -1,8 +1,8 @@
1
1
  require_relative '../lib/mc_dot_art_maker'
2
2
  module MCDotArtMaker
3
- class Table
3
+ class Maker
4
4
  attr_reader :block_list
5
5
  end
6
6
  end
7
- table = MCDotArtMaker::Table.new("test_image.jpg")
8
- table.block_list.to_remap_image.write("palette.png")
7
+ maker = MCDotArtMaker::Maker.new("test_image.jpg")
8
+ maker.block_list.to_remap_image.write("palette.png")
@@ -6,6 +6,6 @@ methods = [MCDotArtMaker::NoDitherMethod,MCDotArtMaker::RiemersmaDitherMethod,MC
6
6
  methods.each do |m|
7
7
  fn = Magick::ImageList.new("test_image.jpg").first
8
8
 
9
- table = MCDotArtMaker::Table.new(fn,m)
10
- table.texture_image.write "test_image_#{m}.png"
9
+ maker = MCDotArtMaker::Maker.new(fn,m)
10
+ maker.texture_image.write "test_image_#{m}.png"
11
11
  end
Binary file
Binary file
Binary file
data/test/test_mosaic.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require_relative '../lib/mc_dot_art_maker'
2
2
 
3
- table = MCDotArtMaker::Table.new("test_image.jpg")
4
- table.resize_to_fit(50,50)
5
- table.texture_image.write "test_image_texture.png"
6
- table.mosaic_image.write "test_image_mosaic.png"
3
+ maker = MCDotArtMaker::Maker.new("test_image.jpg")
4
+ maker.resize_to_fit(50,50)
5
+ maker.texture_image.write "test_image_texture.png"
6
+ maker.mosaic_image.write "test_image_mosaic.png"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mc_dot_art_maker
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
  - Kumassy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-25 00:00:00.000000000 Z
11
+ date: 2016-03-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bindata
@@ -121,6 +121,8 @@ files:
121
121
  - LICENSE
122
122
  - README.md
123
123
  - Rakefile
124
+ - deprecated/lib/mc_dot_art_maker/cell.rb
125
+ - deprecated/lib/mc_dot_art_maker/table.rb
124
126
  - example/make_dot_art/make_dot_art.rb
125
127
  - example/make_dot_art/output/mosaic/test_image.png
126
128
  - example/make_dot_art/output/schematic/test_image.schematic
@@ -129,12 +131,12 @@ files:
129
131
  - lib/mc_dot_art_maker.rb
130
132
  - lib/mc_dot_art_maker/block.rb
131
133
  - lib/mc_dot_art_maker/block_list.rb
132
- - lib/mc_dot_art_maker/cell.rb
133
134
  - lib/mc_dot_art_maker/constants.rb
135
+ - lib/mc_dot_art_maker/dot.rb
134
136
  - lib/mc_dot_art_maker/extern_lib_extension.rb
137
+ - lib/mc_dot_art_maker/maker.rb
135
138
  - lib/mc_dot_art_maker/schematic_helper.rb
136
139
  - lib/mc_dot_art_maker/seed.schematic
137
- - lib/mc_dot_art_maker/table.rb
138
140
  - lib/mc_dot_art_maker/textures/anvil_base.png
139
141
  - lib/mc_dot_art_maker/textures/anvil_top_damaged_0.png
140
142
  - lib/mc_dot_art_maker/textures/anvil_top_damaged_1.png