OpenCV是一个可以处理 PNG 文件以及多种其他格式的库。它应该可以在您的 Ubuntu 9.10 系统上使用apt get libcv-dev
(根据记忆,您可能需要仔细检查包名称)。
/*
* compile with:
*
* g++ -Wall -ggdb -I. -I/usr/include/opencv -L /usr/lib -lm -lcv -lhighgui -lcvaux filename.cpp -o filename.out
*/
#include <cv.h>
#include <highgui.h>
/*
* Your image dimensions.
*/
int width;
int height;
CvSize size = cvSize(width, height);
/*
* Create 3-channel image, unsigned 8-bit per channel.
*/
IplImage *image = cvCreateImage(size, IPL_DEPTH_8U, 3);
for (int i = 0; i < width; ++i)
for (int j = 0; j < height; ++j)
{
unsigned int r;
unsigned int g;
unsigned int b;
/*
* Keep in mind that OpenCV stores things in BGR order.
*/
CvScalar bgr = cvScalar(b, g, r);
cvSet2D(image, i, j, bgr);
}
cvSaveImage("filename.png", image);
cvReleaseImage(&image);