@@ -12,22 +12,28 @@ See [`substitution_encryption`](@ref) and [`substitution_decryption`](@ref) for
1212- `image::Array{RGB{N0f8},2}`: A loaded image.
1313- `keys::Array{Int64, 1}`: Keys for encryption.
1414- `type::Symbol`: Can be `:encrypt` or `:decrypt`.
15+ - `save_img::Bool=false`: Save the resultant image.
1516- `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.
1719"""
1820function _substitution (
1921 image:: Union{String,Array{RGB{N0f8},2}} ,
2022 keys:: Vector{Int64} ,
2123 type:: Symbol ;
24+ save_img:: Bool = false ,
2225 path_for_result:: String = " ./encrypted.png" ,
23- inplace= false ,
26+ inplace:: Bool = false ,
27+ debug:: Bool = false ,
2428)
2529
30+ debug && @info " Loading image from path"
2631 if typeof (image) == String
2732 image = load (image)
2833 end
2934
3035 # Generating dimensions of the image
36+ debug && @info " Generating dimensions"
3137 height = size (image)[1 ]
3238 width = size (image)[2 ]
3339
@@ -36,33 +42,41 @@ function _substitution(
3642 end
3743
3844 # generate a copy if not inplace
45+ debug && " Generating a copy of the image"
3946 ~ inplace && (image = copy (image))
4047
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
4554 end
4655
4756 # reshape keys for broadcasting
57+ debug && @info " Reshaping keys for broadcasting"
4858 keys = reshape (keys, height, width)
4959
5060 # 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
5770 end
5871
59- save (path_for_result, image)
72+ debug && @info " Saving result"
73+ save_img && save (path_for_result, image)
6074 image
6175end
6276
6377
6478"""
65- _substitute_pixel(pixel::RGB, key::Int64)
79+ _substitute_pixel! (pixel::RGB, key::Int64)
6680
6781Returns the pixel after XORing the R, G, and B values with the key.
6882Specifically developed to return an `Array` (or the complete image)
@@ -77,7 +91,7 @@ See [`_substitution`](@ref) for more details.
7791# Returns
7892- `pixel::RGB`: Substituted pixel.
7993"""
80- _substitute_pixel (pixel:: RGB , key:: Int64 ) = RGB (
94+ _substitute_pixel! (pixel:: RGB , key:: Int64 ) = RGB (
8195 (trunc (Int, pixel. r * 255 ) ⊻ key) / 255 ,
8296 (trunc (Int, pixel. g * 255 ) ⊻ key) / 255 ,
8397 (trunc (Int, pixel. b * 255 ) ⊻ key) / 255
@@ -96,7 +110,9 @@ Iterates simulataneously over each pixel and key, and XORs the pixel value
96110# Arguments
97111- `image::Array{RGB{N0f8},2}`: A loaded image.
98112- `keys::Array{Int64, 1}`: Keys for encryption.
113+ - `save_img::Bool=false`: Save the resultant image.
99114- `path_for_result::String`: The path for storing the encrypted image.
115+ - `debug::Bool`: Print debug output.
100116
101117# Returns
102118- `image::Array{RGB{N0f8}, 2}`: Encrypted image.
@@ -116,8 +132,6 @@ julia> keys |> size
116132(262144,)
117133
118134julia> enc = substitution_encryption(img, keys);
119- [ Info: ENCRYPTING
120- [ Info: ENCRYPTED
121135
122136julia> enc |> size
123137(512, 512)
@@ -129,8 +143,17 @@ true
129143substitution_encryption (
130144 image:: Array{RGB{N0f8},2} ,
131145 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+ )
134157
135158
136159"""
@@ -145,7 +168,9 @@ Iterates simulataneously over each pixel and key, and XORs the pixel value
145168# Arguments
146169- `image::Array{RGB{N0f8},2}`: A loaded image.
147170- `keys::Array{Int64, 1}`: Keys for encryption.
171+ - `save_img::Bool=false`: Save the resultant image.
148172- `path_for_result::String`: The path for storing the encrypted image.
173+ - `debug::Bool`: Print debug output.
149174
150175# Returns
151176- `image::Array{RGB{N0f8}, 2}`: Encrypted image.
@@ -167,8 +192,6 @@ julia> keys |> size
167192julia> orig = copy(img);
168193
169194julia> substitution_encryption!(img, keys);
170- [ Info: ENCRYPTING
171- [ Info: ENCRYPTED
172195
173196julia> img != orig # inplace
174197true
@@ -177,8 +200,18 @@ true
177200substitution_encryption! (
178201 image:: Array{RGB{N0f8},2} ,
179202 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+ )
182215
183216
184217"""
@@ -194,7 +227,9 @@ as the ones provided during encryption.
194227# Arguments
195228- `image::Union{String,Array{RGB{N0f8},2}}`: The path to the image or the loaded image to be decrypted.
196229- `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.
198233
199234# Returns
200235- `image::Array{RGB{N0f8}, 2}`: Decrypted image.
@@ -214,8 +249,6 @@ julia> keys |> size
214249(262144,)
215250
216251julia> dec = substitution_decryption(img, keys);
217- [ Info: DECRYPTING
218- [ Info: DECRYPTED
219252
220253julia> dec |> size
221254(512, 512)
@@ -227,8 +260,17 @@ true
227260substitution_decryption (
228261 image:: Union{String,Array{RGB{N0f8},2}} ,
229262 keys:: Vector{Int64} ;
263+ save_img:: Bool = false ,
230264 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+ )
232274
233275
234276"""
@@ -244,7 +286,9 @@ as the ones provided during encryption.
244286# Arguments
245287- `image::Union{String,Array{RGB{N0f8},2}}`: The path to the image or the loaded image to be decrypted.
246288- `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.
248292
249293# Returns
250294- `image::Array{RGB{N0f8}, 2}`: Decrypted image.
@@ -266,8 +310,6 @@ julia> keys |> size
266310julia> orig = copy(img);
267311
268312julia> substitution_decryption!(img, keys);
269- [ Info: DECRYPTING
270- [ Info: DECRYPTED
271313
272314julia> img != orig # inplace
273315true
@@ -276,5 +318,15 @@ true
276318substitution_decryption! (
277319 image:: Array{RGB{N0f8},2} ,
278320 keys:: Vector{Int64} ;
321+ save_img:: Bool = false ,
279322 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