C/C++ 使用 stb_image 加载 png / jpg / gif / bmp等常用图片

ChatGPT 3.5 国内中文镜像站免费使用啦

零基础 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 的一个非常流行的单头文件图像加载库,它能够读写大部分流行的文件格式,支持文件格式如下:

  • png
  • jpg
  • tga
  • bmp
  • psd
  • gif
  • hdr
  • pic

二.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 <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 = "d://input.png";
    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 = "d://output.png";
    // 写入图片
    stbi_write_png(outputPath.c_str(), ow, oh, n, odata, 0);
 
    stbi_image_free(idata);
    stbi_image_free(odata);
    return 0;
}

八.stb_image 完整代码下载

源码下载地址:https://github.com/nothings/stb

九.猜你喜欢

  1. C语言 数组下标越界和内存溢出区别
  2. C语言 使用指针遍历数组
  3. C语言 指针和数组区别
  4. C语言 指针数组和数组指针区别
  5. C语言 野指针
  6. C语言 函数值传递和址传递
  7. 函数不定长参数
  8. C语言 函数指针
  9. C语言 指针函数
  10. C语言 回调函数 callback
  11. C语言 #pragma once
  12. C语言 #include <> 与 #include “” 区别
  13. C语言 const 修饰函数参数
  14. C语言 const 和 define 区别
  15. C语言 va_start / va_end / va_arg 自定义 printf 函数
  16. C语言 main 函数参数 main(int argc, char *argv[])
  17. C语言 结构体struct简介(一)
  18. C语言 结构体struct定义和使用(二)
  19. C语言 结构体struct数组(三)
  20. C语言 结构体struct指针(四)
  21. C语言 结构体struct成员函数(五)
  22. C语言 结构体struct嵌套(六)
  23. C语言 结构体struct值传递和址传递(七)
  24. C/C++ error: cannot assign to non-static data member within const member function ‘xxxx’
  25. C++ 关于类中 const 的使用
  26. C/C++ =delete
  27. C/C++ 条件编译 #ifdef
  28. C/C++ error C2065: “M_PI”: 未声明的标识符
  29. C/C++ error C2027: 使用了未定义类型“std::tuple”
  30. C/C++ vs 没有匹配 if 的非法 else 问题解决办法
  31. C/C++ Visual studio 中文注释导致编译不能通过
  32. C/C++ error C2589: “(”: “::”右边的非法标记
  33. C/C++ error:表达式是必须修改的左值
  34. C/C++ error C4996: ‘getch’: The POSIX name for this item is deprecated. Instead, use the ISO C++ conf
  35. C/C++ error C4146: 一元负运算符应用于无符号类型,结果仍为无符号类型
  36. C/C++ std::string 字符串分割
  37. C/C++ std::string 使用介绍
  38. Visual Studio 中 dumpbin 工具使用
  39. C/C++ error LNK2005:”XXX已经在 XXX.obj 中定义
  40. C/C++ 判断字符串是否为 utf-8 编码
  41. C/C++ svpng 将 RGBA 保存 png 图片
  42. C/C++ 使用 stb_image 加载 png / jpg / gif / bmp等常用图片

ChatGPT 3.5 国内中文镜像站免费使用啦
© 版权声明
THE END
喜欢就支持一下吧
点赞2 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容