Skip to content

Commit

Permalink
Image history first approach
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszmigas committed Mar 4, 2024
1 parent f7ba745 commit 2fe931a
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
9 changes: 9 additions & 0 deletions apps/web/src/components/canvasHost.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import {
ImageUncompressedBufferRect,
ImageUncompressedBuffer,
} from "@/utils/imageData";
import { Size } from "@/utils/common";
import { useLayoutEffect, useRef } from "react";

type CanvasHostProps = {
currentData: ImageUncompressedBuffer; //the whole image
draftDataDiffs: ImageUncompressedBufferRect[]; //image diffs that will override parts of currentData
};

export const CanvasHost = (props: { size: Size }) => {
const { size } = props;
const canvasRef = useRef<HTMLCanvasElement>(null);
Expand Down
51 changes: 51 additions & 0 deletions apps/web/src/history/index.ts
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,
};
}
}

53 changes: 53 additions & 0 deletions apps/web/src/utils/imageData.ts
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
);
}
}
};

0 comments on commit 2fe931a

Please sign in to comment.