Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "OptLab.h"
  4. #include <iostream>
  5. bool CompareResults(const BYTE* image1, const BYTE* image2, const int width, const int height)
  6. {
  7. for (int i = 0; i < width * height; ++i)
  8. if (abs((int)image1[i] - (int)image2[i]) > 1)
  9. return false;
  10. return true;
  11. }
  12.  
  13.  
  14. void Filter(const BYTE *inputImage, BYTE *outputImage, const int width, const int height)
  15. {
  16. int i, j, k, l;
  17. double sum;
  18.  
  19. for (j = 0; j < height; ++j)
  20. for (i = 0; i < width; ++i)
  21. outputImage[GetIndex(i, j, width)] = 0;
  22.  
  23. for (i = 0; i < width; ++i)
  24. for (j = 0; j < height; ++j)
  25. {
  26. if (i != 0 && j != 0 && i != width - 1 && j != height - 1)
  27. {
  28. sum = 0.0;
  29.  
  30. for (k = -1; k <= 1; ++k)
  31. for (l = -1; l <= 1; ++l)
  32. {
  33. if ((k == 0 && l != 0) || (l == 0 && k != 0))
  34. sum += 4.0 * inputImage[GetIndex(i + l, j + k, width)] / 28.0;
  35. else if (k == 0 && l == 0)
  36. sum += 8.0 * inputImage[GetIndex(i + l, j + k, width)] / 28.0;
  37. else
  38. sum += inputImage[GetIndex(i + l, j + k, width)] / 28.0;
  39. }
  40.  
  41. outputImage[GetIndex(i, j, width)] = (BYTE)sum;
  42. }
  43. }
  44. }
  45.  
  46.  
  47. void Filter_optimized(const BYTE *inputImage, BYTE *outputImage, const int width, const int height)
  48. {
  49. int i, j, k, l;
  50. double sum;
  51.  
  52. for (j = 0; j < height; ++j)
  53. outputImage[GetIndex(0, j, width)] = 0;
  54. for (i = 0; i < width; ++i) // rozbicie na 2 rozne petle, minimalny wplyw na czas wykonania ( < 0.1s)
  55. outputImage[GetIndex(i, 0, width)] = 0;
  56.  
  57. for (i = 1; i < width - 1; ++i)
  58. for (j = 1; j < height - 1; ++j)
  59. {
  60. // usuniecie warunkow 'if'
  61. sum = 0.0;
  62.  
  63. sum += inputImage[GetIndex(i - 1, j - 1, width)] / 28.0;
  64. sum += 4.0 / 28.0 * inputImage[GetIndex(i - 1, j, width)];
  65. sum += inputImage[GetIndex(i - 1, j + 1, width)] / 28.0;
  66. sum += 4.0 / 28.0 * inputImage[GetIndex(i, j - 1, width)];
  67. sum += 8.0 / 28.0 * inputImage[GetIndex(i, j, width)];
  68. sum += 4.0 / 28.0 * inputImage[GetIndex(i, j + 1, width)];
  69. sum += inputImage[GetIndex(i + 1, j - 1, width)] / 28.0;
  70. sum += 4.0 / 28.0 * inputImage[GetIndex(i + 1, j, width)]; // zmiania kolejnosci wykonywania dzialan, czas wykonania (-1s)
  71. sum += inputImage[GetIndex(i + 1, j + 1, width)] / 28.0; // zamiana 2 peteli for, czas wykonywania (-1s)
  72.  
  73. outputImage[GetIndex(i, j, width)] = (BYTE)sum;
  74. }
  75. }
  76.  
  77.  
  78.  
  79. inline int GetIndex(int x, int y, int w) // zmieniło czas wykowania o 1/2
  80. {
  81. return x + y * w;
  82. }
  83.  
  84. void PrintImage(const BYTE *image, const int N, const int M)
  85. {
  86. int i, j;
  87.  
  88. for (i = 0; i < N; ++i)
  89. {
  90. for (j = 0; j < M; ++j)
  91. printf("%d ", image[GetIndex(i, j, M)]);
  92. printf("\n");
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement