Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include "OptLab.h"
- #include <iostream>
- bool CompareResults(const BYTE* image1, const BYTE* image2, const int width, const int height)
- {
- for (int i = 0; i < width * height; ++i)
- if (abs((int)image1[i] - (int)image2[i]) > 1)
- return false;
- return true;
- }
- void Filter(const BYTE *inputImage, BYTE *outputImage, const int width, const int height)
- {
- int i, j, k, l;
- double sum;
- for (j = 0; j < height; ++j)
- for (i = 0; i < width; ++i)
- outputImage[GetIndex(i, j, width)] = 0;
- for (i = 0; i < width; ++i)
- for (j = 0; j < height; ++j)
- {
- if (i != 0 && j != 0 && i != width - 1 && j != height - 1)
- {
- sum = 0.0;
- for (k = -1; k <= 1; ++k)
- for (l = -1; l <= 1; ++l)
- {
- if ((k == 0 && l != 0) || (l == 0 && k != 0))
- sum += 4.0 * inputImage[GetIndex(i + l, j + k, width)] / 28.0;
- else if (k == 0 && l == 0)
- sum += 8.0 * inputImage[GetIndex(i + l, j + k, width)] / 28.0;
- else
- sum += inputImage[GetIndex(i + l, j + k, width)] / 28.0;
- }
- outputImage[GetIndex(i, j, width)] = (BYTE)sum;
- }
- }
- }
- void Filter_optimized(const BYTE *inputImage, BYTE *outputImage, const int width, const int height)
- {
- int i, j, k, l;
- double sum;
- for (j = 0; j < height; ++j)
- outputImage[GetIndex(0, j, width)] = 0;
- for (i = 0; i < width; ++i) // rozbicie na 2 rozne petle, minimalny wplyw na czas wykonania ( < 0.1s)
- outputImage[GetIndex(i, 0, width)] = 0;
- for (i = 1; i < width - 1; ++i)
- for (j = 1; j < height - 1; ++j)
- {
- // usuniecie warunkow 'if'
- sum = 0.0;
- sum += inputImage[GetIndex(i - 1, j - 1, width)] / 28.0;
- sum += 4.0 / 28.0 * inputImage[GetIndex(i - 1, j, width)];
- sum += inputImage[GetIndex(i - 1, j + 1, width)] / 28.0;
- sum += 4.0 / 28.0 * inputImage[GetIndex(i, j - 1, width)];
- sum += 8.0 / 28.0 * inputImage[GetIndex(i, j, width)];
- sum += 4.0 / 28.0 * inputImage[GetIndex(i, j + 1, width)];
- sum += inputImage[GetIndex(i + 1, j - 1, width)] / 28.0;
- sum += 4.0 / 28.0 * inputImage[GetIndex(i + 1, j, width)]; // zmiania kolejnosci wykonywania dzialan, czas wykonania (-1s)
- sum += inputImage[GetIndex(i + 1, j + 1, width)] / 28.0; // zamiana 2 peteli for, czas wykonywania (-1s)
- outputImage[GetIndex(i, j, width)] = (BYTE)sum;
- }
- }
- inline int GetIndex(int x, int y, int w) // zmieniło czas wykowania o 1/2
- {
- return x + y * w;
- }
- void PrintImage(const BYTE *image, const int N, const int M)
- {
- int i, j;
- for (i = 0; i < N; ++i)
- {
- for (j = 0; j < M; ++j)
- printf("%d ", image[GetIndex(i, j, M)]);
- printf("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement