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
20 #include "wine/port.h"
31 /* This is a hack, so jpeglib.h does not redefine INT32 and the like*/
33 #define UINT8 JPEG_UINT8
34 #define UINT16 JPEG_UINT16
35 #define boolean jpeg_boolean
39 #define HAVE_STDLIB_H 1
52 #include "wincodecs_private.h"
54 #include "wine/debug.h"
55 #include "wine/library.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs
);
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
);
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; \
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
);
93 return libjpeg_handle
;
96 static void error_exit_fn(j_common_ptr cinfo
)
98 char message
[JMSG_LENGTH_MAX
];
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
);
129 IWICBitmapDecoder IWICBitmapDecoder_iface
;
130 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface
;
133 BOOL cinfo_initialized
;
135 struct jpeg_decompress_struct cinfo
;
136 struct jpeg_error_mgr jerr
;
137 struct jpeg_source_mgr source_mgr
;
138 BYTE source_buffer
[1024];
140 CRITICAL_SECTION lock
;
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
,
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
))
173 return E_NOINTERFACE
;
176 IUnknown_AddRef((IUnknown
*)*ppv
);
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
);
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
);
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
);
210 static HRESULT WINAPI
JpegDecoder_QueryCapability(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
211 DWORD
*pdwCapability
)
213 FIXME("(%p,%p,%p): stub\n", iface
, pIStream
, pdwCapability
);
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
);
227 hr
= IStream_Read(This
->stream
, This
->source_buffer
, 1024, &bytesread
);
229 if (hr
!= S_OK
|| bytesread
== 0)
235 This
->source_mgr
.next_input_byte
= This
->source_buffer
;
236 This
->source_mgr
.bytes_in_buffer
= bytesread
;
241 static void source_mgr_skip_input_data(j_decompress_ptr cinfo
, long num_bytes
)
243 JpegDecoder
*This
= decoder_from_decompress(cinfo
);
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
);
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
;
291 LeaveCriticalSection(&This
->lock
);
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
);
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
);
322 switch (This
->cinfo
.jpeg_color_space
)
325 This
->cinfo
.out_color_space
= JCS_GRAYSCALE
;
329 This
->cinfo
.out_color_space
= JCS_RGB
;
333 This
->cinfo
.out_color_space
= JCS_CMYK
;
336 ERR("Unknown JPEG color space %i\n", This
->cinfo
.jpeg_color_space
);
337 LeaveCriticalSection(&This
->lock
);
341 if (!pjpeg_start_decompress(&This
->cinfo
))
343 ERR("jpeg_start_decompress failed\n");
344 LeaveCriticalSection(&This
->lock
);
348 This
->initialized
= TRUE
;
350 LeaveCriticalSection(&This
->lock
);
355 static HRESULT WINAPI
JpegDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
356 GUID
*pguidContainerFormat
)
358 memcpy(pguidContainerFormat
, &GUID_ContainerFormatJpeg
, sizeof(GUID
));
362 static HRESULT WINAPI
JpegDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
363 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
365 FIXME("(%p,%p): stub\n", iface
, ppIDecoderInfo
);
369 static HRESULT WINAPI
JpegDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
370 IWICPalette
*pIPalette
)
372 TRACE("(%p,%p)\n", iface
, pIPalette
);
374 return WINCODEC_ERR_PALETTEUNAVAILABLE
;
377 static HRESULT WINAPI
JpegDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
378 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
380 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
384 static HRESULT WINAPI
JpegDecoder_GetPreview(IWICBitmapDecoder
*iface
,
385 IWICBitmapSource
**ppIBitmapSource
)
387 FIXME("(%p,%p): stub\n", iface
, ppIBitmapSource
);
388 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
391 static HRESULT WINAPI
JpegDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
392 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
394 FIXME("(%p,%u,%p,%p): stub\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
395 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
398 static HRESULT WINAPI
JpegDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
399 IWICBitmapSource
**ppIThumbnail
)
401 FIXME("(%p,%p): stub\n", iface
, ppIThumbnail
);
402 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
405 static HRESULT WINAPI
JpegDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
412 static HRESULT WINAPI
JpegDecoder_GetFrame(IWICBitmapDecoder
*iface
,
413 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
415 JpegDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
416 TRACE("(%p,%u,%p)\n", iface
, index
, ppIBitmapFrame
);
418 if (!This
->initialized
) return WINCODEC_ERR_NOTINITIALIZED
;
420 if (index
!= 0) return E_INVALIDARG
;
422 IWICBitmapDecoder_AddRef(iface
);
423 *ppIBitmapFrame
= &This
->IWICBitmapFrameDecode_iface
;
428 static const IWICBitmapDecoderVtbl JpegDecoder_Vtbl
= {
429 JpegDecoder_QueryInterface
,
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
,
445 static HRESULT WINAPI
JpegDecoder_Frame_QueryInterface(IWICBitmapFrameDecode
*iface
, REFIID iid
,
448 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
450 if (!ppv
) return E_INVALIDARG
;
452 if (IsEqualIID(&IID_IUnknown
, iid
) ||
453 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
454 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
461 return E_NOINTERFACE
;
464 IUnknown_AddRef((IUnknown
*)*ppv
);
468 static ULONG WINAPI
JpegDecoder_Frame_AddRef(IWICBitmapFrameDecode
*iface
)
470 JpegDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
471 return IUnknown_AddRef((IUnknown
*)This
);
474 static ULONG WINAPI
JpegDecoder_Frame_Release(IWICBitmapFrameDecode
*iface
)
476 JpegDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
477 return IUnknown_Release((IUnknown
*)This
);
480 static HRESULT WINAPI
JpegDecoder_Frame_GetSize(IWICBitmapFrameDecode
*iface
,
481 UINT
*puiWidth
, UINT
*puiHeight
)
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
);
490 static HRESULT WINAPI
JpegDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
491 WICPixelFormatGUID
*pPixelFormat
)
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
));
504 static HRESULT WINAPI
JpegDecoder_Frame_GetResolution(IWICBitmapFrameDecode
*iface
,
505 double *pDpiX
, double *pDpiY
)
507 FIXME("(%p,%p,%p): stub\n", iface
, pDpiX
, pDpiY
);
511 static HRESULT WINAPI
JpegDecoder_Frame_CopyPalette(IWICBitmapFrameDecode
*iface
,
512 IWICPalette
*pIPalette
)
514 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
518 static HRESULT WINAPI
JpegDecoder_Frame_CopyPixels(IWICBitmapFrameDecode
*iface
,
519 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
521 JpegDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
528 TRACE("(%p,%p,%u,%u,%p)\n", iface
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
534 rect
.Width
= This
->cinfo
.output_width
;
535 rect
.Height
= This
->cinfo
.output_height
;
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
)
545 if (This
->cinfo
.out_color_space
== JCS_GRAYSCALE
) bpp
= 8;
546 else if (This
->cinfo
.out_color_space
== JCS_CMYK
) bpp
= 32;
549 stride
= bpp
* This
->cinfo
.output_width
;
550 data_size
= stride
* This
->cinfo
.output_height
;
552 max_row_needed
= prc
->Y
+ prc
->Height
;
553 if (max_row_needed
> This
->cinfo
.output_height
) return E_INVALIDARG
;
555 EnterCriticalSection(&This
->lock
);
557 if (!This
->image_data
)
559 This
->image_data
= HeapAlloc(GetProcessHeap(), 0, data_size
);
560 if (!This
->image_data
)
562 LeaveCriticalSection(&This
->lock
);
563 return E_OUTOFMEMORY
;
567 This
->cinfo
.client_data
= &jmpbuf
;
571 LeaveCriticalSection(&This
->lock
);
575 while (max_row_needed
> This
->cinfo
.output_scanline
)
577 UINT first_scanline
= This
->cinfo
.output_scanline
;
579 JSAMPROW out_rows
[4];
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
);
587 ret
= pjpeg_read_scanlines(&This
->cinfo
, out_rows
, max_rows
);
591 ERR("read_scanlines failed\n");
592 LeaveCriticalSection(&This
->lock
);
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
,
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;
610 LeaveCriticalSection(&This
->lock
);
612 return copy_pixels(bpp
, This
->image_data
,
613 This
->cinfo
.output_width
, This
->cinfo
.output_height
, stride
,
614 prc
, cbStride
, cbBufferSize
, pbBuffer
);
617 static HRESULT WINAPI
JpegDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
618 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
620 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
621 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
624 static HRESULT WINAPI
JpegDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode
*iface
,
625 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
627 FIXME("(%p,%u,%p,%p): stub\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
628 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
631 static HRESULT WINAPI
JpegDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode
*iface
,
632 IWICBitmapSource
**ppIThumbnail
)
634 FIXME("(%p,%p): stub\n", iface
, ppIThumbnail
);
635 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
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
652 HRESULT
JpegDecoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
657 TRACE("(%p,%s,%p)\n", pUnkOuter
, debugstr_guid(iid
), ppv
);
659 if (!libjpeg_handle
&& !load_libjpeg())
661 ERR("Failed reading JPEG because unable to find %s\n", SONAME_LIBJPEG
);
667 if (pUnkOuter
) return CLASS_E_NOAGGREGATION
;
669 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(JpegDecoder
));
670 if (!This
) return E_OUTOFMEMORY
;
672 This
->IWICBitmapDecoder_iface
.lpVtbl
= &JpegDecoder_Vtbl
;
673 This
->IWICBitmapFrameDecode_iface
.lpVtbl
= &JpegDecoder_Frame_Vtbl
;
675 This
->initialized
= FALSE
;
676 This
->cinfo_initialized
= FALSE
;
678 This
->image_data
= NULL
;
679 InitializeCriticalSection(&This
->lock
);
680 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": JpegDecoder.lock");
682 ret
= IUnknown_QueryInterface((IUnknown
*)This
, iid
, ppv
);
683 IUnknown_Release((IUnknown
*)This
);
688 #else /* !defined(SONAME_LIBJPEG) */
690 HRESULT
JpegDecoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
692 ERR("Trying to load JPEG picture, but JPEG support is not compiled in.\n");