Skip to content

Commit

Permalink
Version v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jaakkor2 committed Oct 23, 2024
1 parent c437fd1 commit a947558
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/Manifest.toml
13 changes: 13 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name = "WykoOPDReader"
uuid = "6da23c8c-444c-4310-aaa6-9150b66cb193"
authors = ["Jaakko Ruohio <jaakkor2@gmail.com>"]
version = "0.1.0"

[compat]
julia = "1.10"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# WykoOPDReader

[![Build Status](https://github.com/jaakkor2/WykoOPDReader.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/jaakkor2/WykoOPDReader.jl/actions/workflows/CI.yml?query=branch%3Amain)

Reader for Wyko profilometer datasets (.opd) written in Julia language.

```julia
using WykoOPDReader
data = readopd("profile.opd")
```
13 changes: 13 additions & 0 deletions src/WykoOPDReader.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""
WykoOPDReader
Reader for WYKO data sets (.opd) implemented in Julia.
Exports `readopd`.
"""
module WykoOPDReader

export readopd

include("reader.jl")

end
78 changes: 78 additions & 0 deletions src/reader.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""
readopd(fn)
Read Wyko data set from file `fn`.
# Example
```julia
using WykoOPDReader
fn = joinpath(pathof(WykoOPDReader), "..", "..", "test", "demo.opd")
data = readopd(fn)
using GLMakie
colormap = [:NavyBlue, :royalblue3, :LightGreen, :Red]
nan_color = :black
autolimitaspect = data["Aspect"]
fig = Figure()
ax = Axis(fig[1,1]; autolimitaspect)
hm = heatmap!(ax, data["RAW_DATA"]; colormap, nan_color)
Colorbar(fig[1,2], hm, width = Relative(1/10))
```
"""
function readopd(fn)
io = open(fn, "r")
magic = read(io, 2)
magic == [0x01, 0x00] || error("not .opd file?")

entries = []
data = Dict{String, Any}()
push!(entries, readentry(io))
n_entries = entries[1].len ÷ 24
for i = 2:n_entries
push!(entries, readentry(io))
end
for i = 2:n_entries
(; name, type, len) = entries[i]
if name == ""
continue
elseif type == 3
push!(data, name => readimage(io))
elseif type == 5
push!(data, name => readstring(io, len))
elseif entries[i].type == 6
push!(data, name => readint(io, len))
elseif entries[i].type == 7
push!(data, name => readfloat(io, len))
else
@show "This is unknown: ", name, type, len
end
end
close(io)
return data
end

function readentry(io)
name = rstrip(String(read(io, 16)), '\0')
type = reinterpret(UInt16, read(io, 2))[1]
len = reinterpret(UInt32, read(io, 4))[1]
dunno = reinterpret(UInt16, read(io, 2))[1]
(; name, type, len, dunno)
end

function readimage(io)
width, height, bytewidth = Int.(reinterpret(Int16, read(io, 6)))
data = read(io, width * height * bytewidth)
T = bytewidth == 4 ? Float32 : bytewidth == 2 ? Int16 : UInt8
data = reinterpret(T, data)
data = reshape(data, (height, width))
if !(T <: Integer)
data[data .>= floatmax(T) / 2] .= NaN
end
data = rotr90(data, 1)
data = reverse(data, dims = 2)
end

readstring(io, n) = rstrip(String(read(io, n)), '\0')
readfloat(io, n) = reinterpret(n == 4 ? Float32 : Float64, read(io, n))[1]
readint(io, n) = reinterpret(n == 4 ? Int32 : n == 2 ? Int16 : Int8, read(io, n))[1]
Binary file added test/demo.opd
Binary file not shown.
Binary file added test/raw_integer.opd
Binary file not shown.
23 changes: 23 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using WykoOPDReader
using Test

@testset "WykoOPDReader.jl" begin
data = readopd("demo.opd")
@test data["Pixel_size"] == 0.01f0
@test data["Aspect"] == 1.5
@test data["RAW_DATA"] == Float32[
-3.0 -3.0 -3.0 -3.0
-2.0 2.0 14.0 1.0
-1.0 2.0 3.0 2.0
0.0 2.0 2.0 2.0
1.0 2.0 2.0 2.0]

data = readopd("raw_integer.opd")
@test data["Title"] == "WykoOPDReader.jl"
@test data["Note"] == "Julia"
@test data["Mult"] == 32000
@test data["RAW_DATA"] == Int16[
0 128
192 319
-448 -703]
end

0 comments on commit a947558

Please sign in to comment.