2021SC@SDUSC
decoder.h分析
对三个类似颜色的元素进行排序并返回顺序:
static inline unsigned decode_sort3 (zbar_decoder_t *dcode,
int i0)
{
unsigned w0 = get_width(dcode, i0);
unsigned w2 = get_width(dcode, i0 + 2);
unsigned w4 = get_width(dcode, i0 + 4);
if(w0 < w2) {
if(w2 < w4)
return((i0 << 8) | ((i0 + 2) << 4) | (i0 + 4));
if(w0 < w4)
return((i0 << 8) | ((i0 + 4) << 4) | (i0 + 2));
return(((i0 + 4) << 8) | (i0 << 4) | (i0 + 2));
}
if(w4 < w2)
return(((i0 + 4) << 8) | ((i0 + 2) << 4) | i0);
if(w0 < w4)
return(((i0 + 2) << 8) | (i0 << 4) | (i0 + 4));
return(((i0 + 2) << 8) | ((i0 + 4) << 4) | i0);
}
对N个类似颜色的元素进行排序并返回顺序
static inline unsigned decode_sortn (zbar_decoder_t *dcode,
int n,
int i0)
{
unsigned mask = 0, sort = 0;
int i;
for(i = n - 1; i >= 0; i--) {
unsigned wmin = UINT_MAX;
int jmin = -1, j;
for(j = n - 1; j >= 0; j--) {
if((mask >> j) & 1)
continue;
unsigned w = get_width(dcode, i0 + j * 2);
if(wmin >= w) {
wmin = w;
jmin = j;
}
}
zassert(jmin >= 0, 0, "sortn(%d,%d) jmin=%d",
n, i0, jmin);
sort <<= 4;
mask |= 1 << jmin;
sort |= i0 + jmin * 2;
}
return(sort);
}
获取共享状态锁
static inline char acquire_lock (zbar_decoder_t *dcode,
zbar_symbol_type_t req)
{
if(dcode->lock) {
dbprintf(2, " [locked %d]\n", dcode->lock);
return(1);
}
dcode->lock = req;
return(0);
}
检查并释放共享状态锁
static inline char release_lock (zbar_decoder_t *dcode,
zbar_symbol_type_t req)
{
zassert(dcode->lock == req, 1, "lock=%d req=%d\n",
dcode->lock, req);
dcode->lock = 0;
return(0);
}
确保输出缓冲区有足够的请求分配
static inline char size_buf (zbar_decoder_t *dcode,
unsigned len)
{
unsigned char *buf;
if(len <= BUFFER_MIN)
return(0);
if(len < dcode->buf_alloc)
return(0);
if(len > BUFFER_MAX)
return(1);
if(len < dcode->buf_alloc + BUFFER_INCR) {
len = dcode->buf_alloc + BUFFER_INCR;
if(len > BUFFER_MAX)
len = BUFFER_MAX;
}
buf = realloc(dcode->buf, len);
if(!buf)
return(1);
dcode->buf = buf;
dcode->buf_alloc = len;
return(0);
}