forked from jingweizhanghuai/image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
overlay.c
141 lines (116 loc) · 3.22 KB
/
overlay.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
#include <stdio.h>
#include "type.h"
#define SRC1(a,b) p_src1[(b)*img_width+(a)]
#define SRC2(a,b) p_src2[(b)*ovl_width+(a)]
#define SRC1_0(a,b) p_src1[((b)*img_width+(a))*cn1]
#define SRC1_1(a,b) p_src1[((b)*img_width+(a))*cn1+1]
#define SRC1_2(a,b) p_src1[((b)*img_width+(a))*cn1+2]
#define SRC2_0(a,b) p_src2[((b)*ovl_width+(a))*cn2]
#define SRC2_1(a,b) p_src2[((b)*ovl_width+(a))*cn2+1]
#define SRC2_2(a,b) p_src2[((b)*ovl_width+(a))*cn2+2]
#define SRC2_3(a,b) p_src2[((b)*ovl_width+(a))*cn2+3]
#define DST_0(a,b) p_dst[((b)*img_width+(a))*3]
#define DST_1(a,b) p_dst[((b)*img_width+(a))*3+1]
#define DST_2(a,b) p_dst[((b)*img_width+(a))*3+2]
ImgMat *imgCreateMat(int height,int width,char type);
void imgOverlay(ImgMat *src1,ImgMat *src2,ImgPoint locate)
{
int img_width;
img_width = src1->width;
int img_height;
img_height = src1->height;
unsigned char *p_src1;
p_src1 = src1->data.ptr;
int ovl_width;
ovl_width = src2->width;
int ovl_height;
ovl_height = src2->height;
unsigned char *p_src2;
p_src2 = src2->data.ptr;
if((locate.x>img_width)||(locate.y>img_height))
return;
int p1x,p1y,p2x,p2y;
p1x = (locate.x<0)?0:locate.x;
p1y = (locate.y<0)?0:locate.y;
p2x = locate.x+ovl_width;
p2y = locate.y+ovl_height;
if((p2x<0)||(p2y<0))
return;
p2x = (p2x>img_width)?img_width:p2x;
p2y = (p2y>img_height)?img_height:p2y;
int cn1;
cn1 = ((src1->type&0xF8)>>3)+1;
int cn2;
cn2 = ((src2->type&0xF8)>>3)+1;
int k;
int i,j;
if((cn1 == 1)&&(cn2 == 1))
{
for(j=p1y;j<p2y;j++)
for(i=p1x;i<p2x;i++)
SRC1(i,j) = SRC2(i-locate.x,j-locate.y);
}
else if((cn1 >= 3)&&(cn2 == 1))
{
for(j=p1y;j<p2y;j++)
for(i=p1x;i<p2x;i++)
{
SRC1_0(i,j) = SRC2(i-locate.x,j-locate.y);
SRC1_1(i,j) = SRC2(i-locate.x,j-locate.y);
SRC1_2(i,j) = SRC2(i-locate.x,j-locate.y);
}
}
else if((cn1 >= 3)&&(cn2 == 3))
{
for(j=p1y;j<p2y;j++)
for(i=p1x;i<p2x;i++)
{
SRC1_0(i,j) = SRC2_0(i-locate.x,j-locate.y);
SRC1_1(i,j) = SRC2_1(i-locate.x,j-locate.y);
SRC1_2(i,j) = SRC2_2(i-locate.x,j-locate.y);
}
}
else if((cn1 >= 3)&&(cn2 == 4))
{
for(j=p1y;j<p2y;j++)
for(i=p1x;i<p2x;i++)
{
k = SRC2_3(i-locate.x,j-locate.y);
SRC1_0(i,j) = (SRC1_0(i,j)*(256-k)+SRC2_0(i-locate.x,j-locate.y)*k)>>8;
SRC1_1(i,j) = (SRC1_1(i,j)*(256-k)+SRC2_1(i-locate.x,j-locate.y)*k)>>8;
SRC1_2(i,j) = (SRC1_2(i,j)*(256-k)+SRC2_2(i-locate.x,j-locate.y)*k)>>8;
}
}
else
{
ImgMat *dst;
dst = imgCreateMat(img_height,img_width,TYPE_8UC3);
unsigned char *p_dst;
p_dst = dst->data.ptr;
if((cn1 == 1)&&(cn2 == 3))
{
for(j=p1y;j<p2y;j++)
for(i=p1x;i<p2x;i++)
{
DST_0(i,j) = SRC2_0(i-locate.x,j-locate.y);
DST_1(i,j) = SRC2_1(i-locate.x,j-locate.y);
DST_2(i,j) = SRC2_2(i-locate.x,j-locate.y);
}
}
else if((cn1 == 1)&&(cn2 == 4))
{
for(j=p1y;j<p2y;j++)
for(i=p1x;i<p2x;i++)
{
k = SRC2_3(i-locate.x,j-locate.y);
DST_0(i,j) = (SRC1(i,j)*(256-k)+SRC2_0(i-locate.x,j-locate.y)*k)>>8;
DST_1(i,j) = (SRC1(i,j)*(256-k)+SRC2_1(i-locate.x,j-locate.y)*k)>>8;
DST_2(i,j) = (SRC1(i,j)*(256-k)+SRC2_2(i-locate.x,j-locate.y)*k)>>8;
}
}
free(src1->data.ptr);
free(src1->hidinfo);
*src1 = *dst;
free(dst);
}
}