// JPEG decoder module
// Copyright 1999 Cristi Cuturicu
#include "jpegdec.h"
// Used markers:
#define SOI 0xD8
#define EOI 0xD9
#define APP0 0xE0
#define SOF 0xC0
#define DQT 0xDB
#define DHT 0xC4
#define SOS 0xDA
#define DRI 0xDD
#define COM 0xFE
char error_string[90];
#define exit_func(err) { strcpy(error_string, err); return 0;}
static BYTE *buf; // the buffer we use for storing the entire JPG file
static BYTE bp; //current byte
static WORD wp; //current word
static DWORD byte_pos; // current byte position
#define BYTE_p(i) bp=buf[(i)++]
#define WORD_p(i) wp=(((WORD)(buf[(i)]))<<8) + buf[(i)+1]; (i)+=2
// WORD X_image_size,Y_image_size; // X,Y sizes of the image
static WORD X_round,Y_round; // The dimensions rounded to multiple of Hmax*8 (X_round)
// and Ymax*8 (Y_round)
static BYTE *im_buffer; // RGBA image buffer
static DWORD X_image_bytes; // size in bytes of 1 line of the image = X_round * 4
static DWORD y_inc_value ; // 32*X_round; // used by decode_MCU_1x2,2x1,2x2
BYTE YH,YV,CbH,CbV,CrH,CrV; // sampling factors (horizontal and vertical) for Y,Cb,Cr
static WORD Hmax,Vmax;
static BYTE zigzag[64]={ 0, 1, 5, 6,14,15,27,28,
2, 4, 7,13,16,26,29,42,
3, 8,12,17,25,30,41,43,
9,11,18,24,31,40,44,53,
10,19,23,32,39,45,52,54,
20,22,33,38,46,51,55,60,
21,34,37,47,50,56,59,61,
35,36,48,49,57,58,62,63 };
typedef struct {
BYTE Length[17]; // k =1-16 ; L[k] indicates the number of Huffman codes of length k
WORD minor_code[17]; // indicates the value of the smallest Huffman code of length k
WORD major_code[17]; // similar, but the highest code
BYTE V[65536]; // V[k][j] = Value associated to the j-th Huffman code of length k
// High nibble = nr of previous 0 coefficients
// Low nibble = size (in bits) of the coefficient which will be taken from the data stream
} Huffman_table;
static float *QT[4]; // quantization tables, no more than 4 quantization tables (QT[0..3])
static Huffman_table HTDC[4]; //DC huffman tables , no more than 4 (0..3)
static Huffman_table HTAC[4]; //AC huffman tables (0..3)
static BYTE YQ_nr,CbQ_nr,CrQ_nr; // quantization table number for Y, Cb, Cr
static BYTE YDC_nr,CbDC_nr,CrDC_nr; // DC Huffman table number for Y,Cb, Cr
static BYTE YAC_nr,CbAC_nr,CrAC_nr; // AC Huffman table number for Y,Cb, Cr
static BYTE Restart_markers; // if 1 => Restart markers on , 0 => no restart markers
static WORD MCU_restart; //Restart markers appears every MCU_restart MCU blocks
typedef void (*decode_MCU_func)(DWORD);
static SWORD DCY, DCCb, DCCr; // Coeficientii DC pentru Y,Cb,Cr
static SWORD DCT_coeff[64]; // Current DCT_coefficients
static BYTE Y[64],Cb[64],Cr[64]; // Y, Cb, Cr of the current 8x8 block for the 1x1 case
static BYTE Y_1[64],Y_2[64],Y_3[64],Y_4[64];
static BYTE tab_1[64],tab_2[64],tab_3[64],tab_4[64]; // tabelele de supraesantionare pt cele 4 blocuri
static SWORD Cr_tab[256],Cb_tab[256]; // Precalculated Cr, Cb tables
static SWORD Cr_Cb_green_tab[65536];
// Initial conditions:
// byte_pos = start position in the Huffman coded segment
// WORD_get(w1); WORD_get(w2);wordval=w1;
static BYTE d_k=0; // Bit displacement in memory, relative to the offset of w1
// it's always <16
static WORD w1,w2; // w1 = First word in memory; w2 = Second word
static DWORD wordval ; // the actual used value in Huffman decoding.
static DWORD mask[17];
static SWORD neg_pow2[17]={0,-1,-3,-7,-15,-31,-63,-127,-255,-511,-1023,-2047,-4095,-8191,-16383,-32767};
static DWORD start_neg_pow2=(DWORD)neg_pow2;
static int shift_temp;
#define RIGHT_SHIFT(x,shft) \
((shift_temp = (x)) < 0 ? \
(shift_temp >> (shft)) | ((~(0L)) << (32-(shft))) : \
(shift_temp >> (shft)))
#define DESCALE(x,n) RIGHT_SHIFT((x) + (1L << ((n)-1)), n)
#define RANGE_MASK 1023L
static BYTE *rlimit_table;
void prepare_range_limit_table()
/* Allocate and fill in the sample_range_limit table */
{
int j;
rlimit_table = (BYTE *)malloc(5 * 256L + 128) ;
/* First segment of "simple" table: limit[x] = 0 for x < 0 */
memset((void *)rlimit_table,0,256);
rlimit_table += 256; /* allow negative subscripts of simple table */
/* Main part of "simple" table: limit[x] = x */
for (j = 0; j < 256; j++) rlimit_table[j] = j;
/* End of simple table, rest of first half of post-IDCT table */
for (j = 256; j < 640; j++) rlimit_table[j] = 255;
/* Second half of post-IDCT table */
memset((void *)(rlimit_table + 640),0,384);
for (j = 0; j < 128 ; j++) rlimit_table[j+1024] = j;
}
#ifdef _MSC_VER
WORD lookKbits(BYTE k)
{
_asm {
mov dl, k
mov cl, 16
sub cl, dl
mov eax, [wordval]
shr eax, cl
}
}
WORD WORD_hi_lo(BYTE byte_high,BYTE byte_low)
{
_asm {
mov ah,byte_high
mov al,byte_low
}
}
SWORD get_svalue(BYTE k)
// k>0 always
// Takes k bits out of the BIT stream (wordval), and makes them a signed value
{
_asm {
xor ecx, ecx
mov cl,k
mov eax,[wordval]
shl eax,cl
shr eax, 16
dec cl
bt eax,ecx
jc end_macro
inc cl
mov ebx,[start_neg_pow2]
add ax,word ptr [ebx+ecx*2]
end_macro:
}
}
#endif
#ifdef __WATCOMC__
WORD lookKbits(BYTE k);
#pragma aux lookKbits=\
"mov eax,[wordval]"\
"mov cl, 16"\
"sub cl, dl"\
"shr eax, cl"\
parm [dl] \
value [ax] \
modify [eax cl];
WORD WORD_hi_lo(BYTE byte_high,BYTE BYTE_low);
#pragma aux WORD_hi_lo=\
parm [ah] [al]\
value [ax] \
modify [ax];
SWORD get_svalue(BYTE k);
// k>0 always
// Takes k bits out of the BIT stream (wordval), and makes them a signed value
#pragma aux get_svalue=\
"xor ecx, ecx"\
"mov cl, al"\
"mov eax,[wordval]"\
"shl eax, cl"\
"shr eax, 16"\
"dec cl"\
"bt eax,ecx"\
"jc end_macro"\
"inc cl"\
"mov ebx,[start_neg_pow2]"\
"add ax,word ptr [ebx+ecx*2]"\
"end_macro:"\
parm [al]\
modify [eax ebx ecx]\
value [ax];
#endif
void skipKbits(BYTE k)
{
BYTE b_high,b_low;
d_k+=k;
if (d_k>=16) { d_k-=16;
w1=w2;
// Get the next word in w2
BYTE_p(byte_pos);
if (bp!=0xFF) b_high=bp;
else {
if (buf[byte_pos]==0) byte_pos++; //skip 00
else byte_pos--; // stop byte_pos pe restart marker
b_high=0xFF;
}
BYTE_p(byte_pos);
if (bp!=0xFF) b_low=bp;
else {
if (buf[byte_pos]==0) byte_pos++; //skip 00
else byte_pos--; // stop byte_pos pe restart marker
b_low=0xFF;
}
w2=WORD_hi_lo(b_high,b_low);
}
wordval = ((DWORD)(w1)<<16) + w2;
wordval <<= d_k;
wordval >>= 16;
}
SWORD getKbits(BYTE k)
{
SWORD signed_wordvalue;
signed_wordvalue=get_svalue(k);
skipKbits(k);
return signed_wordvalue;
}
void calculate_mask()
{
BYTE k;
DWORD tmpdv;
for (k=0;k<=16;k++) { tmpdv=0x10000;mask[k]=(tmpdv>>k)-1;} //precalculated bit mask
}
void init_QT()
{
BYTE i;
for (i=0;i<=3;i++) QT[i]=(float *)malloc(sizeof(float)*64);
}
void load_quant_table(float *quant_table)
{
float scalefactor[8]={1.0f, 1.387039845f, 1.306562965f, 1.175875602f,
1.0f, 0.785694958f, 0.541196100f, 0.275899379f};
BYTE j,row,col;
// Load quantization coefficients from JPG file, scale them for DCT and reorder
// from zig-zag order
for (j=0;j<=63;j++) quant_table[j]=buf[byte_pos+zigzag[j]];
j=0;
for (row=0;row<=7;row++)
for (col=0;col<=7;col++) {
quant_table[j]*=scalefactor[row]*scalefactor[col];
j++;
}
byte_pos+=64;
}
void load_Huffman_table(Huffman_table *HT)
{
BYTE k,j;
DWORD code;
for (j=1;j<=16;j++) {
BYTE_p(byte_pos);
HT->Length[j]=bp;
}
for (k=1;k<=16;k++)
for (j=0;j<HT->Length[k];j++) {
BYTE_p(byte_pos);
HT->V[WORD_hi_lo(k,j)]=bp;
}
code=0;
for (k=1;k<=16;k++) {
HT->minor_code[k] = (WORD)code;
for (j=1;j<=HT->Length[k];j++) c
没有合适的资源?快使用搜索试试~ 我知道了~
jpg文件转化为bmp C++
共52个文件
tlog:14个
jpg:4个
bmp:3个
4星 · 超过85%的资源 需积分: 10 20 下载量 72 浏览量
2012-09-15
20:41:48
上传
评论 1
收藏 7.66MB ZIP 举报
温馨提示
程序实现了jpg文件到bmp文件的转化。只需在命令行参数中输入文件名,并将需转化的文件与.cpp文件放在同一目录下即可。
资源推荐
资源详情
资源评论
收起资源包目录
jpg文件转化为bmp.zip (52个子文件)
jpg文件转化为bmp
decJPG.vcxproj.filters 1KB
decJPG.opt 48KB
Debug
cl.command.1.tlog 1KB
vc60.pdb 52KB
decJPG.exe.embed.manifest 406B
rc.command.1.tlog 554B
CL.read.1.tlog 3KB
decJPG_manifest.rc 208B
vc100.idb 51KB
mt.read.1.tlog 338B
link-cvtres.read.1.tlog 2B
link.read.1.tlog 3KB
vc60.idb 33KB
rc.read.1.tlog 310B
link.write.1.tlog 996B
CL.write.1.tlog 1KB
001.jpg 393KB
JPEGDEC.obj 50KB
decJPG.exe.embed.manifest.res 472B
decJPG.exe.intermediate.manifest 381B
vc100.pdb 60KB
decJPG.lastbuildstate 74B
test-hua.jpg 157KB
link.command.1.tlog 2KB
decJPG.log 881B
test-hua.bmp 583KB
Thumbs.db 116KB
mt.write.1.tlog 338B
decJPG.pdb 2.18MB
decJPG.ilk 1.02MB
mt.command.1.tlog 398B
JPEGAPP.obj 12KB
decJPG.exe 487KB
rc.write.1.tlog 318B
link-cvtres.write.1.tlog 2B
decJPG.dsp 4KB
decJPG.ncb 57KB
ipch
decjpg-a5c51a76
decjpg-256a16e3.ipch 2.25MB
decJPG.suo 16KB
decJPG.dsw 535B
001.jpg 393KB
JPEGDEC.H 475B
test-hua.jpg 157KB
test-hua.bmp 583KB
decJPG.sdf 1.96MB
decJPG.vcxproj 6KB
decJPG.sln 878B
001.bmp 6.59MB
decJPG.ilk 212KB
decJPG.vcxproj.user 375B
JPEGDEC.C 27KB
JPEGAPP.C 4KB
共 52 条
- 1
资源评论
- xueluochenyang2014-06-01运行不起,得配环境??
- 郭晶2015-07-17有错误,运行不了
- sunwutian03252012-12-04我只能说错误一大堆
- qianchengsijinmi2013-12-02有错误 还行
- 幻影天河2014-01-02经常出现variable stack courrped错误
sduzzm
- 粉丝: 1
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功