Advertisement
cd62131

Raw Image (RGB) Multi Compositor

Dec 11th, 2013
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. void usage(char *prog_name) {
  5.   fprintf(
  6.     stderr,
  7.     "Usage: %s orig.raw compose1.raw compose2.raw ... out.raw", prog_name);
  8. }
  9. FILE **fopen_many(int n, char **name) {
  10.   FILE **p = (FILE **) malloc(n * sizeof(FILE *));
  11.   for (int i = 0; i < n; i++) {
  12.     printf("Open #%d: %s\n", i, name[i]);
  13.     p[i] = fopen(name[i], "rb");
  14.   }
  15.   return p;
  16. }
  17. unsigned char **read_many(FILE **in, int n) {
  18.   unsigned char **p = (unsigned char **) malloc(n * sizeof(unsigned char *));
  19.   for (int i = 0; i < n; i++) {
  20.     p[i] = (unsigned char *) malloc(1 * sizeof(unsigned char));
  21.     if (fscanf(in[i], "%c", p[i]) == EOF) return NULL;
  22.   }
  23.   return p;
  24. }
  25. void free_many(unsigned char **read, int n) {
  26.   for (int i = 0; i < n; i++)
  27.     free(read[i]);
  28.   free(read);
  29. }
  30. void fclose_many(FILE **in, int n) {
  31.   for (int i = 0; i < n; i++)
  32.     fclose(in[i]);
  33. }
  34. int main(int argc, char **argv) {
  35.   if (argc < 5) {
  36.     usage(argv[0]);
  37.     return EXIT_FAILURE;
  38.   }
  39.   FILE **in = fopen_many(argc - 2, argv + 1);
  40.   FILE *out = fopen(argv[argc - 1], "wb");
  41.   while (true) {
  42.     unsigned char **read = read_many(in, argc - 2);
  43.     if (read == NULL) break;
  44.     unsigned char ou;
  45.     double sum = 0;
  46.     for (int i = 0; i < argc - 2; i++)
  47.       sum += read[i][0];
  48.     ou = 255 * read[0][0] / sum;
  49.     fprintf(out, "%c", ou);
  50.     free_many(read, argc - 2);
  51.   }
  52.   fclose_many(in, argc - 2);
  53.   printf("Output: %s\n", argv[argc - 1]);
  54.   fclose(out);
  55.   return EXIT_SUCCESS;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement