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"
30 /* This is a hack, so jpeglib.h does not redefine INT32 and the like*/
32 #define UINT8 JPEG_UINT8
33 #define UINT16 JPEG_UINT16
34 #define boolean jpeg_boolean
38 #define HAVE_STDLIB_H 1
51 #include "wincodecs_private.h"
53 #include "wine/debug.h"
54 #include "wine/library.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs
);
60 static void *libjpeg_handle
;
62 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
63 MAKE_FUNCPTR(jpeg_CreateDecompress
);
64 MAKE_FUNCPTR(jpeg_destroy_decompress
);
65 MAKE_FUNCPTR(jpeg_read_header
);
66 MAKE_FUNCPTR(jpeg_read_scanlines
);
67 MAKE_FUNCPTR(jpeg_resync_to_restart
);
68 MAKE_FUNCPTR(jpeg_start_decompress
);
69 MAKE_FUNCPTR(jpeg_std_error
);
72 static void *load_libjpeg(void)
74 if((libjpeg_handle
= wine_dlopen(SONAME_LIBJPEG
, RTLD_NOW
, NULL
, 0)) != NULL
) {
76 #define LOAD_FUNCPTR(f) \
77 if((p##f = wine_dlsym(libjpeg_handle, #f, NULL, 0)) == NULL) { \
78 libjpeg_handle = NULL; \
82 LOAD_FUNCPTR(jpeg_CreateDecompress
);
83 LOAD_FUNCPTR(jpeg_destroy_decompress
);
84 LOAD_FUNCPTR(jpeg_read_header
);
85 LOAD_FUNCPTR(jpeg_read_scanlines
);
86 LOAD_FUNCPTR(jpeg_resync_to_restart
);
87 LOAD_FUNCPTR(jpeg_start_decompress
);
88 LOAD_FUNCPTR(jpeg_std_error
);
91 return libjpeg_handle
;
95 const IWICBitmapDecoderVtbl
*lpVtbl
;
96 const IWICBitmapFrameDecodeVtbl
*lpFrameVtbl
;
99 BOOL cinfo_initialized
;
101 struct jpeg_decompress_struct cinfo
;
102 struct jpeg_error_mgr jerr
;
103 struct jpeg_source_mgr source_mgr
;
104 BYTE source_buffer
[1024];
106 CRITICAL_SECTION lock
;
109 static inline JpegDecoder
*decoder_from_decompress(j_decompress_ptr decompress
)
111 return CONTAINING_RECORD(decompress
, JpegDecoder
, cinfo
);
114 static inline JpegDecoder
*decoder_from_frame(IWICBitmapFrameDecode
*iface
)
116 return CONTAINING_RECORD(iface
, JpegDecoder
, lpFrameVtbl
);
119 static HRESULT WINAPI
JpegDecoder_QueryInterface(IWICBitmapDecoder
*iface
, REFIID iid
,
122 JpegDecoder
*This
= (JpegDecoder
*)iface
;
123 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
125 if (!ppv
) return E_INVALIDARG
;
127 if (IsEqualIID(&IID_IUnknown
, iid
) || IsEqualIID(&IID_IWICBitmapDecoder
, iid
))
134 return E_NOINTERFACE
;
137 IUnknown_AddRef((IUnknown
*)*ppv
);
141 static ULONG WINAPI
JpegDecoder_AddRef(IWICBitmapDecoder
*iface
)
143 JpegDecoder
*This
= (JpegDecoder
*)iface
;
144 ULONG ref
= InterlockedIncrement(&This
->ref
);
146 TRACE("(%p) refcount=%u\n", iface
, ref
);
151 static ULONG WINAPI
JpegDecoder_Release(IWICBitmapDecoder
*iface
)
153 JpegDecoder
*This
= (JpegDecoder
*)iface
;
154 ULONG ref
= InterlockedDecrement(&This
->ref
);
156 TRACE("(%p) refcount=%u\n", iface
, ref
);
160 This
->lock
.DebugInfo
->Spare
[0] = 0;
161 DeleteCriticalSection(&This
->lock
);
162 if (This
->cinfo_initialized
) pjpeg_destroy_decompress(&This
->cinfo
);
163 if (This
->stream
) IStream_Release(This
->stream
);
164 HeapFree(GetProcessHeap(), 0, This
->image_data
);
165 HeapFree(GetProcessHeap(), 0, This
);
171 static HRESULT WINAPI
JpegDecoder_QueryCapability(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
172 DWORD
*pdwCapability
)
174 FIXME("(%p,%p,%p): stub\n", iface
, pIStream
, pdwCapability
);
178 static void source_mgr_init_source(j_decompress_ptr cinfo
)
182 static jpeg_boolean
source_mgr_fill_input_buffer(j_decompress_ptr cinfo
)
184 JpegDecoder
*This
= decoder_from_decompress(cinfo
);
188 hr
= IStream_Read(This
->stream
, This
->source_buffer
, 1024, &bytesread
);
190 if (hr
!= S_OK
|| bytesread
== 0)
196 This
->source_mgr
.next_input_byte
= This
->source_buffer
;
197 This
->source_mgr
.bytes_in_buffer
= bytesread
;
202 static void source_mgr_skip_input_data(j_decompress_ptr cinfo
, long num_bytes
)
204 JpegDecoder
*This
= decoder_from_decompress(cinfo
);
207 if (num_bytes
> This
->source_mgr
.bytes_in_buffer
)
209 seek
.QuadPart
= num_bytes
- This
->source_mgr
.bytes_in_buffer
;
210 IStream_Seek(This
->stream
, seek
, STREAM_SEEK_CUR
, NULL
);
211 This
->source_mgr
.bytes_in_buffer
= 0;
213 else if (num_bytes
> 0)
215 This
->source_mgr
.next_input_byte
+= num_bytes
;
216 This
->source_mgr
.bytes_in_buffer
-= num_bytes
;
220 static void source_mgr_term_source(j_decompress_ptr cinfo
)
224 static HRESULT WINAPI
JpegDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
225 WICDecodeOptions cacheOptions
)
227 JpegDecoder
*This
= (JpegDecoder
*)iface
;
229 TRACE("(%p,%p,%u)\n", iface
, pIStream
, cacheOptions
);
231 EnterCriticalSection(&This
->lock
);
233 if (This
->cinfo_initialized
)
235 LeaveCriticalSection(&This
->lock
);
236 return WINCODEC_ERR_WRONGSTATE
;
239 This
->cinfo
.err
= pjpeg_std_error(&This
->jerr
);
241 pjpeg_CreateDecompress(&This
->cinfo
, JPEG_LIB_VERSION
, sizeof(struct jpeg_decompress_struct
));
243 This
->cinfo_initialized
= TRUE
;
245 This
->stream
= pIStream
;
246 IStream_AddRef(pIStream
);
248 This
->source_mgr
.bytes_in_buffer
= 0;
249 This
->source_mgr
.init_source
= source_mgr_init_source
;
250 This
->source_mgr
.fill_input_buffer
= source_mgr_fill_input_buffer
;
251 This
->source_mgr
.skip_input_data
= source_mgr_skip_input_data
;
252 This
->source_mgr
.resync_to_restart
= pjpeg_resync_to_restart
;
253 This
->source_mgr
.term_source
= source_mgr_term_source
;
255 This
->cinfo
.src
= &This
->source_mgr
;
257 ret
= pjpeg_read_header(&This
->cinfo
, TRUE
);
259 if (ret
!= JPEG_HEADER_OK
) {
260 WARN("Jpeg image in stream has bad format, read header returned %d.\n",ret
);
261 LeaveCriticalSection(&This
->lock
);
265 if (This
->cinfo
.jpeg_color_space
== JCS_GRAYSCALE
)
266 This
->cinfo
.out_color_space
= JCS_GRAYSCALE
;
268 This
->cinfo
.out_color_space
= JCS_RGB
;
270 if (!pjpeg_start_decompress(&This
->cinfo
))
272 ERR("jpeg_start_decompress failed\n");
273 LeaveCriticalSection(&This
->lock
);
277 This
->initialized
= TRUE
;
279 LeaveCriticalSection(&This
->lock
);
284 static HRESULT WINAPI
JpegDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
285 GUID
*pguidContainerFormat
)
287 memcpy(pguidContainerFormat
, &GUID_ContainerFormatJpeg
, sizeof(GUID
));
291 static HRESULT WINAPI
JpegDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
292 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
294 FIXME("(%p,%p): stub\n", iface
, ppIDecoderInfo
);
298 static HRESULT WINAPI
JpegDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
299 IWICPalette
*pIPalette
)
301 TRACE("(%p,%p)\n", iface
, pIPalette
);
303 return WINCODEC_ERR_PALETTEUNAVAILABLE
;
306 static HRESULT WINAPI
JpegDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
307 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
309 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
313 static HRESULT WINAPI
JpegDecoder_GetPreview(IWICBitmapDecoder
*iface
,
314 IWICBitmapSource
**ppIBitmapSource
)
316 FIXME("(%p,%p): stub\n", iface
, ppIBitmapSource
);
317 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
320 static HRESULT WINAPI
JpegDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
321 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
323 FIXME("(%p,%u,%p,%p): stub\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
324 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
327 static HRESULT WINAPI
JpegDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
328 IWICBitmapSource
**ppIThumbnail
)
330 FIXME("(%p,%p): stub\n", iface
, ppIThumbnail
);
331 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
334 static HRESULT WINAPI
JpegDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
341 static HRESULT WINAPI
JpegDecoder_GetFrame(IWICBitmapDecoder
*iface
,
342 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
344 JpegDecoder
*This
= (JpegDecoder
*)iface
;
345 TRACE("(%p,%u,%p)\n", iface
, index
, ppIBitmapFrame
);
347 if (!This
->initialized
) return WINCODEC_ERR_NOTINITIALIZED
;
349 if (index
!= 0) return E_INVALIDARG
;
351 IWICBitmapDecoder_AddRef(iface
);
352 *ppIBitmapFrame
= (IWICBitmapFrameDecode
*)&This
->lpFrameVtbl
;
357 static const IWICBitmapDecoderVtbl JpegDecoder_Vtbl
= {
358 JpegDecoder_QueryInterface
,
361 JpegDecoder_QueryCapability
,
362 JpegDecoder_Initialize
,
363 JpegDecoder_GetContainerFormat
,
364 JpegDecoder_GetDecoderInfo
,
365 JpegDecoder_CopyPalette
,
366 JpegDecoder_GetMetadataQueryReader
,
367 JpegDecoder_GetPreview
,
368 JpegDecoder_GetColorContexts
,
369 JpegDecoder_GetThumbnail
,
370 JpegDecoder_GetFrameCount
,
374 static HRESULT WINAPI
JpegDecoder_Frame_QueryInterface(IWICBitmapFrameDecode
*iface
, REFIID iid
,
377 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
379 if (!ppv
) return E_INVALIDARG
;
381 if (IsEqualIID(&IID_IUnknown
, iid
) ||
382 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
383 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
390 return E_NOINTERFACE
;
393 IUnknown_AddRef((IUnknown
*)*ppv
);
397 static ULONG WINAPI
JpegDecoder_Frame_AddRef(IWICBitmapFrameDecode
*iface
)
399 JpegDecoder
*This
= decoder_from_frame(iface
);
400 return IUnknown_AddRef((IUnknown
*)This
);
403 static ULONG WINAPI
JpegDecoder_Frame_Release(IWICBitmapFrameDecode
*iface
)
405 JpegDecoder
*This
= decoder_from_frame(iface
);
406 return IUnknown_Release((IUnknown
*)This
);
409 static HRESULT WINAPI
JpegDecoder_Frame_GetSize(IWICBitmapFrameDecode
*iface
,
410 UINT
*puiWidth
, UINT
*puiHeight
)
412 JpegDecoder
*This
= decoder_from_frame(iface
);
413 *puiWidth
= This
->cinfo
.output_width
;
414 *puiHeight
= This
->cinfo
.output_height
;
415 TRACE("(%p)->(%u,%u)\n", iface
, *puiWidth
, *puiHeight
);
419 static HRESULT WINAPI
JpegDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
420 WICPixelFormatGUID
*pPixelFormat
)
422 JpegDecoder
*This
= decoder_from_frame(iface
);
423 TRACE("(%p,%p)\n", iface
, pPixelFormat
);
424 if (This
->cinfo
.out_color_space
== JCS_RGB
)
425 memcpy(pPixelFormat
, &GUID_WICPixelFormat24bppBGR
, sizeof(GUID
));
426 else /* This->cinfo.out_color_space == JCS_GRAYSCALE */
427 memcpy(pPixelFormat
, &GUID_WICPixelFormat8bppGray
, sizeof(GUID
));
431 static HRESULT WINAPI
JpegDecoder_Frame_GetResolution(IWICBitmapFrameDecode
*iface
,
432 double *pDpiX
, double *pDpiY
)
434 FIXME("(%p,%p,%p): stub\n", iface
, pDpiX
, pDpiY
);
438 static HRESULT WINAPI
JpegDecoder_Frame_CopyPalette(IWICBitmapFrameDecode
*iface
,
439 IWICPalette
*pIPalette
)
441 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
445 static HRESULT WINAPI
JpegDecoder_Frame_CopyPixels(IWICBitmapFrameDecode
*iface
,
446 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
448 JpegDecoder
*This
= decoder_from_frame(iface
);
453 TRACE("(%p,%p,%u,%u,%p)\n", iface
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
455 if (This
->cinfo
.out_color_space
== JCS_GRAYSCALE
) bpp
= 8;
458 stride
= bpp
* This
->cinfo
.output_width
;
459 data_size
= stride
* This
->cinfo
.output_height
;
461 max_row_needed
= prc
->Y
+ prc
->Height
;
462 if (max_row_needed
> This
->cinfo
.output_height
) return E_INVALIDARG
;
464 EnterCriticalSection(&This
->lock
);
466 if (!This
->image_data
)
468 This
->image_data
= HeapAlloc(GetProcessHeap(), 0, data_size
);
469 if (!This
->image_data
)
471 LeaveCriticalSection(&This
->lock
);
472 return E_OUTOFMEMORY
;
476 while (max_row_needed
> This
->cinfo
.output_scanline
)
478 UINT first_scanline
= This
->cinfo
.output_scanline
;
480 JSAMPROW out_rows
[4];
484 max_rows
= min(This
->cinfo
.output_height
-first_scanline
, 4);
485 for (i
=0; i
<max_rows
; i
++)
486 out_rows
[i
] = This
->image_data
+ stride
* (first_scanline
+i
);
488 ret
= pjpeg_read_scanlines(&This
->cinfo
, out_rows
, max_rows
);
492 ERR("read_scanlines failed\n");
493 LeaveCriticalSection(&This
->lock
);
499 /* libjpeg gives us RGB data and we want BGR, so byteswap the data */
500 for (i
=first_scanline
; i
<This
->cinfo
.output_scanline
; i
++)
502 BYTE
*pixel
= This
->image_data
+ stride
* i
;
503 for (j
=0; j
<This
->cinfo
.output_width
; j
++)
515 LeaveCriticalSection(&This
->lock
);
517 return copy_pixels(bpp
, This
->image_data
,
518 This
->cinfo
.output_width
, This
->cinfo
.output_height
, stride
,
519 prc
, cbStride
, cbBufferSize
, pbBuffer
);
522 static HRESULT WINAPI
JpegDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
523 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
525 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
526 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
529 static HRESULT WINAPI
JpegDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode
*iface
,
530 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
532 FIXME("(%p,%u,%p,%p): stub\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
533 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
536 static HRESULT WINAPI
JpegDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode
*iface
,
537 IWICBitmapSource
**ppIThumbnail
)
539 FIXME("(%p,%p): stub\n", iface
, ppIThumbnail
);
540 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
543 static const IWICBitmapFrameDecodeVtbl JpegDecoder_Frame_Vtbl
= {
544 JpegDecoder_Frame_QueryInterface
,
545 JpegDecoder_Frame_AddRef
,
546 JpegDecoder_Frame_Release
,
547 JpegDecoder_Frame_GetSize
,
548 JpegDecoder_Frame_GetPixelFormat
,
549 JpegDecoder_Frame_GetResolution
,
550 JpegDecoder_Frame_CopyPalette
,
551 JpegDecoder_Frame_CopyPixels
,
552 JpegDecoder_Frame_GetMetadataQueryReader
,
553 JpegDecoder_Frame_GetColorContexts
,
554 JpegDecoder_Frame_GetThumbnail
557 HRESULT
JpegDecoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
562 TRACE("(%p,%s,%p)\n", pUnkOuter
, debugstr_guid(iid
), ppv
);
564 if (!libjpeg_handle
&& !load_libjpeg())
566 ERR("Failed reading JPEG because unable to find %s\n", SONAME_LIBJPEG
);
572 if (pUnkOuter
) return CLASS_E_NOAGGREGATION
;
574 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(JpegDecoder
));
575 if (!This
) return E_OUTOFMEMORY
;
577 This
->lpVtbl
= &JpegDecoder_Vtbl
;
578 This
->lpFrameVtbl
= &JpegDecoder_Frame_Vtbl
;
580 This
->initialized
= FALSE
;
581 This
->cinfo_initialized
= FALSE
;
583 This
->image_data
= NULL
;
584 InitializeCriticalSection(&This
->lock
);
585 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": JpegDecoder.lock");
587 ret
= IUnknown_QueryInterface((IUnknown
*)This
, iid
, ppv
);
588 IUnknown_Release((IUnknown
*)This
);
593 #else /* !defined(SONAME_LIBJPEG) */
595 HRESULT
JpegDecoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
597 ERR("Trying to load JPEG picture, but JPEG support is not compiled in.\n");