Skip to content

Commit

Permalink
Faster blur
Browse files Browse the repository at this point in the history
  • Loading branch information
connornishijima committed May 24, 2024
1 parent a180d8b commit 1e683df
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 57 deletions.
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[env:esp32-s3-devkitc-1]
platform = https://github.com/platformio/platform-espressif32.git#develop
platform_packages =
platformio/framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#3.0.0-rc1
platformio/framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#3.0.0-rc3
platformio/framework-arduinoespressif32-libs @ https://github.com/espressif/esp32-arduino-libs.git#idf-release/v5.1
board = esp32-s3-devkitc-1
framework = arduino
Expand Down
2 changes: 1 addition & 1 deletion src/gpu_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void run_gpu() {

//scramble_image( configuration.blur * 50.0 );

apply_fast_blur( configuration.blur * 16.0 );
apply_blur( configuration.blur * 12.0 );

draw_ui_overlay(); // (ui.h)

Expand Down
70 changes: 16 additions & 54 deletions src/leds.h
Original file line number Diff line number Diff line change
Expand Up @@ -589,67 +589,29 @@ void apply_fractional_blur(CRGBF* pixels, uint16_t num_pixels, float kernel_size
}
}

void apply_box_blur(CRGBF* pixels, uint16_t num_pixels, int kernel_size){
memcpy(leds_temp, pixels, sizeof(CRGBF) * num_pixels);
void apply_box_blur( CRGBF* pixels, uint16_t num_pixels, int kernel_size ){
memcpy( leds_temp, pixels, sizeof(CRGBF) * num_pixels );

for(uint16_t i = 0; i < num_pixels; i++){
int16_t kernel_far_left = i - (kernel_size);
int16_t kernel_far_right = i + (kernel_size);
for( uint16_t i = 0; i < num_pixels; i++ ){
int16_t kernel_far_left = i - kernel_size;
int16_t kernel_far_right = i + kernel_size;

if(kernel_far_left < 0){ kernel_far_left = 0; }
if(kernel_far_right >= num_pixels){ kernel_far_right = num_pixels-1; }
int16_t kernel_far_left_clipped = max( (int16_t)0, (int16_t)kernel_far_left );
int16_t kernel_far_right_clipped = min( (int16_t)( num_pixels-1 ), (int16_t)kernel_far_right );

CRGBF sum = {0.0f, 0.0f, 0.0f};
CRGBF sum = { 0.0f, 0.0f, 0.0f };

for(int16_t j = kernel_far_left; j <= kernel_far_right; j++){
sum = add( sum, leds_temp[j] );
int16_t index = max( kernel_far_left_clipped, min( j, kernel_far_right_clipped ) );
sum.r += leds_temp[ index ].r;
sum.g += leds_temp[ index ].g;
sum.b += leds_temp[ index ].b;
}

pixels[i] = sum;
pixels[ i ] = sum;
}

scale_CRGBF_array_by_constant(pixels, 1.0 / (kernel_size*2.0 + 1.0), num_pixels);
}

void apply_box_blur_old(CRGBF* pixels, uint16_t num_pixels, int kernel_size) {
// Ensure kernel size is odd for symmetry around the central pixel
if (kernel_size % 2 == 0) {
kernel_size -= 1;
}

int half_kernel = kernel_size / 2;

// Temporary array to store blurred values
CRGBF temp_pixels[num_pixels];
memset(temp_pixels, 0, sizeof(temp_pixels));

for (int i = 0; i < num_pixels; ++i) {
int valid_kernel_pixels = 0;
CRGBF sum = {0.0f, 0.0f, 0.0f};

// Sum the colors within the kernel's range, handle edges by duplicating pixels
for (int k = -half_kernel; k <= half_kernel; ++k) {
int pixel_index = i + k;

// Handle OOB by duplicating edge pixels
if (pixel_index < 0) pixel_index = 0;
if (pixel_index >= num_pixels) pixel_index = num_pixels - 1;

sum.r += pixels[pixel_index].r;
sum.g += pixels[pixel_index].g;
sum.b += pixels[pixel_index].b;

valid_kernel_pixels++;
}

// Calculate the average and assign it to the temporary array
temp_pixels[i].r = sum.r / valid_kernel_pixels;
temp_pixels[i].g = sum.g / valid_kernel_pixels;
temp_pixels[i].b = sum.b / valid_kernel_pixels;
}

// Copy the blurred values back to the original array
memcpy(pixels, temp_pixels, sizeof(CRGBF) * num_pixels);
scale_CRGBF_array_by_constant( pixels, 1.0 / ( kernel_size*2.0 + 1.0 ), num_pixels );
}

void apply_image_lpf(float cutoff_frequency) {
Expand Down Expand Up @@ -845,14 +807,14 @@ void scramble_image( float distance ){
memcpy(leds, leds_temp, sizeof(CRGBF) * NUM_LEDS);
}

void apply_fast_blur( float kernel_size ){
void apply_blur( float kernel_size ){
if(kernel_size == 0.0){
return;
}

apply_box_blur( leds, NUM_LEDS, kernel_size );

if(kernel_size >= 1.0){
if(kernel_size >= 2.0){
apply_box_blur( leds, NUM_LEDS, kernel_size );
}
}
2 changes: 1 addition & 1 deletion src/wireless.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ void init_web_server() {

// If it's text, it might be a command
if (frame_type == HTTPD_WS_TYPE_TEXT) {
printf("RX: %s\n", (char *)frame->payload);
//printf("RX: %s\n", (char *)frame->payload);
queue_command((char *)frame->payload, frame->len, get_slot_of_client(request->client()));
}
else {
Expand Down

0 comments on commit 1e683df

Please sign in to comment.