Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode Operation #6

Merged
merged 8 commits into from
Oct 2, 2022
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ColorVectorSpace = "c3611d14-8923-5661-9e6a-0046d554d3a4"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
Giflib_jll = "59f7168a-df46-5410-90c8-f2779963d0ec"
Noise = "81d43f40-5267-43b7-ae1c-8b967f377efa"
libgifextra_jll = "d7c3d077-fdb7-5318-9663-3e6817e0e281"

[compat]
Expand Down
9 changes: 7 additions & 2 deletions src/GIFImages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ include("../lib/LibGifExtra.jl")

using .LibGif
using .LibGifExtra
using ColorTypes
using ColorVectorSpace
using FixedPointNumbers
using FileIO
using Noise

include("decode.jl")
include("encode.jl")

export gif_decode

export gif_decode, gif_encode

end # module
4 changes: 0 additions & 4 deletions src/decode.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using ColorTypes
using ColorVectorSpace
using FixedPointNumbers
using FileIO

"""
gif_decode(filepath::AbstractString; use_localpalette=false)
Expand Down
53 changes: 53 additions & 0 deletions src/encode.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

function gif_encode(filepath::AbstractString, img::AbstractArray, numcolors::Int=256)
Error = Cint(0)
gifFile = LibGif.EGifOpenFileName(filepath,0, Ref(Error));
colors = []

if (gifFile == C_NULL)
println("EGifOpenFileName() failed - ");
return false;
end

# generation of a colormap
img = quantization(img, num)
append!(colors, unique(img))
ashwani-rathee marked this conversation as resolved.
Show resolved Hide resolved
mapping = Dict()
for (i,j) in enumerate(colors)
mapping[j] = UInt8(i)
end

# defining the global colormap
colors = map(x->LibGif.GifColorType(x.r,x.g,x.b), colors*255)
ColorMap = LibGif.GifMakeMapObject(num, colors)

# features of the file
gifFile.SWidth = size(img)[2];
gifFile.SHeight = size(img)[1];
gifFile.SColorResolution = 8;
gifFile.SBackGroundColor = 0;
gifFile.SColorMap = ColorMap

# creating the new mapping
new = zeros(UInt8, size(img))
for (i,j) in enumerate(img)
new[i] = mapping[j]
end

# encoding
for i in 1:size(img)[3]
# flatten the image
pix = [new[:,:,i]'...]
pix1 = pointer(map(x-> LibGif.GifByteType(x), convert(Vector{UInt8}, pix)))

# saving a new image in giffile
desc = LibGif.GifImageDesc(0,0,size(img)[2],size(img)[1],0, C_NULL)
c = LibGif.SavedImage(desc, pix1, 0, C_NULL)
LibGif.GifMakeSavedImage(gifFile, Ref(c))
end

# writing and closing the file
if(LibGif.EGifSpew(gifFile) == LibGif.GIF_ERROR)
error("Failed to write to file!")
end
end