0
在一個位圖的讀者,我發現和修改的古怪行爲時,一些圖像的前幾個顏色是由大量innacurate,在其他的圖像,它只是現在讀取完美,在,前7點左右的顏色我必須閱讀的圖像根本不準確。我不明白字節順序,所以請幫忙!Lua中,有位讀書
繼承人我修改了代碼的副本:
---
-- (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
我不能給你,我這個跑,可悲的是代碼,因爲它有太多的依賴與打擾,但它的輸出是:
<90, 106, 113, 256>
<188, 194, 197, 256>
<254, 254, 254, 256>
我看不出有任何的圖案,以及b:
<254, 254, 254, 256>
<99, 254, 254, 256>
<49, 74, 91, 256>
當以下BMP顏色跑mp閱讀器似乎是有道理的,它在閱讀時不打印錯誤,並且我確保根據需要將bmp保存爲24位。所有的幫助表示讚賞:-)
俞灝,我包括一個鏈接到這個函數的源代碼,因爲我想這可能是有用的人審查我的職務,作出了比較,看看我變了,這是對堆棧溢出規則?我並不想讓它成爲「垃圾郵件」 –
然後我錯了稱之爲「垃圾郵件」的編輯,我們對此深感抱歉。但是,你仍然應該在問題中加上一些解釋的鏈接,而不是在句子後面的代碼註釋中:*(Evil Steve)因爲我是一個善良而美好的人。我現在還不明白。 –
這就是我被授予代碼的權限,以及通過論壇上的公開帖子提供給我的人的用戶名。我把它保留在那裏是值得稱讚的,因爲lua代碼很容易複製,它可以落在別人手中,他們可能認爲這是我的原創作品(事實並非如此)。以及我不想要任何法律問題,因爲它沒有抄寫,所以我認爲明智地採用它,以免將來被解讀爲盜竊。 –