1
Fork 0
mirror of https://github.com/Steffo99/unimore-hpc-assignments.git synced 2024-11-29 03:24:22 +00:00
hpc-2022-g3/hls/assignment/sobel/sobel.cpp

69 lines
1.7 KiB
C++
Raw Permalink Normal View History

#include <cstring>
2022-12-18 23:00:31 +00:00
#include <math.h>
#include "sobel.h"
#define WIDTH 512
#define HEIGHT 512
void sobel(uint8_t *__restrict__ out, uint8_t *__restrict__ in)
2022-12-18 23:00:31 +00:00
{
#pragma HLS INTERFACE m_axi port=out offset=slave bundle=bout
#pragma HLS INTERFACE m_axi port=in offset=slave bundle=bin
2022-12-19 08:35:20 +00:00
const int sobelFilter[3][3] = {
{-1, 0, 1},
{-2, 0, 2},
{-1, 0, 1}
};
#pragma HLS ARRAY_PARTITION variable=sobelFilter complete dim=0
2022-12-18 23:00:31 +00:00
// Carica le prime tre righe nel buffer
uint8_t inBuffer[3*HEIGHT];
memcpy(inBuffer, in, 3*HEIGHT*sizeof(uint8_t));
esternoY:
for (int y = 0; y < HEIGHT - 2; y++)
2022-12-18 23:00:31 +00:00
{
esternoX:
for (int x = 0; x < WIDTH - 2; x++)
2022-12-18 23:00:31 +00:00
{
#pragma HLS PIPELINE
int dx = 0;
int dy = 0;
internoY:
2022-12-18 23:00:31 +00:00
for (int k = 0; k < 3; k++)
{
#pragma HLS UNROLL
const int inYOffset = ((y + k) % 3) * WIDTH;
internoX:
2022-12-18 23:00:31 +00:00
for (int z = 0; z < 3; z++)
{
#pragma HLS UNROLL
const int inXOffset = x + z;
const int inOffset = inYOffset + inXOffset;
const int inElement = inBuffer[inOffset];
dx += sobelFilter[k][z] * inElement;
dy += sobelFilter[z][k] * inElement;
2022-12-18 23:00:31 +00:00
}
}
const int outYOffset = (y + 1) * WIDTH;
const int outXOffset = (x + 1);
const int outOffset = outYOffset + outXOffset;
out[outOffset] = sqrt((float)((dx * dx) + (dy * dy)));
2022-12-18 23:00:31 +00:00
}
memcpy(inBuffer, in + (y % 3) * HEIGHT, HEIGHT*sizeof(uint8_t));
2022-12-18 23:00:31 +00:00
}
}