Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For 3D matrixes, I would usually convert them to VRML3D and look at them using ParallelGraphics/Cortona3D.</p> <p>Otherwise, you need some sort of projection or "slicing" of the matrix in order to see <em>all</em> of the matrix.</p> <p>This is a C implementation to dump a 3D matrix to a PNG file. Compile with</p> <pre><code>gcc -W -Wall -o bin2png bin2png.c -lpng </code></pre> <p>Code:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdint.h&gt; #include &lt;stdlib.h&gt; #include &lt;errno.h&gt; #include &lt;png.h&gt; static png_structp png_ptr; static png_infop info_ptr; /** |&lt;--- W ----&gt;| +------------+ - | 18 19 20| | +-------------+ | | | 9 10 11 | | | +-------------+ |23| +--&gt; H | 0 1 2 | | | | | |14 | | | | | |26| | | 3 4 5 | |--+ + - | |17 | / | |---+ +--&gt; D | 6 7 8 | / +-------------+ + @param matrix a 3D matrix. Element [i,j,k] is A[H*(D*k + j) + i] @param W width @param H height @param D depth @param WW width in W-sized chunks of target image @param HH height in H-sized chunks of target image @param filename output filename in PNG format Output image: |&lt;----- WW = 2 ---&gt;| +------------------+ - | 0 1 2 9 10 11| | | 3 4 5 12 13 14| | | 6 7 8 15 16 17| HH = 2 | 18 19 20 | | | 21 22 23 blank | | | 24 25 26 | | +------------------+ - NOTE: W*WW and H*HH may not exceed 32760. Return: 0 success -1 cannot create PNG structure (write) -2 cannot create PNG structure (info) -3 out of memory -4 cannot create output file */ int matrix3D_to_png(uint8_t *matrix, size_t W, size_t H, size_t D, size_t WW, size_t HH, char *filename) { FILE *fp; png_color palette[16]; png_byte transparencies[16]; uint32_t y; size_t x; uint8_t *row; if( !(png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)) ) return -1; if( !(info_ptr = png_create_info_struct(png_ptr)) || setjmp(png_jmpbuf(png_ptr)) ){ /* If we get here, libpng had a problem writing */ png_destroy_write_struct(&amp;png_ptr, &amp;info_ptr); return -2; } if (NULL == (row = malloc(WW*W + 7))) { return -3; } /* Create 16-color palette for representation */ #define SETPAL(i,r,g,b,a) \ palette[i].red = r; palette[i].green = g; palette[i].blue = b; transparencies[i] = 255-a; // We will draw the matrix in red if points are nonzero, black if zero; outside the matrix // we use transparent white. #define INDEX_IF_ZERO 0 #define INDEX_IF_NONZERO 3 #define INDEX_IF_BLANK 15 SETPAL(0, 0, 0, 0, 0); // Black SETPAL(1, 255, 255, 255, 0); // Opaque white SETPAL(2, 192, 192, 192, 0); // Light gray SETPAL(3, 255, 0, 0, 0); // Red SETPAL(4, 0, 255, 0, 0); // Green SETPAL(5, 0, 0, 255, 0);// Blue SETPAL(6, 255, 0, 0, 128); // Halftransparent red SETPAL(7, 0, 255, 0, 128); // green SETPAL(8, 0, 0, 255, 128); // blue SETPAL(9, 255, 0, 0, 0); // red again :-) SETPAL(10, 0, 255, 0, 0); SETPAL(11, 0, 0, 255, 0); SETPAL(12, 255, 0, 0, 0); SETPAL(13, 0, 255, 0, 0); SETPAL(14, 0, 0, 255, 0); SETPAL(15, 255, 255, 255, 255); // Transparent white /* End palette */ /* Create filename */ if (NULL == (fp = fopen(filename, "w"))) { fprintf(stderr, "cannot open output '%s': %s\n", filename, strerror(errno)); return -4; } png_init_io(png_ptr, fp); png_set_IHDR(png_ptr, info_ptr, W*WW, H*HH, 8, PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); png_set_PLTE(png_ptr, info_ptr, palette, 16); png_set_tRNS(png_ptr, info_ptr, transparencies, 16, NULL); png_set_compression_level(png_ptr, Z_BEST_COMPRESSION); png_write_info(png_ptr, info_ptr); for (y = 0; y &lt; H*HH; y++) { size_t mx = y/H; mx = (mx*H*WW + (y%H))*W; for (x = 0; x &lt; WW; x++) { if (mx+x*H &gt;= H*D) memset(row+x*W, INDEX_IF_BLANK, W); else { size_t ii; for (ii = 0; ii &lt; W; ii++) row[x*W+ii] = (matrix[mx+x*W*H+ii]) ? INDEX_IF_NONZERO : INDEX_IF_ZERO; } } png_write_row(png_ptr, row); } png_write_end(png_ptr, NULL /*info_ptr*/); png_destroy_write_struct(&amp;png_ptr, &amp;info_ptr); fclose(fp); free(row); return 0; } int main(int argc, char **argv) { FILE *fp; uint8_t *matrix; size_t W, H, D, WW, HH, i; if (8 != argc) { fprintf(stderr, "Syntax: %s input output.png width height depth TileX TileY\n", *argv); return EXIT_FAILURE; } W = atol(argv[3]); H = atol(argv[4]); D = atol(argv[5]); WW = atol(argv[6]); HH = atol(argv[7]); if ((W * WW &gt; 32767)||(H * HH) &gt; 32767) { fprintf(stderr, "Output image would be too large\n"); return EXIT_FAILURE; } if (WW*HH &lt; D) { fprintf(stderr, "WARNING: matrix does not fit into output image\n"); } if (WW*HH &gt; D*2) { fprintf(stderr, "WARNING: output image is far larger than input matrix\n"); } if (NULL == (fp = fopen(argv[1], "r"))) { fprintf(stderr, "Input file not found\n"); return EXIT_FAILURE; } if (NULL == (matrix = malloc(W*H*D))) { fprintf(stderr, "Out of memory: matrix too large\n"); return EXIT_FAILURE; } for (i = 0; i &lt; D; i++) { int ret; if ((int)H != (ret = fread(matrix + W*H*i, W, H, fp))) { fprintf(stderr, "Read error at plane %d (reading %d rows of %d elements, expecting %d, got %d)\n", (int)i, (int)W, (int)H, (int)H, ret); fclose(fp); return EXIT_FAILURE; } } if (matrix3D_to_png(matrix, W, H, D, WW, HH, argv[2])) { fprintf(stderr, "Error in creating output PNG '%s'\n", argv[2]); return EXIT_FAILURE; } return EXIT_SUCCESS; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload