qrencode.c ( File view )

  • By green75 2014-11-06
  • View(s):334
  • Download(s):8
  • Point(s): 3
			/*
 * qrencode - QR Code encoder
 *
 * Copyright (C) 2006-2012 Kentaro Fukuchi <kentaro@fukuchi.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#if HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "qrencode.h"
#include "qrspec.h"
#include "mqrspec.h"
#include "bitstream.h"
#include "qrinput.h"
#include "rscode.h"
#include "split.h"
#include "mask.h"
#include "mmask.h"

/******************************************************************************
 * Raw code
 *****************************************************************************/

typedef struct {

	int dataLength;
	unsigned char *data;
	int eccLength;
	unsigned char *ecc;

} RSblock;

typedef struct {

	int version;
	int dataLength;
	int eccLength;
	unsigned char *datacode;
	unsigned char *ecccode;
	int b1;
	int blocks;
	RSblock *rsblock;
	int count;

} QRRawCode;

static void RSblock_initBlock(RSblock *block, int dl, unsigned char *data, int el, unsigned char *ecc, RS *rs)
{

	block->dataLength = dl;
	block->data = data;
	block->eccLength = el;
	block->ecc = ecc;

	encode_rs_char(rs, data, ecc);

}

static int RSblock_init(RSblock *blocks, int spec[5], unsigned char *data, unsigned char *ecc)
{

	int i;
	RSblock *block;
	unsigned char *dp, *ep;
	RS *rs;
	int el, dl;

	dl = QRspec_rsDataCodes1(spec);
	el = QRspec_rsEccCodes1(spec);
	rs = init_rs(8, 0x11d, 0, 1, el, 255 - dl - el);
	if(rs == NULL) return -1;

	block = blocks;
	dp = data;
	ep = ecc;
	for(i=0; i<QRspec_rsBlockNum1(spec); i++) {

		RSblock_initBlock(block, dl, dp, el, ep, rs);
		dp += dl;
		ep += el;
		block++;
	
}

	if(QRspec_rsBlockNum2(spec) == 0) return 0;

	dl = QRspec_rsDataCodes2(spec);
	el = QRspec_rsEccCodes2(spec);
	rs = init_rs(8, 0x11d, 0, 1, el, 255 - dl - el);
	if(rs == NULL) return -1;
	for(i=0; i<QRspec_rsBlockNum2(spec); i++) {

		RSblock_initBlock(block, dl, dp, el, ep, rs);
		dp += dl;
		ep += el;
		block++;
	
}

	return 0;

}

__STATIC void QRraw_free(QRRawCode *raw);
__STATIC QRRawCode *QRraw_new(QRinput *input)
{

	QRRawCode *raw;
	int spec[5], ret;

	raw = (QRRawCode *)malloc(sizeof(QRRawCode));
	if(raw == NULL) return NULL;

	raw->datacode = QRinput_getByteStream(input);
	if(raw->datacode == NULL) {

		free(raw);
		return NULL;
	
}

	QRspec_getEccSpec(input->version, input->level, spec);

	raw->version = input->version;
	raw->b1 = QRspec_rsBlockNum1(spec);
	raw->dataLength = QRspec_rsDataLength(spec);
	raw->eccLength = QRspec_rsEccLength(spec);
	raw->ecccode = (unsigned char *)malloc(raw->eccLength);
	if(raw->ecccode == NULL) {

		free(raw->datacode);
		free(raw);
		return NULL;
	
}

	raw->blocks = QRspec_rsBlockNum(spec);
	raw->rsblock = (RSblock *)calloc(raw->blocks, sizeof(RSblock));
	if(raw->rsblock == NULL) {

		QRraw_free(raw);
		return NULL;
	
}
	ret = RSblock_init(raw->rsblock, spec, raw->datacode, raw->ecccode);
	if(ret < 0) {

		QRraw_free(raw);
		return NULL;
	
}

	raw->count = 0;

	return raw;

}

/**
 * Return a code (byte).
 * This function can be called iteratively.
 * @param raw raw code.
 * @return code
 */
__STATIC unsigned char QRraw_getCode(QRRawCode *raw)
{

	int col, row;
	unsigned char ret;

	if(raw->count < raw->dataLength) {

		row = raw->count % raw->blocks;
		col = raw->count / raw->blocks;
		if(col >= raw->rsblock[0].dataLength) {

			row += raw->b1;
		
}
		ret = raw->rsblock[row].data[col];
	
} else if(raw->count < raw->dataLength + raw->eccLength) {

		row = (raw->count - raw->dataLength) % raw->blocks;
		col = (raw->count - raw->dataLength) / raw->blocks;
		ret = raw->rsblock[row].ecc[col];
	
} else {

		return 0;
	
}
	raw->count++;
	return ret;

}

__STATIC void QRraw_free(QRRawCode *raw)
{

	if(raw != NULL) {

		free(raw->datacode);
		free(raw->ecccode);
		free(raw->rsblock);
		free(raw);
	
}

}

/******************************************************************************
 * Raw code for Micro QR Code
 *****************************************************************************/

typedef struct {

	int version;
	int dataLength;
	int eccLength;
	unsigned char *datacode;
	unsigned char *ecccode;
	RSblock *rsblock;
	int oddbits;
	int count;

} MQRRawCode;

__STATIC void MQRraw_free(MQRRawCode *raw);
__STATIC MQRRawCode *MQRraw_new(QRinput *input)
{

	MQRRawCode *raw;
	RS *rs;

	raw = (MQRRawCode *)malloc(sizeof(MQRRawCode));
	if(raw == NULL) return NULL;

	raw->version = input->version;
	raw->dataLength = MQRspec_getDataLength(input->version, input->level);
	raw->eccLength = MQRspec_getECCLength(input->version, input->level);
	raw->oddbits = raw->dataLength * 8 - MQRspec_getDataLengthBit(input->version, input->level);
	raw->datacode = QRinput_getByteStream(input);
	if(raw->datacode == NULL) {

		free(raw);
		return NULL;
	
}
	raw->ecccode = (unsigned char *)malloc(raw->eccLength);
	if(raw->ecccode == NULL) {

		free(raw->datacode);
		free(raw);
		return NULL;
	
}

	raw->rsblock = (RSblock *)calloc(1, sizeof(RSblock));
	if(raw->rsblock == NULL) {

		MQRraw_free(raw);
		return NULL;
	
}

	rs = init_rs(8, 0x11d, 0, 1, raw->eccLength, 255 - raw->dataLength - raw->eccLength);
	if(rs == NULL) {

		MQRraw_free(raw);
		return NULL;
	
}

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

	raw->count = 0;

	return raw;

}

/**
 * Return a code (byte).
 * This function can be called iteratively.
 * @param raw raw code.
 * @return code
 */
__STATIC unsigned char MQRraw_getCode(MQRRawCode *raw)
{

	unsigned char ret;

	if(raw->count < raw->dataLength) {

		ret = raw->datacode[raw->count];
	
} else if(raw->count < raw->dataLength + raw->eccLength) {

		ret = raw->ecccode[raw->count - raw->dataLength];
	
} else {

		return 0;
	
}
	raw->count++;
	return ret;

}

__STATIC void MQRraw_free(MQRRawCode *raw)
{

	if(raw != NULL) {

		free(raw->datacode);
		free(raw->ecccode);
		free(raw->rsblock);
		free(raw);
	
}

}


/******************************************************************************
 * Frame filling
 *****************************************************************************/

typedef struct {

	int width;
	unsigned char *frame;
	int x, y;
	int dir;
	int bit;
	int mqr;

} FrameFiller;

static FrameFiller *FrameFiller_new(int width, unsigned char *frame, int mqr)
{

	FrameFiller *filler;

	filler = (FrameFiller *)malloc(sizeof(FrameFiller));
	if(filler == NULL) return NULL;
	filler->width = width;
	filler->frame = frame;
	filler->x = width - 1;
	filler->y = width - 1;
	filler->dir = -1;
	filler->bit = -1;
	filler->mqr = mqr;

	return filler;

}

static unsigned char *FrameFiller_next(FrameFiller *filler)
{

	unsigned char *p;
	int x, y, w;

	if(filler->bit == -1) {

		filler->bit = 0;
		return filler->frame + filler->y * filler->width + filler->x;
	
}

	x = filler->x;
	y = filler->y;
	p = filler->frame;
	w = filler->width;

	if(filler->bit == 0) {

		x--;
		filler->bit++;
	
} else {

		x++;
		y += filler->dir;
		filler->bit--;
	
}

	if(filler->dir < 0) {

		if(y < 0) {

			y = 0;
			x -= 2;
			filler->dir = 1;
			if(!filler->mqr && x == 6) {

				x--;
				y = 9;
			
}
		
}
	
} else {

		if(y == w) {

			y = w - 1;
			x -= 2;
			filler->dir = -1;
			if(!filler->mqr && x == 6) {

				x--;
				y -= 8;
			
}
		
}
	
}
	if(x < 0 || y < 0) return NULL;

	filler->x = x;
	filler->y = y;

	if(p[y * w + x] & 0x80) {

		// This tail recursion could be optimized.
		return FrameFiller_next(filler);
	
}
	return &p[y * w + x];

}

#ifdef WITH_TESTS
extern unsigned char *FrameFiller_test(int version)
{

	int width;
	unsigned char *frame, *p;
	FrameFiller *filler;
	int i, length;

	width = QRspec_getWidth(version);
	frame = QRspec_newFrame(version);
	if(frame == NULL) return NULL;
	filler = FrameFiller_new(width, frame, 0);
	if(filler == NULL) {

		free(frame);
		return NULL;
	
}
	length = QRspec_getDataLength(version, QR_ECLEVEL_L) * 8
	       + QRspec_getECCLength(version, QR_ECLEVEL_L) * 8
		   + QRspec_getRemainder(version);
	for(i=0; i<length; i++) {

		p = FrameFiller_next(filler);
		if(p == NULL) {

			free(filler);
			free(frame);
			return NULL;
		
}
		*p = (unsigned char)(i & 0x7f) | 0x80;
	
}
	free(filler);
	return frame;

}

extern unsigned char *FrameFiller_testMQR(int version)
{

	int width;
	unsigned char *frame, *p;
	FrameFiller *filler;
	int i, length;

	width = MQRspec_getWidth(version);
	frame = MQRspec_newFrame(version);
	if(frame == NULL) return NULL;
	filler = FrameFiller_new(width, frame, 1);
	if(filler == NULL) {

		free(frame);
		return NULL;
	
}
	length = MQRspec_getDataLengthBit(version, QR_ECLEVEL_L)
	       + MQRspec_getECCLength(version, QR_ECLEVEL_L) * 8;
	for(i=0; i<length; i++) {

		p = FrameFiller_next(filler);
		if(p == NULL) {

			fprintf(stderr, "Frame filler run over the frame!\n");
			free(filler);
			return frame;
		
}
		*p = (unsigned char)(i & 0x7f) | 0x80;
	
}
	free(filler);
	return frame;

}
#endif


/******************************************************************************
 * QR-code encoding
 *****************************************************************************/

__STATIC QRcode *QRcode_new(int version, int width, unsigned char *data)
{

	QRcode *qrcode;

	qrcode = (QRcode *)malloc(sizeof(QRcode));
	if(qrcode == NULL) return NULL;

	qrcode->version = version;
	qrcode->width = width;
	qrcode->data = data;

	return qrcode;

}

