Blame view

msteg/steganography/F4.py 4.59 KB
6cbb3879   Chunk   F4 updated.
1
2
__author__ = 'chunk'

c9fdeb00   Chunk   LSB and F4 added.
3
import numpy as np
6cbb3879   Chunk   F4 updated.
4
import numpy.random as rnd
b69b6985   Chunk   py module refract...
5
from msteg import *
c6c61f81   Chunk   staged.
6
import mjpeg
c9fdeb00   Chunk   LSB and F4 added.
7
8
9
from common import *


45a82355   Chunk   staged.
10
class F4(StegBase):
c9fdeb00   Chunk   LSB and F4 added.
11
12
13
14
    """ This module has two methods: <i>embed_raw_data</i> to embed data
    with the F3 algorithm and <i>extract_raw_data</i> to extract data
    which was embedded previously. """

6cbb3879   Chunk   F4 updated.
15
    def __init__(self, key=sample_key):
c9fdeb00   Chunk   LSB and F4 added.
16
17
18
        """
        Constructor of the F3 class.
        """
033d3b0d   Chunk   staged.
19
        StegBase.__init__(self, key)
c9fdeb00   Chunk   LSB and F4 added.
20

6cbb3879   Chunk   F4 updated.
21
22
23
24
    def _get_cov_data(self, img_path):
        """
        Returns DCT coefficients of the cover image.
        """
c6c61f81   Chunk   staged.
25
        self.cov_jpeg = mjpeg.Jpeg(img_path, key=self.key)
c9fdeb00   Chunk   LSB and F4 added.
26

6cbb3879   Chunk   F4 updated.
27
28
29
        cov_data = self.cov_jpeg.getsignal(channel='Y')
        self.cov_data = np.array(cov_data, dtype=np.int16)
        return self.cov_data
c9fdeb00   Chunk   LSB and F4 added.
30

6cbb3879   Chunk   F4 updated.
31
    def embed_raw_data(self, src_cover, src_hidden, tgt_stego):
c9fdeb00   Chunk   LSB and F4 added.
32

c9fdeb00   Chunk   LSB and F4 added.
33
        self.t0 = time.time()
c9fdeb00   Chunk   LSB and F4 added.
34

6cbb3879   Chunk   F4 updated.
35
36
37
38
        try:
            cov_data = self._get_cov_data(src_cover)
            hid_data = self._get_hid_data(src_hidden)
            # print hid_data.dtype,type(hid_data),hid_data.tolist()
c9fdeb00   Chunk   LSB and F4 added.
39

6cbb3879   Chunk   F4 updated.
40
41
42
43
44
45
46
47
48
            cov_data, bits_cnt = self._raw_embed(cov_data, hid_data)

            if bits_cnt != np.size(hid_data) * 8:
                raise Exception("Expected embedded size is %db but actually %db." % (
                    np.size(hid_data) * 8, bits_cnt))

            self.cov_jpeg.setsignal(cov_data, channel='Y')
            self.cov_jpeg.Jwrite(tgt_stego)

873557f9   Chunk   staged.
49
50
51
52
53
54
55
56
57
            cov_bits = np.sum(cov_data != 0) - cov_data.size / 64
            self._display_rate(cov_bits, bits_cnt)

            # # size_cov = os.path.getsize(tgt_stego)
            # size_cov = np.size(cov_data) / 8
            # size_embedded = np.size(hid_data)
            #
            # self._display_stats("embedded", size_cov, size_embedded,
            #                     time.time() - self.t0)
6cbb3879   Chunk   F4 updated.
58
59
60
61
62
63
64
65

        except TypeError as e:
            raise e
        except Exception as expt:
            print "Exception when embedding!"
            raise

    def extract_raw_data(self, src_steg, tgt_hidden):
c9fdeb00   Chunk   LSB and F4 added.
66

c9fdeb00   Chunk   LSB and F4 added.
67
        self.t0 = time.time()
c9fdeb00   Chunk   LSB and F4 added.
68

6cbb3879   Chunk   F4 updated.
69
70
71
72
73
74
75
76
77
        try:
            steg_data = self._get_cov_data(src_steg)
            # emb_size = os.path.getsize(src_steg)
            emb_size = np.size(steg_data) / 8


            # recovering file size
            header_size = 4 * 8
            size_data, bits_cnt = self._raw_extract(steg_data, header_size)
8cfc1a23   Chunk   F5 half-finished.
78
79
80
81
            if bits_cnt < header_size:
                raise Exception("Expected embedded size is %db but actually %db." % (
                    header_size, bits_cnt))

6cbb3879   Chunk   F4 updated.
82
            size_data = bits2bytes(size_data)
6cbb3879   Chunk   F4 updated.
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

            size_hd = 0
            for i in xrange(4):
                size_hd += size_data[i] * 256 ** i

            raw_size = size_hd * 8

            if raw_size > np.size(steg_data):
                raise Exception("Supposed secret data too large for stego image.")

            hid_data, bits_cnt = self._raw_extract(steg_data, raw_size)

            if bits_cnt != raw_size:
                raise Exception("Expected embedded size is %db but actually %db." % (
                    raw_size, bits_cnt))

            hid_data = bits2bytes(hid_data)
            # print hid_data.dtype,type(hid_data),hid_data.tolist()
            hid_data[4:].tofile(tgt_hidden)

            self._display_stats("extracted", emb_size,
                                np.size(hid_data),
                                time.time() - self.t0)
        except Exception as expt:
            print "Exception when extracting!"
            raise

    def _raw_embed(self, cov_data, hid_data):
c9fdeb00   Chunk   LSB and F4 added.
111
        """
6cbb3879   Chunk   F4 updated.
112
        cov_data - 1-D numpy.int16 array (permunated)
c9fdeb00   Chunk   LSB and F4 added.
113
114
115
116
        hid_data - 1-D numpy.uint8 array
        """
        hid_data = bytes2bits(hid_data)
        i = 0
c9fdeb00   Chunk   LSB and F4 added.
117
        for x in np.nditer(cov_data, op_flags=['readwrite']):
6cbb3879   Chunk   F4 updated.
118
            if x == 0: continue
c9fdeb00   Chunk   LSB and F4 added.
119
120
121
122
123
124
125
126
127

            m = (hid_data[i] & 1)
            if x > 0 and x & 1 != m:
                x[...] -= 1
            elif x < 0 and x & 1 == m:
                x[...] += 1
            if x == 0: continue
            i += 1
            if i == hid_data.size: break
8cfc1a23   Chunk   F5 half-finished.
128

6cbb3879   Chunk   F4 updated.
129
        return cov_data, i
c9fdeb00   Chunk   LSB and F4 added.
130
131
132
133

    def _raw_extract(self, steg_data, num_bits):
        """
        Just a small helper function to extract hidden data.
6cbb3879   Chunk   F4 updated.
134
        steg_data - 1-D numpy.int16 array (permunated)
c9fdeb00   Chunk   LSB and F4 added.
135
        """
8cfc1a23   Chunk   F5 half-finished.
136

c9fdeb00   Chunk   LSB and F4 added.
137
138
        hid_data = np.zeros(num_bits, np.uint8)
        j = 0
6cbb3879   Chunk   F4 updated.
139
140
        for x in steg_data:
            if x == 0: continue
c9fdeb00   Chunk   LSB and F4 added.
141
142
143
144
145
146
147
148
            if j >= num_bits: break
            if x > 0:
                hid_data[j] = x & 1
            else:
                hid_data[j] = (x & 1) ^ 1

            j = j + 1

6cbb3879   Chunk   F4 updated.
149
        return hid_data, j
c9fdeb00   Chunk   LSB and F4 added.
150
151

    def __str__(self):
033d3b0d   Chunk   staged.
152
        return "F4"