Data2Image
This small C-program takes in a path to a file and creates a .ppm image of all the bytes in that file. I made it because I thought i would be cool to visuaize otherwise invisible data.
Examples
The first 5000 words of Lorem Ipsum:
"ELF header, import table, and section layout visualized as a compact byte-image."
The data2image program itself (compiled with gcc -o data2image.exe data2image.c -0fast):
data2image.exe
(gcc fast)
The data2image program itself (compiled with gcc -o data2image.exe data2image.c -0size):
data2image.exe
(gcc size)
Pokemon Red (US) ROM:
Pokemon Red
DirectX DLL:
Microsoft.DirectX.dll
Source Code
C
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
unsigned int SIZE;
void print_file(const char* buffer);
void create_image(const unsigned char* data, const char* name);
int main(int argc, const char** argv) {
FILE* fileptr;
char* buffer;
long filelen;
fileptr = fopen(argv[1], "rb");
if (fileptr == NULL) {
printf("File not found\n");
return 1;
}
(void)fseek(fileptr, 0, SEEK_END);
SIZE = ftell(fileptr);
rewind(fileptr);
buffer = (char*)malloc(SIZE * sizeof(char) + 1);
(void)fread(buffer, SIZE, 1, fileptr);
buffer[SIZE] = 0;
(void)fclose(fileptr);
create_image((unsigned char*)buffer, argv[1]);
free(buffer);
}
void create_image(const unsigned char* data, const char* name) {
const float d_size = sqrt((float)SIZE / 3);
const int dim = (int)ceil(d_size);
const int remainder = (dim * dim) * 3 - SIZE;
char str[20];
(void)sprintf(str, "P3 %i %i 255 ", dim, dim);
int header = strlen(str);
unsigned char* outbuf = (unsigned char*)malloc(remainder * 3 + header + SIZE * 4);
(void)printf("%s, Size:%i, dim: %i, remainder: %i, header: %i\n", str, SIZE, dim, remainder, header);
(void)memcpy(outbuf, str, header);
for (int i = 0; i < SIZE; i++) {
outbuf[header + i * 4] = '0' + (data[i] / 100) % 10;
outbuf[header + i * 4 + 1] = '0' + (data[i] / 10) % 10;
outbuf[header + i * 4 + 2] = '0' + data[i] % 10;
outbuf[header + i * 4 + 3] = ' ';
}
for (int i = 0; i < remainder; i++) {
(void)memcpy(outbuf + header + SIZE * 4 + i * 2, "0 ", 2);
}
char* result_name = (char*)malloc(strlen(name) + 4);
result_name[0] = '\0';
(void)strcat(result_name, name);
(void)strcat(result_name, ".ppm");
(void)printf("%s", result_name);
FILE* out;
out = fopen(result_name, "wb");
if (out == NULL) {
(void)printf("Error creating file\n");
return;
}
(void)fwrite(outbuf, sizeof(unsigned char), header + SIZE * 4 + remainder * 2, out);
(void)fclose(out);
free(outbuf);
free(result_name);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
unsigned int SIZE;
void print_file(const char* buffer);
void create_image(const unsigned char* data, const char* name);
int main(int argc, const char** argv) {
FILE* fileptr;
char* buffer;
long filelen;
fileptr = fopen(argv[1], "rb");
if (fileptr == NULL) {
printf("File not found\n");
return 1;
}
(void)fseek(fileptr, 0, SEEK_END);
SIZE = ftell(fileptr);
rewind(fileptr);
buffer = (char*)malloc(SIZE * sizeof(char) + 1);
(void)fread(buffer, SIZE, 1, fileptr);
buffer[SIZE] = 0;
(void)fclose(fileptr);
create_image((unsigned char*)buffer, argv[1]);
free(buffer);
}
void create_image(const unsigned char* data, const char* name) {
const float d_size = sqrt((float)SIZE / 3);
const int dim = (int)ceil(d_size);
const int remainder = (dim * dim) * 3 - SIZE;
char str[20];
(void)sprintf(str, "P3 %i %i 255 ", dim, dim);
int header = strlen(str);
unsigned char* outbuf = (unsigned char*)malloc(remainder * 3 + header + SIZE * 4);
(void)printf("%s, Size:%i, dim: %i, remainder: %i, header: %i\n", str, SIZE, dim, remainder, header);
(void)memcpy(outbuf, str, header);
for (int i = 0; i < SIZE; i++) {
outbuf[header + i * 4] = '0' + (data[i] / 100) % 10;
outbuf[header + i * 4 + 1] = '0' + (data[i] / 10) % 10;
outbuf[header + i * 4 + 2] = '0' + data[i] % 10;
outbuf[header + i * 4 + 3] = ' ';
}
for (int i = 0; i < remainder; i++) {
(void)memcpy(outbuf + header + SIZE * 4 + i * 2, "0 ", 2);
}
char* result_name = (char*)malloc(strlen(name) + 4);
result_name[0] = '\0';
(void)strcat(result_name, name);
(void)strcat(result_name, ".ppm");
(void)printf("%s", result_name);
FILE* out;
out = fopen(result_name, "wb");
if (out == NULL) {
(void)printf("Error creating file\n");
return;
}
(void)fwrite(outbuf, sizeof(unsigned char), header + SIZE * 4 + remainder * 2, out);
(void)fclose(out);
free(outbuf);
free(result_name);
}