This repository was archived by the owner on Mar 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEmbed1.m
106 lines (100 loc) · 3.53 KB
/
Embed1.m
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
%file:Embedding.m
%origin: the original image blocksize: the size of block
%x,y: the start location of block
%MSB: the number of every bit in adjustment area used for adjustment
%count: the max number of elements in EN1
function [data,SubImage,capacity] = Embedding( origin, blocksize, x, y, MSB, count, bits)
SubImage=origin;
capacity = 0;
%select the highest MSB bits of each pixel in adjustment area
%1-divide all pixel into different sets: EZ & EN1 & EN2 & CN & NC
%2-generate the lacation-map MAP
%3-store the lowest bit of each pixel in EN2 and CN
%EZ: h=0 || h=-1 used as expandable
%EN1: expandable and used as expandable
%EN2: expandable but used as changable
%CN: changable and used as changable
%NC: not changable and never use
SORT = zeros(blocksize,blocksize);%to store the kind of the pixel NC: 0; EZ&EN1: 1; EN2&CN: 2;
MAP = zeros(blocksize-2,(blocksize-2)/2);%MAP: store the location map to tell which pixel used as expandable
LSB = [];%LSB: store the lowest bit of each pixel used as changable
sum = 0; %calculate the number of elements in EN1 at present
num = 0; %store the size of P
for i = x+1 : 1 : x+blocksize-2
for j = y+1 : 2 : y+blocksize-2
l = floor(double(uint16(origin(i,j))+uint16(origin(i,j+1)))/2);
h = double(double(origin(i,j))-double(origin(i,j+1)));
min = 2*(255-l);
b = 2*l+1;
if( b < min )
min = b;
end
if h==0 || h==-1
SORT(i-x,j-y) = 1;
MAP(i-x,floor((j-y+1)/2)) = 1;
elseif abs(2*h+1) < min
if sum < count
SORT(i-x,j-y) = 1;
MAP(i-x,floor((j-y+1)/2)) = 1;
sum = sum+1;
else
SORT(i-x,j-y) = 2;
t = Dec2bin(h);
if h~=1 && h~=-2
num=num+1;
[~,len]=size(t);
LSB(num) = t(len)-'0';
end
end
elseif abs(2*floor(h/2)+1) < min
SORT(i-x,j-y) = 2;
t = Dec2bin(h);
if h~=1 && h~=-2 %as such value could br determined by location map
num=num+1;
[~,len]=size(t);
LSB(num) = t(len)-'0';
end
end
end
end
%encoding the MAP into L (using 01111110 as the ending of the data)
%when meet five and more than five '1' ,use 0 in the ending of '11111'
L = [];
MAP=MAP';
MAP=MAP(:);
MAP=MAP'; %to change the MAP to unidimensional matrix
%embedding bit in L & LSB $ bits into embedding area: EZ & EN1 & EN2 & CN
%in EZ & EN1: the h`=2*h+bit
%in EN2 & CN: the h`=2*(h/2)+bit
%l=(x+y)/2 h=x-y (as x and y are two pixel's value)
%x`=l+(h`+1)/2 y`=l-(h`)/2
%if there are more than len bits spare space, we embed 01111...1 in the end
data=[];
MAP = Encode(MAP);
[~,len1] = size(MAP);
data(1:len1) = MAP(1:len1);
[~,len2] = size(LSB);
data(len1+1:len1+len2) = LSB(1:len2);
no=1; %means the index of data to embed
for i = x+1 : 1 : x+blocksize-2
for j = y+1 : 2 : y+blocksize-2
if SORT(i-x,j-y) == 0
continue;
end
l = floor(double(uint16(origin(i,j))+uint16(origin(i,j+1)))/2);
h = double(double(origin(i,j))-double(origin(i,j+1)));
if SORT(i-x,j-y) == 1 %expandable
hh = 2*h;
capacity = capacity+1;
elseif SORT(i-x,j-y) == 2 %changable
hh = 2*floor(h/2);
capacity = capacity+1;
else
hh = h;
end
no = no+1;
SubImage(i,j) = uint8(l+floor((hh+1)/2));
SubImage(i,j+1) = uint8(l-floor(hh/2));
end
end
end