图像库stb_image

https://github.com/nothings/stb

目前一般主流的图像格式也就是bmp,jpg,png,tga,dds,除了DDS一般是给DX用的,虽然一堆OpenGL程序也有用的,但是我一般只用png和tga,

png不用说了,带alpha通道,tga就是4通道信息,如果你想3通道存颜色,4通道不存透明而是别的什么信息,又有编辑器如Photoshop支持的,tga就用得着,而png虽然

也能存alpha,但是编辑器不支持Alpha单独编辑那种诡异玩法就不行了。然尔一般也就png。所以来吃狗!!!

简单的写个,读写png的例子,精简自自带例子,(0,0)是左上角像素

 

#include <stdio.h>

#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
    //load image
    unsigned char *data = stbi_load("rgba.png", &w, &h, &n, 0);

    printf("%d, %d, %d\n", w, h, n);

    //change pixel

    //rgba,write 10 red pixel at line 11
    for (int dx = 0; dx < 10; ++dx)
    {
        data[n * w * 10 + dx * n + 0] = 255;
        data[n * w * 10 + dx * n + 1] = 0;
        data[n * w * 10 + dx * n + 2] = 0;
        data[n * w * 10 + dx * n + 3] = 255;
    }
    

    //write image
    stbi_write_png("write.png", w, h, n, data, w * 4);

    stbi_image_free(data);


    return 0;
}

 

#include <iostream>
#include <fstream>
#include <stbi/stb_image.h>

const unsigned char * loadfile(const std::string &file, int &size)
{
    std::ifstream fs(file.c_str(), std::ios::binary);
    fs.seekg(0, std::ios::end);
    size = fs.tellg();
    char * data = new char[size + 1];
    fs.seekg(0);
    fs.read(data, size);
    fs.close();
    data[size] = 0;
    return (unsigned char *)data;
}


int main()
{
    int w;
    int h;
    int channels;
    int size;
    const unsigned char * data = loadfile("D:/1.jpg", size);
    const unsigned char * logo = stbi_load_from_memory(data, size, &w, &h, &channels, 0);
    for (int i = 0; i < 3; ++i)
    {
        std::cout << (int)logo[i] << std::endl;
    }
}

 

转载于:https://my.oschina.net/robslove/blog/3003787

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值