#include <iostream>
extern "C" {
#define STB_IMAGE_IMPLEMENTATION
#define STBI_FAILURE_USERMSG
#define STB
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
}
#define print(x) std::cout << (x) << std::endl
std::string imageToASCII(const char filename[], int charHeight = 30, int charWidth = 60) {
const char brightnessChars[] = "@$%#?{!<+=,-. ";
int width, height, channel;
uint8_t *data = stbi_load(filename, &width, &height, &channel, 0);
if (stbi_failure_reason()) {
throw std::invalid_argument(stbi_failure_reason());
// My code breaks here ^^^^^^^^^^^^^^^^^^^^^^^^^
}
// Begin with calculating the image and char ratio so we can fit the chars appropriately
float imageRatio = static_cast<float>(width) / height;
float charRatio = static_cast<float>(charWidth) / charHeight;
// Then scale the ascii grid accordingly
if (imageRatio < charRatio) {
charWidth = round(charHeight * imageRatio);
} else if (imageRatio > charRatio) {
charHeight = round(charWidth / imageRatio);
}
print(stbi_write_jpg("bwdemo.jpg", height/channel, width/channel, 1, grayscaleImage(data, height, width, channel), 0));
stbi_image_free(data);
}
int main()
{
std::string filename = "demo.jpeg";
imageToASCII(filename.c_str());
}
#stb_image is giving me a "not a PNG" error even though I'm trying to read .jpeg
6 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
Instead of checking if stbi_failure_reason() returns a non-null ptr, check the data variable
That was way easier than I thought. Thanks so much
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity
No problem. The way stbi image does error handling is a bit wonky