3 * Copyright 2008-2009 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)
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
28 #include "libraw/libraw.h"
31 #define snprintf _snprintf
33 #pragma comment(lib, "ws2_32.lib")
35 #include <netinet/in.h>
39 // no error reporting, only params check
40 void write_ppm(libraw_processed_image_t
*img
, const char *basename
)
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;
49 snprintf(fn
,1024,"%s.ppm",basename
);
50 FILE *f
= fopen(fn
,"wb");
52 fprintf (f
, "P6\n%d %d\n%d\n", img
->width
, img
->height
, (1 << img
->bits
)-1);
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]);
65 fwrite(img
->data
,img
->data_size
,1,f
);
69 void write_thumb(libraw_processed_image_t
*img
, const char *basename
)
73 if(img
->type
== LIBRAW_IMAGE_BITMAP
)
76 snprintf(fnt
,1024,"%s.thumb",basename
);
79 else if (img
->type
== LIBRAW_IMAGE_JPEG
)
82 snprintf(fn
,1024,"%s.thumb.jpg",basename
);
83 FILE *f
= fopen(fn
,"wb");
85 fwrite(img
->data
,img
->data_size
,1,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];
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",
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
127 if(av
[i
][1]=='4' && av
[i
][2]==0)
129 if(av
[i
][1]=='1' && av
[i
][2]==0)
131 if(av
[i
][1]=='e' && av
[i
][2]==0)
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
));
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
))
163 libraw_processed_image_t
*image
= RawProcessor
.dcraw_make_mem_image(&ret
);
166 write_ppm(image
,av
[i
]);
170 fprintf(stderr
,"Cannot unpack %s to memory buffer: %s\n" , av
[i
],libraw_strerror(ret
));
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
183 libraw_processed_image_t
*thumb
= RawProcessor
.dcraw_make_mem_thumb(&ret
);
186 write_thumb(thumb
,av
[i
]);
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