-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.c
More file actions
45 lines (41 loc) · 1 KB
/
Image.c
File metadata and controls
45 lines (41 loc) · 1 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
#include "Image.h"
#include "MyString.h"
Image* Image_read(MyString* str)
{
Image* img = malloc(sizeof(Image));
img->data = stbi_load(str->data, &(img->w), &(img->h), &(img->comp), STBI_grey);
if(img->data == NULL)
{
free(img);
return NULL;
}
return img;
}
Distribution* Image_getDistribution(Image* img)
{
Distribution* r = Distribution_init();
double quadW = ((float)img->w)/FIELD_SIZE;
double quadH = ((float)img->h)/FIELD_SIZE;
double f = 1.0/(quadW*quadH);
int i, j;
for(i = 0; i < img->h; i++)
{
for(j = 0; j < img->w; j++)
{
r->data[Distribution_getIndex((int)(j/quadW), (int)(i/quadH))]
+= Image_get(img, j, i);
}
}
for(i = 0; i < FIELD_SIZE; i++)
{
for(j = 0; j < FIELD_SIZE; j++)
{
r->data[Distribution_getIndex(j, i)] *= f;
}
}
return r;
}
uchar Image_get(Image* img, int x, int y)
{
return img->data[y * img->w + x];
}