-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackend.py
212 lines (167 loc) · 7.23 KB
/
backend.py
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
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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# -*- coding: utf-8 -*-
"""
safedun-server Backend
Created on Sun Oct 13 00:00:00 2019
Author: Adil Rahman
GitHub: https://github.com/adildsw/safedun-server
"""
import cv2
import numpy
import math
from io import BytesIO
class safedun:
def __init__(self, max_size=5):
"""
Parameters
----------
max_size : int, optional
Maximum size of the output file (in megabytes) (default: 5)
"""
self.max_size = max_size #maximum megabyte filesize allowed
self.PIXEL_LIMIT = self.max_size * 1024 * 1024
return
def _preprocess(self):
"""Loading image from buffer, checking for memory spills, splitting
image channels, and calculating various factors for operation.
"""
self._img = numpy.fromfile(self.file, numpy.uint8)
self._img = cv2.imdecode(self._img, cv2.IMREAD_COLOR)
self._height, self._width, self._channel = self._img.shape
pixel_count = self._height * self._width * self._channel
if pixel_count > self.PIXEL_LIMIT:
self._resize()
self._h_factor = math.ceil(self._height/1000)
self._w_factor = math.ceil(self._width/1000)
self._channel_b = numpy.copy(self._img[:, :, 0])
self._channel_g = numpy.copy(self._img[:, :, 1])
self._channel_r = numpy.copy(self._img[:, :, 2])
self._processed_img = numpy.zeros((self._height, self._width, self._channel))
self._key_length = len(self.key)
return
def _resize(self):
"""Resizing image."""
if self._height > self._width:
ratio = self._height/self._width
self._height = math.sqrt((self.PIXEL_LIMIT * ratio)/3)
self._width = self._height/ratio
else:
ratio = self._width/self._height
self._width = math.sqrt((self.PIXEL_LIMIT * ratio)/3)
self._height = self._width/ratio
self._height = math.floor(self._height)
self._width = math.floor(self._width)
self._img = cv2.resize(self._img, (self._width, self._height))
return
def _scramble(self):
"""Scrambling the channels of the image."""
for i in range(self.cycle):
key_idx = 0
shift_direction = 0
for row in range(self._height):
for ch in range(self._channel):
asciiKey = ord(self.key[key_idx])
roll_unit = asciiKey if shift_direction%2 == 1 else ((-1) * asciiKey)
roll_unit *= self._h_factor
key_idx = (key_idx + 1)%self._key_length
shift_direction += 1
if ch == 0:
self._channel_b[row] = numpy.roll(self._channel_b[row], roll_unit)
elif ch == 1:
self._channel_g[row] = numpy.roll(self._channel_g[row], roll_unit)
else:
self._channel_r[row] = numpy.roll(self._channel_r[row], roll_unit)
key_idx = 0
shift_direction = 0
for column in range(self._width):
for ch in range(self._channel):
asciiKey = ord(self.key[key_idx])
roll_unit = asciiKey if shift_direction%2 == 1 else ((-1) * asciiKey)
roll_unit *= self._w_factor
key_idx = (key_idx + 1)%self._key_length
shift_direction += 1
if ch == 0:
self._channel_b[:, column] = numpy.roll(self._channel_b[:, column], roll_unit)
elif ch == 1:
self._channel_g[:, column] = numpy.roll(self._channel_g[:, column], roll_unit)
else:
self._channel_r[:, column] = numpy.roll(self._channel_r[:, column], roll_unit)
return
def _unscramble(self):
"""Unscrambling the channels of the image."""
for i in range(self.cycle):
key_idx = 0
shift_direction = 0
for column in range(self._width):
for ch in range(self._channel):
asciiKey = ord(self.key[key_idx])
roll_unit = asciiKey if shift_direction%2 == 0 else ((-1) * asciiKey)
roll_unit *= self._w_factor
key_idx = (key_idx + 1)%self._key_length
shift_direction += 1
if ch == 0:
self._channel_b[:, column] = numpy.roll(self._channel_b[:, column], roll_unit)
elif ch == 1:
self._channel_g[:, column] = numpy.roll(self._channel_g[:, column], roll_unit)
else:
self._channel_r[:, column] = numpy.roll(self._channel_r[:, column], roll_unit)
key_idx = 0
shift_direction = 0
for row in range(self._height):
for ch in range(self._channel):
asciiKey = ord(self.key[key_idx])
roll_unit = asciiKey if shift_direction%2 == 0 else ((-1) * asciiKey)
roll_unit *= self._h_factor
key_idx = (key_idx + 1)%self._key_length
shift_direction += 1
if ch == 0:
self._channel_b[row] = numpy.roll(self._channel_b[row], roll_unit)
elif ch == 1:
self._channel_g[row] = numpy.roll(self._channel_g[row], roll_unit)
else:
self._channel_r[row] = numpy.roll(self._channel_r[row], roll_unit)
return
def _result(self):
"""Combines all the scrambled channels into one image matrix, converts
the matrix into a BytesIO buffer and returns it.
Returns
-------
BytesIO Buffer
The image after scramble/unscramble operation stored in a BytesIO
buffer
"""
self._processed_img[..., 0] = self._channel_b
self._processed_img[..., 1] = self._channel_g
self._processed_img[..., 2] = self._channel_r
_, processed_img_buffer = cv2.imencode(".png", self._processed_img)
output_file = BytesIO(processed_img_buffer)
return output_file
def generate(self, mode, cycle, key, file):
"""Runs the pipeline to generate and return the resultant image in the
form of a BytesIO buffer.
Parameters
----------
mode : str
The operation mode chosen (scramble/unscramble)
key : str
The keyphrase used to scramble/unscramble the image
cycle : int
Number of scrambling/unscrambling iterations
file : werkzeug.FileStorage
Incoming file from the flask server for scrambling/unscrambling
Returns
-------
BytesIO Buffer
The image after scramble/unscramble operation stored in a BytesIO
memory buffer
"""
self.mode = mode
self.cycle = cycle
self.key = key
self.file = file
self._preprocess()
if self.mode == "scramble":
self._scramble()
elif self.mode == "unscramble":
self._unscramble()
output_file = self._result()
return output_file