Skip to content
This repository has been archived by the owner on Mar 26, 2023. It is now read-only.

Add Night Vision Filter to Filter list #263

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion api/iOS/VCSimpleSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ typedef NS_ENUM(NSInteger, VCFilter) {
VCFilterInvertColors,
VCFilterSepia,
VCFilterFisheye,
VCFilterGlow
VCFilterGlow,
VCFilterNightVision
};

@protocol VCSessionDelegate <NSObject>
Expand Down
3 changes: 3 additions & 0 deletions api/iOS/VCSimpleSession.mm
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,9 @@ - (void)setFilter:(VCFilter)filterToChange {
case VCFilterGlow:
filterName = @"com.videocore.filters.glow";
break;
case VCFilterNightVision:
filterName = @"com.videocore.filters.nightVision";
break;
default:
break;
}
Expand Down
125 changes: 125 additions & 0 deletions filters/Basic/NightVisionVideoFilter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@

#include <videocore/filters/Basic/NightVisionVideoFilter.h>

#include <TargetConditionals.h>


#ifdef TARGET_OS_IPHONE

#include <OpenGLES/ES2/gl.h>
#include <OpenGLES/ES3/gl.h>
#include <videocore/sources/iOS/GLESUtil.h>
#include <videocore/filters/FilterFactory.h>

#endif

namespace videocore { namespace filters {

bool NightVisionVideoFilter::s_registered = NightVisionVideoFilter::registerFilter();

bool
NightVisionVideoFilter::registerFilter()
{
FilterFactory::_register("com.videocore.filters.nightVision", []() { return new NightVisionVideoFilter(); });
return true;
}

NightVisionVideoFilter::NightVisionVideoFilter()
: IVideoFilter(), m_initialized(false), m_bound(false)
{

}
NightVisionVideoFilter::~NightVisionVideoFilter()
{
glDeleteProgram(m_program);
glDeleteVertexArrays(1, &m_vao);
}

const char * const
NightVisionVideoFilter::vertexKernel() const
{

KERNEL(GL_ES2_3, m_language,
attribute vec2 aPos;
attribute vec2 aCoord;
varying vec2 vCoord;
uniform mat4 uMat;
void main(void) {
gl_Position = uMat * vec4(aPos,0.,1.);
vCoord = aCoord;
}
)

return nullptr;
}

const char * const
NightVisionVideoFilter::pixelKernel() const
{

KERNEL(GL_ES2_3, m_language,
precision mediump float;
varying vec2 vCoord;
uniform sampler2D uTex0;
const vec3 NVIS = vec3(0.6, 1.6, 0.6);
void main(void) {
vec4 color = texture2D(uTex0, vCoord);
float gray = dot(color.rgb, vec3(0.3, 0.59, 0.11));
vec3 nvisColor = vec3(gray) * NVIS;
color.rgb = mix(color.rgb, nvisColor, 0.75);
gl_FragColor = color;
} )

return nullptr;
}
void
NightVisionVideoFilter::initialize()
{
switch(m_language) {
case GL_ES2_3:
case GL_2: {
setProgram(build_program(vertexKernel(), pixelKernel()));
glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);
m_uMatrix = glGetUniformLocation(m_program, "uMat");
int attrpos = glGetAttribLocation(m_program, "aPos");
int attrtex = glGetAttribLocation(m_program, "aCoord");
int unitex = glGetUniformLocation(m_program, "uTex0");
glUniform1i(unitex, 0);
glEnableVertexAttribArray(attrpos);
glEnableVertexAttribArray(attrtex);
glVertexAttribPointer(attrpos, BUFFER_SIZE_POSITION, GL_FLOAT, GL_FALSE, BUFFER_STRIDE, BUFFER_OFFSET_POSITION);
glVertexAttribPointer(attrtex, BUFFER_SIZE_POSITION, GL_FLOAT, GL_FALSE, BUFFER_STRIDE, BUFFER_OFFSET_TEXTURE);
m_initialized = true;
}
break;
case GL_3:
break;
}
}
void
NightVisionVideoFilter::bind()
{
switch(m_language) {
case GL_ES2_3:
case GL_2:
if(!m_bound) {
if(!initialized()) {
initialize();
}
glUseProgram(m_program);
glBindVertexArray(m_vao);
}
glUniformMatrix4fv(m_uMatrix, 1, GL_FALSE, &m_matrix[0][0]);
break;
case GL_3:
break;
}
}
void
NightVisionVideoFilter::unbind()
{
m_bound = false;
}
}
}
63 changes: 63 additions & 0 deletions filters/Basic/NightVisionVideoFilter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*

Video Core
Copyright (c) 2014 James G. Hurley

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/
#ifndef videocore_NightVisionVideoFilter_h
#define videocore_NightVisionVideoFilter_h
#include <videocore/filters/IVideoFilter.hpp>

namespace videocore {
namespace filters {
class NightVisionVideoFilter : public IVideoFilter {

public:
NightVisionVideoFilter();
~NightVisionVideoFilter();

public:
virtual void initialize();
virtual bool initialized() const { return m_initialized; };
virtual std::string const name() { return "com.videocore.filters.nightVision"; };
virtual void bind();
virtual void unbind();

public:

const char * const vertexKernel() const ;
const char * const pixelKernel() const ;

private:
static bool registerFilter();
static bool s_registered;
private:

unsigned int m_vao;
unsigned int m_uMatrix;
bool m_initialized;
bool m_bound;

};
}
}

#endif /* defined(videocore_NightVisionVideoFilter_h) */
2 changes: 2 additions & 0 deletions filters/FilterFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <videocore/filters/Basic/SepiaVideoFilter.h>
#include <videocore/filters/Basic/FisheyeVideoFilter.h>
#include <videocore/filters/Basic/GlowVideoFilter.h>
#include <videocore/filters/Basic/NightVisionVideoFilter.h>

namespace videocore {
std::map<std::string, InstantiateFilter>* FilterFactory::s_registration = nullptr ;
Expand All @@ -17,6 +18,7 @@ namespace videocore {
filters::SepiaVideoFilter s;
filters::FisheyeVideoFilter f;
filters::GlowVideoFilter gl;
filters::NightVisionVideoFilter n;
}
}
IFilter*
Expand Down