// A BMP truecolor to JPEG encoder
// Copyright 1999 Cristian Cuturicu
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jtypes.h"
#include "jglobals.h"
#include "jtables.h"
void write_APP0info()
//Nothing to overwrite for APP0info
{
writeword(APP0info.marker);
writeword(APP0info.length);
writebyte('J');
writebyte('F');
writebyte('I');
writebyte('F');
writebyte(0);
writebyte(APP0info.versionhi);
writebyte(APP0info.versionlo);
writebyte(APP0info.xyunits);
writeword(APP0info.xdensity);
writeword(APP0info.ydensity);
writebyte(APP0info.thumbnwidth);
writebyte(APP0info.thumbnheight);
}
void write_SOF0info()
// We should overwrite width and height
{
writeword(SOF0info.marker);
writeword(SOF0info.length);
writebyte(SOF0info.precision);
writeword(SOF0info.height);
writeword(SOF0info.width);
writebyte(SOF0info.nrofcomponents);
writebyte(SOF0info.IdY);
writebyte(SOF0info.HVY);
writebyte(SOF0info.QTY);
writebyte(SOF0info.IdCb);
writebyte(SOF0info.HVCb);
writebyte(SOF0info.QTCb);
writebyte(SOF0info.IdCr);
writebyte(SOF0info.HVCr);
writebyte(SOF0info.QTCr);
}
void write_DQTinfo()
{
BYTE i;
writeword(DQTinfo.marker);
writeword(DQTinfo.length);
writebyte(DQTinfo.QTYinfo);
for (i=0; i<64; i++)
writebyte(DQTinfo.Ytable[i]);
writebyte(DQTinfo.QTCbinfo);
for (i=0; i<64; i++)
writebyte(DQTinfo.Cbtable[i]);
}
void set_quant_table(BYTE *basic_table, BYTE scale_factor, BYTE *newtable)
// Set quantization table and zigzag reorder it
{
BYTE i;
long temp;
for (i=0; i<64; i++)
{
temp = ((long) basic_table[i] * scale_factor + 50L) / 100L;
// limit the values to the valid range
if (temp <= 0L)
temp = 1L;
if (temp > 255L)
temp = 255L;
newtable[zigzag[i]] = (BYTE) temp;
}
}
void set_DQTinfo()
{
BYTE scalefactor = 50;// scalefactor controls the visual quality of the image
// the smaller is the better image we'll get, and the smaller
// compression we'll achieve
DQTinfo.marker = 0xFFDB;
DQTinfo.length = 132;
DQTinfo.QTYinfo = 0;
DQTinfo.QTCbinfo = 1;
set_quant_table(std_luminance_qt, scalefactor, DQTinfo.Ytable);
set_quant_table(std_chrominance_qt, scalefactor, DQTinfo.Cbtable);
}
void write_DHTinfo()
{
BYTE i;
writeword(DHTinfo.marker);
writeword(DHTinfo.length);
writebyte(DHTinfo.HTYDCinfo);
for (i=0; i<16; i++)
writebyte(DHTinfo.YDC_nrcodes[i]);
for (i=0; i<12; i++)
writebyte(DHTinfo.YDC_values[i]);
writebyte(DHTinfo.HTYACinfo);
for (i=0; i<16; i++)
writebyte(DHTinfo.YAC_nrcodes[i]);
for (i=0; i<162; i++)
writebyte(DHTinfo.YAC_values[i]);
writebyte(DHTinfo.HTCbDCinfo);
for (i=0; i<16; i++)
writebyte(DHTinfo.CbDC_nrcodes[i]);
for (i=0; i<12; i++)
writebyte(DHTinfo.CbDC_values[i]);
writebyte(DHTinfo.HTCbACinfo);
for (i=0; i<16; i++)
writebyte(DHTinfo.CbAC_nrcodes[i]);
for (i=0; i<162; i++)
writebyte(DHTinfo.CbAC_values[i]);
}
void set_DHTinfo()
{
BYTE i;
// fill the DHTinfo structure [get the values from the standard Huffman tables]
DHTinfo.marker = 0xFFC4;
DHTinfo.length = 0x01A2;
DHTinfo.HTYDCinfo = 0;
for (i=0; i<16; i++)
DHTinfo.YDC_nrcodes[i] = std_dc_luminance_nrcodes[i+1];
for (i=0; i<12; i++)
DHTinfo.YDC_values[i] = std_dc_luminance_values[i];
DHTinfo.HTYACinfo = 0x10;
for (i=0; i<16; i++)
DHTinfo.YAC_nrcodes[i] = std_ac_luminance_nrcodes[i+1];
for (i=0; i<162; i++)
DHTinfo.YAC_values[i] = std_ac_luminance_values[i];
DHTinfo.HTCbDCinfo = 1;
for (i=0; i<16; i++)
DHTinfo.CbDC_nrcodes[i] = std_dc_chrominance_nrcodes[i+1];
for (i=0; i<12; i++)
DHTinfo.CbDC_values[i] = std_dc_chrominance_values[i];
DHTinfo.HTCbACinfo = 0x11;
for (i=0; i<16; i++)
DHTinfo.CbAC_nrcodes[i] = std_ac_chrominance_nrcodes[i+1];
for (i=0; i<162; i++)
DHTinfo.CbAC_values[i] = std_ac_chrominance_values[i];
}
void write_SOSinfo()
//Nothing to overwrite for SOSinfo
{
writeword(SOSinfo.marker);
writeword(SOSinfo.length);
writebyte(SOSinfo.nrofcomponents);
writebyte(SOSinfo.IdY);
writebyte(SOSinfo.HTY);
writebyte(SOSinfo.IdCb);
writebyte(SOSinfo.HTCb);
writebyte(SOSinfo.IdCr);
writebyte(SOSinfo.HTCr);
writebyte(SOSinfo.Ss);
writebyte(SOSinfo.Se);
writebyte(SOSinfo.Bf);
}
void write_comment(BYTE *comment)
{
WORD i, length;
writeword(0xFFFE); // The COM marker
length = strlen((const char *)comment);
writeword(length + 2);
for (i=0; i<length; i++)
writebyte(comment[i]);
}
void writebits(bitstring bs)
// A portable version; it should be done in assembler
{
WORD value;
SBYTE posval;// bit position in the bitstring we read, should be <=15 and >=0
value = bs.value;
posval = bs.length - 1;
while (posval >= 0)
{
if (value & mask[posval])
bytenew |= mask[bytepos];
posval--;
bytepos--;
if (bytepos < 0)
{
// write it
if (bytenew == 0xFF)
{
// special case
writebyte(0xFF);
writebyte(0);
}
else
writebyte(bytenew);
// reinit
bytepos = 7;
bytenew = 0;
}
}
}
void compute_Huffman_table(BYTE *nrcodes, BYTE *std_table, bitstring *HT)
{
BYTE k,j;
BYTE pos_in_table;
WORD codevalue;
codevalue = 0;
pos_in_table = 0;
for (k=1; k<=16; k++)
{
for (j=1; j<=nrcodes[k]; j++)
{
HT[std_table[pos_in_table]].value = codevalue;
HT[std_table[pos_in_table]].length = k;
pos_in_table++;
codevalue++;
}
codevalue <<= 1;
}
}
void init_Huffman_tables()
{
// Compute the Huffman tables used for encoding
compute_Huffman_table(std_dc_luminance_nrcodes, std_dc_luminance_values, YDC_HT);
compute_Huffman_table(std_ac_luminance_nrcodes, std_ac_luminance_values, YAC_HT);
compute_Huffman_table(std_dc_chrominance_nrcodes, std_dc_chrominance_values, CbDC_HT);
compute_Huffman_table(std_ac_chrominance_nrcodes, std_ac_chrominance_values, CbAC_HT);
}
void exitmessage(char *error_message)
{
printf("%s\n",error_message);
exit(EXIT_FAILURE);
}
void set_numbers_category_and_bitcode()
{
SDWORD nr;
SDWORD nrlower, nrupper;
BYTE cat;
category_alloc = (BYTE *)malloc(65535*sizeof(BYTE));
if (category_alloc == NULL)
exitmessage("Not enough memory.");
//allow negative subscripts
category = category_alloc + 32767;
bitcode_alloc=(bitstring *)malloc(65535*sizeof(bitstring));
if (bitcode_alloc==NULL)
exitmessage("Not enough memory.");
bitcode = bitcode_alloc + 32767;
nrlower = 1;
nrupper = 2;
for (cat=1; cat<=15; cat++)
{
//Positive numbers
for (nr=nrlower; nr<nrupper; nr++)
{
category[nr] = cat;
bitcode[nr].length = cat;
bitcode[nr].value = (WORD)nr;
}
//Negative numbers
for (nr=-(nrupper-1); nr<=-nrlower; nr++)
{
category[nr] = cat;
bitcode[nr].length = cat;
bitcode[nr].value = (WORD)(nrupper-1+nr);
}
nrlower <<= 1;
nrupper <<= 1;
}
}
void precalculate_YCbCr_tables()
{
WORD R,G,B;
for (R=0; R<256; R++)
{
YRtab[R] = (SDWORD)(65536*0.299+0.5)*R;
CbRtab[R] = (SDWORD)(65536*-0.16874+0.5)*R;
CrRtab[R] = (SDWORD)(32768)*R;
}
for (G=0; G<256; G++)
{
YGtab[G] = (SDWORD)(65536*0.587+0.5)*G;
CbGtab[G] = (SDWORD)(65536*-0.33126+0.5)*G;
CrGtab[G] = (SDWORD)(65536*-0.41869+0.5)*G;
}
for (B=0; B<256; B++)
{
YBtab[B] = (SDWORD)(65536*0.114+0.5)*B;
CbBtab[B] = (SDWORD)(32768)*B;
CrBtab[B] = (SDWORD)(65536*-0.08131+0.5)*B;
}
}
// Using a bit modified form of the FDCT routine from IJG's C source:
// Forward DCT routine idea taken from Independent JPEG Group's C source for
// JPEG encoders/decoders
/* For float AA&N IDCT method, divisors are equal to quantization
coefficients scaled by scalefactor[row]*scalefactor[col], where
scalefactor[0] = 1
scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
We apply a further scale factor of 8.
bmp转jpeg源码
5星 · 超过95%的资源 需积分: 10 20 浏览量
2008-10-18
18:14:01
上传
评论
收藏 10KB RAR 举报
ygw365
- 粉丝: 0
- 资源: 2
最新资源
- [其他类别]256色BMP图片验证码识别脚本_validpic.rar
- 微信小程序设计(含源代码+解释文档)之简单demo.zip
- 微信小程序设计(含源代码+解释文档)之o2o行业.zip
- ixchariot-endpoints-apk
- 数码管闪烁_单片机C语言实例(纯C语言源代码).zip
- matlab基于IEEE802.11a协议,OFDM加高斯白噪声的符号定时同步
- 数学建模与数学实验课件14讲含源程序_第8讲 最短路问题.zip
- matlab用于HDB3的编解码,用matlab仿真,有完整的程序.zip
- [影音娱乐]冰冰音乐程序商业版_bbyy.rar
- OpenGL 3D立方体多纹理贴图.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