Repository navigation
Expand file tree
/
Copy pathhighLevelTestConvolution.c
More file actions
92 lines (73 loc) · 2.79 KB
/
Copy pathhighLevelTestConvolution.c
File metadata and controls
92 lines (73 loc) · 2.79 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
// #define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Define implementation before including (required by stb_image)
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#include <time.h>
void apply_filter_c(unsigned char* src, unsigned char* dest, long width, long height) {
// Standard 3x3 Laplacian Kernel
int kernel[3][3] = {
{-2,-1, 0},
{-1, 1, 1},
{ 0, 1, 2}
};
for (long y = 1; y < height - 1; y++) {
for (long x = 1; x < width - 1; x++) {
int sum = 0;
// Traditional nested loops for convolution
for (int ky = 0; ky < 3; ky++) {
for (int kx = 0; kx < 3; kx++) {
long pixel_idx = (y + ky - 1) * width + (x + kx - 1);
sum += src[pixel_idx] * kernel[ky][kx];
}
}
// Absolute value and clamping (same logic as assembly)
if (sum < 0) sum = -sum;
if (sum > 255) sum = 255;
dest[y * width + x] = (unsigned char)sum;
}
}
}
// Your assembly function declaration
// RDI: src, RSI: dest, RDX: width, RCX: height
extern void apply_filter_simd(unsigned char* src, unsigned char* dest, int width, int height);
int main() {
int width, height, channels;
int blurCount = 1000;
// for (int i = 0; i < blurCount; i++ )
// {
// 1. Load the image (Forcing 1 channel/Grayscale for simplicity)
unsigned char *img = stbi_load("download - Copy.jpeg", &width, &height, &channels, 1);
if (img == NULL) {
printf("Error: Could not load image.\n");
return 1;
}
// 2. Allocate memory for output image
unsigned char *output_img = (unsigned char *)malloc(width * height);
printf("Image Loaded: %d x %d\n", width, height);
// 3. Call your Assembly function
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
// اجرای جادوی اسمبلی
apply_filter_c(img, output_img, width, height);
clock_gettime(CLOCK_MONOTONIC, &end);
// محاسبه زمان با دقت بالا
double seconds = (end.tv_sec - start.tv_sec);
double pixels_per_second = (width * height) / (seconds + (end.tv_nsec - start.tv_nsec) / 1e9);
printf("Time: %f ms\n", (seconds * 1000) + (end.tv_nsec - start.tv_nsec) / 1e6);
printf("Speed: %.2f Megapixels/sec\n", pixels_per_second / 1e6);
// 4. Save the result
// if (!(i % (blurCount/50))){
stbi_write_jpg("download - Copy.jpeg", width, height, 1, output_img, 100);
// }
// 5. Cleanup
stbi_image_free(img);
free(output_img);
// }
printf("Processing complete. Check output.jpg\n");
return 0;
}