/*
*This progrmme is reedited from IJG code rdbmp.c by Fujian Shi(fieagle@yahoo.com).
* This file and commondecls.h can finish a updown of a color bmp image.
*
*/
#include "commondecls.h" /* Common decls for cjpeg/djpeg applications */
/* Macros to deal with unsigned chars as efficiently as compiler allows */
typedef unsigned char U_CHAR;
#define UCH(x) ((int) (x))
#define JFREAD(file,buf,sizeofbuf) \
((size_t) fread((void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file)))
#define JFWRITE(file,buf,sizeofbuf) \
((size_t) fwrite((const void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file)))
#define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len)))
#define WriteOK(file,buffer,len) (JFWRITE(file,buffer,len)==((size_t) (len)))
#define LOCAL(type) static type
/* Private version of data source object */
typedef struct _bmp_source_struct * bmp_source_ptr;
typedef struct _bmp_source_struct {
FILE *inputfile; /*source file pointer*/
FILE *outputfile; /*destination file pointer*/
JSAMPROW buffer; /*buffer pointer*/
JSAMPARRAY colormap; /* BMP colormap (converted to my format) */
UINT8 colormap_used; /*the tag of the colormap,in the real_color image,the colormap isn't used*/
JDIMENSION image_width;
JDIMENSION image_height; /* BMP image height*/
/* Needed to reverse row order ,?reserved temply.*/
/*JDIMENSION source_row; Current source row number */
JDIMENSION row_width; /* Physical width of scanlines in file */
int bits_per_pixel; /* remembers 8- or 24-bit format */
UINT16 X_density ; /* 100 cm per meter */
UINT16 Y_density ;
UINT8 density_unit ; /* dots/cm */
} bmp_source_struct;
/*arrage a row buffer*/
LOCAL(void *)
alloc_one_row(size_t size_object)
{
return (void *) malloc(size_object);
}
/*arrage several rows buffer,that is a array*/
LOCAL(JSAMPARRAY)
alloc_sarray (JDIMENSION samplesperrow, JDIMENSION numrows)
/* Allocate a 2-D sample array */
{
JSAMPARRAY result;
JSAMPROW workspace;
int currow;
/* Get space for row pointers (small object) */
result = (JSAMPARRAY) alloc_one_row((size_t) (numrows * SIZEOF(JSAMPROW)));
/* Get the rows themselves (large objects) */
currow = 0;
while (currow < numrows) {
workspace = (JSAMPROW) alloc_one_row((size_t) (samplesperrow
* SIZEOF(JSAMPLE)));
result[currow++] = workspace;
}
return result;
}
LOCAL(int)
read_byte (bmp_source_ptr sinfo)
/* Read next byte from BMP file */
{
register FILE *infile =sinfo->inputfile;
register int c=0;
if ((c = getc(infile)) == EOF){
printf("The wholefile has been input");
exit (0);
}
return c;
}
LOCAL(void)
read_colormap (bmp_source_ptr sinfo, int cmaplen, int mapentrysize)
/* Read the colormap from a BMP file */
/*cmalen is the number of color in the bmp image*/
{
int i;
switch (mapentrysize) {
case 3:
/* BGR format (occurs in OS/2 files) */
for (i = 0; i < cmaplen; i++) {
sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo);
sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo);
sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo);
}
break;
case 4:
/* BGR0 format (occurs in MS Windows files) */
for (i = 0; i < cmaplen; i++) {
sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo);
sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo);
sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo);
(void) read_byte(sinfo);
}
break;
default:
exit (0);
break;
}
}
/*
* Read one row of pixels.
* The image has been read into the whole_image array, but is otherwise
* unprocessed. We must read it out in top-to-bottom row order, and if
* it is an 8-bit image, we must expand colormapped pixels to 24bit format.
*/
/*LOCAL(JDIMENSION)
get_8bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
*This version is for reading 8-bit colormap indexes*
{
bmp_source_ptr source = (bmp_source_ptr) sinfo;
register JSAMPARRAY colormap = source->colormap;
JSAMPARRAY image_ptr;
register int t;
register JSAMPROW inptr, outptr;
register JDIMENSION col;
* Fetch next row from virtual array *
source->source_row--;
image_ptr = (*cinfo->mem->access_virt_sarray)
((j_common_ptr) cinfo, source->whole_image,
source->source_row, (JDIMENSION) 1, FALSE);
* Expand the colormap indexes to real data *
inptr = image_ptr[0];
outptr = source->pub.buffer[0];
for (col = cinfo->image_width; col > 0; col--) {
t = GETJSAMPLE(*inptr++);
*outptr++ = colormap[0][t]; * can omit GETJSAMPLE() safely *
*outptr++ = colormap[1][t];
*outptr++ = colormap[2][t];
}
return 1;
}
*/
/*METHODDEF(JDIMENSION)
get_24bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
* This version is for reading 24-bit pixels *
{
bmp_source_ptr source = (bmp_source_ptr) sinfo;
JSAMPARRAY image_ptr;
register JSAMPROW inptr, outptr;
register JDIMENSION col;
* Fetch next row from virtual array *
source->source_row--;
image_ptr = (*cinfo->mem->access_virt_sarray)
((j_common_ptr) cinfo, source->whole_image,
source->source_row, (JDIMENSION) 1, FALSE);
* Transfer data. Note source values are in BGR order
* (even though Microsoft's own documents say the opposite).
*
inptr = image_ptr[0];
outptr = source->pub.buffer[0];
for (col = cinfo->image_width; col > 0; col--) {
outptr[2] = *inptr++; * can omit GETJSAMPLE() safely *
outptr[1] = *inptr++;
outptr[0] = *inptr++;
outptr += 3;
}
return 1;
}
*/
/*
* This method loads the image into whole_image during the first call on
* get_pixel_rows. The get_pixel_rows pointer is then adjusted to call
* get_8bit_row or get_24bit_row on subsequent calls.
*/
LOCAL(JDIMENSION)
process_image (bmp_source_ptr sinfo)
{
register FILE *infile = sinfo->inputfile;
register FILE *outfile=sinfo->outputfile;
register int c;
register JSAMPROW buffer_ptr; /*buffer pointer,the buffer is only one row*/
JDIMENSION row, col;
long position_adju;
/* read and write data. */
for (row = 0; row < sinfo->image_height; row++) {
buffer_ptr=sinfo->buffer;
/*out_ptr = image_ptr[0];*/
for (col = sinfo->row_width; col > 0; col--) { /*by the cycle input one row sample into the buffer*/
/* inline copy of read_byte() for speed */
if ((c = getc(infile)) == EOF)
exit(0);
*buffer_ptr++ = (JSAMPLE) c;
if(row==(sinfo->image_height-1)&&(col==2||col==1))
printf("the oribmp.bmp last pixel element is%d\n",c);
}
position_adju=(long) ((row+1)*sinfo->row_width);
/*Whether it is need to add 2 depend on the image format,
*normally it is 24 bit bmp image.
*/
fseek(outfile,-position_adju,SEEK_END);
if(!WriteOK(outfile,sinfo->buffer,sinfo->row_width)){
printf("the data cann't be write into the new image.\n");
exit(0);
}
}
/* Set up to read from the virtual array in top-to-bottom order *
switch (sinfo->bits_per_pixel) {
case 8:
source->pub.get_pixel_rows = get_8bit_row;
break;
case 24:
source->pub.get_pixel_rows = get_24bit_row;
break;
default:
ERREXIT(cinfo, JERR_BMP_BADDEPTH);
}
source->source_row = cinfo->image_height;
* And read the first row *
return (*source->pub.get_pixel_rows) (cinfo, sinfo);
*/
}
/*
* Read the file header; return image size and component count.
*/
LOCAL(void)
start_input_bmp (bmp_source_ptr sinfo)
{
U_CHAR bmpfileheader[14];
U_CHAR bmpinfoheader[64];
#define GET_2B(array,offset) ((unsigned int) UCH(array[offset]) + \
(((unsigned int) UCH(array[offset+1])) << 8))
#define GET_4B(array,offset) ((INT32) UCH(array[offset]) + \
(((INT32) UCH(array[offset+1])) << 8) + \
(((INT32) UCH(array[offset+2])) << 16) + \
(((INT32) UCH(array[offset+3])) << 24))
INT32 bfOffBits;
INT32 headerSize;
INT32 biWidt
图像jpeg压缩算法介绍及其源码
5星 · 超过95%的资源 需积分: 36 126 浏览量
2011-04-03
16:26:38
上传
评论 2
收藏 279KB RAR 举报
hillwing0819
- 粉丝: 0
- 资源: 2
最新资源
- 基于Matlab人脸肤色定理的教师人数统计+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab霍夫曼变换的表盘读数识别+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab火灾烟雾检测源码带GUI界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的恶劣天气交通标志识别系统+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的霍夫曼变换的表盘示数识别+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的车道线识别系统 +源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的教室人数统计系统带Gui界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的教室人数统计系统带Gui界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB 的霍夫曼变换答题卡识别源码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab+bp神经网络的神经网络汉字识别系统+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
前往页