forked from ugocapeto/thepainter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscale_image_nearest_neighbor.c
65 lines (51 loc) · 1.06 KB
/
scale_image_nearest_neighbor.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
#include "header.h"
void scale_image_nearest_neighbor(
int *image_arr1,
int width1,
int height1,
int *image_arr2,
int width2,
int height2
)
{
int i2;
double y1;
int i1;
int j2;
double x1;
int j1;
int pixel1;
int intensity1;
int pixel2;
int intensity2;
for ( i2= 0 ; i2< height2; i2++ ) {
/*
Get corresponding y1 in image1_arr
*/
y1= ( (double)i2/(double)height2 ) * (double)height1;
for ( j2= 0 ; j2< width2; j2++ ) {
/*
Get corresponding x1 in image1_arr
*/
x1= ( (double)j2/(double)width2 ) * (double)width1;
i1= (int)round(y1);
j1= (int)round(x1);
/*
Make sure pixel in image_arr1 is gonna be in bounds
*/
if ( !(i1 >= 0) )
i1= 0;
if ( !(i1 < height1) )
i1= height1-1;
if ( !(j1 >= 0) )
j1= 0;
if ( !(j1 < width1) )
j1= width1-1;
pixel1= i1*width1+j1;
intensity1= image_arr1[pixel1];
pixel2= i2*width2+j2;
intensity2= intensity1;
image_arr2[pixel2]= intensity2;
}
}
}