Blame view

yaj.py 4.31 KB
2b474806   Chunk   init.
1
2
__author__ = 'chunk'

2b474806   Chunk   init.
3
import numpy as np
c6c61f81   Chunk   staged.
4
import mjpeg
8c310e83   Chunk   jpeg base resolved.
5
from common import *
2b474806   Chunk   init.
6

8c310e83   Chunk   jpeg base resolved.
7
timer = Timer()
2b474806   Chunk   init.
8

8c310e83   Chunk   jpeg base resolved.
9
10
11
12
13
14
15
16
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]]
2b474806   Chunk   init.
17

2b474806   Chunk   init.
18

8c310e83   Chunk   jpeg base resolved.
19
20
21
22
23
24
25
def diffblock(c1, c2):
    diff = False
    if np.array_equal(c1, c2):
        print("blocks match")
    else:
        print("blocks not match")
        diff = True
2b474806   Chunk   init.
26

8c310e83   Chunk   jpeg base resolved.
27
    return diff
2b474806   Chunk   init.
28

2b474806   Chunk   init.
29

8c310e83   Chunk   jpeg base resolved.
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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))
                    diff = True
    return diff


def test_setblocks():
    """
    wholewise
    """
c6c61f81   Chunk   staged.
46
    imb = mjpeg.Jpeg("res/test4.jpg")
8c310e83   Chunk   jpeg base resolved.
47
48
49
50
51
52
53
54
55
56
    block = imb.getCoefBlock(channel='Y', loc=(-1, 2))
    print block

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

c6c61f81   Chunk   staged.
57
58
    ima = mjpeg.Jpeg("res/test3.jpg")
    imb = mjpeg.Jpeg("res/test4.jpg")
8c310e83   Chunk   jpeg base resolved.
59
60
61
    diffblocks(ima, imb)


548d95dc   Chunk   steganography(F3 ...
62
63
64
65
def test_setblocks2():
    """
    wholewises
    """
c6c61f81   Chunk   staged.
66
67
    ima = mjpeg.Jpeg("res/test3.jpg")
    imb = mjpeg.Jpeg("res/test4.jpg")
548d95dc   Chunk   steganography(F3 ...
68
69
70
71
72
73
74
75
76
77
78
79
80
81

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

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


8c310e83   Chunk   jpeg base resolved.
87
88
89
90
def test_setblock():
    """
    blockwise
    """
c6c61f81   Chunk   staged.
91
    imb = mjpeg.Jpeg("res/test4.jpg")
8c310e83   Chunk   jpeg base resolved.
92
93
94
95
96
97
98
99
100
101
102
103
104
    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)


548d95dc   Chunk   steganography(F3 ...
105
def test_split():
c6c61f81   Chunk   staged.
106
    imb = mjpeg.Jpeg("res/test3.jpg")
548d95dc   Chunk   steganography(F3 ...
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
    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():
8c310e83   Chunk   jpeg base resolved.
136
    timer.mark()
548d95dc   Chunk   steganography(F3 ...
137
    raw = np.fromfile("res/test4.jpg", np.uint8)
8c310e83   Chunk   jpeg base resolved.
138
    timer.report()
548d95dc   Chunk   steganography(F3 ...
139
    print raw
8c310e83   Chunk   jpeg base resolved.
140

548d95dc   Chunk   steganography(F3 ...
141
142
143
144
145
146
    bitsraw =  bytes2bits(raw)
    # bitsraw = bitsraw[:24]
    timer.report()
    print bitsraw

    bytesraw = bits2bytes(bitsraw)
8c310e83   Chunk   jpeg base resolved.
147
    timer.report()
548d95dc   Chunk   steganography(F3 ...
148
149
150
    print bytesraw

def test_iter():
c6c61f81   Chunk   staged.
151
    imb = mjpeg.Jpeg("res/test4.jpg")
548d95dc   Chunk   steganography(F3 ...
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
    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()
    # test_setblock()
    # timer.report()
    #
    # timer.mark()
    # test_setblocks()
    # timer.report()

    # test_split()

    # test_iter()

    # test_rawfile()

    # test_setblocks2()

    test_bitbyte()

    pass
8c310e83   Chunk   jpeg base resolved.

2b474806   Chunk   init.

2b474806   Chunk   init.