前言

目前游戏中有一个需要传送大量点数据来表示一个地图的需求

做了简单测试,如果要以文本传送512*512的地图数据大小为20KB左右

我想了一个办法把数据写到图片的一个像素中,以不同的颜色来代表类型和地形

然后通过base64同样尺寸的jpeg图片,在100%质量的情况下只有8kb的大小

安装luarocks

1
2
3
4
5
6
7
8
9
$ wget https://keplerproject.github.io/luarocks/releases/luarocks-2.2.2.tar.gz
$ tar zxf luarocks-2.2.2.tar.gz
$ cd luarocks-2.2.2
$ ./configure --prefix=/data/soft/openresty/luajit \ 
--with-lua=/data/soft/openresty/luajit/ \
--lua-suffix=jit-2.1.0-alpha \
--with-lua-include=/data/soft/openresty/luajit/include/luajit-2.1
$ make build
$ make install

See:http://openresty.org/en/using-luarocks.html

安装ImageMagick库

1
2
$ yum install ImageMagick-devel
$ /data/soft/openresty/luajit/bin/luarocks install magick

GG

这个把数据压缩成图的方法最终还是流产了

因为lua对图形的操作太弱了,没有合适的库,我尝试对magick库增加new image和set pixel的方法

中途遇到了各种诡异的问题,而且生成图的效率有点低下

部分代码片段

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# in lib.lua
MagickBooleanType MagickNewImage(MagickWand*,  const size_t*, const size_t*, const PixelWand*);

# in lib.moon
MagickBooleanType MagickNewImage(MagickWand*,  const size_t*, const size_t*, const PixelWand*);


# in init.lua

local new_image
new_image = function(width, height, rgb)
    local wand = ffi.gc(lib.NewMagickWand(), lib.DestroyMagickWand)

    local pixel = ffi.gc(lib.NewPixelWand(), lib.DestroyPixelWand)
    lib.PixelSetRed(pixel, rgb.r)
    lib.PixelSetGreen(pixel, rgb.g)
    lib.PixelSetBlue(pixel, rgb.b)

    if 0 == lib.MagickNewImage(wand, ffi.new("size_t[?]", width),  ffi.new("size_t[?]", height), pixel) then
        local code, msg = get_exception(wand)

        ngx.say("code", code, "msgs", msg)

        return nil, msg, code
    end

    ngx.say("ok")

    return Image(wand, nil)
end

See: http://www.imagemagick.org/api/magick-image.php#MagickNewImage https://github.com/leafo/magick