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

Add Separate Alpha option to Resize node #2400

Merged
merged 3 commits into from
Dec 13, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

import numpy as np

from nodes.groups import if_enum_group
from nodes.groups import Condition, if_enum_group, if_group
from nodes.impl.resize import ResizeFilter, resize
from nodes.properties.inputs import (
BoolInput,
EnumInput,
ImageInput,
NumberInput,
Expand Down Expand Up @@ -51,6 +52,16 @@ class ImageResizeMode(Enum):
NumberInput("Height", minimum=1, default=1, unit="px").with_id(4),
),
ResizeFilterInput().with_id(5),
if_group(Condition.type(0, "Image { channels: 4 } "))(
BoolInput("Separate Alpha", default=False)
.with_docs(
"Resize alpha separately from color. Enable this option of the alpha channel of the image is **not** transparency.",
"To resize images with transparency correctly, the alpha channels must be multiplied with the color channels before resizing. While this will produce correct color values, it will also set the color of fully transparent pixels to black. This is an issue if the alpha channel isn't transparency. E.g. games often use the alpha channel of textures for other purposes, such as height maps or edge maps. For such images, the alpha channel has to be resized separately or else it will corrupt the color channels.",
"For images where the alpha channel is transparency (most transparent images), this option should be **disabled**.",
hint=True,
)
.with_id(6)
),
],
outputs=[
ImageOutput(
Expand Down Expand Up @@ -86,6 +97,7 @@ def resize_node(
width: int,
height: int,
filter: ResizeFilter,
separate_alpha: bool,
) -> np.ndarray:
h, w, _ = get_h_w_c(img)

Expand All @@ -102,6 +114,6 @@ def resize_node(
img,
out_dims,
filter,
separate_alpha=False,
separate_alpha=separate_alpha,
gamma_correction=False,
)
26 changes: 25 additions & 1 deletion src/renderer/components/inputs/DropDownInput.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { QuestionIcon } from '@chakra-ui/icons';
import { Tooltip } from '@chakra-ui/react';
import { memo, useCallback } from 'react';
import { Markdown } from '../Markdown';
import { Checkbox } from './elements/Checkbox';
import { DropDown } from './elements/Dropdown';
import { TabList } from './elements/TabList';
Expand All @@ -8,15 +11,36 @@ import { InputProps } from './props';
type DropDownInputProps = InputProps<'dropdown', string | number>;

export const DropDownInput = memo(({ value, setValue, input, isLocked }: DropDownInputProps) => {
const { options, def, label, preferredStyle, groups } = input;
const { options, def, label, preferredStyle, groups, hint, description } = input;

const reset = useCallback(() => setValue(def), [setValue, def]);

if (preferredStyle === 'checkbox' && options.length === 2) {
// checkbox assumes the first options means yes and the second option means no

let afterText;
if (hint && description) {
afterText = (
<Tooltip
hasArrow
borderRadius={8}
label={<Markdown nonInteractive>{description}</Markdown>}
openDelay={500}
px={2}
py={1}
>
<QuestionIcon
boxSize={3}
ml={1}
verticalAlign="baseline"
/>
</Tooltip>
);
}
return (
<WithoutLabel>
<Checkbox
afterText={afterText}
isDisabled={isLocked}
label={label}
no={options[1]}
Expand Down
6 changes: 4 additions & 2 deletions src/renderer/components/inputs/elements/Checkbox.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Box, Checkbox as ChakraCheckbox } from '@chakra-ui/react';
import { memo, useEffect } from 'react';
import { ReactNode, memo, useEffect } from 'react';
import { DropDownInput, InputSchemaValue } from '../../../../common/common-types';
import './Checkbox.scss';

Expand All @@ -14,10 +14,11 @@ export interface CheckboxProps {
yes: Option;
no: Option;
label: string;
afterText?: ReactNode;
}

export const Checkbox = memo(
({ value, onChange, reset, isDisabled, yes, no, label }: CheckboxProps) => {
({ value, onChange, reset, isDisabled, yes, no, label, afterText }: CheckboxProps) => {
// reset invalid values to default
useEffect(() => {
if (value === undefined || (yes.value !== value && no.value !== value)) {
Expand All @@ -43,6 +44,7 @@ export const Checkbox = memo(
>
{label}
</Box>
{afterText}
</ChakraCheckbox>
);
}
Expand Down