comctl32: Validate the day of month when scrolling through years.
[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;
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 = 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     oldsamprow = samprow;
442     while ( activeDS.jd.output_scanline<activeDS.jd.output_height ) {
443         int i, x = pjpeg_read_scanlines(&activeDS.jd,&samprow,1);
444         if (x != 1) {
445                 FIXME("failed to read current scanline?\n");
446                 break;
447         }
448         /* We have to convert from RGB to BGR, see MSDN/ BITMAPINFOHEADER */
449         for(i=0;i<activeDS.jd.output_width;i++,samprow+=activeDS.jd.output_components) {
450             *(bits++) = *(samprow+2);
451             *(bits++) = *(samprow+1);
452             *(bits++) = *(samprow);
453         }
454         bits = (LPBYTE)(((UINT_PTR)bits + 3) & ~3);
455         samprow = oldsamprow;
456     }
457     HeapFree (GetProcessHeap(), 0, samprow);
458     gp_file_unref (activeDS.file);
459     activeDS.file = NULL;
460     ReleaseDC (activeDS.hwndOwner, dc);
461     *pHandle = (UINT_PTR)hDIB;
462     activeDS.twCC = TWCC_SUCCESS;
463     activeDS.currentState = 7;
464     return TWRC_XFERDONE;
465 #else
466     return TWRC_FAILURE;
467 #endif
468 }
469
470 /* DG_IMAGE/DAT_JPEGCOMPRESSION/MSG_GET */
471 TW_UINT16 GPHOTO2_JPEGCompressionGet (pTW_IDENTITY pOrigin, 
472                                     TW_MEMREF pData)
473 {
474     FIXME ("stub!\n");
475
476     return TWRC_FAILURE;
477 }
478
479 /* DG_IMAGE/DAT_JPEGCOMPRESSION/MSG_GETDEFAULT */
480 TW_UINT16 GPHOTO2_JPEGCompressionGetDefault (pTW_IDENTITY pOrigin,
481                                            
482                                            TW_MEMREF pData)
483 {
484     FIXME ("stub!\n");
485
486     return TWRC_FAILURE;
487 }
488
489 /* DG_IMAGE/DAT_JPEGCOMPRESSION/MSG_RESET */
490 TW_UINT16 GPHOTO2_JPEGCompressionReset (pTW_IDENTITY pOrigin, 
491                                       TW_MEMREF pData)
492 {
493     FIXME ("stub!\n");
494
495     return TWRC_FAILURE;
496 }
497
498 /* DG_IMAGE/DAT_JPEGCOMPRESSION/MSG_SET */
499 TW_UINT16 GPHOTO2_JPEGCompressionSet (pTW_IDENTITY pOrigin, 
500                                     TW_MEMREF pData)
501 {
502     FIXME ("stub!\n");
503
504     return TWRC_FAILURE;
505 }
506
507 /* DG_IMAGE/DAT_PALETTE8/MSG_GET */
508 TW_UINT16 GPHOTO2_Palette8Get (pTW_IDENTITY pOrigin, 
509                              TW_MEMREF pData)
510 {
511     FIXME ("stub!\n");
512
513     return TWRC_FAILURE;
514 }
515
516 /* DG_IMAGE/DAT_PALETTE8/MSG_GETDEFAULT */
517 TW_UINT16 GPHOTO2_Palette8GetDefault (pTW_IDENTITY pOrigin, 
518                                     TW_MEMREF pData)
519 {
520     FIXME ("stub!\n");
521
522     return TWRC_FAILURE;
523 }
524
525 /* DG_IMAGE/DAT_PALETTE8/MSG_RESET */
526 TW_UINT16 GPHOTO2_Palette8Reset (pTW_IDENTITY pOrigin, 
527                                TW_MEMREF pData)
528 {
529     FIXME ("stub!\n");
530
531     return TWRC_FAILURE;
532 }
533
534 /* DG_IMAGE/DAT_PALETTE8/MSG_SET */
535 TW_UINT16 GPHOTO2_Palette8Set (pTW_IDENTITY pOrigin, 
536                              TW_MEMREF pData)
537 {
538     FIXME ("stub!\n");
539
540     return TWRC_FAILURE;
541 }
542
543 /* DG_IMAGE/DAT_RGBRESPONSE/MSG_RESET */
544 TW_UINT16 GPHOTO2_RGBResponseReset (pTW_IDENTITY pOrigin, 
545                                   TW_MEMREF pData)
546 {
547     FIXME ("stub!\n");
548
549     return TWRC_FAILURE;
550 }
551
552 /* DG_IMAGE/DAT_RGBRESPONSE/MSG_SET */
553 TW_UINT16 GPHOTO2_RGBResponseSet (pTW_IDENTITY pOrigin, 
554                                 TW_MEMREF pData)
555 {
556     FIXME ("stub!\n");
557
558     return TWRC_FAILURE;
559 }
560
561 #ifdef HAVE_GPHOTO2
562 TW_UINT16
563 _get_gphoto2_file_as_DIB(
564     const char *folder, const char *filename, CameraFileType type,
565     HWND hwnd, HBITMAP *hDIB
566 ) {
567     const unsigned char *filedata;
568     unsigned long       filesize;
569     int                 ret;
570     CameraFile          *file;
571     struct jpeg_source_mgr              xjsm;
572     struct jpeg_decompress_struct       jd;
573     struct jpeg_error_mgr               jerr;
574     HDC                 dc;
575     BITMAPINFO          bmpInfo;
576     LPBYTE              bits;
577     JSAMPROW            samprow, oldsamprow;
578
579     if(!libjpeg_handle) {
580         if(!load_libjpeg()) {
581             FIXME("Failed reading JPEG because unable to find %s\n", SONAME_LIBJPEG);
582             filedata = NULL;
583             return TWRC_FAILURE;
584         }
585     }
586
587     gp_file_new (&file);
588     ret = gp_camera_file_get(activeDS.camera, folder, filename, type, file, activeDS.context);
589     if (ret < GP_OK) {
590         FIXME("Failed to get file?\n");
591         gp_file_unref (file);
592         return TWRC_FAILURE;
593     }
594     ret = gp_file_get_data_and_size (file, (const char**)&filedata, &filesize);
595     if (ret < GP_OK) {
596         FIXME("Failed to get file data?\n");
597         return TWRC_FAILURE;
598     }
599
600     /* FIXME: Actually we might get other types than JPEG ... But only handle JPEG for now */
601     if (filedata[0] != 0xff) {
602         ERR("File %s/%s might not be JPEG, cannot decode!\n", folder, filename);
603     }
604
605     /* This is basically so we can use in-memory data for jpeg decompression.
606      * We need to have all the functions.
607      */
608     xjsm.next_input_byte        = filedata;
609     xjsm.bytes_in_buffer        = filesize;
610     xjsm.init_source    = _jpeg_init_source;
611     xjsm.fill_input_buffer      = _jpeg_fill_input_buffer;
612     xjsm.skip_input_data        = _jpeg_skip_input_data;
613     xjsm.resync_to_restart      = _jpeg_resync_to_restart;
614     xjsm.term_source    = _jpeg_term_source;
615
616     jd.err = pjpeg_std_error(&jerr);
617     /* jpeg_create_decompress is a macro that expands to jpeg_CreateDecompress - see jpeglib.h
618      * jpeg_create_decompress(&jd); */
619     pjpeg_CreateDecompress(&jd, JPEG_LIB_VERSION, (size_t) sizeof(struct jpeg_decompress_struct));
620     jd.src = &xjsm;
621     ret=pjpeg_read_header(&jd,TRUE);
622     jd.out_color_space = JCS_RGB;
623     pjpeg_start_decompress(&jd);
624     if (ret != JPEG_HEADER_OK) {
625         ERR("Jpeg image in stream has bad format, read header returned %d.\n",ret);
626         gp_file_unref (file);
627         return TWRC_FAILURE;
628     }
629
630     ZeroMemory (&bmpInfo, sizeof (BITMAPINFO));
631     bmpInfo.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
632     bmpInfo.bmiHeader.biWidth = jd.output_width;
633     bmpInfo.bmiHeader.biHeight = -jd.output_height;
634     bmpInfo.bmiHeader.biPlanes = 1;
635     bmpInfo.bmiHeader.biBitCount = jd.output_components*8;
636     bmpInfo.bmiHeader.biCompression = BI_RGB;
637     bmpInfo.bmiHeader.biSizeImage = 0;
638     bmpInfo.bmiHeader.biXPelsPerMeter = 0;
639     bmpInfo.bmiHeader.biYPelsPerMeter = 0;
640     bmpInfo.bmiHeader.biClrUsed = 0;
641     bmpInfo.bmiHeader.biClrImportant = 0;
642     *hDIB = CreateDIBSection ((dc = GetDC(hwnd)), &bmpInfo, DIB_RGB_COLORS, (LPVOID)&bits, 0, 0);
643     if (!*hDIB) {
644         FIXME("Failed creating DIB.\n");
645         gp_file_unref (file);
646         return TWRC_FAILURE;
647     }
648     samprow = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,jd.output_width*jd.output_components);
649     oldsamprow = samprow;
650     while ( jd.output_scanline<jd.output_height ) {
651         int i, x = pjpeg_read_scanlines(&jd,&samprow,1);
652         if (x != 1) {
653             FIXME("failed to read current scanline?\n");
654             break;
655         }
656         /* We have to convert from RGB to BGR, see MSDN/ BITMAPINFOHEADER */
657         for(i=0;i<jd.output_width;i++,samprow+=jd.output_components) {
658             *(bits++) = *(samprow+2);
659             *(bits++) = *(samprow+1);
660             *(bits++) = *(samprow);
661         }
662         bits = (LPBYTE)(((UINT_PTR)bits + 3) & ~3);
663         samprow = oldsamprow;
664     }
665     if (hwnd) ReleaseDC (hwnd, dc);
666     HeapFree (GetProcessHeap(), 0, samprow);
667     gp_file_unref (file);
668     return TWRC_SUCCESS;
669 }
670 #endif