blind-matrix-rotate.c (2093B)
1 /* See LICENSE file for copyright and license details. */ 2 #ifndef TYPE 3 #include "common.h" 4 5 USAGE("[-cd]") 6 7 static int per_channel = 0; 8 static int in_degrees = 0; 9 10 #define FILE "blind-matrix-rotate.c" 11 #include "define-functions.h" 12 13 int 14 main(int argc, char *argv[]) 15 { 16 struct stream stream; 17 void (*process)(struct stream *stream); 18 19 ARGBEGIN { 20 case 'c': 21 per_channel = 1; 22 break; 23 case 'd': 24 in_degrees = 1; 25 break; 26 default: 27 usage(); 28 } ARGEND; 29 30 if (argc) 31 usage(); 32 33 eopen_stream(&stream, NULL); 34 35 SELECT_PROCESS_FUNCTION(&stream); 36 CHECK_CHANS(&stream, == 3, == 1); 37 38 if (stream.width != 1 || stream.height != 1) 39 eprintf("<stdin>: each frame must contain exactly 1 pixels\n"); 40 41 stream.width = 3; 42 stream.height = 3; 43 fprint_stream_head(stdout, &stream); 44 efflush(stdout, "<stdout>"); 45 46 process(&stream); 47 return 0; 48 } 49 50 #else 51 52 static void 53 PROCESS(struct stream *stream) 54 { 55 typedef TYPE pixel_t[4]; 56 pixel_t matrix[9]; 57 pixel_t buf; 58 size_t i; 59 60 for (i = 0; i < stream->n_chan; i++) { 61 matrix[0][i] = 1, matrix[1][i] = 0, matrix[2][i] = 0; 62 matrix[3][i] = 0, matrix[4][i] = 1, matrix[5][i] = 0; 63 matrix[6][i] = 0, matrix[7][i] = 0, matrix[8][i] = 1; 64 } 65 66 while (eread_frame(stream, buf)) { 67 if (per_channel && in_degrees) { 68 for (i = 0; i < stream->n_chan; i++) { 69 matrix[4][i] = matrix[0][i] = degcos(buf[i]); 70 matrix[3][i] = -(matrix[1][i] = degsin(buf[i])); 71 } 72 } else if (per_channel) { 73 for (i = 0; i < stream->n_chan; i++) { 74 matrix[4][i] = matrix[0][i] = cos(buf[i]); 75 matrix[3][i] = -(matrix[1][i] = sin(buf[i])); 76 } 77 } else { 78 buf[1] *= buf[3]; 79 if (in_degrees) { 80 matrix[4][0] = matrix[0][0] = degcos(buf[1]); 81 matrix[3][0] = -(matrix[1][0] = degsin(buf[1])); 82 } else { 83 matrix[4][0] = matrix[0][0] = cos(buf[1]); 84 matrix[3][0] = -(matrix[1][0] = sin(buf[1])); 85 } 86 for (i = 0; i < stream->n_chan; i++) { 87 matrix[0][i] = matrix[0][0]; 88 matrix[1][i] = matrix[1][0]; 89 matrix[3][i] = matrix[3][0]; 90 matrix[4][i] = matrix[4][0]; 91 } 92 } 93 ewriteall(STDOUT_FILENO, matrix, sizeof(matrix), "<stdout>"); 94 } 95 } 96 97 #endif