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

feature: Implement reading mask data #56

Merged
merged 8 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 3 additions & 3 deletions packages/psd/src/classes/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ export class Group implements NodeBase<NodeParent, NodeChild> {

/** @internal */
constructor(
private layerFrame: GroupFrame,
private layerFrame: GroupFrame | undefined,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very weird to me, but there is one sample file where rendering layer fails here with:


TypeError: Cannot read properties of undefined (reading 'layerProperties')
    at get opacity [as opacity] (file://./packages/psd/dist/index.js:1:186)
    at get composedOpacity [as composedOpacity] (file://./packages/psd/dist/index.js:1:273)
    at get composedOpacity [as composedOpacity] (file://./packages/psd/dist/index.js:1:78558)
    at BI.composite (file://./packages/psd/dist/index.js:1:78007)
    at async file://./packages/psd/scripts/parse.mjs:113:7
    at async Promise.all (index 256)
    at async file://./packages/psd/scripts/parse.mjs:109:3

where parse.mjs is

import * as fs from "fs";

import Psd from "../dist/index.js";

for (const input of process.argv.slice(2)) {
  console.log(input);
  const arrayBuffer = fs.readFileSync(input);

  const psd = Psd.parse(arrayBuffer.buffer);

  await Promise.all(
    psd.layers.map(async (layer) => {
      await layer.userMask();
      await layer.realUserMask();
      await layer.composite();
    })
  );
}

thus the workaround.

public readonly parent: NodeParent
) {}

get name(): string {
return this.layerFrame.layerProperties.name;
return this.layerFrame?.layerProperties.name ?? "";
}
get opacity(): number {
return this.layerFrame.layerProperties.opacity;
return this.layerFrame?.layerProperties.opacity ?? 0;
}
get composedOpacity(): number {
return this.parent.composedOpacity * (this.opacity / 255);
Expand Down
24 changes: 23 additions & 1 deletion packages/psd/src/classes/Layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
// MIT License

import {EngineData, ImageData} from "../interfaces";
import {LayerFrame} from "../sections";
import {decodeGrayscale} from "../methods";
import {LayerFrame, MaskData} from "../sections";
import {area} from "../utils";
import {NodeParent} from "./Node";
import {NodeBase} from "./NodeBase";
import {Synthesizable} from "./Synthesizable";
Expand Down Expand Up @@ -48,6 +50,26 @@ export class Layer
get composedOpacity(): number {
return this.parent.composedOpacity * (this.opacity / 255);
}
get maskData(): MaskData {
return this.layerFrame.layerProperties.maskData;
}

async userMask(): Promise<Uint8Array | undefined> {
const userMask = this.layerFrame.userMask;
if (!userMask) {
return undefined;
}
return decodeGrayscale(area(this.maskData), userMask);
}

async realUserMask(): Promise<Uint8Array | undefined> {
const maskData = this.maskData.realData;
const userMask = this.layerFrame.realUserMask;
if (!maskData || !userMask) {
return undefined;
}
return decodeGrayscale(area(maskData), userMask);
}

get isHidden(): boolean {
return this.layerFrame.layerProperties.hidden;
Expand Down
6 changes: 6 additions & 0 deletions packages/psd/src/sections/LayerAndMaskInformation/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ export class LayerFrame {
get alpha(): ChannelBytes | undefined {
return this.channels.get(ChannelKind.TransparencyMask);
}
get userMask(): ChannelBytes | undefined {
return this.channels.get(ChannelKind.UserSuppliedLayerMask);
}
get realUserMask(): ChannelBytes | undefined {
return this.channels.get(ChannelKind.RealUserSuppliedLayerMask);
}

get width(): number {
const {right, left} = this.layerProperties;
Expand Down
51 changes: 51 additions & 0 deletions packages/psd/src/sections/LayerAndMaskInformation/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface LayerRecord {
layerText?: string;
/** If defined, containts extra text properties */
engineData?: EngineData;
maskData: MaskData;
}

export type LayerChannels = Map<ChannelKind, ChannelBytes>;
Expand Down Expand Up @@ -60,6 +61,7 @@ export interface LayerProperties {
text?: string;
/** Text properties */
textProperties?: EngineData;
maskData: MaskData;
}

export const createLayerProperties = (
Expand All @@ -79,6 +81,7 @@ export const createLayerProperties = (
blendMode,
layerText,
engineData,
maskData,
} = layerRecord;

return {
Expand All @@ -95,5 +98,53 @@ export const createLayerProperties = (
groupId,
text: layerText,
textProperties: engineData,
maskData,
};
};

export interface MaskFlags {
// bit 0 = position relative to layer
positionRelativeToLayer: boolean;
// bit 1 = layer mask disabled
layerMaskDisabled: boolean;
// bit 2 = invert layer mask when blending (Obsolete)
invertMaskWhenBlending: boolean;
// bit 3 = indicates that the user mask actually came from rendering other data
userMaskFromRenderingOtherData: boolean;
// bit 4 = indicates that the user and/or vector masks have parameters applied to them
masksHaveParametersApplied: boolean;
}

export interface MaskParameters {
// bit 0 = user mask density, 1 byte
userMaskDensity?: number;
// bit 1 = user mask feather, 8 byte, double
userMaskFeather?: number;
// bit 2 = vector mask density, 1 byte
vectorMaskDensity?: number;
// bit 3 = vector mask feather, 8 bytes, double
vectorMaskFeather?: number;
}

// The spec is confusing at best... what "real" means?
// https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577409_pgfId-1031423
export interface RealMaskData {
flags: MaskFlags;
backgroundColor: number;
top: number;
left: number;
bottom: number;
right: number;
}

export interface MaskData {
top: number;
left: number;
bottom: number;
right: number;
backgroundColor: number;
flags: MaskFlags;
parameters?: MaskParameters;
// only present if size != 20
realData?: RealMaskData;
}
Loading