Skip to content

Commit

Permalink
feat(imago): add ICC profile assignment op
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Apr 1, 2024
1 parent 8c65671 commit 5d022cb
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
12 changes: 12 additions & 0 deletions packages/imago/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,18 @@ export interface HSBLSpec extends ProcSpec {
l?: number;
}

export interface ICCSpec extends ProcSpec {
op: "icc";
/**
* ICC profile preset name
*/
profile?: "srgb" | "p3" | "cmyk";
/**
* ICC profile file path (can be relative)
*/
path?: string;
}

export interface NestSpec extends ProcSpec {
op: "nest";
/**
Expand Down
1 change: 1 addition & 0 deletions packages/imago/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export * from "./ops/extend.js";
export * from "./ops/gamma.js";
export * from "./ops/grayscale.js";
export * from "./ops/hsbl.js";
export * from "./ops/icc.js";
export * from "./ops/nest.js";
export * from "./ops/output.js";
export * from "./ops/resize.js";
Expand Down
6 changes: 6 additions & 0 deletions packages/imago/src/ops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
GammaSpec,
GrayscaleSpec,
HSBLSpec,
ICCSpec,
ImgLayer,
NestSpec,
OutputSpec,
Expand Down Expand Up @@ -108,6 +109,11 @@ export const grayscale = defSpec<GrayscaleSpec>("gray");
*/
export const hsbl = defSpec<HSBLSpec>("hsbl");

/**
* Creates a new {@link ICCSpec} with given opts.
*/
export const icc = defSpec<ICCSpec>("icc");

/**
* Creates a new {@link NestSpec} with given opts.
*/
Expand Down
16 changes: 16 additions & 0 deletions packages/imago/src/ops/icc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { illegalArgs } from "@thi.ng/errors";
import { resolve } from "node:path";
import type { Processor, ICCSpec } from "../api.js";

export const iccProc: Processor = async (opts, src, ctx) => {
const $opts = <ICCSpec>opts;
if ($opts.profile) {
ctx.logger.info("setting ICC profile:", $opts.profile);
return [src.withIccProfile($opts.profile), false];
} else if ($opts.path) {
ctx.logger.info("setting ICC profile:", $opts.path);
ctx.opts.keepICC = false;
return [src.withIccProfile(resolve($opts.path)), false];
}
illegalArgs("require ICC profile name or path");
};
4 changes: 3 additions & 1 deletion packages/imago/src/proc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { extendProc } from "./ops/extend.js";
import { gammaProc } from "./ops/gamma.js";
import { grayscaleProc } from "./ops/grayscale.js";
import { hsblProc } from "./ops/hsbl.js";
import { iccProc } from "./ops/icc.js";
import { nestProc } from "./ops/nest.js";
import { outputProc } from "./ops/output.js";
import { resizeProc } from "./ops/resize.js";
Expand Down Expand Up @@ -59,8 +60,8 @@ export const processImage = async (
logger: opts.logger || LOGGER,
size: [meta.width!, meta.height!],
exif: parentCtx ? structuredClone(parentCtx.exif) : {},
opts: { ...opts },
meta,
opts,
};

if (!parentCtx) {
Expand Down Expand Up @@ -141,6 +142,7 @@ export const processor = defmulti<
gamma: gammaProc,
gray: grayscaleProc,
hsbl: hsblProc,
icc: iccProc,
nest: nestProc,
output: outputProc,
resize: resizeProc,
Expand Down

0 comments on commit 5d022cb

Please sign in to comment.