-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistogram.h
More file actions
69 lines (61 loc) · 1.72 KB
/
Copy pathhistogram.h
File metadata and controls
69 lines (61 loc) · 1.72 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "structs.h"
#include "allocvector.h"
#ifndef HISTOGRAM_H
#define HISTOGRAM_H
void histogram(image img)
{
int num, x, y = 0, z = 0, stars;
char input[50];
// citesc x si y
fgets(input, sizeof(input), stdin);
// pentru verificare daca nu este si ceva in plus
num = sscanf(input, "%d %d %d", &x, &y, &z);
// verific daca exista o imagine in memorie
if (!img.data && !img.rgb) {
printf("No image loaded");
printf("\n");
} else {
// verific daca s-a citit ce trebuie
if (num != 2 || y < 2 || y > 256 || ((y & (y - 1)) != 0)) {
printf("Invalid command");
printf("\n");
} else {
// verific daca imaginea este alb-negru
if (img.magic[1] != '2' && img.magic[1] != '5') {
printf("Black and white image needed");
printf("\n");
} else {
int *frequency, mx = -1, *bins;
allocvector(&frequency); // aloc un vector de frecventa
for (int i = 0; i < img.height; i++)
for (int j = 0; j < img.width; j++)
frequency[img.data[i][j]]++; // actualizez frecventa
allocvector(&bins); // aloc in memorie un vector de bin-uri
for (int i = 0; i < y; i++) {
bins[i] = 0;
// calculez pentru fiecare bin frecventa totala
for (int j = 0; j < 256 / y; j++)
bins[i] = bins[i] + frequency[i * (256 / y) + j];
}
// determin frecventa maxima din bins
for (int i = 0; i < 256; i++)
if (bins[i] > mx)
mx = bins[i];
// calculez numarul de stele si afisez histograma
for (int i = 0; i < y; i++) {
stars = (bins[i] * x) / mx;
printf("%d\t|\t", stars);
for (int j = 0; j < stars; j++)
printf("*");
printf("\n");
}
free(frequency);
free(bins);
}
}
}
}
#endif