void QRcode_free(QRcode *qrcode)
{

	if(qrcode != NULL) {

		free(qrcode->data);
		free(qrcode);
	
}

}

__STATIC QRcode *QRcode_encodeMask(QRinput *input, int mask)
{

	int width, version;
	QRRawCode *raw;
	unsigned char *frame, *masked, *p, code, bit;
	FrameFiller *filler;
	int i, j;
	QRcode *qrcode = NULL;

	if(input
...
...
(Not finished, please download and read the complete file)
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 3

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
01.97 kB
01.97 kB
QRGenerator.ilk366.95 kB2013-06-11 12:21
01.97 kB
01.97 kB
BuildLog.htm15.69 kB2013-06-11 12:21
01.97 kB
bitstream.c4.21 kB2013-05-15 12:07
bitstream.h1.40 kB2013-05-15 12:07
config.h230.00 B2013-05-15 12:07
mask.c6.79 kB2013-05-15 12:07
mask.h1.52 kB2013-05-15 12:07
mmask.c4.01 kB2013-05-15 12:07
mmask.h1.37 kB2013-05-15 12:07
mqrspec.c6.92 kB2013-05-15 12:07
mqrspec.h4.66 kB2013-05-15 12:07
qrenc.c21.53 kB2013-06-05 16:59
qrencode.c19.60 kB2013-05-15 12:07
qrencode.h20.17 kB2013-05-15 12:07
qrencode_inner.h2.71 kB2013-05-15 12:07
qrinput.c38.05 kB2013-05-15 12:07
qrinput.h3.57 kB2013-05-15 12:07
qrspec.c15.35 kB2013-05-15 12:07
qrspec.h5.70 kB2013-05-15 12:07
rscode.c8.96 kB2013-05-15 12:07
rscode.h1.43 kB2013-05-15 12:07
split.c7.20 kB2013-05-15 12:07
split.h1.87 kB2013-05-15 12:07
QRGenerator.cpp6.14 kB2013-05-15 12:07
QRGenerator.vcproj5.68 kB2013-06-11 12:20
QRGenerator.vcproj.NCC1701A.TWOTM.user1.39 kB2013-06-11 12:21
QRGenerator.vcproj.PROXIMA.TWOTM.user1.38 kB2013-05-15 12:07
01.97 kB
bitstream.obj15.25 kB2013-06-11 12:21
BuildLog.htm10.58 kB2013-06-11 12:21
mask.obj19.98 kB2013-06-11 12:21
mmask.obj12.95 kB2013-06-11 12:21
mqrspec.obj16.01 kB2013-06-11 12:21
mt.dep62.00 B2013-06-11 12:21
qrencode.obj40.71 kB2013-06-11 12:21
QRGenerator.exe.intermediate.manifest616.00 B2013-06-11 12:21
QRGenerator.obj34.64 kB2013-06-11 12:21
qrinput.obj62.77 kB2013-06-11 12:21
qrspec.obj26.07 kB2013-06-11 12:21
rscode.obj14.36 kB2013-06-11 12:21
split.obj16.79 kB2013-06-11 12:21
stdafx.obj25.93 kB2013-06-11 12:21
vc90.idb83.00 kB2013-06-11 12:21
vc90.pdb76.00 kB2013-06-11 12:21
stdafx.cpp298.00 B2013-05-15 12:07
stdafx.h320.00 B2013-05-15 12:07
targetver.h765.00 B2013-05-15 12:07
QRGenerator.ncb683.00 kB2013-06-11 12:21
QRGenerator.sln899.00 B2013-05-15 12:07
QRGenerator.suo23.00 kB2013-06-11 12:21
01.97 kB
QRGenerator.exe28.00 kB2013-06-11 12:21
QRGenerator.pdb267.00 kB2013-06-11 12:21
...
Sponsored links

qrencode.c (497.86 kB)

Need 3 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D