EXTERN_C DLLEXPORT int read_raw_image(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument res) {
	int err;
	int check;
	MImage out;
	char * file;
	libraw_data_t *iprc = libraw_init(0);
	libraw_processed_image_t * img;
	WolframImageLibrary_Functions imgFuns = libData->imageLibraryFunctions;

	err = LIBRARY_FUNCTION_ERROR;
	file = MArgument_getUTF8String(Args[0]);

	libraw_open_file(iprc, file);
	libraw_unpack(iprc);

	iprc->params.output_bps = 8;

	check = libraw_dcraw_process(iprc);
	if (check != LIBRAW_SUCCESS) goto cleanup;

	img = libraw_dcraw_make_mem_image(iprc, &check);
	if (img == NULL) goto cleanup;
	if (img->type != LIBRAW_IMAGE_BITMAP || img->colors != 3) goto cleanup;
	
	if (img->bits == 16) {
		raw_t_ubit16 * raw_data = (raw_t_ubit16*)img->data;
		imgFuns->MImage_new2D(img->width, img->height, 3, MImage_Type_Bit16, MImage_CS_RGB, 1, &out);
		memcpy(imgFuns->MImage_getBit16Data(out), raw_data, img->width * img->height * 3 * sizeof(raw_t_ubit16));
	} else if (img->bits == 8) {
		raw_t_ubit8 * raw_data = (raw_t_ubit8*)img->data;
		imgFuns->MImage_new2D(img->width, img->height, 3, MImage_Type_Bit8, MImage_CS_RGB, 1, &out);
		memcpy(imgFuns->MImage_getByteData(out), raw_data, img->width * img->height * 3 * sizeof(raw_t_ubit8));
	} else {
		goto cleanup;
	}
	
	MArgument_setMImage(res, out);
	err = LIBRARY_NO_ERROR;

cleanup:
	libData->UTF8String_disown(file);
	libraw_dcraw_clear_mem(img);
	return err;
}
Esempio n. 2
0
// thread routine
int process_files(void *q)
{
  int ret;
  int count = 0;
  char outfn[1024], *fn;
  libraw_data_t *iprc = libraw_init(0);

  if (!iprc)
  {
    fprintf(stderr, "Cannot create libraw handle\n");
    return -1;
  }

  while ((fn = get_next_file()))
  {

    iprc->params.half_size = 1; /* dcraw -h */
    iprc->params.use_camera_wb = use_camera_wb;
    iprc->params.use_auto_wb = use_auto_wb;
    iprc->params.output_tiff = tiff_mode;

    ret = libraw_open_file(iprc, fn);
    if (verbose)
      fprintf(stderr, "%s: %s/%s\n", fn, iprc->idata.make, iprc->idata.model);
    HANDLE_ERRORS(ret);

    ret = libraw_unpack(iprc);
    HANDLE_ERRORS(ret);

    ret = libraw_dcraw_process(iprc);
    HANDLE_ERRORS(ret);

    snprintf(outfn, 1023, "%s.%s", fn, tiff_mode ? "tif" : "ppm");

    if (verbose)
      fprintf(stderr, "Writing file %s\n", outfn);
    ret = libraw_dcraw_ppm_tiff_writer(iprc, outfn);
    HANDLE_ERRORS(ret);
    count++;
  }
  libraw_close(iprc);
  printf("Processed %d files\n", count);
  return 0;
}
Esempio n. 3
0
int main(int ac, char *av[])
{

  if (ac != 3)
  {
    fprintf(stderr, "Usage: %s <input_file> <output_file>\n %d", av[0],ac);
    exit(1);
  }


  libraw_data_t *iprc = libraw_init(0);

  if (!iprc)
  {
    fprintf(stderr, "Cannot create libraw handle\n");
    exit(1);
  }

  iprc->params.half_size = 1; /* dcraw -h */
  iprc->params.use_camera_wb = 1; /* dcraw -w */

  char outfn[1024];
  int ret = libraw_open_file(iprc, av[1]);
  HANDLE_ERROR(ret);

  printf("Processing %s (%s %s)\n", av[1], iprc->idata.make, iprc->idata.model);

  ret = libraw_unpack(iprc);
  HANDLE_ERROR(ret);

  ret = libraw_dcraw_process(iprc);
  HANDLE_ERROR(ret);

  strcpy(outfn, av[2]);
  printf("Writing to %s\n", outfn);

  ret = libraw_dcraw_ppm_tiff_writer(iprc, outfn);
  HANDLE_ERROR(ret);

  libraw_close(iprc);
  return 0;
}
Esempio n. 4
0
int main(int ac, char *av[])
{
    int i;
    libraw_data_t *iprc = libraw_init(0);

    if(!iprc)
        {
            fprintf(stderr,"Cannot create libraw handle\n");
            exit(1);
        }

    iprc->params.half_size = 1; /* dcraw -h */
	
    for (i=1;i<ac;i++)
        {
            char outfn[1024];
			int ret = libraw_open_file(iprc,av[i]);
            HANDLE_ALL_ERRORS(ret);
            
            printf("Processing %s (%s %s)\n",av[i],iprc->idata.make,iprc->idata.model);

            ret = libraw_unpack(iprc);
            HANDLE_ALL_ERRORS(ret);

            ret = libraw_dcraw_process(iprc);
            HANDLE_ALL_ERRORS(ret);
            
            strcpy(outfn,av[i]);
            strcat(outfn,".ppm");
            printf("Writing to %s\n",outfn);

            ret = libraw_dcraw_ppm_tiff_writer(iprc,outfn);
            HANDLE_FATAL_ERROR(ret);
        }
    libraw_close(iprc);
    return 0;
}
Esempio n. 5
0
// open a raw file, libraw path:
dt_imageio_retval_t
dt_imageio_open_raw(
    dt_image_t  *img,
    const char  *filename,
    dt_mipmap_cache_allocator_t a)
{
  if(!img->exif_inited)
    (void) dt_exif_read(img, filename);
  int ret;
  libraw_data_t *raw = libraw_init(0);
  libraw_processed_image_t *image = NULL;
  raw->params.half_size = 0; /* dcraw -h */
  raw->params.use_camera_wb = 0;
  raw->params.use_auto_wb = 0;
  raw->params.med_passes = 0;//img->raw_params.med_passes;
  raw->params.no_auto_bright = 1;
  // raw->params.filtering_mode |= LIBRAW_FILTERING_NOBLACKS;
  // raw->params.document_mode = 2; // no color scaling, no black, no max, no wb..?
  raw->params.document_mode = 2; // color scaling (clip,wb,max) and black point, but no demosaic
  raw->params.output_color = 0;
  raw->params.output_bps = 16;
  raw->params.user_flip = -1; // -1 means: use orientation from raw
  raw->params.gamm[0] = 1.0;
  raw->params.gamm[1] = 1.0;
  // raw->params.user_qual = img->raw_params.demosaic_method; // 3: AHD, 2: PPG, 1: VNG
  raw->params.user_qual = 0;
  // raw->params.four_color_rgb = img->raw_params.four_color_rgb;
  raw->params.four_color_rgb = 0;
  raw->params.use_camera_matrix = 0;
  raw->params.green_matching = 0;
  raw->params.highlight = 1;
  raw->params.threshold = 0;
  // raw->params.auto_bright_thr = img->raw_auto_bright_threshold;

  // raw->params.amaze_ca_refine = 0;
  raw->params.fbdd_noiserd    = 0;

  ret = libraw_open_file(raw, filename);
  HANDLE_ERRORS(ret, 0);
  raw->params.user_qual = 0;
  raw->params.half_size = 0;

  ret = libraw_unpack(raw);
  // img->black   = raw->color.black/65535.0;
  // img->maximum = raw->color.maximum/65535.0;
  img->bpp = sizeof(uint16_t);
  // printf("black, max: %d %d %f %f\n", raw->color.black, raw->color.maximum, img->black, img->maximum);
  HANDLE_ERRORS(ret, 1);
  ret = libraw_dcraw_process(raw);
  // ret = libraw_dcraw_document_mode_processing(raw);
  HANDLE_ERRORS(ret, 1);
  image = libraw_dcraw_make_mem_image(raw, &ret);
  HANDLE_ERRORS(ret, 1);

  // fallback for broken exif read in case of phase one H25
  if(!strncmp(img->exif_maker, "Phase One", 9))
    img->orientation = raw->sizes.flip;
  // filters seem only ever to take a useful value after unpack/process
  img->filters = raw->idata.filters;
  img->width  = (img->orientation & 4) ? raw->sizes.height : raw->sizes.width;
  img->height = (img->orientation & 4) ? raw->sizes.width  : raw->sizes.height;
  img->exif_iso = raw->other.iso_speed;
  img->exif_exposure = raw->other.shutter;
  img->exif_aperture = raw->other.aperture;
  img->exif_focal_length = raw->other.focal_len;
  g_strlcpy(img->exif_maker, raw->idata.make, sizeof(img->exif_maker));
  img->exif_maker[sizeof(img->exif_maker) - 1] = 0x0;
  g_strlcpy(img->exif_model, raw->idata.model, sizeof(img->exif_model));
  img->exif_model[sizeof(img->exif_model) - 1] = 0x0;
  dt_gettime_t(img->exif_datetime_taken, raw->other.timestamp);

  void *buf = dt_mipmap_cache_alloc(img, DT_MIPMAP_FULL, a);
  if(!buf)
  {
    libraw_recycle(raw);
    libraw_close(raw);
    free(image);
    return DT_IMAGEIO_CACHE_FULL;
  }
#ifdef _OPENMP
  #pragma omp parallel for schedule(static) default(none) shared(img, image, raw, buf)
#endif
  for(int k=0; k<img->width*img->height; k++)
    ((uint16_t *)buf)[k] = CLAMPS((((uint16_t *)image->data)[k] - raw->color.black)*65535.0f/(float)(raw->color.maximum - raw->color.black), 0, 0xffff);
  // clean up raw stuff.
  libraw_recycle(raw);
  libraw_close(raw);
  free(image);
  raw = NULL;
  image = NULL;

  img->flags &= ~DT_IMAGE_LDR;
  img->flags &= ~DT_IMAGE_HDR;
  img->flags |= DT_IMAGE_RAW;
  return DT_IMAGEIO_OK;
}
Esempio n. 6
0
int main(int argc, char *argv[])
{
    char *buf = NULL;
    size_t buf_size = 0;
    char rbuf[BLOCK_SIZE];
    size_t n;

    for (;;) {
        n = fread(rbuf, sizeof(char), BLOCK_SIZE, stdin);
        buf_size += n;
        buf = realloc(buf, buf_size);
        memcpy(buf + (buf_size - n), rbuf, n);

        if (n < BLOCK_SIZE) {
            if (feof(stdin)) {
                break;
            } else {
                fprintf(stderr, "Read error\n");
                exit(1);
            }
        }
    }

    libraw_data_t *iprc = libraw_init(0);

    if (!iprc) {
        fprintf(stderr,"Cannot create libraw handle\n");
        exit(1);
    }

    iprc->params.user_mul[0] = 1;
    iprc->params.user_mul[1] = 0.5;
    iprc->params.user_mul[2] = 1;
    iprc->params.user_mul[3] = 0.5;
    iprc->params.use_camera_wb = 1;
    iprc->params.use_camera_matrix = 1;
    iprc->params.highlight = 9;
    iprc->params.output_color = 0;
    iprc->params.output_bps = 16;
    iprc->params.user_qual = 1;
    iprc->params.four_color_rgb = 1;
    iprc->params.no_auto_bright = 1;


    int ret = libraw_open_buffer(iprc, buf, buf_size);
    if (ret) libraw_error(ret);

    ret = libraw_unpack(iprc);
    if (ret) libraw_error(ret);

    ret = libraw_dcraw_process(iprc);
    if (ret) libraw_error(ret);

    libraw_processed_image_t *out;
    out = libraw_dcraw_make_mem_image(iprc, &ret);
    if (ret) libraw_error(ret);

    printf("P6\n%d %d\n%d\n", out->width, out->height, out->bits == 16 ? 65535 : 255);

    int i;
    for (i = 0; i < out->data_size; i += 2)
        SWAP(out->data[i], out->data[i+1]);

    n = fwrite(out->data, sizeof(char), out->data_size, stdout);
    fflush(stdout);

    if (n < out->data_size) {
        fprintf(stderr, "Write error\n");
        exit(1);
    }

    return 0;
}
Esempio n. 7
0
static GthImage *
_cairo_image_surface_create_from_raw (GInputStream  *istream,
                                      GthFileData   *file_data,
                                      int            requested_size,
                                      int           *original_width,
                                      int           *original_height,
                                      gpointer       user_data,
                                      GCancellable  *cancellable,
                                      GError       **error)
{
    libraw_data_t *raw_data;
    int            result;
    void          *buffer = NULL;
    size_t         size;
    GthImage      *image = NULL;

    raw_data = libraw_init (LIBRAW_OPIONS_NO_MEMERR_CALLBACK | LIBRAW_OPIONS_NO_DATAERR_CALLBACK);
    if (raw_data == NULL) {
        _libraw_set_gerror (error, errno);
        goto fatal_error;
    }

    libraw_set_progress_handler (raw_data, _libraw_progress_cb, cancellable);

    if (! _g_input_stream_read_all (istream, &buffer, &size, cancellable, error))
        goto fatal_error;

    raw_data->params.output_tiff = FALSE;
    raw_data->params.use_camera_wb = TRUE;
    raw_data->params.use_rawspeed = TRUE;
    raw_data->params.highlight = FALSE;
    raw_data->params.use_camera_matrix = TRUE;
    raw_data->params.output_color = RAW_OUTPUT_COLOR_SRGB;
    raw_data->params.output_bps = 8;
    raw_data->params.half_size = (requested_size > 0);

    result = libraw_open_buffer (raw_data, buffer, size);
    if (LIBRAW_FATAL_ERROR (result)) {
        _libraw_set_gerror (error, result);
        goto fatal_error;
    }

    /*  */

#if RAW_USE_EMBEDDED_THUMBNAIL

    if (requested_size > 0) {

        /* read the thumbnail */

        result = libraw_unpack_thumb (raw_data);
        if (result != LIBRAW_SUCCESS) {
            _libraw_set_gerror (error, result);
            goto fatal_error;
        }

        switch (raw_data->thumbnail.tformat) {
        case LIBRAW_THUMBNAIL_JPEG:
            image = _libraw_read_jpeg_data (raw_data->thumbnail.thumb,
                                            raw_data->thumbnail.tlength,
                                            requested_size,
                                            cancellable,
                                            error);
            break;
        case LIBRAW_THUMBNAIL_BITMAP:
            image = _libraw_read_bitmap_data (raw_data->thumbnail.twidth,
                                              raw_data->thumbnail.theight,
                                              raw_data->thumbnail.tcolors,
                                              8,
                                              (guchar *) raw_data->thumbnail.thumb,
                                              raw_data->thumbnail.tlength);
            break;
        default:
            g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, "Unsupported data format");
            break;
        }

        if ((image != NULL) && (_libraw_get_tranform (raw_data) != GTH_TRANSFORM_NONE)) {
            cairo_surface_t *surface;
            cairo_surface_t *rotated;

            surface = gth_image_get_cairo_surface (image);
            rotated = _cairo_image_surface_transform (surface, _libraw_get_tranform (raw_data));
            gth_image_set_cairo_surface (image, rotated);

            cairo_surface_destroy (rotated);
            cairo_surface_destroy (surface);
        }
    }
    else

