blind-norm.c (1695B)
1 /* See LICENSE file for copyright and license details. */ 2 #ifndef TYPE 3 #include "common.h" 4 5 USAGE("[-axyz]") 6 7 static int skip_a = 0; 8 static int skip_x = 0; 9 static int skip_y = 0; 10 static int skip_z = 0; 11 12 #define FILE "blind-norm.c" 13 #include "define-functions.h" 14 15 int 16 main(int argc, char *argv[]) 17 { 18 struct stream stream; 19 void (*process)(struct stream *stream); 20 21 ARGBEGIN { 22 case 'a': 23 skip_a = 1; 24 break; 25 case 'x': 26 skip_x = 1; 27 break; 28 case 'y': 29 skip_y = 1; 30 break; 31 case 'z': 32 skip_z = 1; 33 break; 34 default: 35 usage(); 36 } ARGEND; 37 38 if (argc) 39 usage(); 40 41 eopen_stream(&stream, NULL); 42 43 SELECT_PROCESS_FUNCTION(&stream); 44 CHECK_ALPHA_CHAN(&stream); 45 CHECK_N_CHAN(&stream, 4, 4); 46 47 fprint_stream_head(stdout, &stream); 48 efflush(stdout, "<stdout>"); 49 process(&stream); 50 return 0; 51 } 52 53 #else 54 55 static void 56 PROCESS(struct stream *stream) 57 { 58 size_t i, n; 59 TYPE x, y, z, a, norm; 60 do { 61 n = stream->ptr / stream->pixel_size; 62 for (i = 0; i < n; i++) { 63 x = ((TYPE *)(stream->buf))[4 * i + 0]; 64 y = ((TYPE *)(stream->buf))[4 * i + 1]; 65 z = ((TYPE *)(stream->buf))[4 * i + 2]; 66 a = ((TYPE *)(stream->buf))[4 * i + 3]; 67 norm = sqrt(x * x + y * y + z * z + a * a); 68 if (!skip_x) 69 ((TYPE *)(stream->buf))[4 * i + 0] = norm; 70 if (!skip_y) 71 ((TYPE *)(stream->buf))[4 * i + 1] = norm; 72 if (!skip_z) 73 ((TYPE *)(stream->buf))[4 * i + 2] = norm; 74 if (!skip_a) 75 ((TYPE *)(stream->buf))[4 * i + 3] = norm; 76 } 77 n *= stream->pixel_size; 78 ewriteall(STDOUT_FILENO, stream->buf, n, "<stdout>"); 79 memmove(stream->buf, stream->buf + n, stream->ptr -= n); 80 } while (eread_stream(stream, SIZE_MAX)); 81 if (stream->ptr) 82 eprintf("%s: incomplete frame\n", stream->file); 83 } 84 85 #endif