2017-02-22 2 views
0

Wie konvertiere ich eine Datei mit bis zu 320k Bytes in ein Bild mit 1, 2 oder 3 Bytes Farbe?Willkürliche Bytes in ein beliebiges verlustfreies gemeinsames Bildformat konvertieren

Ich möchte zeigen, wie klein meine 80er Jahre Anwendung ist, indem ich 100% der 320k Daten als ein Bitmap-Bild auf meiner Projektkarte gedruckt.

I wissen die Daten werden nicht wie etwas anderes als Lärm, aber das ist OK. Ich möchte nur sagen können: "Das ist es - diese Pixel repräsentieren eindeutig jedes Byte in dieser Anwendung!"

Es wäre cool, wenn es ein „2D-Barcode“ war app, dass dies der Fall ist, aber nach einer Stunde des Suchens ich nicht finden konnte, die die Kapazität hatte und umgesetzt (viele Papiere.)

+0

Bild Kann Magick dazu gezwungen werden? –

Antwort

2

Kann imageMagick dazu gezwungen werden?

Absolut!

Beginnen wir damit, eine Programmdatei mit einer Größe von 320k zu erzeugen.

dd if=/dev/urandom of=myProgram.blob bs=1000 count=320 

YMMV

Grau Ansatz. Ein 8-Bit-Bild mit einem Kanal.

cat myProgram.blob | \ 
    convert -size 500x640 -depth 8 -format GRAY GRAY:- gray_output.png 

gray_output.png

Farb Ansatz. Dreikanaliges 8-Bit-Bild.

cat myProgram.blob | \ 
    convert -size 500x213 -depth 8 -format RGB RGB:- rgb_output.png 

rgb_output.png

Sie werden ein bisschen Mathe tun müssen, um die Bildgrößen zu bestimmen, aber das ist einfach.

total_bytes = [number of channels] * width * height 

Ich weiß, dass die Daten wie nichts, aber Lärm aussehen wird, aber das ist OK. Ich möchte nur sagen können:

"Das ist es - diese Pixel repräsentieren eindeutig jedes Byte in dieser Anwendung!"

Vor einem Jahr hatte ich das gleiche Problem. Ich brauchte eine schnelle visuelle Prüfsumme, um "auf einen Blick" festzustellen, ob sich die Daten-Blobs unterschieden.

Und so habe ich vizsum erstellt. Auf keinen Fall verlustfrei oder mathematisch sicher, aber schöner auf den Augen.

cat myProgram.blob | vizsum -sha1 -bilinear visual_checksum.png 

visual_checksum.png


aktualisieren nur zum Spaß.

Hier ist ein schnelles C-Programm mit dem MagickWand API

// program2img.c 
#include <stdio.h> 
#include <math.h> 
#include <wand/MagickWand.h> // Or <MagickWand/MagickWand.h> if using IM 7.x.x 

int main(int argc, const char * argv[]) { 

    FILE 
     * fd; 

    const char 
     * input, 
     * output; 

    unsigned char 
     * buffer; 

    long 
     program_size, 
     program_area, 
     program_width, 
     program_width_data, 
     program_height, 
     col, 
     row; 

    MagickBooleanType ok; 

    // Collect input/output 
    if (argc != 3) { 
     fprintf(stderr, "Usage: %s <input> <output>\n", argv[0]); 
     return 1; 
    } 
    input = argv[1]; 
    output = argv[2]; 

    // Open input for reading 
    fd = fopen(argv[1], "rb"); 
    if (fd == NULL) { 
     fprintf(stderr, "Unable to open `%s'\n", argv[1]); 
     return 1; 
    } 

    // Get size of input 
    fseek(fd, 0L, SEEK_END); 
    program_size = ftell(fd); 
    fseek(fd, 0L, SEEK_SET); 

    // Roughly calculate size of output image. 
    program_area = program_size/3; 
    program_width = sqrtl(program_area); 
    if (program_width < 1) { 
     fprintf(stderr, "Unable to generate image width\n"); 
     return 1; 
    } 
    program_width_data = program_width * 3; 
    program_height = program_area/program_width + 1; 
    if (program_height < 1) { 
     fprintf(stderr, "Unable to generate image width\n"); 
     return 1; 
    } 

    // Boot ImageMagick 
    MagickWandGenesis(); 

    PixelWand * color; 
    MagickWand * wand; 

    color = NewPixelWand(); 
    wand = NewMagickWand(); 

    PixelSetColor(color, "BLACK"); 
    MagickNewImage(wand, program_width, program_height, color); 
    color = DestroyPixelWand(color); 

    buffer = malloc(program_width_data); 

    // Iterate over input and build image line-by-line 
    for (row = 0; row < program_height; ++row) { 
     col = fread(buffer, 1, program_width_data, fd); 
     // Add padding (might need to double check this logic) 
     while (col < program_width_data) { 
      buffer[col++] = '\0'; 
     } 
     MagickImportImagePixels(wand, 
           0, row, 
           program_width, 1, 
           "RGB", CharPixel, buffer); 
    } 
    fclose(fd); 
    // Save image output. 
    ok = MagickWriteImage(wand, output); 
    if (ok == MagickFalse) { 
     fprintf(stderr, "Unable to save output `%s'\n", output); 
    } 
    wand = DestroyMagickWand(wand); 

    MagickWandTerminus(); 
    return 0; 
} 

den mit

clang `MagickWand-config --cflags --libs` program2img.c -o program2img 

kompiliert werden kann und

./program2img mySmallProgram output.png 

output.png

+0

Super! Vielen Dank. –

Verwandte Themen