mc_dot_art_maker 0.1.4 → 0.2.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: 0a39ef04ca4a069c931c7207f4789ae5fbeef514
4
- data.tar.gz: 246a961f7cd2ff7fb346130b21c1e2cc7a092278
3
+ metadata.gz: 4ada9f71a0b53191792fa771b9ecb3439d8994a2
4
+ data.tar.gz: 0e1c14b21b56675f8e13cc47442aa2bc5975d217
5
5
  SHA512:
6
- metadata.gz: 33cbf8624fad14ed26eb63a5c16379efec16ff558ef1927aada97c0c97a64fb4cd11147d43a81418032a06912c0942fae0a7e8eda67166436741876cca4b82db
7
- data.tar.gz: f671ab9d3c9428788515780d2ba3b01d575288bf26f41435942e75a511169c0fd38470fee9108daa820ab82a281c40e23752dd4bbe0a9fcce9c29d68c9fa8640
6
+ metadata.gz: e3557db8a6ffba7eda172948ca1f1931cd2862ca0d95c8a82d67aee59a2f8acf828f8d64f92b2689d6da809ede0723af938c934946cd8340c81a1dcf414f18ea
7
+ data.tar.gz: 6c4c01fd6277c184cedd8d8e38f6dc9f207662e1cf9a075eb9ba6a703dd5286a9571d7360adce6994f3fb8ed8c9bb4f1fb57c3d7678a1e0375bb4ed2bb75afee
data/README.md CHANGED
@@ -1 +1,3 @@
1
1
  # MCDotArtMaker
2
+
3
+ We are creating a Web Service so that you can try this gem easily.
@@ -3,6 +3,8 @@ require_relative '../../lib/mc_dot_art_maker'
3
3
  Dir::entries("src").reject{|e| e[0]=='.'}.each do |entry|
4
4
  table = MCDotArtMaker::Table.new("src/#{entry}")
5
5
  base = entry.split('.').first
6
+
7
+ table.resize_to_fit(500,500)
6
8
  table.texture_image.write "output/texture/#{base}.png"
7
9
  table.mosaic_image.write "output/mosaic/#{base}.png"
8
10
  table.write_schematic "output/schematic/#{base}.schematic"
@@ -4,6 +4,8 @@ require 'nbt_utils'
4
4
  require 'color-rgb'
5
5
  require 'rmagick'
6
6
 
7
+ require File.expand_path('../mc_dot_art_maker/constants.rb', __FILE__)
8
+ require File.expand_path('../mc_dot_art_maker/extern_lib_extension.rb', __FILE__)
7
9
  require File.expand_path('../mc_dot_art_maker/block.rb', __FILE__)
8
10
  require File.expand_path('../mc_dot_art_maker/block_list.rb', __FILE__)
9
11
  require File.expand_path('../mc_dot_art_maker/cell.rb', __FILE__)
@@ -1,25 +1,6 @@
1
- module Color
2
- class RGB
3
- def ==(other)
4
- @r == other.r && @g == other.g && @b == other.b
5
- end
6
- def eql?(other)
7
- @r.eql?(other.r) && @g.eql?(other.g) && @b.eql?(other.b)
8
- end
9
- def hash
10
- code = 17
11
- code = 37*code + @r.hash
12
- code = 37*code + @g.hash
13
- code = 37*code + @b.hash
14
- code
15
- end
16
- end
17
- end
18
-
19
1
  module MCDotArtMaker
20
- TEXTURE_SIZE = 16
21
2
  class Block
22
- attr_reader :name,:id,:data,:texture,:color
3
+ attr_reader :name,:id,:data,:color
23
4
  # - name - ブロック名 e.g. :orange_wool
24
5
  # - id - id e.g. 35
25
6
  # - data - data e.g. 1
@@ -32,12 +13,19 @@ module MCDotArtMaker
32
13
  @color = Color::RGB.new(r, g, b)
33
14
  @texture = tx.resize(TEXTURE_SIZE,TEXTURE_SIZE)
34
15
  end
16
+ def texture(size=TEXTURE_SIZE)
17
+ if size != TEXTURE_SIZE
18
+ @texture.resize(size,size)
19
+ else
20
+ @texture
21
+ end
22
+ end
35
23
  def to_lab
36
24
  @color.to_lab
37
25
  end
38
26
  def to_rmagic_color
39
27
  # p "calc color :#{@color.r} #{@color.g} #{@color.b}"
40
- Magick::Pixel.new(@color.r*256, @color.g*256, @color.b*256)
28
+ Magick::Pixel.new(@color.r*257, @color.g*257, @color.b*257)
41
29
  end
42
30
 
43
31
  def hash
@@ -316,16 +316,16 @@ module MCDotArtMaker
316
316
  #
317
317
  # block :redstone_ore, '73', 'redstone_ore.png'
318
318
  #
319
- # block :snow, '78', 'snow.png'
319
+ block :snow, '78', 'snow.png'
320
320
  block :ice, '79', 'ice.png'
321
321
  #
322
- # block :clay, '82', 'clay.png'
322
+ block :clay, '82', 'clay.png'
323
323
  #
324
324
  # block :jukebox, '84', 'jukebox_top.png'
325
325
  #
326
326
  # block :pumpkin, '86', 'pumpkin_top.png'
327
327
  # block :netherrack, '87', 'netherrack.png'
328
- # block :soul_sand, '88', 'soul_sand.png'
328
+ block :soul_sand, '88', 'soul_sand.png'
329
329
  block :glowstone, '89', 'glowstone.png'
330
330
  #
331
331
  # block :cake_block, '92', 'cake_top.png'
@@ -457,15 +457,15 @@ module MCDotArtMaker
457
457
 
458
458
  private
459
459
  def block(name, id_data_label, filename)
460
- # ブロックをリストに入れるためのヘルパーメソッド
461
- image = Magick::ImageList.new(File.expand_path("../textures/#{filename}", __FILE__))
462
- r,g,b = MCDotArtMaker.calc_average_color(image.get_pixels(0,0,image.columns,image.rows))
460
+ # Add blocks to @blocks
461
+ image = Magick::ImageList.new(File.expand_path("../textures/#{filename}", __FILE__)).first
462
+ r,g,b = MCDotArtMaker.calc_average_color(image)
463
463
  id = id_data_label.split(':')[0].to_i
464
464
  data = id_data_label.split(':')[1].to_i
465
465
 
466
- block = Block.new(name,id, data,r,g,b, image[0])
466
+ block = Block.new(name,id, data,r,g,b, image)
467
467
  @blocks << block
468
- puts "Block #{name} registered. R:#{r} G:#{g} B:#{b}"
468
+ MCDotArtMaker.puts "Block #{name} registered. R:#{r} G:#{g} B:#{b}"
469
469
  end
470
470
  end
471
471
  end
@@ -5,21 +5,21 @@ module MCDotArtMaker
5
5
  attr_accessor :block
6
6
  def initialize(image,x,y)
7
7
  pixel = image.pixel_color(x,y)
8
- r = pixel.red / 256
9
- g = pixel.green / 256
10
- b = pixel.blue / 256
8
+ r = pixel.red / 257
9
+ g = pixel.green / 257
10
+ b = pixel.blue / 257
11
11
  @color = Color::RGB.new(r, g, b)
12
12
  end
13
13
  # def set_color(r,g,b)
14
14
  # @color = Color::RGB.new(r, g, b)
15
15
  # # p "set color #{r} #{g} #{b}"
16
16
  # end
17
- def to_lab
18
- @color.to_lab
19
- end
17
+ # def to_lab
18
+ # @color.to_lab
19
+ # end
20
20
  def to_rmagic_color
21
21
  # p "calc color :#{@color.r} #{@color.g} #{@color.b}"
22
- Magick::Pixel.new(@color.r*256, @color.g*256, @color.b*256)
22
+ Magick::Pixel.new(@color.r*257, @color.g*257, @color.b*257)
23
23
  end
24
24
  def comparitor
25
25
  Color::Comparison.new(@color)
@@ -0,0 +1,14 @@
1
+ #
2
+ # Constants used in this gem
3
+ #
4
+
5
+ module MCDotArtMaker
6
+ DEFAULT_DOT_SIZE = 5
7
+ TEXTURE_SIZE = 16
8
+
9
+ DEBUG = false
10
+
11
+ NoDitherMethod = Magick::NoDitherMethod
12
+ RiemersmaDitherMethod = Magick::RiemersmaDitherMethod
13
+ FloydSteinbergDitherMethod = Magick::FloydSteinbergDitherMethod
14
+ end
@@ -0,0 +1,52 @@
1
+ #
2
+ # Add some features to extern gem.
3
+ #
4
+
5
+ module Color
6
+ class RGB
7
+ # Add to check equality
8
+ def ==(other)
9
+ @r == other.r && @g == other.g && @b == other.b
10
+ end
11
+ # Add to use this object for hash key
12
+ def eql?(other)
13
+ @r.eql?(other.r) && @g.eql?(other.g) && @b.eql?(other.b)
14
+ end
15
+ def hash
16
+ code = 17
17
+ code = 37*code + @r.hash
18
+ code = 37*code + @g.hash
19
+ code = 37*code + @b.hash
20
+ code
21
+ end
22
+ end
23
+ end
24
+
25
+ # Add to rewrite NBT files
26
+ module NBTUtils
27
+ module Tag
28
+ class ByteArray
29
+ def payload=(new_value)
30
+ @payload = ::BinData::String.new(new_value)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ module NBTUtils
36
+ module Tag
37
+ class Int
38
+ def payload=(new_value)
39
+ @payload = ::BinData::Int32be.new(new_value)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ module NBTUtils
45
+ module Tag
46
+ class Short
47
+ def payload=(new_value)
48
+ @payload = ::BinData::Int16be.new(new_value)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -1,32 +1,3 @@
1
- # NBTUtilsを開いてデータ書き換えのためのメソッドを定義する
2
- module NBTUtils
3
- module Tag
4
- class ByteArray
5
- def payload=(new_value)
6
- @payload = ::BinData::String.new(new_value)
7
- end
8
- end
9
- end
10
- end
11
- module NBTUtils
12
- module Tag
13
- class Int
14
- def payload=(new_value)
15
- @payload = ::BinData::Int32be.new(new_value)
16
- end
17
- end
18
- end
19
- end
20
- module NBTUtils
21
- module Tag
22
- class Short
23
- def payload=(new_value)
24
- @payload = ::BinData::Int16be.new(new_value)
25
- end
26
- end
27
- end
28
- end
29
-
30
1
  module MCDotArtMaker
31
2
  class IllegalBlockSizeError < StandardError ; end
32
3
 
@@ -1,53 +1,36 @@
1
1
  module MCDotArtMaker
2
- DEFAULT_DOT_SIZE = 5
3
2
  #= ドット絵を表すクラス。Cellの集合体
4
3
  class Table
5
4
  attr_reader :width, :height, :cells
6
- include Enumerable
5
+ # include Enumerable
7
6
 
8
- def initialize(filename,dither_method = Magick::RiemersmaDitherMethod)
7
+ def initialize(filename,dither_method = RiemersmaDitherMethod)
9
8
  # モザイクにしたい元画像をセット
10
-
11
9
  if filename.instance_of? String
12
10
  @image = Magick::ImageList.new(filename).first
13
11
  elsif filename.instance_of? Magick::Image
14
12
  @image = filename
15
13
  end
16
-
17
-
18
- # - width - 絵の横方向の大きさ
19
- # - height - 絵の縦方向の大きさ
20
- # 端っこは切り捨てる
21
- @width = @image.columns
22
- @height = @image.rows
23
- @cells = []
24
- @block_list = MCDotArtMaker::BlockList.instance
25
-
26
- puts "remapping..."
27
- @image = @image.remap(@block_list.to_remap_image,dither_method)
28
- puts "Done!"
29
-
30
- puts "Registering cells..."
31
- (@width*@height).times do |i|
32
- x = i % @width #列番号・横方向: x
33
- y = i / @width #行番号・縦方向: y
34
- @cells << Cell.new(@image,x,y)
35
- end
36
- puts "Done!"
37
-
14
+ @dither_method = dither_method
38
15
 
39
16
  @schematic_helper = MCDotArtMaker::SchematicHelper.instance
40
17
  @schematic_helper.read File.expand_path('../seed.schematic', __FILE__)
41
18
 
42
19
  @calculation_count = 0 # 近似ブロック計算が終わったらインクリメント
43
20
  end
21
+ def resize_to_fit(width, height)
22
+ @image = @image.resize_to_fit(width, height)
23
+ end
44
24
  def [](row,col)
25
+ load_image unless image_loaded?
45
26
  @cells[row*@width + col]
46
27
  end
47
28
  def []=(row,col,newValue)
29
+ load_image unless image_loaded?
48
30
  @cells[row*@width + col] = newValue
49
31
  end
50
32
  def each(size = 1)
33
+ load_image unless image_loaded?
51
34
  @cells.each_with_index do |c,i|
52
35
  x = (i % @width) * size #列番号・横方向: x
53
36
  y = (i/@width) * size #行番号・縦方向: y
@@ -56,6 +39,7 @@ module MCDotArtMaker
56
39
  end
57
40
  end
58
41
  def each_with_index(size = 1)
42
+ load_image unless image_loaded?
59
43
  @cells.each_with_index do |c,i|
60
44
  x = (i % @width) * size #列番号・横方向: x
61
45
  y = (i/@width) * size #行番号・縦方向: y
@@ -64,6 +48,7 @@ module MCDotArtMaker
64
48
  end
65
49
  end
66
50
  def block_ids
51
+ load_image unless image_loaded?
67
52
  tmp = []
68
53
  each do |c|
69
54
  tmp << c.block.id
@@ -71,42 +56,44 @@ module MCDotArtMaker
71
56
  tmp
72
57
  end
73
58
  def block_data
59
+ load_image unless image_loaded?
74
60
  tmp = []
75
61
  each do |c|
76
62
  tmp << c.block.data
77
63
  end
78
64
  tmp
79
65
  end
80
- def mosaic_image
66
+ def mosaic_image(size = 1)
81
67
  # モザイク化した画像を返す
82
68
  calculate
83
69
 
84
- new_image = Magick::Image.new(@width*TEXTURE_SIZE, @height*TEXTURE_SIZE){ self.background_color = "white" }
85
- each(TEXTURE_SIZE) do |c,x,y|
70
+ new_image = Magick::Image.new(@width*size, @height*size){ self.background_color = "white" }
71
+ each(size) do |c,x,y|
86
72
  idr = Magick::Draw.new
87
73
  idr.fill = c.block.to_rmagic_color
88
- idr.rectangle(x, y, x + TEXTURE_SIZE, y + TEXTURE_SIZE)
74
+ idr.rectangle(x, y, x + size, y + size)
89
75
  idr.draw(new_image)
90
76
  end
91
77
  new_image
92
78
  end
93
79
  def calculate
80
+ load_image unless image_loaded?
94
81
  calc_nearest_block unless calculation_done?
95
82
  end
96
83
 
97
- def texture_image
84
+ def texture_image(size = TEXTURE_SIZE)
98
85
  # 近似ブロック色でのモザイク画像を返す
99
86
  calculate
100
87
 
101
- new_image = Magick::Image.new(@width*TEXTURE_SIZE, @height*TEXTURE_SIZE){ self.background_color = "white" }
102
- each(TEXTURE_SIZE) do |c,x,y|
103
- new_image.composite!(c.block.texture, x, y, Magick::OverCompositeOp)
88
+ new_image = Magick::Image.new(@width*size, @height*size){ self.background_color = "white" }
89
+ each(size) do |c,x,y|
90
+ new_image.composite!(c.block.texture(size), x, y, Magick::OverCompositeOp)
104
91
  end
105
92
  new_image
106
93
  end
107
94
 
108
95
  def write_schematic(filename)
109
- calc_nearest_block unless calculation_done?
96
+ calculate
110
97
 
111
98
  @schematic_helper.blocks = block_ids
112
99
  @schematic_helper.data = block_data
@@ -121,14 +108,16 @@ module MCDotArtMaker
121
108
  @schematic_helper.length = @height
122
109
  @schematic_helper.write filename
123
110
  end
124
-
111
+ def image_loaded?
112
+ !@cells.nil?
113
+ end
125
114
  private
126
115
  def calc_nearest_block
127
- puts "Calc nearest block...."
116
+ MCDotArtMaker.puts "Calc nearest block...."
128
117
  @nearest_color_cash ||= {}
129
118
 
130
119
  each_with_index do |cell,index|
131
- puts "Calculating #{index+1} of #{@cells.size}" if (index % 10000 == 0)|| (index == @cells.size)
120
+ puts "Calculating #{index+1} of #{@cells.size}" if ((index+1) % 10000 == 0)|| (index+1 == @cells.size)
132
121
  if @nearest_color_cash.include?(cell.color)
133
122
  cell.block = @nearest_color_cash[cell.color]
134
123
  else
@@ -141,14 +130,38 @@ module MCDotArtMaker
141
130
  @nearest_color_cash[cell.color] = nearest_block
142
131
  cell.block = nearest_block
143
132
 
144
- puts "Cashed new color #{@nearest_color_cash.size} of #{@block_list.size}"
133
+ MCDotArtMaker.puts "Cashed new color #{@nearest_color_cash.size} of #{@block_list.size}"
145
134
  end
146
135
  @calculation_count += 1
147
136
  end
148
- puts "Done!"
137
+ MCDotArtMaker.puts "Done!"
149
138
  end
150
139
  def calculation_done?
151
140
  @cells.size == @calculation_count
152
141
  end
142
+ def load_image
143
+ # - width - 絵の横方向の大きさ
144
+ # - height - 絵の縦方向の大きさ
145
+ # 端っこは切り捨てる
146
+ @width = @image.columns
147
+ @height = @image.rows
148
+ @cells = []
149
+ @block_list = MCDotArtMaker::BlockList.instance
150
+
151
+ MCDotArtMaker.puts "remapping..."
152
+ @image = @image.remap(@block_list.to_remap_image,@dither_method)
153
+ MCDotArtMaker.puts "Done!"
154
+
155
+ MCDotArtMaker.puts "Registering cells..."
156
+ (@width*@height).times do |i|
157
+ x = i % @width #列番号・横方向: x
158
+ y = i / @width #行番号・縦方向: y
159
+ @cells << Cell.new(@image,x,y)
160
+ end
161
+ MCDotArtMaker.puts "Done!"
162
+
163
+ @calculation_count = 0
164
+ end
165
+
153
166
  end
154
167
  end
@@ -1,23 +1,34 @@
1
1
  module MCDotArtMaker
2
- def self.calc_average_color(pixels)
2
+ def self.calc_average_color(arg)
3
+ pixels = nil
4
+ if arg.instance_of? Magick::Image or arg.instance_of? Magick::ImageList
5
+ pixels = arg.get_pixels(0,0,arg.columns,arg.rows)
6
+ elsif arg.instance_of? Array
7
+ pixels = arg
8
+ end
9
+
3
10
  if pixels.size == 1
4
- return pixels[0].red/256, pixels[0].green/256, pixels[0].blue/256
11
+ return pixels[0].red/257, pixels[0].green/257, pixels[0].blue/257
5
12
  end
6
13
  sum_r = pixels.reduce(0) do |a,elem|
7
- a+elem.red/256
14
+ a+elem.red/257
8
15
  end
9
16
  ave_r = sum_r/pixels.size
10
17
 
11
18
  sum_g = pixels.reduce(0) do |a,elem|
12
- a+elem.green/256
19
+ a+elem.green/257
13
20
  end
14
21
  ave_g = sum_g/pixels.size
15
22
 
16
23
  sum_b = pixels.reduce(0) do |a,elem|
17
- a+elem.blue/256
24
+ a+elem.blue/257
18
25
  end
19
26
  ave_b = sum_b/pixels.size
20
27
 
21
28
  [ave_r, ave_g, ave_b]
22
29
  end
30
+
31
+ def self.puts(*args)
32
+ Kernel.puts *args if DEBUG
33
+ end
23
34
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'mc_dot_art_maker'
3
- s.version = '0.1.4'
3
+ s.version = '0.2.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.'
@@ -1,7 +1,7 @@
1
1
  require_relative '../lib/mc_dot_art_maker'
2
2
 
3
3
 
4
- methods = [Magick::NoDitherMethod,Magick::RiemersmaDitherMethod,Magick::FloydSteinbergDitherMethod]
4
+ methods = [MCDotArtMaker::NoDitherMethod,MCDotArtMaker::RiemersmaDitherMethod,MCDotArtMaker::FloydSteinbergDitherMethod]
5
5
 
6
6
  methods.each do |m|
7
7
  fn = Magick::ImageList.new("test_image.jpg").first
Binary file
Binary file
@@ -0,0 +1,6 @@
1
+ require_relative '../lib/mc_dot_art_maker'
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"
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.1.4
4
+ version: 0.2.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-15 00:00:00.000000000 Z
11
+ date: 2016-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bindata
@@ -130,6 +130,8 @@ files:
130
130
  - lib/mc_dot_art_maker/block.rb
131
131
  - lib/mc_dot_art_maker/block_list.rb
132
132
  - lib/mc_dot_art_maker/cell.rb
133
+ - lib/mc_dot_art_maker/constants.rb
134
+ - lib/mc_dot_art_maker/extern_lib_extension.rb
133
135
  - lib/mc_dot_art_maker/schematic_helper.rb
134
136
  - lib/mc_dot_art_maker/seed.schematic
135
137
  - lib/mc_dot_art_maker/table.rb
@@ -524,6 +526,9 @@ files:
524
526
  - test/test_image_FloydSteinbergDitherMethod.png
525
527
  - test/test_image_NoDitherMethod.png
526
528
  - test/test_image_RiemersmaDitherMethod.png
529
+ - test/test_image_mosaic.png
530
+ - test/test_image_texture.png
531
+ - test/test_mosaic.rb
527
532
  homepage: https://github.com/Kumassy/MCDotArtMaker
528
533
  licenses:
529
534
  - MIT