Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Compile with: gcc -std=c11 -o gif gif.c -lgd */
- #include <stdio.h>
- #include <string.h>
- #include <stdbool.h>
- #include <err.h>
- #include <gd.h>
- const char * const morse_alpha[] = {
- ".-", // A
- "-...", // B
- "-.-.", // C
- "-..", // D
- ".", // E
- "..-.", // F
- "--.", // G
- "....", // H
- "..", // I
- ".---", // J
- "-.-", // K
- ".-..", // L
- "--", // M
- "-.", // N
- "---", // O
- ".--.", // P
- "--.-", // Q
- ".-.", // R
- "...", // S
- "-", // T
- "..-", // U
- "...-", // V
- ".--", // W
- "-..-", // X
- "-.--", // Y
- "--..", // Z
- " " // space
- };
- const char * const morse_digit[] = {
- "-----", // 0
- ".----", // 1
- "..---", // 2
- "...--", // 3
- "....-", // 4
- ".....", // 5
- "-....", // 6
- "--...", // 7
- "---..", // 8
- "----.", // 9
- };
- int morse_encode(int *data, int data_len, const char *string)
- {
- int data_idx = 0;
- void append_data(int value)
- {
- if(data_idx < data_len)
- data[data_idx++] = value;
- }
- char c;
- while(c = *string++) {
- int idx;
- char **table = NULL;
- if(c >= 'A' && c <= 'Z') {
- table = morse_alpha;
- idx = c - 'A';
- } else if(c >= 'a' && c <= 'z') {
- table = morse_alpha;
- idx = c - 'a';
- } else if(c >= '0' && c <= '9') {
- table = morse_digit;
- idx = c - '0';
- } else if(c == ' ') {
- table = morse_alpha;
- idx = 26;
- } else {
- abort();
- }
- if(table == NULL)
- continue;
- const char *morse = table[idx];
- printf("%c -> %s\n", c, table[idx]);
- for(int i = 0; i < strlen(morse); i++) {
- char m = morse[i];
- switch(m) {
- case '.':
- append_data(1);
- break;
- case '-':
- append_data(1);
- append_data(1);
- append_data(1);
- break;
- case ' ':
- append_data(0);
- break;
- }
- append_data(0);
- }
- // space between letters is three units
- append_data(0);
- append_data(0);
- }
- return data_idx;
- }
- int main(int argc, char **argv)
- {
- gdImagePtr im[2];
- FILE *out;
- int num_frames = 3;
- for(int i = 0; i < num_frames; i++) {
- gdImagePtr img;
- img = gdImageCreate(8, 8);
- int fg_val = 0x7f;
- int bg_val = 0x80;
- #if 0
- fg_val = 0xff;
- bg_val = 0x00;
- #endif
- #if 0
- int palette[256];
- for(int c = 0; c < 256; c++) {
- palette[c] = gdImageColorAllocate(img, c, c, c);
- }
- #endif
- int fg = gdImageColorAllocate(img, fg_val, fg_val, fg_val);
- int bg = gdImageColorAllocate(img, bg_val, bg_val, bg_val);
- for(int y = 0; y < gdImageSY(img); y++) {
- for(int x = 0; x < gdImageSX(img); x++) {
- bool tx_on = false;
- if(i >= 1) {
- if(1) { // y < (gdImageSY(img) / 2) ) {
- if((x >> 0) & 1)
- tx_on = !tx_on;
- if((y >> 0) & 1)
- tx_on = !tx_on;
- } else {
- if((x >> 1) & 1)
- tx_on = !tx_on;
- if((y >> 1) & 1)
- tx_on = !tx_on;
- }
- } else {
- if((x >> 1) & 1)
- tx_on = !tx_on;
- if((y >> 1) & 1)
- tx_on = !tx_on;
- tx_on = false;
- }
- if(i == 2) {
- if(y > (gdImageSY(img) / 2))
- tx_on = false;
- }
- gdImageSetPixel(img, x, y, tx_on ? fg : bg);
- }
- }
- im[i] = img;
- }
- out = fopen("out.gif", "w");
- if(out == NULL)
- err(1, "unable to open output file");
- int morse_array[16384];
- int len;
- len = morse_encode(morse_array, 16384, "HIDDEN MORSE TX IN WEB PAGE WTF ");
- for(int i = 0; i < len; i++) {
- printf("%d", morse_array[i]);
- }
- printf("\n");
- gdImageGifAnimBegin(im[2], out, 1, 0);
- #if 0
- gdImageGifAnimAdd(im[2], out, 0, 0, 0, 0,
- gdDisposalRestorePrevious, NULL);
- #endif
- gdImagePtr prev = im[0];
- srand(1);
- int prev_data = morse_array[0];
- int im_prev_data = -1;
- int pos = 1;
- int num_periods = 0;
- while(pos <= len) {
- int data;
- if(pos < len)
- data = morse_array[pos];
- else
- data = -1;
- num_periods++;
- if(data != prev_data) {
- gdImageGifAnimAdd(im[prev_data], out, 0, 0, 0, num_periods * 10,
- gdDisposalNone, NULL);
- num_periods = 0;
- }
- prev_data = data;
- pos++;
- }
- #if 0
- for(int i = 0; i < len; i++) {
- int idx = morse_array[i] & 1;
- gdImageGifAnimAdd(im[idx], out, 0, 0, 0, 10, gdDisposalRestorePrevious, NULL);
- }
- #endif
- gdImageGifAnimEnd(out);
- fclose(out);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment