我正在做一些图像处理,我想单独读取JPEG和PNG图像中的每个像素值。
在我的部署场景中,如果我使用第三方库(因为我限制了目标计算机上的访问权限),我会感到很尴尬,但是我假设没有标准的C或C++库来读取JPEG/PNG.
所以,如果你知道一种不使用库的方法,那就太好了,如果不是,答案仍然是受欢迎的!
发布于 2009-03-29 04:02:39
C标准中没有标准库来读取文件格式.
但是,大多数程序,特别是linux平台上的程序,都使用相同的库来解码图像格式:
对于jpeg,它是libjpeg,对于png,它是libpng。
libs已经安装的可能性非常高。
http://www.libpng.org
http://www.ijg.org
发布于 2009-03-29 04:16:49
这是我从10年前的源代码中挖掘出来的一个小例程(使用libjpeg):
#include <jpeglib.h>
int loadJpg(const char* Name) {
unsigned char a, r, g, b;
int width, height;
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE * infile; /* source file */
JSAMPARRAY pJpegBuffer; /* Output row buffer */
int row_stride; /* physical row width in output buffer */
if ((infile = fopen(Name, "rb")) == NULL) {
fprintf(stderr, "can't open %s\n", Name);
return 0;
}
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, infile);
(void) jpeg_read_header(&cinfo, TRUE);
(void) jpeg_start_decompress(&cinfo);
width = cinfo.output_width;
height = cinfo.output_height;
unsigned char * pDummy = new unsigned char [width*height*4];
unsigned char * pTest = pDummy;
if (!pDummy) {
printf("NO MEM FOR JPEG CONVERT!\n");
return 0;
}
row_stride = width * cinfo.output_components;
pJpegBuffer = (*cinfo.mem->alloc_sarray)
((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
while (cinfo.output_scanline < cinfo.output_height) {
(void) jpeg_read_scanlines(&cinfo, pJpegBuffer, 1);
for (int x = 0; x < width; x++) {
a = 0; // alpha value is not supported on jpg
r = pJpegBuffer[0][cinfo.output_components * x];
if (cinfo.output_components > 2) {
g = pJpegBuffer[0][cinfo.output_components * x + 1];
b = pJpegBuffer[0][cinfo.output_components * x + 2];
} else {
g = r;
b = r;
}
*(pDummy++) = b;
*(pDummy++) = g;
*(pDummy++) = r;
*(pDummy++) = a;
}
}
fclose(infile);
(void) jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
BMap = (int*)pTest;
Height = height;
Width = width;
Depth = 32;
}发布于 2009-03-29 04:03:02
对于jpeg,已经有一个名为利比伯的库,还有用于png的利布平。好消息是,它们在其中编译,因此目标机器将不需要dll文件或任何东西。坏消息是他们在C区:
另外,你自己也不要想到试着读 档案。如果您想要一种易于阅读的格式,请改用百万分率。
https://stackoverflow.com/questions/694080
复制相似问题