项目介绍
stb项目是一个ALLINONE的c解决方案,其中有一个stb_image.h,可以用来操作图片。 推荐给大家的博客
Example
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#define COLOR_L 3
int main()
{
int width, height, depth;
unsigned char *buffer;
buffer = stbi_load("wld.jpg", &width, &height, &depth, 0);
unsigned char *newbuffer = (unsigned char *)malloc(width * height * COLOR_L);
unsigned char r, g, b, grey;
for (int n = 0; n < height; n++)
{
for (int m = 0; m < width; m++)
{
// 第n行,m列像素
r = buffer[n * width * COLOR_L + m * COLOR_L];
g = buffer[n * width * COLOR_L + m * COLOR_L + 1];
b = buffer[n * width * COLOR_L + m * COLOR_L + 2];
grey = 0.3 * r + 0.59 * g + 0.11 * b;
newbuffer[n * width * COLOR_L + m * COLOR_L] = grey;
newbuffer[n * width * COLOR_L + m * COLOR_L + 1] = grey;
newbuffer[n * width * COLOR_L + m * COLOR_L + 2] = grey;
}
}
stbi_image_free(buffer);
stbi_write_png("wld.png", width, height, depth, newbuffer, width * depth);
stbi_write_jpg("wld2.jpg", width, height, depth, newbuffer, 100);
return 0;
}
程序使用stbi_load读取数据,使用stbi_write_png和stbi_write_jpg写入数据。
针对每个像素,我们做了一个加权取值, 得