Files
rawTherapee/samples/mem_image_sample.cpp
Lawrence Lee 4c61b7d3c3 Squashed 'rtengine/libraw/' changes from 1ef70158d..12b0e5d60
12b0e5d60 Snapshot 202403
a4c9b1981 loop parameters in remove_trailing_spaces
e231b01a4 CR3-Qstep table: avoid wrong 64-bit code generation
21368133a 0.21.2 release
a6f212a4a tag type => tag size mapping fixed
41506ef73 cubic_spline: better handling of non-integer data
17294b5fd extra metadata check in arq_load_raw
47d9dd8e2 Better incorrect data handling in cubic_spline
3baa51068 skip invalid pattern in xtrans_interpolate
eaf63bf5f Check HL recovery coeffs before processing
a14574080 limit wavelet denoise minimum size
4e597e4e9 Merge pull request #594 from pinotree/path_max
086dcb9a6 raw-identify: use fallback if PATH_MAX not available
0e105f9f8 additional check against corrupted ljpeg layout
0c8b68e9f Disable color conversion for Canon 16-bit thumbnails
cd1abd695 docs/changelog: explained the case when no thumbnail is found in specific file
6fffd414b rename swapXX to libraw_swapXX to avoid name conflict
af78ae48a Check against corrupted LJPEG header in Canon sRAW decoder
ba780e600 Limit embedded color profile allocation/read size
122bf7c5b Wrong alloc result check for 16-bit bitmap thumbnail
e6dd2709e check pana_data/buffer offset before use
ae2dc5884 Check P1 quadrant linearization coeff[15] against zero
f2998bacc avoid integer overflow in buffer space check
443b7fb51 prevent buffer overrun in buffer_datastream::scanf_one
35a6d3615 ensure correct T.tlength for 16b bitmap thumbnails(2)
b69ea8be5 ensure correct T.tlength for 16b bitmap thumbnails
2b6eca897 Do not run sraw decoder on (crafted) bayer files
68808b57f better striped thumbnails handling
9ab70f6dc do not set shrink flag for 3/4 component images
32425dd96 allow more decoders for fuji-rotated RAWs
REVERT: 1ef70158d 0.21.2 release
REVERT: 62f042366 tag type => tag size mapping fixed
REVERT: ee087e3fe cubic_spline: better handling of non-integer data
REVERT: af755b991 extra metadata check in arq_load_raw
REVERT: 0fadd8819 Better incorrect data handling in cubic_spline
REVERT: d7fb66053 skip invalid pattern in xtrans_interpolate
REVERT: d059ed280 Check HL recovery coeffs before processing
REVERT: 104730519 limit wavelet denoise minimum size
REVERT: cae09838e raw-identify: use fallback if PATH_MAX not available
REVERT: d6c677608 additional check against corrupted ljpeg layout
REVERT: 1001a6ac1 Disable color conversion for Canon 16-bit thumbnails
REVERT: a5130b01b docs/changelog: explained the case when no thumbnail is found in specific file
REVERT: 600c0c63d rename swapXX to libraw_swapXX to avoid name conflict
REVERT: 299c8a11b Check against corrupted LJPEG header in Canon sRAW decoder
REVERT: ec8671ad9 Limit embedded color profile allocation/read size
REVERT: 5229d5942 Wrong alloc result check for 16-bit bitmap thumbnail
REVERT: b278b775f check pana_data/buffer offset before use
REVERT: 7f4b8d3af Check P1 quadrant linearization coeff[15] against zero
REVERT: e942a7db6 avoid integer overflow in buffer space check
REVERT: f6a57cfb8 prevent buffer overrun in buffer_datastream::scanf_one
REVERT: 3e62ed304 ensure correct T.tlength for 16b bitmap thumbnails(2)
REVERT: 8e52d81cd ensure correct T.tlength for 16b bitmap thumbnails
REVERT: 8e1af15e2 Do not run sraw decoder on (crafted) bayer files
REVERT: 0ace959c2 better striped thumbnails handling
REVERT: 477e0719f do not set shrink flag for 3/4 component images
REVERT: c8efae6c5 allow more decoders for fuji-rotated RAWs

