Created
May 19, 2013 02:51
-
-
Save eerock/5606491 to your computer and use it in GitHub Desktop.
C++ glitch-art helper code.Compile this glitcher.exe, then drag a jpg or gif onto it. A "filename.glitched.ext" will be created alongside the original and will have several random bytes randomized.
This file contains 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
// C++ glitch-art creation tool | |
// written by eerock@github.com | |
#include <cstdio> | |
#include <cstdlib> | |
#include <ctime> | |
#include <iostream> | |
#include <fstream> | |
#include <string> | |
static const int num_glitched_bytes = 8; | |
int main(int argc, char* argv[]) { | |
if (argc < 2) { | |
std::cout << "Usage:" << std::endl; | |
std::cout << "\tglitcher <filename>" << std::endl; | |
return -1; | |
} | |
srand(static_cast<unsigned int>(time(0))); | |
std::string filename(argv[1]); | |
std::ifstream filein; | |
filein.open(filename.c_str(), std::ios::in | std::ios::ate | std::ios::binary); | |
//std::cout << "File: " << filename << std::endl; | |
std::ofstream fileout; | |
size_t extidx = filename.rfind("."); | |
if (extidx != std::string::npos) { | |
filename.insert(extidx, ".glitched"); | |
} | |
fileout.open(filename.c_str(), std::ios::out | std::ios::trunc | std::ios::binary); | |
//std::cout << "Out : " << filename << std::endl; | |
if (filein.is_open() && fileout.is_open()) { | |
// operate on the binary data... | |
// first find the size of the file... | |
size_t sz = filein.tellg(); | |
filein.seekg(0, std::ios::beg); | |
std::cout << "Size: " << sz << std::endl; | |
// read the file into a buffer... | |
char* buffer = new char[sz]; | |
if (buffer) { | |
filein.read(buffer, sz); | |
} | |
// glitch random bytes to random values... | |
for (int i = 0; i < num_glitched_bytes; ++i) { | |
size_t glitch_pos = rand() % sz; | |
buffer[glitch_pos] = rand() % 256; | |
} | |
// write the result to fileout... | |
fileout.write(buffer, sz); | |
delete [] buffer; | |
} | |
filein.close(); | |
fileout.close(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment