From 2844addf484585770074c2065a90ceaaaee24c76 Mon Sep 17 00:00:00 2001 From: Igor Zinken <730069+igorski@users.noreply.github.com> Date: Sat, 1 Aug 2026 12:59:51 +0200 Subject: [PATCH] Set missing LICENSE header to white balance process --- src/rendering/filters/white-balance.ts | 38 ++++++++++++++++++++------ 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/src/rendering/filters/white-balance.ts b/src/rendering/filters/white-balance.ts index e7531df..dd592dd 100644 --- a/src/rendering/filters/white-balance.ts +++ b/src/rendering/filters/white-balance.ts @@ -1,33 +1,53 @@ +/** + * The MIT License (MIT) + * + * Igor Zinken 2026 - https://www.igorski.nl + * + * 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. + */ export const applyWhiteBalance = ( pixels: Uint8ClampedArray ): void => { const totalPixels = pixels.length / 4; let totalR = 0, totalG = 0, totalB = 0; - // Step 1: Calculate the sum of all RGB values + // calculate the sum of all RGB values for ( let i = 0, len = pixels.length; i < len; i += 4) { totalR += pixels[ i ]; totalG += pixels[ i + 1 ]; totalB += pixels[ i + 2 ]; } - // Step 2: Find the average value for each channel + // find the average value for each channel const avgR = totalR / totalPixels; const avgG = totalG / totalPixels; const avgB = totalB / totalPixels; - // Step 3: Calculate the overall average brightness + // calculate the overall average brightness const avgGray = ( avgR + avgG + avgB ) / 3; - // Step 4: Calculate scaling factors for each channel + // apply scaling factors per channel const scaleR = avgGray / avgR; const scaleG = avgGray / avgG; const scaleB = avgGray / avgB; - // Step 5: Apply the scaling factors to every pixel for ( let i = 0, len = pixels.length; i < len; i += 4 ) { - pixels[i] = Math.min(255, pixels[i] * scaleR); // New Red - pixels[i + 1] = Math.min(255, pixels[i + 1] * scaleG); // New Green - pixels[i + 2] = Math.min(255, pixels[i + 2] * scaleB); // New Blue - // data[i+3] is Alpha (transparency), leave it unchanged + pixels[ i ] = Math.min( 255, pixels[ i ] * scaleR ); + pixels[ i + 1 ] = Math.min( 255, pixels[ i + 1 ] * scaleG ); + pixels[ i + 2 ] = Math.min( 255, pixels[ i + 2 ] * scaleB ); } };