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
);
58 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 const IWICBitmapDecoderVtbl
*lpVtbl
;
130 const IWICBitmapFrameDecodeVtbl
*lpFrameVtbl
;
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
*decoder_from_decompress(j_decompress_ptr decompress
)
145 return CONTAINING_RECORD(decompress
, JpegDecoder
, cinfo
);
148 static inline JpegDecoder
*decoder_from_frame(IWICBitmapFrameDecode
*iface
)
150 return CONTAINING_RECORD(iface
, JpegDecoder
, lpFrameVtbl
);
153 static HRESULT WINAPI
JpegDecoder_QueryInterface(IWICBitmapDecoder
*iface
, REFIID iid
,
156 JpegDecoder
*This
= (JpegDecoder
*)iface
;
157 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
159 if (!ppv
) return E_INVALIDARG
;
161 if (IsEqualIID(&IID_IUnknown
, iid
) || IsEqualIID(&IID_IWICBitmapDecoder
, iid
))
168 return E_NOINTERFACE
;
171 IUnknown_AddRef((IUnknown
*)*ppv
);
175 static ULONG WINAPI
JpegDecoder_AddRef(IWICBitmapDecoder
*iface
)
177 JpegDecoder
*This
= (JpegDecoder
*)iface
;
178 ULONG ref
= InterlockedIncrement(&This
->ref
);
180 TRACE("(%p) refcount=%u\n", iface
, ref
);
185 static ULONG WINAPI
JpegDecoder_Release(IWICBitmapDecoder
*iface
)
187 JpegDecoder
*This
= (JpegDecoder
*)iface
;
188 ULONG ref
= InterlockedDecrement(&This
->ref
);
190 TRACE("(%p) refcount=%u\n", iface
, ref
);
194 This
->lock
.DebugInfo
->Spare
[0] = 0;
195 DeleteCriticalSection(&This
->lock
);
196 if (This
->cinfo_initialized
) pjpeg_destroy_decompress(&This
->cinfo
);
197 if (This
->stream
) IStream_Release(This
->stream
);
198 HeapFree(GetProcessHeap(), 0, This
->image_data
);
199 HeapFree(GetProcessHeap(), 0, This
);
205 static HRESULT WINAPI
JpegDecoder_QueryCapability(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
206 DWORD
*pdwCapability
)
208 FIXME("(%p,%p,%p): stub\n", iface
, pIStream
, pdwCapability
);
212 static void source_mgr_init_source(j_decompress_ptr cinfo
)
216 static jpeg_boolean
source_mgr_fill_input_buffer(j_decompress_ptr cinfo
)
218 JpegDecoder
*This
= decoder_from_decompress(cinfo
);
222 hr
= IStream_Read(This
->stream
, This
->source_buffer
, 1024, &bytesread
);
224 if (hr
!= S_OK
|| bytesread
== 0)
230 This
->source_mgr
.next_input_byte
= This
->source_buffer
;
231 This
->source_mgr
.bytes_in_buffer
= bytesread
;
236 static void source_mgr_skip_input_data(j_decompress_ptr cinfo
, long num_bytes
)
238 JpegDecoder
*This
= decoder_from_decompress(cinfo
);
241 if (num_bytes
> This
->source_mgr
.bytes_in_buffer
)
243 seek
.QuadPart
= num_bytes
- This
->source_mgr
.bytes_in_buffer
;
244 IStream_Seek(This
->stream
, seek
, STREAM_SEEK_CUR
, NULL
);
245 This
->source_mgr
.bytes_in_buffer
= 0;
247 else if (num_bytes
> 0)
249 This
->source_mgr
.next_input_byte
+= num_bytes
;
250 This
->source_mgr
.bytes_in_buffer
-= num_bytes
;
254 static void source_mgr_term_source(j_decompress_ptr cinfo
)
258 static HRESULT WINAPI
JpegDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
259 WICDecodeOptions cacheOptions
)
261 JpegDecoder
*This
= (JpegDecoder
*)iface
;
265 TRACE("(%p,%p,%u)\n", iface
, pIStream
, cacheOptions
);
267 EnterCriticalSection(&This
->lock
);
269 if (This
->cinfo_initialized
)
271 LeaveCriticalSection(&This
->lock
);
272 return WINCODEC_ERR_WRONGSTATE
;
275 pjpeg_std_error(&This
->jerr
);
277 This
->jerr
.error_exit
= error_exit_fn
;
278 This
->jerr
.emit_message
= emit_message_fn
;
280 This
->cinfo
.err
= &This
->jerr
;
282 This
->cinfo
.client_data
= &jmpbuf
;
286 LeaveCriticalSection(&This
->lock
);
290 pjpeg_CreateDecompress(&This
->cinfo
, JPEG_LIB_VERSION
, sizeof(struct jpeg_decompress_struct
));
292 This
->cinfo_initialized
= TRUE
;
294 This
->stream
= pIStream
;
295 IStream_AddRef(pIStream
);
298 IStream_Seek(This
->stream
, seek
, STREAM_SEEK_SET
, NULL
);
300 This
->source_mgr
.bytes_in_buffer
= 0;
301 This
->source_mgr
.init_source
= source_mgr_init_source
;
302 This
->source_mgr
.fill_input_buffer
= source_mgr_fill_input_buffer
;
303 This
->source_mgr
.skip_input_data
= source_mgr_skip_input_data
;
304 This
->source_mgr
.resync_to_restart
= pjpeg_resync_to_restart
;
305 This
->source_mgr
.term_source
= source_mgr_term_source
;
307 This
->cinfo
.src
= &This
->source_mgr
;
309 ret
= pjpeg_read_header(&This
->cinfo
, TRUE
);
311 if (ret
!= JPEG_HEADER_OK
) {
312 WARN("Jpeg image in stream has bad format, read header returned %d.\n",ret
);
313 LeaveCriticalSection(&This
->lock
);
317 switch (This
->cinfo
.jpeg_color_space
)
320 This
->cinfo
.out_color_space
= JCS_GRAYSCALE
;
324 This
->cinfo
.out_color_space
= JCS_RGB
;
328 This
->cinfo
.out_color_space
= JCS_CMYK
;
331 ERR("Unknown JPEG color space %i\n", This
->cinfo
.jpeg_color_space
);
332 LeaveCriticalSection(&This
->lock
);
336 if (!pjpeg_start_decompress(&This
->cinfo
))
338 ERR("jpeg_start_decompress failed\n");
339 LeaveCriticalSection(&This
->lock
);
343 This
->initialized
= TRUE
;
345 LeaveCriticalSection(&This
->lock
);
350 static HRESULT WINAPI
JpegDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
351 GUID
*pguidContainerFormat
)
353 memcpy(pguidContainerFormat
, &GUID_ContainerFormatJpeg
, sizeof(GUID
));
357 static HRESULT WINAPI
JpegDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
358 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
360 FIXME("(%p,%p): stub\n", iface
, ppIDecoderInfo
);
364 static HRESULT WINAPI
JpegDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
365 IWICPalette
*pIPalette
)
367 TRACE("(%p,%p)\n", iface
, pIPalette
);
369 return WINCODEC_ERR_PALETTEUNAVAILABLE
;
372 static HRESULT WINAPI
JpegDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
373 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
375 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
379 static HRESULT WINAPI
JpegDecoder_GetPreview(IWICBitmapDecoder
*iface
,
380 IWICBitmapSource
**ppIBitmapSource
)
382 FIXME("(%p,%p): stub\n", iface
, ppIBitmapSource
);
383 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
386 static HRESULT WINAPI
JpegDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
387 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
389 FIXME("(%p,%u,%p,%p): stub\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
390 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
393 static HRESULT WINAPI
JpegDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
394 IWICBitmapSource
**ppIThumbnail
)
396 FIXME("(%p,%p): stub\n", iface
, ppIThumbnail
);
397 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
400 static HRESULT WINAPI
JpegDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
407 static HRESULT WINAPI
JpegDecoder_GetFrame(IWICBitmapDecoder
*iface
,
408 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
410 JpegDecoder
*This
= (JpegDecoder
*)iface
;
411 TRACE("(%p,%u,%p)\n", iface
, index
, ppIBitmapFrame
);
413 if (!This
->initialized
) return WINCODEC_ERR_NOTINITIALIZED
;
415 if (index
!= 0) return E_INVALIDARG
;
417 IWICBitmapDecoder_AddRef(iface
);
418 *ppIBitmapFrame
= (IWICBitmapFrameDecode
*)&This
->lpFrameVtbl
;
423 static const IWICBitmapDecoderVtbl JpegDecoder_Vtbl
= {
424 JpegDecoder_QueryInterface
,
427 JpegDecoder_QueryCapability
,
428 JpegDecoder_Initialize
,
429 JpegDecoder_GetContainerFormat
,
430 JpegDecoder_GetDecoderInfo
,
431 JpegDecoder_CopyPalette
,
432 JpegDecoder_GetMetadataQueryReader
,
433 JpegDecoder_GetPreview
,
434 JpegDecoder_GetColorContexts
,
435 JpegDecoder_GetThumbnail
,
436 JpegDecoder_GetFrameCount
,
440 static HRESULT WINAPI
JpegDecoder_Frame_QueryInterface(IWICBitmapFrameDecode
*iface
, REFIID iid
,
443 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
445 if (!ppv
) return E_INVALIDARG
;
447 if (IsEqualIID(&IID_IUnknown
, iid
) ||
448 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
449 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
456 return E_NOINTERFACE
;
459 IUnknown_AddRef((IUnknown
*)*ppv
);
463 static ULONG WINAPI
JpegDecoder_Frame_AddRef(IWICBitmapFrameDecode
*iface
)
465 JpegDecoder
*This
= decoder_from_frame(iface
);
466 return IUnknown_AddRef((IUnknown
*)This
);
469 static ULONG WINAPI
JpegDecoder_Frame_Release(IWICBitmapFrameDecode
*iface
)
471 JpegDecoder
*This
= decoder_from_frame(iface
);
472 return IUnknown_Release((IUnknown
*)This
);
475 static HRESULT WINAPI
JpegDecoder_Frame_GetSize(IWICBitmapFrameDecode
*iface
,
476 UINT
*puiWidth
, UINT
*puiHeight
)
478 JpegDecoder
*This
= decoder_from_frame(iface
);
479 *puiWidth
= This
->cinfo
.output_width
;
480 *puiHeight
= This
->cinfo
.output_height
;
481 TRACE("(%p)->(%u,%u)\n", iface
, *puiWidth
, *puiHeight
);
485 static HRESULT WINAPI
JpegDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
486 WICPixelFormatGUID
*pPixelFormat
)
488 JpegDecoder
*This
= decoder_from_frame(iface
);
489 TRACE("(%p,%p)\n", iface
, pPixelFormat
);
490 if (This
->cinfo
.out_color_space
== JCS_RGB
)
491 memcpy(pPixelFormat
, &GUID_WICPixelFormat24bppBGR
, sizeof(GUID
));
492 else if (This
->cinfo
.out_color_space
== JCS_CMYK
)
493 memcpy(pPixelFormat
, &GUID_WICPixelFormat32bppCMYK
, sizeof(GUID
));
494 else /* This->cinfo.out_color_space == JCS_GRAYSCALE */
495 memcpy(pPixelFormat
, &GUID_WICPixelFormat8bppGray
, sizeof(GUID
));
499 static HRESULT WINAPI
JpegDecoder_Frame_GetResolution(IWICBitmapFrameDecode
*iface
,
500 double *pDpiX
, double *pDpiY
)
502 FIXME("(%p,%p,%p): stub\n", iface
, pDpiX
, pDpiY
);
506 static HRESULT WINAPI
JpegDecoder_Frame_CopyPalette(IWICBitmapFrameDecode
*iface
,
507 IWICPalette
*pIPalette
)
509 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
513 static HRESULT WINAPI
JpegDecoder_Frame_CopyPixels(IWICBitmapFrameDecode
*iface
,
514 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
516 JpegDecoder
*This
= decoder_from_frame(iface
);
523 TRACE("(%p,%p,%u,%u,%p)\n", iface
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
529 rect
.Width
= This
->cinfo
.output_width
;
530 rect
.Height
= This
->cinfo
.output_height
;
535 if (prc
->X
< 0 || prc
->Y
< 0 || prc
->X
+prc
->Width
> This
->cinfo
.output_width
||
536 prc
->Y
+prc
->Height
> This
->cinfo
.output_height
)
540 if (This
->cinfo
.out_color_space
== JCS_GRAYSCALE
) bpp
= 8;
541 else if (This
->cinfo
.out_color_space
== JCS_CMYK
) bpp
= 32;
544 stride
= bpp
* This
->cinfo
.output_width
;
545 data_size
= stride
* This
->cinfo
.output_height
;
547 max_row_needed
= prc
->Y
+ prc
->Height
;
548 if (max_row_needed
> This
->cinfo
.output_height
) return E_INVALIDARG
;
550 EnterCriticalSection(&This
->lock
);
552 if (!This
->image_data
)
554 This
->image_data
= HeapAlloc(GetProcessHeap(), 0, data_size
);
555 if (!This
->image_data
)
557 LeaveCriticalSection(&This
->lock
);
558 return E_OUTOFMEMORY
;
562 This
->cinfo
.client_data
= &jmpbuf
;
566 LeaveCriticalSection(&This
->lock
);
570 while (max_row_needed
> This
->cinfo
.output_scanline
)
572 UINT first_scanline
= This
->cinfo
.output_scanline
;
574 JSAMPROW out_rows
[4];
578 max_rows
= min(This
->cinfo
.output_height
-first_scanline
, 4);
579 for (i
=0; i
<max_rows
; i
++)
580 out_rows
[i
] = This
->image_data
+ stride
* (first_scanline
+i
);
582 ret
= pjpeg_read_scanlines(&This
->cinfo
, out_rows
, max_rows
);
586 ERR("read_scanlines failed\n");
587 LeaveCriticalSection(&This
->lock
);
593 /* libjpeg gives us RGB data and we want BGR, so byteswap the data */
594 for (i
=first_scanline
; i
<This
->cinfo
.output_scanline
; i
++)
596 BYTE
*pixel
= This
->image_data
+ stride
* i
;
597 for (j
=0; j
<This
->cinfo
.output_width
; j
++)
608 if (This
->cinfo
.out_color_space
== JCS_CMYK
&& This
->cinfo
.saw_Adobe_marker
)
609 /* Adobe JPEG's have inverted CMYK data. */
610 for (i
=0; i
<data_size
; i
++)
611 This
->image_data
[i
] ^= 0xff;
614 LeaveCriticalSection(&This
->lock
);
616 return copy_pixels(bpp
, This
->image_data
,
617 This
->cinfo
.output_width
, This
->cinfo
.output_height
, stride
,
618 prc
, cbStride
, cbBufferSize
, pbBuffer
);
621 static HRESULT WINAPI
JpegDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
622 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
624 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
625 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
628 static HRESULT WINAPI
JpegDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode
*iface
,
629 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
631 FIXME("(%p,%u,%p,%p): stub\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
632 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
635 static HRESULT WINAPI
JpegDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode
*iface
,
636 IWICBitmapSource
**ppIThumbnail
)
638 FIXME("(%p,%p): stub\n", iface
, ppIThumbnail
);
639 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
642 static const IWICBitmapFrameDecodeVtbl JpegDecoder_Frame_Vtbl
= {
643 JpegDecoder_Frame_QueryInterface
,
644 JpegDecoder_Frame_AddRef
,
645 JpegDecoder_Frame_Release
,
646 JpegDecoder_Frame_GetSize
,
647 JpegDecoder_Frame_GetPixelFormat
,
648 JpegDecoder_Frame_GetResolution
,
649 JpegDecoder_Frame_CopyPalette
,
650 JpegDecoder_Frame_CopyPixels
,
651 JpegDecoder_Frame_GetMetadataQueryReader
,
652 JpegDecoder_Frame_GetColorContexts
,
653 JpegDecoder_Frame_GetThumbnail
656 HRESULT
JpegDecoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
661 TRACE("(%p,%s,%p)\n", pUnkOuter
, debugstr_guid(iid
), ppv
);
663 if (!libjpeg_handle
&& !load_libjpeg())
665 ERR("Failed reading JPEG because unable to find %s\n", SONAME_LIBJPEG
);
671 if (pUnkOuter
) return CLASS_E_NOAGGREGATION
;
673 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(JpegDecoder
));
674 if (!This
) return E_OUTOFMEMORY
;
676 This
->lpVtbl
= &JpegDecoder_Vtbl
;
677 This
->lpFrameVtbl
= &JpegDecoder_Frame_Vtbl
;
679 This
->initialized
= FALSE
;
680 This
->cinfo_initialized
= FALSE
;
682 This
->image_data
= NULL
;
683 InitializeCriticalSection(&This
->lock
);
684 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": JpegDecoder.lock");
686 ret
= IUnknown_QueryInterface((IUnknown
*)This
, iid
, ppv
);
687 IUnknown_Release((IUnknown
*)This
);
692 #else /* !defined(SONAME_LIBJPEG) */
694 HRESULT
JpegDecoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
696 ERR("Trying to load JPEG picture, but JPEG support is not compiled in.\n");