@@ -12,22 +12,28 @@ See [`substitution_encryption`](@ref) and [`substitution_decryption`](@ref) for
12
12
- `image::Array{RGB{N0f8},2}`: A loaded image.
13
13
- `keys::Array{Int64, 1}`: Keys for encryption.
14
14
- `type::Symbol`: Can be `:encrypt` or `:decrypt`.
15
+ - `save_img::Bool=false`: Save the resultant image.
15
16
- `path_for_result::String`: The path for storing the encrypted image.
16
- - `inplace::Boolean`: Perform substitution on the provided image.
17
+ - `inplace::Bool`: Perform substitution on the provided image.
18
+ - `debug::Bool`: Print debug output.
17
19
"""
18
20
function _substitution (
19
21
image:: Union{String,Array{RGB{N0f8},2}} ,
20
22
keys:: Vector{Int64} ,
21
23
type:: Symbol ;
24
+ save_img:: Bool = false ,
22
25
path_for_result:: String = " ./encrypted.png" ,
23
- inplace= false ,
26
+ inplace:: Bool = false ,
27
+ debug:: Bool = false ,
24
28
)
25
29
30
+ debug && @info " Loading image from path"
26
31
if typeof (image) == String
27
32
image = load (image)
28
33
end
29
34
30
35
# Generating dimensions of the image
36
+ debug && @info " Generating dimensions"
31
37
height = size (image)[1 ]
32
38
width = size (image)[2 ]
33
39
@@ -36,33 +42,41 @@ function _substitution(
36
42
end
37
43
38
44
# generate a copy if not inplace
45
+ debug && " Generating a copy of the image"
39
46
~ inplace && (image = copy (image))
40
47
41
- if type == :encrypt
42
- @info " ENCRYPTING"
43
- else
44
- @info " DECRYPTING"
48
+ if debug
49
+ if type == :encrypt
50
+ @info " ENCRYPTING"
51
+ elseif type == :decrypt
52
+ @info " DECRYPTING"
53
+ end
45
54
end
46
55
47
56
# reshape keys for broadcasting
57
+ debug && @info " Reshaping keys for broadcasting"
48
58
keys = reshape (keys, height, width)
49
59
50
60
# substitute all pixels in one go
51
- @. image = _substitute_pixel (image, keys)
52
-
53
- if type == :encrypt
54
- @info " ENCRYPTED"
55
- else
56
- @info " DECRYPTED"
61
+ debug && @info " Substituting all pixels"
62
+ @. image = _substitute_pixel! (image, keys)
63
+
64
+ if debug
65
+ if type == :encrypt
66
+ @info " ENCRYPTED"
67
+ elseif type == :decrypt
68
+ @info " DECRYPTED"
69
+ end
57
70
end
58
71
59
- save (path_for_result, image)
72
+ debug && @info " Saving result"
73
+ save_img && save (path_for_result, image)
60
74
image
61
75
end
62
76
63
77
64
78
"""
65
- _substitute_pixel(pixel::RGB, key::Int64)
79
+ _substitute_pixel! (pixel::RGB, key::Int64)
66
80
67
81
Returns the pixel after XORing the R, G, and B values with the key.
68
82
Specifically developed to return an `Array` (or the complete image)
@@ -77,7 +91,7 @@ See [`_substitution`](@ref) for more details.
77
91
# Returns
78
92
- `pixel::RGB`: Substituted pixel.
79
93
"""
80
- _substitute_pixel (pixel:: RGB , key:: Int64 ) = RGB (
94
+ _substitute_pixel! (pixel:: RGB , key:: Int64 ) = RGB (
81
95
(trunc (Int, pixel. r * 255 ) ⊻ key) / 255 ,
82
96
(trunc (Int, pixel. g * 255 ) ⊻ key) / 255 ,
83
97
(trunc (Int, pixel. b * 255 ) ⊻ key) / 255
@@ -96,7 +110,9 @@ Iterates simulataneously over each pixel and key, and XORs the pixel value
96
110
# Arguments
97
111
- `image::Array{RGB{N0f8},2}`: A loaded image.
98
112
- `keys::Array{Int64, 1}`: Keys for encryption.
113
+ - `save_img::Bool=false`: Save the resultant image.
99
114
- `path_for_result::String`: The path for storing the encrypted image.
115
+ - `debug::Bool`: Print debug output.
100
116
101
117
# Returns
102
118
- `image::Array{RGB{N0f8}, 2}`: Encrypted image.
@@ -116,8 +132,6 @@ julia> keys |> size
116
132
(262144,)
117
133
118
134
julia> enc = substitution_encryption(img, keys);
119
- [ Info: ENCRYPTING
120
- [ Info: ENCRYPTED
121
135
122
136
julia> enc |> size
123
137
(512, 512)
@@ -129,8 +143,17 @@ true
129
143
substitution_encryption (
130
144
image:: Array{RGB{N0f8},2} ,
131
145
keys:: Vector{Int64} ;
132
- path_for_result:: String = " ./encrypted.png"
133
- ) = _substitution (image, keys, :encrypt ; path_for_result= path_for_result)
146
+ save_img:: Bool = false ,
147
+ path_for_result:: String = " ./encrypted.png" ,
148
+ debug:: Bool = false ,
149
+ ) = _substitution (
150
+ image,
151
+ keys,
152
+ :encrypt ;
153
+ save_img= save_img,
154
+ path_for_result= path_for_result,
155
+ debug= debug,
156
+ )
134
157
135
158
136
159
"""
@@ -145,7 +168,9 @@ Iterates simulataneously over each pixel and key, and XORs the pixel value
145
168
# Arguments
146
169
- `image::Array{RGB{N0f8},2}`: A loaded image.
147
170
- `keys::Array{Int64, 1}`: Keys for encryption.
171
+ - `save_img::Bool=false`: Save the resultant image.
148
172
- `path_for_result::String`: The path for storing the encrypted image.
173
+ - `debug::Bool`: Print debug output.
149
174
150
175
# Returns
151
176
- `image::Array{RGB{N0f8}, 2}`: Encrypted image.
@@ -167,8 +192,6 @@ julia> keys |> size
167
192
julia> orig = copy(img);
168
193
169
194
julia> substitution_encryption!(img, keys);
170
- [ Info: ENCRYPTING
171
- [ Info: ENCRYPTED
172
195
173
196
julia> img != orig # inplace
174
197
true
@@ -177,8 +200,18 @@ true
177
200
substitution_encryption! (
178
201
image:: Array{RGB{N0f8},2} ,
179
202
keys:: Vector{Int64} ;
180
- path_for_result:: String = " ./encrypted.png"
181
- ) = _substitution (image, keys, :encrypt ; path_for_result= path_for_result, inplace= true )
203
+ save_img:: Bool = false ,
204
+ path_for_result:: String = " ./encrypted.png" ,
205
+ debug:: Bool = false ,
206
+ ) = _substitution (
207
+ image,
208
+ keys,
209
+ :encrypt ;
210
+ save_img= save_img,
211
+ path_for_result= path_for_result,
212
+ inplace= true ,
213
+ debug= false ,
214
+ )
182
215
183
216
184
217
"""
@@ -194,7 +227,9 @@ as the ones provided during encryption.
194
227
# Arguments
195
228
- `image::Union{String,Array{RGB{N0f8},2}}`: The path to the image or the loaded image to be decrypted.
196
229
- `keys::Array{Int64, 1}`: Keys for decryption.
197
- - `path_for_result::String`: The path for storing the decrypted image.
230
+ - `save_img::Bool=false`: Save the resultant image.
231
+ - `path_for_result::String`: The path for storing the encrypted image.
232
+ - `debug::Bool`: Print debug output.
198
233
199
234
# Returns
200
235
- `image::Array{RGB{N0f8}, 2}`: Decrypted image.
@@ -214,8 +249,6 @@ julia> keys |> size
214
249
(262144,)
215
250
216
251
julia> dec = substitution_decryption(img, keys);
217
- [ Info: DECRYPTING
218
- [ Info: DECRYPTED
219
252
220
253
julia> dec |> size
221
254
(512, 512)
@@ -227,8 +260,17 @@ true
227
260
substitution_decryption (
228
261
image:: Union{String,Array{RGB{N0f8},2}} ,
229
262
keys:: Vector{Int64} ;
263
+ save_img:: Bool = false ,
230
264
path_for_result:: String = " ./decrypted.png" ,
231
- ) = _substitution (image, keys, :decrypt ; path_for_result= path_for_result)
265
+ debug:: Bool = false
266
+ ) = _substitution (
267
+ image,
268
+ keys,
269
+ :decrypt ;
270
+ save_img= save_img,
271
+ path_for_result= path_for_result,
272
+ debug= debug,
273
+ )
232
274
233
275
234
276
"""
@@ -244,7 +286,9 @@ as the ones provided during encryption.
244
286
# Arguments
245
287
- `image::Union{String,Array{RGB{N0f8},2}}`: The path to the image or the loaded image to be decrypted.
246
288
- `keys::Array{Int64, 1}`: Keys for decryption.
247
- - `path_for_result::String`: The path for storing the decrypted image.
289
+ - `save_img::Bool=false`: Save the resultant image.
290
+ - `path_for_result::String`: The path for storing the encrypted image.
291
+ - `debug::Bool`: Print debug output.
248
292
249
293
# Returns
250
294
- `image::Array{RGB{N0f8}, 2}`: Decrypted image.
@@ -266,8 +310,6 @@ julia> keys |> size
266
310
julia> orig = copy(img);
267
311
268
312
julia> substitution_decryption!(img, keys);
269
- [ Info: DECRYPTING
270
- [ Info: DECRYPTED
271
313
272
314
julia> img != orig # inplace
273
315
true
@@ -276,5 +318,15 @@ true
276
318
substitution_decryption! (
277
319
image:: Array{RGB{N0f8},2} ,
278
320
keys:: Vector{Int64} ;
321
+ save_img:: Bool = false ,
279
322
path_for_result:: String = " ./decrypted.png" ,
280
- ) = _substitution (image, keys, :decrypt ; path_for_result= path_for_result, inplace= true )
323
+ debug:: Bool = false ,
324
+ ) = _substitution (
325
+ image,
326
+ keys,
327
+ :decrypt ;
328
+ save_img= save_img,
329
+ path_for_result= path_for_result,
330
+ inplace= true ,
331
+ debug= debug,
332
+ )
0 commit comments