1 /* -*- C++ -*-
2  * File: multirender_test.cpp
3  * Copyright 2008-2021 LibRaw LLC (info@libraw.org)
4  * Created: Jul 10, 2011
5  *
6  * LibRaw simple C++ API:  creates 8 different renderings from 1 source file.
7 The 1st and 4th one should be identical
8 
9 LibRaw is free software; you can redistribute it and/or modify
10 it under the terms of the one of two licenses as you choose:
11 
12 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
13    (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
14 
15 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
16    (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
17 
18 
19  */
20 #include <stdio.h>
21 #include <string.h>
22 #include <math.h>
23 #include "libraw/libraw.h"
24 
25 #ifndef LIBRAW_WIN32_CALLS
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <sys/stat.h>
29 #include <sys/mman.h>
30 #endif
31 
32 #ifdef LIBRAW_WIN32_CALLS
33 #define snprintf _snprintf
34 #endif
35 
process_once(LibRaw & RawProcessor,int half_mode,int camera_wb,int auto_wb,int suffix,int user_flip,char * fname)36 int process_once(LibRaw &RawProcessor, int half_mode, int camera_wb,
37                  int auto_wb, int suffix, int user_flip, char *fname)
38 {
39   char outfn[1024];
40   RawProcessor.imgdata.params.half_size = half_mode;
41   RawProcessor.imgdata.params.use_camera_wb = camera_wb;
42   RawProcessor.imgdata.params.use_auto_wb = auto_wb;
43   RawProcessor.imgdata.params.user_flip = user_flip;
44 
45   int ret = RawProcessor.dcraw_process();
46 
47   if (LIBRAW_SUCCESS != ret)
48   {
49     fprintf(stderr, "Cannot do postpocessing on %s: %s\n", fname,
50             libraw_strerror(ret));
51     return ret;
52   }
53   snprintf(outfn, sizeof(outfn), "%s.%d.%s", fname, suffix,
54            (RawProcessor.imgdata.idata.colors > 1 ? "ppm" : "pgm"));
55 
56   printf("Writing file %s\n", outfn);
57 
58   if (LIBRAW_SUCCESS != (ret = RawProcessor.dcraw_ppm_tiff_writer(outfn)))
59     fprintf(stderr, "Cannot write %s: %s\n", outfn, libraw_strerror(ret));
60   return ret;
61 }
62 
main(int ac,char * av[])63 int main(int ac, char *av[])
64 {
65   int i, ret;
66 
67   LibRaw RawProcessor;
68   if (ac < 2)
69   {
70     printf("multirender_test - LibRaw %s sample. Performs 4 different "
71            "renderings of one file\n"
72            " %d cameras supported\n"
73            "Usage: %s raw-files....\n",
74            LibRaw::version(), LibRaw::cameraCount(), av[0]);
75     return 0;
76   }
77 
78   for (i = 1; i < ac; i++)
79   {
80 
81     printf("Processing file %s\n", av[i]);
82 
83     if ((ret = RawProcessor.open_file(av[i])) != LIBRAW_SUCCESS)
84     {
85       fprintf(stderr, "Cannot open_file %s: %s\n", av[i], libraw_strerror(ret));
86       continue; // no recycle b/c open file will recycle itself
87     }
88 
89     if ((ret = RawProcessor.unpack()) != LIBRAW_SUCCESS)
90     {
91       fprintf(stderr, "Cannot unpack %s: %s\n", av[i], libraw_strerror(ret));
92       continue;
93     }
94     process_once(RawProcessor, 0, 0, 0, 1, -1, av[i]); // default flip
95     process_once(RawProcessor, 1, 0, 1, 2, -1, av[i]);
96     process_once(RawProcessor, 1, 1, 0, 3, -1, av[i]); // default flip
97     process_once(RawProcessor, 1, 1, 0, 4, 1, av[i]);  // flip 1
98     process_once(RawProcessor, 1, 1, 0, 5, 3, av[i]);  // flip 3
99     process_once(RawProcessor, 1, 1, 0, 6, 1, av[i]);  // 1 again same as 4
100     process_once(RawProcessor, 1, 1, 0, 7, -1,
101                  av[i]); // default again, same as 3
102     process_once(RawProcessor, 0, 0, 0, 8, -1, av[i]); // same as 1
103 
104     RawProcessor.recycle(); // just for show this call
105   }
106   return 0;
107 }
108