Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | 60x 60x 60x 60x 60x 60x 248579810x 745739430x 248579810x 248579810x 248579810x 248579810x 248579810x 1x 1x 1x 1x 6x 6x 6x 6x 43x 43x 43x 43x 43x 6x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 14x 14x 2x 2x 12x 2x | /**
* Test Image Generator
*
* Utilities for creating test images programmatically using Sharp.
* No large images are stored in the repository.
*/
import sharp from 'sharp'
import path from 'path'
import fs from 'fs-extra'
export interface GenerateImageOptions {
width: number
height: number
format: 'jpeg' | 'png' | 'webp' | 'tiff'
quality?: number
background?: { r: number; g: number; b: number }
}
/**
* Generate a test image with specified dimensions and format
* Creates images with semi-random noise to prevent over-compression
*/
export async function generateTestImage(
outputPath: string,
options: GenerateImageOptions
): Promise<void> {
const { width, height, format, quality = 80, background = { r: 100, g: 150, b: 200 } } = options
await fs.ensureDir(path.dirname(outputPath))
const channels = 3
const pixelCount = width * height
const buffer = Buffer.alloc(pixelCount * channels)
// Fill with semi-random noise based on background color
// This creates less compressible images for realistic file size testing
for (let i = 0; i < pixelCount; i++) {
const offset = i * channels
// Add random noise to prevent JPEG over-compression
// Vary each channel by ±50 from the background color
const noise = () => Math.floor(Math.random() * 100) - 50
buffer[offset] = Math.min(255, Math.max(0, background.r + noise()))
buffer[offset + 1] = Math.min(255, Math.max(0, background.g + noise()))
buffer[offset + 2] = Math.min(255, Math.max(0, background.b + noise()))
}
const image = sharp(buffer, {
raw: {
width,
height,
channels
}
})
await image[format]({ quality }).toFile(outputPath)
}
/**
* Generate a very large high-resolution image (for testing memory handling)
*/
export async function generateLargeImage(
outputPath: string,
megapixels: number
): Promise<void> {
// Calculate dimensions for target megapixels (16:9 aspect ratio)
const totalPixels = megapixels * 1_000_000
const height = Math.floor(Math.sqrt(totalPixels / (16 / 9)))
const width = Math.floor(height * (16 / 9))
await generateTestImage(outputPath, {
width,
height,
format: 'jpeg',
quality: 90,
background: { r: 50, g: 100, b: 150 }
})
}
/**
* Generate a batch of test images with various formats
*/
export async function generateImageBatch(
baseDir: string,
count: number,
options?: Partial<GenerateImageOptions>
): Promise<string[]> {
await fs.ensureDir(baseDir)
const formats: Array<'jpeg' | 'png' | 'webp'> = ['jpeg', 'png', 'webp']
const paths: string[] = []
for (let i = 0; i < count; i++) {
const format = formats[i % formats.length]
const filename = `test-image-${i}.${format}`
const filepath = path.join(baseDir, filename)
await generateTestImage(filepath, {
width: options?.width ?? 800,
height: options?.height ?? 600,
format,
quality: options?.quality ?? 80,
background: options?.background ?? { r: 100 + i * 10, g: 150, b: 200 }
})
paths.push(filepath)
}
return paths
}
/**
* Generate images with a target total file size
* Efficiently generates a small number of large images rather than many small ones
*/
export async function generateImagesBySize(
baseDir: string,
targetSizeMB: number
): Promise<string[]> {
await fs.ensureDir(baseDir)
// Generate 6 large images instead of many small ones
const imageCount = 6
const targetSizePerImage = targetSizeMB / imageCount
// With random noise at quality 85:
// Random noise compresses poorly in JPEG, actual ratio ~6-7:1
// Target ~6.5:1 compression ratio for ~100MB total
// So: uncompressed size = targetSize * 6.5
// uncompressed size = width * height * 3 bytes (RGB)
// Therefore: width * height = (targetSizePerImage * 6.5 * 1024 * 1024) / 3
const pixelsPerImage = (targetSizePerImage * 6.5 * 1024 * 1024) / 3
const megapixelsPerImage = pixelsPerImage / 1_000_000
const paths: string[] = []
for (let i = 0; i < imageCount; i++) {
// Calculate dimensions for target megapixels (4:3 aspect ratio)
const height = Math.floor(Math.sqrt(megapixelsPerImage * 1_000_000 / (4 / 3)))
const width = Math.floor(height * (4 / 3))
const filename = `large-image-${i}.jpeg`
const filepath = path.join(baseDir, filename)
await generateTestImage(filepath, {
width,
height,
format: 'jpeg',
quality: 85,
background: { r: 50 + i * 20, g: 100 + i * 10, b: 150 + i * 15 }
})
paths.push(filepath)
}
return paths
}
/**
* Copy a file and change extension (for testing GIF, SVG, ICO handling)
*/
export async function createSpecialFormatFiles(baseDir: string): Promise<{
gif: string
svg: string
ico: string
}> {
await fs.ensureDir(baseDir)
// Create a simple SVG
const svgPath = path.join(baseDir, 'test-image.svg')
const svgContent = `
<svg width="400" height="400" xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" fill="#3498db"/>
<circle cx="200" cy="200" r="100" fill="#e74c3c"/>
<text x="200" y="210" font-size="30" text-anchor="middle" fill="white">SVG</text>
</svg>
`
await fs.writeFile(svgPath, svgContent.trim())
// Create a simple GIF (by generating a PNG and renaming - for testing copy behavior)
const gifPath = path.join(baseDir, 'test-image.gif')
await sharp({
create: {
width: 400,
height: 400,
channels: 4,
background: { r: 255, g: 0, b: 0, alpha: 1 }
}
})
.png()
.toFile(gifPath.replace('.gif', '.png'))
// Rename to .gif (actual GIF encoding is complex, we just test the extension handling)
await fs.rename(gifPath.replace('.gif', '.png'), gifPath)
// Create a simple ICO (similar approach - test extension handling)
const icoPath = path.join(baseDir, 'test-image.ico')
await sharp({
create: {
width: 32,
height: 32,
channels: 4,
background: { r: 0, g: 0, b: 255, alpha: 1 }
}
})
.png()
.toFile(icoPath.replace('.ico', '.png'))
await fs.rename(icoPath.replace('.ico', '.png'), icoPath)
return { gif: gifPath, svg: svgPath, ico: icoPath }
}
/**
* Get the size of a file in MB
*/
export async function getFileSizeMB(filepath: string): Promise<number> {
const stats = await fs.stat(filepath)
return stats.size / (1024 * 1024)
}
/**
* Get total size of multiple files in MB
*/
export async function getTotalSizeMB(filepaths: string[]): Promise<number> {
let total = 0
for (const filepath of filepaths) {
total += await getFileSizeMB(filepath)
}
return total
}
|