音视频处理-图像格式-jpeg-tinyjpeg

从现有的jpeg库推倒jpeg算法
文章关系:jpeg字段的意义
tinyjpeg是一个微型的jpeg解码库,源代码可以通过雷博的csdn分享资源下载到
感谢雷博的无私分享
tinyjpeg的目录结构比较简单:loadjpeg.c : 示范应用
tinyjpeg.c : 主要的API接口
主要关注这两个文件就可以了

看一下tinyjpeg的Makefile

这里写图片描述
很明显 一个示例的源码目录 并没有把tinyjpeg编译成动态链接库 所以在使用的时候直接把tinyjpeg.c以及包含头文件加入就好。假如碰上多个工程都在使用tinyjpeg 再把它编译成so使用

make测试tinyjpeg 出错
tinyjpeg.c:1:0: error: CPU you selected does not support x86-64 instruction set
tinyjpeg.c:1:0: error: CPU you selected does not support x86-64 instruction set
makefile在我的64bit ubuntu下不能通过,因为march指定的平台不支持 修改 -march=native 即可make通过

static void exitmessage(const char *message) attribute((noreturn));
用到了GCC的attribute属性设置函数的属性 以为函数不反回值,我理解为允许编译器不去检查函数的返回值 例如
int p_main_test(int cnt)
{
if(cnt > 0)
{
return cnt;//可能不会执行到
}
}`
编译会产生警告 设置了noreturn属性后,可以解决这个警告。
注:使用attribute属性需要GCC附带参数 -Wall 见Makefile line17
CFLAGS += -g -Wall -fno-inline-functions-called-once -Wextra

打印字符函数 打印后退出进程
static void exitmessage(const char *message)
{
printf(“%s\n”, message);
exit(0);
}

确定文件大小 先fseek到文件尾 pos获取文件头到文件指针的byte数 再重新把文件指针位置复原到文件头
static int filesize(FILE *fp)
{
long pos;
fseek(fp, 0, SEEK_END);
pos = ftell(fp);
fseek(fp, 0, SEEK_SET);
return pos;
}

static void write_tga(const char *filename, int output_format, int width, int height, unsigned char **components)
{
unsigned char targaheader[18];
FILE *F;
char temp[1024];
unsigned int bufferlen = width * height * 3;
unsigned char *rgb_data = components[0];

snprintf(temp, sizeof(temp), filename);

memset(targaheader,0,sizeof(targaheader));

targaheader[12] = (unsigned char) (width & 0xFF);//宽度的
targaheader[13] = (unsigned char) (width >> 8);
targaheader[14] = (unsigned char) (height & 0xFF);
targaheader[15] = (unsigned char) (height >> 8);
targaheader[17] = 0x20; /* Top-down, non-interlaced */
targaheader[2] = 2; /未压缩的真彩图像 /
targaheader[16] = 24;

if (output_format == TINYJPEG_FMT_RGB24)
{
unsigned char *data = rgb_data + bufferlen - 3;
do
{
unsigned char c = data[0];
data[0] = data[2];
data[2] = c;
data-=3;
}
while (data > rgb_data);
}

F = fopen(temp, “wb”);
fwrite(targaheader, sizeof(targaheader), 1, F);
fwrite(rgb_data, 1, bufferlen, F);
fclose(F);
}
数据转储为tga文件 首先写tga文件头 然后写rgb数据
tga格式:tga格式数据详细点这里
TGA原始文件结构(v1.0)由以下两个部分组成:文件头和图像/颜色表数据 文件头18byte
tag字段解析

//困了 先到这 明早起来继续
雷博的tinyjpeg解析,和我的帖子参照着看
雷博解析了tinyjpeg自身的API,我的帖子主要是补充:1、jpeg字段解析 2、tinyjpeg自带DEMO中关于图像格式如tag的解析
这里贴一个tinyjpeg的API写的解码器

struct jdec_private *p_decode_jpg(uint8_t *buf, int buf_size, uint8_t **components, int ctl)
{
    static struct jdec_private *dec_jpg;
    static int f_init = 1;
    int width, height;
    if(f_init)
    {
        dec_jpg = tinyjpeg_init();
        if(dec_jpg == NULL)
        {
            printf("failed to init tinyjpeg\n");
            return -1;
        }
        f_init = 0;
    }

    if(ctl)
    {
        tinyjpeg_free(dec_jpg);
        return 0;
    }

    if(tinyjpeg_parse_header(dec_jpg, buf, buf_size) < 0)//解析头部,获取格式信息写入dec_jpg
    {
        printf("failed to decode jpeg header\n");
        return -1;
    }

    tinyjpeg_decode(dec_jpg ,TINYJPEG_FMT_YUV420P);//解码
    tinyjpeg_get_components(dec_jpg, components);//获取YUV(Y U V分量)数据的地址
    //tinyjpeg_get_size(dec_jpg, &width, &height);

    return dec_jpg;
}

**后记/总结:1、tinyjpeg很小,可以直接添加入代码
2、tinyjpeg使用了一些GCC特性,如inline。编译时需要gcc添加对应参数才能编译通过,具体参考tinyjpeg自带的makefile**

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值