Skip to content

Commit

Permalink
minor fixes (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC authored Jan 1, 2022
1 parent 577645a commit a17847d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 32 deletions.
57 changes: 26 additions & 31 deletions src/QOI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ using FixedPointNumbers
using ColorTypes
using FileIO


#############
# Constants #
#############
Expand Down Expand Up @@ -37,17 +38,34 @@ end
@inline _qoi_color_hash(p::Pixel) = p.r*3 + p.g*5 + p.b*7 + p.a*11


##############
# Exceptions #
##############

struct QOIException <: Exception
msg::String
end
Base.showerror(io::IO, qoi::QOIException) = print(io, qoi.msg)

@noinline throw_magic_bytes_error(magic::UInt32) = throw(QOIException("invalid magic bytes, got $(repr(magic)), expected $(repr(QOI_MAGIC))"))
@noinline throw_invalid_header_width(width::UInt32) = throw(QOIException("invalid width in header, got $width"))
@noinline throw_invalid_header_height(height::UInt32) = throw(QOIException("invalid height in header, got $height"))
@noinline throw_invalid_header_channels(channels::UInt8) = throw(QOIException("invalid channels in header, got $channels"))
@noinline throw_invalid_header_colorspace(colorspace::UInt8) = throw(QOIException("invalid colorspace in header, got $colorspace"))
@noinline throw_unexpected_eof() = throw(QOIException("unexpected end of file"))


#############
# QOIHeader #
#############

@enum QOIChannel::UInt8 begin
QOI_RGB = 0x03
QOI_RGB = 0x03
QOI_RGBA = 0x04
end

@enum QOIColorSpace::UInt8 begin
QOI_SRGB = 0x00
QOI_SRGB = 0x00
QOI_LINEAR = 0x01
end

Expand All @@ -58,37 +76,14 @@ struct QOIHeader
colorspace::QOIColorSpace
end
function QOIHeader(width::UInt32, height::UInt32, channels::UInt8, colorspace::UInt8)
if (width == 0)
throw_invalid_header_width(width)
elseif (height == 0)
throw_invalid_header_height(height)
elseif channels < 3 || channels > 4
throw_invalid_header_channels(channels)
elseif colorspace > 1
throw_invalid_header_colorspace(colorspace)
end
width == 0 && throw_invalid_header_width(width)
height == 0 && throw_invalid_header_height(height)
channels < 3 || channels > 4 && throw_invalid_header_channels(channels)
colorspace > 1 && throw_invalid_header_colorspace(colorspace)
return QOIHeader(width, height, QOIChannel(channels), QOIColorSpace(colorspace))
end


##############
# Exceptions #
##############

struct QOIException <: Exception
msg::String
end
Base.showerror(io::IO, qoi::QOIException) = print(io, qoi.msg)

@noinline throw_magic_bytes_error(magic::UInt32) =
throw(QOIException("invalid magic bytes, got $(repr(magic)), expected $(repr(QOI_MAGIC))"))
@noinline throw_invalid_header_width(width::UInt32) = throw(QOIException("invalid width in header, got $width"))
@noinline throw_invalid_header_height(height::UInt32) = throw(QOIException("invalid height in header, got $height"))
@noinline throw_invalid_header_channels(channels::UInt8) = throw(QOIException("invalid channels in header, got $channels"))
@noinline throw_invalid_header_colorspace(colorspace::UInt8) = throw(QOIException("invalid colorspace in header, got $colorspace"))
@noinline throw_unexpected_eof() = throw(QOIException("unexpected end of file"))


############
# Encoding #
############
Expand Down Expand Up @@ -122,7 +117,7 @@ function qoi_encode_raw(io::IO, image::AbstractVecOrMat{UInt8}, header::QOIHeade
end

function qoi_encode_raw!(data::AbstractVector{UInt8}, image::AbstractVecOrMat{UInt8}, header::QOIHeader)
# error Check
# TODO: Check size of data against QOI_PIXELS_MAX?

qoiw = QOIWriter(data)

Expand Down Expand Up @@ -204,7 +199,7 @@ function qoi_encode_raw!(data::AbstractVector{UInt8}, image::AbstractVecOrMat{UI
# Padding
for x in QOI_PADDING
_qoi_write!(qoiw, x)
end
end

sizehint!(data, qoiw.pos)
resize!(data, qoiw.pos)
Expand Down
19 changes: 18 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ using p7zip_jll
using ImageIO
using FileIO
using Scratch
using ColorTypes
using FixedPointNumbers

const DOWNLOAD_SHA = "869a6433a3af7ce84fc55fda6a5387d6c2113c3e8231153549a6407ed1e71696"

Expand Down Expand Up @@ -49,7 +51,19 @@ for file_name in Set(getindex.(splitext.(readdir(testset; join=true)), 1))
@test read(t) == read(qoi)
end

f_logo = joinpath(@__DIR__, "qoi_logo.qoi")
# Test saving imges not in N0f8 format
img_gray = rand(Gray{N0f8}, 5, 5)
t = tempname()
QOI.qoi_encode(t, img_gray)
img_qoi = QOI.qoi_decode(t)
@test convert(Matrix{RGB{N0f8}}, img_gray) == img_qoi

img_24bit = rand(RGBA{Float64}, 5, 5)
t = tempname()
QOI.qoi_encode(t, img_24bit)
img_qoi = QOI.qoi_decode(t)
@test convert(Matrix{RGBA{N0f8}}, img_24bit) == img_qoi


# Invalid images

Expand Down Expand Up @@ -103,6 +117,9 @@ write(io, hton.(UInt32.([10, 5])))
write(io, hton.(UInt8.([3, 1])))
@test_throws QOI.QOIException("invalid magic bytes, got 0x716f697a, expected 0x716f6966") QOI.qoi_decode_raw(take!(io))


const f_logo = joinpath(@__DIR__, "qoi_logo.qoi")

# Test IO API
data_io = open(QOI.qoi_decode, f_logo)
data_file = QOI.qoi_decode(f_logo)
Expand Down

0 comments on commit a17847d

Please sign in to comment.