diff options
author | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-01-18 20:37:27 +0000 |
---|---|---|
committer | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-01-18 20:37:27 +0000 |
commit | 3f38a0b873dd92aed82ca1c47cc9af5417ae68f5 (patch) | |
tree | 8725d04c261a0c02398103c7118b5a459acefd67 /libkdcraw/libraw/samples | |
parent | 080528894f0f3ac6940d70065ae078d195ca8c6d (diff) | |
download | libkdcraw-3f38a0b873dd92aed82ca1c47cc9af5417ae68f5.tar.gz libkdcraw-3f38a0b873dd92aed82ca1c47cc9af5417ae68f5.zip |
Added missing libraw files
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/libraries/libkdcraw@1076753 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'libkdcraw/libraw/samples')
-rw-r--r-- | libkdcraw/libraw/samples/4channels.cpp | 187 | ||||
-rw-r--r-- | libkdcraw/libraw/samples/dcraw_emu.cpp | 281 | ||||
-rw-r--r-- | libkdcraw/libraw/samples/dcraw_half.c | 83 | ||||
-rw-r--r-- | libkdcraw/libraw/samples/half_mt.c | 178 | ||||
-rw-r--r-- | libkdcraw/libraw/samples/half_mt_win32.c | 212 | ||||
-rw-r--r-- | libkdcraw/libraw/samples/identify.cpp | 147 | ||||
-rw-r--r-- | libkdcraw/libraw/samples/mem_image.cpp | 198 | ||||
-rw-r--r-- | libkdcraw/libraw/samples/simple_dcraw.cpp | 213 | ||||
-rw-r--r-- | libkdcraw/libraw/samples/unprocessed_raw.cpp | 156 |
9 files changed, 1655 insertions, 0 deletions
diff --git a/libkdcraw/libraw/samples/4channels.cpp b/libkdcraw/libraw/samples/4channels.cpp new file mode 100644 index 0000000..d266c53 --- /dev/null +++ b/libkdcraw/libraw/samples/4channels.cpp @@ -0,0 +1,187 @@ +/* -*- C++ -*- + * File: 4channels.cpp + * Copyright 2009 Alex Tutubalin <lexa@lexa.ru> + * Created: Mon Feb 09, 2009 + * + * LibRaw sample + * Generates 4 TIFF file from RAW data, one file per channel + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ +#include <stdio.h> +#include <string.h> +#include <math.h> +#ifndef WIN32 +#include <netinet/in.h> +#else +#include <winsock2.h> +#endif + +#include "libraw/libraw.h" + +#ifdef WIN32 +#define snprintf _snprintf +#endif + +int main(int ac, char *av[]) +{ + int i, ret; + int autoscale=0,filtering_mode=LIBRAW_FILTERING_DEFAULT,black_subtraction=1; + char outfn[1024],thumbfn[1024]; + + LibRaw RawProcessor; + if(ac<2) + { + usage: + printf( + "4channeld - LibRaw %s sample. %d cameras supported\n" + "Usage: %s [-s N] [-g] [-A] [-B] [-N] raw-files....\n" + "\t-s N - select Nth image in file (default=0)\n" + "\t-g - use gamma correction with gamma 2.2 (not precise,use for visual inspection only)\n" + "\t-A - autoscaling (by integer factor)\n" + "\t-B - no black subtraction\n" + "\t-N - no raw curve\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 + + OUT.document_mode=2; + OUT.output_bps=16; + OUT.output_tiff=1; + OUT.user_flip=0; + OUT.no_auto_bright = 1; + OUT.half_size=1; + OUT.filtering_mode= LIBRAW_FILTERING_AUTOMATIC; + + for (i=1;i<ac;i++) + { + if(av[i][0]=='-') + { + if(av[i][1]=='s' && av[i][2]==0) + { + i++; + OUT.shot_select=atoi(av[i]); + } + else if(av[i][1]=='g' && av[i][2]==0) + OUT.gamma_16bit=1; + else if(av[i][1]=='A' && av[i][2]==0) + autoscale=1; + else if(av[i][1]=='B' && av[i][2]==0) + { + filtering_mode |= (LIBRAW_FILTERING_NOZEROES | LIBRAW_FILTERING_NOBLACKS); + black_subtraction=0; + } + else if(av[i][1]=='N' && av[i][2]==0) + filtering_mode |= LIBRAW_FILTERING_NORAWCURVE; + else + goto usage; + continue; + } + if(filtering_mode) + OUT.filtering_mode = (LibRaw_filtering) filtering_mode; + int r,c; + 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; + } + if(black_subtraction && C.black>0) + { + for(int j=0; j<S.iheight*S.iwidth; j++) + for(int c = 0; c< 4; c++) + if(RawProcessor.imgdata.image[j][c]>C.black) + RawProcessor.imgdata.image[j][c]-=C.black; + else + RawProcessor.imgdata.image[j][c]=0; + } + + if(autoscale) + { + unsigned max=0,scale; + for(int j=0; j<S.iheight*S.iwidth; j++) + for(int c = 0; c< 4; c++) + if(max < RawProcessor.imgdata.image[j][c]) + max = RawProcessor.imgdata.image[j][c]; + if (max >0 && max< 1<<15) + { + scale = (1<<16)/max; + printf("Scaling with multiplier=%d (max=%d)\n",scale,max); + for(int j=0; j<S.iheight*S.iwidth; j++) + for(c=0;c<4;c++) + RawProcessor.imgdata.image[j][c] *= scale; + } + printf("Black level (scaled)=%d\n",C.black*scale); + } + else + printf("Black level (unscaled)=%d\n",C.black); + + + // hack to make dcraw tiff writer happy + int isrgb=(P1.colors==4?0:1); + P1.colors = 1; + S.width = S.iwidth; + S.height = S.iheight; + + for (int layer=0;layer<4;layer++) + { + if(layer>0) + { + for (int rc = 0; rc < S.iheight*S.iwidth; rc++) + RawProcessor.imgdata.image[rc][0] = RawProcessor.imgdata.image[rc][layer]; + } + char lname[8]; + if(isrgb) + { + snprintf(lname,7,"%c",(char*)("RGBG")[layer]); + if(layer==3) + strcat(lname,"2"); + } + else + snprintf(lname,7,"%c",(char*)("GCMY")[layer]); + + if(OUT.shot_select) + snprintf(outfn,sizeof(outfn),"%s-%d.%s.tiff",av[i],OUT.shot_select,lname); + else + snprintf(outfn,sizeof(outfn),"%s.%s.tiff",av[i],lname); + + 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; +} diff --git a/libkdcraw/libraw/samples/dcraw_emu.cpp b/libkdcraw/libraw/samples/dcraw_emu.cpp new file mode 100644 index 0000000..704475a --- /dev/null +++ b/libkdcraw/libraw/samples/dcraw_emu.cpp @@ -0,0 +1,281 @@ +/* -*- C++ -*- + * File: dcraw_emu.cpp + * Copyright 2008-2009 Alex Tutubalin <lexa@lexa.ru> + * Created: Sun Mar 23, 2008 + * + * LibRaw simple C++ API (almost complete dcraw emulator) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <math.h> +#include <ctype.h> +#ifndef WIN32 +#include <fcntl.h> +#include <unistd.h> +#include <sys/stat.h> +#include <sys/mman.h> +#endif + +#include "libraw/libraw.h" +#ifdef WIN32 +#define snprintf _snprintf +#endif + + +void usage(const char *prog) +{ + printf("dcraw_emu: almost complete dcraw emulator\n"); + printf("Usage: %s [OPTION]... [FILE]...\n", prog); + printf( +"-v Verbose: print progress messages (repeated -v will add verbosity)\n" +"-w Use camera white balance, if possible\n" +"-a Average the whole image for white balance\n" +"-A <x y w h> Average a grey box for white balance\n" +"-r <r g b g> Set custom white balance\n" +"+M/-M Use/don't use an embedded color matrix\n" +"-C <r b> Correct chromatic aberration\n" +"-P <file> Fix the dead pixels listed in this file\n" +"-K <file> Subtract dark frame (16-bit raw PGM)\n" +"-k <num> Set the darkness level\n" +"-S <num> Set the saturation level\n" +"-n <num> Set threshold for wavelet denoising\n" +"-H [0-9] Highlight mode (0=clip, 1=unclip, 2=blend, 3+=rebuild)\n" +"-t [0-7] Flip image (0=none, 3=180, 5=90CCW, 6=90CW)\n" +"-o [0-5] Output colorspace (raw,sRGB,Adobe,Wide,ProPhoto,XYZ)\n" +#ifndef NO_LCMS +"-o file Output ICC profile\n" +"-p file Camera input profile (use \'embed\' for embedded profile)\n" +#endif +"-j Don't stretch or rotate raw pixels\n" +"-W Don't automatically brighten the image\n" +"-b <num> Adjust brightness (default = 1.0)\n" +"-q [0-3] Set the interpolation quality\n" +"-h Half-size color image (twice as fast as \"-q 0\")\n" +"-f Interpolate RGGB as four colors\n" +"-m <num> Apply a 3x3 median filter to R-G and B-G\n" +"-s [0..N-1] Select one raw image from input file\n" +"-4 Write 16-bit linear instead of 8-bit with gamma\n" +"-g pow ts Set gamma curve to gamma pow and toe slope ts (default = 2.222 4.5)\n" +"-T Write TIFF instead of PPM\n" +#ifndef WIN32 +"-B Use mmap()-ed buffer instead of plain FILE I/O\n" +#endif + ); + exit(1); +} + +static int verbosity=0; + +int cnt=0; +int my_progress_callback(void *d,enum LibRaw_progress p,int iteration, int expected) +{ + char *passed = (char*)(d?d:"default string"); // data passed to callback at set_callback stage + + if(verbosity>2) // verbosity set by repeat -v switches + { + printf("CB: %s pass %d of %d (data passed=%s)\n",libraw_strprogress(p),iteration,expected,passed); + } + else if (iteration == 0) // 1st iteration of each step + printf("Starting %s (expecting %d iterations)\n", libraw_strprogress(p),expected); + else if (iteration == expected-1) + printf("%s finished\n",libraw_strprogress(p)); + +/// if(++cnt>10) return 1; // emulate user termination on 10-th callback call + + return 0; // always return 0 to continue processing +} + + +int main(int argc, char *argv[]) +{ + if(argc==1) usage(argv[0]); + + LibRaw RawProcessor; + int i,arg,c,ret; + char opm,opt,*cp,*sp; + int use_mmap=0, msize; + void *iobuffer; + +#define OUT RawProcessor.imgdata.params + + argv[argc] = ""; + for (arg=1; (((opm = argv[arg][0]) - 2) | 2) == '+'; ) + { + opt = argv[arg++][1]; + if ((cp = strchr (sp="nbrkStqmHACgU", opt))) + for (i=0; i < "11411111142"[cp-sp]-'0'; i++) + if (!isdigit(argv[arg+i][0])) + { + fprintf (stderr,"Non-numeric argument to \"-%c\"\n", opt); + return 1; + } + switch (opt) + { + case 'v': verbosity++; break; + + case 'U': OUT.auto_bright_thr = atof(argv[arg++]); break; + case 'n': OUT.threshold = atof(argv[arg++]); break; + case 'b': OUT.bright = atof(argv[arg++]); break; + case 'P': OUT.bad_pixels = argv[arg++]; break; + case 'K': OUT.dark_frame = argv[arg++]; break; + case 'r': + for(c=0;c<4;c++) + OUT.user_mul[c] = atof(argv[arg++]); + break; + case 'C': + OUT.aber[0] = 1 / atof(argv[arg++]); + OUT.aber[2] = 1 / atof(argv[arg++]); + break; + case 'g': + OUT.gamm[0] = 1 / atof(argv[arg++]); + OUT.gamm[1] = atof(argv[arg++]); + break; + case 'k': OUT.user_black = atoi(argv[arg++]); break; + case 'S': OUT.user_sat = atoi(argv[arg++]); break; + case 't': OUT.user_flip = atoi(argv[arg++]); break; + case 'q': OUT.user_qual = atoi(argv[arg++]); break; + case 'm': OUT.med_passes = atoi(argv[arg++]); break; + case 'H': OUT.highlight = atoi(argv[arg++]); break; + case 's': OUT.shot_select = abs(atoi(argv[arg++])); break; + case 'o': + if(isdigit(argv[arg+1][0]) && !isdigit(argv[arg+1][1])) + OUT.output_color = atoi(argv[arg++]); +#ifndef NO_LCMS + else + OUT.output_profile = argv[arg++]; + break; + case 'p': OUT.camera_profile = argv[arg++]; +#endif + break; + case 'h': OUT.half_size = 1; + // no break: "-h" implies "-f" + case 'f': + OUT.four_color_rgb = 1; + break; + case 'A': for(c=0; c<4;c++) OUT.greybox[c] = atoi(argv[arg++]); + case 'a': OUT.use_auto_wb = 1; break; + case 'w': OUT.use_camera_wb = 1; break; + case 'M': OUT.use_camera_matrix = (opm == '+'); break; + case 'j': OUT.use_fuji_rotate = 0; break; + case 'W': OUT.no_auto_bright = 1; break; + case 'T': OUT.output_tiff = 1; break; + case '4': OUT.output_bps = 16; break; + case '1': OUT.gamma_16bit = 1; break; +#ifndef WIN32 + case 'B': use_mmap = 1; break; +#endif + default: + fprintf (stderr,"Unknown option \"-%c\".\n", opt); + return 1; + } + } + putenv ((char*)"TZ=UTC"); // dcraw compatibility, affects TIFF datestamp field + OUT.filtering_mode = LIBRAW_FILTERING_AUTOMATIC; +#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 + + if(verbosity>1) + RawProcessor.set_progress_handler(my_progress_callback,(void*)"Sample data passed"); +#ifdef _OPENMP + if(verbosity) + printf ("Using %d threads\n", omp_get_max_threads()); +#endif + + for ( ; arg < argc; arg++) + { + char outfn[1024]; + + if(verbosity) printf("Processing file %s\n",argv[arg]); +#ifndef WIN32 + if(use_mmap) + { + int file = open(argv[arg],O_RDONLY); + struct stat st; + if(file<0) + { + fprintf(stderr,"Cannot open %s: %s\n",argv[arg],strerror(errno)); + continue; + } + if(fstat(file,&st)) + { + fprintf(stderr,"Cannot stat %s: %s\n",argv[arg],strerror(errno)); + close(file); + continue; + } + int pgsz = getpagesize(); + msize = ((st.st_size+pgsz-1)/pgsz)*pgsz; + iobuffer = mmap(NULL,msize,PROT_READ,MAP_PRIVATE,file,0); + if(!iobuffer) + { + fprintf(stderr,"Cannot mmap %s: %s\n",argv[arg],strerror(errno)); + close(file); + continue; + } + close(file); + if( (ret = RawProcessor.open_buffer(iobuffer,st.st_size) != LIBRAW_SUCCESS)) + { + fprintf(stderr,"Cannot open_buffer %s: %s\n",argv[arg],libraw_strerror(ret)); + continue; // no recycle b/c open file will recycle itself + } + + } + else +#endif + { + if( (ret = RawProcessor.open_file(argv[arg])) != LIBRAW_SUCCESS) + { + fprintf(stderr,"Cannot open %s: %s\n",argv[arg],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",argv[arg],libraw_strerror(ret)); + continue; + } + if (LIBRAW_SUCCESS != (ret = RawProcessor.dcraw_process())) + { + fprintf(stderr,"Cannot do postpocessing on %s: %s\n",argv[arg],libraw_strerror(ret)); + if(LIBRAW_FATAL_ERROR(ret)) + continue; + } + snprintf(outfn,sizeof(outfn), + "%s.%s", + argv[arg], OUT.output_tiff ? "tiff" : (P1.colors>1?"ppm":"pgm")); + + 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)); + +#ifndef WIN32 + if(use_mmap && iobuffer) + { + munmap(iobuffer,msize); + iobuffer=0; + } +#endif + + RawProcessor.recycle(); // just for show this call + } + return 0; +} diff --git a/libkdcraw/libraw/samples/dcraw_half.c b/libkdcraw/libraw/samples/dcraw_half.c new file mode 100644 index 0000000..814f697 --- /dev/null +++ b/libkdcraw/libraw/samples/dcraw_half.c @@ -0,0 +1,83 @@ +/* -*- C++ -*- + * File: dcraw_half.c + * Copyright 2008-2009 Alex Tutubalin <lexa@lexa.ru> + * Created: Sat Mar 8 , 2008 + * + * LibRaw C API sample (emulates call to "dcraw -h") + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <math.h> + +#include "libraw/libraw.h" + + +#define HANDLE_FATAL_ERROR(ret)\ + if(ret)\ + {\ + fprintf(stderr,"%s: libraw %s\n",av[i],libraw_strerror(ret));\ + if(LIBRAW_FATAL_ERROR(ret))\ + exit(1); \ + }\ + +#define HANDLE_ALL_ERRORS(ret)\ + if(ret)\ + {\ + fprintf(stderr,"%s: libraw %s\n",av[i],libraw_strerror(ret));\ + continue; \ + }\ + + +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; +} diff --git a/libkdcraw/libraw/samples/half_mt.c b/libkdcraw/libraw/samples/half_mt.c new file mode 100644 index 0000000..7d5177c --- /dev/null +++ b/libkdcraw/libraw/samples/half_mt.c @@ -0,0 +1,178 @@ +/* -*- C++ -*- + * File: halt_mt.c + * Copyright 2008-2009 Alex Tutubalin <lexa@lexa.ru> + * Created: Sat Mar 8 , 2008 + * + * LibRaw C API mutithreaded sample (emulates call to "dcraw -h [-w] [-a] [-v]") + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <math.h> +#include <pthread.h> + +#include "libraw/libraw.h" + +#define HANDLE_ERRORS(ret) do { \ + if(ret) \ + { \ + fprintf(stderr,"%s: %s\n",fn,libraw_strerror(ret)); \ + if(LIBRAW_FATAL_ERROR(ret)) \ + { \ + libraw_close(iprc); \ + return -1; \ + } \ + } \ + }while(0) + + +// global settings +int verbose=0,use_camera_wb=0,use_auto_wb=0,tiff_mode=0; + +// global file queue +pthread_mutex_t qm; +char **queue=NULL; +size_t qsize=0,qptr=0; + +char *get_next_file() +{ + char *ret; + if(!queue) return NULL; + if(qptr>=qsize) return NULL; + pthread_mutex_lock(&qm); + ret = queue[qptr++]; + pthread_mutex_unlock(&qm); + return ret; +} + + +// 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.ppm",fn); + + if(verbose) fprintf(stderr,"Writing file %s\n",outfn); + ret = libraw_dcraw_ppm_tiff_writer(iprc,outfn); + HANDLE_ERRORS(ret); + count++; + } + libraw_close(iprc); + return count; +} + +void usage(const char*p) +{ + printf("%s: Multi-threaded LibRaw sample app. Emulates dcraw -h [-w] [-a]\n",p); + printf( + "Options:\n" + "-J n - set parrallel job coun (default 2)\n" + "-v - verbose\n" + "-w - use camera white balance\n" + "-a - average image for white balance\n"); + exit(1); +} + +int show_files(void *q) +{ + char *p; + int cnt = 0; + while(p = get_next_file()) + { + printf("%s\n",p); + cnt++; + } + return cnt; + +} + +int main(int ac, char *av[]) +{ + int i, thread_count,max_threads = 2; + pthread_t *threads; + if(ac<2) + usage(av[0]); + + queue = calloc(ac-1,sizeof(queue[0])); + + for (i=1;i<ac;i++) + { + if(av[i][0]=='-') + { + if(av[i][1]=='w') use_camera_wb = 1; + if(av[i][1]=='a') use_auto_wb = 1; + if(av[i][1]=='v') verbose = 1; + if(av[i][1]=='T') tiff_mode = 1; + if(av[i][1]=='J') + { + max_threads=atoi(av[++i]); + if(max_threads<1) + { + fprintf(stderr,"Job count should be at least 1\n"); + exit(1); + } + } + } + else + queue[qsize++] = av[i]; + } + pthread_mutex_init(&qm,NULL); + threads = calloc(max_threads,sizeof(threads[0])); + for(i=0;i<max_threads;i++) + pthread_create(&threads[i],NULL,process_files,NULL); + for(i=0;i<max_threads;i++) + { + int *iptr; + if(threads[i]) + { + pthread_join(threads[i],&iptr); + if(iptr) + printf("Thread %d : %d files\n",i,(int)iptr); + } + } + + return 0; +} diff --git a/libkdcraw/libraw/samples/half_mt_win32.c b/libkdcraw/libraw/samples/half_mt_win32.c new file mode 100644 index 0000000..990a800 --- /dev/null +++ b/libkdcraw/libraw/samples/half_mt_win32.c @@ -0,0 +1,212 @@ +/* -*- C++ -*- + * File: halt_mt_win32.c + * Copyright 2008-2009 Alex Tutubalin <lexa@lexa.ru> + * Created: Sat Mar 8 , 2008 + * + * LibRaw C API mutithreaded sample (emulates call to "dcraw -h [-w] [-a] [-v]") + * Win32 version + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <math.h> +#include <windows.h> +#include "libraw/libraw.h" + +#ifdef WIN32 +#define snprintf _snprintf +#endif + + +#define HANDLE_ERRORS(ret) do { \ + if(ret) \ + { \ + fprintf(stderr,"%s: %s\n",fn,libraw_strerror(ret)); \ + if(LIBRAW_FATAL_ERROR(ret)) \ + { \ + libraw_close(iprc); \ + return -1; \ + } \ + } \ + }while(0) + + +// global settings +int verbose=0,use_camera_wb=0,use_auto_wb=0,tiff_mode=0; + +// global file queue +HANDLE qmutex; +char **queue=NULL; +size_t qsize=0,qptr=0; + +char *get_next_file() +{ + char *ret; + DWORD dwWaitResult; + if(!queue) return NULL; + if(qptr>=qsize) return NULL; + + dwWaitResult = WaitForSingleObject( + qmutex, // handle to mutex + INFINITE); // no time-out interval + switch (dwWaitResult) + { + // The thread got ownership of the mutex + case WAIT_OBJECT_0: + ret = queue[qptr++]; + ReleaseMutex(qmutex); + break; + case WAIT_ABANDONED: + return NULL; // cannot obtain the lock + }; + return ret; +} + + +// 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; +} + +void usage(const char*p) +{ + printf( + "Options:\n" + "-J n - set parrallel job coun (default 2)\n" + "-v - verbose\n" + "-w - use camera white balance\n" + "-T - output TIFF instead of PPM\n" + "-a - average image for white balance\n"); + exit(1); +} + +int show_files(void *q) +{ + char *p; + int cnt = 0; + while(p = get_next_file()) + { + printf("%s\n",p); + cnt++; + } + return cnt; + +} + +int main(int ac, char *av[]) +{ + int i,max_threads = 2; + HANDLE *threads; + DWORD ThreadID; + + if(ac<2) + usage(av[0]); + + queue = calloc(ac-1,sizeof(queue[0])); + + for (i=1;i<ac;i++) + { + if(av[i][0]=='-') + { + if(av[i][1]=='w') use_camera_wb = 1; + if(av[i][1]=='a') use_auto_wb = 1; + if(av[i][1]=='v') verbose = 1; + if(av[i][1]=='T') tiff_mode = 1; + if(av[i][1]=='J') + { + max_threads=atoi(av[++i]); + if(max_threads<1) + { + fprintf(stderr,"Job count should be at least 1\n"); + exit(1); + } + } + } + else + queue[qsize++] = av[i]; + } + qmutex = CreateMutex(NULL,FALSE,NULL); + threads = calloc(max_threads,sizeof(threads[0])); + for(i=0;i<max_threads;i++) + { + + if (NULL == (threads[i] = CreateThread( + NULL, // default security attributes + 0, // default stack size + (LPTHREAD_START_ROUTINE) process_files, + NULL, // no thread function arguments + 0, // default creation flags + &ThreadID) // receive thread identifier + ) + ) + { + printf("CreateThread error: %d\n", GetLastError()); + return 1; + } + } + + WaitForMultipleObjects(max_threads, threads, TRUE, INFINITE); + + // Close thread and mutex handles + + for( i=0; i < max_threads; i++ ) + CloseHandle(threads[i]); + + CloseHandle(qmutex); + + return 0; +} diff --git a/libkdcraw/libraw/samples/identify.cpp b/libkdcraw/libraw/samples/identify.cpp new file mode 100644 index 0000000..a1e0f20 --- /dev/null +++ b/libkdcraw/libraw/samples/identify.cpp @@ -0,0 +1,147 @@ +/* -*- C++ -*- + * File: identify.cpp + * Copyright 2008-2009 Alex Tutubalin <lexa@lexa.ru> + * Created: Sat Mar 8 , 2008 + * + * LibRaw C++ demo (emulates dcraw -i [-v]) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ + +#include <stdio.h> +#include <string.h> +#include <math.h> +#include <time.h> + +#include "libraw/libraw.h" + +#ifdef WIN32 +#define snprintf _snprintf +#endif + + +int main(int ac, char *av[]) +{ + int verbose = 0, ret,print_unpack=0,print_frame=0; + LibRaw MyCoolRawProcessor; + + for (int i=1;i<ac;i++) { + if(av[i][0]=='-') + { + if(av[i][1]=='v' && av[i][2]==0) verbose++; + if(av[i][1]=='u' && av[i][2]==0) print_unpack++; + if(av[i][1]=='f' && av[i][2]==0) print_frame++; + continue; + } + if( (ret = MyCoolRawProcessor.open_file(av[i])) != LIBRAW_SUCCESS) + { + printf("Cannot decode %s: %s\n",av[i],libraw_strerror(ret)); + continue; // no recycle, open_file will recycle + } + if(verbose) { + +#define P1 MyCoolRawProcessor.imgdata.idata +#define P2 MyCoolRawProcessor.imgdata.other + +#define S MyCoolRawProcessor.imgdata.sizes +#define O MyCoolRawProcessor.imgdata.params +#define C MyCoolRawProcessor.imgdata.color +#define T MyCoolRawProcessor.imgdata.thumbnail + + + if( (ret = MyCoolRawProcessor.adjust_sizes_info_only())) + { + printf("Cannot decode %s: %s\n",av[i],libraw_strerror(ret)); + continue; // no recycle, open_file will recycle + } + + printf ("\nFilename: %s\n", av[i]); + printf ("Timestamp: %s", ctime(&(P2.timestamp))); + printf ("Camera: %s %s\n", P1.make, P1.model); + if (P2.artist[0]) + printf ("Owner: %s\n", P2.artist); + if (P1.dng_version) { + printf ("DNG Version: "); + for (int i=24; i >= 0; i -= 8) + printf ("%d%c", P1.dng_version >> i & 255, i ? '.':'\n'); + } + printf ("ISO speed: %d\n", (int) P2.iso_speed); + printf ("Shutter: "); + if (P2.shutter > 0 && P2.shutter < 1) + P2.shutter = (printf ("1/"), 1 / P2.shutter); + printf ("%0.1f sec\n", P2.shutter); + printf ("Aperture: f/%0.1f\n", P2.aperture); + printf ("Focal length: %0.1f mm\n", P2.focal_len); + if(C.profile) + printf ("Embedded ICC profile: yes, %d bytes\n", C.profile_length); + else + printf ("Embedded ICC profile: no\n", C.profile_length); + + printf ("Number of raw images: %d\n", P1.raw_count); + if (S.pixel_aspect != 1) + printf ("Pixel Aspect Ratio: %0.6f\n", S.pixel_aspect); + if (T.tlength) + printf ("Thumb size: %4d x %d\n", T.twidth, T.theight); + printf ("Full size: %4d x %d\n", S.raw_width, S.raw_height); + + printf ("Image size: %4d x %d\n", S.width, S.height); + printf ("Output size: %4d x %d\n", S.iwidth, S.iheight); + printf ("Raw colors: %d", P1.colors); + if (P1.filters) + { + printf ("\nFilter pattern: "); + if (!P1.cdesc[3]) P1.cdesc[3] = 'G'; + for (int i=0; i < 16; i++) + putchar (P1.cdesc[MyCoolRawProcessor.fc(i >> 1,i & 1)]); + } + printf ("\nDaylight multipliers:"); + for(int c=0;c<P1.colors;c++) printf (" %f", C.pre_mul[c]); + if (C.cam_mul[0] > 0) + { + printf ("\nCamera multipliers:"); + for(int c=0;c<4;c++) printf (" %f", C.cam_mul[c]); + } + char *csl[] = {"U","I","CO","L","CA"}; + printf("\nColor sources /Legend: (U)nknown, (I)nit, (CO)nstant, (L)oaded, (CA)lculated/:\n"); + printf("\tcurve=%s; rgb_cam=%s; cmatrix=%s, pre_mul=%s, cam_mul=%s", + csl[C.color_flags.curve_state],csl[C.color_flags.rgb_cam_state], + csl[C.color_flags.cmatrix_state],csl[C.color_flags.pre_mul_state], + csl[C.color_flags.cam_mul_state]); + putchar ('\n'); + printf("Cam->XYZ matrix:\n"); + for(int i=0; i< 4; i++) + printf("%6.4f\t%6.4f\t%6.4f\n",C.cam_xyz[i][0],C.cam_xyz[i][1],C.cam_xyz[i][2]); + } + else + { + if(print_unpack) + { + char frame[32]=""; + if(print_frame) + snprintf(frame,32,"%dx%dx%dx%d",S.left_margin,S.top_margin,S.right_margin,S.bottom_margin); + printf ("%s\t%s\t%s\t%s/%s\n", + av[i], + MyCoolRawProcessor.unpack_function_name(), + frame, + P1.make, P1.model); + } + else + printf ("%s is a %s %s image.\n", av[i],P1.make, P1.model); + } + MyCoolRawProcessor.recycle(); + }// endfor + return 0; +} diff --git a/libkdcraw/libraw/samples/mem_image.cpp b/libkdcraw/libraw/samples/mem_image.cpp new file mode 100644 index 0000000..29fd7c4 --- /dev/null +++ b/libkdcraw/libraw/samples/mem_image.cpp @@ -0,0 +1,198 @@ +/* -*- C++ -*- + * File: mem_image.cpp + * Copyright 2008-2009 Alex Tutubalin <lexa@lexa.ru> + * Created: Sat Mar 8 , 2008 + * + * LibRaw mem_image/mem_thumb API test. Resuls should be same (bitwise) as in dcraw [-4] [-e] + * Testing note: for ppm-thumbnails you should use dcraw -w -e for thumbnail extraction + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ +#include <stdio.h> +#include <string.h> +#include <math.h> + +#include "libraw/libraw.h" + +#ifdef WIN32 +#define snprintf _snprintf +#include <winsock2.h> +#pragma comment(lib, "ws2_32.lib") +#else +#include <netinet/in.h> +#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; + // only 3-color images supported... + if(img->colors != 3) return; + + char fn[1024]; + snprintf(fn,1024,"%s.ppm",basename); + FILE *f = fopen(fn,"wb"); + if(!f) return; + fprintf (f, "P6\n%d %d\n%d\n", 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(int i=0; i< img->data_size; 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, verbose=0, output_thumbs=0; + + // don't use fixed size buffers in real apps! + char outfn[1024],thumbfn[1024]; + + LibRaw RawProcessor; + + if(ac<2) + { + printf( + "mem_image - LibRaw sample, to illustrate work for memory buffers. Emulates dcraw [-4] [-1] [-e]\n" + "Usage: %s [-D] [-T] [-v] [-e] raw-files....\n" + "\t-4 - output 16-bit PPM\n" + "\t-1 - gamma-correct 16-bit data\n" + "\t-e - extract thumbnails (same as dcraw -e in separate run)\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]=='4' && av[i][2]==0) + OUT.output_bps = 16; + if(av[i][1]=='1' && av[i][2]==0) + OUT.gamma_16bit = 1; + if(av[i][1]=='e' && av[i][2]==0) + output_thumbs++; + continue; + } + 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 thumbnal should be set + // from main image settings + + + ret = RawProcessor.dcraw_process(); + + if(LIBRAW_SUCCESS !=ret) + { + fprintf(stderr,"Cannot do postpocessing 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) + { + write_ppm(image,av[i]); + free(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]); + free(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; +} diff --git a/libkdcraw/libraw/samples/simple_dcraw.cpp b/libkdcraw/libraw/samples/simple_dcraw.cpp new file mode 100644 index 0000000..8c78c4d --- /dev/null +++ b/libkdcraw/libraw/samples/simple_dcraw.cpp @@ -0,0 +1,213 @@ +/* -*- C++ -*- + * File: simple_dcraw.cpp + * Copyright 2008-2009 Alex Tutubalin <lexa@lexa.ru> + * Created: Sat Mar 8 , 2008 + * + * LibRaw simple C++ API (emulates call to "dcraw [-D] [-T] [-v] [-e] [-4]") + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ +#include <stdio.h> +#include <string.h> +#include <math.h> + +#ifndef WIN32 +#include <unistd.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <sys/mman.h> +#endif + +#include "libraw/libraw.h" + +#ifdef WIN32 +#define snprintf _snprintf +#endif + +int my_progress_callback(void *unused_data,enum LibRaw_progress state,int iter, int expected) +{ + if(iter==0) + printf("CB: state=%x, expected %d iterations\n",state,expected); + return 0; +} + + +int main(int ac, char *av[]) +{ + int i, ret, verbose=0, output_thumbs=0,use_mmap=0,msize; + void *file_buffer; + + // don't use fixed size buffers in real apps! + char outfn[1024],thumbfn[1024]; + + LibRaw RawProcessor; + if(ac<2) + { + printf( + "simple_dcraw - LibRaw %s sample. Emulates dcraw [-D] [-T] [-v] [-e] [-B]\n" + " %d cameras supported\n" + "Usage: %s [-D] [-T] [-v] [-e] raw-files....\n" + "\t-D - document mode emulation\n" + "\t-4 - 16-bit mode\n" + "\t-v - verbose output\n" + "\t-T - output TIFF files instead of .pgm/ppm\n" +#ifndef WIN32 + "\t-B - use mmap()-ed I/O (Unix only)\n" +#endif + "\t-e - extract thumbnails (same as dcraw -e in separate run)\n",LibRaw::version(), + LibRaw::cameraCount(), + 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]=='T' && av[i][2]==0) + OUT.output_tiff=1; + if(av[i][1]=='v' && av[i][2]==0) + verbose++; + if(av[i][1]=='e' && av[i][2]==0) + output_thumbs++; + if(av[i][1]=='D' && av[i][2]==0) + OUT.document_mode=2; + if(av[i][1]=='B' && av[i][2]==0) + use_mmap=1; + if(av[i][1]=='4' && av[i][2]==0) + OUT.output_bps=16; + if(av[i][1]=='C' && av[i][2]==0) + RawProcessor.set_progress_handler(my_progress_callback,NULL); + continue; + } + + if(verbose) printf("Processing file %s\n",av[i]); + +#ifndef WIN32 + if(use_mmap) + { + int file = open(av[i],O_RDONLY); + struct stat st; + if(file<0) + { + fprintf(stderr,"Cannot open %s: %s\n",av[i],strerror(errno)); + continue; + } + if(fstat(file,&st)) + { + fprintf(stderr,"Cannot stat %s: %s\n",av[i],strerror(errno)); + close(file); + continue; + } + int pgsz = getpagesize(); + msize = ((st.st_size+pgsz-1)/pgsz)*pgsz; + file_buffer = mmap(NULL,msize,PROT_READ,MAP_PRIVATE,file,0); + if(!file_buffer) + { + fprintf(stderr,"Cannot mmap %s: %s\n",av[i],strerror(errno)); + close(file); + continue; + } + close(file); + if( (ret = RawProcessor.open_buffer(file_buffer,st.st_size) != LIBRAW_SUCCESS)) + { + fprintf(stderr,"Cannot open_buffer %s: %s\n",av[i],libraw_strerror(ret)); + continue; // no recycle b/c open file will recycle itself + } + } + else +#endif + { + if( (ret = RawProcessor.open_file(av[i])) != LIBRAW_SUCCESS) + { + fprintf(stderr,"Cannot open_file %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; + } + + // thumbnail unpacking and output in the middle of main + // image processing - for test purposes! + 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 + { + snprintf(thumbfn,sizeof(thumbfn),"%s.%s", + av[i],T.tformat == LIBRAW_THUMBNAIL_JPEG ? "thumb.jpg" : "thumb.ppm"); + + if(verbose) printf("Writing thumbnail file %s\n",thumbfn); + if( LIBRAW_SUCCESS != (ret = RawProcessor.dcraw_thumb_writer(thumbfn))) + { + fprintf(stderr,"Cannot write %s: %s\n",thumbfn,libraw_strerror(ret)); + if(LIBRAW_FATAL_ERROR(ret)) + continue; + } + } + } + + if(OUT.document_mode) + ret = RawProcessor.dcraw_document_mode_processing(); + else + ret = RawProcessor.dcraw_process(); + + if(LIBRAW_SUCCESS !=ret) + { + fprintf(stderr,"Cannot do postpocessing on %s: %s\n", + av[i],libraw_strerror(ret)); + if(LIBRAW_FATAL_ERROR(ret)) + continue; + } + snprintf(outfn,sizeof(outfn), + "%s.%s", + av[i], OUT.output_tiff ? "tiff" : (P1.colors>1?"ppm":"pgm")); + + if(verbose) 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)); + +#ifndef WIN32 + if(use_mmap && file_buffer) + { + munmap(file_buffer,msize); + file_buffer=0; + } +#endif + RawProcessor.recycle(); // just for show this call + } + return 0; +} diff --git a/libkdcraw/libraw/samples/unprocessed_raw.cpp b/libkdcraw/libraw/samples/unprocessed_raw.cpp new file mode 100644 index 0000000..15e9169 --- /dev/null +++ b/libkdcraw/libraw/samples/unprocessed_raw.cpp @@ -0,0 +1,156 @@ +/* -*- C++ -*- + * File: unprocessed_raw.cpp + * Copyright 2009 Alex Tutubalin <lexa@lexa.ru> + * Created: Fri Jan 02, 2009 + * + * LibRaw sample + * Generates unprocessed raw image: with masked pixels and without black subtraction + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ +#include <stdio.h> +#include <string.h> +#include <math.h> +#ifndef WIN32 +#include <netinet/in.h> +#else +#include <winsock2.h> +#endif + +#include "libraw/libraw.h" + +#ifdef WIN32 +#define snprintf _snprintf +#endif + +int main(int ac, char *av[]) +{ + int i, ret; + int verbose=1,autoscale=0; + char outfn[1024],thumbfn[1024]; + + LibRaw RawProcessor; + if(ac<2) + { + usage: + printf( + "unprocessed_raw - LibRaw %s sample. %d cameras supported\n" + "Usage: %s [-q] [-A] [-g] [-s N] [-N] raw-files....\n" + "\t-q - be quiet\n" + "\t-s N - select Nth image in file (default=0)\n" + "\t-g - use gamma correction with gamma 2.2 (not precise,use for visual inspection only)\n" + "\t-A - autoscaling (by integer factor)\n" + "\t-N - no raw curve\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 + + OUT.document_mode=2; + OUT.output_bps=16; + OUT.output_tiff=1; + OUT.user_flip=0; + OUT.no_auto_bright = 1; + OUT.filtering_mode=(LibRaw_filtering)( LIBRAW_FILTERING_NOBLACKS|LIBRAW_FILTERING_NOZEROES); + for (i=1;i<ac;i++) + { + if(av[i][0]=='-') + { + if(av[i][1]=='q' && av[i][2]==0) + verbose=0; + else if(av[i][1]=='A' && av[i][2]==0) + autoscale=1; + else if(av[i][1]=='g' && av[i][2]==0) + OUT.gamma_16bit=1; + else if(av[i][1]=='N' && av[i][2]==0) + OUT.filtering_mode=LIBRAW_FILTERING_NONE; + else if(av[i][1]=='s' && av[i][2]==0) + { + i++; + OUT.shot_select=atoi(av[i]); + } + else + goto usage; + continue; + } + int r,c; + if(verbose) 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(verbose) + { + printf("Image size: %dx%d\nRaw size: %dx%d\n",S.width,S.height,S.raw_width,S.raw_height); + printf("Margins: top=%d, left=%d, right=%d, bottom=%d\n", + S.top_margin,S.left_margin,S.right_margin,S.bottom_margin); + } + + if( (ret = RawProcessor.unpack() ) != LIBRAW_SUCCESS) + { + fprintf(stderr,"Cannot unpack %s: %s\n",av[i],libraw_strerror(ret)); + continue; + } + if(verbose) + printf("Unpacked....\n"); + + if( (ret = RawProcessor.add_masked_borders_to_bitmap() ) != LIBRAW_SUCCESS) + { + fprintf(stderr,"Cannot add mask data to bitmap %s\n",av[i]); + } + for(int r=0;r<S.iheight;r++) + for(c=0;c<S.iwidth;c++) + RawProcessor.imgdata.image[r*S.iwidth+c][0] + = RawProcessor.imgdata.image[r*S.iwidth+c][RawProcessor.FC(r,c)]; + + P1.colors=1; + if(autoscale) + { + unsigned max=0,scale; + for(int j=0; j<S.iheight*S.iwidth; j++) + if(max < RawProcessor.imgdata.image[j][0]) + max = RawProcessor.imgdata.image[j][0]; + if (max >0 && max< 1<<15) + { + scale = (1<<16)/max; + if(verbose) + printf("Scaling with multiplier=%d (max=%d)\n",scale,max); + + for(int j=0; j<S.iheight*S.iwidth; j++) + RawProcessor.imgdata.image[j][0] *= scale; + } + } + + if(OUT.shot_select) + snprintf(outfn,sizeof(outfn),"%s-%d.tiff",av[i],OUT.shot_select); + else + snprintf(outfn,sizeof(outfn),"%s.tiff",av[i]); + + if(verbose) 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; +} |