Skip to content

Instantly share code, notes, and snippets.

@Floessie
Created October 25, 2016 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Floessie/3ba7829dd53672f7f10d6e6fc2347a88 to your computer and use it in GitHub Desktop.
Save Floessie/3ba7829dd53672f7f10d6e6fc2347a88 to your computer and use it in GitHub Desktop.
// Simple rtti2pnm converter
// Assumes little endian rtti and binary i/o from stdin/stdout (Posix)
#include <iostream>
#include <string>
int main(int argc, char** argv)
{
enum State {
TYPE,
WIDTH,
HEIGHT,
IMAGE8R,
IMAGE8G,
IMAGE8B,
IMAGE16R,
IMAGE16G,
IMAGE16B,
IMAGEFLOATR,
IMAGEFLOATG,
IMAGEFLOATB,
SUCCESS,
FAILURE
} state = TYPE;
unsigned int counter = 0;
unsigned char c = 0;
std::string type;
unsigned int width = 0;
unsigned int height = 0;
std::basic_string<unsigned char> r;
std::basic_string<unsigned char> g;
std::basic_string<unsigned char> b;
union {
unsigned int raw;
float cooked;
} f;
while (
std::cin.get(reinterpret_cast<char&>(c))
&& state != SUCCESS
&& state != FAILURE
) {
switch (state) {
case TYPE: {
switch (c) {
case '\n': {
state = WIDTH;
break;
}
default: {
type += c;
break;
}
}
break;
}
case WIDTH: {
width |= c << (8 * counter);
++counter;
if (counter == 4) {
counter = 0;
state = HEIGHT;
}
break;
}
case HEIGHT: {
height |= c << (8 * counter);
++counter;
if (counter == 4) {
r.reserve(width * height);
g.reserve(width * height);
b.reserve(width * height);
counter = 0;
if (type == "Image8") {
state = IMAGE8R;
}
else if (type == "Image16") {
state = IMAGE16R;
}
else if (type == "Imagefloat") {
state = IMAGEFLOATR;
f.raw = 0;
}
else {
state = FAILURE;
}
}
break;
}
case IMAGE8R: {
r.push_back(c);
state = IMAGE8G;
break;
}
case IMAGE8G: {
g.push_back(c);
state = IMAGE8B;
break;
}
case IMAGE8B: {
b.push_back(c);
++counter;
if (counter == width * height) {
state = SUCCESS;
} else {
state = IMAGE8R;
}
break;
}
case IMAGE16R: {
if (counter % 2) {
r.push_back(c);
}
++counter;
if (counter == width * height * 2) {
counter = 0;
state = IMAGE16G;
}
break;
}
case IMAGE16G: {
if (counter % 2) {
g.push_back(c);
}
++counter;
if (counter == width * height * 2) {
counter = 0;
state = IMAGE16B;
}
break;
}
case IMAGE16B: {
if (counter % 2) {
b.push_back(c);
}
++counter;
if (counter == width * height * 2) {
state = SUCCESS;
}
break;
}
case IMAGEFLOATR: {
switch (counter % 4) {
default: {
f.raw |= c << (8 * (counter % 4));
break;
}
case 3: {
f.raw |= c << 24;
r.push_back(f.cooked / 256.f);
f.raw = 0;
break;
}
}
++counter;
if (counter == width * height * 4) {
counter = 0;
state = IMAGEFLOATG;
}
break;
}
case IMAGEFLOATG: {
switch (counter % 4) {
default: {
f.raw |= c << (8 * (counter % 4));
break;
}
case 3: {
f.raw |= c << 24;
g.push_back(f.cooked / 256.f);
f.raw = 0;
break;
}
}
++counter;
if (counter == width * height * 4) {
counter = 0;
state = IMAGEFLOATB;
}
break;
}
case IMAGEFLOATB: {
switch (counter % 4) {
default: {
f.raw |= c << (8 * (counter % 4));
break;
}
case 3: {
f.raw |= c << 24;
b.push_back(f.cooked / 256.f);
f.raw = 0;
break;
}
}
++counter;
if (counter == width * height * 4) {
state = SUCCESS;
}
break;
}
case SUCCESS:
case FAILURE: {
break;
}
}
}
if (state == SUCCESS) {
std::cout << "P6\n";
std::cout << "# Created by rtti2pnm\n";
std::cout << width << " " << height << "\n";
std::cout << "255\n";
for (unsigned int i = 0; i < width * height; ++i) {
std::cout.put(r[i]);
std::cout.put(g[i]);
std::cout.put(b[i]);
}
} else {
std::cerr << "Could not convert input RTTI." << std::endl;
return 1;
}
return 0;
}
@mizupsi
Copy link

mizupsi commented Sep 3, 2017

I hate to ask, but I haven't been able to figure out how to use this. I've got it compiled, but my knowledge of cpp is so old and foggy (and frankly rudimentary) that absent any prompts, I can't get it to do right.

@JimmyJoeJohn1234567
Copy link

I know this is a long shot since this was posted so long ago, but I'm with the last guy who commented, I've got it compiled but I'm lost from there, any chance you can help a guy out with some step-by-step instructions? I lost a ton of pics awhile back and I've been holding on to the thumbnail cache files for years hoping I'd eventually find a way to use them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment