mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-24 22:59:25 +00:00
feat(transcode): add dcraw_emu fallback for RAW image preview generation (#446)
This commit is contained in:
@@ -3,6 +3,8 @@ import * as fs from 'fs'
|
||||
import {
|
||||
extractEmbeddedJpeg,
|
||||
validateExtractedJpeg,
|
||||
validateExtractedImage,
|
||||
decodeRawWithDcraw,
|
||||
extractAndValidateRawPreview,
|
||||
EXIF_ORIENTATION_TO_ROTATION,
|
||||
} from './raw-extract'
|
||||
@@ -26,13 +28,20 @@ vi.mock('sharp', () => {
|
||||
return { default: mockSharp }
|
||||
})
|
||||
|
||||
// Mock child_process
|
||||
vi.mock('child_process', () => ({
|
||||
execFile: vi.fn(),
|
||||
}))
|
||||
|
||||
import { exiftool } from 'exiftool-vendored'
|
||||
import sharp from 'sharp'
|
||||
import { execFile } from 'child_process'
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const mockMetadata = (sharp as any)._mockMetadata as ReturnType<typeof vi.fn>
|
||||
const mockExtractBinaryTag = vi.mocked(exiftool.extractBinaryTag)
|
||||
const mockRead = vi.mocked(exiftool.read)
|
||||
const mockExecFile = vi.mocked(execFile)
|
||||
|
||||
describe('extractEmbeddedJpeg', () => {
|
||||
beforeEach(() => {
|
||||
@@ -202,16 +211,231 @@ describe('validateExtractedJpeg', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('validateExtractedImage', () => {
|
||||
it('is an alias for validateExtractedJpeg', () => {
|
||||
expect(validateExtractedImage).toBe(validateExtractedJpeg)
|
||||
})
|
||||
})
|
||||
|
||||
describe('decodeRawWithDcraw', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('decodes RAW using camera white balance, sRGB, and TIFF output without -h when longest dimension <= 8192', async () => {
|
||||
mockRead.mockResolvedValueOnce({
|
||||
ImageWidth: 6000,
|
||||
ImageHeight: 4000,
|
||||
Orientation: 1,
|
||||
} as never)
|
||||
|
||||
let executedArgs: string[] = []
|
||||
mockExecFile.mockImplementationOnce((_bin, args, _opts, cb) => {
|
||||
executedArgs = args as string[]
|
||||
const tiffDest = (args as string[])[(args as string[]).indexOf('-Z') + 1]
|
||||
fs.writeFileSync(tiffDest, 'tiff-data')
|
||||
const callback = typeof _opts === 'function' ? _opts : cb
|
||||
if (callback) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(callback as any)(null, { stdout: '', stderr: '' })
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return {} as any
|
||||
})
|
||||
|
||||
const result = await decodeRawWithDcraw('/path/to/photo.cr2')
|
||||
|
||||
expect(result).not.toBeNull()
|
||||
expect(fs.existsSync(result!.previewPath)).toBe(true)
|
||||
expect(result!.orientation).toBeUndefined()
|
||||
expect(result!.rawWidth).toBe(6000)
|
||||
expect(result!.rawHeight).toBe(4000)
|
||||
|
||||
expect(executedArgs).toContain('-w')
|
||||
expect(executedArgs).toContain('-o')
|
||||
expect(executedArgs).toContain('1')
|
||||
expect(executedArgs).toContain('-T')
|
||||
expect(executedArgs).toContain('-Z')
|
||||
expect(executedArgs).not.toContain('-h')
|
||||
expect(executedArgs[executedArgs.length - 1]).toBe('/path/to/photo.cr2')
|
||||
|
||||
result!.cleanup()
|
||||
expect(fs.existsSync(result!.previewPath)).toBe(false)
|
||||
})
|
||||
|
||||
it('enables half-size (-h) when longest dimension > 8192px and computes upright raw dimensions', async () => {
|
||||
mockRead.mockResolvedValueOnce({
|
||||
ImageWidth: 9504,
|
||||
ImageHeight: 6336,
|
||||
Orientation: 6,
|
||||
} as never)
|
||||
|
||||
let executedArgs: string[] = []
|
||||
mockExecFile.mockImplementationOnce((_bin, args, _opts, cb) => {
|
||||
executedArgs = args as string[]
|
||||
const tiffDest = (args as string[])[(args as string[]).indexOf('-Z') + 1]
|
||||
fs.writeFileSync(tiffDest, 'tiff-data')
|
||||
const callback = typeof _opts === 'function' ? _opts : cb
|
||||
if (callback) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(callback as any)(null, { stdout: '', stderr: '' })
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return {} as any
|
||||
})
|
||||
|
||||
const result = await decodeRawWithDcraw('/path/to/photo.arw')
|
||||
|
||||
expect(result).not.toBeNull()
|
||||
expect(executedArgs).toContain('-h')
|
||||
expect(result!.orientation).toBeUndefined()
|
||||
// For orientation 6 (90 CW), upright raw dimensions are swapped
|
||||
expect(result!.rawWidth).toBe(6336)
|
||||
expect(result!.rawHeight).toBe(9504)
|
||||
|
||||
result!.cleanup()
|
||||
})
|
||||
|
||||
it('omits -h when longest dimension is exactly 8192px', async () => {
|
||||
mockRead.mockResolvedValueOnce({
|
||||
ImageWidth: 8192,
|
||||
ImageHeight: 5464,
|
||||
Orientation: 1,
|
||||
} as never)
|
||||
|
||||
let executedArgs: string[] = []
|
||||
mockExecFile.mockImplementationOnce((_bin, args, _opts, cb) => {
|
||||
executedArgs = args as string[]
|
||||
const tiffDest = (args as string[])[(args as string[]).indexOf('-Z') + 1]
|
||||
fs.writeFileSync(tiffDest, 'tiff-data')
|
||||
const callback = typeof _opts === 'function' ? _opts : cb
|
||||
if (callback) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(callback as any)(null, { stdout: '', stderr: '' })
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return {} as any
|
||||
})
|
||||
|
||||
const result = await decodeRawWithDcraw('/path/to/photo.nef')
|
||||
|
||||
expect(result).not.toBeNull()
|
||||
expect(executedArgs).not.toContain('-h')
|
||||
result!.cleanup()
|
||||
})
|
||||
|
||||
it('defaults to full size without -h when EXIF metadata cannot be read', async () => {
|
||||
mockRead.mockRejectedValueOnce(new Error('Exif read failure'))
|
||||
|
||||
let executedArgs: string[] = []
|
||||
mockExecFile.mockImplementationOnce((_bin, args, _opts, cb) => {
|
||||
executedArgs = args as string[]
|
||||
const tiffDest = (args as string[])[(args as string[]).indexOf('-Z') + 1]
|
||||
fs.writeFileSync(tiffDest, 'tiff-data')
|
||||
const callback = typeof _opts === 'function' ? _opts : cb
|
||||
if (callback) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(callback as any)(null, { stdout: '', stderr: '' })
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return {} as any
|
||||
})
|
||||
|
||||
const result = await decodeRawWithDcraw('/path/to/photo.dng')
|
||||
|
||||
expect(result).not.toBeNull()
|
||||
expect(executedArgs).not.toContain('-h')
|
||||
result!.cleanup()
|
||||
})
|
||||
|
||||
it('returns null and cleans up when dcraw_emu exits with non-zero code', async () => {
|
||||
mockRead.mockResolvedValueOnce({ ImageWidth: 6000, ImageHeight: 4000 } as never)
|
||||
mockExecFile.mockImplementationOnce((_bin, _args, _opts, cb) => {
|
||||
const callback = typeof _opts === 'function' ? _opts : cb
|
||||
const err = new Error('Command failed: dcraw_emu')
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(err as any).code = 1
|
||||
if (callback) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(callback as any)(err)
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return {} as any
|
||||
})
|
||||
|
||||
const result = await decodeRawWithDcraw('/path/to/photo.cr2')
|
||||
|
||||
expect(result).toBeNull()
|
||||
})
|
||||
|
||||
it('returns null and cleans up when dcraw_emu times out', async () => {
|
||||
mockRead.mockResolvedValueOnce({ ImageWidth: 6000, ImageHeight: 4000 } as never)
|
||||
mockExecFile.mockImplementationOnce((_bin, _args, _opts, cb) => {
|
||||
const callback = typeof _opts === 'function' ? _opts : cb
|
||||
const err = new Error('Process timed out')
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(err as any).code = 'ETIMEDOUT'
|
||||
if (callback) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(callback as any)(err)
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return {} as any
|
||||
})
|
||||
|
||||
const result = await decodeRawWithDcraw('/path/to/photo.cr2')
|
||||
|
||||
expect(result).toBeNull()
|
||||
})
|
||||
|
||||
it('returns null and cleans up when dcraw_emu is not found (ENOENT)', async () => {
|
||||
mockRead.mockResolvedValueOnce({ ImageWidth: 6000, ImageHeight: 4000 } as never)
|
||||
mockExecFile.mockImplementationOnce((_bin, _args, _opts, cb) => {
|
||||
const callback = typeof _opts === 'function' ? _opts : cb
|
||||
const err = new Error('spawn dcraw_emu ENOENT')
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(err as any).code = 'ENOENT'
|
||||
if (callback) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(callback as any)(err)
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return {} as any
|
||||
})
|
||||
|
||||
const result = await decodeRawWithDcraw('/path/to/photo.cr2')
|
||||
|
||||
expect(result).toBeNull()
|
||||
})
|
||||
|
||||
it('returns null and cleans up when output TIFF is missing or empty', async () => {
|
||||
mockRead.mockResolvedValueOnce({ ImageWidth: 6000, ImageHeight: 4000 } as never)
|
||||
mockExecFile.mockImplementationOnce((_bin, _args, _opts, cb) => {
|
||||
const callback = typeof _opts === 'function' ? _opts : cb
|
||||
if (callback) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(callback as any)(null, { stdout: '', stderr: '' })
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return {} as any
|
||||
})
|
||||
|
||||
const result = await decodeRawWithDcraw('/path/to/photo.cr2')
|
||||
|
||||
expect(result).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('extractAndValidateRawPreview', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('returns previewPath, dimensions, orientation, and cleanup for valid RAW', async () => {
|
||||
it('returns previewPath, dimensions, orientation, and cleanup for valid RAW using embedded JPEG first', async () => {
|
||||
mockExtractBinaryTag.mockImplementationOnce(async (_tag, _src, dest) => {
|
||||
fs.writeFileSync(dest, 'jpeg-data')
|
||||
})
|
||||
mockRead.mockResolvedValueOnce({ Orientation: 6 } as never)
|
||||
mockRead.mockResolvedValueOnce({ Orientation: 6, ImageWidth: 4000, ImageHeight: 3000 } as never)
|
||||
mockMetadata.mockResolvedValueOnce({ width: 4000, height: 3000 })
|
||||
|
||||
const result = await extractAndValidateRawPreview('/path/to/photo.cr2')
|
||||
@@ -221,35 +445,133 @@ describe('extractAndValidateRawPreview', () => {
|
||||
expect(result!.width).toBe(4000)
|
||||
expect(result!.height).toBe(3000)
|
||||
expect(result!.orientation).toBe(6)
|
||||
expect(mockExecFile).not.toHaveBeenCalled()
|
||||
|
||||
result!.cleanup()
|
||||
expect(fs.existsSync(result!.previewPath)).toBe(false)
|
||||
})
|
||||
|
||||
it('returns null when extraction fails', async () => {
|
||||
it('falls back to dcraw_emu when embedded preview extraction fails', async () => {
|
||||
mockExtractBinaryTag
|
||||
.mockRejectedValueOnce(new Error('fail'))
|
||||
.mockRejectedValueOnce(new Error('fail'))
|
||||
.mockRejectedValueOnce(new Error('fail'))
|
||||
|
||||
mockRead.mockResolvedValueOnce({
|
||||
ImageWidth: 6000,
|
||||
ImageHeight: 4000,
|
||||
Orientation: 1,
|
||||
} as never)
|
||||
|
||||
mockExecFile.mockImplementationOnce((_bin, args, _opts, cb) => {
|
||||
const tiffDest = (args as string[])[(args as string[]).indexOf('-Z') + 1]
|
||||
fs.writeFileSync(tiffDest, 'tiff-data')
|
||||
const callback = typeof _opts === 'function' ? _opts : cb
|
||||
if (callback) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(callback as any)(null, { stdout: '', stderr: '' })
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return {} as any
|
||||
})
|
||||
|
||||
mockMetadata.mockResolvedValueOnce({ width: 6000, height: 4000 })
|
||||
|
||||
const result = await extractAndValidateRawPreview('/path/to/photo.cr2')
|
||||
|
||||
expect(result).not.toBeNull()
|
||||
expect(mockExecFile).toHaveBeenCalled()
|
||||
expect(result!.width).toBe(6000)
|
||||
expect(result!.height).toBe(4000)
|
||||
expect(result!.rawWidth).toBe(6000)
|
||||
expect(result!.rawHeight).toBe(4000)
|
||||
expect(result!.orientation).toBeUndefined()
|
||||
|
||||
result!.cleanup()
|
||||
expect(fs.existsSync(result!.previewPath)).toBe(false)
|
||||
})
|
||||
|
||||
it('falls back to dcraw_emu when extracted embedded JPEG is corrupt', async () => {
|
||||
let createdJpegPath = ''
|
||||
mockExtractBinaryTag.mockImplementationOnce(async (_tag, _src, dest) => {
|
||||
createdJpegPath = dest
|
||||
fs.writeFileSync(dest, 'corrupt-jpeg')
|
||||
})
|
||||
mockRead.mockResolvedValueOnce({ Orientation: 1 } as never)
|
||||
mockMetadata.mockRejectedValueOnce(new Error('Not a valid image'))
|
||||
|
||||
// Fallback dcraw_emu
|
||||
mockRead.mockResolvedValueOnce({ ImageWidth: 6000, ImageHeight: 4000 } as never)
|
||||
mockExecFile.mockImplementationOnce((_bin, args, _opts, cb) => {
|
||||
const tiffDest = (args as string[])[(args as string[]).indexOf('-Z') + 1]
|
||||
fs.writeFileSync(tiffDest, 'tiff-data')
|
||||
const callback = typeof _opts === 'function' ? _opts : cb
|
||||
if (callback) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(callback as any)(null, { stdout: '', stderr: '' })
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return {} as any
|
||||
})
|
||||
mockMetadata.mockResolvedValueOnce({ width: 6000, height: 4000 })
|
||||
|
||||
const result = await extractAndValidateRawPreview('/path/to/photo.cr2')
|
||||
|
||||
expect(result).not.toBeNull()
|
||||
expect(fs.existsSync(createdJpegPath)).toBe(false) // Embedded jpeg was cleaned up
|
||||
expect(fs.existsSync(result!.previewPath)).toBe(true)
|
||||
|
||||
result!.cleanup()
|
||||
expect(fs.existsSync(result!.previewPath)).toBe(false)
|
||||
})
|
||||
|
||||
it('returns null when both embedded extraction and dcraw_emu fail', async () => {
|
||||
mockExtractBinaryTag
|
||||
.mockRejectedValueOnce(new Error('fail'))
|
||||
.mockRejectedValueOnce(new Error('fail'))
|
||||
.mockRejectedValueOnce(new Error('fail'))
|
||||
|
||||
mockRead.mockRejectedValueOnce(new Error('fail'))
|
||||
mockExecFile.mockImplementationOnce((_bin, _args, _opts, cb) => {
|
||||
const callback = typeof _opts === 'function' ? _opts : cb
|
||||
if (callback) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(callback as any)(new Error('dcraw failed'))
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return {} as any
|
||||
})
|
||||
|
||||
const result = await extractAndValidateRawPreview('/path/to/photo.cr2')
|
||||
|
||||
expect(result).toBeNull()
|
||||
})
|
||||
|
||||
it('returns null and cleans up when validation fails', async () => {
|
||||
let createdPath = ''
|
||||
mockExtractBinaryTag.mockImplementationOnce(async (_tag, _src, dest) => {
|
||||
createdPath = dest
|
||||
fs.writeFileSync(dest, 'bad-jpeg')
|
||||
it('returns null and cleans up when decoded TIFF is corrupt', async () => {
|
||||
mockExtractBinaryTag
|
||||
.mockRejectedValueOnce(new Error('fail'))
|
||||
.mockRejectedValueOnce(new Error('fail'))
|
||||
.mockRejectedValueOnce(new Error('fail'))
|
||||
|
||||
let createdTiff = ''
|
||||
mockRead.mockResolvedValueOnce({ ImageWidth: 6000, ImageHeight: 4000 } as never)
|
||||
mockExecFile.mockImplementationOnce((_bin, args, _opts, cb) => {
|
||||
createdTiff = (args as string[])[(args as string[]).indexOf('-Z') + 1]
|
||||
fs.writeFileSync(createdTiff, 'corrupt-tiff')
|
||||
const callback = typeof _opts === 'function' ? _opts : cb
|
||||
if (callback) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(callback as any)(null, { stdout: '', stderr: '' })
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return {} as any
|
||||
})
|
||||
mockRead.mockResolvedValueOnce({ Orientation: 1 } as never)
|
||||
mockMetadata.mockRejectedValueOnce(new Error('Not a valid image'))
|
||||
mockMetadata.mockRejectedValueOnce(new Error('Invalid TIFF'))
|
||||
|
||||
const result = await extractAndValidateRawPreview('/path/to/photo.cr2')
|
||||
|
||||
expect(result).toBeNull()
|
||||
expect(fs.existsSync(createdPath)).toBe(false)
|
||||
expect(fs.existsSync(createdTiff)).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -1,15 +1,37 @@
|
||||
import { execFile } from 'child_process'
|
||||
import { exiftool } from 'exiftool-vendored'
|
||||
import sharp from 'sharp'
|
||||
import * as fs from 'fs'
|
||||
import * as os from 'os'
|
||||
import * as path from 'path'
|
||||
import sharp from 'sharp'
|
||||
import { promisify } from 'util'
|
||||
import { logger } from '@shumai/core/src/logger'
|
||||
|
||||
const execFileAsync = promisify(execFile)
|
||||
|
||||
export interface RawExtractResult {
|
||||
previewPath: string
|
||||
cleanup: () => void
|
||||
/** EXIF orientation from the RAW container (1–8), or undefined if absent. */
|
||||
/** EXIF orientation from the RAW container (1–8), or undefined if absent or already applied. */
|
||||
orientation?: number
|
||||
/** Original unoriented or oriented image dimensions from EXIF if available */
|
||||
rawWidth?: number
|
||||
rawHeight?: number
|
||||
}
|
||||
|
||||
export interface RawPreviewResult {
|
||||
previewPath: string
|
||||
cleanup: () => void
|
||||
width: number
|
||||
height: number
|
||||
orientation?: number
|
||||
rawWidth?: number
|
||||
rawHeight?: number
|
||||
}
|
||||
|
||||
export interface DecodeRawOptions {
|
||||
timeoutMs?: number
|
||||
signal?: AbortSignal
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,22 +90,37 @@ export async function extractEmbeddedJpeg(
|
||||
if (fs.existsSync(candidatePath) && fs.statSync(candidatePath).size > 0) {
|
||||
logger.debug({ tag, rawFilePath, candidatePath }, 'Extracted embedded JPEG from RAW')
|
||||
|
||||
// Read orientation from the RAW container's EXIF metadata.
|
||||
// The extracted JPEG binary stream often does NOT carry its own
|
||||
// orientation tag, so we must read it from the parent RAW file
|
||||
// to avoid incorrectly-rotated previews.
|
||||
// Read orientation and container dimensions from the RAW container's EXIF metadata.
|
||||
let orientation: number | undefined
|
||||
let rawWidth: number | undefined
|
||||
let rawHeight: number | undefined
|
||||
try {
|
||||
const exif = await exiftool.read(rawFilePath)
|
||||
const rawOrientation = exif.Orientation
|
||||
if (typeof rawOrientation === 'number' && rawOrientation >= 1 && rawOrientation <= 8) {
|
||||
orientation = rawOrientation
|
||||
}
|
||||
const w =
|
||||
typeof exif.ImageWidth === 'number'
|
||||
? exif.ImageWidth
|
||||
: typeof exif.ExifImageWidth === 'number'
|
||||
? exif.ExifImageWidth
|
||||
: undefined
|
||||
const h =
|
||||
typeof exif.ImageHeight === 'number'
|
||||
? exif.ImageHeight
|
||||
: typeof exif.ExifImageHeight === 'number'
|
||||
? exif.ExifImageHeight
|
||||
: undefined
|
||||
if (w && h && w > 0 && h > 0) {
|
||||
rawWidth = w
|
||||
rawHeight = h
|
||||
}
|
||||
} catch {
|
||||
logger.debug({ rawFilePath }, 'Could not read EXIF orientation from RAW container')
|
||||
}
|
||||
|
||||
return { previewPath: candidatePath, cleanup, orientation }
|
||||
return { previewPath: candidatePath, cleanup, orientation, rawWidth, rawHeight }
|
||||
}
|
||||
} catch {
|
||||
logger.debug({ tag, rawFilePath }, `Could not extract ${tag} from RAW file`)
|
||||
@@ -103,10 +140,123 @@ export async function extractEmbeddedJpeg(
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that an extracted JPEG file is decodable.
|
||||
* Fallback RAW decoder using dcraw_emu (from LibRaw).
|
||||
*
|
||||
* Used when no usable embedded JPEG preview exists in the RAW file.
|
||||
*
|
||||
* Decoding options:
|
||||
* - Camera white balance and sRGB output (-w -o 1)
|
||||
* - Output temporary TIFF (-T -Z <tempPath>)
|
||||
* - Half-size processing (-h) enabled only when longest dimension > 8192px
|
||||
*
|
||||
* LibRaw auto-rotates the output image to upright orientation based on camera EXIF,
|
||||
* so the returned orientation is undefined to avoid double-rotation in downstream Sharp.
|
||||
*/
|
||||
export async function decodeRawWithDcraw(
|
||||
rawFilePath: string,
|
||||
targetDir?: string,
|
||||
options?: DecodeRawOptions,
|
||||
): Promise<RawExtractResult | null> {
|
||||
const ownTempDir = !targetDir
|
||||
const workDir = targetDir ?? fs.mkdtempSync(path.join(os.tmpdir(), 'raw-decode-'))
|
||||
const tempTiffPath = path.join(
|
||||
workDir,
|
||||
`decoded-${Date.now()}-${Math.random().toString(36).slice(2, 8)}.tiff`,
|
||||
)
|
||||
|
||||
const cleanup = () => {
|
||||
try {
|
||||
if (ownTempDir && fs.existsSync(workDir)) {
|
||||
fs.rmSync(workDir, { recursive: true, force: true })
|
||||
} else if (fs.existsSync(tempTiffPath)) {
|
||||
fs.rmSync(tempTiffPath, { force: true })
|
||||
}
|
||||
} catch (err) {
|
||||
logger.warn({ workDir, tempTiffPath, err }, 'Failed to clean up dcraw temporary files')
|
||||
}
|
||||
}
|
||||
|
||||
// 1. Probe RAW dimensions and orientation via ExifTool to check for 8192px threshold
|
||||
let useHalfSize = false
|
||||
let rawWidth: number | undefined
|
||||
let rawHeight: number | undefined
|
||||
try {
|
||||
const exif = await exiftool.read(rawFilePath)
|
||||
const w =
|
||||
typeof exif.ImageWidth === 'number'
|
||||
? exif.ImageWidth
|
||||
: typeof exif.ExifImageWidth === 'number'
|
||||
? exif.ExifImageWidth
|
||||
: undefined
|
||||
const h =
|
||||
typeof exif.ImageHeight === 'number'
|
||||
? exif.ImageHeight
|
||||
: typeof exif.ExifImageHeight === 'number'
|
||||
? exif.ExifImageHeight
|
||||
: undefined
|
||||
|
||||
if (w && h && w > 0 && h > 0) {
|
||||
const longest = Math.max(w, h)
|
||||
if (longest > 8192) {
|
||||
useHalfSize = true
|
||||
}
|
||||
|
||||
// Compute upright raw dimensions matching dcraw_emu's auto-rotation
|
||||
const rawOrientation = exif.Orientation
|
||||
const isSwapped =
|
||||
typeof rawOrientation === 'number' && rawOrientation >= 5 && rawOrientation <= 8
|
||||
rawWidth = isSwapped ? h : w
|
||||
rawHeight = isSwapped ? w : h
|
||||
}
|
||||
} catch (err) {
|
||||
logger.debug({ rawFilePath, err }, 'Could not read EXIF metadata for RAW dimension check')
|
||||
}
|
||||
|
||||
// 2. Build dcraw_emu arguments
|
||||
const args = ['-w', '-o', '1']
|
||||
if (useHalfSize) {
|
||||
args.push('-h')
|
||||
}
|
||||
args.push('-T', '-Z', tempTiffPath, rawFilePath)
|
||||
|
||||
const dcrawBin = process.env.DCRAW_EMU_PATH || 'dcraw_emu'
|
||||
const envTimeout = process.env.DCRAW_EMU_TIMEOUT_MS
|
||||
? parseInt(process.env.DCRAW_EMU_TIMEOUT_MS, 10)
|
||||
: undefined
|
||||
const timeoutMs = options?.timeoutMs ?? (envTimeout && !isNaN(envTimeout) ? envTimeout : 60_000)
|
||||
|
||||
try {
|
||||
logger.debug({ dcrawBin, args, rawFilePath }, 'Executing dcraw_emu fallback')
|
||||
await execFileAsync(dcrawBin, args, {
|
||||
timeout: timeoutMs,
|
||||
signal: options?.signal,
|
||||
})
|
||||
|
||||
if (!fs.existsSync(tempTiffPath) || fs.statSync(tempTiffPath).size === 0) {
|
||||
logger.warn({ rawFilePath, tempTiffPath }, 'dcraw_emu did not produce a valid output TIFF')
|
||||
cleanup()
|
||||
return null
|
||||
}
|
||||
|
||||
return {
|
||||
previewPath: tempTiffPath,
|
||||
cleanup,
|
||||
orientation: undefined, // dcraw_emu already auto-rotates upright based on camera EXIF
|
||||
rawWidth,
|
||||
rawHeight,
|
||||
}
|
||||
} catch (err) {
|
||||
logger.warn({ rawFilePath, err }, 'dcraw_emu failed to decode RAW file')
|
||||
cleanup()
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that an extracted or decoded image file (JPEG, TIFF, etc.) is decodable.
|
||||
* Returns dimensions if valid, null if corrupt/unusable.
|
||||
*/
|
||||
export async function validateExtractedJpeg(
|
||||
export async function validateExtractedImage(
|
||||
filePath: string,
|
||||
): Promise<{ width: number; height: number } | null> {
|
||||
try {
|
||||
@@ -120,34 +270,66 @@ export async function validateExtractedJpeg(
|
||||
}
|
||||
}
|
||||
|
||||
/** Backward compatibility alias */
|
||||
export const validateExtractedJpeg = validateExtractedImage
|
||||
|
||||
/**
|
||||
* Extract and validate an embedded JPEG from a RAW file to a temporary file.
|
||||
* Extract and validate a preview from a RAW file to a temporary file.
|
||||
*
|
||||
* 1. Attempts ExifTool embedded JPEG extraction first.
|
||||
* 2. If no embedded preview is available or extraction fails, falls back to dcraw_emu decoding.
|
||||
*
|
||||
* Returns a validated file path + dimensions + orientation + cleanup function, or null.
|
||||
*/
|
||||
export async function extractAndValidateRawPreview(
|
||||
rawFilePath: string,
|
||||
targetDir?: string,
|
||||
): Promise<{
|
||||
previewPath: string
|
||||
cleanup: () => void
|
||||
width: number
|
||||
height: number
|
||||
orientation?: number
|
||||
} | null> {
|
||||
): Promise<RawPreviewResult | null> {
|
||||
// 1. Try ExifTool embedded JPEG extraction first
|
||||
const extracted = await extractEmbeddedJpeg(rawFilePath, targetDir)
|
||||
if (!extracted) return null
|
||||
|
||||
const dims = await validateExtractedJpeg(extracted.previewPath)
|
||||
if (!dims) {
|
||||
logger.warn({ rawFilePath }, 'Extracted JPEG from RAW is not decodable, skipping preview')
|
||||
if (extracted) {
|
||||
const dims = await validateExtractedImage(extracted.previewPath)
|
||||
if (dims) {
|
||||
return {
|
||||
previewPath: extracted.previewPath,
|
||||
cleanup: extracted.cleanup,
|
||||
orientation: extracted.orientation,
|
||||
rawWidth: extracted.rawWidth,
|
||||
rawHeight: extracted.rawHeight,
|
||||
...dims,
|
||||
}
|
||||
}
|
||||
logger.warn(
|
||||
{ rawFilePath },
|
||||
'Extracted JPEG from RAW is not decodable, falling back to dcraw_emu',
|
||||
)
|
||||
extracted.cleanup()
|
||||
} else {
|
||||
logger.info(
|
||||
{ rawFilePath },
|
||||
'No usable embedded JPEG found in RAW file, falling back to dcraw_emu',
|
||||
)
|
||||
}
|
||||
|
||||
// 2. Fallback to dcraw_emu
|
||||
const decoded = await decodeRawWithDcraw(rawFilePath, targetDir)
|
||||
if (!decoded) {
|
||||
return null
|
||||
}
|
||||
|
||||
const dims = await validateExtractedImage(decoded.previewPath)
|
||||
if (!dims) {
|
||||
logger.warn({ rawFilePath }, 'Decoded TIFF from dcraw_emu is not decodable, skipping preview')
|
||||
decoded.cleanup()
|
||||
return null
|
||||
}
|
||||
|
||||
return {
|
||||
previewPath: extracted.previewPath,
|
||||
cleanup: extracted.cleanup,
|
||||
orientation: extracted.orientation,
|
||||
previewPath: decoded.previewPath,
|
||||
cleanup: decoded.cleanup,
|
||||
orientation: decoded.orientation,
|
||||
rawWidth: decoded.rawWidth,
|
||||
rawHeight: decoded.rawHeight,
|
||||
...dims,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -506,6 +506,26 @@ describe('TranscodeService', () => {
|
||||
expect(info.mimeType).toBe('')
|
||||
})
|
||||
|
||||
it('should prioritize rawWidth and rawHeight in getImageInfo when available', async () => {
|
||||
const rawPath = path.join(tempDir, 'highres.arw')
|
||||
const cleanupSpy = vi.fn()
|
||||
vi.spyOn(rawExtract, 'extractAndValidateRawPreview').mockResolvedValueOnce({
|
||||
previewPath: '/tmp/decoded-half.tiff',
|
||||
cleanup: cleanupSpy,
|
||||
width: 4752,
|
||||
height: 3168,
|
||||
rawWidth: 9504,
|
||||
rawHeight: 6336,
|
||||
orientation: undefined,
|
||||
})
|
||||
|
||||
const info = await transcodeService.getImageInfo(rawPath)
|
||||
expect(info.originalWidth).toBe(9504)
|
||||
expect(info.originalHeight).toBe(6336)
|
||||
expect(info.mimeType).toBe('jpeg')
|
||||
expect(cleanupSpy).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should transcode RAW image by extracting preview to temp file and feeding to sharp', async () => {
|
||||
const rawPath = path.join(tempDir, 'sample.nef')
|
||||
const outputFile = path.join(tempDir, 'output-raw.webp')
|
||||
@@ -528,6 +548,31 @@ describe('TranscodeService', () => {
|
||||
expect(cleanupSpy).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should transcode RAW image from dcraw_emu fallback TIFF without double-rotating', async () => {
|
||||
const rawPath = path.join(tempDir, 'fallback.dng')
|
||||
const outputFile = path.join(tempDir, 'output-fallback.webp')
|
||||
const cleanupSpy = vi.fn()
|
||||
vi.spyOn(rawExtract, 'extractAndValidateRawPreview').mockResolvedValueOnce({
|
||||
previewPath: '/tmp/decoded-dcraw.tiff',
|
||||
cleanup: cleanupSpy,
|
||||
width: 3168,
|
||||
height: 4752,
|
||||
rawWidth: 6336,
|
||||
rawHeight: 9504,
|
||||
orientation: undefined,
|
||||
})
|
||||
|
||||
await transcodeService.transcodeImage(rawPath, outputFile, 1920, 85)
|
||||
|
||||
expect(sharp).toHaveBeenCalledWith('/tmp/decoded-dcraw.tiff', { limitInputPixels: false })
|
||||
const mockSharp = vi.mocked(sharp).mock.results[vi.mocked(sharp).mock.results.length - 1].value
|
||||
expect(mockSharp.rotate).not.toHaveBeenCalled()
|
||||
expect(mockSharp.toColorspace).toHaveBeenCalledWith('srgb')
|
||||
expect(mockSharp.webp).toHaveBeenCalledWith({ quality: 85 })
|
||||
expect(mockSharp.toFile).toHaveBeenCalledWith(outputFile)
|
||||
expect(cleanupSpy).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should apply orientation rotation when transcoding RAW image', async () => {
|
||||
const rawPath = path.join(tempDir, 'rotated.cr3')
|
||||
const outputFile = path.join(tempDir, 'output-rotated.webp')
|
||||
|
||||
@@ -784,7 +784,7 @@ export class TranscodeService {
|
||||
}
|
||||
}
|
||||
|
||||
// RAW branch — extract embedded JPEG for metadata
|
||||
// RAW branch — extract embedded JPEG or decode RAW for metadata
|
||||
if (typeof input === 'string' && isRawImage(input)) {
|
||||
const extracted = await extractAndValidateRawPreview(input)
|
||||
if (extracted) {
|
||||
@@ -793,8 +793,10 @@ export class TranscodeService {
|
||||
extracted.orientation !== undefined &&
|
||||
extracted.orientation >= 5 &&
|
||||
extracted.orientation <= 8
|
||||
const originalWidth = isSwapped ? extracted.height : extracted.width
|
||||
const originalHeight = isSwapped ? extracted.width : extracted.height
|
||||
const sourceWidth = extracted.rawWidth ?? extracted.width
|
||||
const sourceHeight = extracted.rawHeight ?? extracted.height
|
||||
const originalWidth = isSwapped ? sourceHeight : sourceWidth
|
||||
const originalHeight = isSwapped ? sourceWidth : sourceHeight
|
||||
|
||||
return {
|
||||
originalWidth,
|
||||
@@ -1545,13 +1547,15 @@ export class TranscodeService {
|
||||
input = Buffer.from(await resp.arrayBuffer())
|
||||
}
|
||||
|
||||
// RAW branch — extract embedded JPEG to temporary file + orientation, then treat as normal image
|
||||
// RAW branch — extract embedded preview or decode RAW to temporary file + orientation, then treat as normal image
|
||||
let rawOrientation: number | undefined
|
||||
let rawCleanup: (() => void) | null = null
|
||||
if (typeof input === 'string' && isRawImage(input)) {
|
||||
const extracted = await extractAndValidateRawPreview(input)
|
||||
if (!extracted) {
|
||||
throw new Error(`Cannot generate preview for RAW file: no usable embedded JPEG in ${input}`)
|
||||
throw new Error(
|
||||
`Cannot generate preview for RAW file: no usable embedded preview or decoded image in ${input}`,
|
||||
)
|
||||
}
|
||||
input = extracted.previewPath
|
||||
rawOrientation = extracted.orientation
|
||||
|
||||
Reference in New Issue
Block a user