#endif

    {
        /* read the image */

        libraw_processed_image_t *processed_image;

        result = libraw_unpack (raw_data);
        if (result != LIBRAW_SUCCESS) {
            _libraw_set_gerror (error, result);
            goto fatal_error;
        }

        result = libraw_dcraw_process (raw_data);
        if (result != LIBRAW_SUCCESS) {
            _libraw_set_gerror (error, result);
            goto fatal_error;
        }

        processed_image = libraw_dcraw_make_mem_image (raw_data, &result);
        if (result != LIBRAW_SUCCESS) {
            _libraw_set_gerror (error, result);
            goto fatal_error;
        }

        switch (processed_image->type) {
        case LIBRAW_IMAGE_JPEG:
            image = _libraw_read_jpeg_data (processed_image->data,
                                            processed_image->data_size,
                                            -1,
                                            cancellable,
                                            error);
            break;
        case LIBRAW_IMAGE_BITMAP:
            image = _libraw_read_bitmap_data (processed_image->width,
                                              processed_image->height,
                                              processed_image->colors,
                                              processed_image->bits,
                                              processed_image->data,
                                              processed_image->data_size);
            break;
        default:
            g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, "Unsupported data format");
            break;
        }

        libraw_dcraw_clear_mem (processed_image);
    }

    /* get the original size */

    if ((original_width != NULL) && (original_height != NULL)) {
        result = libraw_adjust_sizes_info_only (raw_data);
        if (result != LIBRAW_SUCCESS) {
            _libraw_set_gerror (error, result);
            goto fatal_error;
        }

        *original_width = raw_data->sizes.iwidth;
        *original_height = raw_data->sizes.iheight;
    }

fatal_error:

    if (raw_data != NULL)
        libraw_close (raw_data);
    g_free (buffer);

    return image;
}