Lua,位图读取的奇怪行为

时间:2013-07-31 03:17:25

标签: image image-processing lua

在我找到并修改过的位图阅读器中,一些图像的前几种颜色大量被遮挡,在其他图像中它完全读取,刚才是我必须拥有的前7种左右图像颜色它读起来都不准确。我不懂字节顺序,所以请帮忙!

继承我修改过的代码副本:

---
-- (Evil Steve)Because I'm a kind and wonderful person: http://www.gamedev.net/topic/572784-lua-read-bitmap/
---

function error(err)
    -- Replace with your own error output method:
    print(err);
end

-- Helper function: Parse a 16-bit WORD from the binary string
function ReadWORD(str, offset)
    local loByte = str:byte(offset);
    local hiByte = str:byte(offset+1);
    return hiByte*256 + loByte;
end

-- Helper function: Parse a 32-bit DWORD from the binary string
function ReadDWORD(str, offset)
    local loWord = ReadWORD(str, offset);
    local hiWord = ReadWORD(str, offset+2);
    return hiWord*65536 + loWord;
end

-- Process a bitmap file in a string, and call DrawPoint for each pixel
function OpenBitmap(File, Stream)
    if Stream == nil then Stream = false end
    local bytecode = File:read("*a")

    -------------------------
    -- Parse BITMAPFILEHEADER
    -------------------------
    local offset = 1;
    local bfType = ReadWORD(bytecode, offset);
    if(bfType ~= 0x4D42) then
        error("Not a bitmap file (Invalid BMP magic value)");
        return;
    end
    local bfOffBits = ReadWORD(bytecode, offset+10);

    -------------------------
    -- Parse BITMAPINFOHEADER
    -------------------------
    offset = 15; -- BITMAPFILEHEADER is 14 bytes long
    local biWidth = ReadDWORD(bytecode, offset+4);
    local biHeight = ReadDWORD(bytecode, offset+8);
    local biBitCount = ReadWORD(bytecode, offset+14);
    local biCompression = ReadDWORD(bytecode, offset+16);

    if(biBitCount ~= 24) then
        error("Only 24-bit bitmaps supported (Is " .. biBitCount .. "bpp)");
        return;
    end

    if(biCompression ~= 0) then
        error("Only uncompressed bitmaps supported (Compression type is " .. biCompression .. ")");
        return;
    end

    ---------------------
    -- Parse bitmap image
    ---------------------
    local TmpImg = {}
    if Stream == false then
        for y = biHeight-1, 0, -1 do
            offset = bfOffBits + (biWidth*biBitCount/8)*y + 1;
            for x = 0, biWidth-1 do
                local b = bytecode:byte(offset);
                local g = bytecode:byte(offset+1);
                local r = bytecode:byte(offset+2);
                offset = offset + 3;

                TmpImg[#TmpImg+1] = {r,g,b}
            end
        end
    else
        for y = biHeight-1, 0, -1 do
            offset = bfOffBits + (biWidth*biBitCount/8)*y + 1;
            for x = 0, biWidth-1 do
                local b = bytecode:byte(offset);
                local g = bytecode:byte(offset+1);
                local r = bytecode:byte(offset+2);
                offset = offset + 3;

                TmpImg[#TmpImg+1] = r
                TmpImg[#TmpImg+1] = g
                TmpImg[#TmpImg+1] = b
            end
        end
    end

    return TmpImg, biWidth, biHeight
end

function OpenBmp(FileName, Stream)
    if Stream == nil then Stream = false end
    if FileName == nil then
        return false
    end

    local File = assert(io.open(FileName, 'rb'))
    local Data, Width, Height = OpenBitmap(File, Stream)

    File:close()
    return Data, Width, Height
end

我遗憾的是,我无法提供我运行的代码,因为它有太多的依赖关系而烦恼,但它的输出是:

<254, 254, 254, 256>
<99, 254, 254, 256>
<49, 74, 91, 256>

使用以下bmp颜色运行时:

<90, 106, 113, 256>
<188, 194, 197, 256>
<254, 254, 254, 256>

我没有看到任何模式,并且bmp阅读器似乎有意义,它在阅读时没有打印错误,我确保将bmp保存为24位。所有帮助表示赞赏: - )

1 个答案:

答案 0 :(得分:0)

在上面的示例中,offset不考虑行宽必须始终是4个字节宽的倍数,如果它低于4的倍数,则填充它。您可以通过将行宽向上舍入到最近来解决此问题。 4的倍数,这解释了为什么函数有时准确地读取图像,有时则没有。

开始时像素的行为是假的,但后来准确的其余部分是由于爬行,从逻辑上讲,第一个是准确的,最后一个不准确,但由于位图的存在,爬行方向相反阅读自下而上和左右。