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

optimizations to encode function #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 12 additions & 25 deletions qrcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "qrcodegen.h"
#include "string.h"

STATIC mp_obj_t qrcode_encode(mp_obj_t text_obj){
STATIC mp_obj_t qrcode_encode(mp_obj_t text_obj) {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(text_obj, &bufinfo, MP_BUFFER_READ);

Expand All @@ -16,41 +16,28 @@ STATIC mp_obj_t qrcode_encode(mp_obj_t text_obj){
memcpy(tempBuffer, bufinfo.buf, bufinfo.len);
bool ok = false;
bool is_bytes = false;
for(int i = 0; i < bufinfo.len; i++){
if(tempBuffer[i] == 0){ // \x00 will cut the byte array
for (int i = 0; i < bufinfo.len; i++) {
if (tempBuffer[i] == 0) { // \x00 will cut the byte array
is_bytes = true;
}
}
if(is_bytes){
if (is_bytes) {
ok = qrcodegen_encodeBinary(tempBuffer, bufinfo.len, qrcode, errCorLvl,
qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);
}else{
qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);
} else {
ok = qrcodegen_encodeText(bufinfo.buf, tempBuffer, qrcode, errCorLvl,
qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);
qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);
}
if(!ok){
if (!ok) {
mp_raise_ValueError("Failed to encode");
}
int size = qrcodegen_getSize(qrcode);
// align to 8 bits and add 2-block border
int imgsize = (size/8+1)*8+4;
int lpad = (imgsize-size)/2;
size_t bufsize = (imgsize*imgsize)/8;

size_t bufsize = (size * size + 7) / 8;
vstr_t vstr;
vstr_init_len(&vstr, bufsize);
memset((byte*)vstr.buf, 0, bufsize);
size_t cur = imgsize*lpad;
for(int y=0; y<imgsize-lpad; y++){
cur+=lpad;
for(int x=0; x<imgsize-lpad; x++){
vstr.buf[cur/8] = vstr.buf[cur/8] << 1;
if(x<size && y<size){
if(qrcodegen_getModule(qrcode, x, y)){
vstr.buf[cur/8]++;
}
}
cur++;
}
for (size_t i = 0; i < bufsize - 1; ++i) {
vstr.buf[i] = qrcode[(i + 1)];
}
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
Expand Down