Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

render raw data to jpeg (hidden behind dev flag) #126

Merged
merged 7 commits into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
"homepage": "https://github.com/catdad/raw-viewer#readme",
"dependencies": {
"dcraw": "^1.0.3",
"dcraw-vendored-darwin": "^9.28.1",
"dcraw-vendored-linux": "^9.28.1",
"dcraw-vendored-win32": "^9.28.1",
"fast-deep-equal": "^2.0.1",
"fs-extra": "^7.0.1",
"lodash": "^4.17.11",
Expand Down
20 changes: 20 additions & 0 deletions public/dcraw-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ async function imageUrl(filepath) {
return data;
}

async function tiffBuffer(filepath) {
return await log.timing('tiff', async () => {
const file = await readFileBuffer(filepath);

// read image from raw data
const tiff = dcraw(file, {
setNoAutoBrightnessMode: true,
useCameraWhiteBalance: true,
exportAsTiff: true,
setFourColorMode: true,
});

return tiff;
});
}

function exec(data) {
function onDone(err, res) {
const result = {
Expand Down Expand Up @@ -67,6 +83,10 @@ function exec(data) {
return execPromise(imageUrl(...data.args));
}

if (data.name === 'tiffBuffer') {
return execPromise(tiffBuffer(...data.args));
}

onDone(new Error(`${data.name} is an unknown worker command`));
}

Expand Down
15 changes: 15 additions & 0 deletions renderer/sidebar/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,21 @@ module.exports = function ({ events }) {
fragment.appendChild(showFullMeta);
fragment.appendChild(download);

if (process.env.RAW_VIEWER_DEV) {
const rawRender = document.createElement('button');
rawRender.innerHTML = 'Render from RAW';
rawRender.onclick = async () => {
const raw = await exiftool.rawRender(filepath);

events.emit('image:load', {
filepath: filepath,
imageUrl: raw,
rotation: 0
});
};
fragment.appendChild(rawRender);
}

elem.innerHTML = '';
elem.appendChild(fragment);
}
Expand Down
48 changes: 48 additions & 0 deletions renderer/tools/dcraw-bin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const { promisify } = require('util');
const { execFile } = require('child_process');

const fs = require('fs-extra');
const dcraw = require(`dcraw-vendored-${process.platform}`);
const sharp = require('sharp');
const log = require('../../lib/log.js')('dcraw-bin');

log.info(`using dcraw at ${dcraw}`);

const exec = async (args, size) => {
try {
const { stdout } = await promisify(execFile)(dcraw, args, {
windowsHide: true,
maxBuffer: size,
encoding: 'buffer'
});
return stdout;
} catch (e) {
return new Error(`dcraw error: ${e.errno || e.code}`);
}
};

const getTiff = async (filepath, size) => {
return await log.timing(`render tiff ${filepath}`, async () => {
return exec(['-w', '-W', '-T', '-c', filepath], size * 5);
});
};

const getJpegPreview = async (filepath, size) => {
return await log.timing(`extract jpeg preview ${filepath}`, async () => {
return exec(['-e', '-c', filepath], size);
});
};

module.exports = async (filepath, { type = 'raw' } = {}) => {
const { size } = await fs.stat(filepath);

if (type === 'preview') {
return await getJpegPreview(filepath, size);
}

const tiff = await getTiff(filepath, size);

return await log.timing(`convert tiff to jpeg ${filepath}`, async () => {
return await sharp(tiff).jpeg().toBuffer();
});
};
11 changes: 10 additions & 1 deletion renderer/tools/exiftool-child.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const ipc = require('electron').ipcRenderer;

const log = require('../../lib/log.js')('exiftool-child');
const dcraw = require('./dcraw.js')(2);
const dcrawBin = require('./dcraw-bin.js');
const bufferToUrl = require('./bufferToUrl.js');

const unknown = (function () {
Expand Down Expand Up @@ -199,12 +200,20 @@ async function copyExif(filepath, targetpath) {
return await exiftool('copy:exif', { filepath, targetpath });
}

async function rawRender(filepath) {
return await log.timing(`render ${filepath} from RAW`, async () => {
const jpeg = await dcrawBin(filepath, { type: 'raw' });
return bufferToUrl(jpeg);
});
}

module.exports = {
isPlainImage,
readMeta,
readShortMeta,
readJpegFromMeta,
readThumbFromMeta,
setRating,
copyExif
copyExif,
rawRender
};