Skip to content

Instantly share code, notes, and snippets.

@sanelson
Created April 5, 2020 00:17
Show Gist options
  • Save sanelson/8e15a81a7c09f469dea32d6f2316264b to your computer and use it in GitHub Desktop.
Save sanelson/8e15a81a7c09f469dea32d6f2316264b to your computer and use it in GitHub Desktop.
RAW image file dump example for LibRaw
/* -*- C++ -*-
* File: raw_dump.cpp
* Copyright 2008-2019 LibRaw LLC (info@libraw.org)
* Created: Sat Mar 14, 2020
*
* LibRaw sample
* Demosaic RAW image and write color channel data to stdout
*
* The default is to output all color channels, but a combination of channels can also be selected
* Inspiration taken from the rawtran RAW to FITS conversion wrapper for dcraw
*
LibRaw is free software; you can redistribute it and/or modify
it under the terms of the one of two licenses as you choose:
1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
(See file LICENSE.LGPL provided in LibRaw distribution archive for details).
2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
(See file LICENSE.CDDL provided in LibRaw distribution archive for details).
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "libraw/libraw.h"
#ifndef LIBRAW_WIN32_CALLS
#include <netinet/in.h>
#else
#include <winsock2.h>
#endif
#ifdef LIBRAW_WIN32_CALLS
#define snprintf _snprintf
#endif
int main(int ac, char *av[])
{
int i, ret;
int color_channel = 0, output_fullcolor = 1, output_tiff = 1, output_fits = 0, output_stdout = 0;
static int verbosity = 0;
char outfn[1024];
LibRaw RawProcessor;
if (ac < 2)
{
usage:
printf("raw_dump - LibRaw %s sample. %d cameras supported\n"
"Usage: %s raw-files....\n"
"\t-c - Image channel index to extract { 0 => 'R', 1 => 'G1', 2 => 'B', 3 => 'G2' }\n"
"\t-T - Output TIFF format image (default)\n"
"\t-F - Output FITS format image\n"
"\t-o - Send color channel data to STDOUT in PPM format\n",
LibRaw::version(), LibRaw::cameraCount(), av[0]);
return 0;
}
#define P1 RawProcessor.imgdata.idata
#define S RawProcessor.imgdata.sizes
#define C RawProcessor.imgdata.color
#define T RawProcessor.imgdata.thumbnail
#define P2 RawProcessor.imgdata.other
#define OUT RawProcessor.imgdata.params
/* Define our standard options, generally we want the RAW data untouched */
OUT.output_bps = 16;
OUT.user_flip = 0;
OUT.no_auto_bright = 1;
OUT.half_size = 1;
OUT.no_interpolation = 1;
OUT.gamm[0] = OUT.gamm[1] = 1;
for (i = 1; i < ac; i++)
{
if (av[i][0] == '-')
{
if (av[i][1] == 'c' && av[i][2] == 0)
{
i++;
color_channel = av[i] ? atoi(av[i]) : 0;
output_fullcolor = 0;
}
else if (av[i][1] == 'T' && av[i][2] == 0)
{
output_tiff = 1;
output_fits = 0;
output_stdout = 0;
OUT.output_tiff = 1;
}
else if (av[i][1] == 'F' && av[i][2] == 0)
{
output_tiff = 0;
output_fits = 1;
output_stdout = 0;
OUT.output_tiff = 0;
}
else if (av[i][1] == 'o' && av[i][2] == 0)
{
output_tiff = 0;
output_fits = 0;
output_stdout = 1;
OUT.output_tiff = 0;
verbosity = 0;
snprintf(outfn,sizeof(outfn),"-");
}
else
goto usage;
continue;
}
int c;
if (verbosity)
{
printf("Processing file %s\n", av[i]);
}
if ((ret = RawProcessor.open_file(av[i])) != LIBRAW_SUCCESS)
{
fprintf(stderr, "Cannot open %s: %s\n", av[i], libraw_strerror(ret));
continue; // no recycle b/c open file will recycle itself
}
if (P1.is_foveon)
{
printf("Cannot process Foveon image %s\n", av[i]);
continue;
}
if ((ret = RawProcessor.unpack()) != LIBRAW_SUCCESS)
{
fprintf(stderr, "Cannot unpack %s: %s\n", av[i], libraw_strerror(ret));
continue;
}
RawProcessor.raw2image();
// hack to make dcraw tiff writer happy
int isrgb = (P1.colors == 4 ? 0 : 1);
S.width = S.iwidth;
S.height = S.iheight;
if (!output_fullcolor)
{
P1.colors = 1;
for (int rc = 0; rc < S.iheight * S.iwidth; rc++)
{
RawProcessor.imgdata.image[rc][0] = RawProcessor.imgdata.image[rc][color_channel];
}
char lname[8];
if (isrgb)
{
snprintf(lname, 7, "%c", ((char *)("RGBG"))[color_channel]);
if (color_channel == 3)
{
strcat(lname, "2");
}
else if (color_channel == 1)
{
strcat(lname, "1");
}
}
else
snprintf(lname, 7, "%c", ((char *)("GCMY"))[color_channel]);
if (!output_stdout)
{
snprintf(outfn, sizeof(outfn), "%s.%s.tiff", av[i], lname);
}
}
else
{
if (!output_stdout)
{
snprintf(outfn, sizeof(outfn), "%s.tiff", av[i]);
}
}
if (verbosity)
{
printf("Writing file %s\n", outfn);
}
if (LIBRAW_SUCCESS != (ret = RawProcessor.dcraw_ppm_tiff_writer(outfn)))
fprintf(stderr, "Cannot write %s: %s\n", outfn, libraw_strerror(ret));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment