Skip to content

Commit

Permalink
Functions for Micro QR Code encoding have been added.
Browse files Browse the repository at this point in the history
  • Loading branch information
fukuchi committed May 30, 2009
1 parent 2dca5f4 commit 01ad229
Show file tree
Hide file tree
Showing 12 changed files with 678 additions and 355 deletions.
16 changes: 16 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@
enabled by default.
* rscode.c, qrspec.c:
- libqrencode has become thread-safe! (probably)
* tests/common.h:
- sprintfBin() removed, printBstream() added.
* qrinput.[ch], qrencode.[ch], mqrspec.c:
- Functions for Micro QR Code encoding have been added.
* tests/common.h:
- Utility functions improved.
* tests/*.c:
- Code cleanups.
- Tests for Micro QR Code added.

2009.05.30 Kentaro FUKUCHI <fukuchi@megaui.net>
* qrinput.c:
- padlen check was wrong in QRinput_appendPaddingBit().
* tests/test_qrinput.c:
- Stop printing bstream->data.
- test_padding2() has been added.

2009.05.21 Kentaro FUKUCHI <fukuchi@megaui.net>
* qrencode.c, qrencode_inner.h:
Expand Down
30 changes: 22 additions & 8 deletions mqrspec.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#ifdef HAVE_LIBPTHREAD
#include <pthread.h>
#endif

#include "mqrspec.h"

Expand All @@ -38,19 +43,19 @@

typedef struct {
int width; //< Edge length of the symbol
int ec[3]; //< Number of ECC code (bytes)
int ec[4]; //< Number of ECC code (bytes)
} MQRspec_Capacity;

/**
* Table of the capacity of symbols
* See Table 1 (pp.10) and Table 8 (pp.113) of Appendix 1, JIS X0510:2004.
* See Table 1 (pp.106) and Table 8 (pp.113) of Appendix 1, JIS X0510:2004.
*/
static const MQRspec_Capacity mqrspecCapacity[MQRSPEC_VERSION_MAX + 1] = {
{ 0, {0, 0, 0}},
{ 11, {2, 0, 0}},
{ 13, {5, 6, 0}},
{ 15, {6, 8, 0}},
{ 17, {8, 10, 14}}
{ 0, {0, 0, 0, 0}},
{ 11, {2, 0, 0, 0}},
{ 13, {5, 6, 0, 0}},
{ 15, {6, 8, 0, 0}},
{ 17, {8, 10, 14, 0}}
};

int MQRspec_getDataLength(int version, QRecLevel level)
Expand All @@ -61,7 +66,7 @@ int MQRspec_getDataLength(int version, QRecLevel level)
w = mqrspecCapacity[version].width - 1;
ecc = mqrspecCapacity[version].ec[level];
if(ecc == 0) return 0;

/* Warning again: unlike in QRSpec_getDataLength, return in BITS! */
return w * w - 64 - ecc * 8;
}

Expand Down Expand Up @@ -152,6 +157,9 @@ unsigned int MQRspec_getFormatInfo(int mask, int version, QRecLevel level)
/* C99 says that static storage shall be initialized to a null pointer
* by compiler. */
static unsigned char *frames[MQRSPEC_VERSION_MAX + 1];
#ifdef HAVE_LIBPTHREAD
static pthread_mutex_t frames_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif

/**
* Put a finder pattern.
Expand Down Expand Up @@ -231,9 +239,15 @@ unsigned char *MQRspec_newFrame(int version)

if(version < 1 || version > MQRSPEC_VERSION_MAX) return NULL;

#ifdef HAVE_LIBPTHREAD
pthread_mutex_lock(&frames_mutex);
#endif
if(frames[version] == NULL) {
frames[version] = MQRspec_createFrame(version);
}
#ifdef HAVE_LIBPTHREAD
pthread_mutex_unlock(&frames_mutex);
#endif
if(frames[version] == NULL) return NULL;

width = mqrspecCapacity[version].width;
Expand Down
7 changes: 5 additions & 2 deletions qrencode.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ __STATIC MQRRawCode *MQRraw_new(QRinput *input)
{
MQRRawCode *raw;
RS *rs;
int dl;

raw = (MQRRawCode *)malloc(sizeof(MQRRawCode));
if(raw == NULL) return NULL;
Expand All @@ -227,13 +228,14 @@ __STATIC MQRRawCode *MQRraw_new(QRinput *input)
return NULL;
}

rs = init_rs(8, 0x11d, 0, 1, raw->eccLength, 255 - raw->dataLength - raw->eccLength);
dl = (raw->dataLength + 4)/ 8; // M1 and M3 has 4 bit length data code.
rs = init_rs(8, 0x11d, 0, 1, raw->eccLength, 255 - dl - raw->eccLength);
if(rs == NULL) {
MQRraw_free(raw);
return NULL;
}

RSblock_initBlock(raw->rsblock, raw->dataLength, raw->datacode, raw->eccLength, raw->ecccode, rs);
RSblock_initBlock(raw->rsblock, dl, raw->datacode, raw->eccLength, raw->ecccode, rs);

raw->count = 0;

Expand All @@ -245,6 +247,7 @@ __STATIC MQRRawCode *MQRraw_new(QRinput *input)
* This function can be called iteratively.
* @param raw raw code.
* @return code
* FIXME: M1 and M3 has 4bit-length data code!
*/
__STATIC unsigned char MQRraw_getCode(MQRRawCode *raw)
{
Expand Down
14 changes: 14 additions & 0 deletions qrencode.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ extern QRinput *QRinput_new(void);
*/
extern QRinput *QRinput_new2(int version, QRecLevel level);

/**
* Instantiate an input data object. Object's Micro QR Code flag is set.
* Unlike with full-sized QR Code, version number must be specified (>0).
* @param version version number (1--4).
* @param level Error correction level.
* @return an input object (initialized). On error, NULL is returned and errno
* is set to indicate the error.
* @throw ENOMEM unable to allocate memory for input objects.
* @throw EINVAL invalid arguments.
*/
extern QRinput *QRinput_newMQR(int version, QRecLevel level);

/**
* Append data to an input object.
* The data is copied and appended to the input object.
Expand All @@ -182,6 +194,7 @@ extern int QRinput_getVersion(QRinput *input);

/**
* Set version of the QR-code that is to be encoded.
* This function cannot be applied to Micro QR Code.
* @param input input object.
* @param version version number (0 = auto)
* @retval 0 success.
Expand All @@ -198,6 +211,7 @@ extern QRecLevel QRinput_getErrorCorrectionLevel(QRinput *input);

/**
* Set error correction level of the QR-code that is to be encoded.
* This function cannot be applied to Micro QR Code.
* @param input input object.
* @param level Error correction level.
* @retval 0 success.
Expand Down
Loading

0 comments on commit 01ad229

Please sign in to comment.