Skip to content

Commit

Permalink
separate function for changing all 3 (R,G,B) mapping arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
zcernigoj committed May 18, 2020
1 parent 0b690cd commit 38203e8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
16 changes: 12 additions & 4 deletions src/mapDataManipulation/mapDataManipulationUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ export function prepareRgbMappingArrays(): RgbMappingArrays {
};
}

export function changeRgbMappingArraysWithFunction(
rgbMappingArrays: RgbMappingArrays,
transfrormationFunction: Function,
) {
rgbMappingArrays.red = rgbMappingArrays.red.map(x => transfrormationFunction(x));
rgbMappingArrays.green = rgbMappingArrays.green.map(x => transfrormationFunction(x));
rgbMappingArrays.blue = rgbMappingArrays.blue.map(x => transfrormationFunction(x));
return rgbMappingArrays;
}

// from one interval to another
// f(x) = c + ((d - c) / (b - a)) * (x - a)
// a = oldMin, b = oldMax; c = newMin, d = newMax
Expand All @@ -31,13 +41,11 @@ export function changeRgbMappingArraysInterval(
return newX;
};

rgbMappingArrays.red = rgbMappingArrays.red.map(x => transformValueToInterval(x));
rgbMappingArrays.green = rgbMappingArrays.green.map(x => transformValueToInterval(x));
rgbMappingArrays.blue = rgbMappingArrays.blue.map(x => transformValueToInterval(x));
rgbMappingArrays = changeRgbMappingArraysWithFunction(rgbMappingArrays, transformValueToInterval);
return rgbMappingArrays;
}

export function prepareManipulatePixel(rgbMappingArrays: RgbMappingArrays): any {
export function prepareManipulatePixel(rgbMappingArrays: RgbMappingArrays): Function {
return function(r: number, g: number, b: number, a: number): object {
return { r: rgbMappingArrays.red[r], g: rgbMappingArrays.green[g], b: rgbMappingArrays.blue[b], a };
};
Expand Down
15 changes: 5 additions & 10 deletions src/mapDataManipulation/predefinedEffectFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PredefinedEffects, RgbMappingArrays } from 'src/mapDataManipulation/const';
import { changeRgbMappingArraysWithFunction } from 'src/mapDataManipulation/mapDataManipulationUtils';

export function gainEffectFunction(
export function runGainEffectFunction(
rgbMappingArrays: RgbMappingArrays,
predefinedEffects: PredefinedEffects,
): RgbMappingArrays {
Expand All @@ -13,14 +14,11 @@ export function gainEffectFunction(
offset = offset - factor * minValue;

const transformValueWithGain = (x: number): number => Math.max(0.0, x * factor + offset);
rgbMappingArrays.red = rgbMappingArrays.red.map(x => transformValueWithGain(x));
rgbMappingArrays.green = rgbMappingArrays.green.map(x => transformValueWithGain(x));
rgbMappingArrays.blue = rgbMappingArrays.blue.map(x => transformValueWithGain(x));

rgbMappingArrays = changeRgbMappingArraysWithFunction(rgbMappingArrays, transformValueWithGain);
return rgbMappingArrays;
}

export function gammaEffectFunction(
export function runGammaEffectFunction(
rgbMappingArrays: RgbMappingArrays,
predefinedEffects: PredefinedEffects,
): RgbMappingArrays {
Expand All @@ -29,10 +27,7 @@ export function gammaEffectFunction(

if (gamma != 1.0) {
const transformValueWithGamma = (x: number): number => Math.pow(x, gamma);
rgbMappingArrays.red = rgbMappingArrays.red.map(x => transformValueWithGamma(x));
rgbMappingArrays.green = rgbMappingArrays.green.map(x => transformValueWithGamma(x));
rgbMappingArrays.blue = rgbMappingArrays.blue.map(x => transformValueWithGamma(x));
rgbMappingArrays = changeRgbMappingArraysWithFunction(rgbMappingArrays, transformValueWithGamma);
}

return rgbMappingArrays;
}
9 changes: 6 additions & 3 deletions src/mapDataManipulation/runPredefinedEffectFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import {
changeRgbMappingArraysInterval,
prepareManipulatePixel,
} from 'src/mapDataManipulation/mapDataManipulationUtils';
import { gainEffectFunction, gammaEffectFunction } from 'src/mapDataManipulation/predefinedEffectFunctions';
import {
runGainEffectFunction,
runGammaEffectFunction,
} from 'src/mapDataManipulation/predefinedEffectFunctions';

// The algorithm works with numbers between 0 and 1, so we must:
// - change the interval of the values from [0, 255] to [0, 1]
Expand All @@ -22,10 +25,10 @@ export async function runPredefinedEffectFunctions(
rgbMappingArrays = changeRgbMappingArraysInterval(rgbMappingArrays, 0, 255, 0, 1);

// change the values according to the algorithm (gain)
rgbMappingArrays = gainEffectFunction(rgbMappingArrays, predefinedEffects);
rgbMappingArrays = runGainEffectFunction(rgbMappingArrays, predefinedEffects);

// change the values according to the algorithm (gamma)
rgbMappingArrays = gammaEffectFunction(rgbMappingArrays, predefinedEffects);
rgbMappingArrays = runGammaEffectFunction(rgbMappingArrays, predefinedEffects);

// change the interval of the values from [0, 1] back to [0, 255], strictly limit values to the interval
const limitValuesToInterval = true;
Expand Down

0 comments on commit 38203e8

Please sign in to comment.