|
| 1 | +import { saveAs } from "file-saver"; |
| 2 | +import { FunctionThread, Pool } from "threads"; |
| 3 | +import type MandelbrotMap from "./MandelbrotMap"; |
| 4 | +import type MandelbrotLayer from "./MandelbrotLayer"; |
| 5 | +import { |
| 6 | + ComplexBounds, |
| 7 | + OptimiseRequest, |
| 8 | + OptimiseResponse, |
| 9 | + WorkerRequest, |
| 10 | + WorkerResponse, |
| 11 | +} from "./MandelbrotMap"; |
| 12 | + |
| 13 | +type TaskThread = FunctionThread<[WorkerRequest], WorkerResponse>; |
| 14 | + |
| 15 | +class ImageSaver { |
| 16 | + private map: MandelbrotMap; |
| 17 | + private pool: Pool<TaskThread>; |
| 18 | + private mandelbrotLayer: MandelbrotLayer; |
| 19 | + |
| 20 | + constructor( |
| 21 | + map: MandelbrotMap, |
| 22 | + pool: Pool<TaskThread>, |
| 23 | + mandelbrotLayer: MandelbrotLayer, |
| 24 | + ) { |
| 25 | + this.map = map; |
| 26 | + this.pool = pool; |
| 27 | + this.mandelbrotLayer = mandelbrotLayer; |
| 28 | + } |
| 29 | + |
| 30 | + async saveVisibleImage( |
| 31 | + totalWidth: number, |
| 32 | + totalHeight: number, |
| 33 | + optimize: boolean, |
| 34 | + onStartOptimizing?: () => void, |
| 35 | + ) { |
| 36 | + const bounds = this.adjustBoundsForAspectRatio( |
| 37 | + this.map.mapBoundsAsComplexParts, |
| 38 | + totalWidth, |
| 39 | + totalHeight, |
| 40 | + ); |
| 41 | + const imageCanvases = await this.generateImageColumns( |
| 42 | + bounds, |
| 43 | + totalWidth, |
| 44 | + totalHeight, |
| 45 | + ); |
| 46 | + const finalCanvas = this.combineImageColumns( |
| 47 | + imageCanvases, |
| 48 | + totalWidth, |
| 49 | + totalHeight, |
| 50 | + ); |
| 51 | + await this.saveCanvasAsImage(finalCanvas, optimize, onStartOptimizing); |
| 52 | + } |
| 53 | + |
| 54 | + private adjustBoundsForAspectRatio( |
| 55 | + bounds: ComplexBounds, |
| 56 | + totalWidth: number, |
| 57 | + totalHeight: number, |
| 58 | + ): ComplexBounds { |
| 59 | + const imageAspectRatio = totalWidth / totalHeight; |
| 60 | + const complexAspectRatio = |
| 61 | + (bounds.reMax - bounds.reMin) / (bounds.imMax - bounds.imMin); |
| 62 | + |
| 63 | + const adjustedBounds = { ...bounds }; |
| 64 | + |
| 65 | + if (imageAspectRatio < complexAspectRatio) { |
| 66 | + const newImHeight = (bounds.reMax - bounds.reMin) / imageAspectRatio; |
| 67 | + const imCenter = (bounds.imMin + bounds.imMax) / 2; |
| 68 | + adjustedBounds.imMin = imCenter - newImHeight / 2; |
| 69 | + adjustedBounds.imMax = imCenter + newImHeight / 2; |
| 70 | + } else if (imageAspectRatio > complexAspectRatio) { |
| 71 | + const newReWidth = (bounds.imMax - bounds.imMin) * imageAspectRatio; |
| 72 | + const reCenter = (bounds.reMin + bounds.reMax) / 2; |
| 73 | + adjustedBounds.reMin = reCenter - newReWidth / 2; |
| 74 | + adjustedBounds.reMax = reCenter + newReWidth / 2; |
| 75 | + } |
| 76 | + |
| 77 | + return adjustedBounds; |
| 78 | + } |
| 79 | + |
| 80 | + private async generateImageColumns( |
| 81 | + bounds: ComplexBounds, |
| 82 | + totalWidth: number, |
| 83 | + totalHeight: number, |
| 84 | + ): Promise<HTMLCanvasElement[]> { |
| 85 | + const numColumns = 24; |
| 86 | + const columnWidth = Math.ceil(totalWidth / numColumns); |
| 87 | + const reDiff = bounds.reMax - bounds.reMin; |
| 88 | + const reDiffPerColumn = reDiff * (columnWidth / totalWidth); |
| 89 | + |
| 90 | + const imagePromises: Promise<HTMLCanvasElement>[] = []; |
| 91 | + for (let i = 0; i < numColumns; i++) { |
| 92 | + const subBounds = { |
| 93 | + ...bounds, |
| 94 | + reMin: bounds.reMin + reDiffPerColumn * i, |
| 95 | + reMax: bounds.reMin + reDiffPerColumn * (i + 1), |
| 96 | + }; |
| 97 | + imagePromises.push( |
| 98 | + this.mandelbrotLayer.getImage(subBounds, columnWidth, totalHeight), |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + return Promise.all(imagePromises); |
| 103 | + } |
| 104 | + |
| 105 | + private combineImageColumns( |
| 106 | + imageCanvases: HTMLCanvasElement[], |
| 107 | + totalWidth: number, |
| 108 | + totalHeight: number, |
| 109 | + ): HTMLCanvasElement { |
| 110 | + const finalCanvas = document.createElement("canvas"); |
| 111 | + finalCanvas.width = totalWidth; |
| 112 | + finalCanvas.height = totalHeight; |
| 113 | + const ctx = finalCanvas.getContext("2d"); |
| 114 | + |
| 115 | + if (!ctx) { |
| 116 | + throw new Error("Could not get canvas context for combining columns"); |
| 117 | + } |
| 118 | + |
| 119 | + let xOffset = 0; |
| 120 | + imageCanvases.forEach((canvas) => { |
| 121 | + ctx.drawImage(canvas, xOffset, 0); |
| 122 | + xOffset += canvas.width; |
| 123 | + }); |
| 124 | + |
| 125 | + return finalCanvas; |
| 126 | + } |
| 127 | + |
| 128 | + private async saveCanvasAsImage( |
| 129 | + canvas: HTMLCanvasElement, |
| 130 | + optimize: boolean, |
| 131 | + onStartOptimizing?: () => void, |
| 132 | + ): Promise<void> { |
| 133 | + const ctx = canvas.getContext("2d"); |
| 134 | + if (!ctx) { |
| 135 | + console.error("Could not get canvas context for saving image"); |
| 136 | + return; |
| 137 | + } |
| 138 | + const dataUrl = canvas.toDataURL("image/png"); |
| 139 | + const response = await fetch(dataUrl); |
| 140 | + const rawPngBuffer = await response.arrayBuffer(); |
| 141 | + |
| 142 | + let finalBuffer = rawPngBuffer; |
| 143 | + |
| 144 | + if (optimize) { |
| 145 | + onStartOptimizing?.(); |
| 146 | + const optimiseRequest: OptimiseRequest = { |
| 147 | + type: "optimise", |
| 148 | + payload: { buffer: rawPngBuffer }, |
| 149 | + }; |
| 150 | + finalBuffer = (await this.pool.queue((worker) => |
| 151 | + worker(optimiseRequest), |
| 152 | + )) as OptimiseResponse; |
| 153 | + } |
| 154 | + |
| 155 | + const blob = new Blob([finalBuffer], { type: "image/png" }); |
| 156 | + |
| 157 | + saveAs( |
| 158 | + blob, |
| 159 | + `mandelbrot${Date.now()}_r${this.map.config.re}_im${ |
| 160 | + this.map.config.im |
| 161 | + }_z${this.map.config.zoom}.png`, |
| 162 | + ); |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +export default ImageSaver; |
0 commit comments