Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- void usage(char *prog_name) {
- fprintf(
- stderr,
- "Usage: %s orig.raw compose1.raw compose2.raw ... out.raw", prog_name);
- }
- FILE **fopen_many(int n, char **name) {
- FILE **p = (FILE **) malloc(n * sizeof(FILE *));
- for (int i = 0; i < n; i++) {
- printf("Open #%d: %s\n", i, name[i]);
- p[i] = fopen(name[i], "rb");
- }
- return p;
- }
- unsigned char **read_many(FILE **in, int n) {
- unsigned char **p = (unsigned char **) malloc(n * sizeof(unsigned char *));
- for (int i = 0; i < n; i++) {
- p[i] = (unsigned char *) malloc(1 * sizeof(unsigned char));
- if (fscanf(in[i], "%c", p[i]) == EOF) return NULL;
- }
- return p;
- }
- void free_many(unsigned char **read, int n) {
- for (int i = 0; i < n; i++)
- free(read[i]);
- free(read);
- }
- void fclose_many(FILE **in, int n) {
- for (int i = 0; i < n; i++)
- fclose(in[i]);
- }
- int main(int argc, char **argv) {
- if (argc < 5) {
- usage(argv[0]);
- return EXIT_FAILURE;
- }
- FILE **in = fopen_many(argc - 2, argv + 1);
- FILE *out = fopen(argv[argc - 1], "wb");
- while (true) {
- unsigned char **read = read_many(in, argc - 2);
- if (read == NULL) break;
- unsigned char ou;
- double sum = 0;
- for (int i = 0; i < argc - 2; i++)
- sum += read[i][0];
- ou = 255 * read[0][0] / sum;
- fprintf(out, "%c", ou);
- free_many(read, argc - 2);
- }
- fclose_many(in, argc - 2);
- printf("Output: %s\n", argv[argc - 1]);
- fclose(out);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement