Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incorrect encoding for repeating 0 #4

Closed
mutoo opened this issue Jun 3, 2020 · 10 comments
Closed

incorrect encoding for repeating 0 #4

mutoo opened this issue Jun 3, 2020 · 10 comments
Assignees

Comments

@mutoo
Copy link
Contributor

mutoo commented Jun 3, 2020

Hi Patrick, thanks for your great work on this project. I am using luaqrcode in my aseprite plugin to generate qrcode from pixel art.

But I found an issue when I try to generate a qrcode with a long repeating 0x00 (pixels with 0th palette) with ec_level = M, the decoder returns a lot of 1s among the 0s, which causing dirty dots when we decode the image from qrcode.

Here is the code to replica the bug:

local data = {}
for i = 1, 255 do
    table.insert(data, 0)
end

qrencode.qrcode(data, 2)

this code generates a qrcode which is decoded by https://zxing.org/w/decode.jspx as following data:

image

40 0f f0 00 00 00 00 00   00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00
00 00 00 00 01 00 00 00   00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00   01 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00   00 00 00 00 01 00 00 00
00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00
01 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00
00 00 00 00 01 00 00 00   00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00   01 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00   00 00 00 00 00 01 00 00
00 00 ec 11 ec 11 ec 11   ec 11 ec 11 ec 11 ec 11
ec 11 ec 11 ec 11 ec 11   ec 11 ec 11 ec 11 ec 11
ec 11 

As you can see there are 01s inside the data, which should be all 00.

I had tried to figure it out for a while, but it's far out of my scope, I'd appreciate it if you could help.

Thanks,
mutoo

@pgundlach pgundlach self-assigned this Jun 3, 2020
@pgundlach
Copy link
Member

Hello mutoo, thanks for the report. I'll try to find out what goes wrong, but I cannot promise anything. The last time I looked into the code I felt like somebody else has written it...

@pgundlach pgundlach assigned mutoo and unassigned pgundlach Jun 3, 2020
@pgundlach
Copy link
Member

You have used a function encode() which is (as far as I can see) not part of the qrenocde library. So you need to provide more feedback.

@mutoo
Copy link
Contributor Author

mutoo commented Jun 3, 2020

You have used a function encode() which is (as far as I can see) not part of the qrenocde library. So you need to provide more feedback.

oh sorry, it's my typo. it should be qrencode.qrcode(data, 2)

@mutoo mutoo removed their assignment Jun 3, 2020
@pgundlach
Copy link
Member

OK, then I get the following error:

lua: qrencode.lua:179: bad argument #1 to 'match' (string expected, got table)
stack traceback:
	[C]: in function 'string.match'
	qrencode.lua:179: in upvalue 'get_mode'
	qrencode.lua:310: in upvalue 'get_version_eclevel_mode_bistringlength'
	qrencode.lua:1311: in field 'qrcode'
	foo.lua:8: in main chunk
	[C]: in ?

You don't get the same error?

@mutoo
Copy link
Contributor Author

mutoo commented Jun 3, 2020

@pgundlach the pseudo-code above may not express my idea. could you try this one:

local data = {}
for i = 1, 255 do
    table.insert(data, 0)
end

local bitstr = ""
for i, v in ipairs(data) do
    bitstr = bitstr .. string.char(v)
end

qrencode.qrcode(bitstr, 2)

@pgundlach pgundlach self-assigned this Jun 3, 2020
@pgundlach
Copy link
Member

OK, I can produce a qr code now.

@mutoo
Copy link
Contributor Author

mutoo commented Jun 11, 2020

Hi, Patrick, I spent some time to dig a little bit into the code with the help of this article:
https://www.thonky.com/qr-code-tutorial/error-correction-coding

I found there are some issues within the calculate_error_correction() function. Once there is a block of the message with all 0s, it's like an edge case, the polynomial long division may yield a wrong result.

The article doesn't mention how to deal with such a case, I may keep digging when I've got time.

@pgundlach
Copy link
Member

I have no idea what could be wrong.

@mutoo
Copy link
Contributor Author

mutoo commented Jun 12, 2020

Hi Patrick, with the fix I had committed, the QRcode now works well:

qrcode#4

40 0f f0 00 00 00 00 00   00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00
00 00 ec 11 ec 11 ec 11   ec 11 ec 11 ec 11 ec 11
ec 11 ec 11 ec 11 ec 11   ec 11 ec 11 ec 11 ec 11
ec 11 

Could you please review the pull request and let me know if I need to do anything.

Thanks,
mutoo

pgundlach added a commit that referenced this issue Jun 12, 2020
* mutoo-issue/issue-4-leading-0s:
  fix #4 message with leading 0s encoding issue
@pgundlach
Copy link
Member

Thank you very much!

pgundlach added a commit to speedata/publisher that referenced this issue Jun 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants