Skip to content

Commit

Permalink
Merge pull request #92 from kmsquire/teh/newimages
Browse files Browse the repository at this point in the history
Update to the new Images
  • Loading branch information
kmsquire committed Feb 22, 2017
2 parents 9e96af2 + 1531d08 commit e019929
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 70 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
deps/*
!deps/build.jl
.DS_Store
videos/*
10 changes: 6 additions & 4 deletions .travis.yml
Expand Up @@ -18,11 +18,13 @@ os:
- linux
# - osx
julia:
- 0.4
- 0.5
- nightly
notifications:
email: false
# uncomment the following lines to override the default test script
#script:
# - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
# - julia -e 'Pkg.clone(pwd()); Pkg.build("VideoIO2"); Pkg.test("VideoIO2"; coverage=true)'
script:
- julia -e 'Pkg.clone(pwd())'
- julia -e 'Pkg.build("VideoIO")'
# coverage=true causes tests to hang, see pull request #92
- if [ -f test/runtests.jl ]; then julia -e 'Pkg.test("VideoIO", coverage=false)'; fi
10 changes: 6 additions & 4 deletions REQUIRE
@@ -1,7 +1,9 @@
julia 0.4
Compat 0.8.7
julia 0.5
BinDeps 0.3.4
Images 0.4.4
ImageView 0.1.4
FixedPointNumbers 0.3.0
ColorTypes
FileIO
ImageCore
ImageView 0.3.0
@osx Homebrew
@linux Glob
4 changes: 3 additions & 1 deletion src/VideoIO.jl
@@ -1,6 +1,8 @@
__precompile__(false)

module VideoIO

using Compat
using FixedPointNumbers, ColorTypes, ImageCore

include("init.jl")

Expand Down
56 changes: 15 additions & 41 deletions src/avio.jl
Expand Up @@ -13,17 +13,12 @@ end

abstract StreamContext

if isdefined(Main, :FixedPointNumbers)
UFixed8 = Main.FixedPointNumbers.UFixed8
end
typealias EightBitTypes Union{UInt8, N0f8, Main.ColorTypes.RGB{N0f8}}
typealias PermutedArray{T,N,perm,iperm,AA<:Array} Base.PermutedDimsArrays.PermutedDimsArray{T,N,perm,iperm,AA}
typealias VidArray{T,N} Union{Array{T,N},PermutedArray{T,N}}

if isdefined(Main, :ColorTypes)
typealias EightBitTypes Union{UInt8, UFixed8, Main.ColorTypes.RGB{UFixed8}}
elseif isdefined(Main, :FixedPointNumbers)
typealias EightBitTypes Union{UInt8, UFixed8}
else
typealias EightBitTypes UInt8
end
# TODO: move this to Base
Base.unsafe_convert{T}(::Type{Ptr{T}}, A::PermutedArray{T}) = Base.unsafe_convert(Ptr{T}, parent(A))

# An audio-visual input stream/file
type AVInput{I}
Expand Down Expand Up @@ -405,9 +400,9 @@ function retrieve(r::VideoReader{TRANSCODE}) # true=transcode
end

if t.target_bits_per_pixel == 8
buf = Array(UInt8, r.width, r.height)
buf = permuteddimsview(Matrix{Gray{N0f8}}(r.width, r.height), (2,1))
else
buf = Array(UInt8, t.target_bits_per_pixel>>3, r.width, r.height)
buf = permuteddimsview(Matrix{RGB{N0f8}}(r.width, r.height), (2,1))
end

retrieve!(r, buf)
Expand All @@ -428,7 +423,7 @@ function retrieve(r::VideoReader{NO_TRANSCODE}) # false=don't transcode
end

# Converts a grabbed frame to the correct format (RGB by default)
function retrieve!{T<:EightBitTypes}(r::VideoReader{TRANSCODE}, buf::Array{T})
function retrieve!{T<:EightBitTypes}(r::VideoReader{TRANSCODE}, buf::VidArray{T})
while !have_frame(r)
idx = pump(r.avin)
idx == r.stream_index0 && break
Expand Down Expand Up @@ -471,7 +466,7 @@ function retrieve!{T<:EightBitTypes}(r::VideoReader{TRANSCODE}, buf::Array{T})
return buf
end

function retrieve!{T<:EightBitTypes}(r::VideoReader{NO_TRANSCODE}, buf::Array{T})
function retrieve!{T<:EightBitTypes}(r::VideoReader{NO_TRANSCODE}, buf::VidArray{T})
while !have_frame(r)
idx = pump(r.avin)
idx == r.stream_index0 && break
Expand All @@ -496,9 +491,9 @@ isopen{I<:IO}(avin::AVInput{I}) = isopen(avin.io)
isopen(avin::AVInput) = avin.isopen
isopen(r::VideoReader) = isopen(r.avin)

bufsize_check{T<:EightBitTypes}(r::VideoReader{NO_TRANSCODE}, buf::Array{T}) = (length(buf)*sizeof(T) == avpicture_get_size(r.format, r.width, r.height))
bufsize_check{T<:EightBitTypes}(r::VideoReader{TRANSCODE}, buf::Array{T}) = bufsize_check(r.transcodeContext, buf)
bufsize_check{T<:EightBitTypes}(t::VideoTranscodeContext, buf::Array{T}) = (length(buf)*sizeof(T) == avpicture_get_size(t.target_pix_fmt, t.width, t.height))
bufsize_check{T<:EightBitTypes}(r::VideoReader{NO_TRANSCODE}, buf::VidArray{T}) = (length(buf)*sizeof(T) == avpicture_get_size(r.format, r.width, r.height))
bufsize_check{T<:EightBitTypes}(r::VideoReader{TRANSCODE}, buf::VidArray{T}) = bufsize_check(r.transcodeContext, buf)
bufsize_check{T<:EightBitTypes}(t::VideoTranscodeContext, buf::VidArray{T}) = (length(buf)*sizeof(T) == avpicture_get_size(t.target_pix_fmt, t.width, t.height))

have_decoded_frame(r) = r.aFrameFinished[1] > 0 # TODO: make sure the last frame was made available
have_frame(r::StreamContext) = !isempty(r.frame_queue) || have_decoded_frame(r)
Expand Down Expand Up @@ -667,39 +662,18 @@ if have_avdevice()
end
end

try
if isa(Main.Images, Module)
# Define read and retrieve for Images
global retrieve, retrieve!, read!, read
for r in [:read, :retrieve]
r! = Symbol("$(r)!")

@eval begin
# read!, retrieve!
$r!(c::VideoReader, img::Main.Images.Image) = ($r!(c, Main.Images.data(img)); img)

# read, retrieve
function $r(c::VideoReader, ::Type{Main.Images.Image}, colorspace="RGB", colordim=1, spatialorder=["x","y"])
img = Main.Images.colorim($r(c::VideoReader))
end
end
end
end
end

try
if isa(Main.ImageView, Module)
# Define read and retrieve for Images
global playvideo, viewcam, play

function play(f, flip=false)
img = read(f, Main.Images.Image)
canvas, _ = Main.ImageView.view(img, flipx=flip, interactive=false)
buf = Main.Images.data(img)
buf = read(f)
canvas, _ = Main.ImageView.imshow(buf, flipx=flip, interactive=false)

while !eof(f)
read!(f, buf)
Main.ImageView.view(canvas, img, flipx=flip, interactive=false)
Main.ImageView.imshow(canvas, buf, flipx=flip, interactive=false)
sleep(1/f.framerate)
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/testvideos.jl
Expand Up @@ -46,7 +46,7 @@ const videofiles = Dict(
"pubic_domain (US)",
"",
"http://commons.wikimedia.org/wiki/File:Annie_Oakley_shooting_glass_balls,_1894.ogg",
"http://upload.wikimedia.org/wikipedia/commons/d/dd/Annie_Oakley_shooting_glass_balls%2C_1894.ogg"),
"https://upload.wikimedia.org/wikipedia/commons/8/87/Annie_Oakley_shooting_glass_balls%2C_1894.ogv"),

"crescent-moon.ogv" => VideoFile("crescent-moon.ogv",
"Moonset (time-lapse).",
Expand Down
34 changes: 15 additions & 19 deletions test/avio.jl
@@ -1,8 +1,6 @@
using Base.Test
using Compat
using FixedPointNumbers
using ColorTypes, FileIO, ImageCore

import Images: Image
import VideoIO

testdir = dirname(@__FILE__)
Expand All @@ -15,31 +13,25 @@ swapext(f, new_ext) = "$(splitext(f)[1])$new_ext"

println(STDERR, "Testing file reading...")

if !isdefined(Main, :UFixed8)
UFixed8 = Ufixed8
end

function notblank(img)
all(Images.green(img) .== 0x00uf8) || all(Images.blue(img) .== 0x00uf8) || all(Images.red(img) .== 0x00uf8) || maximum(reinterpret(UFixed8, img)) < 0xcfuf8
end

if isdefined(Images, :load)
imload = Images.load
else
imload = Images.imread
@noinline function notblank(img)
all(c->green(c) == 0, img) || all(c->blue(c) == 0, img) || all(c->red(c) == 0, img) || maximum(rawview(channelview(img))) < 0xcf
end

for name in VideoIO.TestVideos.names()
is_apple() && startswith(name, "crescent") && continue
println(STDERR, " Testing $name...")

first_frame_file = joinpath(testdir, swapext(name, ".png"))
first_frame = imload(first_frame_file) # comment line when creating png files
first_frame = load(first_frame_file) # comment line when creating png files

f = VideoIO.testvideo(name)
v = VideoIO.openvideo(f)

img = read(v, Image)
if size(first_frame, 1) > v.height
first_frame = first_frame[1+size(first_frame,1)-v.height:end,:]
end

img = read(v)

# Find the first non-trivial image
while notblank(img)
Expand Down Expand Up @@ -76,12 +68,16 @@ for name in VideoIO.TestVideos.names()

println(STDERR, " Testing $name...")
first_frame_file = joinpath(testdir, swapext(name, ".png"))
first_frame = imload(first_frame_file) # comment line when creating png files
first_frame = load(first_frame_file) # comment line when creating png files

filename = joinpath(videodir, name)
v = VideoIO.openvideo(open(filename))

img = read(v, Image)
if size(first_frame, 1) > v.height
first_frame = first_frame[1+size(first_frame,1)-v.height:end,:]
end

img = read(v)

# Find the first non-trivial image
while notblank(img)
Expand Down

0 comments on commit e019929

Please sign in to comment.