-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtry2.cpp
More file actions
203 lines (104 loc) · 4.45 KB
/
try2.cpp
File metadata and controls
203 lines (104 loc) · 4.45 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>
#include <vector>
#include <algorithm>
#define STB_IMAGE_IMPLEMENTATION
#include "stb-master/stb-master/stb_image.h"
#include "lodepng-master/lodepng-master/lodepng.h"
#include"dma.hpp"
std::vector<std::vector<uint8_t>> getPixelValues(const char* filename, int& width, int& height) {
int channels;
// Load the image file
unsigned char* image = stbi_load(filename, &width, &height, &channels, 1);
// Check if the image was successfully loaded
if (image == nullptr) {
std::cout << "Failed to read the image file." << std::endl;
return std::vector<std::vector<uint8_t>>();
}
// Create a 2D vector to store the pixel values
std::vector<std::vector<uint8_t>> pixelValues(height, std::vector<uint8_t>(width));
// Copy the pixel values to the 2D vector
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
pixelValues[i][j] = image[i * width + j];
}
}
stbi_image_free(image);
return pixelValues;
}
////////////////////////////////////// HARDWARE ACCELERATOR /////////////////////////////////////////////////
// std::vector<std::vector<uint8_t>> medianFilter(const std::vector<std::vector<uint8_t>>& image, int filterSize) {
// int width = image[0].size();
// int height = image.size();
// std::vector<std::vector<uint8_t>> filteredImage(height, std::vector<uint8_t>(width));
// int filterOffset = filterSize / 2;
// for (int y = 0; y < height; y++) {
// for (int x = 0; x < width; x++) {
// std::vector<uint8_t> pixels;
// for (int filterY = -filterOffset; filterY <= filterOffset; filterY++) {
// for (int filterX = -filterOffset; filterX <= filterOffset; filterX++) {
// int imageX = std::min(std::max(x + filterX, 0), width - 1);
// int imageY = std::min(std::max(y + filterY, 0), height - 1);
// pixels.push_back(image[imageY][imageX]);
// }
// }
// std::sort(pixels.begin(), pixels.end());
// uint8_t medianValue = pixels[pixels.size() / 2];
// filteredImage[y][x] = medianValue;
// }
// }
// return filteredImage;
// }
//////////////////////////////////////////////////////////////////////////////////////////////////
void saveImage(const std::vector<std::vector<uint8_t>>& image, const std::string& filename) {
int width = image[0].size();
int height = image.size();
std::vector<unsigned char> buffer(width * height);
// Copy the pixel values to the buffer
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
buffer[y * width + x] = image[y][x];
}
}
// Save the image in PNG format
if (lodepng::encode(filename, buffer, width, height, LCT_GREY, 8) != 0) {
std::cout << "Failed to save the image file." << std::endl;
}
}
int main() {
const char* filename = "lenna4.jpg";
int width, height;
// Get the pixel values for the image
std::vector<std::vector<uint8_t>> pixelValues = getPixelValues(filename, width, height);
int radius = 7;
// Apply median filter to the image
// std::vector<std::vector<uint8_t>> filteredImage = medianFilter(pixelValues, radius);
unsigned long int status;
DirectMemoryAccess* dma = new DirectMemoryAccess(0x40400000, 0x0e000000, 0x0f000000);
dma->reset();
dma->halt();
size_t rows=512, cols=512;
dma->write2DArray(0x0e000000,pixelValues, rows, cols);
dma->hexdumpSource(256*512);
dma->setInterrupt(true, false, 0);
dma->ready(); // ready the DMA
dma->setDestinationAddress(0x0f000000);
dma->setSourceAddress(0x0e000000);
dma->setDestinationLength(256*512);
dma->setSourceLength(256*512);
printf("Waiting for MM2S...\n");
do {
status = dma->getMM2SStatus();
dma->dumpStatus(status);
} while((!(status & 1<<12) && !(status & 1 << 1)));
printf("Waiting for S2MM...\n");
do {
status = dma->getS2MMStatus();
dma->dumpStatus(status);
} while((!(status & 1<<12) && !(status & 1 << 1)));
dma->hexdumpDestination(256*512);
// Save the filtered image
std::string outputFilename = "lenna4_filtered.png";
saveImage(filteredImage, outputFilename);
std::cout << "Median filter applied and filtered image saved successfully." <<std::endl;
return 0;
}