Blame view

test_jpeg.py 4.56 KB
548d95dc   Chunk   steganography(F3 ...
1
2
3
__author__ = 'chunk'

import numpy as np
c6c61f81   Chunk   staged.
4
5
import mjsteg
import jpegObj
26e2fe9f   Chunk   MPB steganalysis ...
6
from common import *
548d95dc   Chunk   steganography(F3 ...
7
8
9
10
11
12
13
14
15
16
17
18
19

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]]


8cfc1a23   Chunk   F5 half-finished.
20
21
22
def diffblock(c1, c2):
    diff = False
    if np.array_equal(c1, c2):
548d95dc   Chunk   steganography(F3 ...
23
        print("blocks match")
548d95dc   Chunk   steganography(F3 ...
24
25
26
27
    else:
        print("blocks not match")
        diff = True

c6c61f81   Chunk   staged.
28
    return diff
548d95dc   Chunk   steganography(F3 ...
29
30
31
32
33
34
35
36
37
38


def diffblocks(a, b):
    diff = False
    for comp in range(a.image_components):
        xmax, ymax = a.Jgetcompdim(comp)
        for y in range(ymax):
            for x in range(xmax):
                if a.Jgetblock(x, y, comp) != b.Jgetblock(x, y, comp):
                    print("blocks({},{}) in component {} not match".format(y, x, comp))
c6c61f81   Chunk   staged.
39
40
41
                    diff = True
    return diff

548d95dc   Chunk   steganography(F3 ...
42
43
44
45
46
47

def test_setblocks():
    """
    wholewise
    """
    imb = jpegObj.Jpeg("res/test4.jpg")
c6c61f81   Chunk   staged.
48
49
    block = imb.getCoefBlock(channel='Y', loc=(-1, 2))
    print block
548d95dc   Chunk   steganography(F3 ...
50
51
52
53
54
55
56
57
58
59
60
61
62
63

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

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

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

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


def test_setblocks2():
c6c61f81   Chunk   staged.
64
65
66
    """
    wholewises
    """
548d95dc   Chunk   steganography(F3 ...
67
68
69
70
71
72
    ima = jpegObj.Jpeg("res/test3.jpg")
    imb = jpegObj.Jpeg("res/test4.jpg")

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

c6c61f81   Chunk   staged.
73
    block = imb.getCoefBlock(channel='Y', loc=(-1, 2))
548d95dc   Chunk   steganography(F3 ...
74
75
76
77
78
79
80
81
82
83
    print block

    imb.setCoefBlocks(ima.getCoefBlocks())

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

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

    ima = jpegObj.Jpeg("res/test3.jpg")
c6c61f81   Chunk   staged.
84
    imb = jpegObj.Jpeg("res/test4.jpg")
548d95dc   Chunk   steganography(F3 ...
85
86
87
    diffblocks(ima, imb)


c6c61f81   Chunk   staged.
88
def test_setblock():
548d95dc   Chunk   steganography(F3 ...
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
    """
    blockwise
    """
    imb = jpegObj.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
    diffblock(blocks1, block_to_show)


def test_split():
    imb = jpegObj.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)])
8cfc1a23   Chunk   F5 half-finished.
113
    c3 = imb.getCoefBlocks()
548d95dc   Chunk   steganography(F3 ...
114
115
116
    print c1.shape, c2.shape
    if np.array_equal(c1, c2) and np.array_equal(c1, c3):
        print("blocks match")
8cfc1a23   Chunk   F5 half-finished.
117

548d95dc   Chunk   steganography(F3 ...
118
119
120
121
122
123

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()


8cfc1a23   Chunk   F5 half-finished.
124
def test_rawfile():
548d95dc   Chunk   steganography(F3 ...
125
126
127
128
129
130
131
132
    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
8cfc1a23   Chunk   F5 half-finished.
133
    # print raw.size
548d95dc   Chunk   steganography(F3 ...
134
    # print bytes2bits(raw)
c6c61f81   Chunk   staged.
135

548d95dc   Chunk   steganography(F3 ...
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
161
162
163
164
165
166
167
168
169
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 = jpegObj.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


if __name__ == '__main__':
    # timer.mark()
dceec280   Chunk   get capacity.
170
    # test_setblock()
f4b5291c   Chunk   Qaulity Calculati...
171
    # timer.report()
dceec280   Chunk   get capacity.
172
    #
f4b5291c   Chunk   Qaulity Calculati...
173
174
    # timer.mark()
    # test_setblocks()
c6c61f81   Chunk   staged.
175
176
    # timer.report()

45a82355   Chunk   staged.
177
    # test_split()
8cfc1a23   Chunk   F5 half-finished.
178

c6c61f81   Chunk   staged.
179
    # test_iter()
c9fdeb00   Chunk   LSB and F4 added.
180

6cbb3879   Chunk   F4 updated.
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
    # test_rawfile()

    # test_setblocks2()

    # test_bitbyte()

    ima = jpegObj.Jpeg("res/test3.jpg")
    imb = jpegObj.Jpeg("res/steged.jpg")
    print ima.Jgetcompdim(0)
    diffblocks(ima, imb)

    c1 = ima.getCoefBlocks()
    c2 =  imb.getCoefBlocks()

    print c1[0],c2[0]
548d95dc   Chunk   steganography(F3 ...
196
197

    pass