c 实现jpeg中的ALI(可变长度整数转换)正反向转换

用于DC的ALI表:DIFF 就是前后两个8X8块DC的差值,ssss就是DIFF值用二进制表示的位数

亮度,与色度的DC都是这种处理的。两个相邻的亮度与亮度比差,色度与色度比差产生DIFF,

扫描开始DIFF等于0。

用于AC ALI表:表中的AC 就是Z变换后(a,b)对中的b。ssss 是b值用2进制表示的位数

亮度与色度的AC都是这样处理的

对比,两者就是少了0的处理。因为AC中的0已经被(a,b)对中的a处理了。

1.把DC转换为 二进制位数加二进制的中间格式

复制代码
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>  
#include <string.h>
#include <sys/mman.h>
#include <linux/fb.h>
#include <math.h>

int main(void) {
	int i=-5;
	int o=-1;      //如果输出负数无意义
	char len=-1;   //如果输出负数无意义
	
	if(i==0){
		len=0;
	}
	if(i==-1){
		len=1;
		o=0;
	}
	if(i==1){
		len=1;
		o=1;
	}

	if((i>=2)&&(i<=32767)){             //二进制位数0-16位
		for(int a=0;a<16;a++){
			if((i>=pow(2,a))&&(i<pow(2,(a+1)))){
				len=a+1;
				o=i;
			}
		}
	}
	if((i>=-32767)&&(i<=-2)){
		for(int a=0;a<16;a++){
			if((i<=-pow(2,a))&&(i>-pow(2,(a+1)))){
				len=a+1;
				o=i+pow(2,(a+1))-1;
			}
		}
	}
	
	printf("len:%d  o:%d\n",len,o);

	return 0;
}
  1. ALI逆向转换

    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/ioctl.h>
    #include <linux/videodev2.h>
    #include <string.h>
    #include <sys/mman.h>
    #include <linux/fb.h>
    #include <math.h>

    int main(void) {
    int len = 4;
    int i = 7;
    int o;

    复制代码
     if (len == 0) {
     	o = 0;
     }
     if ((len == 1) && (i == 0)) {
     	o = -1;
     }
     if ((len == 1) && (i == 1)) {
     	o = 1;
     }
     //--------------------------
     if ((i >= pow(2, len - 1)) && (i <= pow(2, len))) {
     	o = i;
     }
     if ((i >= 0) && (i < pow(2, len - 1))) {
     	o = i - pow(2, len) + 1;
     }
    
    
     printf("o:%d ", o);
     return 0;

    }

相关推荐
BothSavage3 小时前
Trae远程开发中DeepSeek自定义模型4054错误的排查与修复
算法
小林ixn3 小时前
从暴力到KMP:一道题彻底搞懂字符串匹配的前世今生
算法
人活一口气3 小时前
从JVM调优到MCP协议:Java全栈技术体系深度总结与企业级架构实践
java·spring boot
烬羽4 小时前
字符串算法入门:从反转字符串到回文判断,面试不再慌
算法·面试
NE_STOP4 小时前
Vibe Coding -- 完整项目案例实操
java
荣码5 小时前
GraphRAG:普通RAG只能回答"点"的问题,我踩了4个坑才搞懂
java·python
SimonKing5 小时前
Google第三方授权登录
java·后端·程序员
明月光8185 小时前
从一行 @Builder 说起:重新拾起 Java 的 Lombok、注解与 Builder 模式
java
考虑考虑14 小时前
Mybatis实现批量插入
java·后端·mybatis
咖啡八杯15 小时前
GoF设计模式——中介者模式
java·后端·spring·设计模式