Tiny Jpeg Decoder (JPEG解码程序) 源代码分析 1:解码文件头
Tiny Jpeg Decoder是一个可以用于嵌入式系统的JPEG解码器。也可以在Windows上编译通过。在此分析一下它部分的源代码,辅助学习JPEG解码知识。
通过TinyJpeg可以将JPEG(*.jpg)文件解码为YUV(*.yuv)或者RGB(*.tga)文件。
真正的解码开始于convert_one_image()函数:
//解析Define quantization tablestatic int parse_DQT(struct jdec_private *priv, const unsigned char *stream){ int qi; float *table; const unsigned char *dqt_block_end;//------------------------------------------------ int j,k;char *temp; FILE *fp;//------------------------------------------------#if TRACE_PARAM fprintf(param_trace,"> DQT marker\n"); fflush(param_trace);#endif //该Segment末尾 dqt_block_end = stream + be16_to_cpu(stream); //跳过标签length(2字节) stream += 2;/* Skip length */ //没到末尾 while (stream < dqt_block_end) {//跳过该Segment的第1个字节,QT信息//precision: 00 (Higher 4 bit)//index: 00 (Lower 4 bit) //qi取1,第1张量化表(例如,亮度表),取2,第2张量化表(例如,色度表) qi = *stream++;#if SANITY_CHECK if (qi>>4) snprintf(error_string, sizeof(error_string),"16 bits quantization table is not supported\n"); if (qi>4) snprintf(error_string, sizeof(error_string),"No more 4 quantization table is supported (got %d)\n", qi);#endif //table指向jdec_private的Q_tables数组,为了在其中写入数据 table = priv->Q_tables[qi]; //注意:一次搞定整张!写入 //需要对数值进行变换!cos(k*PI/16) * sqrt(2) //这样才能得到离散余弦变换的系数 build_quantization_table(table, stream);//----------------------temp=(char *)stream;//fp = fopen("DQT.txt", "a+");//fwrite(temp, 64, 1, fp);char temp_str1[MAX_URL_LENGTH]={0};char temp_str2[MAX_URL_LENGTH]={0};for(j=0;j<64;j++){sprintf(temp_str2,"%d ",temp[j]);strcat(temp_str1,temp_str2);//fprintf(fp,"%d ",temp[j]);}//计数char temp_str3[MAX_URL_LENGTH]={0};sprintf(temp_str3,"量化表【%d】",priv->DQT_table_num);priv->dlg->AppendBInfo("DQT",temp_str3,temp_str1,"JPEG格式文件的量化表,一般来说第一张是亮度的,后面是色度的");priv->DQT_table_num++;//fprintf(fp,"\n-----------------------\n");//fclose(fp);#if TRACE_PARAMfor(j=0;j<8;j++){ for(k=0;k<8;k++){fprintf(param_trace,"%d ",temp[j*8+k]);}fprintf(param_trace,"\n");}fprintf(fp,"\n-----------------------\n"); fflush(param_trace);#endif//---------------------- //完事了! stream += 64; }#if TRACE_PARAM fprintf(param_trace,"< DQT marker\n"); fflush(param_trace);#endif return 0;}待续未完。。。
主页:http://www.saillard.org/programs_and_patches/tinyjpegdecoder/
源代码下载:http://download.csdn.net/detail/leixiaohua1020/6383115