Advertisement
cd62131

Raw Image Manipulation in C

Nov 8th, 2013
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. FILE **init_in(int ac, char **av) {
  6.   FILE **in = (FILE **) malloc(sizeof(FILE *) * (ac - 1 - 1));
  7.   for (int i = 0; i < ac - 1; i++) in[i] = fopen(av[i], "rb");
  8.   return in;
  9. }
  10.  
  11. unsigned char **init_reader(int ac) {
  12.   unsigned char **reader =
  13.     (unsigned char **) malloc(sizeof(unsigned char *) * (ac - 1 - 1));
  14.   for (int i = 0; i < ac - 1; i++)
  15.     reader[i] = (unsigned char *) malloc(sizeof(unsigned char));
  16.   return reader;
  17. }
  18.  
  19. int reader_read(int ac, FILE **in, unsigned char **reader) {
  20.   int read_count;
  21.   for (int i = 0; i < ac - 1; i++)
  22.     read_count = fread(reader[i], sizeof(unsigned char), 1, in[i]);
  23.   return read_count;
  24. }
  25.  
  26. void close_in(int ac, FILE **in) {
  27.   for (int i = 0; i < ac - 1; i++) fclose(in[i]);
  28. }
  29.  
  30. int main(int ac, char **av) {
  31.   if (ac < 5) return EXIT_FAILURE;
  32.   FILE **in = init_in(ac, av);
  33.   FILE *out = fopen(av[ac - 1], "wb");
  34.   unsigned char **r = init_reader(ac);
  35.   while (true) {
  36.     int status = reader_read(ac, in, r);
  37.     if (status < 1) break;
  38.     unsigned char *w = 0;
  39.     unsigned char sum = 0;
  40.     for (int i = 0; i < ac - 1; i++) sum += *r[i];
  41.     if (sum == 0) *w = 0;
  42.     else *w = (unsigned char) ((1UL << 8) - 1) * *r[0] / sum;
  43.     fwrite(w, sizeof(unsigned char), 1, out);
  44.   }
  45.   fclose(out);
  46.   close_in(ac, in);
  47.   return EXIT_SUCCESS;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement