nis_gfx 0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/bin/nis_gfx +29 -0
  2. data/lib/nis_gfx.rb +134 -0
  3. data/nis_gfx.gemspec +12 -0
  4. metadata +67 -0
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'nis_gfx.rb'
4
+
5
+ if ARGV.size == 8 and ARGV[0] == 'nis2png'
6
+ NIS::Graphics.nis2png(
7
+ ARGV[1], # nisname
8
+ ARGV[2].to_i, # width
9
+ ARGV[3].to_i, # height
10
+ ARGV[4].to_i, # bpp
11
+ (ARGV[5].upcase == 'T') ? true : false, # tile?
12
+ ARGV[6].to_i(16), # o_data
13
+ ARGV[7].to_i(16) # o_pal
14
+ )
15
+ elsif ARGV.size == 5 and ARGV[0] = 'png2nis'
16
+ NIS::Graphics.png2nis(
17
+ ARGV[1], # nisname
18
+ ARGV[2].to_i, # bpp
19
+ (ARGV[3].upcase == 'T') ? true : false, # tile?
20
+ ARGV[4].to_i(16), # o_data
21
+ ARGV[5].to_i(16) # o_pal
22
+ )
23
+ else
24
+ puts 'NIS<->PNG Graphics Converter'
25
+ puts 'Author: tonypolik 02/11/2010'
26
+ puts 'Usage:'
27
+ puts ' ' + File.basename($0) + ' nis2png w h bpp tile? o_pal o_data'
28
+ puts ' ' + File.basename($0) + ' png2nis bpp tile? o_pal o_data'
29
+ end
@@ -0,0 +1,134 @@
1
+ require 'chunky_png'
2
+
3
+
4
+ module NIS
5
+ module Graphics
6
+
7
+
8
+ def self.nis2png nisname,w,h,bpp,tiled,o_pal,o_data
9
+ nis = File.new(nisname,"rb")
10
+ # get palette
11
+ nis.pos = o_pal
12
+ palette = []
13
+ (2**bpp).times do
14
+ palette << ChunkyPNG::Color.rgba(
15
+ nis.getbyte,
16
+ nis.getbyte,
17
+ nis.getbyte,
18
+ nis.getbyte
19
+ )
20
+ end
21
+ # get indexes
22
+ img = ChunkyPNG::Image.new(w, h)
23
+ nis.pos = o_data
24
+ case [bpp, tiled]
25
+ when [4, true]
26
+ raise if img.height % 8 != 0
27
+ raise if img.width % 32 != 0
28
+ num_tiley = img.height / 8
29
+ num_tilex = img.width / 32
30
+ num_tiley.times do |tiley|
31
+ num_tilex.times do |tilex|
32
+ 8.times do |tile_y|
33
+ 16.times do |tile_x|
34
+ byte = nis.getbyte
35
+ x = tilex*32 + tile_x*2
36
+ y = tiley*8 + tile_y
37
+ img[x+1,y] = palette[ (byte & 0xF0)>>4 ]
38
+ img[x,y] = palette[ byte & 0x0F ]
39
+ end
40
+ end
41
+ end
42
+ end
43
+ when [4, false]
44
+ raise if img.width % 2 != 0
45
+ img.height.times do |y|
46
+ (img.width / 2).times do |x|
47
+ byte = nis.getbyte
48
+ img[x*2+1,y] = palette[ (byte & 0xF0)>>4 ]
49
+ img[x*2,y] = palette[ byte & 0x0F ]
50
+ end
51
+ end
52
+ when [8, true]
53
+ raise if img.height % 8 != 0
54
+ raise if img.width % 16 != 0
55
+ num_tiley = img.height / 8
56
+ num_tilex = img.width / 16
57
+ num_tiley.times do |tiley|
58
+ num_tilex.times do |tilex|
59
+ 8.times do |tile_y|
60
+ 16.times do |tile_x|
61
+ x = tilex*16 + tile_x
62
+ y = tiley*8 + tile_y
63
+ img[x,y] = palette[nis.getbyte]
64
+ end
65
+ end
66
+ end
67
+ end
68
+ when [8, false]
69
+ img.height.times do |y|
70
+ img.width.times do |x|
71
+ img[x,y] = palette[nis.getbyte]
72
+ end
73
+ end
74
+ else
75
+ raise
76
+ end
77
+ nis.close
78
+ img.save(
79
+ nisname + ".png",
80
+ :filtering => ChunkyPNG::FILTER_NONE )
81
+ end
82
+
83
+
84
+ def self.png2nis pngname,bpp,tiled,o_pal,o_data
85
+ ds = ChunkyPNG::Datastream.from_file(pngname)
86
+ raise if ds.header_chunk.depth > bpp
87
+ plte = ds.palette_chunk.content
88
+ trns = ds.transparency_chunk.content
89
+ palette = "".force_encoding('binary')
90
+
91
+ trns.bytesize.times do |i|
92
+ palette << (plte[i*3] + plte[i*3+1] + plte[i*3+2] + trns[i])
93
+ end
94
+ trns.bytesize.upto(plte.bytesize / 3 - 1) do |i|
95
+ palette << (plte[i*3] + plte[i*3+1] + plte[i*3+2] + 255.chr)
96
+ end
97
+
98
+ nis = File.new(pngname.chomp(".png"), "rb+")
99
+ nis.pos = o_pal
100
+ nis.write palette
101
+
102
+ palette = palette.unpack("N*")
103
+
104
+ img = ChunkyPNG::Image.from_datastream(ds)
105
+ nis.pos = o_data
106
+ case [bpp, tiled]
107
+ when [4, true]
108
+ raise if img.height % 8 != 0
109
+ raise if img.width % 32 != 0
110
+ num_tiley = img.height / 8
111
+ num_tilex = img.width / 32
112
+ num_tiley.times do |tiley|
113
+ num_tilex.times do |tilex|
114
+ 8.times do |tile_y|
115
+ 16.times do |tile_x|
116
+ x = tilex*32 + tile_x*2
117
+ y = tiley*8 + tile_y
118
+ sx = palette.index(img[x+1,y])
119
+ dx = palette.index(img[x,y])
120
+ nis.putc(sx<<4 | dx)
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
126
+
127
+ nis.close
128
+
129
+ end
130
+
131
+
132
+
133
+ end
134
+ end
@@ -0,0 +1,12 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "nis_gfx"
3
+ s.version = "0.2"
4
+ s.author = "tonypolik"
5
+ s.email = "tonypolik@fastwebnet.it"
6
+ s.homepage = "http://www.cetramod.it"
7
+ s.description = "Graphics utilities for NIS images"
8
+ s.files = Dir.glob('**/*')
9
+ s.required_ruby_version = ">= 1.9"
10
+ s.executable = "nis_gfx"
11
+ s.summary = "Graphics utilities for NIS images"
12
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nis_gfx
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ version: "0.2"
9
+ platform: ruby
10
+ authors:
11
+ - tonypolik
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2010-11-02 00:00:00 +01:00
17
+ default_executable:
18
+ dependencies: []
19
+
20
+ description: Graphics utilities for NIS images
21
+ email: tonypolik@fastwebnet.it
22
+ executables:
23
+ - nis_gfx
24
+ extensions: []
25
+
26
+ extra_rdoc_files: []
27
+
28
+ files:
29
+ - bin/nis_gfx
30
+ - nis_gfx.gemspec
31
+ - nis_gfx-0.2.gem
32
+ - lib/nis_gfx.rb
33
+ has_rdoc: true
34
+ homepage: http://www.cetramod.it
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ segments:
48
+ - 1
49
+ - 9
50
+ version: "1.9"
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.3.7
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Graphics utilities for NIS images
66
+ test_files: []
67
+