Beispiel #1
0
jbyteArray Java_org_libraw_LibRaw_getThumbFromBuffer(JNIEnv* env, jclass clazz, jbyteArray bufferBytes) {
	libraw_data_t * data;
	jbyte* buffer;
	jsize len;
	jbyteArray ret;
	libraw_processed_image_t * image;
	
	data = libraw_init(0);
	len = (*env)->GetArrayLength(env, bufferBytes);
	buffer = (*env)->GetByteArrayElements(env, bufferBytes, NULL);
	libraw_open_buffer(data, (void *) buffer, (size_t) len);
	libraw_unpack_thumb(data);
	image = libraw_dcraw_make_mem_thumb(data, 0);
	ret = (*env)->NewByteArray(env, image->data_size);
	(*env)->SetByteArrayRegion(env, ret, 0, image->data_size, (jbyte *) image->data);
	(*env)->ReleaseByteArrayElements(env, bufferBytes, buffer, 0);
	return ret;
}
Beispiel #2
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;
}
Beispiel #3
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;
}