ZBar源码分析(五)

2021SC@SDUSC

目录

一、image头文件分析

二、image源文件分析

1.zbar_image_create函数分析

2._zbar_image_free函数分析

3.一系列get、set函数


一、image头文件分析

typedef enum zbar_format_group_e {
    ZBAR_FMT_GRAY,
    ZBAR_FMT_YUV_PLANAR,
    ZBAR_FMT_YUV_PACKED,
    ZBAR_FMT_RGB_PACKED,
    ZBAR_FMT_YUV_NV,
    ZBAR_FMT_JPEG,

    /* enum size */
    ZBAR_FMT_NUM
} zbar_format_group_t;

粗图像格式分组,目的是限制转换变化。

struct zbar_image_s {
    uint32_t format;            /* fourcc image format code */
    unsigned width, height;     /* image size */
    const void *data;           /* image sample data */
    unsigned long datalen;      /* allocated/mapped size of data */
    unsigned crop_x, crop_y;    /* crop rectangle */
    unsigned crop_w, crop_h;
    void *userdata;             /* user specified data associated w/image */

    /* cleanup handler */
    zbar_image_cleanup_handler_t *cleanup;
    refcnt_t refcnt;            /* reference count */
    zbar_video_t *src;          /* originator */
    int srcidx;                 /* index used by originator */
    zbar_image_t *next;         /* internal image lists */

    unsigned seq;               /* page/frame sequence number */
    zbar_symbol_set_t *syms;    /* decoded result set */
};

zbar_image_s,具体属性已经在ZBar源码分析(四)中分析过。

typedef struct zbar_format_def_s {
    uint32_t format;                    /* fourcc */
    zbar_format_group_t group;          /* 粗分类 */
    union {
        uint8_t gen[4];                 /* 原始字节 */
        struct {
            uint8_t bpp;                /* 每像素位数 */
            uint8_t red, green, blue;   /* RGB位的大小/位置() */
        } rgb;
        struct {
            uint8_t xsub2, ysub2;       /* 每个轴上的色度二次采样 */
            uint8_t packorder;          /* 通道顺序标志
                                         *   bit0: 0=UV, 1=VU
                                         *   bit1: 0=Y/chroma, 1=chroma/Y
                                         */
        } yuv;
        uint32_t cmp;                   /* 快速比较等效格式 */
    } p;
} zbar_format_def_t;

图像格式的描述,具体属性分析见注释。

二、image源文件分析

1.zbar_image_create函数分析

zbar_image_t *zbar_image_create ()
{
    zbar_image_t *img = calloc(1, sizeof(zbar_image_t));
    _zbar_refcnt_init();
    _zbar_image_refcnt(img, 1);
    img->srcidx = -1;
    return(img);
}

创建一个zbar_image_t对象,函数调用了_zbar_refcnt_init和_zbar_image_refcnt两个函数。

_zbar_refcnt_init在refcnt.h文件中定义,用于初始化refcnt,_zbar_image_refcnt在image.h中定义,用于清理和释放图像。

2._zbar_image_free函数分析

void _zbar_image_free (zbar_image_t *img)
{
    if(img->syms) {
        zbar_symbol_set_ref(img->syms, -1);
        img->syms = NULL;
    }
    free(img);
}

检测是否存在解码结果集,如果存在则调用zbar_symbol_set_ref函数将引用计数减一,最后对图像进行释放。

3.一系列get、set函数

unsigned long zbar_image_get_format (const zbar_image_t *img)
{
    return(img->format);
}

unsigned zbar_image_get_sequence (const zbar_image_t *img)
{
    return(img->seq);
}

unsigned zbar_image_get_width (const zbar_image_t *img)
{
    return(img->width);
}

unsigned zbar_image_get_height (const zbar_image_t *img)
{
    return(img->height);
}

void zbar_image_get_size (const zbar_image_t *img,
                          unsigned *w,
                          unsigned *h)
{
    if(w) *w = img->width;
    if(h) *h = img->height;
}

void zbar_image_get_crop (const zbar_image_t *img,
                          unsigned *x,
                          unsigned *y,
                          unsigned *w,
                          unsigned *h)
{
    if(x) *x = img->crop_x;
    if(y) *y = img->crop_y;
    if(w) *w = img->crop_w;
    if(h) *h = img->crop_h;
}

const void *zbar_image_get_data (const zbar_image_t *img)
{
    return(img->data);
}

unsigned long zbar_image_get_data_length (const zbar_image_t *img)
{
    return(img->datalen);
}

void zbar_image_set_format (zbar_image_t *img,
                            unsigned long fmt)
{
    img->format = fmt;
}

void zbar_image_set_sequence (zbar_image_t *img,
                              unsigned seq)
{
    img->seq = seq;
}

void zbar_image_set_size (zbar_image_t *img,
                          unsigned w,
                          unsigned h)
{
    img->crop_x = img->crop_y = 0;
    img->width = img->crop_w = w;
    img->height = img->crop_h = h;
}

void zbar_image_set_data (zbar_image_t *img,
                          const void *data,
                          unsigned long len,
                          zbar_image_cleanup_handler_t *cleanup)
{
    zbar_image_free_data(img);
    img->data = data;
    img->datalen = len;
    img->cleanup = cleanup;
}

void zbar_image_set_userdata (zbar_image_t *img,
                              void *userdata)
{
    img->userdata = userdata;
}

void *zbar_image_get_userdata (const zbar_image_t *img)
{
    return(img->userdata);
}

普通的get、set函数,不再赘述。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值