Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
arttu76 committed Dec 3, 2023
1 parent 9d4b54f commit 7e304b7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 33 deletions.
10 changes: 3 additions & 7 deletions src/store/windowPropertyMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { AnyAction, Dispatch, MiddlewareAPI } from '@reduxjs/toolkit';
import * as R from "ramda";
import { Color, Keys, Layer, Nullable, PartialRgbImage, PixelationSource, Rgb, ToolType } from "../types";
import { Color, DitheringErrorBuffer, Keys, Layer, Nullable, PartialRgbImage, PixelationSource, Rgb, ToolType } from "../types";
import { edgeEnhance, gaussianBlur, getColorAdjusted, getInverted, sharpen } from "../utils/colors";
import { computeAttributeBlockColor, isDitheredPixelSet } from "../utils/dithering";
import { initializeLayerContext } from "../utils/layerContextManager";
import { isMaskSet } from "../utils/maskManager";
import { applyRange2DExclusive, getInitialized2DArray, getSourceRgb, getWindow, rangeExclusive } from "../utils/utils";
import { repaint } from "./housekeepingSlice";
Expand Down Expand Up @@ -153,10 +152,7 @@ const updateSpectrumPixelsAndAttributesIfRequired = () => {
win[Keys.adjustedSpectrumPixels][layer.id] = getInitialized2DArray<Nullable<boolean>>(192, 256, null);
}

const ditheringContext = initializeLayerContext(
layer,
state.tools.tool
);
const ditheringErrorBuffer: DitheringErrorBuffer = getInitialized2DArray(192 + 2, 256 + 2, 0);

applyRange2DExclusive(192, 256, (y, x) => {
const attrX = Math.floor(x / 8);
Expand Down Expand Up @@ -191,7 +187,7 @@ const updateSpectrumPixelsAndAttributesIfRequired = () => {

win[Keys.adjustedSpectrumPixels][layer.id][y][x] = (
attribute // if there is no attribute, no use checking if pixel is to be dithered or not
? isDitheredPixelSet(ditheringContext, x, y)
? isDitheredPixelSet(layer, ditheringErrorBuffer, x, y)
: null
);

Expand Down
24 changes: 14 additions & 10 deletions src/utils/dithering.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Color, DitheringErrorBuffer, Keys, Layer, Nullable, PatternCache, Percentage, PixelationSource, PixelationType, Rgb, SpectrumPixelCoordinate } from '../types';
import { getInkIntensity, getIntensity, getIntensityDifference, getPaperIntensity, spectrumColor } from './colors';
import { LayerContext } from './layerContextManager';
import { applyRange2DExclusive, getWindow } from './utils';

export const getColorDistance = (a: Rgb, b: Rgb): number => {
Expand Down Expand Up @@ -82,28 +81,33 @@ const pattern = (source: Rgb, x: number, y: number, layerPatternCache: PatternCa
}
}

export const isDitheredPixelSet = (ctx: LayerContext, x: SpectrumPixelCoordinate, y: SpectrumPixelCoordinate): Nullable<boolean> => {
export const isDitheredPixelSet = (
layer: Layer,
ditheringErrorBuffer: DitheringErrorBuffer,
x: SpectrumPixelCoordinate,
y: SpectrumPixelCoordinate
): Nullable<boolean> => {

const win = getWindow();
const sourceRgb = win[Keys.adjustedPixels][ctx.layer.id][y][x];
const targetAttribute = win[Keys.adjustedSpectrumAttributes][ctx.layer.id][Math.floor(y / 8)][Math.floor(x / 8)]
const sourceRgb = win[Keys.adjustedPixels][layer.id][y][x];
const targetAttribute = win[Keys.adjustedSpectrumAttributes][layer.id][Math.floor(y / 8)][Math.floor(x / 8)]
if (sourceRgb === null || targetAttribute! === null) {
return null;
}

if (ctx.layer.pixelate === PixelationType.simple) {
if (layer.pixelate === PixelationType.simple) {
return simple(sourceRgb, targetAttribute);
}
if (ctx.layer.pixelate === PixelationType.noise) {
if (layer.pixelate === PixelationType.noise) {
return noise(sourceRgb, x, y, targetAttribute);
}

if (ctx.layer.pixelate === PixelationType.floydsteinberg) {
return floydsteinberg(sourceRgb, x, y, ctx.ditheringErrorBuffer, targetAttribute);
if (layer.pixelate === PixelationType.floydsteinberg) {
return floydsteinberg(sourceRgb, x, y, ditheringErrorBuffer, targetAttribute);
}

if (ctx.layer.pixelate === PixelationType.pattern) {
return pattern(sourceRgb, x, y, win[Keys.patternCache][ctx.layer.id], targetAttribute);
if (layer.pixelate === PixelationType.pattern) {
return pattern(sourceRgb, x, y, win[Keys.patternCache][layer.id], targetAttribute);
}

return null;
Expand Down
16 changes: 0 additions & 16 deletions src/utils/layerContextManager.ts

This file was deleted.

0 comments on commit 7e304b7

Please sign in to comment.