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

来源:互联网 发布:直销软件结算系统公司 编辑:程序博客网 时间:2024/05/20 17:25

从现有的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 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 必修课没有选上怎么办 大学错过了选课怎么办 大学忘记选课了怎么办 火车网上购票儿童票怎么办 售票厅看见小偷怎么办 没赶上火车火车票怎么办 重庆到韩国签证怎么办 重庆去韩国签证怎么办 重庆办韩国签证怎么办 故宫网上预定后怎么办 坐游轮如果晕船怎么办 听听力反应慢怎么办? 毕业证照片丢了怎么办 离线网盘有违规内容怎么办 手机不能向下拉怎么办 cad运行不了插件怎么办 ai中缺少文字怎么办 电脑被格式化了怎么办 迷你世界地图下载失败怎么办 网页上广告太多怎么办 网页打开广告太多怎么办 PS界面图标小怎么办 百度地图反应慢怎么办 汽车导航不播报怎么办 wps菜单栏隐藏了怎么办 手机导航声音小怎么办 手机导航不好用怎么办 手机导航箭头不准怎么办 手机处于离线状态怎么办 穷人让人看不起怎么办 非洲人口过多怎么办啊 鼻子上长白头怎么办 高铁查到违禁品怎么办 青岛涂料备案证怎么办 淘宝集运不能寄怎么办 防静电指数过高怎么办 手机玻璃上裂痕怎么办 车辆环保检测不合格怎么办 眼镜被擦破镜片怎么办 手机边框有刮痕怎么办 手机金属壳磨损怎么办