Skip to content

Commit

Permalink
use matte
Browse files Browse the repository at this point in the history
  • Loading branch information
anshul committed Nov 22, 2020
1 parent f361906 commit bbea9d8
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 123 deletions.
23 changes: 19 additions & 4 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,30 @@ export class GIF implements Preset {
export class MozJPEG implements Preset {
private readonly quality: number
private readonly isProgressive: boolean
private readonly matte: Colors
toPreset(): Object {
console.log(this.matte?.toColor())
return {
mozjpeg: {
quality: this.quality,
progressive: this.isProgressive,
matte: this.matte?.toColor(),
},
}
}

constructor(quality: number, isProgressive: boolean) {
constructor(
quality: number,
{
isProgressive,
matte,
}: { isProgressive?: boolean; matte?: Colors } = {}
) {
if (quality > 100 || quality < 0)
throw new Error('invalid quality for preset')
this.isProgressive = isProgressive
this.quality = quality
this.matte = matte
}
}

Expand Down Expand Up @@ -231,6 +241,7 @@ interface ConstrainHintsOptions {
scalingColorScape?: ScalingFloatspace
resampleWhen?: ResampleWhen
sharpenWhen?: SharpenWhen
backgroundColor?: Colors
}

export class ConstrainHints {
Expand All @@ -240,6 +251,7 @@ export class ConstrainHints {
private scalingColorSpace: ScalingFloatspace | null
private resampleWhen: ResampleWhen | null
private sharpenWhen: SharpenWhen | null
private backgroundColor: Colors
toHint(): Object {
return {
sharpen_percent: this.sharpenPercent,
Expand All @@ -250,6 +262,7 @@ export class ConstrainHints {
.toLowerCase(),
resample_when: this.resampleWhen?.toString(),
sharpen_when: this.sharpenWhen?.toString(),
background_color: this.backgroundColor?.toColor(),
}
}
constructor(
Expand All @@ -260,6 +273,7 @@ export class ConstrainHints {
scalingColorScape,
resampleWhen,
sharpenWhen,
backgroundColor,
}: ConstrainHintsOptions = {}
) {
this.sharpenPercent = sharpenPercent
Expand All @@ -268,6 +282,7 @@ export class ConstrainHints {
this.scalingColorSpace = scalingColorScape
this.resampleWhen = resampleWhen
this.sharpenWhen = sharpenWhen
this.backgroundColor = backgroundColor
}
}

Expand Down Expand Up @@ -316,7 +331,7 @@ export enum ConstrainMode {
}

interface ConstrainOptions {
canvasColor?: string
canvasColor?: Colors
canvasGravity?: ConstrainGravity
hints?: ConstrainHints
}
Expand All @@ -326,7 +341,7 @@ export class Constrain implements BaseStep {
private readonly height?: number
private gravity?: ConstrainGravity
private mode: ConstrainMode
private readonly canvasColor?: string
private readonly canvasColor?: Colors
toStep(): Object {
return {
constrain: {
Expand All @@ -335,7 +350,7 @@ export class Constrain implements BaseStep {
h: this.height,
hints: this.hints?.toHint(),
gravity: this.gravity?.toGravity(),
canvas_color: this.canvasColor,
canvas_color: this.canvasColor?.toColor(),
},
}
}
Expand Down
11 changes: 6 additions & 5 deletions native/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions native/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ crate-type = ["cdylib"]


[dependencies]
imageflow_core = { git = "https://github.com/imazen/imageflow", tag = "v1.5.2-rc56" }
imageflow_types = { git = "https://github.com/imazen/imageflow", tag = "v1.5.2-rc56" }
imageflow_core = { git = "https://github.com/imazen/imageflow", tag = "v1.5.3-rc57" }
imageflow_types = { git = "https://github.com/imazen/imageflow", tag = "v1.5.3-rc57" }
nodejs-sys = "0.3.0"
threadpool = "1.8.1"

Expand Down
167 changes: 55 additions & 112 deletions test/file.js
Original file line number Diff line number Diff line change
@@ -1,123 +1,66 @@
const {
MozJPEG,
Steps,
FromURL,
FromFile,
FromStream,
FromBuffer,
FromURL,
Constrain,
ConstrainMode,
ReSample,
ConstrainHints,
TransparentColor,
BlackColor,
SRGBColor,
ColorType,
} = require('..')
// const fs = require('fs')

// let step = new Steps(new FromURL('https://jpeg.org/images/jpeg2000-home.jpg'))
// .constraintWithin(500, 500)
// .branch((step) =>
// step
// .constraintWithin(400, 400)
// .branch((step) =>
// step
// .constraintWithin(200, 200)
// .rotate90()
// .colorFilterGrayscaleFlat()
// .encode(new FromFile('./branch_2.jpg'), new MozJPEG(80))
// )
// .copyRectangle(
// (canvas) =>
// canvas.decode(
// new FromStream(fs.createReadStream('./test.jpg'))
// ),
// { x: 0, y: 0, w: 100, h: 100 },
// 10,
// 10
// )
// .encode(new FromFile('./branch.jpg'), new MozJPEG(80))
// )
// .constraintWithin(100, 100)
// .rotate180()
// step.encode(new FromBuffer(null, 'key'), new MozJPEG(80))
// .execute()
// .then(console.log)
// .catch(console.log)
class Resize extends Steps {
resize(source, output, sizes) {
const [width, height] = sizes[0].split('x')
const out = output.replace('{w}', width).replace('{h}', height)
const nextSizes = sizes.slice(1)

const test = new Steps(new FromFile('./test/test.jpg'))
.constraintWithin(800, 800)
.branch((step) => step.encode(new FromFile('large.jpeg'), new MozJPEG()))
.branch((step) =>
step
.constraintWithin(400, 400)
.branch((step) =>
step
.constraintWithin(200, 200)
.branch((step) =>
step
.constraintWithin(100, 100)
.encode(
new FromFile('performing.jpeg'),
new MozJPEG()
)
)
.encode(new FromFile('small.jpeg'), new MozJPEG())
this.branch((step) => {
step.constrain(
new Constrain(ConstrainMode.Within, width * 1, height * 1)
)
.encode(new FromFile('medium.jpeg'), new MozJPEG())
)
.execute()

// class Graph {
// map

// constructor() {
// this.map = new Map()
// }

// addVertex(ver) {
// this.map.set(ver, [])
// }

// addEdge(to, from) {
// console.log(this.map, to, from)
// this.map.get(from).push(to)
// }
// }

// class Base {
// graph
// vertex
// last

// constructor() {
// this.graph = new Graph()
// this.vertex = []
// this.last = 0
// this.graph.addVertex(0)
// this.vertex.push(0)
// }

// addEdge() {
// this.graph.addVertex(this.vertex.length)
// this.vertex.push(this.vertex.length)
// this.graph.addEdge(this.vertex.length - 1, this.last)
// this.last = this.vertex.length - 1
// return this
// }
if (nextSizes.length) {
step.resize(source, output, nextSizes)
}
step.encode(
new FromFile(out),
new MozJPEG(90, {
isProgressive: true,
matte: new SRGBColor(ColorType.Hex, '00ffff'),
})
)
})

// branch(f) {
// let last = this.last
// f(this)
// this.last = last
// return this
// }
// }
return this
}
}

// let base = new Base()
const start = async function () {
const [src, out, min, max] = process.argv.slice(2)
const sizes = [
'1920x1920',
'1080x1080',
'720x720',
'480x480',
'360x360',
'120x120',
'60x60',
]

// base.addEdge()
// .branch((step) =>
// step
// .addEdge()
// .addEdge()
// .branch((step) => step.addEdge().addEdge())
// .addEdge()
// )
// .addEdge()
// .addEdge()
const input = src.indexOf('://') > -1 ? new FromURL(src) : new FromFile(src)
for await (const size of sizes) {
if (min || max) {
const [width, height] = size.split('x')
if ((min && width * 1 < min * 1) || (max && width * 1 > max * 1)) {
continue
}
}
console.log('size ' + size)
await new Resize(input).resize(src, out, [size]).execute()
}
}

// console.log(base.graph)
start()

0 comments on commit bbea9d8

Please sign in to comment.