msxml3: Added ISAXLocator_getLineNumber and ISAXLocator_getColumnNumber partial imple...
[wine] / dlls / gphoto2.ds / ds_image.c
1 /*
2  * Copyright 2000 Corel Corporation
3  * Copyright 2006 Marcus Meissner
4  * Copyright 2006 CodeWeavers, Aric Stewart
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23 #include "wine/library.h"
24
25 #include <stdarg.h>
26 #include <stdio.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "twain.h"
33 #include "gphoto2_i.h"
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(twain);
37
38 #ifdef HAVE_GPHOTO2
39 static void *libjpeg_handle;
40 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
41 MAKE_FUNCPTR(jpeg_std_error);
42 MAKE_FUNCPTR(jpeg_CreateDecompress);
43 MAKE_FUNCPTR(jpeg_read_header);
44 MAKE_FUNCPTR(jpeg_start_decompress);
45 MAKE_FUNCPTR(jpeg_read_scanlines);
46 MAKE_FUNCPTR(jpeg_finish_decompress);
47 MAKE_FUNCPTR(jpeg_destroy_decompress);
48 #undef MAKE_FUNCPTR
49
50 static void *load_libjpeg(void)
51 {
52     if((libjpeg_handle = wine_dlopen(SONAME_LIBJPEG, RTLD_NOW, NULL, 0)) != NULL) {
53
54 #define LOAD_FUNCPTR(f) \
55     if((p##f = wine_dlsym(libjpeg_handle, #f, NULL, 0)) == NULL) { \
56         libjpeg_handle = NULL; \
57         return NULL; \
58     }
59
60         LOAD_FUNCPTR(jpeg_std_error);
61         LOAD_FUNCPTR(jpeg_CreateDecompress);
62         LOAD_FUNCPTR(jpeg_read_header);
63         LOAD_FUNCPTR(jpeg_start_decompress);
64         LOAD_FUNCPTR(jpeg_read_scanlines);
65         LOAD_FUNCPTR(jpeg_finish_decompress);
66         LOAD_FUNCPTR(jpeg_destroy_decompress);
67 #undef LOAD_FUNCPTR
68     }
69     return libjpeg_handle;
70 }
71
72
73 /* for the jpeg decompressor source manager. */
74 static void _jpeg_init_source(j_decompress_ptr cinfo) { }
75
76 static boolean _jpeg_fill_input_buffer(j_decompress_ptr cinfo) {
77     ERR("(), should not get here.\n");
78     return FALSE;
79 }
80
81 static void _jpeg_skip_input_data(j_decompress_ptr cinfo,long num_bytes) {
82     TRACE("Skipping %ld bytes...\n", num_bytes);
83     cinfo->src->next_input_byte += num_bytes;
84     cinfo->src->bytes_in_buffer -= num_bytes;
85 }
86
87 static boolean _jpeg_resync_to_restart(j_decompress_ptr cinfo, int desired) {
88     ERR("(desired=%d), should not get here.\n",desired);
89     return FALSE;
90 }
91 static void _jpeg_term_source(j_decompress_ptr cinfo) { }
92 #endif
93
94 /* DG_IMAGE/DAT_CIECOLOR/MSG_GET */
95 TW_UINT16 GPHOTO2_CIEColorGet (pTW_IDENTITY pOrigin, 
96                              TW_MEMREF pData)
97 {
98     FIXME ("stub!\n");
99
100     return TWRC_FAILURE;
101 }
102
103 /* DG_IMAGE/DAT_EXTIMAGEINFO/MSG_GET */
104 TW_UINT16 GPHOTO2_ExtImageInfoGet (pTW_IDENTITY pOrigin, 
105                                  TW_MEMREF pData)
106 {
107     FIXME ("stub!\n");
108
109     return TWRC_FAILURE;
110 }
111
112 /* DG_IMAGE/DAT_GRAYRESPONSE/MSG_RESET */
113 TW_UINT16 GPHOTO2_GrayResponseReset (pTW_IDENTITY pOrigin, 
114                                    TW_MEMREF pData)
115 {
116     FIXME ("stub!\n");
117
118     return TWRC_FAILURE;
119 }
120
121 /* DG_IMAGE/DAT_GRAYRESPONSE/MSG_SET */
122 TW_UINT16 GPHOTO2_GrayResponseSet (pTW_IDENTITY pOrigin, 
123                                  TW_MEMREF pData)
124 {
125     FIXME ("stub!\n");
126
127     return TWRC_FAILURE;
128 }
129
130 /* DG_IMAGE/DAT_IMAGEFILEXFER/MSG_GET */
131 TW_UINT16 GPHOTO2_ImageFileXferGet (pTW_IDENTITY pOrigin, 
132                                   TW_MEMREF pData)
133 {
134     FIXME ("stub!\n");
135
136     return TWRC_FAILURE;
137 }
138
139 #ifdef HAVE_GPHOTO2
140 static TW_UINT16 _get_image_and_startup_jpeg(void) {
141     const char *folder = NULL, *filename = NULL;
142     struct gphoto2_file *file;
143     const unsigned char *filedata;
144     unsigned long filesize;
145     int ret;
146
147     if (activeDS.file) /* Already loaded. */
148         return TWRC_SUCCESS;
149
150     if(!libjpeg_handle) {
151         if(!load_libjpeg()) {
152             FIXME("Failed reading JPEG because unable to find %s\n", SONAME_LIBJPEG);
153             filedata = NULL;
154             return TWRC_FAILURE;
155         }
156     }
157
158     LIST_FOR_EACH_ENTRY( file, &activeDS.files, struct gphoto2_file, entry ) {
159         if (strstr(file->filename,".JPG") || strstr(file->filename,".jpg")) {
160             filename = file->filename;
161             folder = file->folder;
162             TRACE("downloading %s/%s\n", folder, filename);
163             if (file->download) {
164                 file->download = FALSE; /* mark as done */
165                 break;
166             }
167         }
168     }
169     gp_file_new (&activeDS.file);
170     ret = gp_camera_file_get(activeDS.camera, folder, filename, GP_FILE_TYPE_NORMAL,
171                              activeDS.file, activeDS.context);
172     if (ret < GP_OK) {
173         FIXME("Failed to get file?\n");
174         activeDS.twCC = TWCC_SEQERROR;
175         return TWRC_FAILURE;
176     }
177     ret = gp_file_get_data_and_size (activeDS.file, (const char**)&filedata, &filesize);
178     if (ret < GP_OK) {
179         FIXME("Failed to get file data?\n");
180         activeDS.twCC = TWCC_SEQERROR;
181         return TWRC_FAILURE;
182     }
183
184     /* This is basically so we can use in-memory data for jpeg decompression.
185      * We need to have all the functions.
186      */
187     activeDS.xjsm.next_input_byte       = filedata;
188     activeDS.xjsm.bytes_in_buffer       = filesize;
189     activeDS.xjsm.init_source   = _jpeg_init_source;
190     activeDS.xjsm.fill_input_buffer     = _jpeg_fill_input_buffer;
191     activeDS.xjsm.skip_input_data       = _jpeg_skip_input_data;
192     activeDS.xjsm.resync_to_restart     = _jpeg_resync_to_restart;
193     activeDS.xjsm.term_source   = _jpeg_term_source;
194
195     activeDS.jd.err = pjpeg_std_error(&activeDS.jerr);
196     /* jpeg_create_decompress is a macro that expands to jpeg_CreateDecompress - see jpeglib.h
197      * jpeg_create_decompress(&jd); */
198     pjpeg_CreateDecompress(&activeDS.jd, JPEG_LIB_VERSION, (size_t) sizeof(struct jpeg_decompress_struct));
199     activeDS.jd.src = &activeDS.xjsm;
200     ret=pjpeg_read_header(&activeDS.jd,TRUE);
201     activeDS.jd.out_color_space = JCS_RGB;
202     pjpeg_start_decompress(&activeDS.jd);
203     if (ret != JPEG_HEADER_OK) {
204         ERR("Jpeg image in stream has bad format, read header returned %d.\n",ret);
205         gp_file_unref (activeDS.file);
206         activeDS.file = NULL;
207         return TWRC_FAILURE;
208     }
209     return TWRC_SUCCESS;
210 }
211 #endif
212
213 /* DG_IMAGE/DAT_IMAGEINFO/MSG_GET */
214 TW_UINT16 GPHOTO2_ImageInfoGet (pTW_IDENTITY pOrigin, 
215                               TW_MEMREF pData)
216 {
217 #ifdef HAVE_GPHOTO2
218     pTW_IMAGEINFO pImageInfo = (pTW_IMAGEINFO) pData;
219
220     TRACE("DG_IMAGE/DAT_IMAGEINFO/MSG_GET\n");
221
222     if (activeDS.currentState != 6 && activeDS.currentState != 7) {
223         activeDS.twCC = TWCC_SEQERROR;
224         return TWRC_FAILURE;
225     }
226     if (TWRC_SUCCESS != _get_image_and_startup_jpeg()) {
227         FIXME("Failed to get an image\n");
228         activeDS.twCC = TWCC_SEQERROR;
229         return TWRC_FAILURE;
230     }
231     if (activeDS.currentState == 6)
232     {
233         /* return general image description information about the image about to be transferred */
234         TRACE("Getting parameters\n");
235     }
236     TRACE("activeDS.jd.output_width = %d\n", activeDS.jd.output_width);
237     TRACE("activeDS.jd.output_height = %d\n", activeDS.jd.output_height);
238     pImageInfo->Compression     = TWCP_NONE;
239     pImageInfo->SamplesPerPixel = 3;
240     pImageInfo->BitsPerSample[0]= 8;
241     pImageInfo->BitsPerSample[1]= 8;
242     pImageInfo->BitsPerSample[2]= 8;
243     pImageInfo->PixelType       = TWPT_RGB;
244     pImageInfo->Planar          = FALSE; /* R-G-B is chunky! */
245     pImageInfo->XResolution.Whole = -1;
246     pImageInfo->XResolution.Frac = 0;
247     pImageInfo->YResolution.Whole = -1;
248     pImageInfo->YResolution.Frac = 0;
249     pImageInfo->ImageWidth      = activeDS.jd.output_width;
250     pImageInfo->ImageLength     = activeDS.jd.output_height;
251     pImageInfo->BitsPerPixel    = 24;
252     return TWRC_SUCCESS;
253 #else
254     return TWRC_FAILURE;
255 #endif
256 }
257
258 /* DG_IMAGE/DAT_IMAGELAYOUT/MSG_GET */
259 TW_UINT16 GPHOTO2_ImageLayoutGet (pTW_IDENTITY pOrigin, 
260                                 TW_MEMREF pData)
261 {
262     FIXME ("stub!\n");
263
264     return TWRC_FAILURE;
265 }
266
267 /* DG_IMAGE/DAT_IMAGELAYOUT/MSG_GETDEFAULT */
268 TW_UINT16 GPHOTO2_ImageLayoutGetDefault (pTW_IDENTITY pOrigin, 
269                                        TW_MEMREF pData)
270 {
271     FIXME ("stub!\n");
272
273     return TWRC_FAILURE;
274 }
275
276 /* DG_IMAGE/DAT_IMAGELAYOUT/MSG_RESET */
277 TW_UINT16 GPHOTO2_ImageLayoutReset (pTW_IDENTITY pOrigin, 
278                                   TW_MEMREF pData)
279 {
280     FIXME ("stub!\n");
281
282     return TWRC_FAILURE;
283 }
284
285 /* DG_IMAGE/DAT_IMAGELAYOUT/MSG_SET */
286 TW_UINT16 GPHOTO2_ImageLayoutSet (pTW_IDENTITY pOrigin, 
287                                 TW_MEMREF pData)
288 {
289     FIXME ("stub!\n");
290
291     return TWRC_FAILURE;
292 }
293
294 /* DG_IMAGE/DAT_IMAGEMEMXFER/MSG_GET */
295 TW_UINT16 GPHOTO2_ImageMemXferGet (pTW_IDENTITY pOrigin, 
296                                  TW_MEMREF pData)
297 {
298 #ifdef HAVE_GPHOTO2
299     TW_UINT16 twRC = TWRC_SUCCESS;
300     pTW_IMAGEMEMXFER pImageMemXfer = (pTW_IMAGEMEMXFER) pData;
301     LPBYTE buffer;
302     int readrows;
303     unsigned int curoff;
304
305     TRACE ("DG_IMAGE/DAT_IMAGEMEMXFER/MSG_GET\n");
306     if (activeDS.currentState < 6 || activeDS.currentState > 7) {
307         activeDS.twCC = TWCC_SEQERROR;
308         return TWRC_FAILURE;
309     }
310     TRACE("pImageMemXfer.Compression is %d\n", pImageMemXfer->Compression);
311     if (activeDS.currentState == 6) {
312         if (TWRC_SUCCESS != _get_image_and_startup_jpeg()) {
313             FIXME("Failed to get an image\n");
314             activeDS.twCC = TWCC_SEQERROR;
315             return TWRC_FAILURE;
316         }
317
318     if (!activeDS.progressWnd)
319         activeDS.progressWnd = TransferringDialogBox(NULL,0);
320     TransferringDialogBox(activeDS.progressWnd,0);
321
322         activeDS.currentState = 7;
323     } else {
324         if (!activeDS.file) {
325             activeDS.twCC = TWRC_SUCCESS;
326             return TWRC_XFERDONE;
327         }
328     }
329
330     if (pImageMemXfer->Memory.Flags & TWMF_HANDLE) {
331         FIXME("Memory Handle, may not be locked correctly\n");
332         buffer = LocalLock(pImageMemXfer->Memory.TheMem);
333     } else
334         buffer = pImageMemXfer->Memory.TheMem;
335    
336     memset(buffer,0,pImageMemXfer->Memory.Length);
337     curoff = 0; readrows = 0;
338     pImageMemXfer->YOffset      = activeDS.jd.output_scanline;
339     pImageMemXfer->XOffset      = 0;    /* we do whole strips */
340     while ((activeDS.jd.output_scanline<activeDS.jd.output_height) &&
341            ((pImageMemXfer->Memory.Length - curoff) > activeDS.jd.output_width*activeDS.jd.output_components)
342     ) {
343         JSAMPROW row = buffer+curoff;
344         int x = pjpeg_read_scanlines(&activeDS.jd,&row,1);
345         if (x != 1) {
346                 FIXME("failed to read current scanline?\n");
347                 break;
348         }
349         readrows++;
350         curoff += activeDS.jd.output_width*activeDS.jd.output_components;
351     }
352     pImageMemXfer->Compression  = TWCP_NONE;
353     pImageMemXfer->BytesPerRow  = activeDS.jd.output_components * activeDS.jd.output_width;
354     pImageMemXfer->Rows         = readrows;
355     pImageMemXfer->Columns      = activeDS.jd.output_width; /* we do whole strips */
356     pImageMemXfer->BytesWritten = curoff;
357
358     TransferringDialogBox(activeDS.progressWnd,0);
359
360     if (activeDS.jd.output_scanline == activeDS.jd.output_height) {
361         pjpeg_finish_decompress(&activeDS.jd);
362         pjpeg_destroy_decompress(&activeDS.jd);
363         gp_file_unref (activeDS.file);
364         activeDS.file = NULL;
365         TRACE("xfer is done!\n");
366
367         /*TransferringDialogBox(activeDS.progressWnd, -1);*/
368         twRC = TWRC_XFERDONE;
369     }
370     activeDS.twCC = TWRC_SUCCESS;
371     if (pImageMemXfer->Memory.Flags & TWMF_HANDLE)
372         LocalUnlock(pImageMemXfer->Memory.TheMem);
373     return twRC;
374 #else
375     return TWRC_FAILURE;
376 #endif
377 }
378
379 /* DG_IMAGE/DAT_IMAGENATIVEXFER/MSG_GET */
380 TW_UINT16 GPHOTO2_ImageNativeXferGet (pTW_IDENTITY pOrigin, 
381                                     TW_MEMREF pData)
382 {
383 #ifdef HAVE_GPHOTO2
384     pTW_UINT32 pHandle = (pTW_UINT32) pData;
385     HBITMAP hDIB;
386     BITMAPINFO bmpInfo;
387     LPBYTE bits, oldbits;
388     JSAMPROW samprow, oldsamprow;
389     HDC dc;
390
391     FIXME("DG_IMAGE/DAT_IMAGENATIVEXFER/MSG_GET: implemented, but expect program crash due to DIB.\n");
392
393 /*  NOTE NOTE NOTE NOTE NOTE NOTE NOTE
394  *
395  *  While this is a mandatory transfer mode and this function
396  *  is correctly implemented and fully works, the calling program
397  *  will likely crash after calling.
398  *
399  *  Reason is that there is a lot of example code that does:
400  *  bmpinfo = (LPBITMAPINFOHEADER)GlobalLock(hBITMAP); ... pointer access to bmpinfo
401  *
402  *  Our current HBITMAP handles do not support getting GlobalLocked -> App Crash
403  *
404  *  This needs a GDI Handle rewrite, at least for DIB sections.
405  *  - Marcus
406  */
407     if (activeDS.currentState != 6) {
408         activeDS.twCC = TWCC_SEQERROR;
409         return TWRC_FAILURE;
410     }
411     if (TWRC_SUCCESS != _get_image_and_startup_jpeg()) {
412         FIXME("Failed to get an image\n");
413         activeDS.twCC = TWCC_OPERATIONERROR;
414         return TWRC_FAILURE;
415     }
416     TRACE("Acquiring image %dx%dx%d bits from gphoto.\n",
417         activeDS.jd.output_width, activeDS.jd.output_height,
418         activeDS.jd.output_components*8);
419     ZeroMemory (&bmpInfo, sizeof (BITMAPINFO));
420     bmpInfo.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
421     bmpInfo.bmiHeader.biWidth = activeDS.jd.output_width;
422     bmpInfo.bmiHeader.biHeight = -activeDS.jd.output_height;
423     bmpInfo.bmiHeader.biPlanes = 1;
424     bmpInfo.bmiHeader.biBitCount = activeDS.jd.output_components*8;
425     bmpInfo.bmiHeader.biCompression = BI_RGB;
426     bmpInfo.bmiHeader.biSizeImage = 0;
427     bmpInfo.bmiHeader.biXPelsPerMeter = 0;
428     bmpInfo.bmiHeader.biYPelsPerMeter = 0;
429     bmpInfo.bmiHeader.biClrUsed = 0;
430     bmpInfo.bmiHeader.biClrImportant = 0;
431     hDIB = CreateDIBSection ((dc = GetDC(activeDS.hwndOwner)), &bmpInfo,
432                              DIB_RGB_COLORS, (LPVOID)&bits, 0, 0);
433     if (!hDIB) {
434         FIXME("Failed creating DIB.\n");
435         gp_file_unref (activeDS.file);
436         activeDS.file = NULL;
437         activeDS.twCC = TWCC_LOWMEMORY;
438         return TWRC_FAILURE;
439     }
440     samprow = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,activeDS.jd.output_width*activeDS.jd.output_components);
441     oldbits = bits;
442     oldsamprow = samprow;
443     while ( activeDS.jd.output_scanline<activeDS.jd.output_height ) {
444         int i, x = pjpeg_read_scanlines(&activeDS.jd,&samprow,1);
445         if (x != 1) {
446                 FIXME("failed to read current scanline?\n");
447                 break;
448         }
449         /* We have to convert from RGB to BGR, see MSDN/ BITMAPINFOHEADER */
450         for(i=0;i<activeDS.jd.output_width;i++,samprow+=activeDS.jd.output_components) {
451             *(bits++) = *(samprow+2);
452             *(bits++) = *(samprow+1);
453             *(bits++) = *(samprow);
454         }
455         bits = (LPBYTE)(((UINT_PTR)bits + 3) & ~3);
456         samprow = oldsamprow;
457     }
458     bits = oldbits;
459     HeapFree (GetProcessHeap(), 0, samprow);
460     gp_file_unref (activeDS.file);
461     activeDS.file = NULL;
462     ReleaseDC (activeDS.hwndOwner, dc);
463     *pHandle = (TW_UINT32)hDIB;
464     activeDS.twCC = TWCC_SUCCESS;
465     activeDS.currentState = 7;
466     return TWRC_XFERDONE;
467 #else
468     return TWRC_FAILURE;
469 #endif
470 }
471
472 /* DG_IMAGE/DAT_JPEGCOMPRESSION/MSG_GET */
473 TW_UINT16 GPHOTO2_JPEGCompressionGet (pTW_IDENTITY pOrigin, 
474                                     TW_MEMREF pData)
475 {
476     FIXME ("stub!\n");
477
478     return TWRC_FAILURE;
479 }
480
481 /* DG_IMAGE/DAT_JPEGCOMPRESSION/MSG_GETDEFAULT */
482 TW_UINT16 GPHOTO2_JPEGCompressionGetDefault (pTW_IDENTITY pOrigin,
483                                            
484                                            TW_MEMREF pData)
485 {
486     FIXME ("stub!\n");
487
488     return TWRC_FAILURE;
489 }
490
491 /* DG_IMAGE/DAT_JPEGCOMPRESSION/MSG_RESET */
492 TW_UINT16 GPHOTO2_JPEGCompressionReset (pTW_IDENTITY pOrigin, 
493                                       TW_MEMREF pData)
494 {
495     FIXME ("stub!\n");
496
497     return TWRC_FAILURE;
498 }
499
500 /* DG_IMAGE/DAT_JPEGCOMPRESSION/MSG_SET */
501 TW_UINT16 GPHOTO2_JPEGCompressionSet (pTW_IDENTITY pOrigin, 
502                                     TW_MEMREF pData)
503 {
504     FIXME ("stub!\n");
505
506     return TWRC_FAILURE;
507 }
508
509 /* DG_IMAGE/DAT_PALETTE8/MSG_GET */
510 TW_UINT16 GPHOTO2_Palette8Get (pTW_IDENTITY pOrigin, 
511                              TW_MEMREF pData)
512 {
513     FIXME ("stub!\n");
514
515     return TWRC_FAILURE;
516 }
517
518 /* DG_IMAGE/DAT_PALETTE8/MSG_GETDEFAULT */
519 TW_UINT16 GPHOTO2_Palette8GetDefault (pTW_IDENTITY pOrigin, 
520                                     TW_MEMREF pData)
521 {
522     FIXME ("stub!\n");
523
524     return TWRC_FAILURE;
525 }
526
527 /* DG_IMAGE/DAT_PALETTE8/MSG_RESET */
528 TW_UINT16 GPHOTO2_Palette8Reset (pTW_IDENTITY pOrigin, 
529                                TW_MEMREF pData)
530 {
531     FIXME ("stub!\n");
532
533     return TWRC_FAILURE;
534 }
535
536 /* DG_IMAGE/DAT_PALETTE8/MSG_SET */
537 TW_UINT16 GPHOTO2_Palette8Set (pTW_IDENTITY pOrigin, 
538                              TW_MEMREF pData)
539 {
540     FIXME ("stub!\n");
541
542     return TWRC_FAILURE;
543 }
544
545 /* DG_IMAGE/DAT_RGBRESPONSE/MSG_RESET */
546 TW_UINT16 GPHOTO2_RGBResponseReset (pTW_IDENTITY pOrigin, 
547                                   TW_MEMREF pData)
548 {
549     FIXME ("stub!\n");
550
551     return TWRC_FAILURE;
552 }
553
554 /* DG_IMAGE/DAT_RGBRESPONSE/MSG_SET */
555 TW_UINT16 GPHOTO2_RGBResponseSet (pTW_IDENTITY pOrigin, 
556                                 TW_MEMREF pData)
557 {
558     FIXME ("stub!\n");
559
560     return TWRC_FAILURE;
561 }
562
563 #ifdef HAVE_GPHOTO2
564 TW_UINT16
565 _get_gphoto2_file_as_DIB(
566     const char *folder, const char *filename, CameraFileType type,
567     HWND hwnd, HBITMAP *hDIB
568 ) {
569     const unsigned char *filedata;
570     unsigned long       filesize;
571     int                 ret;
572     CameraFile          *file;
573     struct jpeg_source_mgr              xjsm;
574     struct jpeg_decompress_struct       jd;
575     struct jpeg_error_mgr               jerr;
576     HDC                 dc;
577     BITMAPINFO          bmpInfo;
578     LPBYTE              bits, oldbits;
579     JSAMPROW            samprow, oldsamprow;
580
581     if(!libjpeg_handle) {
582         if(!load_libjpeg()) {
583             FIXME("Failed reading JPEG because unable to find %s\n", SONAME_LIBJPEG);
584             filedata = NULL;
585             return TWRC_FAILURE;
586         }
587     }
588
589     gp_file_new (&file);
590     ret = gp_camera_file_get(activeDS.camera, folder, filename, type, file, activeDS.context);
591     if (ret < GP_OK) {
592         FIXME("Failed to get file?\n");
593         gp_file_unref (file);
594         return TWRC_FAILURE;
595     }
596     ret = gp_file_get_data_and_size (file, (const char**)&filedata, &filesize);
597     if (ret < GP_OK) {
598         FIXME("Failed to get file data?\n");
599         return TWRC_FAILURE;
600     }
601
602     /* FIXME: Actually we might get other types than JPEG ... But only handle JPEG for now */
603     if (filedata[0] != 0xff) {
604         ERR("File %s/%s might not be JPEG, cannot decode!\n", folder, filename);
605     }
606
607     /* This is basically so we can use in-memory data for jpeg decompression.
608      * We need to have all the functions.
609      */
610     xjsm.next_input_byte        = filedata;
611     xjsm.bytes_in_buffer        = filesize;
612     xjsm.init_source    = _jpeg_init_source;
613     xjsm.fill_input_buffer      = _jpeg_fill_input_buffer;
614     xjsm.skip_input_data        = _jpeg_skip_input_data;
615     xjsm.resync_to_restart      = _jpeg_resync_to_restart;
616     xjsm.term_source    = _jpeg_term_source;
617
618     jd.err = pjpeg_std_error(&jerr);
619     /* jpeg_create_decompress is a macro that expands to jpeg_CreateDecompress - see jpeglib.h
620      * jpeg_create_decompress(&jd); */
621     pjpeg_CreateDecompress(&jd, JPEG_LIB_VERSION, (size_t) sizeof(struct jpeg_decompress_struct));
622     jd.src = &xjsm;
623     ret=pjpeg_read_header(&jd,TRUE);
624     jd.out_color_space = JCS_RGB;
625     pjpeg_start_decompress(&jd);
626     if (ret != JPEG_HEADER_OK) {
627         ERR("Jpeg image in stream has bad format, read header returned %d.\n",ret);
628         gp_file_unref (file);
629         return TWRC_FAILURE;
630     }
631
632     ZeroMemory (&bmpInfo, sizeof (BITMAPINFO));
633     bmpInfo.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
634     bmpInfo.bmiHeader.biWidth = jd.output_width;
635     bmpInfo.bmiHeader.biHeight = -jd.output_height;
636     bmpInfo.bmiHeader.biPlanes = 1;
637     bmpInfo.bmiHeader.biBitCount = jd.output_components*8;
638     bmpInfo.bmiHeader.biCompression = BI_RGB;
639     bmpInfo.bmiHeader.biSizeImage = 0;
640     bmpInfo.bmiHeader.biXPelsPerMeter = 0;
641     bmpInfo.bmiHeader.biYPelsPerMeter = 0;
642     bmpInfo.bmiHeader.biClrUsed = 0;
643     bmpInfo.bmiHeader.biClrImportant = 0;
644     *hDIB = CreateDIBSection ((dc = GetDC(hwnd)), &bmpInfo, DIB_RGB_COLORS, (LPVOID)&bits, 0, 0);
645     if (!*hDIB) {
646         FIXME("Failed creating DIB.\n");
647         gp_file_unref (file);
648         return TWRC_FAILURE;
649     }
650     samprow = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,jd.output_width*jd.output_components);
651     oldbits = bits;
652     oldsamprow = samprow;
653     while ( jd.output_scanline<jd.output_height ) {
654         int i, x = pjpeg_read_scanlines(&jd,&samprow,1);
655         if (x != 1) {
656             FIXME("failed to read current scanline?\n");
657             break;
658         }
659         /* We have to convert from RGB to BGR, see MSDN/ BITMAPINFOHEADER */
660         for(i=0;i<jd.output_width;i++,samprow+=jd.output_components) {
661             *(bits++) = *(samprow+2);
662             *(bits++) = *(samprow+1);
663             *(bits++) = *(samprow);
664         }
665         bits = (LPBYTE)(((UINT_PTR)bits + 3) & ~3);
666         samprow = oldsamprow;
667     }
668     if (hwnd) ReleaseDC (hwnd, dc);
669     HeapFree (GetProcessHeap(), 0, samprow);
670     gp_file_unref (file);
671     return TWRC_SUCCESS;
672 }
673 #endif