rpcrt4: Don't try to bind to a null handle.
[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     FIXME("(%p,%p): stub\n", iface, ppIDecoderInfo);
366     return E_NOTIMPL;
367 }
368
369 static HRESULT WINAPI JpegDecoder_CopyPalette(IWICBitmapDecoder *iface,
370     IWICPalette *pIPalette)
371 {
372     TRACE("(%p,%p)\n", iface, pIPalette);
373
374     return WINCODEC_ERR_PALETTEUNAVAILABLE;
375 }
376
377 static HRESULT WINAPI JpegDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
378     IWICMetadataQueryReader **ppIMetadataQueryReader)
379 {
380     FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
381     return E_NOTIMPL;
382 }
383
384 static HRESULT WINAPI JpegDecoder_GetPreview(IWICBitmapDecoder *iface,
385     IWICBitmapSource **ppIBitmapSource)
386 {
387     FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
388     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
389 }
390
391 static HRESULT WINAPI JpegDecoder_GetColorContexts(IWICBitmapDecoder *iface,
392     UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
393 {
394     FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
395     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
396 }
397
398 static HRESULT WINAPI JpegDecoder_GetThumbnail(IWICBitmapDecoder *iface,
399     IWICBitmapSource **ppIThumbnail)
400 {
401     FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
402     return WINCODEC_ERR_CODECNOTHUMBNAIL;
403 }
404
405 static HRESULT WINAPI JpegDecoder_GetFrameCount(IWICBitmapDecoder *iface,
406     UINT *pCount)
407 {
408     *pCount = 1;
409     return S_OK;
410 }
411
412 static HRESULT WINAPI JpegDecoder_GetFrame(IWICBitmapDecoder *iface,
413     UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
414 {
415     JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
416     TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
417
418     if (!This->initialized) return WINCODEC_ERR_NOTINITIALIZED;
419
420     if (index != 0) return E_INVALIDARG;
421
422     IWICBitmapDecoder_AddRef(iface);
423     *ppIBitmapFrame = &This->IWICBitmapFrameDecode_iface;
424
425     return S_OK;
426 }
427
428 static const IWICBitmapDecoderVtbl JpegDecoder_Vtbl = {
429     JpegDecoder_QueryInterface,
430     JpegDecoder_AddRef,
431     JpegDecoder_Release,
432     JpegDecoder_QueryCapability,
433     JpegDecoder_Initialize,
434     JpegDecoder_GetContainerFormat,
435     JpegDecoder_GetDecoderInfo,
436     JpegDecoder_CopyPalette,
437     JpegDecoder_GetMetadataQueryReader,
438     JpegDecoder_GetPreview,
439     JpegDecoder_GetColorContexts,
440     JpegDecoder_GetThumbnail,
441     JpegDecoder_GetFrameCount,
442     JpegDecoder_GetFrame
443 };
444
445 static HRESULT WINAPI JpegDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
446     void **ppv)
447 {
448     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
449
450     if (!ppv) return E_INVALIDARG;
451
452     if (IsEqualIID(&IID_IUnknown, iid) ||
453         IsEqualIID(&IID_IWICBitmapSource, iid) ||
454         IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
455     {
456         *ppv = iface;
457     }
458     else
459     {
460         *ppv = NULL;
461         return E_NOINTERFACE;
462     }
463
464     IUnknown_AddRef((IUnknown*)*ppv);
465     return S_OK;
466 }
467
468 static ULONG WINAPI JpegDecoder_Frame_AddRef(IWICBitmapFrameDecode *iface)
469 {
470     JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
471     return IUnknown_AddRef((IUnknown*)This);
472 }
473
474 static ULONG WINAPI JpegDecoder_Frame_Release(IWICBitmapFrameDecode *iface)
475 {
476     JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
477     return IUnknown_Release((IUnknown*)This);
478 }
479
480 static HRESULT WINAPI JpegDecoder_Frame_GetSize(IWICBitmapFrameDecode *iface,
481     UINT *puiWidth, UINT *puiHeight)
482 {
483     JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
484     *puiWidth = This->cinfo.output_width;
485     *puiHeight = This->cinfo.output_height;
486     TRACE("(%p)->(%u,%u)\n", iface, *puiWidth, *puiHeight);
487     return S_OK;
488 }
489
490 static HRESULT WINAPI JpegDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
491     WICPixelFormatGUID *pPixelFormat)
492 {
493     JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
494     TRACE("(%p,%p)\n", iface, pPixelFormat);
495     if (This->cinfo.out_color_space == JCS_RGB)
496         memcpy(pPixelFormat, &GUID_WICPixelFormat24bppBGR, sizeof(GUID));
497     else if (This->cinfo.out_color_space == JCS_CMYK)
498         memcpy(pPixelFormat, &GUID_WICPixelFormat32bppCMYK, sizeof(GUID));
499     else /* This->cinfo.out_color_space == JCS_GRAYSCALE */
500         memcpy(pPixelFormat, &GUID_WICPixelFormat8bppGray, sizeof(GUID));
501     return S_OK;
502 }
503
504 static HRESULT WINAPI JpegDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
505     double *pDpiX, double *pDpiY)
506 {
507     FIXME("(%p,%p,%p): stub\n", iface, pDpiX, pDpiY);
508     return E_NOTIMPL;
509 }
510
511 static HRESULT WINAPI JpegDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
512     IWICPalette *pIPalette)
513 {
514     FIXME("(%p,%p): stub\n", iface, pIPalette);
515     return E_NOTIMPL;
516 }
517
518 static HRESULT WINAPI JpegDecoder_Frame_CopyPixels(IWICBitmapFrameDecode *iface,
519     const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
520 {
521     JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
522     UINT bpp;
523     UINT stride;
524     UINT data_size;
525     UINT max_row_needed;
526     jmp_buf jmpbuf;
527     WICRect rect;
528     TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
529
530     if (!prc)
531     {
532         rect.X = 0;
533         rect.Y = 0;
534         rect.Width = This->cinfo.output_width;
535         rect.Height = This->cinfo.output_height;
536         prc = &rect;
537     }
538     else
539     {
540         if (prc->X < 0 || prc->Y < 0 || prc->X+prc->Width > This->cinfo.output_width ||
541             prc->Y+prc->Height > This->cinfo.output_height)
542             return E_INVALIDARG;
543     }
544
545     if (This->cinfo.out_color_space == JCS_GRAYSCALE) bpp = 8;
546     else if (This->cinfo.out_color_space == JCS_CMYK) bpp = 32;
547     else bpp = 24;
548
549     stride = bpp * This->cinfo.output_width;
550     data_size = stride * This->cinfo.output_height;
551
552     max_row_needed = prc->Y + prc->Height;
553     if (max_row_needed > This->cinfo.output_height) return E_INVALIDARG;
554
555     EnterCriticalSection(&This->lock);
556
557     if (!This->image_data)
558     {
559         This->image_data = HeapAlloc(GetProcessHeap(), 0, data_size);
560         if (!This->image_data)
561         {
562             LeaveCriticalSection(&This->lock);
563             return E_OUTOFMEMORY;
564         }
565     }
566
567     This->cinfo.client_data = &jmpbuf;
568
569     if (setjmp(jmpbuf))
570     {
571         LeaveCriticalSection(&This->lock);
572         return E_FAIL;
573     }
574
575     while (max_row_needed > This->cinfo.output_scanline)
576     {
577         UINT first_scanline = This->cinfo.output_scanline;
578         UINT max_rows;
579         JSAMPROW out_rows[4];
580         UINT i;
581         JDIMENSION ret;
582
583         max_rows = min(This->cinfo.output_height-first_scanline, 4);
584         for (i=0; i<max_rows; i++)
585             out_rows[i] = This->image_data + stride * (first_scanline+i);
586
587         ret = pjpeg_read_scanlines(&This->cinfo, out_rows, max_rows);
588
589         if (ret == 0)
590         {
591             ERR("read_scanlines failed\n");
592             LeaveCriticalSection(&This->lock);
593             return E_FAIL;
594         }
595
596         if (bpp == 24)
597         {
598             /* libjpeg gives us RGB data and we want BGR, so byteswap the data */
599             reverse_bgr8(3, This->image_data + stride * first_scanline,
600                 This->cinfo.output_width, This->cinfo.output_scanline - first_scanline,
601                 stride);
602         }
603
604         if (This->cinfo.out_color_space == JCS_CMYK && This->cinfo.saw_Adobe_marker)
605             /* Adobe JPEG's have inverted CMYK data. */
606             for (i=0; i<data_size; i++)
607                 This->image_data[i] ^= 0xff;
608     }
609
610     LeaveCriticalSection(&This->lock);
611
612     return copy_pixels(bpp, This->image_data,
613         This->cinfo.output_width, This->cinfo.output_height, stride,
614         prc, cbStride, cbBufferSize, pbBuffer);
615 }
616
617 static HRESULT WINAPI JpegDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
618     IWICMetadataQueryReader **ppIMetadataQueryReader)
619 {
620     FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
621     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
622 }
623
624 static HRESULT WINAPI JpegDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode *iface,
625     UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
626 {
627     FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
628     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
629 }
630
631 static HRESULT WINAPI JpegDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode *iface,
632     IWICBitmapSource **ppIThumbnail)
633 {
634     FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
635     return WINCODEC_ERR_CODECNOTHUMBNAIL;
636 }
637
638 static const IWICBitmapFrameDecodeVtbl JpegDecoder_Frame_Vtbl = {
639     JpegDecoder_Frame_QueryInterface,
640     JpegDecoder_Frame_AddRef,
641     JpegDecoder_Frame_Release,
642     JpegDecoder_Frame_GetSize,
643     JpegDecoder_Frame_GetPixelFormat,
644     JpegDecoder_Frame_GetResolution,
645     JpegDecoder_Frame_CopyPalette,
646     JpegDecoder_Frame_CopyPixels,
647     JpegDecoder_Frame_GetMetadataQueryReader,
648     JpegDecoder_Frame_GetColorContexts,
649     JpegDecoder_Frame_GetThumbnail
650 };
651
652 HRESULT JpegDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
653 {
654     JpegDecoder *This;
655     HRESULT ret;
656
657     TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
658
659     if (!libjpeg_handle && !load_libjpeg())
660     {
661         ERR("Failed reading JPEG because unable to find %s\n", SONAME_LIBJPEG);
662         return E_FAIL;
663     }
664
665     *ppv = NULL;
666
667     if (pUnkOuter) return CLASS_E_NOAGGREGATION;
668
669     This = HeapAlloc(GetProcessHeap(), 0, sizeof(JpegDecoder));
670     if (!This) return E_OUTOFMEMORY;
671
672     This->IWICBitmapDecoder_iface.lpVtbl = &JpegDecoder_Vtbl;
673     This->IWICBitmapFrameDecode_iface.lpVtbl = &JpegDecoder_Frame_Vtbl;
674     This->ref = 1;
675     This->initialized = FALSE;
676     This->cinfo_initialized = FALSE;
677     This->stream = NULL;
678     This->image_data = NULL;
679     InitializeCriticalSection(&This->lock);
680     This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JpegDecoder.lock");
681
682     ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
683     IUnknown_Release((IUnknown*)This);
684
685     return ret;
686 }
687
688 #else /* !defined(SONAME_LIBJPEG) */
689
690 HRESULT JpegDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
691 {
692     ERR("Trying to load JPEG picture, but JPEG support is not compiled in.\n");
693     return E_FAIL;
694 }
695
696 #endif