Skip to content

Instantly share code, notes, and snippets.

@hyrious
Last active August 19, 2019 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyrious/d5aa6acd384ca2688dfbddc93a647e31 to your computer and use it in GitHub Desktop.
Save hyrious/d5aa6acd384ca2688dfbddc93a647e31 to your computer and use it in GitHub Desktop.
RGSS WebP
# coding: utf-8
# this file provides a handy way to call win32api
class Dll
def initialize dll
@dll = dll.to_s
end
def method_missing func, *args
imports = args.map { |e| Integer === e ? 'L' : 'p' }
Win32API.new(@dll, func.to_s, imports, 'L').call(*args)
end
end
def dll name
Dll.new name
end
Kernel32 = dll 'kernel32'
User32 = dll 'user32'
class Buffer
DIRECTIVES = {
"C" => 1, "c" => 1, "A" => 1, "a" => 1, "Z" => 1, "x" => 1,
"S" => 2, "s" => 2, "S_" => 2, "S!" => 2, "s_" => 2, "s!" => 2,
"n" => 2, "v" => 2,
"L" => 4, "l" => 4, "I" => 4, "I_" => 4, "I!" => 4, "L_" => 4,
"L!" => 4, "i" => 4, "i_" => 4, "i!" => 4, "l_" => 4, "l!" => 4,
"N" => 4, "V" => 4, "F" => 4, "f" => 4, "e" => 4, "g" => 4,
"Q" => 8, "q" => 8, "D" => 8, "d" => 8, "E" => 8, "G" => 8,
}
def self.buf template=nil
if template
@buf = new template
else
@buf && @buf.unpack
end
end
def initialize template
@template = template
@buffer = make_buffer template
end
attr_reader :buffer
alias to_str buffer
def make_buffer template=@template
sum = template.scan(/([[:alpha:]][_!]?)(\d*)/).inject 0 do |sum, (d, n)|
sum + DIRECTIVES[d] * (n.to_i.nonzero? || 1)
end
[].pack("x#{sum}")
end
def unpack template=@template
@buffer.unpack template
end
def inspect
"#<Buffer:%#010x #{unpack.inspect}>" % (object_id << 1)
end
end
def buf(*args)
Buffer.buf(*args)
end
# coding: utf-8
require_relative 'api'
WebP = dll 'webp'
Ntdll = dll 'ntdll'
class Bitmap
GETADDR = [
0x8b, 0104,0044, 4, # mov 4(%esp), %eax
0x8b, 0100, 16, # mov 16(%eax), %eax
0x8b, 0100, 8, # mov 8(%eax), %eax
0x8b, 0100, 16, # mov 16(%eax), %eax
0xc2, 16,0, # ret $16
].pack('C*')
def addr
@_addr ||= User32.CallWindowProc GETADDR, object_id * 2, 0, 0, 0
end
def self.webp file
data = IO.binread file
w, h = buf('L'), buf('L')
WebP.WebPGetInfo(data, data.bytesize, w, h)
w = w.unpack[0]
h = h.unpack[0]
b = new w, h
WebP.WebPDecodeBGRAInto(data, data.bytesize, b.addr, w * h * 4, w * 4)
p = new w, h
r = Rect.new 0, 0, w, 1
((h + 1) / 2).times do |y|
r.y = h - 1 - y
p.blt 0, y, b, r
y, r.y = r.y, y
p.blt 0, y, b, r
end
b.dispose
p
end
end
@hyrious
Copy link
Author

hyrious commented Aug 19, 2019

WebP.dll is built by vcpkg: vcpkg\installed\x86-windows\bin\webp.dll

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment