rscode.c ( File view )

  • By green75 2014-11-06
  • View(s):334
  • Download(s):8
  • Point(s): 3
			/*
 * qrencode - QR Code encoder
 *
 * Reed solomon encoder. This code is taken from Phil Karn's libfec then
 * editted and packed into a pair of .c and .h files.
 *
 * Copyright (C) 2002, 2003, 2004, 2006 Phil Karn, KA9Q
 * (libfec is released under the GNU Lesser General Public License.)
 *
 * Copyright (C) 2006-2011 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 <stdlib.h>
#include <string.h>
#ifdef HAVE_LIBPTHREAD
#  include <pthread.h>
#endif

#include "rscode.h"

/* Stuff specific to the 8-bit symbol version of the general purpose RS codecs
 *
 */
typedef unsigned char data_t;


/**
 * Reed-Solomon codec control block
 */
struct _RS {

	int mm;              /* Bits per symbol */
	int nn;              /* Symbols per block (= (1<<mm)-1) */
	data_t *alpha_to;     /* log lookup table */
	data_t *index_of;     /* Antilog lookup table */
	data_t *genpoly;      /* Generator polynomial */
	int nroots;     /* Number of generator roots = number of parity symbols */
	int fcr;        /* First consecutive root, index form */
	int prim;       /* Primitive element, index form */
	int iprim;      /* prim-th root of 1, index form */
	int pad;        /* Padding bytes in shortened block */
	int gfpoly;
	struct _RS *next;

};

static RS *rslist = NULL;
#ifdef HAVE_LIBPTHREAD
static pthread_mutex_t rslist_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif

int modnn(RS *rs, int x){

	while (x >= rs->nn) {

		x -= rs->nn;
		x = (x >> rs->mm) + (x & rs->nn);
	
}
	return x;

}


#define MODNN(x) modnn(rs,x)

#define MM (rs->mm)
#define NN (rs->nn)
#define ALPHA_TO (rs->alpha_to) 
#define INDEX_OF (rs->index_of)
#define GENPOLY (rs->genpoly)
#define NROOTS (rs->nroots)
#define FCR (rs->fcr)
#define PRIM (rs->prim)
#define IPRIM (rs->iprim)
#define PAD (rs->pad)
#define A0 (NN)


/* Initialize a Reed-Solomon codec
 * symsize = symbol size, bits
 * gfpoly = Field generator polynomial coefficients
 * fcr = first root of RS code generator polynomial, index form
 * prim = primitive element to generate polynomial roots
 * nroots = RS code generator polynomial degree (number of roots)
 * pad = padding bytes at front of shortened block
 */
static RS *init_rs_char(int symsize, int gfpoly, int fcr, int prim, int nroots, int pad)
{

  RS *rs;


/* Common code for intializing a Reed-Solomon control block (char or int symbols)
 * Copyright 2004 Phil Karn, KA9Q
 * May be used under the terms of the GNU Lesser General Public License (LGPL)
 */
//#undef NULL
//#define NULL ((void *)0)

  int i, j, sr,root,iprim;

  rs = NULL;
  /* Check parameter ranges */
  if(symsize < 0 || symsize > (int)(8*sizeof(data_t))){

    goto done;
  
}

  if(fcr < 0 || fcr >= (1<<symsize))
    goto done;
  if(prim <= 0 || prim >= (1<<symsize))
    goto done;
  if(nroots < 0 || nroots >= (1<<symsize))
    goto done; /* Can't have more roots than symbol values! */
  if(pad < 0 || pad >= ((1<<symsize) -1 - nroots))
    goto done; /* Too much padding */

  rs = (RS *)calloc(1,sizeof(RS));
  if(rs == NULL)
    goto done;

  rs->mm = symsize;
  rs->nn = (1<<symsize)-1;
  rs->pad = pad;

  rs->alpha_to = (data_t *)malloc(sizeof(data_t)*(rs->nn+1));
  if(rs->alpha_to == NULL){

    free(rs);
    rs = NULL;
    goto done;
  
}
  rs->index_of = (data_t *)malloc(sizeof(data_t)*(rs->nn+1));
  if(rs->index_of == NULL){

    free(rs->alpha_to);
    free(rs);
    rs = NULL;
    goto done;
  
}

  /* Generate Galois field lookup tables */
  rs->index_of[0] = A0; /* log(zero) = -inf */
  rs->alpha_to[A0] = 0; /* alpha**-inf = 0 */
  sr = 1;
  for(i=0;i<rs->nn;i++){

    rs->index_of[sr] = i;
    rs->alpha_to[i] = sr;
    sr <<= 1;
    if(sr & (1<<symsize))
      sr ^= gfpoly;
    sr &= rs->nn;
  
}
  if(sr != 1){

    /* field generator polynomial is not primitive! */
    free(rs->alpha_to);
    free(rs->index_of);
    free(rs);
    rs = NULL;
    goto done;
  
}

  /* Form RS code generator polynomial from its roots */
  rs->genpoly = (data_t *)malloc(sizeof(data_t)*(nroots+1));
  if(rs->genpoly == NULL){

    free(rs->alpha_to);
    free(rs->index_of);
    free(rs);
    rs = NULL;
    goto done;
  
}
  rs->fcr = fcr;
  rs->prim = prim;
  rs->nroots = nroots;
  rs->gfpoly = gfpoly;

  /* Find prim-th root of 1, used in decoding */
  for(iprim=1;(iprim % prim) != 0;iprim += rs->nn)
    ;
  rs->iprim = iprim / prim;

  rs->genpoly[0] = 1;
  for (i = 0,root=fcr*prim; i < nroots; i++,root += prim) {

    rs->genpoly[i+1] = 1;

    /* Multiply rs->genpoly[] by  @**(root + x) */
    for (j = i; j > 0; j--){

      if (rs->genpoly[j] != 0)
	rs->genpoly[j] = rs->genpoly[j-1] ^ rs->alpha_to[modnn(rs,rs->index_of[rs->genpoly[j]] + root)];
      else
	rs->genpoly[j] = rs->genpoly[j-1];
    
}
    /* rs->genpoly[0] can never be zero */
    rs->genpoly[0] = rs->alpha_to[modnn(rs,rs->index_of[rs->genpoly[0]] + root)];
  
}
  /* convert rs->genpoly[] to index form for quicker encoding */
  for (i = 0; i <= nroots; i++)
    rs->genpoly[i] = rs->index_of[rs->genpoly[i]];
 done:;

  return rs;

}

RS *init_rs(int symsize, int gfpoly, int fcr, int prim, int nroots, int pad)
{

	RS *rs;

#ifdef HAVE_LIBPTHREAD
	pthread_mutex_lock(&rslist_mutex);
#endif
	for(rs = rslist; rs != NULL; rs = rs->next) {

		if(rs->pad != pad) continue;
		if(rs->nroots != nroots) continue;
		if(rs->mm != symsize) continue;
		if(rs->gfpoly != gfpoly) continue;
		if(rs->fcr != fcr) continue;
		if(
...
...
(Please download the complete source code to view)
			
...
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

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