-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f7ba745
commit 2fe931a
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { | ||
ImageCompressedBuffer, | ||
ImageUncompressedBuffer, | ||
ImageUncompressedBufferRect, | ||
} from "@/utils/imageData"; | ||
|
||
export type ImageChange = | ||
| { | ||
type: "chunk"; | ||
data: ImageUncompressedBufferRect; | ||
} | ||
| { | ||
type: "full"; | ||
data: ImageUncompressedBuffer; | ||
}; | ||
|
||
export class ImageHistory { | ||
private checkpoints: Record<number, ImageCompressedBuffer> = {}; | ||
|
||
init(data?: ImageUncompressedBuffer) {} | ||
|
||
push(change: ImageChange) { | ||
//resets stack | ||
if (change.type === "chunk") { | ||
//store original chunks | ||
} else if (change.type === "full") { | ||
//compress current and use as checkpoint | ||
} else { | ||
throw new Error("Invalid change type"); | ||
} | ||
} | ||
|
||
undo() { | ||
//pop from stack | ||
//apply to current | ||
} | ||
|
||
redo() { | ||
//pop from stack | ||
//apply to current | ||
} | ||
|
||
private compress(data: ImageUncompressedBuffer): ImageCompressedBuffer { | ||
return { | ||
width: data.width, | ||
height: data.height, | ||
data: data.data, | ||
}; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
export type ImageUncompressedBuffer = { | ||
width: number; | ||
height: number; | ||
data: Uint8ClampedArray; | ||
}; | ||
|
||
export type ImageCompressedBuffer = { | ||
width: number; | ||
height: number; | ||
data: Uint8ClampedArray; | ||
}; | ||
|
||
export const pickChunk = ( | ||
base: ImageUncompressedBuffer, | ||
x: number, | ||
y: number, | ||
width: number, | ||
height: number | ||
): ImageUncompressedBufferRect => { | ||
const data = new Uint8ClampedArray(width * height * 4); | ||
for (let i = 0; i < height; i++) { | ||
const baseOffset = (y + i) * width * 4 + x * 4; | ||
const rectOffset = i * width * 4; | ||
data.set( | ||
base.data.subarray(baseOffset, baseOffset + width * 4), | ||
rectOffset | ||
); | ||
} | ||
return { x, y, width, height, data }; | ||
}; | ||
|
||
export type ImageUncompressedBufferRect = ImageUncompressedBuffer & { | ||
x: number; | ||
y: number; | ||
}; | ||
|
||
export const applyRects = ( | ||
base: ImageUncompressedBuffer, | ||
diffs: ImageUncompressedBufferRect[] | ||
) => { | ||
for (const diff of diffs) { | ||
const { x, y, width, height, data } = diff; | ||
for (let i = 0; i < height; i++) { | ||
const baseOffset = (y + i) * width * 4 + x * 4; | ||
const rectOffset = i * width * 4; | ||
base.data.set( | ||
data.subarray(rectOffset, rectOffset + width * 4), | ||
baseOffset | ||
); | ||
} | ||
} | ||
}; | ||
|