Skip to content

Instantly share code, notes, and snippets.

@initialneil
Last active October 17, 2018 06:57
Show Gist options
  • Save initialneil/a23b96f27ba8bea23d7ee304f985b68b to your computer and use it in GitHub Desktop.
Save initialneil/a23b96f27ba8bea23d7ee304f985b68b to your computer and use it in GitHub Desktop.
Break down iPhone photo with depth (jpg file with Multi-picture Format) https://wordpress.com/post/initialneil.wordpress.com/254
#include <fstream>
#include <vector>
#include <string>
using namespace std;
int main(int argc, char **argv)
{
string fn = argc > 1 ? argv[1] : "";
ifstream in(fn, ios::binary);
if (!in.is_open())
return -1;
ofstream fout;
long addr = 0;
int n_jpg = 0, n_out = 0;
std::vector<int> stack;
// c0 is the last, c is the current
unsigned char c0 = 0, c = 0;
// Skips potential padding between markers
while (!in.eof()) {
c = in.get();
//printf("%02X ", c);
// find jpg start
if (c0 == 0xff && c == 0xd8) {
for (int i = 0; i < stack.size(); ++i)
printf(" ");
printf("|-- [image %d] start at %ld\n", n_jpg, addr - 1);
// open output file
if (stack.empty()) {
fout.open(fn + "-" + to_string(n_out) + ".jpg", ios::binary | ios::out);
fout.write((char*)&c0, sizeof(c0));
n_out++;
}
// push to jpg stack
stack.push_back(n_jpg);
n_jpg++;
}
// write current uchar
fout.write((char*)&c, sizeof(c));
// check jpg finished
if (c0 == 0xff && c == 0xd9) {
for (int i = 0; i < stack.size() - 1; ++i)
printf(" ");
printf("|-- [image %d] end at %ld\n", stack.back(), addr);
stack.pop_back();
// close finished file
if (stack.empty())
fout.close();
}
c0 = c;
addr++;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment