-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedian.cpp
83 lines (64 loc) · 3.14 KB
/
median.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <algorithm>
#include "include/bitmapRW.h"
#include "include/border_checker.h"
#define INPUT "../data/img2.bmp"
#define OUTPUT "../data/img3.bmp"
int main(int argc, char* argv[]) {
byte *pixels; //array for data to be read from image
byte *pixels_filtered; //array for data to be written to
int32_t width;
int32_t height;
uint32_t bytesPerPixel;
char* input = argv[1];
char* output = argv[2];
clock_t start, end;
start = clock();
ReadImage(input, &pixels, &width, &height, &bytesPerPixel);
pixels_filtered = (byte*) malloc(width * height *bytesPerPixel);
for (int64_t i = 0; i < width * height * bytesPerPixel; i+=bytesPerPixel) {
int chunk[9]; //chunk of pixels surrounding A0
// [][ ][]
// [][A0][]
// [][ ][]
border_t border = checkBorder(i, width, height);
//central pixel
chunk[0] = pixels[i];
//on the L and R sides
chunk[1] = (border.widthBorderDistance != -1) ? pixels[i - bytesPerPixel] : 0;
chunk[2] = (border.widthBorderDistance != 1) ? pixels[i + bytesPerPixel] : 0;
//on the U and D sides
chunk[3] = (border.heightBorderDistance != -1) ? pixels[i + bytesPerPixel * width] : 0;
chunk[4] = (border.heightBorderDistance != 1) ? pixels[i - bytesPerPixel * width] : 0;
//upper corners
chunk[5] = ((border.heightBorderDistance != 1) && (border.widthBorderDistance != 1)) ? pixels[i - bytesPerPixel * width + bytesPerPixel] : 0;
chunk[6] = ((border.heightBorderDistance != 1) && (border.widthBorderDistance != -1)) ? pixels[i - bytesPerPixel * width - bytesPerPixel] : 0;
//bottom corners
chunk[7] = ((border.heightBorderDistance != -1) && (border.widthBorderDistance != 1)) ? pixels[i + bytesPerPixel * width + bytesPerPixel] : 0;
chunk[8] = ((border.heightBorderDistance != -1) && (border.widthBorderDistance != -1)) ? pixels[i + bytesPerPixel * width - bytesPerPixel] : 0;
std::sort(chunk, chunk + 9); //std::sort gets 2 addresses of the piece of memory to be sorted - beginning and the end.
//after sorting, the median value gonna end up in the 5th place in the array
pixels_filtered[i] = chunk[5];
pixels_filtered[i+1] = chunk[5];
pixels_filtered[i+2] = chunk[5];
}
WriteImage(output, pixels_filtered, width, height, bytesPerPixel);
end = clock();
double runtime = double(end - start) / double(CLOCKS_PER_SEC);
printf("%f\n", runtime);
//Даня, это тебе
byte* pixel_data_bin;
pixel_data_bin = (byte*) malloc(width * height);
int64_t j = 0;
int64_t i = 0;
while (i < width*height*bytesPerPixel){
pixel_data_bin[j] = pixels_filtered[i];
i+=bytesPerPixel;
j++;
}
//теперь в pixel_data_bin хранятся последовательно пиксели (из правого нижнего угла в левый верхний (sic!))
//его можно использовать дальше или записать в любой файл ("wb")
free(pixel_data_bin);
free(pixels);
free(pixels_filtered);
}