I have a canon 5D and i am shooting in raw, as you probably familiar, canon uses proprietary CR2.
I have been trying with no success to generate an hdr image until now.
With the help of chatgpt, we debugged the raw2hdr script and we made the following changes:
Key Differences from the Original Script
- Simplified input pipeline
- The original script used
dcraw -t 0 -4 | ra_ppm | ra_tiff
to generate 16-bit TIFFs. - This failed because your build of
hdrgen
did not support TIFF/PPM/HDR inputs. - The new script uses
dcraw -t 0 -h -w -c | pnmtojpeg
to create 8-bit JPGs, which yourhdrgen
accepts (24-bit RGB requirement
).
- Removed Radiance utilities dependency
- No more use of
ra_ppm
,ra_tiff
, or Radiance.hdr
intermediate files. - Conversion goes directly from RAW → JPG with Netpbm/ImageMagick tools.
- Stonits calculation reimplemented with
awk
- The original script relied on
rcalc
+/tmp/rawinfo.fmt
to parsedcraw -i -v
output. - This broke because field names in your
dcraw
version didn’t match the expected format. - The new script parses
dcraw
output directly withawk
to compute exposure scaling (stonits
).
- Temporary file handling simplified
- Original script created multiple temp files (
rawinfo.fmt
,rawinfo$.txt
,.tif
, etc.) and deleted them at the end. - New script only creates:
/tmp/rawinfo-debug.txt
(metadata),/tmp/hdrgen-args.txt
(hdrgen arguments),/tmp/sqr.rsp
(response pattern),- plus the temporary JPGs.
- All are cleaned up automatically at the end of the run.
- Removed unsupported/legacy options
- No use of
-q
,-m
,-r
,-s
parsing insidehdrgen
setup. - Script only supports the essential option:
-o <output.hdr> input.CR2...
.
Of course i am not completly sure if the changes are acceptable, so i would like to consult it with you, experts. I am attaching the new script.
I have not been able, until now, to generate hdr from my canon camera, tried using jpg, dngs converted from dnglab until now.
Should i consider another approach ?
Here is the script
#!/bin/bash
#
# raw2hdr - Generar imagen HDR a partir de CR2 usando dcraw + hdrgen
#
# Uso:
# raw2hdr -o salida.hdr *.CR2
#
if [ $# -lt 2 ]; then
echo "Uso: $0 -o salida.hdr input1.CR2 input2.CR2 ..."
exit 1
fi
# --- Parseo de argumentos ---
outfile=""
args=()
while [ $# -gt 0 ]; do
case "$1" in
-o)
shift
outfile="$1"
;;
*)
args+=("$1")
;;
esac
shift
done
if [ -z "$outfile" ]; then
echo "Falta -o salida.hdr"
exit 1
fi
# --- Archivos temporales ---
rawinfo="/tmp/rawinfo-debug.txt"
hdrgen_args="/tmp/hdrgen-args.txt"
sqr="/tmp/sqr.rsp"
# 1. Extraer metadatos de CR2
echo "Extrayendo metadatos con dcraw..."
dcraw -i -v "${args[@]}" > "$rawinfo"
# Normalizar shutter en decimal si viene como fracciĂłn
perl -pi -e 'if (my $test = /^Shutter: (.*)\/(.*) sec/) { my $speed = sprintf("%0.9f",$1/$2); s/^Shutter: $1\/$2 sec/Shutter: $speed sec/ }' "$rawinfo"
# 2. Convertir cada CR2 a JPG 8-bit (24-bit RGB que hdrgen acepta)
echo "Generando JPGs 8-bit..."
jpgs=()
for f in "${args[@]}"; do
base="${f%.*}"
outjpg="${base}_ldr.jpg"
dcraw -t 0 -h -w -c "$f" | pnmtojpeg > "$outjpg"
jpgs+=("$outjpg")
done
# 3. Calcular stonits y armar args para hdrgen
echo "Calculando stonits..."
awk '
/^Filename:/ {
if (have) {
stonits = 161 * ap * ap / (spd * iso);
printf("-s %.6f %s_ldr.jpg\n", stonits, img);
}
fname = $2; sub(/\r$/,"",fname);
split(fname, a, "."); img = a[1];
iso = spd = ap = 0; have = 0;
}
/^(ISO speed:|ISO:)/ { iso = $NF + 0; }
/^Shutter:/ {
if ($2 ~ /\//) { split($2, a, "/"); spd = a[1] / a[2]; }
else { spd = $2 + 0; }
}
/^Aperture:/ { sub(/^Aperture: f\//,""); ap = $0 + 0; }
{ if (img != "" && iso > 0 && spd > 0 && ap > 0) have = 1; }
END {
if (have) {
stonits = 161 * ap * ap / (spd * iso);
printf("-s %.6f %s_ldr.jpg\n", stonits, img);
}
}
' "$rawinfo" > "$hdrgen_args"
# 4. Crear archivo de respuesta (patrĂłn cuadrado)
cat > "$sqr" << _EOF_
2 1 0 0
2 1 0 0
2 1 0 0
_EOF_
# 5. Ejecutar hdrgen
echo "Ejecutando hdrgen..."
hdrgen -m 400 -e -a -r "$sqr" -o "$outfile" $(cat "$hdrgen_args")
res=$?
# 6. Limpiar temporales
rm -f "$rawinfo" "$hdrgen_args" "$sqr"
rm -f "${jpgs[@]}"
exit $res