Home | History | Annotate | Download | only in research
      1 /* Copyright 2016 Google Inc. All Rights Reserved.
      2    Author: zip753 (at) gmail.com (Ivan Nikulin)
      3 
      4    Distributed under MIT license.
      5    See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
      6 */
      7 
      8 /* Tool for drawing diff PPM images between two input PGM images. Normally used
      9    with backward reference histogram drawing tool. */
     10 
     11 #include <algorithm>
     12 #include <cassert>
     13 #include <cmath>
     14 #include <cstdint>
     15 #include <cstdio>
     16 #include <cstdlib>  /* exit, EXIT_FAILURE */
     17 #include <vector>
     18 
     19 #ifndef CHECK
     20 #define CHECK(X) if (!(X)) exit(EXIT_FAILURE);
     21 #endif
     22 
     23 void ReadPGM(FILE* f, uint8_t*** image, size_t* height, size_t* width) {
     24   int colors;
     25   CHECK(fscanf(f, "P5\n%lu %lu\n%d\n", width, height, &colors) == 3);
     26   assert(colors == 255);
     27   *image = new uint8_t*[*height];
     28   for (int i = *height - 1; i >= 0; --i) {
     29     (*image)[i] = new uint8_t[*width];
     30     CHECK(fread((*image)[i], 1, *width, f) == *width);
     31   }
     32 }
     33 
     34 void CalculateDiff(int** diff, uint8_t** image1, uint8_t** image2,
     35                    size_t height, size_t width) {
     36   for (size_t i = 0; i < height; ++i) {
     37     for (size_t j = 0; j < width; ++j) {
     38       diff[i][j] = static_cast<int>(image1[i][j]) - image2[i][j];
     39     }
     40   }
     41 }
     42 
     43 void DrawDiff(int** diff, uint8_t** image1, uint8_t** image2,
     44               size_t height, size_t width, FILE* f) {
     45   int max = -1234;
     46   int min = +1234;
     47   for (size_t i = 0; i < height; ++i) {
     48     for (size_t j = 0; j < width; ++j) {
     49       if (max < diff[i][j]) max = diff[i][j];
     50       if (min > diff[i][j]) min = diff[i][j];
     51       int img_min = std::min(255 - image1[i][j], 255 - image2[i][j]);
     52       if (max < img_min) max = img_min;
     53     }
     54   }
     55 
     56   int abs_max = -min;
     57   if (abs_max < max) abs_max = max;
     58 
     59   fprintf(f, "P6\n%lu %lu\n%d\n", width, height, abs_max);
     60 
     61   uint8_t* row = new uint8_t[3 * width];
     62   for (int i = height - 1; i >= 0; --i) {
     63     for (int j = 0; j < width; ++j) {
     64       int min_val = std::min(255 - image1[i][j], 255 - image2[i][j]);
     65       int max_val = std::max(min_val, abs(diff[i][j]));
     66       if (diff[i][j] > 0) { /* red */
     67         row[3 * j + 0] = abs_max - max_val + diff[i][j];
     68         row[3 * j + 1] = abs_max - max_val;
     69         row[3 * j + 2] = abs_max - max_val + min_val;
     70       } else { /* green */
     71         row[3 * j + 0] = abs_max - max_val;
     72         row[3 * j + 1] = abs_max - max_val - diff[i][j];
     73         row[3 * j + 2] = abs_max - max_val + min_val;
     74       }
     75     }
     76     fwrite(row, 1, 3 * width, f);
     77   }
     78   delete[] row;
     79 }
     80 
     81 int main(int argc, char* argv[]) {
     82   if (argc != 4) {
     83     printf("usage: %s pgm1 pgm2 diff_ppm_path\n", argv[0]);
     84     return 1;
     85   }
     86 
     87   uint8_t **image1, **image2;
     88   size_t h1, w1, h2, w2;
     89 
     90   FILE* fimage1 = fopen(argv[1], "rb");
     91   ReadPGM(fimage1, &image1, &h1, &w1);
     92   fclose(fimage1);
     93 
     94   FILE* fimage2 = fopen(argv[2], "rb");
     95   ReadPGM(fimage2, &image2, &h2, &w2);
     96   fclose(fimage2);
     97 
     98   if (!(h1 == h2 && w1 == w2)) {
     99     printf("Images must have the same size.\n");
    100     return 1;
    101   }
    102 
    103   int** diff = new int*[h1];
    104   for (size_t i = 0; i < h1; ++i) diff[i] = new int[w1];
    105   CalculateDiff(diff, image1, image2, h1, w1);
    106 
    107   FILE* fdiff = fopen(argv[3], "wb");
    108   DrawDiff(diff, image1, image2, h1, w1, fdiff);
    109   fclose(fdiff);
    110 
    111   return 0;
    112 }
    113