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

Fix one Col-major case. Make 1 bpp images always [0 1] #1645

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/lepto_funs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function img2pix(mat::Matrix{<:Integer}, bpp=8; layout="TRBa")::Sppix
@assert isa(layout, String) && length(layout) >= 3
img2pix(mat2img(mat, layout=layout), bpp=bpp)
end
function img2pix(I::GMTimage{<:Integer, 2}, bpp=8)::Sppix # Minimalist. Still doesn't have a colormap and not yet RGB(A)
function img2pix(I::GMTimage, bpp=8)::Sppix # Minimalist. Still doesn't have a colormap and not yet RGB(A)
# The Leptonoica library has (in fact it almost hasn't) an awfull documentation.
# I was able, however, to find a way to create and fill a Pix object with a matrix. The trick is to
# create a Pix object with the right size and then fill it with the matrix data. The pixSetupByteProcessing()
Expand All @@ -39,7 +39,7 @@ function img2pix(I::GMTimage{<:Integer, 2}, bpp=8)::Sppix # Minimalist. Still d
for j = 1:w[] line[j] = UInt8(I.image[i,j]) end
end
end
else # bpp == 1
else # bpp == 1
resto = rem(width, 8)
cols_iter = (resto == 0) ? (1:_width) : (1:(_width-1))
if (I.layout[2] == 'R')
Expand All @@ -57,7 +57,7 @@ function img2pix(I::GMTimage{<:Integer, 2}, bpp=8)::Sppix # Minimalist. Still d
end
end
end
else
else # Column major
for i = 1:h[]
line = unsafe_wrap(Array, lineptrs[i], w[])
for j = cols_iter
Expand All @@ -67,7 +67,7 @@ function img2pix(I::GMTimage{<:Integer, 2}, bpp=8)::Sppix # Minimalist. Still d
end
if (resto != 0)
for n = resto-1:-1:0
(I.image[i,j+n+1] != 0) && (line[_width] = line[_width] | UInt8(1) << UInt8(n))
(I.image[i,width] != 0) && (line[_width] = line[_width] | UInt8(1) << UInt8(n))
end
end
end
Expand Down Expand Up @@ -104,7 +104,7 @@ function pix2img(ppix::Sppix)::GMTimage
if (bpp == 1)
# https://www.geeksforgeeks.org/extract-bits-in-c/
mat = zeros(UInt8, width, height) # We are storing the image as "TRBa"
u255 = UInt8(255)
u1 = UInt8(1)
masks8 = [1,2,4,8,16,32,64,128]
k, m = 1, 0
resto = rem(width, 8)
Expand All @@ -114,15 +114,14 @@ function pix2img(ppix::Sppix)::GMTimage
for j = cols_iter
t = r[m+=1]
for n = 7:-1:0
#((t & u1) == u1) && (mat[k] = u255); t = t >> u1
(((t & masks8[n+1]) >> n) == 1) && (mat[k] = u255)
(((t & masks8[n+1]) >> n) == 1) && (mat[k] = u1)
k += 1
end
end
if (resto != 0)
t = r[m+=1]
for n = resto-1:-1:0
(((t & masks8[n+1]) >> n) == 1) && (mat[k] = u255)
(((t & masks8[n+1]) >> n) == 1) && (mat[k] = u1)
k += 1
end
end
Expand Down Expand Up @@ -799,6 +798,7 @@ end
# =====================================================================================================
function helper_morph(I, hsize, vsize, tipo, sel)
bpp = (eltype(I) == Bool || I.range[6] == 1) ? 1 : 8
(bpp == 8 && I.range[6] == 255 && I.n_colors == 0 && rem(sum(I.image), 255) == 0) && (bpp = 1) # A bit risky but not much
(tipo in ["hitmiss", "perim", "skell"] && bpp != 1) && error("'bwhitmiss' or 'bwperim' are only available for binary images")
nosel = (tipo in ["skell"]) # List of 1 bpp that do not use 'sel'
(bpp == 1 && !nosel && sel === nothing) && (sel = strel("box", hsize, vsize))
Expand Down
Loading