Created
November 3, 2016 14:58
-
-
Save BSVino/76a3b98a1e814a09ac0a510d4a0ca75f to your computer and use it in GitHub Desktop.
Generates arbitrary-size bayer matrices using two algorithms. The first algorithm I wrote on stream here: https://www.youtube.com/watch?v=tV4M1cGaEJA The second algorithm is a transcription from this article on Bayer matrices: https://bartwronski.com/2016/10/30/dithering-part-three-real-world-2d-quantization-dithering/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| void bayer(int* matrix, int n, int recursion_level = 0, int start_number = 0, int x = 0, int y = 0) { | |
| int stride = (1<<recursion_level); | |
| int log_2_matrix_size = n; | |
| int increment = (1<<(2*(log_2_matrix_size-recursion_level-1))); | |
| if (increment == 1) { | |
| int matrix_size = (1<<n); | |
| matrix[(x + stride) + matrix_size*(y + stride)] = start_number+0; | |
| matrix[ x + matrix_size* y ] = start_number+1; | |
| matrix[(x + stride) + matrix_size* y ] = start_number+2; | |
| matrix[ x + matrix_size*(y + stride)] = start_number+3; | |
| } else { | |
| int new_recursion = recursion_level+1; | |
| bayer(matrix, n, new_recursion, start_number + increment*0, x + stride, y + stride); | |
| bayer(matrix, n, new_recursion, start_number + increment*1, x, y); | |
| bayer(matrix, n, new_recursion, start_number + increment*2, x + stride, y); | |
| bayer(matrix, n, new_recursion, start_number + increment*3, x, y + stride); | |
| } | |
| } | |
| int bayer2(int x, int y, int level) { | |
| // Mod[Mod[BitShiftRight[x, level], 2] + 1 + 2*Mod[BitShiftRight[y, level], 2], 4] + If[level == 0, 0, 4*Bayer[x, y, level – 1]] ; | |
| int number = (((x>>level)%2) + 1 + 2*((y>>level)%2))%4; | |
| if (level == 0) { | |
| return number; | |
| } else { | |
| return number + 4*bayer2(x, y, level - 1); | |
| } | |
| } | |
| int main(int argc, char** args) { | |
| if (argc < 2) { | |
| printf("Provide an n. Matrix size will be (2^n, 2^n).\n"); | |
| return 1; | |
| } | |
| int n = atoi(args[1]); | |
| int matrix_size = (1<<n); | |
| int* matrix = (int*)malloc(matrix_size*matrix_size * sizeof(int)); | |
| #if 1 | |
| bayer(matrix, n); | |
| #else | |
| for (int x = 0; x < matrix_size; x++) { | |
| for (int y = 0; y < matrix_size; y++) { | |
| matrix[x + matrix_size*y] = bayer2(x, y, n-1); | |
| } | |
| } | |
| #endif | |
| srand(0); | |
| // Prevent optimization from removing the algorithm | |
| printf("%d\n", matrix[rand()%(matrix_size*matrix_size)]); | |
| #if 0 | |
| for (int x = 0; x < matrix_size; x++) { | |
| for (int y = 0; y < matrix_size; y++) { | |
| printf("%d ", matrix[x + matrix_size*y]); | |
| } | |
| printf("\n"); | |
| } | |
| #endif | |
| free(matrix); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment