目录
图片来自:stb (c++) 操作 jpg图片
图片来自:stb (c++) 操作png图片
jpg图片
Resize: 200x100
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "stb/stb_image_resize.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("./s_l.jpg", &w, &h, &n, 0);
cout << "宽: " << w << endl;
cout << "高: " << h << endl;
cout << "channel: " << n << endl;
auto *re_data = (unsigned char *) malloc(200*100*3);
stbir_resize(data, w, h, 0, re_data, 200, 100, 0, STBIR_TYPE_UINT8, n, STBIR_ALPHA_CHANNEL_NONE, 0,
STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP,
STBIR_FILTER_BOX, STBIR_FILTER_BOX,
STBIR_COLORSPACE_SRGB, nullptr
);
string name = "s_l-jpg.jpg";
stbi_write_jpg(name.c_str(), 200, 100, n, re_data, 95);
string name2 = "s_l-png.png";
stbi_write_png(name2.c_str(),200,100,n,re_data,0);
stbi_image_free(data);
stbi_image_free(re_data);
}
宽: 400
高: 328
channel: 3
修改放缩后的像素值
中间-1列
蓝色
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "stb/stb_image_resize.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("./s_l.jpg", &w, &h, &n, 0);
auto *re_data = (unsigned char *) malloc(200*100*n);
stbir_resize(data, w, h, 0, re_data, 200, 100, 0, STBIR_TYPE_UINT8, n, STBIR_ALPHA_CHANNEL_NONE, 0,
STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP,
STBIR_FILTER_BOX, STBIR_FILTER_BOX,
STBIR_COLORSPACE_SRGB, nullptr
);
int red_col = 200/2;
for(int i=0; i<100; i++)
{
re_data[200*i*n+red_col*n+0] = 0;
re_data[200*i*n+red_col*n+1] = 0;
re_data[200*i*n+red_col*n+2] = 255;
}
string name = "s_l-jpg.jpg";
stbi_write_jpg(name.c_str(), 200, 100, n, re_data, 95);
string name2 = "s_l-png.png";
stbi_write_png(name2.c_str(),200,100,n,re_data,0);
stbi_image_free(data);
stbi_image_free(re_data);
}
png图片
Resize: 200x100
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "stb/stb_image_resize.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);
cout << "宽: " << w << endl;
cout << "高: " << h << endl;
cout << "channel: " << n << endl;
auto *re_data = (unsigned char *) malloc(200*100*n);
stbir_resize(data, w, h, 0, re_data, 200, 100, 0, STBIR_TYPE_UINT8, n, STBIR_ALPHA_CHANNEL_NONE, 0,
STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP,
STBIR_FILTER_BOX, STBIR_FILTER_BOX,
STBIR_COLORSPACE_SRGB, nullptr
);
string name = "world-jpg.jpg";
stbi_write_jpg(name.c_str(), 200, 100, n, re_data, 95);
string name2 = "world-png.png";
stbi_write_png(name2.c_str(),200,100,n,re_data,0);
stbi_image_free(data);
stbi_image_free(re_data);
}
宽: 1950
高: 1092
channel: 4
修改放缩后的像素值
中间-1列
红色
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "stb/stb_image_resize.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);
auto *re_data = (unsigned char *) malloc(200*100*n);
stbir_resize(data, w, h, 0, re_data, 200, 100, 0, STBIR_TYPE_UINT8, n, STBIR_ALPHA_CHANNEL_NONE, 0,
STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP,
STBIR_FILTER_BOX, STBIR_FILTER_BOX,
STBIR_COLORSPACE_SRGB, nullptr
);
int red_col = 200/2;
for(int i=0; i<100; i++)
{
re_data[200*i*n+red_col*n+0] = 255;
re_data[200*i*n+red_col*n+1] = 0;
re_data[200*i*n+red_col*n+2] = 0;
}
string name = "world-jpg.jpg";
stbi_write_jpg(name.c_str(), 200, 100, n, re_data, 95);
string name2 = "world-png.png";
stbi_write_png(name2.c_str(),200,100,n,re_data,0);
stbi_image_free(data);
stbi_image_free(re_data);
}