栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

stb

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

stb

读写使用一个头文件,可以不使用libpng,libjpeg等等库,好处是更为简单,使用opengl时极其简单。
定义STB_IMAGE_IMPLEMENTATION,预处理器会修改头文件,让其只包含相关的函数定义源码,将这个头文件包含源代码

读文件
#include 
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

int main(int argc, const char * argv[]) {
    int width, height, nrChannels;
    unsigned char *data = stbi_load("test.jpg", &width, &height, &nrChannels, 0);
    printf("w %d, h %d, c %dn", width, height, nrChannels);
    
    return 0;
}
写文件

注意时rgba的写入,四字节

#include 
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
int main(int argc, char** argv)
{
    int w, h, n;
    //rgba
    unsigned char *data = stbi_load("test.png", &w, &h, &n, 0);
    printf("%d, %d, %dn", w, h, n);
    //rgba
    for (int dx = 0; dx < 10; ++dx)
    {
        data[n * w * 10 + dx * n + 0] = 255;
        data[n * w * 10 + dx * n + 1] = 255;
        data[n * w * 10 + dx * n + 2] = 128;
        data[n * w * 10 + dx * n + 3] = 100;
     }
    //write image
    stbi_write_png("write.png", w, h, n, data, w * n);
    stbi_image_free(data);
    return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/289764.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号