概述
在有限的内存下,访问完一张的图片的所有数据。
需要搭配jpeglib
void analyse_jpeg()
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
JSAMPARRAY buffer;
unsigned char *src_buff;
unsigned char *point;
unsigned char *dst_width_buff;
cinfo.err=jpeg_std_error(&jerr); //一下为libjpeg函数,具体参看相关文档
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo,input_file);
jpeg_read_header(&cinfo,TRUE);
jpeg_start_decompress(&cinfo);
unsigned long width=cinfo.output_width;
unsigned long height=cinfo.output_height;
unsigned short depth=cinfo.output_components;
src_buff=new unsigned char[width*400*depth];
memset(src_buff,0,sizeof(unsigned char)*width*400*depth);
dst_width_buff=new unsigned char[width*400*3];
memset(dst_width_buff,0,sizeof(unsigned char)*width*400*3);
buffer=(*cinfo.mem->alloc_sarray)
((j_common_ptr)&cinfo,JPOOL_IMAGE,width*depth,1);
point=src_buff;
//while (cinfo.output_scanline<height)
//{
// jpeg_read_scanlines(&cinfo,buffer,1); //读取一行jpg图像数据到buffer
// memcpy(point,*buffer,width*depth); //将buffer中的数据逐行给src_buff
// point+=width*depth; //一次改变一行
//}
unsigned int i = 0;
int j = 0;
linkList *tempList;
tempList = (linkList *)malloc(sizeof(linkList));
initLinkList(tempList);
while(cinfo.output_scanline < height)
{
if (height - cinfo.output_scanline > 400)
{
for (i = 0;i< 400;i++)
{
(void) jpeg_read_scanlines(&cinfo, buffer, 1);
memcpy(point,*buffer,width*depth); //将buffer中的数据逐行给src_buff
point += width*depth; //一次改变一次
}
//图片格式转换CMYk->RGB
for (unsigned long jBuffer=0;jBuffer<width*400;jBuffer+=1)
{
dst_width_buff[3*jBuffer+0]=255-(src_buff[4*jBuffer+3]+src_buff[4*jBuffer+0]);
dst_width_buff[3*jBuffer+1]=255-(src_buff[4*jBuffer+3]+src_buff[4*jBuffer+1]);
dst_width_buff[3*jBuffer+2]=255-(src_buff[4*jBuffer+3]+src_buff[4*jBuffer+2]);
}
point -= width*depth*400;
j = cinfo.output_scanline/400;
HObject image;
try
{
GenImageInterleaved(&image,(int)dst_width_buff, "rgb", (int)width,(int)height, 0, "byte", (int)width,400, 0, 0, -1, 0);
ImagePro(image,tempList,j);
WriteImage(image,"jpeg",0,"D:\佳能图片\007");
}
catch(HException &except)
{
string str = except.ErrorMessage();
char *p =(char *)str.data();
printf("%s",p);
}
memset(src_buff,0,sizeof(unsigned char)*width*400*depth);
}
else
{
// /*在此添加图像处理代码*/
while (height - cinfo.output_scanline >0)
{
(void) jpeg_read_scanlines(&cinfo, buffer, 1);
memcpy(point,*buffer,width*depth); //将buffer中的数据逐行给src_buff
point+=width*depth; //一次改变一行
}
j = cinfo.output_scanline/400 + 1;
for (unsigned long jBuffer=0;jBuffer<width*(height%400);jBuffer+=1)
{
dst_width_buff[3*jBuffer+0]=255-(src_buff[4*jBuffer+3]+src_buff[4*jBuffer+0]);
dst_width_buff[3*jBuffer+1]=255-(src_buff[4*jBuffer+3]+src_buff[4*jBuffer+1]);
dst_width_buff[3*jBuffer+2]=255-(src_buff[4*jBuffer+3]+src_buff[4*jBuffer+2]);
}
HObject image;
try
{
GenImageInterleaved(&image,(int)dst_width_buff, "rgb", (int)width,(int)height, 0, "byte", (int)width,(int)(height%400), 0, 0, -1, 0);
ImagePro(image,tempList,j);
WriteImage(image,"jpeg",0,"D:\佳能图片\006");
}
catch(HException &except)
{
string str = except.ErrorMessage();
char *p =(char *)str.data();
printf("%s",p);
}
}
}
//write_bmp_header(&cinfo); //写bmp文件头
//write_bmp_data(&cinfo,src_buff); //写bmp像素数据
HObject ho_Contour,ObjectsConcat;
while (tempList->next!=NULL)
{
tempList = tempList->next;
//HTuple Row;
//HTuple Col;
//memcpy(&Row,&tempList->Row,sizeof(HTuple));
//memcpy(&Col,&tempList->Col,sizeof(HTuple));
GenContourNurbsXld(&ho_Contour, tempList->Row,tempList->Col, "auto", "auto", 3, 1, 5);
ConcatObj(ho_Contour,ObjectsConcat,&ObjectsConcat);
}
WriteContourXldDxf(ObjectsConcat,"D://icon//2.dxf");
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
delete[] src_buff;
}
int main()
{
input_file=fopen("D:\佳能图片\巡边图2.jpg","rb");
//input_file=fopen("D:\佳能图片\巡边图2.bmp","rb");
analyse_jpeg();
fclose(input_file);
//fclose(output_file);
cout<<"good job."<<endl;
cin.get();
return 0;
}
最后
以上就是干净玫瑰为你收集整理的jpg图片的解压缩算法以及分段取图片的全部内容,希望文章能够帮你解决jpg图片的解压缩算法以及分段取图片所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复