mqrspec.c ( File view )

  • By green75 2014-11-06
  • View(s):334
  • Download(s):8
  • Point(s): 3
			/*
 * qrencode - QR Code encoder
 *
 * Micor QR Code specification in convenient format. 
 * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
 *
 * The following data / specifications are taken from
 * "Two dimensional symbol -- QR-code -- Basic Specification" (JIS X0510:2004)
 *  or
 * "Automatic identification and data capture techniques -- 
 *  QR Code 2005 bar code symbology specification" (ISO/IEC 18004:2006)
 *
 * 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>
#ifdef HAVE_LIBPTHREAD
#include <pthread.h>
#endif

#include "mqrspec.h"

/******************************************************************************
 * Version and capacity
 *****************************************************************************/

typedef struct {

	int width; //< Edge length of the symbol
	int ec[4];  //< Number of ECC code (bytes)

} MQRspec_Capacity;

/**
 * Table of the capacity of symbols
 * 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, 0
}
},
	{
 11, {
2,  0,  0, 0
}
},
	{
 13, {
5,  6,  0, 0
}
},
	{
 15, {
6,  8,  0, 0
}
},
	{
 17, {
8, 10, 14, 0
}
}

};

int MQRspec_getDataLengthBit(int version, QRecLevel level)
{

	int w;
	int ecc;

	w = mqrspecCapacity[version].width - 1;
	ecc = mqrspecCapacity[version].ec[level];
	if(ecc == 0) return 0;
	return w * w - 64 - ecc * 8;

}

int MQRspec_getDataLength(int version, QRecLevel level)
{

	return (MQRspec_getDataLengthBit(version, level) + 4) / 8;

}

int MQRspec_getECCLength(int version, QRecLevel level)
{

	return mqrspecCapacity[version].ec[level];

}

int MQRspec_getWidth(int version)
{

	return mqrspecCapacity[version].width;

}

/******************************************************************************
 * Length indicator
 *****************************************************************************/

/**
 * See Table 3 (pp.107) of Appendix 1, JIS X0510:2004.
 */
static const int lengthTableBits[4][4] = {

	{
 3, 4, 5, 6
},
	{
 0, 3, 4, 5
},
	{
 0, 0, 4, 5
},
	{
 0, 0, 3, 4
}

};

int MQRspec_lengthIndicator(QRencodeMode mode, int version)
{

	return lengthTableBits[mode][version - 1];

}

int MQRspec_maximumWords(QRencodeMode mode, int version)
{

	int bits;
	int words;

	bits = lengthTableBits[mode][version - 1];
	words = (1 << bits) - 1;
	if(mode == QR_MODE_KANJI) {

		words *= 2; // the number of bytes is required
	
}

	return words;

}

/******************************************************************************
 * Format information
 *****************************************************************************/

/* See calcFormatInfo in tests/test_mqrspec.c */
static const unsigned int formatInfo[4][8] = {

	{
0x4445, 0x55ae, 0x6793, 0x7678, 0x06de, 0x1735, 0x2508, 0x34e3
},
	{
0x4172, 0x5099, 0x62a4, 0x734f, 0x03e9, 0x1202, 0x203f, 0x31d4
},
	{
0x4e2b, 0x5fc0, 0x6dfd, 0x7c16, 0x0cb0, 0x1d5b, 0x2f66, 0x3e8d
},
	{
0x4b1c, 0x5af7, 0x68ca, 0x7921, 0x0987, 0x186c, 0x2a51, 0x3bba
}

};

/* See Table 10 of Appendix 1. (pp.115) */
static const int typeTable[MQRSPEC_VERSION_MAX + 1][3] = {

	{
-1, -1, -1
},
	{
 0, -1, -1
},
	{
 1,  2, -1
},
	{
 3,  4, -1
},
	{
 5,  6,  7
}

};

unsigned int MQRspec_getFormatInfo(int mask, int version, QRecLevel level)
{

	int type;

	if(mask < 0 || mask > 3) return 0;
	if(version <= 0 || version > MQRSPEC_VERSION_MAX) return 0;
	if(level == QR_ECLEVEL_H) return 0;
	type = typeTable[version][level];
	if(type < 0) return 0;

	return formatInfo[mask][type];

}

/******************************************************************************
 * Frame
 *****************************************************************************/

/**
 * Cache of initial frames.
 */
/* 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.
 * @param frame
 * @param width
 * @param ox,oy upper-left coordinate of the pattern
 */
static void putFinderPattern(unsigned char *frame, int width, int ox, int oy)
{

	static const unsigned char finder[] = {

		0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1,
		0xc1, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc1,
		0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,
		0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,
		0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,
		0xc1, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc1,
		0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1,
	
};
	int x, y;
	const unsigned char *s;

	frame += oy * width + ox;
	s = finder;
	for(y=0; y<7; y++) {

		for(x=0; x<7; x++) {

			frame[x] = s[x];
		
}
		frame += width;
		s += 7;
	
}

}

static unsigned char *MQRspec_createFrame(int version)
{

	unsigned char *frame, *p, *q;
	int width;
	int x, y;

	width = mqrspecCapacity[version].width;
	frame = (unsigned char *)malloc(width * width);
	if(frame == NULL) return NULL;

	memset(frame, 0, width * width);
	/* Finder pattern */
	putFinderPattern(frame, width, 0, 0);
	/* Separator */
	p = frame;
	for(y=0; y<7; y++) {

		p[7] = 0xc0;
		p += width;
	
}
	memset(frame + width * 7, 0xc0, 8);
	/* Mask format information area */
	memset(frame + width * 8 + 1, 0x84, 8);
	p = frame + width + 8;
	for(y=0; y<7; y++) {

		*p = 0x84;
		p += width;
	
}
	/* Timing pattern */
	p = frame + 8;
	q = frame + width * 8;
	for(x=1; x<width-7; x++) {

		*p =  0x90 | (x & 1);
		*q =  0x90 | (x & 1);
		p++;
		q += width;
	
}

	return frame;

}

unsigned char *MQRspec_newFrame(int version)
{

	unsigned char *frame;
	int width;

	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;
	frame = (unsigned char *)malloc(width * width);
	if(frame == NULL) return NULL;
	memcpy(frame, frames[version], width * width);

	return frame;

}

void MQRspec_clearCache(void)
{

	int i;

#ifdef HAVE_LIBPTHREAD
	pthread_mutex_lock(&frames_mutex);
#endif
	for(i=1; i<=MQRSPEC_VERSION_MAX; i++) {

		free(frames[i]);
		frames[i] = NULL;
	
}
#ifdef HAVE_LIBPTHREAD
	pthread_mutex_unlock(&frames_mutex);
#endif

}
			
...
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

mqrspec.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