git-subtree-dir: rtengine/libraw
git-subtree-split: 12b0e5d60c57bb795382fda8494fc45f683550b8
2024-04-13 22:44:59 -07:00

283 lines
7.9 KiB
C++

/* -*- C++ -*-
* File: mem_image.cpp
* Copyright 2008-2024 LibRaw LLC (info@libraw.org)
*
* LibRaw mem_image/mem_thumb API test. Results should be same (bitwise) to
dcraw [-4] [-6] [-e]
* Testing note: for ppm-thumbnails you should use dcraw -w -e for thumbnail
extraction
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"
#ifdef USE_JPEG
#include "jpeglib.h"
#endif
#ifdef LIBRAW_WIN32_CALLS
#define snprintf _snprintf
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#else
#include <netinet/in.h>
#endif
#ifdef USE_JPEG
void write_jpeg(libraw_processed_image_t *img, const char *basename, int quality)
{
char fn[1024];
if(img->colors != 1 && img->colors != 3)
{
printf("Only BW and 3-color images supported for JPEG output\n");
return;
}
snprintf(fn, 1024, "%s.jpg", basename);
FILE *f = fopen(fn, "wb");
if (!f)
return;
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
int row_stride; /* physical row width in image buffer */
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, f);
cinfo.image_width = img->width; /* image width and height, in pixels */
cinfo.image_height = img->height;
cinfo.input_components = img->colors; /* # of color components per pixel */
cinfo.in_color_space = img->colors==3?JCS_RGB:JCS_GRAYSCALE; /* colorspace of input image */
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE);
jpeg_start_compress(&cinfo, TRUE);
row_stride = img->width * img->colors; /* JSAMPLEs per row in image_buffer */
while (cinfo.next_scanline < cinfo.image_height) {
row_pointer[0] = &img->data[cinfo.next_scanline * row_stride];
(void)jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
jpeg_finish_compress(&cinfo);
fclose(f);
jpeg_destroy_compress(&cinfo);
}
#endif
// no error reporting, only params check
void write_ppm(libraw_processed_image_t *img, const char *basename)
{
if (!img)
return;
// type SHOULD be LIBRAW_IMAGE_BITMAP, but we'll check
if (img->type != LIBRAW_IMAGE_BITMAP)
return;
if (img->colors != 3 && img->colors != 1)
{
printf("Only monochrome and 3-color images supported for PPM output\n");
return;
}
char fn[1024];
snprintf(fn, 1024, "%s.p%cm", basename, img->colors==1?'g':'p');
FILE *f = fopen(fn, "wb");
if (!f)
return;
fprintf(f, "P%d\n%d %d\n%d\n", img->colors/2 + 5, img->width, img->height, (1 << img->bits) - 1);
/*
NOTE:
data in img->data is not converted to network byte order.
So, we should swap values on some architectures for dcraw compatibility
(unfortunately, xv cannot display 16-bit PPMs with network byte order data
*/
#define SWAP(a, b) \
{ \
a ^= b; \
a ^= (b ^= a); \
}
if (img->bits == 16 && htons(0x55aa) != 0x55aa)
for (unsigned i = 0; i < img->data_size-1; i += 2)
SWAP(img->data[i], img->data[i + 1]);
#undef SWAP
fwrite(img->data, img->data_size, 1, f);
fclose(f);
}
void write_thumb(libraw_processed_image_t *img, const char *basename)
{
if (!img)
return;
if (img->type == LIBRAW_IMAGE_BITMAP)
{
char fnt[1024];
snprintf(fnt, 1024, "%s.thumb", basename);
write_ppm(img, fnt);
}
else if (img->type == LIBRAW_IMAGE_JPEG)
{
char fn[1024];
snprintf(fn, 1024, "%s.thumb.jpg", basename);
FILE *f = fopen(fn, "wb");
if (!f)
return;
fwrite(img->data, img->data_size, 1, f);
fclose(f);
}
}
int main(int ac, char *av[])
{
int i, ret, output_thumbs = 0;
#ifdef USE_JPEG
int output_jpeg = 0, jpgqual = 90;
#endif
// don't use fixed size buffers in real apps!
LibRaw RawProcessor;
if (ac < 2)
{
printf("mem_image - LibRaw sample, to illustrate work for memory buffers.\n"
"Emulates dcraw [-4] [-1] [-e] [-h]\n"
#ifdef USE_JPEG
"Usage: %s [-D] [-j[nn]] [-T] [-v] [-e] raw-files....\n"
#else
"Usage: %s [-D] [-T] [-v] [-e] raw-files....\n"
#endif
"\t-6 - output 16-bit PPM\n"
"\t-4 - linear 16-bit data\n"
"\t-e - extract thumbnails (same as dcraw -e in separate run)\n"
#ifdef USE_JPEG
"\t-j[qual] - output JPEG with qual quality (e.g. -j90)\n"
#endif
"\t-h - use half_size\n", av[0]);
return 0;
}
putenv((char *)"TZ=UTC"); // dcraw compatibility, affects TIFF datestamp field
#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
for (i = 1; i < ac; i++)
{
if (av[i][0] == '-')
{
if (av[i][1] == '6' && av[i][2] == 0)
OUT.output_bps = 16;
if (av[i][1] == '4' && av[i][2] == 0)
{
OUT.output_bps = 16;
OUT.gamm[0] = OUT.gamm[1] = OUT.no_auto_bright = 1;
}
if (av[i][1] == 'e' && av[i][2] == 0)
output_thumbs++;
if (av[i][1] == 'h' && av[i][2] == 0)
OUT.half_size = 1;
#ifdef USE_JPEG
if (av[i][1] == 'j')
{
output_jpeg = 1;
if(av[i][2] != 0)
jpgqual = atoi(av[i]+2);
}
#endif
continue;
}
#ifdef USE_JPEG
if(output_jpeg && OUT.output_bps>8)
{
printf("JPEG is limited to 8 bit\n");
OUT.output_bps = 8;
}
#endif
printf("Processing %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 ((ret = RawProcessor.unpack()) != LIBRAW_SUCCESS)
{
fprintf(stderr, "Cannot unpack %s: %s\n", av[i], libraw_strerror(ret));
continue;
}
// we should call dcraw_process before thumbnail extraction because for
// some cameras (i.e. Kodak ones) white balance for thumbnail should be set
// from main image settings
ret = RawProcessor.dcraw_process();
if (LIBRAW_SUCCESS != ret)
{
fprintf(stderr, "Cannot do postprocessing on %s: %s\n", av[i],
libraw_strerror(ret));
if (LIBRAW_FATAL_ERROR(ret))
continue;
}
libraw_processed_image_t *image = RawProcessor.dcraw_make_mem_image(&ret);
if (image)
{
#ifdef USE_JPEG
if(output_jpeg)
write_jpeg(image, av[i], jpgqual);
else
#endif
write_ppm(image, av[i]);
LibRaw::dcraw_clear_mem(image);
}
else
fprintf(stderr, "Cannot unpack %s to memory buffer: %s\n", av[i],
libraw_strerror(ret));
if (output_thumbs)
{
if ((ret = RawProcessor.unpack_thumb()) != LIBRAW_SUCCESS)
{
fprintf(stderr, "Cannot unpack_thumb %s: %s\n", av[i],
libraw_strerror(ret));
if (LIBRAW_FATAL_ERROR(ret))
continue; // skip to next file
}
else
{
libraw_processed_image_t *thumb =
RawProcessor.dcraw_make_mem_thumb(&ret);
if (thumb)
{
write_thumb(thumb, av[i]);
LibRaw::dcraw_clear_mem(thumb);
}
else
fprintf(stderr,
"Cannot unpack thumbnail of %s to memory buffer: %s\n", av[i],
libraw_strerror(ret));
}
}
RawProcessor.recycle(); // just for show this call
}
return 0;
}