update
[kdegraphics.git] / libs / libkdcraw / libraw / samples / mem_image.cpp
blobf5769fa142046f247415ef8deba3a3a8f16d9d64
1 /* -*- C++ -*-
2 * File: mem_image.cpp
3 * Copyright 2008 Alex Tutubalin <lexa@lexa.ru>
4 * Created: Sat Mar 8 , 2008
6 * LibRaw mem_image/mem_thumb API test. Resuls should be same (bitwise) as in dcraw [-4] [-e]
7 * Testing note: for ppm-thumbnails you should use dcraw -w -e for thumbnail extraction
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
12 * any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 * 02111-1307, USA.
24 #include <stdio.h>
25 #include <string.h>
26 #include <math.h>
28 #include "libraw/libraw.h"
30 #ifdef WIN32
31 #define snprintf _snprintf
32 #include <winsock2.h>
33 #pragma comment(lib, "ws2_32.lib")
34 #else
35 #include <netinet/in.h>
36 #endif
39 // no error reporting, only params check
40 void write_ppm(libraw_processed_image_t *img, const char *basename)
42 if(!img) return;
43 // type SHOULD be LIBRAW_IMAGE_BITMAP, but we'll check
44 if(img->type != LIBRAW_IMAGE_BITMAP) return;
45 // only 3-color images supported...
46 if(img->colors != 3) return;
48 char fn[1024];
49 snprintf(fn,1024,"%s.ppm",basename);
50 FILE *f = fopen(fn,"wb");
51 if(!f) return;
52 fprintf (f, "P6\n%d %d\n%d\n", img->width, img->height, (1 << img->bits)-1);
54 NOTE:
55 data in img->data is not converted to network byte order.
56 So, we should swap values on some architectures for dcraw compatibility
57 (unfortunately, xv cannot display 16-bit PPMs with network byte order data
59 #define SWAP(a,b) { a ^= b; a ^= (b ^= a); }
60 if (img->bits == 16 && htons(0x55aa) != 0x55aa)
61 for(int i=0; i< img->data_size; i+=2)
62 SWAP(img->data[i],img->data[i+1]);
63 #undef SWAP
65 fwrite(img->data,img->data_size,1,f);
66 fclose(f);
69 void write_thumb(libraw_processed_image_t *img, const char *basename)
71 if(!img) return;
73 if(img->type == LIBRAW_IMAGE_BITMAP)
75 char fnt[1024];
76 snprintf(fnt,1024,"%s.thumb",basename);
77 write_ppm(img,fnt);
79 else if (img->type == LIBRAW_IMAGE_JPEG)
81 char fn[1024];
82 snprintf(fn,1024,"%s.thumb.jpg",basename);
83 FILE *f = fopen(fn,"wb");
84 if(!f) return;
85 fwrite(img->data,img->data_size,1,f);
86 fclose(f);
92 int main(int ac, char *av[])
94 int i, ret, verbose=0, output_thumbs=0;
96 // don't use fixed size buffers in real apps!
97 char outfn[1024],thumbfn[1024];
99 LibRaw RawProcessor;
101 if(ac<2)
103 printf(
104 "mem_image - LibRaw sample, to illustrate work for memory buffers. Emulates dcraw [-4] [-1] [-e]\n"
105 "Usage: %s [-D] [-T] [-v] [-e] raw-files....\n"
106 "\t-4 - output 16-bit PPM\n"
107 "\t-1 - gamma-correct 16-bit data\n"
108 "\t-e - extract thumbnails (same as dcraw -e in separate run)\n",
109 av[0]);
110 return 0;
113 putenv ((char*)"TZ=UTC"); // dcraw compatibility, affects TIFF datestamp field
115 #define P1 RawProcessor.imgdata.idata
116 #define S RawProcessor.imgdata.sizes
117 #define C RawProcessor.imgdata.color
118 #define T RawProcessor.imgdata.thumbnail
119 #define P2 RawProcessor.imgdata.other
120 #define OUT RawProcessor.imgdata.params
123 for (i=1;i<ac;i++)
125 if(av[i][0]=='-')
127 if(av[i][1]=='4' && av[i][2]==0)
128 OUT.output_bps = 16;
129 if(av[i][1]=='1' && av[i][2]==0)
130 OUT.gamma_16bit = 1;
131 if(av[i][1]=='e' && av[i][2]==0)
132 output_thumbs++;
133 continue;
135 printf("Processing %s\n",av[i]);
136 if( (ret = RawProcessor.open_file(av[i])) != LIBRAW_SUCCESS)
138 fprintf(stderr,"Cannot open %s: %s\n",av[i],libraw_strerror(ret));
139 continue; // no recycle b/c open file will recycle itself
143 if( (ret = RawProcessor.unpack() ) != LIBRAW_SUCCESS)
145 fprintf(stderr,"Cannot unpack %s: %s\n",av[i],libraw_strerror(ret));
146 continue;
149 // we should call dcraw_process before thumbnail extraction because for
150 // some cameras (i.e. Kodak ones) white balance for thumbnal should be set
151 // from main image settings
154 ret = RawProcessor.dcraw_process();
156 if(LIBRAW_SUCCESS !=ret)
158 fprintf(stderr,"Cannot do postpocessing on %s: %s\n",
159 av[i],libraw_strerror(ret));
160 if(LIBRAW_FATAL_ERROR(ret))
161 continue;
163 libraw_processed_image_t *image = RawProcessor.dcraw_make_mem_image(&ret);
164 if(image)
166 write_ppm(image,av[i]);
167 free(image);
169 else
170 fprintf(stderr,"Cannot unpack %s to memory buffer: %s\n" , av[i],libraw_strerror(ret));
172 if(output_thumbs)
175 if( (ret = RawProcessor.unpack_thumb() ) != LIBRAW_SUCCESS)
177 fprintf(stderr,"Cannot unpack_thumb %s: %s\n",av[i],libraw_strerror(ret));
178 if(LIBRAW_FATAL_ERROR(ret))
179 continue; // skip to next file
181 else
183 libraw_processed_image_t *thumb = RawProcessor.dcraw_make_mem_thumb(&ret);
184 if(thumb)
186 write_thumb(thumb,av[i]);
187 free(thumb);
189 else
190 fprintf(stderr,"Cannot unpack thumbnail of %s to memory buffer: %s\n" , av[i],libraw_strerror(ret));
195 RawProcessor.recycle(); // just for show this call
197 return 0;