Skip to content

Commit d8871d2

Browse files
authored
Introduce save_img, debug, and add ! to _substitute_pixel (#83)
* Introduce `save_img`, `debug`, and add `!` to `_substitute_pixel` * CHANGELOG * Make everything strict except `:missing docs` * Codacy
1 parent c0e5233 commit d8871d2

File tree

6 files changed

+126
-50
lines changed

6 files changed

+126
-50
lines changed

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
# Unreleased
22

3+
## Features
4+
5+
- Introduced `save_img` argument in encryption/decryption functions to manually save the resultant image ([#83](https://github.com/Saransh-cpp/ChaoticEncryption.jl/pull/83))
6+
- Introduced `debug` argument in encryption/decryption functions to print debug statements ([#83](https://github.com/Saransh-cpp/ChaoticEncryption.jl/pull/83))
7+
8+
## Bug fixes
9+
10+
- Renamed `_substitute_pixel` to `_substitute_pixel!` to show the inplace alterations ([#83](https://github.com/Saransh-cpp/ChaoticEncryption.jl/pull/83))
11+
12+
## Optimisations
13+
14+
- Added `Bool` type to the `inplace` variable ([#83](https://github.com/Saransh-cpp/ChaoticEncryption.jl/pull/83))
15+
16+
## Documentation
17+
18+
- Made everything strict except `:missing docs` ([#83](https://github.com/Saransh-cpp/ChaoticEncryption.jl/pull/83))
19+
320
# ChaoticEncryption v0.3.2
421

522
## Optimisations

docs/make.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ makedocs(
99
format = Documenter.HTML(sidebar_sitename=false),
1010
modules = [ChaoticEncryption, Images, TestImages],
1111
doctest = true,
12-
strict = :doctest,
12+
strict = Documenter.except(:missing_docs),
1313
pages = [
1414
"Home" => "index.md",
1515
"Tutorials" => [

docs/src/apidocs/algorithms.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@
77
```@docs
88
substitution_encryption(
99
image::Array{RGB{N0f8},2},
10-
keys::Array{Int64, 1};
11-
path_for_result::String="./encrypted.png"
10+
keys::Vector{Int64};
11+
save_img::Bool=false,
12+
path_for_result::String="./encrypted.png",
13+
debug::Bool=false,
1214
)
1315
```
1416

1517
```@docs
1618
substitution_encryption!(
1719
image::Array{RGB{N0f8},2},
18-
keys::Array{Int64, 1};
19-
path_for_result::String="./encrypted.png"
20+
keys::Vector{Int64};
21+
save_img::Bool=false,
22+
path_for_result::String="./encrypted.png",
23+
debug::Bool=false,
2024
)
2125
```
2226

@@ -25,15 +29,19 @@ substitution_encryption!(
2529
```@docs
2630
substitution_decryption(
2731
image::Union{String,Array{RGB{N0f8},2}},
28-
keys::Array{Int64, 1};
29-
path_for_result::String="./decrypted.png"
32+
keys::Vector{Int64};
33+
save_img::Bool=false,
34+
path_for_result::String="./decrypted.png",
35+
debug::Bool=false
3036
)
3137
```
3238

3339
```@docs
3440
substitution_decryption!(
3541
image::Array{RGB{N0f8},2},
36-
keys::Array{Int64, 1};
37-
path_for_result::String="./decrypted.png"
42+
keys::Vector{Int64};
43+
save_img::Bool=false,
44+
path_for_result::String="./decrypted.png",
45+
debug::Bool=false,
3846
)
3947
```

docs/src/devdocs/algorithms.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ ChaoticEncryption._substitution(
1010
image::Union{String,Array{RGB{N0f8},2}},
1111
keys::Vector{Int64},
1212
type::Symbol;
13+
save::Bool=false,
1314
path_for_result::String="./encrypted.png",
14-
inplace=false,
15+
inplace::Bool=false,
16+
debug::Bool=false
1517
)
16-
```
17-
18-
```@docs
19-
ChaoticEncryption._substitute_pixel(
18+
ChaoticEncryption._substitute_pixel!(
2019
pixel::RGB,
2120
key::Int64
2221
)

src/substitution.jl

Lines changed: 83 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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
"""
1820
function _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
6175
end
6276

6377

6478
"""
65-
_substitute_pixel(pixel::RGB, key::Int64)
79+
_substitute_pixel!(pixel::RGB, key::Int64)
6680
6781
Returns the pixel after XORing the R, G, and B values with the key.
6882
Specifically 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
118134
julia> enc = substitution_encryption(img, keys);
119-
[ Info: ENCRYPTING
120-
[ Info: ENCRYPTED
121135
122136
julia> enc |> size
123137
(512, 512)
@@ -129,8 +143,17 @@ true
129143
substitution_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
167192
julia> orig = copy(img);
168193
169194
julia> substitution_encryption!(img, keys);
170-
[ Info: ENCRYPTING
171-
[ Info: ENCRYPTED
172195
173196
julia> img != orig # inplace
174197
true
@@ -177,8 +200,18 @@ true
177200
substitution_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
216251
julia> dec = substitution_decryption(img, keys);
217-
[ Info: DECRYPTING
218-
[ Info: DECRYPTED
219252
220253
julia> dec |> size
221254
(512, 512)
@@ -227,8 +260,17 @@ true
227260
substitution_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
266310
julia> orig = copy(img);
267311
268312
julia> substitution_decryption!(img, keys);
269-
[ Info: DECRYPTING
270-
[ Info: DECRYPTED
271313
272314
julia> img != orig # inplace
273315
true
@@ -276,5 +318,15 @@ true
276318
substitution_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+
)

test/test_chaotic_encryption.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ using Test
2222
height, width = size(img)
2323
keys = logistic_key(0.01, 3.97, height * width)
2424

25-
substitution_encryption(img, keys; path_for_result="../test_images/encrypted.png")
25+
substitution_encryption(img, keys; save_img=true, path_for_result="../test_images/encrypted.png", debug=true)
2626
@test isfile("../test_images/encrypted.png")
2727

2828
keys = logistic_key(0.01, 3.97, 20)
@@ -34,7 +34,7 @@ using Test
3434
height, width = size(img)
3535
keys = logistic_key(0.01, 3.97, height * width)
3636

37-
enc = substitution_encryption!(img, keys; path_for_result="../test_images/encrypted.png")
37+
enc = substitution_encryption!(img, keys; save_img=true, path_for_result="../test_images/encrypted.png", debug=true)
3838
@test isfile("../test_images/encrypted.png")
3939
@test img == enc # inplace
4040
end
@@ -44,10 +44,10 @@ using Test
4444
height, width = size(img)
4545
keys = logistic_key(0.01, 3.97, height * width)
4646

47-
substitution_decryption("../test_images/encrypted.png", keys; path_for_result="../test_images/decrypted.png")
47+
substitution_decryption("../test_images/encrypted.png", keys; save_img=true, path_for_result="../test_images/decrypted.png", debug=true)
4848
@test isfile("../test_images/decrypted.png")
4949

50-
substitution_decryption(img, keys; path_for_result="../test_images/decrypted.png")
50+
substitution_decryption(img, keys; save_img=true, path_for_result="../test_images/decrypted.png", debug=true)
5151
@test isfile("../test_images/decrypted.png")
5252

5353
keys = logistic_key(0.01, 3.97, 20)
@@ -62,7 +62,7 @@ using Test
6262
height, width = size(img)
6363
keys = logistic_key(0.01, 3.97, height * width)
6464

65-
dec = substitution_decryption!(img, keys; path_for_result="../test_images/decrypted.png")
65+
dec = substitution_decryption!(img, keys; save_img=true, path_for_result="../test_images/decrypted.png", debug=true)
6666
@test isfile("../test_images/decrypted.png")
6767
@test img == dec # inplace
6868

0 commit comments

Comments
 (0)