ddraw: Explicitly check for the IDirectDrawColorControl interface in ddraw_surface7_Q...
[wine] / dlls / windowscodecs / jpegformat.c
1 /*
2  * Copyright 2009 Vincent Povirk for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "config.h"
20 #include "wine/port.h"
21
22 #ifdef HAVE_UNISTD_H
23 # include <unistd.h>
24 #endif
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <setjmp.h>
29
30 #ifdef SONAME_LIBJPEG
31 /* This is a hack, so jpeglib.h does not redefine INT32 and the like*/
32 #define XMD_H
33 #define UINT8 JPEG_UINT8
34 #define UINT16 JPEG_UINT16
35 #define boolean jpeg_boolean
36 #undef HAVE_STDLIB_H
37 # include <jpeglib.h>
38 #undef HAVE_STDLIB_H
39 #define HAVE_STDLIB_H 1
40 #undef UINT8
41 #undef UINT16
42 #undef boolean
43 #endif
44
45 #define COBJMACROS
46
47 #include "windef.h"
48 #include "winbase.h"
49 #include "objbase.h"
50 #include "wincodec.h"
51
52 #include "wincodecs_private.h"
53
54 #include "wine/debug.h"
55 #include "wine/library.h"
56
57 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
58
59 #ifdef SONAME_LIBJPEG
60 WINE_DECLARE_DEBUG_CHANNEL(jpeg);
61
62 static void *libjpeg_handle;
63
64 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
65 MAKE_FUNCPTR(jpeg_CreateDecompress);
66 MAKE_FUNCPTR(jpeg_destroy_decompress);
67 MAKE_FUNCPTR(jpeg_read_header);
68 MAKE_FUNCPTR(jpeg_read_scanlines);
69 MAKE_FUNCPTR(jpeg_resync_to_restart);
70 MAKE_FUNCPTR(jpeg_start_decompress);
71 MAKE_FUNCPTR(jpeg_std_error);
72 #undef MAKE_FUNCPTR
73
74 static void *load_libjpeg(void)
75 {
76     if((libjpeg_handle = wine_dlopen(SONAME_LIBJPEG, RTLD_NOW, NULL, 0)) != NULL) {
77
78 #define LOAD_FUNCPTR(f) \
79     if((p##f = wine_dlsym(libjpeg_handle, #f, NULL, 0)) == NULL) { \
80         libjpeg_handle = NULL; \
81         return NULL; \
82     }
83
84         LOAD_FUNCPTR(jpeg_CreateDecompress);
85         LOAD_FUNCPTR(jpeg_destroy_decompress);
86         LOAD_FUNCPTR(jpeg_read_header);
87         LOAD_FUNCPTR(jpeg_read_scanlines);
88         LOAD_FUNCPTR(jpeg_resync_to_restart);
89         LOAD_FUNCPTR(jpeg_start_decompress);
90         LOAD_FUNCPTR(jpeg_std_error);
91 #undef LOAD_FUNCPTR
92     }
93     return libjpeg_handle;
94 }
95
96 static void error_exit_fn(j_common_ptr cinfo)
97 {
98     char message[JMSG_LENGTH_MAX];
99     if (ERR_ON(jpeg))
100     {
101         cinfo->err->format_message(cinfo, message);
102         ERR_(jpeg)("%s\n", message);
103     }
104     longjmp(*(jmp_buf*)cinfo->client_data, 1);
105 }
106
107 static void emit_message_fn(j_common_ptr cinfo, int msg_level)
108 {
109     char message[JMSG_LENGTH_MAX];
110
111     if (msg_level < 0 && ERR_ON(jpeg))
112     {
113         cinfo->err->format_message(cinfo, message);
114         ERR_(jpeg)("%s\n", message);
115     }
116     else if (msg_level == 0 && WARN_ON(jpeg))
117     {
118         cinfo->err->format_message(cinfo, message);
119         WARN_(jpeg)("%s\n", message);
120     }
121     else if (msg_level > 0 && TRACE_ON(jpeg))
122     {
123         cinfo->err->format_message(cinfo, message);
124         TRACE_(jpeg)("%s\n", message);
125     }
126 }
127
128 typedef struct {
129     IWICBitmapDecoder IWICBitmapDecoder_iface;
130     IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
131     LONG ref;
132     BOOL initialized;
133     BOOL cinfo_initialized;
134     IStream *stream;
135     struct jpeg_decompress_struct cinfo;
136     struct jpeg_error_mgr jerr;
137     struct jpeg_source_mgr source_mgr;
138     BYTE source_buffer[1024];
139     BYTE *image_data;
140     CRITICAL_SECTION lock;
141 } JpegDecoder;
142
143 static inline JpegDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
144 {
145     return CONTAINING_RECORD(iface, JpegDecoder, IWICBitmapDecoder_iface);
146 }
147
148 static inline JpegDecoder *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
149 {
150     return CONTAINING_RECORD(iface, JpegDecoder, IWICBitmapFrameDecode_iface);
151 }
152
153 static inline JpegDecoder *decoder_from_decompress(j_decompress_ptr decompress)
154 {
155     return CONTAINING_RECORD(decompress, JpegDecoder, cinfo);
156 }
157
158 static HRESULT WINAPI JpegDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
159     void **ppv)
160 {
161     JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
162     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
163
164     if (!ppv) return E_INVALIDARG;
165
166     if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
167     {
168         *ppv = This;
169     }
170     else
171     {
172         *ppv = NULL;
173         return E_NOINTERFACE;
174     }
175
176     IUnknown_AddRef((IUnknown*)*ppv);
177     return S_OK;
178 }
179
180 static ULONG WINAPI JpegDecoder_AddRef(IWICBitmapDecoder *iface)
181 {
182     JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
183     ULONG ref = InterlockedIncrement(&This->ref);
184
185     TRACE("(%p) refcount=%u\n", iface, ref);
186
187     return ref;
188 }
189
190 static ULONG WINAPI JpegDecoder_Release(IWICBitmapDecoder *iface)
191 {
192     JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
193     ULONG ref = InterlockedDecrement(&This->ref);
194
195     TRACE("(%p) refcount=%u\n", iface, ref);
196
197     if (ref == 0)
198     {
199         This->lock.DebugInfo->Spare[0] = 0;
200         DeleteCriticalSection(&This->lock);
201         if (This->cinfo_initialized) pjpeg_destroy_decompress(&This->cinfo);
202         if (This->stream) IStream_Release(This->stream);
203         HeapFree(GetProcessHeap(), 0, This->image_data);
204         HeapFree(GetProcessHeap(), 0, This);
205     }
206
207     return ref;
208 }
209
210 static HRESULT WINAPI JpegDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
211     DWORD *pdwCapability)
212 {
213     FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
214     return E_NOTIMPL;
215 }
216
217 static void source_mgr_init_source(j_decompress_ptr cinfo)
218 {
219 }
220
221 static jpeg_boolean source_mgr_fill_input_buffer(j_decompress_ptr cinfo)
222 {
223     JpegDecoder *This = decoder_from_decompress(cinfo);
224     HRESULT hr;
225     ULONG bytesread;
226
227     hr = IStream_Read(This->stream, This->source_buffer, 1024, &bytesread);
228
229     if (hr != S_OK || bytesread == 0)
230     {
231         return FALSE;
232     }
233     else
234     {
235         This->source_mgr.next_input_byte = This->source_buffer;
236         This->source_mgr.bytes_in_buffer = bytesread;
237         return TRUE;
238     }
239 }
240
241 static void source_mgr_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
242 {
243     JpegDecoder *This = decoder_from_decompress(cinfo);
244     LARGE_INTEGER seek;
245
246     if (num_bytes > This->source_mgr.bytes_in_buffer)
247     {
248         seek.QuadPart = num_bytes - This->source_mgr.bytes_in_buffer;
249         IStream_Seek(This->stream, seek, STREAM_SEEK_CUR, NULL);
250         This->source_mgr.bytes_in_buffer = 0;
251     }
252     else if (num_bytes > 0)
253     {
254         This->source_mgr.next_input_byte += num_bytes;
255         This->source_mgr.bytes_in_buffer -= num_bytes;
256     }
257 }
258
259 static void source_mgr_term_source(j_decompress_ptr cinfo)
260 {
261 }
262
263 static HRESULT WINAPI JpegDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
264     WICDecodeOptions cacheOptions)
265 {
266     JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
267     int ret;
268     LARGE_INTEGER seek;
269     jmp_buf jmpbuf;
270     TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOptions);
271
272     EnterCriticalSection(&This->lock);
273
274     if (This->cinfo_initialized)
275     {
276         LeaveCriticalSection(&This->lock);
277         return WINCODEC_ERR_WRONGSTATE;
278     }
279
280     pjpeg_std_error(&This->jerr);
281
282     This->jerr.error_exit = error_exit_fn;
283     This->jerr.emit_message = emit_message_fn;
284
285     This->cinfo.err = &This->jerr;
286
287     This->cinfo.client_data = jmpbuf;
288
289     if (setjmp(jmpbuf))
290     {
291         LeaveCriticalSection(&This->lock);
292         return E_FAIL;
293     }
294
295     pjpeg_CreateDecompress(&This->cinfo, JPEG_LIB_VERSION, sizeof(struct jpeg_decompress_struct));
296
297     This->cinfo_initialized = TRUE;
298
299     This->stream = pIStream;
300     IStream_AddRef(pIStream);
301
302     seek.QuadPart = 0;
303     IStream_Seek(This->stream, seek, STREAM_SEEK_SET, NULL);
304
305     This->source_mgr.bytes_in_buffer = 0;
306     This->source_mgr.init_source = source_mgr_init_source;
307     This->source_mgr.fill_input_buffer = source_mgr_fill_input_buffer;
308     This->source_mgr.skip_input_data = source_mgr_skip_input_data;
309     This->source_mgr.resync_to_restart = pjpeg_resync_to_restart;
310     This->source_mgr.term_source = source_mgr_term_source;
311
312     This->cinfo.src = &This->source_mgr;
313
314     ret = pjpeg_read_header(&This->cinfo, TRUE);
315
316     if (ret != JPEG_HEADER_OK) {
317         WARN("Jpeg image in stream has bad format, read header returned %d.\n",ret);
318         LeaveCriticalSection(&This->lock);
319         return E_FAIL;
320     }
321
322     switch (This->cinfo.jpeg_color_space)
323     {
324     case JCS_GRAYSCALE:
325         This->cinfo.out_color_space = JCS_GRAYSCALE;
326         break;
327     case JCS_RGB:
328     case JCS_YCbCr:
329         This->cinfo.out_color_space = JCS_RGB;
330         break;
331     case JCS_CMYK:
332     case JCS_YCCK:
333         This->cinfo.out_color_space = JCS_CMYK;
334         break;
335     default:
336         ERR("Unknown JPEG color space %i\n", This->cinfo.jpeg_color_space);
337         LeaveCriticalSection(&This->lock);
338         return E_FAIL;
339     }
340
341     if (!pjpeg_start_decompress(&This->cinfo))
342     {
343         ERR("jpeg_start_decompress failed\n");
344         LeaveCriticalSection(&This->lock);
345         return E_FAIL;
346     }
347
348     This->initialized = TRUE;
349
350     LeaveCriticalSection(&This->lock);
351
352     return S_OK;
353 }
354
355 static HRESULT WINAPI JpegDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
356     GUID *pguidContainerFormat)
357 {
358     memcpy(pguidContainerFormat, &GUID_ContainerFormatJpeg, sizeof(GUID));
359     return S_OK;
360 }
361
362 static HRESULT WINAPI JpegDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
363     IWICBitmapDecoderInfo **ppIDecoderInfo)
364 {
365     HRESULT hr;
366     IWICComponentInfo *compinfo;
367
368     TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
369
370     hr = CreateComponentInfo(&CLSID_WICJpegDecoder, &compinfo);
371     if (FAILED(hr)) return hr;
372
373     hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
374         (void**)ppIDecoderInfo);
375
376     IWICComponentInfo_Release(compinfo);
377
378     return hr;
379 }
380
381 static HRESULT WINAPI JpegDecoder_CopyPalette(IWICBitmapDecoder *iface,
382     IWICPalette *pIPalette)
383 {
384     TRACE("(%p,%p)\n", iface, pIPalette);
385
386     return WINCODEC_ERR_PALETTEUNAVAILABLE;
387 }
388
389 static HRESULT WINAPI JpegDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
390     IWICMetadataQueryReader **ppIMetadataQueryReader)
391 {
392     FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
393     return E_NOTIMPL;
394 }
395
396 static HRESULT WINAPI JpegDecoder_GetPreview(IWICBitmapDecoder *iface,
397     IWICBitmapSource **ppIBitmapSource)
398 {
399     FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
400     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
401 }
402
403 static HRESULT WINAPI JpegDecoder_GetColorContexts(IWICBitmapDecoder *iface,
404     UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
405 {
406     FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
407     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
408 }
409
410 static HRESULT WINAPI JpegDecoder_GetThumbnail(IWICBitmapDecoder *iface,
411     IWICBitmapSource **ppIThumbnail)
412 {
413     FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
414     return WINCODEC_ERR_CODECNOTHUMBNAIL;
415 }
416
417 static HRESULT WINAPI JpegDecoder_GetFrameCount(IWICBitmapDecoder *iface,
418     UINT *pCount)
419 {
420     *pCount = 1;
421     return S_OK;
422 }
423
424 static HRESULT WINAPI JpegDecoder_GetFrame(IWICBitmapDecoder *iface,
425     UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
426 {
427     JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
428     TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
429
430     if (!This->initialized) return WINCODEC_ERR_NOTINITIALIZED;
431
432     if (index != 0) return E_INVALIDARG;
433
434     IWICBitmapDecoder_AddRef(iface);
435     *ppIBitmapFrame = &This->IWICBitmapFrameDecode_iface;
436
437     return S_OK;
438 }
439
440 static const IWICBitmapDecoderVtbl JpegDecoder_Vtbl = {
441     JpegDecoder_QueryInterface,
442     JpegDecoder_AddRef,
443     JpegDecoder_Release,
444     JpegDecoder_QueryCapability,
445     JpegDecoder_Initialize,
446     JpegDecoder_GetContainerFormat,
447     JpegDecoder_GetDecoderInfo,
448     JpegDecoder_CopyPalette,
449     JpegDecoder_GetMetadataQueryReader,
450     JpegDecoder_GetPreview,
451     JpegDecoder_GetColorContexts,
452     JpegDecoder_GetThumbnail,
453     JpegDecoder_GetFrameCount,
454     JpegDecoder_GetFrame
455 };
456
457 static HRESULT WINAPI JpegDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
458     void **ppv)
459 {
460     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
461
462     if (!ppv) return E_INVALIDARG;
463
464     if (IsEqualIID(&IID_IUnknown, iid) ||
465         IsEqualIID(&IID_IWICBitmapSource, iid) ||
466         IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
467     {
468         *ppv = iface;
469     }
470     else
471     {
472         *ppv = NULL;
473         return E_NOINTERFACE;
474     }
475
476     IUnknown_AddRef((IUnknown*)*ppv);
477     return S_OK;
478 }
479
480 static ULONG WINAPI JpegDecoder_Frame_AddRef(IWICBitmapFrameDecode *iface)
481 {
482     JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
483     return IUnknown_AddRef((IUnknown*)This);
484 }
485
486 static ULONG WINAPI JpegDecoder_Frame_Release(IWICBitmapFrameDecode *iface)
487 {
488     JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
489     return IUnknown_Release((IUnknown*)This);
490 }
491
492 static HRESULT WINAPI JpegDecoder_Frame_GetSize(IWICBitmapFrameDecode *iface,
493     UINT *puiWidth, UINT *puiHeight)
494 {
495     JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
496     *puiWidth = This->cinfo.output_width;
497     *puiHeight = This->cinfo.output_height;
498     TRACE("(%p)->(%u,%u)\n", iface, *puiWidth, *puiHeight);
499     return S_OK;
500 }
501
502 static HRESULT WINAPI JpegDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
503     WICPixelFormatGUID *pPixelFormat)
504 {
505     JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
506     TRACE("(%p,%p)\n", iface, pPixelFormat);
507     if (This->cinfo.out_color_space == JCS_RGB)
508         memcpy(pPixelFormat, &GUID_WICPixelFormat24bppBGR, sizeof(GUID));
509     else if (This->cinfo.out_color_space == JCS_CMYK)
510         memcpy(pPixelFormat, &GUID_WICPixelFormat32bppCMYK, sizeof(GUID));
511     else /* This->cinfo.out_color_space == JCS_GRAYSCALE */
512         memcpy(pPixelFormat, &GUID_WICPixelFormat8bppGray, sizeof(GUID));
513     return S_OK;
514 }
515
516 static HRESULT WINAPI JpegDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
517     double *pDpiX, double *pDpiY)
518 {
519     FIXME("(%p,%p,%p): stub\n", iface, pDpiX, pDpiY);
520     return E_NOTIMPL;
521 }
522
523 static HRESULT WINAPI JpegDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
524     IWICPalette *pIPalette)
525 {
526     FIXME("(%p,%p): stub\n", iface, pIPalette);
527     return E_NOTIMPL;
528 }
529
530 static HRESULT WINAPI JpegDecoder_Frame_CopyPixels(IWICBitmapFrameDecode *iface,
531     const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
532 {
533     JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
534     UINT bpp;
535     UINT stride;
536     UINT data_size;
537     UINT max_row_needed;
538     jmp_buf jmpbuf;
539     WICRect rect;
540     TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
541
542     if (!prc)
543     {
544         rect.X = 0;
545         rect.Y = 0;
546         rect.Width = This->cinfo.output_width;
547         rect.Height = This->cinfo.output_height;
548         prc = &rect;
549     }
550     else
551     {
552         if (prc->X < 0 || prc->Y < 0 || prc->X+prc->Width > This->cinfo.output_width ||
553             prc->Y+prc->Height > This->cinfo.output_height)
554             return E_INVALIDARG;
555     }
556
557     if (This->cinfo.out_color_space == JCS_GRAYSCALE) bpp = 8;
558     else if (This->cinfo.out_color_space == JCS_CMYK) bpp = 32;
559     else bpp = 24;
560
561     stride = bpp * This->cinfo.output_width;
562     data_size = stride * This->cinfo.output_height;
563
564     max_row_needed = prc->Y + prc->Height;
565     if (max_row_needed > This->cinfo.output_height) return E_INVALIDARG;
566
567     EnterCriticalSection(&This->lock);
568
569     if (!This->image_data)
570     {
571         This->image_data = HeapAlloc(GetProcessHeap(), 0, data_size);
572         if (!This->image_data)
573         {
574             LeaveCriticalSection(&This->lock);
575             return E_OUTOFMEMORY;
576         }
577     }
578
579     This->cinfo.client_data = jmpbuf;
580
581     if (setjmp(jmpbuf))
582     {
583         LeaveCriticalSection(&This->lock);
584         return E_FAIL;
585     }
586
587     while (max_row_needed > This->cinfo.output_scanline)
588     {
589         UINT first_scanline = This->cinfo.output_scanline;
590         UINT max_rows;
591         JSAMPROW out_rows[4];
592         UINT i;
593         JDIMENSION ret;
594
595         max_rows = min(This->cinfo.output_height-first_scanline, 4);
596         for (i=0; i<max_rows; i++)
597             out_rows[i] = This->image_data + stride * (first_scanline+i);
598
599         ret = pjpeg_read_scanlines(&This->cinfo, out_rows, max_rows);
600
601         if (ret == 0)
602         {
603             ERR("read_scanlines failed\n");
604             LeaveCriticalSection(&This->lock);
605             return E_FAIL;
606         }
607
608         if (bpp == 24)
609         {
610             /* libjpeg gives us RGB data and we want BGR, so byteswap the data */
611             reverse_bgr8(3, This->image_data + stride * first_scanline,
612                 This->cinfo.output_width, This->cinfo.output_scanline - first_scanline,
613                 stride);
614         }
615
616         if (This->cinfo.out_color_space == JCS_CMYK && This->cinfo.saw_Adobe_marker)
617             /* Adobe JPEG's have inverted CMYK data. */
618             for (i=0; i<data_size; i++)
619                 This->image_data[i] ^= 0xff;
620     }
621
622     LeaveCriticalSection(&This->lock);
623
624     return copy_pixels(bpp, This->image_data,
625         This->cinfo.output_width, This->cinfo.output_height, stride,
626         prc, cbStride, cbBufferSize, pbBuffer);
627 }
628
629 static HRESULT WINAPI JpegDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
630     IWICMetadataQueryReader **ppIMetadataQueryReader)
631 {
632     FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
633     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
634 }
635
636 static HRESULT WINAPI JpegDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode *iface,
637     UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
638 {
639     FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
640     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
641 }
642
643 static HRESULT WINAPI JpegDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode *iface,
644     IWICBitmapSource **ppIThumbnail)
645 {
646     FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
647     return WINCODEC_ERR_CODECNOTHUMBNAIL;
648 }
649
650 static const IWICBitmapFrameDecodeVtbl JpegDecoder_Frame_Vtbl = {
651     JpegDecoder_Frame_QueryInterface,
652     JpegDecoder_Frame_AddRef,
653     JpegDecoder_Frame_Release,
654     JpegDecoder_Frame_GetSize,
655     JpegDecoder_Frame_GetPixelFormat,
656     JpegDecoder_Frame_GetResolution,
657     JpegDecoder_Frame_CopyPalette,
658     JpegDecoder_Frame_CopyPixels,
659     JpegDecoder_Frame_GetMetadataQueryReader,
660     JpegDecoder_Frame_GetColorContexts,
661     JpegDecoder_Frame_GetThumbnail
662 };
663
664 HRESULT JpegDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
665 {
666     JpegDecoder *This;
667     HRESULT ret;
668
669     TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
670
671     if (!libjpeg_handle && !load_libjpeg())
672     {
673         ERR("Failed reading JPEG because unable to find %s\n", SONAME_LIBJPEG);
674         return E_FAIL;
675     }
676
677     *ppv = NULL;
678
679     if (pUnkOuter) return CLASS_E_NOAGGREGATION;
680
681     This = HeapAlloc(GetProcessHeap(), 0, sizeof(JpegDecoder));
682     if (!This) return E_OUTOFMEMORY;
683
684     This->IWICBitmapDecoder_iface.lpVtbl = &JpegDecoder_Vtbl;
685     This->IWICBitmapFrameDecode_iface.lpVtbl = &JpegDecoder_Frame_Vtbl;
686     This->ref = 1;
687     This->initialized = FALSE;
688     This->cinfo_initialized = FALSE;
689     This->stream = NULL;
690     This->image_data = NULL;
691     InitializeCriticalSection(&This->lock);
692     This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JpegDecoder.lock");
693
694     ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
695     IUnknown_Release((IUnknown*)This);
696
697     return ret;
698 }
699
700 #else /* !defined(SONAME_LIBJPEG) */
701
702 HRESULT JpegDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
703 {
704     ERR("Trying to load JPEG picture, but JPEG support is not compiled in.\n");
705     return E_FAIL;
706 }
707
708 #endif