引擎开发二: stb_image库及使用

  stb_image 是一个简单易用的图像解码库。

安装及使用

环境:win7 VS2015

1. 下载stb_image :

github地址:https://github.com/nothings/stb

在这里插入图片描述

2. opengl项目配置:

  因为stb_image库实现都写在头文件中,不需要编译成库,项目中直接引用头文件目录即可。

a. 项目属性 ----> C/C++ —> 附加包含目录 —> your_path\stb-master

3. 代码:

该程序主要实现了加载图片,修改图片宽高并导出。

#include <iostream>

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "stb_image_resize.h"
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <vector>

using namespace std;

int main() {
	std::cout << "Hello, STB_Image" << std::endl;

	string inputPath = "../res/11.jpg";
	int iw, ih, n;

	// 加载图片获取宽、高、颜色通道信息
	unsigned char *idata = stbi_load(inputPath.c_str(), &iw, &ih, &n, 0);

	int ow = iw / 2;
	int oh = ih / 2;
	auto *odata = (unsigned char *)malloc(ow * oh * n);

	// 改变图片尺寸
	stbir_resize(idata, iw, ih, 0, odata, ow, oh, 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 outputPath = "../res/11out.jpg";
	// 写入图片
	stbi_write_png(outputPath.c_str(), ow, oh, n, odata, 0);

	stbi_image_free(idata);
	stbi_image_free(odata);
	return 0;
}
4. 运行结果:

原图:
在这里插入图片描述

处理后:

在这里插入图片描述

  • 11
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值