draw_diff.cc 3.2 KB

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