Skip to content

Commit

Permalink
Merge pull request #2 from JuliaWTF/tgf/glitch_file
Browse files Browse the repository at this point in the history
Glitch file directly
  • Loading branch information
theogf authored Jul 13, 2024
2 parents f8bfc3b + 3867579 commit a39ebc2
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 18 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/RunTests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Run tests

on:
push:
branches:
- main
pull_request:

# needed to allow julia-actions/cache to delete old caches that it has created
permissions:
actions: write
contents: read

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
julia-version: ['1.9', '1', 'nightly']
julia-arch:
- x64
os: [ubuntu-latest, windows-latest]

steps:
- uses: actions/checkout@v4
- uses: julia-actions/setup-julia@v1
with:
version: ${{ matrix.julia-version }}
arch: ${{ matrix.julia-arch }}
- uses: julia-actions/cache@v1
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
# with:
# annotate: true
19 changes: 15 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
name = "JpegGlitcher"
uuid = "7e1ea31b-b9a5-4cc0-940e-7c5f7cd080d6"
authors = ["Théo Galy-Fajou <[email protected]> and contributors"]
version = "1.0.0"
authors = [
"JuliaWTF, Théo Galy-Fajou <[email protected]> and contributors",
]
version = "1.1.0"

[deps]
JpegTurbo = "b835a17e-a41a-41e7-81f0-2f016b05efe0"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[weakdeps]
ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"

[extensions]
JpegGlitcherImageIOExt = ["ImageIO", "FileIO"]

[compat]
julia = "1.6"
JpegTurbo = "0.1"
FileIO = "1"
ImageIO = "0.6"
JpegTurbo = "0.1"
julia = "1.9"
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ Here is a basic example, using the default parameters.

```julia
using JpegGlitcher
using Random, FileIO, TestImages
using RandomFileIO, TestImages

img = testimage("mountainview")
img = testimage("mountainstream")
glitch(img)
```

Expand All @@ -25,3 +25,15 @@ cat([glitch(img; rng = Random.Xoshiro(42), n = i, quality = 20) for i in 1:50]..
```

![Low quality animated glitching](assets/glitched_anim.gif)

## Glitch file

You can also directly glitch a file by loading `FileIO.jl` and `ImageIO.jl`.

```julia
using JpegGlitcher, FileIO, ImageIO

glitch("my_beautiful_img.png", "my_glitched_img.png")
```

The glitching will work on any image format supported by `ImageIO`.
33 changes: 33 additions & 0 deletions ext/JpegGlitcherImageIOExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module JpegGlitcherImageIOExt

using ImageIO
using FileIO
using JpegGlitcher
using Random: AbstractRNG, default_rng

"""
glitch(file_in::AbstractString, file_out::AbstractString = auto_glitch_name(file_in); rng::AbstractRNG=default_rng(), nflips::Integer=10, quality::Integer=100)
Glitch image from `file_in` and write the output in `file_out`.
See other method signature for keyword usage.
"""
function JpegGlitcher.glitch(
file_in::AbstractString,
file_out::AbstractString=auto_glitch_name(file_in);
rng::AbstractRNG=default_rng(),
n::Integer=10,
quality::Integer=100,
)
img = FileIO.load(file_in)
glitched_img = glitch(img; rng, n, quality)
FileIO.save(file_out, glitched_img)
end

"Automatically append `_glitched` to the original file name."
function auto_glitch_name(path::String)
path, ext = splitext(path)
string(path, "_glitched", ext)
end

end
12 changes: 6 additions & 6 deletions src/JpegGlitcher.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Within the entropy-coded data, after any 0xFF byte, a 0x00 byte is inserted by t
=#

"""
glitch(img; rng::AbstractRNG=default_rng(), nflips::Integer=10, quality::Integer=100)
glitch(img::AbstractMatrix; rng::AbstractRNG=default_rng(), nflips::Integer=10, quality::Integer=100)
`glitch` will turn an image into a compressed JPEG format with JPEGTurbo.jl and
randomly change some bytes (safe to change) of the encoded image.
Expand All @@ -29,10 +29,10 @@ randomly change some bytes (safe to change) of the encoded image.
- `quality::Integer`: the encoding quality of the JPEG (lower gives worse quality).
"""
function glitch(
img;
rng::AbstractRNG = default_rng(),
n::Integer = 10,
quality::Integer = 100,
img::AbstractMatrix;
rng::AbstractRNG=default_rng(),
n::Integer=10,
quality::Integer=100,
)
0 < quality <= 100 || error("quality should be between 1 and 100.")
n > 0 || error("number `n` should be positive.")
Expand All @@ -53,7 +53,7 @@ function glitch(
elseif data[i] WITH_VARSIZE # The next two bytes indicate the size
# of the data contained by the marker.
high_byte, low_byte = data[i+1:i+2]
size = parse(UInt16, bitstring(high_byte) * bitstring(low_byte); base = 2)
size = parse(UInt16, bitstring(high_byte) * bitstring(low_byte); base=2)
i += size
end
else
Expand Down
8 changes: 8 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
[deps]
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TestImages = "5e47fb64-e119-507b-a336-dd2b206d9990"

[compat]
FileIO = "1"
ImageIO = "0.6"
30 changes: 24 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,42 @@ using JpegGlitcher
using Random: Xoshiro
using Test
using TestImages
using ImageIO, FileIO

@testset "JpegGlitcher.jl" begin
# Test Gray images
for img_name in ("cameraman", "mountainview")
for img_name in ("cameraman", "mountainstream")
@testset "$img_name" begin
img = testimage(img_name)
# test alg runs fine
for kw in ((;), (; quality = 10), (; n = 100))
for kw in ((;), (; quality=10), (; n=100))
glitched = glitch(img)
@test size(img) == size(glitched)
@test eltype(img) == eltype(glitched)
end
# test rng consistency
@test glitch(img; rng = Xoshiro(42)) == glitch(img; rng = Xoshiro(42))
@test glitch(img; rng=Xoshiro(42)) == glitch(img; rng=Xoshiro(42))
# test errors
@test_throws ErrorException glitch(img; quality = -1)
@test_throws ErrorException glitch(img; quality = 101)
@test_throws ErrorException glitch(img; n = -2)
@test_throws ErrorException glitch(img; quality=-1)
@test_throws ErrorException glitch(img; quality=101)
@test_throws ErrorException glitch(img; n=-2)
end
end
@testset "Image in and out" begin
assets = joinpath(pkgdir(JpegGlitcher), "assets")
file_in = joinpath(assets, "glitched.png")
file_out = joinpath(pkgdir(JpegGlitcher), "test", "test_out.png")
file_out_auto = joinpath(assets, "glitched_glitched.png")
try
glitch(file_in, file_out)
@test isfile(file_out)
@test load(file_out) isa Matrix
glitch(file_in)
@test isfile(file_out_auto)
@test load(file_out_auto) isa Matrix
finally
isfile(file_out) && rm(file_out)
isfile(file_out_auto) && rm(file_out_auto)
end
end
end

4 comments on commit a39ebc2

@theogf
Copy link
Member Author

@theogf theogf commented on a39ebc2 Jul 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error while trying to register: Changing package repo URL not allowed, please submit a pull request with the URL change to the target registry and retry.

@theogf
Copy link
Member Author

@theogf theogf commented on a39ebc2 Jul 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/111009

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.1.0 -m "<description of version>" a39ebc2f0682ba9ecd70970776d1f1dcee342493
git push origin v1.1.0

Please sign in to comment.