Commit 27e2bdda authored by Cool Fire's avatar Cool Fire

Make an effort to clean the code a little

parent 28cfc4c4
Metrics/LineLength:
Enabled: false
Style/VariableName:
Enabled: false
......@@ -8,7 +8,7 @@ width = 0
height = 0
padding = 0
decode = false
ARGV.options do |opts|
opts.separator 'When no filenames are provided STDIO is used'
opts.separator 'When no width is provided a the square root of the input size is used.'
......@@ -21,15 +21,15 @@ ARGV.options do |opts|
end
# Start reading input
data = ''
output = ''
if(inputfile == '')
data = ARGF.read
else
data = IO.binread(inputfile)
end
data =
if inputfile == ''
ARGF.read
else
IO.binread(inputfile)
end
if(decode)
if decode
# Decoding
# Read headers we care about
......@@ -39,31 +39,25 @@ if(decode)
width = data[18..21].pack('C*').unpack('I')[0]
# Calculate end of data
dend = data.size - padsize - 1
dend = data.size - padsize - 1
# Calculate row padding
rowpadsize = (width * 3) % 4
if(rowpadsize > 0)
rowpadsize = 4 - rowpadsize
end
rowpadsize = 4 - rowpadsize if rowpadsize > 0
# Decode
if(padding == 1 && rowpadsize != 0)
if padding == 1 && rowpadsize != 0
pc = 0
data[54..-1].pack('C*').each_byte { |b|
pc +=1
if(pc > width * 3)
if(pc == (width * 3) + rowpadsize)
pc = 0
end
data[54..-1].pack('C*').each_byte do |b|
pc += 1
if pc > width * 3
pc = 0 if pc == (width * 3) + rowpadsize
next
else
output.concat([b].pack('C*'))
end
}
end
dend = -1 - padsize
output = output.bytes[0..dend].pack('C*')
else
......@@ -74,28 +68,22 @@ else
# Add padding if needed
padsize = data.bytesize % 4
if(padsize > 0)
padsize = 4 - padsize
end
padsize = 4 - padsize if padsize > 0
data = data.concat([0x00].pack('C*') * padsize)
# Calculate image dimensions
if(width == 0)
width = Math.sqrt(data.bytesize / 3).ceil()
if width.zero?
width = Math.sqrt(data.bytesize / 3).ceil
height = width
else
height = ((data.bytesize / 3) / width.to_f).ceil()
height = ((data.bytesize / 3) / width.to_f).ceil
end
# Calculate extra bytes required for row padding
rowpadsize = (width * 3) % 4
if(rowpadsize > 0)
rowpadsize = 4 - rowpadsize
end
rowpadsize = (width * 3) % 4
rowpadsize = 4 - rowpadsize if rowpadsize > 0
totalrowpad = 0
if(padding == 1)
totalrowpad = rowpadsize * height
end
totalrowpad = rowpadsize * height if padding == 1
# Construct BMP header
bfType = [0x42, 0x4D] # BMP specification type
......@@ -137,25 +125,25 @@ else
output.concat(biClrImportant.pack('C*'))
# Print content
if(padding == 1)
if padding == 1
pc = 0
data.each_byte { |b|
data.each_byte do |b|
pc += 1
output.concat([b].pack('C*'))
if(pc == width * 3)
if pc == width * 3
output.concat([0x00].pack('C*') * rowpadsize)
pc = 0
end
}
end
else
output.concat(data)
end
end
# Write out results
if(outputfile == '')
if outputfile == ''
print output
else
IO.binwrite(outputfile, output)
end
\ No newline at end of file
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment