Skip to content

Instantly share code, notes, and snippets.

@gszauer
Created June 6, 2013 00:45
Show Gist options
  • Save gszauer/5718519 to your computer and use it in GitHub Desktop.
Save gszauer/5718519 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include "HexChar.h"
#include "LodePng.h"
/* HexChar
std::ostream& operator<<(std::ostream& o, const HexCharStruct& hs) {
return (o << std::hex << (int)hs.c); }
HexCharStruct hex(unsigned char _c) {
return HexCharStruct(_c); }
*/
void ApplyPostProcess(unsigned int width, unsigned int height, std::vector<unsigned char>& image);
int main(int argc, const char * argv[]) {
if (argc < 2) {
std::cout << "Usage: DDPng <string: filename> <bool: post process>\n";
return 0;
}
std::vector<unsigned char> png, image;
unsigned int width, height;
lodepng::State state;
lodepng::load_file(png, argv[1]);
unsigned int error = lodepng::decode(image, width, height, state, png);
if (error) {
std::cout << "Error decoding png image\n";
return 0;
} // Image now contains RGBA image
bool postProcessImage = false;
if (argc >= 3) {
std::string postProcessArg = argv[2];
std::transform(postProcessArg.begin(), postProcessArg.end(), postProcessArg.begin(), ::tolower);
if (postProcessArg == "yes") postProcessImage = true;
if (postProcessArg == "y") postProcessImage = true;
if (postProcessArg == "1") postProcessImage = true;
if (postProcessArg == "true") postProcessImage = true;
}
if (postProcessImage)
ApplyPostProcess(width, height, image);
std::string outFile = argv[1];
size_t periodPosition = outFile.rfind('.');
if (periodPosition != std::string::npos) {
outFile = outFile.erase(periodPosition);
outFile += ".out";
}
std::ofstream outputFile;
outputFile.open(outFile);
if (!outputFile.is_open()) {
std::cout << "Error, could not open output file\n";
return 0;
}
outputFile << "unsigned int imageWidth = " << width << ";\n";
outputFile << "unsigned int imageHeight = " << height << ";\n";
outputFile << "unsigned char imageData[] = { ";
bool firstChar = true;
for (unsigned int i = 0, size = (unsigned int)(image.size()); i < size; ++i) {
if (!firstChar) outputFile << ", ";
firstChar = false;
outputFile << "0x" << hex(image[i]);
}
outputFile << " };\n";
outputFile.close();
return 0;
}
void ApplyPostProcess(unsigned int width, unsigned int height, std::vector<unsigned char>& image) {
for (unsigned int y = 0; y < height; ++y) {
for (unsigned int x = 0; x < width; ++x) {
unsigned int pixel = 4 * y * width + 4 * x;
// +0:R, +1:G, +2:B, +3:A
if (image[pixel + 0] == 0 && image[pixel + 1] == 0 && image[pixel + 2 == 0]) // Pixel is black
image[pixel + 0] = image[pixel + 1] = image[pixel + 2] = 255; // Make pixel white
if (image[pixel + 0] == 255 && image[pixel + 1] == 192 && image[pixel + 2] == 203) // Pixel is pink
image[pixel + 3] = 0; // No alpha, transparent pixel
else image[pixel + 3] = 255;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment