Commit 03b59ce6 by nosa

add rockspec and ansi type

parent 8ebca45a
2014-10-30 vinoca <vinoca@vinoca.org>
* qrencode.c:
* add ansi type.
[1.0]
* add luarocks rockspec.
- Bumped verstion to 3.4.4.
2014-10-16 vinoca <vinoca@vinoca.org> 2014-10-16 vinoca <vinoca@vinoca.org>
* Bumped version to 1.0.0. * Bumped version to 1.0.0.
# Makefile for qrencode library for Lua
PREFIX=/usr/local LIBNAME= qrencode
PROGS = qrencode LUAEXE= lua
CFLAGS = -Wall -fPIC -shared
LDFLAGS = -lqrencode -lpng
ROCKSPEC= $(shell find . -name $(LIBNAME)-*-*.rockspec)
all: install
install:
luarocks make $(ROCKSPEC)
test:
$(LUAEXE) test/test.lua
.PHONY: all test install
all: $(PROGS)
%: %.c
$(CC) $(CFLAGS) $(LDFLAGS) -o $@.so $^
clean:
$(RM) $(PROGS).so
...@@ -15,13 +15,20 @@ qr = require "qrencode" ...@@ -15,13 +15,20 @@ qr = require "qrencode"
-- print PNG data stream to stdout. -- print PNG data stream to stdout.
-- print(qr.encode("is ok?")) print(qr.encode("is ok?"))
print(qr:encode("is ok?"))
print(qr("is ok?"))
-- print ansi char
print(qr {text = "is ok?", ansi = true})
-- or pass a table : -- or pass a table :
print(qr.encode(
{ print(qr {
text="is ok?", text="is ok?",
level="L", level="L",
kanji=false, kanji=false,
ansi=false,
size=3, size=3,
symversion=0, symversion=0,
dpi=80, dpi=80,
...@@ -31,7 +38,6 @@ print(qr.encode( ...@@ -31,7 +38,6 @@ print(qr.encode(
background="#3FAF6F" background="#3FAF6F"
} }
) )
)
``` ```
......
#!/bin/sh
prefix='/usr/local'
while [ -n "$1" ]; do
e="`expr "$1" : '--\(.*=.*\)'`"
if [ -z "$e" ]; then
cat<<EOF
Usage: $0 [OPTIONS]
Option Description Default
--prefix=... Directories to put files in $prefix
EOF
exit;
fi
keyname=$(expr "$e" : '\(.*\)=.*' | sed 's/[^a-z0-9_]/_/g')
value=$(expr "$e" : '.*=\(.*\)' | sed "s/'/_/g")
eval "$keyname='$value'"
shift;
done
grep -q "PREFIX=$prefix" Makefile || sed -i "1a\PREFIX=$prefix" Makefile
package = "qrencode"
version = "0.0.1-1"
source = {
url = "qr.tar.gz",
file = "qr.tar.gz"
}
description = {
summary = "qrencode is a wrapper of libqrencode with libpng for lua",
detailed = [[
qrencode is a wrapper of libqrencode with libpng for lua.
]],
license = "MIT/X11",
homepage = "http://fukuchi.org/works/qrencode/"
}
dependencies = {
"lua >= 5.1"
}
build = {
type = "builtin",
modules = {
qrencode = {
sources = { "qrencode.c" },
libraries = { "png", "qrencode" },
}
},
}
...@@ -11,11 +11,20 @@ ...@@ -11,11 +11,20 @@
#include <png.h> #include <png.h>
struct mem_data { #if LUA_VERSION_NUM < 502
# define luaL_newlib(L,l) (lua_newtable(L), luaL_register(L,NULL,l))
#endif
struct memData {
char *buffer; char *buffer;
size_t size; size_t size;
}; };
enum imageType {
PNG_TYPE,
ANSI_TYPE
};
#define INCHES_PER_METER (100.0/2.54) #define INCHES_PER_METER (100.0/2.54)
static int casesensitive = 1; static int casesensitive = 1;
...@@ -26,13 +35,15 @@ static int margin = 4; ...@@ -26,13 +35,15 @@ static int margin = 4;
static int dpi = 72; static int dpi = 72;
static QRecLevel level = QR_ECLEVEL_L; static QRecLevel level = QR_ECLEVEL_L;
static QRencodeMode hint = QR_MODE_8; static QRencodeMode hint = QR_MODE_8;
static enum imageType image_type = PNG_TYPE;
static unsigned int fg_color[4] = {0, 0, 0, 255}; static unsigned int fg_color[4] = {0, 0, 0, 255};
static unsigned int bg_color[4] = {255, 255, 255, 255}; static unsigned int bg_color[4] = {255, 255, 255, 255};
/* png io callback function */ /* png io callback function */
static void cp_png_data(png_structp png_ptr, png_bytep data, png_size_t length) static void cp_png_data(png_structp png_ptr, png_bytep data, png_size_t length)
{ {
struct mem_data* p = (struct mem_data*)png_get_io_ptr(png_ptr); struct memData* p = (struct memData*)png_get_io_ptr(png_ptr);
p->buffer = realloc(p->buffer, p->size + length); p->buffer = realloc(p->buffer, p->size + length);
if (!p->buffer) if (!p->buffer)
...@@ -43,7 +54,7 @@ static void cp_png_data(png_structp png_ptr, png_bytep data, png_size_t length) ...@@ -43,7 +54,7 @@ static void cp_png_data(png_structp png_ptr, png_bytep data, png_size_t length)
} }
/* 0:failure/1:true to_png( qrcode: qr data , result: buffer to save result.) {{{ */ /* 0:failure/1:true to_png( qrcode: qr data , result: buffer to save result.) {{{ */
static int to_png(QRcode *qrcode, struct mem_data *result) static int to_png(QRcode *qrcode, struct memData *result)
{ {
png_structp png_ptr; png_structp png_ptr;
png_infop info_ptr; png_infop info_ptr;
...@@ -155,6 +166,104 @@ static int to_png(QRcode *qrcode, struct mem_data *result) ...@@ -155,6 +166,104 @@ static int to_png(QRcode *qrcode, struct mem_data *result)
} }
/* }}} */ /* }}} */
/* buffer is in, and result is out */
static void mputs(char *buffer, struct memData *result)
{
size_t length = strlen(buffer);
result->buffer = realloc(result->buffer, result->size + length);
if (!result->buffer) {
fprintf(stderr, "Write Error.\n");
exit(EXIT_FAILURE);
}
memcpy(result->buffer + result->size, buffer, length);
result->size += length;
}
/* to_ansi {{{ */
static int to_ansi(QRcode *qrcode, struct memData *result)
{
unsigned char *row, *p;
int x, y;
int realwidth;
int last;
char *white, *black, *buffer;
int white_s, black_s, buffer_s;
white = "\033[47m";
white_s = 5;
black = "\033[40m";
black_s = 5;
size = 1;
realwidth = (qrcode->width + margin * 2) * size;
buffer_s = ( realwidth * white_s ) * 2;
buffer = (char *)malloc( buffer_s );
if(buffer == NULL) {
fprintf(stderr, "Failed to allocate memory.\n");
exit(EXIT_FAILURE);
}
/* top margin */
strncpy(buffer, white, white_s);
memset(buffer + white_s, ' ', realwidth * 2);
strcpy(buffer + white_s + realwidth * 2, "\033[0m\n"); // reset to default colors
for(y=0; y<margin; y++ ){
mputs(buffer, result);
}
/* data */
p = qrcode->data;
for(y=0; y<qrcode->width; y++) {
row = (p+(y*qrcode->width));
bzero( buffer, buffer_s );
strncpy( buffer, white, white_s );
for(x=0; x<margin; x++ ){
strncat( buffer, " ", 2 );
}
last = 0;
for(x=0; x<qrcode->width; x++) {
if(*(row+x)&0x1) {
if( last != 1 ){
strncat( buffer, black, black_s );
last = 1;
}
} else {
if( last != 0 ){
strncat( buffer, white, white_s );
last = 0;
}
}
strncat( buffer, " ", 2 );
}
if( last != 0 ){
strncat( buffer, white, white_s );
}
for(x=0; x<margin; x++ ){
strncat( buffer, " ", 2 );
}
strncat( buffer, "\033[0m\n", 5 );
mputs( buffer, result );
}
/* bottom margin */
strncpy(buffer, white, white_s);
memset(buffer + white_s, ' ', realwidth * 2);
strcpy(buffer + white_s + realwidth * 2, "\033[0m\n"); // reset to default colors
for(y=0; y<margin; y++ ){
mputs(buffer, result);
}
free(buffer);
return 0;
}
/* }}} */
static void set_error_info(lua_State *L, const char *info) static void set_error_info(lua_State *L, const char *info)
{ {
lua_pushstring(L, info); lua_pushstring(L, info);
...@@ -163,7 +272,7 @@ static void set_error_info(lua_State *L, const char *info) ...@@ -163,7 +272,7 @@ static void set_error_info(lua_State *L, const char *info)
lua_error(L); lua_error(L);
} }
static void set_qr_color(lua_State *L, unsigned int color[4], const char * field) static void set_png_color(lua_State *L, unsigned int color[4], const char * field)
{ {
size_t len, count; size_t len, count;
const char * value; const char * value;
...@@ -225,13 +334,18 @@ static int encode (lua_State *L) ...@@ -225,13 +334,18 @@ static int encode (lua_State *L)
{ {
QRcode *qrcode; QRcode *qrcode;
const char *intext; const char *intext;
struct mem_data png_data; struct memData result;
png_data.buffer = NULL; result.buffer = NULL;
png_data.size = 0; result.size = 0;
image_type = PNG_TYPE;
if (lua_istable(L, 1)) { if ((lua_gettop(L) == 1 && lua_istable(L, 1)) ||
lua_gettop(L) == 2 && lua_istable(L, 2)) {
if (lua_gettop(L) == 2)
lua_remove(L, 1);
/* text field must exist and type of this is string */
lua_pushstring(L, "text"); lua_pushstring(L, "text");
lua_gettable(L, 1); lua_gettable(L, 1);
intext = luaL_checkstring(L, -1); intext = luaL_checkstring(L, -1);
...@@ -277,17 +391,35 @@ static int encode (lua_State *L) ...@@ -277,17 +391,35 @@ static int encode (lua_State *L)
} }
lua_pop(L, 1); lua_pop(L, 1);
lua_pushstring(L, "ansi");
lua_gettable(L, 1);
if (!lua_isnil(L, 2)) {
if (!lua_isboolean(L, 2)) {
lua_pushstring(L, "ansi is not \"true\" or \"false\"");
lua_error(L);
}
if(lua_toboolean(L, 2))
image_type = ANSI_TYPE;
}
lua_pop(L, 1);
set_qr_int(L, &size, "size"); set_qr_int(L, &size, "size");
set_qr_int(L, &version, "symversion"); set_qr_int(L, &version, "symversion");
set_qr_int(L, &margin, "margin"); set_qr_int(L, &margin, "margin");
set_qr_int(L, &dpi, "dpi"); set_qr_int(L, &dpi, "dpi");
set_qr_boolean(L,&casesensitive, "casesensitive"); set_qr_boolean(L,&casesensitive, "casesensitive");
set_qr_boolean(L,&eightbit, "eightbit"); set_qr_boolean(L,&eightbit, "eightbit");
set_qr_color(L, fg_color, "foreground"); set_png_color(L, fg_color, "foreground");
set_qr_color(L, bg_color, "background"); set_png_color(L, bg_color, "background");
}
else if (lua_gettop(L) == 1)
intext = luaL_checkstring(L, 1);
else if (lua_gettop(L) == 2 && lua_istable(L, 1))
intext = luaL_checkstring(L, 2);
else {
lua_pushstring(L, "argument is invalid.");
lua_error(L);
} }
else
intext = luaL_checkstring(L, 1);
if (strlen(intext) <= 0) { if (strlen(intext) <= 0) {
lua_pushstring(L, "The input data is invalid"); lua_pushstring(L, "The input data is invalid");
...@@ -300,26 +432,38 @@ static int encode (lua_State *L) ...@@ -300,26 +432,38 @@ static int encode (lua_State *L)
lua_pushstring(L, "Failed to encode the input data"); lua_pushstring(L, "Failed to encode the input data");
return 2; return 2;
} }
if (!to_png(qrcode, &png_data)) {
lua_pushnil(L); if ( image_type == PNG_TYPE) {
lua_pushstring(L, "Failed to encode the input data to png"); if (!to_png(qrcode, &result)) {
return 2; lua_pushnil(L);
lua_pushstring(L, "Failed to encode the input data to png");
return 2;
}
} else if ( image_type == ANSI_TYPE) {
to_ansi(qrcode, &result);
} else {
lua_pushstring(L, "image type is invalid.");
lua_error(L);
} }
QRcode_free(qrcode); QRcode_free(qrcode);
lua_pushlstring(L, png_data.buffer, png_data.size); lua_pushlstring(L, result.buffer, result.size);
free(png_data.buffer); free(result.buffer);
return 1; return 1;
} }
static const struct luaL_Reg qr [] = { static const struct luaL_Reg R [] = {
{"__call", encode},
{"encode", encode}, {"encode", encode},
{NULL, NULL} {NULL, NULL}
}; };
int luaopen_qrencode (lua_State *L) int luaopen_qrencode (lua_State *L)
{ {
luaL_register(L, "qrencode", qr); //luaL_register(L, "qrencode", R);
luaL_newlib(L, R);
lua_pushvalue(L, -1);
lua_setmetatable(L, -2);
return 1; return 1;
} }
......
qr = require("qrencode")
print(qr.encode("is ok?"))
print(qr:encode("is ok?"))
print(qr("is ok?"))
print(qr {text = "is ok?", ansi = true})
print(qr {
text="123",
level="L",
kanji=false,
ansi=false,
size=3,
symversion=0,
dpi=80,
casesensitive=true,
eightbit=false,
foreground="#48AF6D",
background="#3FAF6F"
}
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment