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;
}
}