Created
February 6, 2022 05:42
-
-
Save ibreathebsb/c317d71b1463e4bf30202c65d8a1762e to your computer and use it in GitHub Desktop.
The imgp file loader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include <fstream> | |
#include <map> | |
#include<vector> | |
#include <stb_image.h> | |
#include <stb_image_write.h> | |
#define IMGP_SIZE 1024 * 1024 | |
unsigned char buffer[IMGP_SIZE]; | |
unsigned char result[IMGP_SIZE]; | |
unsigned char* imgpParse() { | |
int width = *(int*)(buffer); | |
int height = *(int*)(buffer + 4); | |
auto indexPtr = (unsigned char *)buffer + 8; | |
int* colorDataPtr = (int*)(buffer + 8); | |
auto cnt = width * height; | |
std::map<unsigned char, unsigned short> colorMap; | |
unsigned int colorCnt = *(unsigned char*)(buffer + 8 + width * height + 10); | |
int* colorPtr = (int*)(buffer + 8 + width * height + 10 + 2); | |
for (unsigned int i = 0; i < colorCnt; i++) | |
{ | |
unsigned short rgba5551 = 0; | |
int color = *(int*)(colorPtr + i); | |
int alpha = color >> 23; | |
rgba5551 = alpha; | |
rgba5551 = alpha | ( 2 * (color & 0x1F | (32 * ((color >> 6) & 0x1F | (32 * (color >> 11)))))); | |
colorMap.insert({ i, rgba5551 }); | |
} | |
unsigned short* resultPtr = (unsigned short *)result; | |
for (int i = 0; i < cnt; i++) | |
{ | |
resultPtr[i] = colorMap.at(*(indexPtr + i)); | |
} | |
std::cout << std::endl; | |
std::cout << width << " " << colorMap.size() << std::endl; | |
return result; | |
} | |
unsigned char* imgpLoad(char *path) { | |
std::ifstream is(path, std::ifstream::in | std::ifstream::binary); | |
if (is) { | |
// get length of file: | |
is.seekg(0, is.end); | |
int length = is.tellg(); | |
is.seekg(0, is.beg); | |
std::cout << "Reading " << length << " characters... "; | |
// read data as a block: | |
is.read((char *)buffer, length); | |
if (is) | |
std::cout << "all characters read successfully."; | |
else | |
std::cout << "error: only " << is.gcount() << " could be read"; | |
is.close(); | |
} | |
return imgpParse(); | |
} | |
void saveImage(char* filepath, GLFWwindow* w) { | |
int size = 30; | |
int width, height; | |
glfwGetFramebufferSize(w, &width, &height); | |
GLsizei nrChannels = 4; | |
GLsizei bufferSize = nrChannels * size * size; | |
std::vector<char> buffer(bufferSize); | |
glPixelStorei(GL_PACK_ALIGNMENT, 1); | |
glReadBuffer(GL_BACK); | |
auto res = glGetError(); | |
std::cout << res << std::endl; | |
glReadPixels(0, 0, size, size, GL_RGBA, GL_UNSIGNED_BYTE, buffer.data()); | |
res = glGetError(); | |
std::cout << res << std::endl; | |
stbi_write_png(filepath, size, size, nrChannels, buffer.data(), size * nrChannels); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment