Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/filter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ add_subdirectory (gamma)
add_subdirectory (gateweave)
add_subdirectory (glow)
add_subdirectory (glitch0r)
add_subdirectory (harris0r)
#add_subdirectory (host_param_test)
add_subdirectory (heatmap0r)
add_subdirectory (hueshift0r)
Expand Down
11 changes: 11 additions & 0 deletions src/filter/harris0r/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
set (SOURCES harris0r.c)
set (TARGET harris0r)

if (MSVC)
set (SOURCES ${SOURCES} ${FREI0R_DEF})
endif (MSVC)

add_library (${TARGET} MODULE ${SOURCES})
set_target_properties (${TARGET} PROPERTIES PREFIX "")

install (TARGETS ${TARGET} LIBRARY DESTINATION ${LIBDIR})
305 changes: 305 additions & 0 deletions src/filter/harris0r/harris0r.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,305 @@
/* harris0r.c
* Copyright (C) 2026 Marko Deinterlace
* Implements a Harris shutter effect
*
* This file is a Frei0r plugin.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>

#include "frei0r.h"

#ifdef HARRIS_TEST_ALLOCATORS
/* Redirect allocations only after the system headers have been included. */
void *harris_test_malloc(size_t size);
void *harris_test_calloc(size_t count, size_t size);
void *harris_test_realloc(void *ptr, size_t size);
#define malloc harris_test_malloc
#define calloc harris_test_calloc
#define realloc harris_test_realloc
#endif

// Frei0r parameter indices
#define HARRIS_PARAM_DELAY 0
#define HARRIS_PARAM_SIMPLE 1

// Delay boundaries
#define HARRIS_DELAY_MIN 1
#define HARRIS_DELAY_MAX 16 // maximum frames of Harris shutter delay

typedef struct harris0r_instance
{
unsigned int width;
unsigned int height;
size_t size; // precalculated pixel size

// Parameters
short int simple;

Check warning on line 54 in src/filter/harris0r/harris0r.c

View workflow job for this annotation

GitHub Actions / 🚨 C lint

[cpplint] reported by reviewdog 🐶 Use int16_t/int64_t/etc, rather than the C type short [runtime/int] [4] Raw Output: src/filter/harris0r/harris0r.c:54: Use int16_t/int64_t/etc, rather than the C type short [runtime/int] [4]
unsigned int delay;

// Cross-mode properties
uint32_t pos;

// Simple effect properties
uint32_t *cur_frame;

// Smooth effect properties
uint32_t *history;
unsigned int history_frames;

} harris0r_instance_t;

static void harris_mem_init(harris0r_instance_t *inst)
{
inst->pos = 0; // reset position when switching modes
free(inst->cur_frame);
free(inst->history);
inst->cur_frame = NULL;
inst->history = NULL;
inst->history_frames = 0;
// Allocate on update so a failure can produce passthrough and be retried.
}

int f0r_init()
{
return 1;
}

void f0r_deinit()
{ /* no initialization required */ }

void f0r_get_plugin_info(f0r_plugin_info_t* harris0rInfo)
{
harris0rInfo->name = "harris0r";
harris0rInfo->author = "Marko Deinterlace";
harris0rInfo->plugin_type = F0R_PLUGIN_TYPE_FILTER;
harris0rInfo->color_model = F0R_COLOR_MODEL_RGBA8888;
harris0rInfo->frei0r_version = FREI0R_MAJOR_VERSION;
harris0rInfo->major_version = 1;
harris0rInfo->minor_version = 0;
harris0rInfo->num_params = 2;
harris0rInfo->explanation = "Harris shutter effect: dynamic RGB split with a delay";
}

void f0r_get_param_info(f0r_param_info_t* info, int param_index)
{
switch(param_index) {
case HARRIS_PARAM_DELAY:
{
info->name = "Delay";
info->type = F0R_PARAM_DOUBLE;
info->explanation = "Harris shutter color filtering delay";
break;
}

case HARRIS_PARAM_SIMPLE:
{
info->name = "Simple";
info->type = F0R_PARAM_BOOL;
info->explanation = "Use simple effect instead of smooth. Reduces RAM usage, less eye-pleasing";
break;
}
}
}

f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
{
if (width == 0 || height == 0 || (size_t)width > SIZE_MAX / height)
return NULL;
size_t size = (size_t)width * height;
if (size > SIZE_MAX / sizeof(uint32_t)) return NULL;

harris0r_instance_t* inst = (harris0r_instance_t*)calloc(1, sizeof(*inst));
if (inst == NULL) return NULL;
inst->width = width;
inst->height = height;
inst->size = size;

inst->simple = 0; // use smooth implementation by default

inst->delay = 1; // Default delay of 1 frame
inst->pos = 0;

inst->cur_frame = NULL;

inst->history = NULL;
inst->history_frames = 0;

// Frame buffers are allocated on the first update in each mode.

return (f0r_instance_t)inst;
}

void f0r_destruct(f0r_instance_t instance)
{
harris0r_instance_t *inst = (harris0r_instance_t*)instance;

free(inst->cur_frame);
free(inst->history);

free(instance);
}

void f0r_set_param_value(f0r_instance_t instance,
f0r_param_t param, int param_index)
{
assert(instance);
harris0r_instance_t *inst = (harris0r_instance_t*)instance;

switch (param_index)
{
case HARRIS_PARAM_DELAY:
{
double value = *((double*)param);
if (value < 0.0) value = 0.0;
if (value > 1.0) value = 1.0;
inst->delay = HARRIS_DELAY_MIN + (unsigned int)(value * (HARRIS_DELAY_MAX - HARRIS_DELAY_MIN));
break;
}

case HARRIS_PARAM_SIMPLE:
{
short new_simple = *((double*)param) >= 0.5;

Check warning on line 179 in src/filter/harris0r/harris0r.c

View workflow job for this annotation

GitHub Actions / 🚨 C lint

[cpplint] reported by reviewdog 🐶 Use int16_t/int64_t/etc, rather than the C type short [runtime/int] [4] Raw Output: src/filter/harris0r/harris0r.c:179: Use int16_t/int64_t/etc, rather than the C type short [runtime/int] [4]

if (new_simple != inst->simple) {
inst->simple = new_simple;
harris_mem_init(inst); // reallocate memory dynamically. TODO: test with keyframed usage
}

break;
}
}
}

void f0r_get_param_value(f0r_instance_t instance,
f0r_param_t param, int param_index)
{

assert(instance);
harris0r_instance_t *inst = (harris0r_instance_t*)instance;

switch (param_index)
{
case HARRIS_PARAM_DELAY:
{
*((double*)param) = (double)(inst->delay - HARRIS_DELAY_MIN) /
(HARRIS_DELAY_MAX - HARRIS_DELAY_MIN);
break;
}

case HARRIS_PARAM_SIMPLE:
{
*((double*)param) = inst->simple ? 1. : 0.;
break;
}
}

}

inline static int harris_update_simple(harris0r_instance_t *inst, const uint32_t *src, uint32_t *dst)
{
size_t size = inst->size;
if (inst->cur_frame == NULL) {
inst->cur_frame = (uint32_t*)malloc(size * sizeof(uint32_t));
if (inst->cur_frame == NULL) {
memmove(dst, src, size * sizeof(uint32_t));
return 0;
}
}

// First frame: initialize cur_frame with source and copy to destination
if (inst->pos == 0) {
memcpy(inst->cur_frame, src, size * sizeof(uint32_t));
memcpy(dst, inst->cur_frame, size * sizeof(uint32_t));
return 1;
}

unsigned int channel_index = ((inst->pos - 1) / inst->delay) % 3;
uint8_t *cur = (uint8_t*)inst->cur_frame;
const uint8_t *input = (const uint8_t*)src;

for (size_t i = 0; i < size; i++) {
cur[4 * i + channel_index] = input[4 * i + channel_index];
cur[4 * i + 3] = input[4 * i + 3];
}

// Copy processed frame to destination
memcpy(dst, inst->cur_frame, size * sizeof(uint32_t));
return 1;
}

inline static int harris_update_smooth(harris0r_instance_t *inst, const uint32_t *src, uint32_t *dst)
{
unsigned int needed = 2 * inst->delay + 1;
if (inst->history_frames != needed) {

if (inst->size > SIZE_MAX / sizeof(uint32_t) / needed) {
memmove(dst, src, inst->size * sizeof(uint32_t));
return 0;
}
size_t fb_size = (size_t)needed * inst->size * sizeof(uint32_t);

uint32_t *tmp = realloc(inst->history, fb_size);

if (!tmp) {
memmove(dst, src, inst->size * sizeof(uint32_t)); // passthrough
return 0;
}

inst->history = tmp;
inst->history_frames = needed;
inst->pos = 0;
}

unsigned int len = inst->history_frames;
unsigned int head = inst->pos % len; // slot for the current frame

memcpy(inst->history + (size_t)head * inst->size, src,
(size_t)inst->size * sizeof(uint32_t));

unsigned int off_g = (inst->delay < inst->pos) ? inst->delay : inst->pos;
unsigned int off_r = (2 * inst->delay < inst->pos) ? 2 * inst->delay : inst->pos;

const uint8_t *b_frame = (const uint8_t*)(inst->history + (size_t)head * inst->size);
const uint8_t *g_frame = (const uint8_t*)(inst->history + (size_t)((head + len - off_g) % len) * inst->size);
const uint8_t *r_frame = (const uint8_t*)(inst->history + (size_t)((head + len - off_r) % len) * inst->size);
const uint8_t *input = (const uint8_t*)src;
uint8_t *output = (uint8_t*)dst;

for (size_t i = 0; i < inst->size; i++) {
output[4 * i] = r_frame[4 * i]; // red from the oldest frame
output[4 * i + 1] = g_frame[4 * i + 1]; // green from the middle one
output[4 * i + 2] = b_frame[4 * i + 2]; // blue from the current frame
output[4 * i + 3] = input[4 * i + 3]; // current alpha
}
return 1;
}


void f0r_update(f0r_instance_t instance, double time,
const uint32_t* src, uint32_t* dst)
{
assert(instance);
harris0r_instance_t* inst = (harris0r_instance_t*)instance;

int updated = inst->simple ? harris_update_simple(inst, src, dst)
: harris_update_smooth(inst, src, dst);
if (updated) inst->pos++;
}
9 changes: 9 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ add_test(NAME bundle-classification-coverage
add_executable(aech0r-contract aech0r-contract.c
${CMAKE_SOURCE_DIR}/src/filter/aech0r/aech0r.cpp)
add_test(NAME aech0r-contract COMMAND aech0r-contract)
add_library(harris0r-contract-plugin OBJECT
${CMAKE_SOURCE_DIR}/src/filter/harris0r/harris0r.c)
target_compile_definitions(harris0r-contract-plugin PRIVATE HARRIS_TEST_ALLOCATORS)
add_executable(harris0r-contract harris0r-contract.c
$<TARGET_OBJECTS:harris0r-contract-plugin>)
if(NOT MSVC)
target_link_libraries(harris0r-contract PRIVATE m)
endif()
add_test(NAME harris0r-contract COMMAND harris0r-contract)

# This intentionally compiles plugins with private entry point names so that
# the archive/registry path can be exercised without changing MODULE targets.
Expand Down
Loading
Loading