-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfc_HIST.c
More file actions
95 lines (79 loc) · 1.82 KB
/
Copy pathfc_HIST.c
File metadata and controls
95 lines (79 loc) · 1.82 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
// Leca Maria Catalina 315CA
#include "functions.h"
// creaza dinamic un vector ce va retine valorile de frecventa
double *freq(img image, int y)
{
int i, j, k, l;
double *v = (double *)malloc(y * sizeof(double));
if (!v)
return NULL;
l = 256 / y;
for (i = 0; i < y; i++) // initializeaza cu 0
v[i] = 0;
for (i = 0; i < image.height; i++)
for (j = 0; j < image.width; j++)
for (k = 0; k < y; k++)
if (l * k <= image.gray[i][j] && image.gray[i][j] < l * (k + 1))
v[k]++;
return v;
}
void hist(img image, char arg[])
{
if (image.type == 0) {
printf("No image loaded\n");
return;
}
if (image.type == 3 || image.type == 6) {
printf("Black and white image needed\n");
return;
}
int c[2], n = 0; // pentru parametrii
char *p = strtok(arg, " ");
while (p) {
if (n == 2) { // mai multi parametrii
printf("Invalid command\n");
return;
}
for (size_t i = 0; i < strlen(p); i++) {
if (p[i] < '0' || p[i] > '9') { // daca nu sunt numere
printf("Invalid command\n");
return;
}
}
c[n++] = to_number(p);
p = strtok(NULL, " ");
}
if (n < 2) {
printf("Invalid command\n");
return;
}
int x = c[0], y = c[1];
if (x < 1 || y < 1 || y > 256) {
printf("Invalid command\n");
return;
}
double *v1 = freq(image, y); // vectorul frecventa
double max = v1[0]; // cauta valoarea maxima in vector
for (int i = 1; i < y; i++)
if (v1[i] > max)
max = v1[i];
// un vector pentru valorile midificate din primul vector
int *v2 = (int *)malloc(y * sizeof(int));
if (!v2)
return;
double aux;
for (int i = 0; i < y; i++) {
aux = v1[i] / max;
aux *= x; // se aplica formula
v2[i] = (int)aux;
}
for (int i = 0; i < y; i++) {
printf("%d\t|\t", v2[i]);
for (int j = 0; j < v2[i]; j++) // afisare
printf("*");
printf("\n");
}
// stergere vectori
free(v1);
free(v2);
}