C/C++ 使用 stb 您所在的位置:网站首页 data3883/images/779804fd4534edb08b2ff93328451155.png C/C++ 使用 stb

C/C++ 使用 stb

2022-11-26 19:47| 来源: 网络整理| 查看: 265

目录

零基础 C/C++ 学习路线推荐 : C/C++ 学习目录 >> C 语言基础入门

零基础 C/C++ 学习路线推荐 : C/C++ 学习目录 >> C 语言 pthread

零基础 C/C++ 学习路线推荐 : C/C++ 学习目录 >> C++ 面向对象

零基础 C/C++ 学习路线推荐 : C/C++ 学习目录 >> C++ 设计模式

零基础 C/C++ 学习路线推荐 : C/C++ 学习目录 >> C++ STL

零基础 C/C++ 学习路线推荐 : C/C++ 学习目录 >> C/C++ 技术杂谈

零基础 C/C++ 学习路线推荐 : C/C++ 学习目录 >> C/C++ 常用函数

前面介绍了 svpng 函数,用于将 RGB / RGBA 图像保存为 PNG。今天在介绍另外一个 stb_image;

一.stb_image 简介

stb_image.h 是 Sean Barrett 的一个非常流行的单头文件图像加载库,它能够读写大部分流行的文件格式,支持文件格式如下:

pngjpgtgabmppsdgifhdrpic 二.stb_image 配置到工程中使用

stb_image.h 代码在文章末尾,可以直接复制粘贴使用,当然也可以在 github https://github.com/nothings/stb下载。

stb_image.h 加入你的工程,并另创建一个新的 C++ 文件,输入以下代码:

#define STB_IMAGE_IMPLEMENTATION //必须加上 #include "stb_image.h"

通过定义 STB_IMAGE_IMPLEMENTATION,预处理器会修改头文件,让其只包含相关的函数定义源码,等于是将这个头文件变为一个 .cpp 文件了,现在只需要在你的程序中包含 stb_image.h 之后输入你的代码并编译就可以了。

三.stb_image 读取

要使用 stb_image.h 加载图片,我们需要使用它的 stbi_load 函数:

/******************************************************************************************/ //@Author:猿说编程 //@Blog(个人博客地址): www.codersrc.com //@File:C/C++ 使用 stb_image 加载 png / jpg / gif / bmp等常用图片 //@Time:2022/03/28 07:30 //@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! /******************************************************************************************/ #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" /* * 描述:载入图像,支持的图像文件格式包括JPEG、PNG、TGA、BMP、PSD、GIF、HDR、PIC、PNM * * 参数: * filename:图像文件名 * x:获取图像宽 * y:获取图像高 * channels_in_file:获取图像通道数 * desired_channels:指定期望的通道数,若为0则不做颜色空间变换 * * 返回值:加载图像成功返回图像数据指针,否则返回NULL; */ unsigned char *stbi_load (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels); 四.stb_image 写入

stb_image 可以分别支持生产 png 和 jpg,函数声明如下

/******************************************************************************************/ //@Author:猿说编程 //@Blog(个人博客地址): www.codersrc.com //@File:C/C++ 使用 stb_image 加载 png / jpg / gif / bmp等常用图片 //@Time:2022/03/28 07:30 //@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! /******************************************************************************************/ #define STB_IMAGE_WRITE_IMPLEMENTATION #define STB_IMAGE_WRITE_STATIC #include "stb_image_write.h" /* * 描述:保存图像,支持的图像文件格式包括PNG、BMP、TGA、JPG、HDR * * 参数: * filename:保存图像名 * x:图像宽 * y:图像高 * comp:图像通道数 * data:指定期望的通道数,若为0则不做颜色空间变换 * quality:图像质量(quality,取值范围1~100,仅限jpg) * * 返回值:成功返回非0值,否则返回0。 */ int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality) /* * 描述:保存图像,支持的图像文件格式包括PNG、BMP、TGA、JPG、HDR * * 参数: * filename:保存图像名 * x:图像宽 * y:图像高 * comp:图像通道数 * data:指定期望的通道数,若为0则不做颜色空间变换 * stride_bytes:步长,若为0则为宽*通道数,仅限png * * 返回值:成功返回非0值,否则返回0。 */ int stbi_write_png(char const *filename, int x, int y, int comp, const void *data, int stride_bytes) 五.stb_image 缩放

stb_image 缩放函数如下:

/******************************************************************************************/ //@Author:猿说编程 //@Blog(个人博客地址): www.codersrc.com //@File:C/C++ 使用 stb_image 加载 png / jpg / gif / bmp等常用图片 //@Time:2022/03/28 07:30 //@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! /******************************************************************************************/ #define STB_IMAGE_RESIZE_IMPLEMENTATION #define STB_IMAGE_RESIZE_STATIC #include "stb_image_resize.h" /* * 描述:图像缩放 * * 参数: * input_pixels:输入图像数据指针 * input_w:输入图像宽 * input_h:输入图像高 * input_stride_in_bytes:输入图像步长,若为0则为宽x通道数 * output_pixels:输出图像数据指针 * output_w:输出图像宽 * output_h:输出图像高 * output_stride_in_bytes:输出图像步长,若为0则为宽x通道数 * num_channels:图像通道数,输入与输出一致 * * 返回值:成功返回1,否则返回0; */ int stbir_resize(const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,stbir_datatype datatype,int num_channels, int alpha_channel, int flags,stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical,stbir_filter filter_horizontal, stbir_filter filter_vertical,stbir_colorspace space, void *alloc_context); 六.stb_image 内存释放

stb_image 释放内存如下:

void stbi_image_free (void *retval_from_stbi_load); 七.stb_image 使用案例 /******************************************************************************************/ //@Author:猿说编程 //@Blog(个人博客地址): www.codersrc.com //@File:C/C++ 使用 stb_image 加载 png / jpg / gif / bmp等常用图片 //@Time:2022/03/28 07:30 //@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! /******************************************************************************************/ #include #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 #include #include #include using namespace std; int main() { std::cout


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有