-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircle.c
142 lines (112 loc) · 4.03 KB
/
circle.c
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define HEADER_SIZE 54+84 // BMP header len
#define BPP 2 // expected bytes per pixel
int circle_asm(void *image, int width, int height, int xc, int yc,
int radius, unsigned int color);
int circle_asm2(void *image, int width, int height, int xc, int yc,
int radius, unsigned int color);
void draw_pixel(void* image, int width, int height, int x, int y, unsigned short color);
void draw_point(void* image, int width, int height, int x, int y, unsigned short color) {
short* addr = (short*)(image + width * BPP * y + x * BPP);
*addr = color;
printf("addr:%llu\n",(unsigned long long)addr);
}
int circle_c(void *image, int width, int height, int xc, int yc,
int radius, unsigned int color) {
int rad2 = radius * radius;
printf("buffer:%llu\n",(unsigned long long)image);
for(int x=0;x<radius;x++) {
int x2 = x * x;
int y = sqrt(rad2-x2);
draw_pixel(image,width,height,xc+x,yc+y,color);
draw_pixel(image,width,height,xc-x,yc+y,color);
draw_pixel(image,width,height,xc-x,yc-y,color);
draw_pixel(image,width,height,xc+x,yc-y,color);
printf("x=%d, y=%d, xc=%d, yc=%d\n",x,y,xc+x,yc+y);
}
return 0;
}
int circle_c2(void *image, int width, int height, int xc, int yc,
int radius, unsigned int color) {
int x = 0;
int y = radius;
int delta = 1 - 2 * radius;
int error = 0;
while (y >= x) {
draw_pixel(image,width,height,xc + x, yc + y, color);
draw_pixel(image,width,height,xc + x, yc - y, color);
draw_pixel(image,width,height,xc - x, yc + y, color);
draw_pixel(image,width,height,xc - x, yc - y, color);
draw_pixel(image,width,height,xc + y, yc + x, color);
draw_pixel(image,width,height,xc + y, yc - x, color);
draw_pixel(image,width,height,xc - y, yc + x, color);
draw_pixel(image,width,height,xc - y, yc - x, color);
error = 2 * (delta + y) - 1;
if ((delta < 0) && (error <= 0)) {
delta += 2 * ++x + 1;
continue;
}
if ((delta > 0) && (error > 0)) {
delta -= 2 * --y + 1;
continue;
break;
}
delta += 2 * (++x - --y);
}
}
void debug(unsigned long long i) {
printf("debug(%llu)\n",i);
}
void udebug(long long i) {
printf("debug(%lld)\n",i);
}
int sqrt_c(unsigned long long i) {
unsigned long long result = sqrt(i);
// printf("sqrt_c(%llu) retruns %llu\n",i,result);
return result;
}
int main(int argc, char *argv[])
{
// if (argc < 4)
// return 1;
// char *filename = argv[1];
// int radius = atoi(argv[2]);
// unsigned int color = atoi(argv[3]);
char *filename = "picEx1.bmp";
int radius = 100;
unsigned int color = 0x3542;
FILE *file = fopen(filename, "rb");
if (file == NULL) {
printf("Nie można otworzyć pliku.\n");
return 1;
}
// Odczyt nagłówka pliku BMP
unsigned char bmp_header[HEADER_SIZE];
fread(bmp_header, sizeof(unsigned char), HEADER_SIZE, file);
// Pobranie szerokości i wysokości obrazu z nagłówka BMP
int width = *(int*)&bmp_header[18];
int height = *(int*)&bmp_header[22];
int bytePerPixel = *(short*)&bmp_header[28]/8;
printf("w:%d, h:%d, bpp:%d\n",width,height,bytePerPixel);
// Alokacja pamięci dla obrazu
void *image = malloc(width * height * bytePerPixel);
// Odczyt danych obrazu
fread(image, bytePerPixel, width * height, file);
fclose(file);
int xc = width / 2; // Współrzędna x środka okręgu
int yc = height / 2; // Współrzędna y środka okręgu
circle_asm2(image, width, height, xc, yc, radius, color);
circle_c2(image, width, height, xc+20, yc+20, radius, color);
FILE *output_file = fopen("output.bmp", "wb");
if (output_file != NULL) {
fwrite(bmp_header, sizeof(unsigned char), HEADER_SIZE, output_file);
fwrite(image, bytePerPixel, width * height, output_file);
fclose(output_file);
} else {
printf("Nie można otworzyć pliku do zapisu.\n");
}
free(image);
return 0;
}