~sjb/ten-print-bitmap

1972f98683f579b8480929306534f52340ad40be — Simon Bates 1 year, 9 months ago main
init
4 files changed, 100 insertions(+), 0 deletions(-)

A .editorconfig
A 10print.c
A LICENSE.txt
A README.md
A  => .editorconfig +9 -0
@@ 1,9 @@
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

A  => 10print.c +58 -0
@@ 1,58 @@
#include <stdio.h>
#include <stdlib.h>

#define GRID_WIDTH 15
#define GRID_HEIGHT 5
#define TILE_SIZE_PX 100
#define DRAW_WIDTH 25
#define FG_R 16 * 15
#define FG_G 16 * 11
#define FG_B 16 * 12
#define BG_R 16 * 15
#define BG_G 16 * 14
#define BG_B 16 * 13

void print_line(int y, int which)
{
    for (int x = 0; x < TILE_SIZE_PX; ++x) {
        int dist = (which == 0 ? abs(x - y) : abs(TILE_SIZE_PX - 1 - x - y));
        if (dist <= DRAW_WIDTH) {
            printf("%d %d %d\n", FG_R, FG_G, FG_B);
        } else if (dist == DRAW_WIDTH + 1) {
            /* Anti-alias */
            printf("%d %d %d\n",
                (FG_R + BG_R) / 2,
                (FG_G + BG_G) / 2,
                (FG_B + BG_B) / 2);
        } else {
            printf("%d %d %d\n", BG_R, BG_G, BG_B);
        }
    }
}

int main(void) {
    int row_tiles[GRID_WIDTH];

    srand(22);

    /* Print the header */
    printf("P3\n");
    printf("%d %d\n", GRID_WIDTH * TILE_SIZE_PX, GRID_HEIGHT * TILE_SIZE_PX);
    printf("255\n");

    for (int grid_row = 0; grid_row < GRID_HEIGHT; ++grid_row) {
        /* Select the tiles for this row */
        for (int grid_col = 0; grid_col < GRID_WIDTH; ++grid_col) {
            row_tiles[grid_col] = rand() % 2;
        }

        /* Draw the tiles for this row */
        for (int y = 0; y < TILE_SIZE_PX; ++y) {
            for (int grid_col = 0; grid_col < GRID_WIDTH; ++grid_col) {
                print_line(y, row_tiles[grid_col]);
            }
        }
    }

    return EXIT_SUCCESS;
}

A  => LICENSE.txt +21 -0
@@ 1,21 @@
MIT License

Copyright (c) 2021 Simon Bates

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

A  => README.md +12 -0
@@ 1,12 @@
Generates an image in the style of the Commodore 64 program
[`10 PRINT CHR$(205.5+RND(1)); : GOTO 10`](https://10print.org/)

Usage:

* Edit the `#define` statements to set the colours and size
* Change the seed in the `srand()` call to make a different pattern

Then compile and run:

    cc 10print.c
    ./a.out > image.ppm