目录
测试图
world.png
读图
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
using namespace std;
int main()
{
int w,h,n;
unsigned char *data = stbi_load("./world.png", &w, &h, &n, 0);
cout << "宽: " << w << endl;
cout << "高: " << h << endl;
cout << "channel: " << n << endl;
}
宽: 1950
高: 1092
channel: 4
取像素值
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
using namespace std;
int main()
{
int w,h,n;
unsigned char *data = stbi_load("./world.png", &w, &h, &n, 0);
cout << "宽: " << w << endl;
cout << "高: " << h << endl;
cout << "channel: " << n << endl;
cout << "---------\n";
int x=200,y=100;
cout << data[y*w*n + x*n + 0] << endl;
cout << data[y*w*n + x*n + 1] << endl;
cout << data[y*w*n + x*n + 2] << endl;
cout << "---------\n";
cout << int(data[y*w*n + x*n + 0]) << endl;
cout << int(data[y*w*n + x*n + 1]) << endl;
cout << int(data[y*w*n + x*n + 2]) << endl;
}
宽: 1950
高: 1092
channel: 4
---------
0
A
%
---------
48
65
37
保存图片
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
using namespace std;
int main()
{
int w,h,n;
unsigned char *data = stbi_load("./world.png", &w, &h, &n, 0);
string name = "world-jpg.jpg";
stbi_write_jpg(name.c_str(), w,h,n,data,95);
string name2 = "world-png.png";
stbi_write_png(name2.c_str(),w,h,n,data,0);
}
看下world-jpg.jpg属性
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
using namespace std;
int main()
{
int w,h,n;
unsigned char *data = stbi_load("./world-jpg.jpg", &w, &h, &n, 0);
cout << "宽: " << w << endl;
cout << "高: " << h << endl;
cout << "channel: " << n << endl;
}
宽: 1950
高: 1092
channel: 3
修改像素值
中间-1列
红色
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
using namespace std;
int main()
{
int w,h,n;
unsigned char *data = stbi_load("./world.png", &w, &h, &n, 0);
int red_col = w/2;
for(int i=0; i<h; i++)
{
data[w*i*n+red_col*n+0] = 255;
data[w*i*n+red_col*n+1] = 0;
data[w*i*n+red_col*n+2] = 0;
}
string name = "world-plot.jpg";
stbi_write_jpg(name.c_str(), w,h,n,data,95);
string name2 = "world-plot.png";
stbi_write_png(name2.c_str(),w,h,n,data,0);
}
绿色
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
using namespace std;
int main()
{
int w,h,n;
unsigned char *data = stbi_load("./world.png", &w, &h, &n, 0);
int red_col = w/2;
for(int i=0; i<h; i++)
{
data[w*i*n+red_col*n+0] = 0;
data[w*i*n+red_col*n+1] = 255;
data[w*i*n+red_col*n+2] = 0;
}
string name = "world-plot.jpg";
stbi_write_jpg(name.c_str(), w,h,n,data,95);
string name2 = "world-plot.png";
stbi_write_png(name2.c_str(),w,h,n,data,0);
}
蓝色
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
using namespace std;
int main()
{
int w,h,n;
unsigned char *data = stbi_load("./world.png", &w, &h, &n, 0);
int red_col = w/2;
for(int i=0; i<h; i++)
{
data[w*i*n+red_col*n+0] = 0;
data[w*i*n+red_col*n+1] = 0;
data[w*i*n+red_col*n+2] = 255;
}
string name = "world-plot.jpg";
stbi_write_jpg(name.c_str(), w,h,n,data,95);
string name2 = "world-plot.png";
stbi_write_png(name2.c_str(),w,h,n,data,0);
}
中间-1行
红色
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
using namespace std;
int main()
{
int w,h,n;
unsigned char *data = stbi_load("./world.png", &w, &h, &n, 0);
int red_col = h/2;
for(int i=0; i<w; i++)
{
data[w*red_col*n+i*n+0] = 255;
data[w*red_col*n+i*n+1] = 0;
data[w*red_col*n+i*n+2] = 0;
}
string name = "world-plot.jpg";
stbi_write_jpg(name.c_str(), w,h,n,data,95);
string name2 = "world-plot.png";
stbi_write_png(name2.c_str(),w,h,n,data,0);
}
绿色
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
using namespace std;
int main()
{
int w,h,n;
unsigned char *data = stbi_load("./world.png", &w, &h, &n, 0);
int red_col = h/2;
for(int i=0; i<w; i++)
{
data[w*red_col*n+i*n+0] = 0;
data[w*red_col*n+i*n+1] = 255;
data[w*red_col*n+i*n+2] = 0;
}
string name = "world-plot.jpg";
stbi_write_jpg(name.c_str(), w,h,n,data,95);
string name2 = "world-plot.png";
stbi_write_png(name2.c_str(),w,h,n,data,0);
}
蓝色
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
using namespace std;
int main()
{
int w,h,n;
unsigned char *data = stbi_load("./world.png", &w, &h, &n, 0);
int red_col = h/2;
for(int i=0; i<w; i++)
{
data[w*red_col*n+i*n+0] = 0;
data[w*red_col*n+i*n+1] = 0;
data[w*red_col*n+i*n+2] = 255;
}
string name = "world-plot.jpg";
stbi_write_jpg(name.c_str(), w,h,n,data,95);
string name2 = "world-plot.png";
stbi_write_png(name2.c_str(),w,h,n,data,0);
}