2017-02-22 21 views
0

如何將具有最多320k字節的文件轉換爲帶有1,2或3字節(任何可以)的圖像的圖像?將任意字節轉換爲任何無損的常見圖像格式

我想通過將我的項目卡上打印的位圖圖像的全部320k數據表示爲我的1980年代應用程序的多小。

知道數據看起來不像噪音,但沒關係。我只想說:「就是這樣 - 這些像素唯一地表示這個應用程序中的每個字節!」

這會是很酷的,如果有一個「二維條碼」的應用程序,這樣做,但搜索我無法找到一個有能力的一個小時後開始實施(大量的論文。)

+0

可對圖像Magick被強迫這樣做? –

回答

2

imageMagick可以被強制執行嗎?

絕對!

讓我們開始生成一個大小爲320k的程序文件。

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

因人而異

灰色的方法。單通道8位圖像。

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

gray_output.png

顏色的方法。三通道8位圖像。

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

rgb_output.png

你需要做一些數學來確定圖像的大小,但是這很容易。

total_bytes = [number of channels] * width * height 

我知道數據不會像什麼,但噪音,但沒關係。我只想說:

「就是這樣 - 這些像素唯一地表示這個應用程序中的每個字節!」

大約一年前我有同樣的問題。我需要一個快速的視覺校驗和來「一目瞭然」確定數據斑點是否有所不同。

所以,我創建了vizsum。絕不是無損的,或數學上安全的,但更漂亮的眼睛。

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

visual_checksum.png


更新只是爲了好玩。

下面是使用快速C程序的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; 
} 

可與

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

進行編譯和使用

./program2img mySmallProgram output.png 

output.png

+0

太棒了!謝謝。 –

相關問題