C++ 使用ijg支持JPEG-获取访问冲突
我最近尝试更新以存储压缩格式(JPEG和PNG)的图形 虽然我最终选择了另一个库,但我最初的尝试是合并来进行JPEG解压缩。然而,即使是最简单的控制台应用程序也无法正常工作,我想知道是否有人能够解释原因 这是我的代码,链接到作为ijg包一部分的jpeg.lib:C++ 使用ijg支持JPEG-获取访问冲突,c++,c,jpeg,ijg,C++,C,Jpeg,Ijg,我最近尝试更新以存储压缩格式(JPEG和PNG)的图形 虽然我最终选择了另一个库,但我最初的尝试是合并来进行JPEG解压缩。然而,即使是最简单的控制台应用程序也无法正常工作,我想知道是否有人能够解释原因 这是我的代码,链接到作为ijg包一部分的jpeg.lib: #include "stdafx.h" #include <stdio.h> #include <assert.h> #include <jpeglib.h> int _tmain(int argc
#include "stdafx.h"
#include <stdio.h>
#include <assert.h>
#include <jpeglib.h>
int _tmain(int argc, _TCHAR* argv[])
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
JSAMPARRAY buffer;
int row_stride;
//initialize error handling
cinfo.err = jpeg_std_error(&jerr);
//initialize the decompression
jpeg_create_decompress(&cinfo);
FILE* infile;
errno_t err = fopen_s(&infile, "..\\Sample.jpg", "rb");
assert(err == 0);
//specify the input
jpeg_stdio_src(&cinfo, infile);
//read headers
(void) jpeg_read_header(&cinfo, TRUE);
return 0;
}
#包括“stdafx.h”
#包括
#包括
#包括
int _tmain(int argc,_TCHAR*argv[]
{
结构jpeg\u解压缩\u结构cinfo;
结构jpeg\u错误\u管理器jerr;
JSAMPA数组缓冲区;
int row_跨步;
//初始化错误处理
cinfo.err=jpeg\u std\u error(&jerr);
//初始化解压缩
jpeg\u创建\u解压缩(&cinfo);
文件*填充;
errno_t err=fopen_s(&infle,“..\\Sample.jpg”,“rb”);
断言(err==0);
//指定输入
jpeg_stdio_src(&cinfo,infle);
//读标题
(void)jpeg_read_头(&cinfo,TRUE);
返回0;
}
问题是调用jpeg\u read\u header()
失败,导致访问冲突:
0x7c91b1fa处未处理的异常
(ntdll.dll)在JPEGTest.exe中:
0xC0000005:访问冲突写入
位置0x00000010
有人知道我做错了什么吗?要处理多种格式的图像,请允许我向您推荐DevIL作为库。这是一个很好的选择,因为我已经多次使用它并取得了很好的效果。请注意,它的语法类似于OpenGL 功能列表: 支持加载:
- .bmp
- .切
- .dcx
- .dds
- 埃克斯
- .ico
- .icns
- .gif
- jpg先生
- .jp2
- .lbm
- .lif
- .mdl
- .pcd
- .pcx
- 图
- .png
- .pnm
- .psd
- .psp
- 生的
- sgi先生
- .tga
- .tif
- 沃尔先生
- .表演
- 朋友
- .hdr
- 厄运图形
- .bmp
- .dds
- jpg先生
- .pcx
- .png
- .pnm
- 生的
- sgi先生
- .tga
- .tif
- 朋友
- 便携式,支持Windows、Mac OS X和*nix
- OpenGL风格的语法
- 使用图像名称而不是丑陋的指针
- 从文件、文件流或内存“块”加载
- 通过ilGetData()和ilSetData()直接访问数据
- 支持亮度、rgb(a)、bgr(a)和彩色索引图像
- 支持每个通道3个不同位数的位
- 所有格式和数据类型(包括选项板)之间的转换
- 加载图像时,如果需要,用户定义的自动转换
- 如果需要,在保存图像时自动转换
- 如果需要,自动将彩色索引图像转换为真彩色图像
- 保存时可控制压缩
- 维护一个可以推送和弹出的状态堆栈
- 完全支持3d纹理体积(3d图像)
- 图像验证
- 对图层的支持
- 支持mipmap
- 支持动画
- 用户指定的清晰颜色
- 如果加载失败,可以加载默认图像
- 用户指定的提示
- 关键颜色的使用
- 支持将一个图像叠加到另一个图像上
- 允许用户指定自己的加载和保存回调,甚至覆盖默认回调
- 支持用户指定的读写功能
- Delphi支持
- Visual Basic支持
- Linux支持
- 可以选择用于创建较小DLL的功能
- 选择是使用英特尔Jpeg库还是使用libjpeg
- 应用于图像的一整套效果和过滤器,如浮雕和边缘检测
- 图像可以调整大小,甚至可以放在更大的背景上(放大的画布)
- OpenGL、Allegro、Windows GDI和DirectX API支持李>
- 我同意赫尔南的观点。这不是一个好的接口(我认为内部代码本身可能很好),除非您真的需要在底层工作(甚至可能不需要)。我认为ImageMagick可能更好。他们有一个更高级的“MagickWand”C接口,更不用说它支持更多的格式
然而,我对libjpeg的接口很好奇,所以我得到了一个测试程序,它基于您的示例程序以及,和,工作得很满意。不管怎样,这是代码。它只打印出尺寸和每行第一个像素的RGB
我很惊讶你在我的代码中出错。它对我来说很好,编译时没有任何警告。其他人能测试一下吗
#include <stdio.h> #include <assert.h> #include <jpeglib.h> int main(int argc, char* argv[]) { struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; JSAMPARRAY buffer; int row_stride; //initialize error handling cinfo.err = jpeg_std_error(&jerr); FILE* infile; infile = fopen("Sample.jpg", "rb"); assert(infile != NULL); //initialize the decompression jpeg_create_decompress(&cinfo); //specify the input jpeg_stdio_src(&cinfo, infile); //read headers (void) jpeg_read_header(&cinfo, TRUE); jpeg_start_decompress(&cinfo); printf("width: %d, height: %d\n", cinfo.output_width, cinfo.output_height); row_stride = cinfo.output_width * cinfo.output_components; buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1); JSAMPLE firstRed, firstGreen, firstBlue; // first pixel of each row, recycled while (cinfo.output_scanline < cinfo.output_height) { (void)jpeg_read_scanlines(&cinfo, buffer, 1); firstRed = buffer[0][0]; firstBlue = buffer[0][1]; firstGreen = buffer[0][2]; printf("R: %d, G: %d, B: %d\n", firstRed, firstBlue, firstGreen); } jpeg_finish_decompress(&cinfo); return 0; }
#包括 #包括 #包括 int main(int argc,char*argv[]) { 结构jpeg\u解压缩\u结构cinfo; 结构jpeg\u错误\u管理器jerr; JSAMPA数组缓冲区; int row_跨步; //初始化错误处理 cinfo.err=jpeg\u std\u error(&jerr); 文件*填充; infle=fopen(“Sample.jpg”、“rb”); 断言(infle!=NULL); //初始化解压缩 jpeg\u创建\u解压缩(&cinfo); //指定输入 jpeg_stdio_src(&cinfo,infle); //读标题 (void)jpeg_read_头(&cinfo,TRUE); jpeg\u开始\u解压缩(&cinfo); printf(“宽度:%d,高度:%d\n”,cinfo.output\u width,cinfo.output\u height); 行步距=cinfo.output\u width*cinfo.output\u组件; 缓冲区=(*cinfo.mem->alloc_sarray) ((j_common_ptr)和cinfo,JPOOL_图像,第1行步幅); JSAMPLE firstRed、firstGreen、firstBlue;//每行的第一个像素,循环使用 while(cinfo.output\u扫描线
png_set_write_fn (png_ptr,file,replwrite,replflush);
void replwrite (png_structp png_ptr, png_bytep data, png_size_t length) { fwrite (data,1,length,(FILE*) png_get_io_ptr(png_ptr)); } void replflush (png_structp png_ptr) { fflush ((FILE*) png_get_io_ptr(png_ptr)); }
void test(char FileName[]) { unsigned long x, y; struct jpeg_decompress_struct info; //for our jpeg info struct jpeg_error_mgr err; //the error handler JSAMPARRAY buffer; FILE* infile; //initialize error handling info.err = jpeg_std_error(& err); infile = fopen(FileName, "rb"); //open the file //if the jpeg file doesn't load if(!infile) { fprintf(stderr, "Error reading JPEG file %s!", FileName); // printf("Error reading JPEG file %s!", FileName); //return 0; } //initialize the decompression jpeg_create_decompress(&info); //specify the input jpeg_stdio_src(&info, infile); //read headers jpeg_read_header(&info, TRUE); // read jpeg file header jpeg_start_decompress(&info); // decompress the file //set width and height x = info.output_width; y = info.output_height; printf("x value is %ul", x); printf("x value is %ul", y); }