gdi32: Make clip_line external.
[wine/multimedia.git] / dlls / windowscodecs / jpegformat.c
blob083a352e0437e3219e92df8736d1b5f2d6ccce23
1 /*
2 * Copyright 2009 Vincent Povirk for CodeWeavers
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.
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.
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
19 #include "config.h"
20 #include "wine/port.h"
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>
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
45 #define COBJMACROS
47 #include "windef.h"
48 #include "winbase.h"
49 #include "objbase.h"
50 #include "wincodec.h"
52 #include "wincodecs_private.h"
54 #include "wine/debug.h"
55 #include "wine/library.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
59 #ifdef SONAME_LIBJPEG
60 WINE_DECLARE_DEBUG_CHANNEL(jpeg);
62 static void *libjpeg_handle;
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
74 static void *load_libjpeg(void)
76 if((libjpeg_handle = wine_dlopen(SONAME_LIBJPEG, RTLD_NOW, NULL, 0)) != NULL) {
78 #define LOAD_FUNCPTR(f) \
79 if((p##f = wine_dlsym(libjpeg_handle, #f, NULL, 0)) == NULL) { \
80 libjpeg_handle = NULL; \
81 return NULL; \
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
93 return libjpeg_handle;
96 static void error_exit_fn(j_common_ptr cinfo)
98 char message[JMSG_LENGTH_MAX];
99 if (ERR_ON(jpeg))
101 cinfo->err->format_message(cinfo, message);
102 ERR_(jpeg)("%s\n", message);
104 longjmp(*(jmp_buf*)cinfo->client_data, 1);
107 static void emit_message_fn(j_common_ptr cinfo, int msg_level)
109 char message[JMSG_LENGTH_MAX];
111 if (msg_level < 0 && ERR_ON(jpeg))
113 cinfo->err->format_message(cinfo, message);
114 ERR_(jpeg)("%s\n", message);
116 else if (msg_level == 0 && WARN_ON(jpeg))
118 cinfo->err->format_message(cinfo, message);
119 WARN_(jpeg)("%s\n", message);
121 else if (msg_level > 0 && TRACE_ON(jpeg))
123 cinfo->err->format_message(cinfo, message);
124 TRACE_(jpeg)("%s\n", message);
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;
143 static inline JpegDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
145 return CONTAINING_RECORD(iface, JpegDecoder, IWICBitmapDecoder_iface);
148 static inline JpegDecoder *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
150 return CONTAINING_RECORD(iface, JpegDecoder, IWICBitmapFrameDecode_iface);
153 static inline JpegDecoder *decoder_from_decompress(j_decompress_ptr decompress)
155 return CONTAINING_RECORD(decompress, JpegDecoder, cinfo);
158 static HRESULT WINAPI JpegDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
159 void **ppv)
161 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
162 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
164 if (!ppv) return E_INVALIDARG;
166 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
168 *ppv = This;
170 else
172 *ppv = NULL;
173 return E_NOINTERFACE;
176 IUnknown_AddRef((IUnknown*)*ppv);
177 return S_OK;
180 static ULONG WINAPI JpegDecoder_AddRef(IWICBitmapDecoder *iface)
182 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
183 ULONG ref = InterlockedIncrement(&This->ref);
185 TRACE("(%p) refcount=%u\n", iface, ref);
187 return ref;
190 static ULONG WINAPI JpegDecoder_Release(IWICBitmapDecoder *iface)
192 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
193 ULONG ref = InterlockedDecrement(&This->ref);
195 TRACE("(%p) refcount=%u\n", iface, ref);
197 if (ref == 0)
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);
207 return ref;
210 static HRESULT WINAPI JpegDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
211 DWORD *pdwCapability)
213 FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
214 return E_NOTIMPL;
217 static void source_mgr_init_source(j_decompress_ptr cinfo)
221 static jpeg_boolean source_mgr_fill_input_buffer(j_decompress_ptr cinfo)
223 JpegDecoder *This = decoder_from_decompress(cinfo);
224 HRESULT hr;
225 ULONG bytesread;
227 hr = IStream_Read(This->stream, This->source_buffer, 1024, &bytesread);
229 if (hr != S_OK || bytesread == 0)
231 return FALSE;
233 else
235 This->source_mgr.next_input_byte = This->source_buffer;
236 This->source_mgr.bytes_in_buffer = bytesread;
237 return TRUE;
241 static void source_mgr_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
243 JpegDecoder *This = decoder_from_decompress(cinfo);
244 LARGE_INTEGER seek;
246 if (num_bytes > This->source_mgr.bytes_in_buffer)
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;
252 else if (num_bytes > 0)
254 This->source_mgr.next_input_byte += num_bytes;
255 This->source_mgr.bytes_in_buffer -= num_bytes;
259 static void source_mgr_term_source(j_decompress_ptr cinfo)
263 static HRESULT WINAPI JpegDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
264 WICDecodeOptions cacheOptions)
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);
272 EnterCriticalSection(&This->lock);
274 if (This->cinfo_initialized)
276 LeaveCriticalSection(&This->lock);
277 return WINCODEC_ERR_WRONGSTATE;
280 pjpeg_std_error(&This->jerr);
282 This->jerr.error_exit = error_exit_fn;
283 This->jerr.emit_message = emit_message_fn;
285 This->cinfo.err = &This->jerr;
287 This->cinfo.client_data = &jmpbuf;
289 if (setjmp(jmpbuf))
291 LeaveCriticalSection(&This->lock);
292 return E_FAIL;
295 pjpeg_CreateDecompress(&This->cinfo, JPEG_LIB_VERSION, sizeof(struct jpeg_decompress_struct));
297 This->cinfo_initialized = TRUE;
299 This->stream = pIStream;
300 IStream_AddRef(pIStream);
302 seek.QuadPart = 0;
303 IStream_Seek(This->stream, seek, STREAM_SEEK_SET, NULL);
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;
312 This->cinfo.src = &This->source_mgr;
314 ret = pjpeg_read_header(&This->cinfo, TRUE);
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;
322 switch (This->cinfo.jpeg_color_space)
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;
341 if (!pjpeg_start_decompress(&This->cinfo))
343 ERR("jpeg_start_decompress failed\n");
344 LeaveCriticalSection(&This->lock);
345 return E_FAIL;
348 This->initialized = TRUE;
350 LeaveCriticalSection(&This->lock);
352 return S_OK;
355 static HRESULT WINAPI JpegDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
356 GUID *pguidContainerFormat)
358 memcpy(pguidContainerFormat, &GUID_ContainerFormatJpeg, sizeof(GUID));
359 return S_OK;
362 static HRESULT WINAPI JpegDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
363 IWICBitmapDecoderInfo **ppIDecoderInfo)
365 HRESULT hr;
366 IWICComponentInfo *compinfo;
368 TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
370 hr = CreateComponentInfo(&CLSID_WICJpegDecoder, &compinfo);
371 if (FAILED(hr)) return hr;
373 hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
374 (void**)ppIDecoderInfo);
376 IWICComponentInfo_Release(compinfo);
378 return hr;
381 static HRESULT WINAPI JpegDecoder_CopyPalette(IWICBitmapDecoder *iface,
382 IWICPalette *pIPalette)
384 TRACE("(%p,%p)\n", iface, pIPalette);
386 return WINCODEC_ERR_PALETTEUNAVAILABLE;
389 static HRESULT WINAPI JpegDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
390 IWICMetadataQueryReader **ppIMetadataQueryReader)
392 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
393 return E_NOTIMPL;
396 static HRESULT WINAPI JpegDecoder_GetPreview(IWICBitmapDecoder *iface,
397 IWICBitmapSource **ppIBitmapSource)
399 FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
400 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
403 static HRESULT WINAPI JpegDecoder_GetColorContexts(IWICBitmapDecoder *iface,
404 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
406 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
407 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
410 static HRESULT WINAPI JpegDecoder_GetThumbnail(IWICBitmapDecoder *iface,
411 IWICBitmapSource **ppIThumbnail)
413 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
414 return WINCODEC_ERR_CODECNOTHUMBNAIL;
417 static HRESULT WINAPI JpegDecoder_GetFrameCount(IWICBitmapDecoder *iface,
418 UINT *pCount)
420 *pCount = 1;
421 return S_OK;
424 static HRESULT WINAPI JpegDecoder_GetFrame(IWICBitmapDecoder *iface,
425 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
427 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
428 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
430 if (!This->initialized) return WINCODEC_ERR_NOTINITIALIZED;
432 if (index != 0) return E_INVALIDARG;
434 IWICBitmapDecoder_AddRef(iface);
435 *ppIBitmapFrame = &This->IWICBitmapFrameDecode_iface;
437 return S_OK;
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
457 static HRESULT WINAPI JpegDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
458 void **ppv)
460 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
462 if (!ppv) return E_INVALIDARG;
464 if (IsEqualIID(&IID_IUnknown, iid) ||
465 IsEqualIID(&IID_IWICBitmapSource, iid) ||
466 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
468 *ppv = iface;
470 else
472 *ppv = NULL;
473 return E_NOINTERFACE;
476 IUnknown_AddRef((IUnknown*)*ppv);
477 return S_OK;
480 static ULONG WINAPI JpegDecoder_Frame_AddRef(IWICBitmapFrameDecode *iface)
482 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
483 return IUnknown_AddRef((IUnknown*)This);
486 static ULONG WINAPI JpegDecoder_Frame_Release(IWICBitmapFrameDecode *iface)
488 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
489 return IUnknown_Release((IUnknown*)This);
492 static HRESULT WINAPI JpegDecoder_Frame_GetSize(IWICBitmapFrameDecode *iface,
493 UINT *puiWidth, UINT *puiHeight)
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;
502 static HRESULT WINAPI JpegDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
503 WICPixelFormatGUID *pPixelFormat)
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;
516 static HRESULT WINAPI JpegDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
517 double *pDpiX, double *pDpiY)
519 FIXME("(%p,%p,%p): stub\n", iface, pDpiX, pDpiY);
520 return E_NOTIMPL;
523 static HRESULT WINAPI JpegDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
524 IWICPalette *pIPalette)
526 FIXME("(%p,%p): stub\n", iface, pIPalette);
527 return E_NOTIMPL;
530 static HRESULT WINAPI JpegDecoder_Frame_CopyPixels(IWICBitmapFrameDecode *iface,
531 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
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);
542 if (!prc)
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;
550 else
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;
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;
561 stride = bpp * This->cinfo.output_width;
562 data_size = stride * This->cinfo.output_height;
564 max_row_needed = prc->Y + prc->Height;
565 if (max_row_needed > This->cinfo.output_height) return E_INVALIDARG;
567 EnterCriticalSection(&This->lock);
569 if (!This->image_data)
571 This->image_data = HeapAlloc(GetProcessHeap(), 0, data_size);
572 if (!This->image_data)
574 LeaveCriticalSection(&This->lock);
575 return E_OUTOFMEMORY;
579 This->cinfo.client_data = &jmpbuf;
581 if (setjmp(jmpbuf))
583 LeaveCriticalSection(&This->lock);
584 return E_FAIL;
587 while (max_row_needed > This->cinfo.output_scanline)
589 UINT first_scanline = This->cinfo.output_scanline;
590 UINT max_rows;
591 JSAMPROW out_rows[4];
592 UINT i;
593 JDIMENSION ret;
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);
599 ret = pjpeg_read_scanlines(&This->cinfo, out_rows, max_rows);
601 if (ret == 0)
603 ERR("read_scanlines failed\n");
604 LeaveCriticalSection(&This->lock);
605 return E_FAIL;
608 if (bpp == 24)
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);
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;
622 LeaveCriticalSection(&This->lock);
624 return copy_pixels(bpp, This->image_data,
625 This->cinfo.output_width, This->cinfo.output_height, stride,
626 prc, cbStride, cbBufferSize, pbBuffer);
629 static HRESULT WINAPI JpegDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
630 IWICMetadataQueryReader **ppIMetadataQueryReader)
632 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
633 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
636 static HRESULT WINAPI JpegDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode *iface,
637 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
639 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
640 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
643 static HRESULT WINAPI JpegDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode *iface,
644 IWICBitmapSource **ppIThumbnail)
646 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
647 return WINCODEC_ERR_CODECNOTHUMBNAIL;
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
664 HRESULT JpegDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
666 JpegDecoder *This;
667 HRESULT ret;
669 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
671 if (!libjpeg_handle && !load_libjpeg())
673 ERR("Failed reading JPEG because unable to find %s\n", SONAME_LIBJPEG);
674 return E_FAIL;
677 *ppv = NULL;
679 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
681 This = HeapAlloc(GetProcessHeap(), 0, sizeof(JpegDecoder));
682 if (!This) return E_OUTOFMEMORY;
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");
694 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
695 IUnknown_Release((IUnknown*)This);
697 return ret;
700 #else /* !defined(SONAME_LIBJPEG) */
702 HRESULT JpegDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
704 ERR("Trying to load JPEG picture, but JPEG support is not compiled in.\n");
705 return E_FAIL;
708 #endif