Files
LonaRPG-Online-Games/Data/Scripts/Frames/RVscript/230_BitmapExport.rb
T

217 lines
5.1 KiB
Ruby
Raw Normal View History

2026-10-29 21:59:06 +04:00
# Bitmap Export optimized mkxp-z override
# Native mkxp-z / Ruby 3.10 contract:
# - get_data / set_data expose RGBA, top-down rows
# - raw_data is used directly whenever available
# - export_png writes RGBA, top-down rows
# - export_bmp converts to BMP-native BGRA, bottom-up rows only at file export time
require 'zlib'
class Bitmap
def last_row_address
0
end
def bytesize
width * height * 4
end
def __mkxpz_byte(value)
n = value.is_a?(Numeric) ? value.round : value.to_i
n = 0 if n < 0
n = 255 if n > 255
n.chr
end
private :__mkxpz_byte
def __mkxpz_native_raw_data_available?
respond_to?(:raw_data) && respond_to?(:raw_data=)
end
private :__mkxpz_native_raw_data_available?
def __mkxpz_native_raw_data
data = raw_data
return nil unless data.is_a?(String)
return nil if data.bytesize != bytesize
data.b
rescue StandardError
nil
end
private :__mkxpz_native_raw_data
# Public native contract for mkxp-z backend:
# returns RGBA bytes, top-down.
def get_data
if (native = __mkxpz_native_raw_data)
return native.dup
end
data = String.new(capacity: bytesize, encoding: Encoding::ASCII_8BIT)
y = 0
while y < height
x = 0
while x < width
c = get_pixel(x, y)
data << __mkxpz_byte(c.red)
data << __mkxpz_byte(c.green)
data << __mkxpz_byte(c.blue)
data << __mkxpz_byte(c.alpha)
x += 1
end
y += 1
end
data
end
# Public native contract for mkxp-z backend:
# accepts RGBA bytes, top-down.
def set_data(data)
return self if disposed?
raw = if data.is_a?(String)
data.b
else
bytes = String.new(capacity: [data.length, bytesize].min, encoding: Encoding::ASCII_8BIT)
i = 0
limit = [data.length, bytesize].min
while i < limit
value = data[i]
value = 0 if value.nil?
value = value.to_i
value = 0 if value < 0
value = 255 if value > 255
bytes << value.chr
i += 1
end
bytes
end
raw = raw.byteslice(0, bytesize) if raw.bytesize > bytesize
if __mkxpz_native_raw_data_available? && raw.bytesize == bytesize
self.raw_data = raw
return self
end
i = 0
y = 0
limit = raw.bytesize
while y < height
x = 0
while x < width
break if i + 3 >= limit
r = raw.getbyte(i)
g = raw.getbyte(i + 1)
b = raw.getbyte(i + 2)
a = raw.getbyte(i + 3)
set_pixel(x, y, Color.new(r, g, b, a))
i += 4
x += 1
end
y += 1
end
self
end
def get_data_ptr
data = get_data
return data unless block_given?
yield data
end
def _dump(level)
Marshal.dump([width, height, Zlib::Deflate.deflate(get_data, 9)])
end
def self._load(dump)
width, height, data = *Marshal.load(dump)
data = Zlib::Inflate.inflate(data)
bitmap = new(width, height)
bitmap.set_data(data)
bitmap
end
def export(filename)
case format = File.extname(filename).downcase
when '.bmp' then export_bmp(filename)
when '.png' then export_png(filename)
when '' then export_png("#{filename}.png")
else
print("Export format '#{format}' not supported.")
end
end
alias save export
def export_bmp(filename)
rgba = get_data
row_bytes = width * 4
row_data = String.new(capacity: rgba.bytesize, encoding: Encoding::ASCII_8BIT)
y = height - 1
while y >= 0
offset = y * row_bytes
row = rgba.byteslice(offset, row_bytes)
x = 0
while x < row_bytes
r = row.getbyte(x)
g = row.getbyte(x + 1)
b = row.getbyte(x + 2)
a = row.getbyte(x + 3)
row_data << __mkxpz_byte(b)
row_data << __mkxpz_byte(g)
row_data << __mkxpz_byte(r)
row_data << __mkxpz_byte(a)
x += 4
end
y -= 1
end
File.open(filename, 'wb') do |file|
file.write(['BM', row_data.bytesize + 54, 0, 54, 40, width, height,
1, 32, 0, row_data.bytesize, 0, 0, 0, 0].pack('a2L6S2L6'))
file.write(row_data)
end
end
def export_png(filename)
rgba = get_data
raw = String.new(capacity: height + rgba.bytesize, encoding: Encoding::ASCII_8BIT)
row_bytes = width * 4
y = 0
while y < height
raw << "\x00"
offset = y * row_bytes
raw << rgba.byteslice(offset, row_bytes)
y += 1
end
compressed = Zlib::Deflate.deflate(raw, 9)
File.open(filename, 'wb') do |file|
def file.write_chunk(chunk)
write([chunk.bytesize - 4].pack('N'))
write(chunk)
write([Zlib.crc32(chunk)].pack('N'))
end
file.write("\x89PNG\r\n\x1A\n")
file.write_chunk(['IHDR', width, height, 8, 6, 0, 0, 0].pack('a4N2C5'))
file.write_chunk('IDAT' + compressed)
file.write_chunk('IEND')
end
end
end
module Graphics
class << self
def export(filename = Time.now.strftime("snapshot %Y-%m-%d %Hh%Mm%Ss #{frame_count}"))
bitmap = snap_to_bitmap
bitmap.export(filename)
bitmap.dispose
end
alias save export
alias snapshot export
end
end