Blame view

test/test_jpeg.py 6.52 KB
080c30c2   Chunk   F5 lib updated. I...
1
2
3
__author__ = 'chunk'

import numpy as np
95f76ce8   Chunk   theis finished.论文...
4
5
import matplotlib.pyplot as plt
import seaborn as sns
9ff70cf4   Chunk   capacity engeneer...
6
7
from .. import mjpeg
from ..mjpeg import base
95f76ce8   Chunk   theis finished.论文...
8
from ..msteg.steganography import LSB, F3, F4, F5
080c30c2   Chunk   F5 lib updated. I...
9

9ff70cf4   Chunk   capacity engeneer...
10
from ..common import *
080c30c2   Chunk   F5 lib updated. I...
11
12
13
14
15
16
17
18
19
20
21
22

timer = Timer()

sample = [[7, 12, 14, -12, 1, 0, -1, 0],
          [6, 5, -10, 0, 6, 0, 0, 0],
          [0, 6, -5, 4, 0, -1, 0, 0],
          [0, -3, 0, 1, -1, 0, 0, 0],
          [-3, 5, 0, 0, 0, 0, 0, 0],
          [2, -1, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 0]]

95f76ce8   Chunk   theis finished.论文...
23
24
sample_key = [46812L, 20559L, 31360L, 16681L, 27536L, 39553L, 5427L, 63029L, 56572L, 36476L, 25695L,
              61908L, 63014L,
080c30c2   Chunk   F5 lib updated. I...
25
26
              5908L, 59816L, 56765L]

95f76ce8   Chunk   theis finished.论文...
27
28
29
30
# plt.ticklabel_format(style='sci', axis='both', scilimits=(1, 4))

plt.ticklabel_format(style='sci', axis='both')

9ff70cf4   Chunk   capacity engeneer...
31
32
package_dir = os.path.dirname(os.path.abspath(__file__))

080c30c2   Chunk   F5 lib updated. I...
33
34
35
36
37
38
39
40
41
42
43

def test_setblocks():
    """
    wholewise
    """
    imb = mjpeg.Jpeg("res/test4.jpg")
    block = imb.getCoefBlock(channel='Y', loc=(-1, 2))
    print block

    imb.setCoefMatrix(np.array([[0] * 800 for i in range(600)]), channel='Y')

95f76ce8   Chunk   theis finished.论文...
44
    block = imb.getCoefBlock(channe99l='Y', loc=(-1, 2))
080c30c2   Chunk   F5 lib updated. I...
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
    print block

    imb.Jwrite("res/test4.jpg")

    ima = mjpeg.Jpeg("res/test3.jpg")
    imb = mjpeg.Jpeg("res/test4.jpg")
    mjpeg.diffblocks(ima, imb)


def test_setblocks2():
    """
    wholewises
    """
    ima = mjpeg.Jpeg("res/test3.jpg")
    imb = mjpeg.Jpeg("res/test4.jpg")

    block = ima.getCoefBlock(channel='Y', loc=(-1, 2))
    print block

    block = imb.getCoefBlock(channel='Y', loc=(-1, 2))
    print block

    imb.setCoefBlocks(ima.getCoefBlocks())

    block = imb.getCoefBlock(channel='Y', loc=(-1, 2))
    print block

    imb.Jwrite("res/test4.jpg")

    ima = mjpeg.Jpeg("res/test3.jpg")
    imb = mjpeg.Jpeg("res/test4.jpg")
    mjpeg.diffblocks(ima, imb)


def test_setblock():
    """
    blockwise
    """
    imb = mjpeg.Jpeg("res/test4.jpg")
    block = imb.getCoefBlock(channel='Y', loc=(0, 1))
    print block

    # print b.setCoefBlock(np.array([[27] + [0] * 7] + [[0] * 8 for i in range(7)]), loc=(0, 1))
    imb.setCoefBlock(np.array(sample), loc=(0, 1))
    blocks1 = imb.getCoefBlock(channel='Y', loc=(0, 1))
    print blocks1
    blocks2 = imb.Jgetblock(1, 0, 0)
    block_to_show = np.frombuffer(blocks2, dtype=np.int16, count=-1, offset=0).reshape(8, 8)
    print block_to_show
    mjpeg.diffblock(blocks1, block_to_show)


def test_split():
    imb = mjpeg.Jpeg("res/test3.jpg")
    c = imb.getCoefMatrix(channel='Y')
    print type(c[0, 0])
    d = c.ravel()
    c1 = np.array([np.split(arr, arr.shape[1] / 8, axis=1) for arr in np.split(c, c.shape[0] / 8)])
    c2 = np.array([np.hsplit(arr, arr.shape[1] / 8) for arr in np.vsplit(c, c.shape[0] / 8)])
    c3 = imb.getCoefBlocks()
    print c1.shape, c2.shape
    if np.array_equal(c1, c2) and np.array_equal(c1, c3):
        print("blocks match")


def _convert_bits(hid_data):
    hid_data_bits = [map(int, '{0:08b}'.format(byte)) for byte in hid_data]
    return np.array(hid_data_bits).ravel()


def test_rawfile():
    raw = [0, 0, 0, 0] + np.fromfile("res/test4.jpg", np.uint8).tolist()
    raw_size = len(raw)
    print raw_size
    for i in xrange(4):
        raw[i] = raw_size % 256
        raw_size /= 256
    raw = np.array(raw)
    print raw.shape, raw
    # print raw.size
    # print bytes2bits(raw)


def test_bitbyte():
    timer.mark()
    raw = np.fromfile("res/test4.jpg", np.uint8)
    timer.report()
    print raw

    bitsraw = bytes2bits(raw)
    # bitsraw = bitsraw[:24]
    timer.report()
    print bitsraw

    bytesraw = bits2bytes(bitsraw)
    timer.report()
    print bytesraw


def test_iter():
    imb = mjpeg.Jpeg("res/test4.jpg")
    blocks = imb.getCoefBlocks(channel='Y')
    cnt = 0
    for x in np.nditer(blocks, op_flags=['readwrite']):
        if x == 0:
            continue
        x[...] = 2
        print x
    for x in np.nditer(blocks):
        if x == 0:
            continue
        cnt += 1
        print x
    print cnt


9ff70cf4   Chunk   capacity engeneer...
161
def test_jpeg():
b9990e77   Chunk   staged.
162
    ima = mjpeg.Jpeg(os.path.join(package_dir, "../res/cropped_pil.jpg"), key=sample_key)
9ff70cf4   Chunk   capacity engeneer...
163
164
165
166
167
168
169
170
171
172
173
174
175
    print ima.getQuality()
    print ima.getCapacity('All')
    sys.exit(0)
    """
    55df71faed43b37a8ac242120a72d 74903
    988d826212a6eed023ab99284971d 113279
    221379062154e32d0efa5cce32f1b 97137
    cecdb4b3fef5433faa3d24259e3e4 140687
    e0255871e984f61ee8dc1800b9184 81294
    tmp000 84057
    """


95f76ce8   Chunk   theis finished.论文...
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
def test_hist():
    ima = mjpeg.Jpeg(os.path.join(package_dir, "../res/high/pic3_orig.jpg"), key=sample_key)
    print ima.getQuality()
    print ima.getCapacity('All')

    capacity = ima.getCapacity()
    print capacity
    rate = 0.65
    hidden = np.random.bytes(int(int(capacity) * rate) / 8)
    steger = F5.F5(sample_key, 1)
    steger2 = F4.F4(key=None)
    steger3 = LSB.LSB(key=None)
    embed_rate = steger3.embed_raw_data(os.path.join(package_dir, "../res/high/pic3_orig.jpg"),
                                       hidden,
                                       os.path.join(package_dir, "../res/high/pic3_dest.jpg"),
                                       frommem=True)
    #
    print embed_rate

    imb = mjpeg.Jpeg(os.path.join(package_dir, "../res/high/pic3_dest.jpg"), key=sample_key)
    print imb.getQuality()
    print imb.getCapacity('All')
84648488   Chunk   reverted.
198

95f76ce8   Chunk   theis finished.论文...
199
200
201
202
203
204
205
206
    A = imb.rawsignal().tolist()
    E = [i for i in range(-8, 10)]
    plt.hist(A, E, histtype='bar', rwidth=0.8, align='left')
    plt.xlabel("JPEG coefficients after quantisation")
    plt.ylabel("Frequency")
    plt.xticks([i for i in range(-8, 9)])
    plt.ylim(ymax=300000)
    plt.show()
84648488   Chunk   reverted.
207
208


080c30c2   Chunk   F5 lib updated. I...
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
if __name__ == '__main__':
    # timer.mark()
    # test_setblock()
    # timer.report()
    #
    # timer.mark()
    # test_setblocks()
    # timer.report()

    # test_split()

    # test_iter()

    # test_rawfile()

    # test_setblocks2()

    # test_bitbyte()

d0be60e7   Chunk   jpeg update.
228
    ima = mjpeg.Jpeg("res/166500/cecdb4b3fef5433faa3d24259e3e4.jpg", key=sample_key)
080c30c2   Chunk   F5 lib updated. I...
229
    print ima.getQuality()
d0be60e7   Chunk   jpeg update.
230
    print ima.getCapacity()
080c30c2   Chunk   F5 lib updated. I...
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
    sys.exit(0)

    # imb = mjpeg.Jpeg("res/new.jpg",key=sample_key)
    imc = mjpeg.Jpeg("res/steged.jpg", key=sample_key)
    print ima.Jgetcompdim(0)
    print ima.getkey(), imc.getkey()
    print mjpeg.diffblocks(ima, imc)

    # c1 = ima.getCoefBlocks()
    # c2 =  imb.getCoefBlocks()
    #
    # # print c1[0],c2[0]
    # s1 = imb.getsignal(channel='Y')
    # s2 = ima.getsignal(channel='Y')
    # imb.setsignal(s2,channel='Y')
    # imb.Jwrite('res/new.jpg')


    # print base.acMask(8,16)
    # mmask = base.acMaskBlock
    # print mmask
    # sample = np.array(sample)[mmask]
    # print np.hstack([[],sample])
    pass