Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#819 Toogle alpha channel reduction #1401

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions src/features/processors/quantize/client/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { h, Component } from 'preact';
import { Options as QuantizeOptions } from '../shared/meta';
import * as style from 'client/lazy-app/Compress/Options/style.css';
import {
inputFieldChecked,
inputFieldValueAsNumber,
konami,
preventDefault,
} from 'client/lazy-app/util';
import Expander from 'client/lazy-app/Compress/Options/Expander';
import Select from 'client/lazy-app/Compress/Options/Select';
import Range from 'client/lazy-app/Compress/Options/Range';
import Checkbox from 'client/lazy-app/Compress/Options/Checkbox';

const konamiPromise = konami();

Expand Down Expand Up @@ -43,6 +45,7 @@ export class Options extends Component<Props, State> {
options.maxNumColors,
),
dither: inputFieldValueAsNumber(form.dither),
alphaOnly: inputFieldChecked(form.alphaOnly),
};
this.props.onChange(newOptions);
};
Expand Down Expand Up @@ -92,6 +95,14 @@ export class Options extends Component<Props, State> {
Dithering:
</Range>
</div>
<label class={style.optionToggle}>
Alpha only
<Checkbox
name="alphaOnly"
checked={options.alphaOnly}
onChange={this.onChange}
/>
</label>
</form>
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/features/processors/quantize/shared/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ export interface Options {
zx: number;
maxNumColors: number;
dither: number;
alphaOnly: boolean;
}

export const defaultOptions: Options = {
zx: 0,
maxNumColors: 256,
dither: 1.0,
alphaOnly: false,
};
44 changes: 41 additions & 3 deletions src/features/processors/quantize/worker/quantize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,54 @@ export default async function process(
}

const module = await emscriptenModule;
let alpha_data: Uint8ClampedArray;

if (opts.alphaOnly) {
alpha_data = getAlphaData(data.data);
}

const result = opts.zx
? module.zx_quantize(data.data, data.width, data.height, opts.dither)
? module.zx_quantize(
opts.alphaOnly ? alpha_data! : data.data,
data.width,
data.height,
opts.dither,
)
: module.quantize(
data.data,
opts.alphaOnly ? alpha_data! : data.data,
data.width,
data.height,
opts.maxNumColors,
opts.dither,
);

return new ImageData(result, data.width, data.height);
//Apply the result back to the original image if only alpha is checked
if (opts.alphaOnly) {
for (let i = 0; i < result.length; i += 4) {
const alpha = alpha_data![i + 3];
data.data[i + 3] = alpha;
}
}

return new ImageData(
opts.alphaOnly ? data.data : result,
data.width,
data.height,
);
}

/**
* Return a single color version of the image with alpha channel preserved
* @param data The original image data
* @returns A single color image with alpha channel preseved
*/
function getAlphaData(data: Uint8ClampedArray): Uint8ClampedArray {
let copy = new Uint8ClampedArray(data);
for (let i = 0; i < copy.length; i += 4) {
copy[i] = 0;
copy[i + 1] = 0;
copy[i + 2] = 0;
}

return copy;
}
Loading