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
51 #include "wincodecs_private.h"
53 #include "wine/debug.h"
54 #include "wine/library.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs
);
59 WINE_DECLARE_DEBUG_CHANNEL(jpeg
);
61 static void *libjpeg_handle
;
63 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
64 MAKE_FUNCPTR(jpeg_CreateCompress
);
65 MAKE_FUNCPTR(jpeg_CreateDecompress
);
66 MAKE_FUNCPTR(jpeg_destroy_compress
);
67 MAKE_FUNCPTR(jpeg_destroy_decompress
);
68 MAKE_FUNCPTR(jpeg_finish_compress
);
69 MAKE_FUNCPTR(jpeg_read_header
);
70 MAKE_FUNCPTR(jpeg_read_scanlines
);
71 MAKE_FUNCPTR(jpeg_resync_to_restart
);
72 MAKE_FUNCPTR(jpeg_set_defaults
);
73 MAKE_FUNCPTR(jpeg_start_compress
);
74 MAKE_FUNCPTR(jpeg_start_decompress
);
75 MAKE_FUNCPTR(jpeg_std_error
);
76 MAKE_FUNCPTR(jpeg_write_scanlines
);
79 static void *load_libjpeg(void)
81 if((libjpeg_handle
= wine_dlopen(SONAME_LIBJPEG
, RTLD_NOW
, NULL
, 0)) != NULL
) {
83 #define LOAD_FUNCPTR(f) \
84 if((p##f = wine_dlsym(libjpeg_handle, #f, NULL, 0)) == NULL) { \
85 libjpeg_handle = NULL; \
89 LOAD_FUNCPTR(jpeg_CreateCompress
);
90 LOAD_FUNCPTR(jpeg_CreateDecompress
);
91 LOAD_FUNCPTR(jpeg_destroy_compress
);
92 LOAD_FUNCPTR(jpeg_destroy_decompress
);
93 LOAD_FUNCPTR(jpeg_finish_compress
);
94 LOAD_FUNCPTR(jpeg_read_header
);
95 LOAD_FUNCPTR(jpeg_read_scanlines
);
96 LOAD_FUNCPTR(jpeg_resync_to_restart
);
97 LOAD_FUNCPTR(jpeg_set_defaults
);
98 LOAD_FUNCPTR(jpeg_start_compress
);
99 LOAD_FUNCPTR(jpeg_start_decompress
);
100 LOAD_FUNCPTR(jpeg_std_error
);
101 LOAD_FUNCPTR(jpeg_write_scanlines
);
104 return libjpeg_handle
;
107 static void error_exit_fn(j_common_ptr cinfo
)
109 char message
[JMSG_LENGTH_MAX
];
112 cinfo
->err
->format_message(cinfo
, message
);
113 ERR_(jpeg
)("%s\n", message
);
115 longjmp(*(jmp_buf*)cinfo
->client_data
, 1);
118 static void emit_message_fn(j_common_ptr cinfo
, int msg_level
)
120 char message
[JMSG_LENGTH_MAX
];
122 if (msg_level
< 0 && ERR_ON(jpeg
))
124 cinfo
->err
->format_message(cinfo
, message
);
125 ERR_(jpeg
)("%s\n", message
);
127 else if (msg_level
== 0 && WARN_ON(jpeg
))
129 cinfo
->err
->format_message(cinfo
, message
);
130 WARN_(jpeg
)("%s\n", message
);
132 else if (msg_level
> 0 && TRACE_ON(jpeg
))
134 cinfo
->err
->format_message(cinfo
, message
);
135 TRACE_(jpeg
)("%s\n", message
);
140 IWICBitmapDecoder IWICBitmapDecoder_iface
;
141 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface
;
142 IWICMetadataBlockReader IWICMetadataBlockReader_iface
;
145 BOOL cinfo_initialized
;
147 struct jpeg_decompress_struct cinfo
;
148 struct jpeg_error_mgr jerr
;
149 struct jpeg_source_mgr source_mgr
;
150 BYTE source_buffer
[1024];
152 CRITICAL_SECTION lock
;
155 static inline JpegDecoder
*impl_from_IWICBitmapDecoder(IWICBitmapDecoder
*iface
)
157 return CONTAINING_RECORD(iface
, JpegDecoder
, IWICBitmapDecoder_iface
);
160 static inline JpegDecoder
*impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode
*iface
)
162 return CONTAINING_RECORD(iface
, JpegDecoder
, IWICBitmapFrameDecode_iface
);
165 static inline JpegDecoder
*decoder_from_decompress(j_decompress_ptr decompress
)
167 return CONTAINING_RECORD(decompress
, JpegDecoder
, cinfo
);
170 static inline JpegDecoder
*impl_from_IWICMetadataBlockReader(IWICMetadataBlockReader
*iface
)
172 return CONTAINING_RECORD(iface
, JpegDecoder
, IWICMetadataBlockReader_iface
);
175 static HRESULT WINAPI
JpegDecoder_QueryInterface(IWICBitmapDecoder
*iface
, REFIID iid
,
178 JpegDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
179 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
181 if (!ppv
) return E_INVALIDARG
;
183 if (IsEqualIID(&IID_IUnknown
, iid
) ||
184 IsEqualIID(&IID_IWICBitmapDecoder
, iid
))
186 *ppv
= &This
->IWICBitmapDecoder_iface
;
191 return E_NOINTERFACE
;
194 IUnknown_AddRef((IUnknown
*)*ppv
);
198 static ULONG WINAPI
JpegDecoder_AddRef(IWICBitmapDecoder
*iface
)
200 JpegDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
201 ULONG ref
= InterlockedIncrement(&This
->ref
);
203 TRACE("(%p) refcount=%u\n", iface
, ref
);
208 static ULONG WINAPI
JpegDecoder_Release(IWICBitmapDecoder
*iface
)
210 JpegDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
211 ULONG ref
= InterlockedDecrement(&This
->ref
);
213 TRACE("(%p) refcount=%u\n", iface
, ref
);
217 This
->lock
.DebugInfo
->Spare
[0] = 0;
218 DeleteCriticalSection(&This
->lock
);
219 if (This
->cinfo_initialized
) pjpeg_destroy_decompress(&This
->cinfo
);
220 if (This
->stream
) IStream_Release(This
->stream
);
221 HeapFree(GetProcessHeap(), 0, This
->image_data
);
222 HeapFree(GetProcessHeap(), 0, This
);
228 static HRESULT WINAPI
JpegDecoder_QueryCapability(IWICBitmapDecoder
*iface
, IStream
*stream
,
233 TRACE("(%p,%p,%p)\n", iface
, stream
, capability
);
235 if (!stream
|| !capability
) return E_INVALIDARG
;
237 hr
= IWICBitmapDecoder_Initialize(iface
, stream
, WICDecodeMetadataCacheOnDemand
);
238 if (hr
!= S_OK
) return hr
;
240 *capability
= WICBitmapDecoderCapabilityCanDecodeAllImages
|
241 WICBitmapDecoderCapabilityCanDecodeSomeImages
;
242 /* FIXME: WICBitmapDecoderCapabilityCanEnumerateMetadata */
246 static void source_mgr_init_source(j_decompress_ptr cinfo
)
250 static jpeg_boolean
source_mgr_fill_input_buffer(j_decompress_ptr cinfo
)
252 JpegDecoder
*This
= decoder_from_decompress(cinfo
);
256 hr
= IStream_Read(This
->stream
, This
->source_buffer
, 1024, &bytesread
);
258 if (FAILED(hr
) || bytesread
== 0)
264 This
->source_mgr
.next_input_byte
= This
->source_buffer
;
265 This
->source_mgr
.bytes_in_buffer
= bytesread
;
270 static void source_mgr_skip_input_data(j_decompress_ptr cinfo
, long num_bytes
)
272 JpegDecoder
*This
= decoder_from_decompress(cinfo
);
275 if (num_bytes
> This
->source_mgr
.bytes_in_buffer
)
277 seek
.QuadPart
= num_bytes
- This
->source_mgr
.bytes_in_buffer
;
278 IStream_Seek(This
->stream
, seek
, STREAM_SEEK_CUR
, NULL
);
279 This
->source_mgr
.bytes_in_buffer
= 0;
281 else if (num_bytes
> 0)
283 This
->source_mgr
.next_input_byte
+= num_bytes
;
284 This
->source_mgr
.bytes_in_buffer
-= num_bytes
;
288 static void source_mgr_term_source(j_decompress_ptr cinfo
)
292 static HRESULT WINAPI
JpegDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
293 WICDecodeOptions cacheOptions
)
295 JpegDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
299 TRACE("(%p,%p,%u)\n", iface
, pIStream
, cacheOptions
);
301 EnterCriticalSection(&This
->lock
);
303 if (This
->cinfo_initialized
)
305 LeaveCriticalSection(&This
->lock
);
306 return WINCODEC_ERR_WRONGSTATE
;
309 pjpeg_std_error(&This
->jerr
);
311 This
->jerr
.error_exit
= error_exit_fn
;
312 This
->jerr
.emit_message
= emit_message_fn
;
314 This
->cinfo
.err
= &This
->jerr
;
316 This
->cinfo
.client_data
= jmpbuf
;
320 LeaveCriticalSection(&This
->lock
);
324 pjpeg_CreateDecompress(&This
->cinfo
, JPEG_LIB_VERSION
, sizeof(struct jpeg_decompress_struct
));
326 This
->cinfo_initialized
= TRUE
;
328 This
->stream
= pIStream
;
329 IStream_AddRef(pIStream
);
332 IStream_Seek(This
->stream
, seek
, STREAM_SEEK_SET
, NULL
);
334 This
->source_mgr
.bytes_in_buffer
= 0;
335 This
->source_mgr
.init_source
= source_mgr_init_source
;
336 This
->source_mgr
.fill_input_buffer
= source_mgr_fill_input_buffer
;
337 This
->source_mgr
.skip_input_data
= source_mgr_skip_input_data
;
338 This
->source_mgr
.resync_to_restart
= pjpeg_resync_to_restart
;
339 This
->source_mgr
.term_source
= source_mgr_term_source
;
341 This
->cinfo
.src
= &This
->source_mgr
;
343 ret
= pjpeg_read_header(&This
->cinfo
, TRUE
);
345 if (ret
!= JPEG_HEADER_OK
) {
346 WARN("Jpeg image in stream has bad format, read header returned %d.\n",ret
);
347 LeaveCriticalSection(&This
->lock
);
351 switch (This
->cinfo
.jpeg_color_space
)
354 This
->cinfo
.out_color_space
= JCS_GRAYSCALE
;
358 This
->cinfo
.out_color_space
= JCS_RGB
;
362 This
->cinfo
.out_color_space
= JCS_CMYK
;
365 ERR("Unknown JPEG color space %i\n", This
->cinfo
.jpeg_color_space
);
366 LeaveCriticalSection(&This
->lock
);
370 if (!pjpeg_start_decompress(&This
->cinfo
))
372 ERR("jpeg_start_decompress failed\n");
373 LeaveCriticalSection(&This
->lock
);
377 This
->initialized
= TRUE
;
379 LeaveCriticalSection(&This
->lock
);
384 static HRESULT WINAPI
JpegDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
385 GUID
*pguidContainerFormat
)
387 memcpy(pguidContainerFormat
, &GUID_ContainerFormatJpeg
, sizeof(GUID
));
391 static HRESULT WINAPI
JpegDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
392 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
395 IWICComponentInfo
*compinfo
;
397 TRACE("(%p,%p)\n", iface
, ppIDecoderInfo
);
399 hr
= CreateComponentInfo(&CLSID_WICJpegDecoder
, &compinfo
);
400 if (FAILED(hr
)) return hr
;
402 hr
= IWICComponentInfo_QueryInterface(compinfo
, &IID_IWICBitmapDecoderInfo
,
403 (void**)ppIDecoderInfo
);
405 IWICComponentInfo_Release(compinfo
);
410 static HRESULT WINAPI
JpegDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
411 IWICPalette
*pIPalette
)
413 TRACE("(%p,%p)\n", iface
, pIPalette
);
415 return WINCODEC_ERR_PALETTEUNAVAILABLE
;
418 static HRESULT WINAPI
JpegDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
419 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
421 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
425 static HRESULT WINAPI
JpegDecoder_GetPreview(IWICBitmapDecoder
*iface
,
426 IWICBitmapSource
**ppIBitmapSource
)
428 FIXME("(%p,%p): stub\n", iface
, ppIBitmapSource
);
429 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
432 static HRESULT WINAPI
JpegDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
433 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
435 FIXME("(%p,%u,%p,%p): stub\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
436 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
439 static HRESULT WINAPI
JpegDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
440 IWICBitmapSource
**ppIThumbnail
)
442 FIXME("(%p,%p): stub\n", iface
, ppIThumbnail
);
443 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
446 static HRESULT WINAPI
JpegDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
449 if (!pCount
) return E_INVALIDARG
;
455 static HRESULT WINAPI
JpegDecoder_GetFrame(IWICBitmapDecoder
*iface
,
456 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
458 JpegDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
459 TRACE("(%p,%u,%p)\n", iface
, index
, ppIBitmapFrame
);
461 if (!This
->initialized
) return WINCODEC_ERR_FRAMEMISSING
;
463 if (index
!= 0) return E_INVALIDARG
;
465 IWICBitmapDecoder_AddRef(iface
);
466 *ppIBitmapFrame
= &This
->IWICBitmapFrameDecode_iface
;
471 static const IWICBitmapDecoderVtbl JpegDecoder_Vtbl
= {
472 JpegDecoder_QueryInterface
,
475 JpegDecoder_QueryCapability
,
476 JpegDecoder_Initialize
,
477 JpegDecoder_GetContainerFormat
,
478 JpegDecoder_GetDecoderInfo
,
479 JpegDecoder_CopyPalette
,
480 JpegDecoder_GetMetadataQueryReader
,
481 JpegDecoder_GetPreview
,
482 JpegDecoder_GetColorContexts
,
483 JpegDecoder_GetThumbnail
,
484 JpegDecoder_GetFrameCount
,
488 static HRESULT WINAPI
JpegDecoder_Frame_QueryInterface(IWICBitmapFrameDecode
*iface
, REFIID iid
,
491 JpegDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
493 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
495 if (!ppv
) return E_INVALIDARG
;
497 if (IsEqualIID(&IID_IUnknown
, iid
) ||
498 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
499 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
501 *ppv
= &This
->IWICBitmapFrameDecode_iface
;
506 return E_NOINTERFACE
;
509 IUnknown_AddRef((IUnknown
*)*ppv
);
513 static ULONG WINAPI
JpegDecoder_Frame_AddRef(IWICBitmapFrameDecode
*iface
)
515 JpegDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
516 return IWICBitmapDecoder_AddRef(&This
->IWICBitmapDecoder_iface
);
519 static ULONG WINAPI
JpegDecoder_Frame_Release(IWICBitmapFrameDecode
*iface
)
521 JpegDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
522 return IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
525 static HRESULT WINAPI
JpegDecoder_Frame_GetSize(IWICBitmapFrameDecode
*iface
,
526 UINT
*puiWidth
, UINT
*puiHeight
)
528 JpegDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
529 *puiWidth
= This
->cinfo
.output_width
;
530 *puiHeight
= This
->cinfo
.output_height
;
531 TRACE("(%p)->(%u,%u)\n", iface
, *puiWidth
, *puiHeight
);
535 static HRESULT WINAPI
JpegDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
536 WICPixelFormatGUID
*pPixelFormat
)
538 JpegDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
539 TRACE("(%p,%p)\n", iface
, pPixelFormat
);
540 if (This
->cinfo
.out_color_space
== JCS_RGB
)
541 memcpy(pPixelFormat
, &GUID_WICPixelFormat24bppBGR
, sizeof(GUID
));
542 else if (This
->cinfo
.out_color_space
== JCS_CMYK
)
543 memcpy(pPixelFormat
, &GUID_WICPixelFormat32bppCMYK
, sizeof(GUID
));
544 else /* This->cinfo.out_color_space == JCS_GRAYSCALE */
545 memcpy(pPixelFormat
, &GUID_WICPixelFormat8bppGray
, sizeof(GUID
));
549 static HRESULT WINAPI
JpegDecoder_Frame_GetResolution(IWICBitmapFrameDecode
*iface
,
550 double *pDpiX
, double *pDpiY
)
552 JpegDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
554 EnterCriticalSection(&This
->lock
);
556 switch (This
->cinfo
.density_unit
)
558 case 2: /* pixels per centimeter */
559 *pDpiX
= This
->cinfo
.X_density
* 2.54;
560 *pDpiY
= This
->cinfo
.Y_density
* 2.54;
563 case 1: /* pixels per inch */
564 *pDpiX
= This
->cinfo
.X_density
;
565 *pDpiY
= This
->cinfo
.Y_density
;
568 case 0: /* unknown */
575 LeaveCriticalSection(&This
->lock
);
577 TRACE("(%p)->(%0.2f,%0.2f)\n", iface
, *pDpiX
, *pDpiY
);
582 static HRESULT WINAPI
JpegDecoder_Frame_CopyPalette(IWICBitmapFrameDecode
*iface
,
583 IWICPalette
*pIPalette
)
585 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
589 static HRESULT WINAPI
JpegDecoder_Frame_CopyPixels(IWICBitmapFrameDecode
*iface
,
590 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
592 JpegDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
599 TRACE("(%p,%p,%u,%u,%p)\n", iface
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
605 rect
.Width
= This
->cinfo
.output_width
;
606 rect
.Height
= This
->cinfo
.output_height
;
611 if (prc
->X
< 0 || prc
->Y
< 0 || prc
->X
+prc
->Width
> This
->cinfo
.output_width
||
612 prc
->Y
+prc
->Height
> This
->cinfo
.output_height
)
616 if (This
->cinfo
.out_color_space
== JCS_GRAYSCALE
) bpp
= 8;
617 else if (This
->cinfo
.out_color_space
== JCS_CMYK
) bpp
= 32;
620 stride
= bpp
* This
->cinfo
.output_width
;
621 data_size
= stride
* This
->cinfo
.output_height
;
623 max_row_needed
= prc
->Y
+ prc
->Height
;
624 if (max_row_needed
> This
->cinfo
.output_height
) return E_INVALIDARG
;
626 EnterCriticalSection(&This
->lock
);
628 if (!This
->image_data
)
630 This
->image_data
= HeapAlloc(GetProcessHeap(), 0, data_size
);
631 if (!This
->image_data
)
633 LeaveCriticalSection(&This
->lock
);
634 return E_OUTOFMEMORY
;
638 This
->cinfo
.client_data
= jmpbuf
;
642 LeaveCriticalSection(&This
->lock
);
646 while (max_row_needed
> This
->cinfo
.output_scanline
)
648 UINT first_scanline
= This
->cinfo
.output_scanline
;
650 JSAMPROW out_rows
[4];
654 max_rows
= min(This
->cinfo
.output_height
-first_scanline
, 4);
655 for (i
=0; i
<max_rows
; i
++)
656 out_rows
[i
] = This
->image_data
+ stride
* (first_scanline
+i
);
658 ret
= pjpeg_read_scanlines(&This
->cinfo
, out_rows
, max_rows
);
662 ERR("read_scanlines failed\n");
663 LeaveCriticalSection(&This
->lock
);
669 /* libjpeg gives us RGB data and we want BGR, so byteswap the data */
670 reverse_bgr8(3, This
->image_data
+ stride
* first_scanline
,
671 This
->cinfo
.output_width
, This
->cinfo
.output_scanline
- first_scanline
,
675 if (This
->cinfo
.out_color_space
== JCS_CMYK
&& This
->cinfo
.saw_Adobe_marker
)
676 /* Adobe JPEG's have inverted CMYK data. */
677 for (i
=0; i
<data_size
; i
++)
678 This
->image_data
[i
] ^= 0xff;
681 LeaveCriticalSection(&This
->lock
);
683 return copy_pixels(bpp
, This
->image_data
,
684 This
->cinfo
.output_width
, This
->cinfo
.output_height
, stride
,
685 prc
, cbStride
, cbBufferSize
, pbBuffer
);
688 static HRESULT WINAPI
JpegDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
689 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
691 JpegDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
693 TRACE("(%p,%p)\n", iface
, ppIMetadataQueryReader
);
695 if (!ppIMetadataQueryReader
)
698 return MetadataQueryReader_CreateInstance(&This
->IWICMetadataBlockReader_iface
, ppIMetadataQueryReader
);
701 static HRESULT WINAPI
JpegDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode
*iface
,
702 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
704 FIXME("(%p,%u,%p,%p): stub\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
705 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
708 static HRESULT WINAPI
JpegDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode
*iface
,
709 IWICBitmapSource
**ppIThumbnail
)
711 FIXME("(%p,%p): stub\n", iface
, ppIThumbnail
);
712 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
715 static const IWICBitmapFrameDecodeVtbl JpegDecoder_Frame_Vtbl
= {
716 JpegDecoder_Frame_QueryInterface
,
717 JpegDecoder_Frame_AddRef
,
718 JpegDecoder_Frame_Release
,
719 JpegDecoder_Frame_GetSize
,
720 JpegDecoder_Frame_GetPixelFormat
,
721 JpegDecoder_Frame_GetResolution
,
722 JpegDecoder_Frame_CopyPalette
,
723 JpegDecoder_Frame_CopyPixels
,
724 JpegDecoder_Frame_GetMetadataQueryReader
,
725 JpegDecoder_Frame_GetColorContexts
,
726 JpegDecoder_Frame_GetThumbnail
729 static HRESULT WINAPI
JpegDecoder_Block_QueryInterface(IWICMetadataBlockReader
*iface
, REFIID iid
,
732 JpegDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
733 return IWICBitmapFrameDecode_QueryInterface(&This
->IWICBitmapFrameDecode_iface
, iid
, ppv
);
736 static ULONG WINAPI
JpegDecoder_Block_AddRef(IWICMetadataBlockReader
*iface
)
738 JpegDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
739 return IWICBitmapDecoder_AddRef(&This
->IWICBitmapDecoder_iface
);
742 static ULONG WINAPI
JpegDecoder_Block_Release(IWICMetadataBlockReader
*iface
)
744 JpegDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
745 return IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
748 static HRESULT WINAPI
JpegDecoder_Block_GetContainerFormat(IWICMetadataBlockReader
*iface
,
749 GUID
*pguidContainerFormat
)
751 TRACE("%p,%p\n", iface
, pguidContainerFormat
);
753 if (!pguidContainerFormat
) return E_INVALIDARG
;
755 memcpy(pguidContainerFormat
, &GUID_ContainerFormatJpeg
, sizeof(GUID
));
760 static HRESULT WINAPI
JpegDecoder_Block_GetCount(IWICMetadataBlockReader
*iface
,
763 FIXME("%p,%p\n", iface
, pcCount
);
765 if (!pcCount
) return E_INVALIDARG
;
772 static HRESULT WINAPI
JpegDecoder_Block_GetReaderByIndex(IWICMetadataBlockReader
*iface
,
773 UINT nIndex
, IWICMetadataReader
**ppIMetadataReader
)
775 FIXME("%p,%d,%p\n", iface
, nIndex
, ppIMetadataReader
);
779 static HRESULT WINAPI
JpegDecoder_Block_GetEnumerator(IWICMetadataBlockReader
*iface
,
780 IEnumUnknown
**ppIEnumMetadata
)
782 FIXME("%p,%p\n", iface
, ppIEnumMetadata
);
786 static const IWICMetadataBlockReaderVtbl JpegDecoder_Block_Vtbl
= {
787 JpegDecoder_Block_QueryInterface
,
788 JpegDecoder_Block_AddRef
,
789 JpegDecoder_Block_Release
,
790 JpegDecoder_Block_GetContainerFormat
,
791 JpegDecoder_Block_GetCount
,
792 JpegDecoder_Block_GetReaderByIndex
,
793 JpegDecoder_Block_GetEnumerator
,
796 HRESULT
JpegDecoder_CreateInstance(REFIID iid
, void** ppv
)
801 TRACE("(%s,%p)\n", debugstr_guid(iid
), ppv
);
803 if (!libjpeg_handle
&& !load_libjpeg())
805 ERR("Failed reading JPEG because unable to find %s\n", SONAME_LIBJPEG
);
811 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(JpegDecoder
));
812 if (!This
) return E_OUTOFMEMORY
;
814 This
->IWICBitmapDecoder_iface
.lpVtbl
= &JpegDecoder_Vtbl
;
815 This
->IWICBitmapFrameDecode_iface
.lpVtbl
= &JpegDecoder_Frame_Vtbl
;
816 This
->IWICMetadataBlockReader_iface
.lpVtbl
= &JpegDecoder_Block_Vtbl
;
818 This
->initialized
= FALSE
;
819 This
->cinfo_initialized
= FALSE
;
821 This
->image_data
= NULL
;
822 InitializeCriticalSection(&This
->lock
);
823 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": JpegDecoder.lock");
825 ret
= IWICBitmapDecoder_QueryInterface(&This
->IWICBitmapDecoder_iface
, iid
, ppv
);
826 IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
831 typedef struct jpeg_compress_format
{
832 const WICPixelFormatGUID
*guid
;
835 J_COLOR_SPACE color_space
;
837 } jpeg_compress_format
;
839 static const jpeg_compress_format compress_formats
[] = {
840 { &GUID_WICPixelFormat24bppBGR
, 24, 3, JCS_RGB
, 1 },
841 { &GUID_WICPixelFormat32bppCMYK
, 32, 4, JCS_CMYK
},
842 { &GUID_WICPixelFormat8bppGray
, 8, 1, JCS_GRAYSCALE
},
846 typedef struct JpegEncoder
{
847 IWICBitmapEncoder IWICBitmapEncoder_iface
;
848 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface
;
850 struct jpeg_compress_struct cinfo
;
851 struct jpeg_error_mgr jerr
;
852 struct jpeg_destination_mgr dest_mgr
;
855 BOOL frame_initialized
;
856 BOOL started_compress
;
858 BOOL frame_committed
;
862 const jpeg_compress_format
*format
;
864 CRITICAL_SECTION lock
;
865 BYTE dest_buffer
[1024];
868 static inline JpegEncoder
*impl_from_IWICBitmapEncoder(IWICBitmapEncoder
*iface
)
870 return CONTAINING_RECORD(iface
, JpegEncoder
, IWICBitmapEncoder_iface
);
873 static inline JpegEncoder
*impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode
*iface
)
875 return CONTAINING_RECORD(iface
, JpegEncoder
, IWICBitmapFrameEncode_iface
);
878 static inline JpegEncoder
*encoder_from_compress(j_compress_ptr compress
)
880 return CONTAINING_RECORD(compress
, JpegEncoder
, cinfo
);
883 static void dest_mgr_init_destination(j_compress_ptr cinfo
)
885 JpegEncoder
*This
= encoder_from_compress(cinfo
);
887 This
->dest_mgr
.next_output_byte
= This
->dest_buffer
;
888 This
->dest_mgr
.free_in_buffer
= sizeof(This
->dest_buffer
);
891 static jpeg_boolean
dest_mgr_empty_output_buffer(j_compress_ptr cinfo
)
893 JpegEncoder
*This
= encoder_from_compress(cinfo
);
897 hr
= IStream_Write(This
->stream
, This
->dest_buffer
,
898 sizeof(This
->dest_buffer
), &byteswritten
);
900 if (hr
!= S_OK
|| byteswritten
== 0)
902 ERR("Failed writing data, hr=%x\n", hr
);
906 This
->dest_mgr
.next_output_byte
= This
->dest_buffer
;
907 This
->dest_mgr
.free_in_buffer
= sizeof(This
->dest_buffer
);
911 static void dest_mgr_term_destination(j_compress_ptr cinfo
)
913 JpegEncoder
*This
= encoder_from_compress(cinfo
);
917 if (This
->dest_mgr
.free_in_buffer
!= sizeof(This
->dest_buffer
))
919 hr
= IStream_Write(This
->stream
, This
->dest_buffer
,
920 sizeof(This
->dest_buffer
) - This
->dest_mgr
.free_in_buffer
, &byteswritten
);
922 if (hr
!= S_OK
|| byteswritten
== 0)
923 ERR("Failed writing data, hr=%x\n", hr
);
927 static HRESULT WINAPI
JpegEncoder_Frame_QueryInterface(IWICBitmapFrameEncode
*iface
, REFIID iid
,
930 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
931 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
933 if (!ppv
) return E_INVALIDARG
;
935 if (IsEqualIID(&IID_IUnknown
, iid
) ||
936 IsEqualIID(&IID_IWICBitmapFrameEncode
, iid
))
938 *ppv
= &This
->IWICBitmapFrameEncode_iface
;
943 return E_NOINTERFACE
;
946 IUnknown_AddRef((IUnknown
*)*ppv
);
950 static ULONG WINAPI
JpegEncoder_Frame_AddRef(IWICBitmapFrameEncode
*iface
)
952 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
953 return IWICBitmapEncoder_AddRef(&This
->IWICBitmapEncoder_iface
);
956 static ULONG WINAPI
JpegEncoder_Frame_Release(IWICBitmapFrameEncode
*iface
)
958 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
959 return IWICBitmapEncoder_Release(&This
->IWICBitmapEncoder_iface
);
962 static HRESULT WINAPI
JpegEncoder_Frame_Initialize(IWICBitmapFrameEncode
*iface
,
963 IPropertyBag2
*pIEncoderOptions
)
965 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
966 TRACE("(%p,%p)\n", iface
, pIEncoderOptions
);
968 EnterCriticalSection(&This
->lock
);
970 if (This
->frame_initialized
)
972 LeaveCriticalSection(&This
->lock
);
973 return WINCODEC_ERR_WRONGSTATE
;
976 This
->frame_initialized
= TRUE
;
978 LeaveCriticalSection(&This
->lock
);
983 static HRESULT WINAPI
JpegEncoder_Frame_SetSize(IWICBitmapFrameEncode
*iface
,
984 UINT uiWidth
, UINT uiHeight
)
986 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
987 TRACE("(%p,%u,%u)\n", iface
, uiWidth
, uiHeight
);
989 EnterCriticalSection(&This
->lock
);
991 if (!This
->frame_initialized
|| This
->started_compress
)
993 LeaveCriticalSection(&This
->lock
);
994 return WINCODEC_ERR_WRONGSTATE
;
997 This
->width
= uiWidth
;
998 This
->height
= uiHeight
;
1000 LeaveCriticalSection(&This
->lock
);
1005 static HRESULT WINAPI
JpegEncoder_Frame_SetResolution(IWICBitmapFrameEncode
*iface
,
1006 double dpiX
, double dpiY
)
1008 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1009 TRACE("(%p,%0.2f,%0.2f)\n", iface
, dpiX
, dpiY
);
1011 EnterCriticalSection(&This
->lock
);
1013 if (!This
->frame_initialized
|| This
->started_compress
)
1015 LeaveCriticalSection(&This
->lock
);
1016 return WINCODEC_ERR_WRONGSTATE
;
1022 LeaveCriticalSection(&This
->lock
);
1027 static HRESULT WINAPI
JpegEncoder_Frame_SetPixelFormat(IWICBitmapFrameEncode
*iface
,
1028 WICPixelFormatGUID
*pPixelFormat
)
1030 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1032 TRACE("(%p,%s)\n", iface
, debugstr_guid(pPixelFormat
));
1034 EnterCriticalSection(&This
->lock
);
1036 if (!This
->frame_initialized
|| This
->started_compress
)
1038 LeaveCriticalSection(&This
->lock
);
1039 return WINCODEC_ERR_WRONGSTATE
;
1042 for (i
=0; compress_formats
[i
].guid
; i
++)
1044 if (memcmp(compress_formats
[i
].guid
, pPixelFormat
, sizeof(GUID
)) == 0)
1048 if (!compress_formats
[i
].guid
) i
= 0;
1050 This
->format
= &compress_formats
[i
];
1051 memcpy(pPixelFormat
, This
->format
->guid
, sizeof(GUID
));
1053 LeaveCriticalSection(&This
->lock
);
1058 static HRESULT WINAPI
JpegEncoder_Frame_SetColorContexts(IWICBitmapFrameEncode
*iface
,
1059 UINT cCount
, IWICColorContext
**ppIColorContext
)
1061 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
1065 static HRESULT WINAPI
JpegEncoder_Frame_SetPalette(IWICBitmapFrameEncode
*iface
,
1066 IWICPalette
*pIPalette
)
1068 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
1069 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1072 static HRESULT WINAPI
JpegEncoder_Frame_SetThumbnail(IWICBitmapFrameEncode
*iface
,
1073 IWICBitmapSource
*pIThumbnail
)
1075 FIXME("(%p,%p): stub\n", iface
, pIThumbnail
);
1076 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1079 static HRESULT WINAPI
JpegEncoder_Frame_WritePixels(IWICBitmapFrameEncode
*iface
,
1080 UINT lineCount
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbPixels
)
1082 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1084 BYTE
*swapped_data
= NULL
, *current_row
;
1087 TRACE("(%p,%u,%u,%u,%p)\n", iface
, lineCount
, cbStride
, cbBufferSize
, pbPixels
);
1089 EnterCriticalSection(&This
->lock
);
1091 if (!This
->frame_initialized
|| !This
->width
|| !This
->height
|| !This
->format
)
1093 LeaveCriticalSection(&This
->lock
);
1094 return WINCODEC_ERR_WRONGSTATE
;
1097 if (lineCount
== 0 || lineCount
+ This
->lines_written
> This
->height
)
1099 LeaveCriticalSection(&This
->lock
);
1100 return E_INVALIDARG
;
1103 /* set up setjmp/longjmp error handling */
1106 LeaveCriticalSection(&This
->lock
);
1107 HeapFree(GetProcessHeap(), 0, swapped_data
);
1110 This
->cinfo
.client_data
= jmpbuf
;
1112 if (!This
->started_compress
)
1114 This
->cinfo
.image_width
= This
->width
;
1115 This
->cinfo
.image_height
= This
->height
;
1116 This
->cinfo
.input_components
= This
->format
->num_components
;
1117 This
->cinfo
.in_color_space
= This
->format
->color_space
;
1119 pjpeg_set_defaults(&This
->cinfo
);
1121 if (This
->xres
!= 0.0 && This
->yres
!= 0.0)
1123 This
->cinfo
.density_unit
= 1; /* dots per inch */
1124 This
->cinfo
.X_density
= This
->xres
;
1125 This
->cinfo
.Y_density
= This
->yres
;
1128 pjpeg_start_compress(&This
->cinfo
, TRUE
);
1130 This
->started_compress
= TRUE
;
1133 row_size
= This
->format
->bpp
/ 8 * This
->width
;
1135 if (This
->format
->swap_rgb
)
1137 swapped_data
= HeapAlloc(GetProcessHeap(), 0, row_size
);
1140 LeaveCriticalSection(&This
->lock
);
1141 return E_OUTOFMEMORY
;
1145 for (line
=0; line
< lineCount
; line
++)
1147 if (This
->format
->swap_rgb
)
1151 memcpy(swapped_data
, pbPixels
+ (cbStride
* line
), row_size
);
1153 for (x
=0; x
< This
->width
; x
++)
1157 b
= swapped_data
[x
*3];
1158 swapped_data
[x
*3] = swapped_data
[x
*3+2];
1159 swapped_data
[x
*3+2] = b
;
1162 current_row
= swapped_data
;
1165 current_row
= pbPixels
+ (cbStride
* line
);
1167 if (!pjpeg_write_scanlines(&This
->cinfo
, ¤t_row
, 1))
1169 ERR("failed writing scanlines\n");
1170 LeaveCriticalSection(&This
->lock
);
1171 HeapFree(GetProcessHeap(), 0, swapped_data
);
1175 This
->lines_written
++;
1178 LeaveCriticalSection(&This
->lock
);
1179 HeapFree(GetProcessHeap(), 0, swapped_data
);
1184 static HRESULT WINAPI
JpegEncoder_Frame_WriteSource(IWICBitmapFrameEncode
*iface
,
1185 IWICBitmapSource
*pIBitmapSource
, WICRect
*prc
)
1187 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1189 TRACE("(%p,%p,%p)\n", iface
, pIBitmapSource
, prc
);
1191 if (!This
->frame_initialized
)
1192 return WINCODEC_ERR_WRONGSTATE
;
1194 hr
= configure_write_source(iface
, pIBitmapSource
, prc
,
1195 This
->format
? This
->format
->guid
: NULL
, This
->width
, This
->height
,
1196 This
->xres
, This
->yres
);
1200 hr
= write_source(iface
, pIBitmapSource
, prc
,
1201 This
->format
->guid
, This
->format
->bpp
, This
->width
, This
->height
);
1207 static HRESULT WINAPI
JpegEncoder_Frame_Commit(IWICBitmapFrameEncode
*iface
)
1209 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1211 TRACE("(%p)\n", iface
);
1213 EnterCriticalSection(&This
->lock
);
1215 if (!This
->started_compress
|| This
->lines_written
!= This
->height
|| This
->frame_committed
)
1217 LeaveCriticalSection(&This
->lock
);
1218 return WINCODEC_ERR_WRONGSTATE
;
1221 /* set up setjmp/longjmp error handling */
1224 LeaveCriticalSection(&This
->lock
);
1227 This
->cinfo
.client_data
= jmpbuf
;
1229 pjpeg_finish_compress(&This
->cinfo
);
1231 This
->frame_committed
= TRUE
;
1233 LeaveCriticalSection(&This
->lock
);
1238 static HRESULT WINAPI
JpegEncoder_Frame_GetMetadataQueryWriter(IWICBitmapFrameEncode
*iface
,
1239 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
1241 FIXME("(%p, %p): stub\n", iface
, ppIMetadataQueryWriter
);
1245 static const IWICBitmapFrameEncodeVtbl JpegEncoder_FrameVtbl
= {
1246 JpegEncoder_Frame_QueryInterface
,
1247 JpegEncoder_Frame_AddRef
,
1248 JpegEncoder_Frame_Release
,
1249 JpegEncoder_Frame_Initialize
,
1250 JpegEncoder_Frame_SetSize
,
1251 JpegEncoder_Frame_SetResolution
,
1252 JpegEncoder_Frame_SetPixelFormat
,
1253 JpegEncoder_Frame_SetColorContexts
,
1254 JpegEncoder_Frame_SetPalette
,
1255 JpegEncoder_Frame_SetThumbnail
,
1256 JpegEncoder_Frame_WritePixels
,
1257 JpegEncoder_Frame_WriteSource
,
1258 JpegEncoder_Frame_Commit
,
1259 JpegEncoder_Frame_GetMetadataQueryWriter
1262 static HRESULT WINAPI
JpegEncoder_QueryInterface(IWICBitmapEncoder
*iface
, REFIID iid
,
1265 JpegEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1266 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
1268 if (!ppv
) return E_INVALIDARG
;
1270 if (IsEqualIID(&IID_IUnknown
, iid
) ||
1271 IsEqualIID(&IID_IWICBitmapEncoder
, iid
))
1273 *ppv
= &This
->IWICBitmapEncoder_iface
;
1278 return E_NOINTERFACE
;
1281 IUnknown_AddRef((IUnknown
*)*ppv
);
1285 static ULONG WINAPI
JpegEncoder_AddRef(IWICBitmapEncoder
*iface
)
1287 JpegEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1288 ULONG ref
= InterlockedIncrement(&This
->ref
);
1290 TRACE("(%p) refcount=%u\n", iface
, ref
);
1295 static ULONG WINAPI
JpegEncoder_Release(IWICBitmapEncoder
*iface
)
1297 JpegEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1298 ULONG ref
= InterlockedDecrement(&This
->ref
);
1300 TRACE("(%p) refcount=%u\n", iface
, ref
);
1304 This
->lock
.DebugInfo
->Spare
[0] = 0;
1305 DeleteCriticalSection(&This
->lock
);
1306 if (This
->initialized
) pjpeg_destroy_compress(&This
->cinfo
);
1307 if (This
->stream
) IStream_Release(This
->stream
);
1308 HeapFree(GetProcessHeap(), 0, This
);
1314 static HRESULT WINAPI
JpegEncoder_Initialize(IWICBitmapEncoder
*iface
,
1315 IStream
*pIStream
, WICBitmapEncoderCacheOption cacheOption
)
1317 JpegEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1320 TRACE("(%p,%p,%u)\n", iface
, pIStream
, cacheOption
);
1322 EnterCriticalSection(&This
->lock
);
1324 if (This
->initialized
)
1326 LeaveCriticalSection(&This
->lock
);
1327 return WINCODEC_ERR_WRONGSTATE
;
1330 pjpeg_std_error(&This
->jerr
);
1332 This
->jerr
.error_exit
= error_exit_fn
;
1333 This
->jerr
.emit_message
= emit_message_fn
;
1335 This
->cinfo
.err
= &This
->jerr
;
1337 This
->cinfo
.client_data
= jmpbuf
;
1341 LeaveCriticalSection(&This
->lock
);
1345 pjpeg_CreateCompress(&This
->cinfo
, JPEG_LIB_VERSION
, sizeof(struct jpeg_compress_struct
));
1347 This
->stream
= pIStream
;
1348 IStream_AddRef(pIStream
);
1350 This
->dest_mgr
.next_output_byte
= This
->dest_buffer
;
1351 This
->dest_mgr
.free_in_buffer
= sizeof(This
->dest_buffer
);
1353 This
->dest_mgr
.init_destination
= dest_mgr_init_destination
;
1354 This
->dest_mgr
.empty_output_buffer
= dest_mgr_empty_output_buffer
;
1355 This
->dest_mgr
.term_destination
= dest_mgr_term_destination
;
1357 This
->cinfo
.dest
= &This
->dest_mgr
;
1359 This
->initialized
= TRUE
;
1361 LeaveCriticalSection(&This
->lock
);
1366 static HRESULT WINAPI
JpegEncoder_GetContainerFormat(IWICBitmapEncoder
*iface
,
1367 GUID
*pguidContainerFormat
)
1369 FIXME("(%p,%s): stub\n", iface
, debugstr_guid(pguidContainerFormat
));
1373 static HRESULT WINAPI
JpegEncoder_GetEncoderInfo(IWICBitmapEncoder
*iface
,
1374 IWICBitmapEncoderInfo
**ppIEncoderInfo
)
1376 FIXME("(%p,%p): stub\n", iface
, ppIEncoderInfo
);
1380 static HRESULT WINAPI
JpegEncoder_SetColorContexts(IWICBitmapEncoder
*iface
,
1381 UINT cCount
, IWICColorContext
**ppIColorContext
)
1383 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
1387 static HRESULT WINAPI
JpegEncoder_SetPalette(IWICBitmapEncoder
*iface
, IWICPalette
*pIPalette
)
1389 TRACE("(%p,%p)\n", iface
, pIPalette
);
1390 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1393 static HRESULT WINAPI
JpegEncoder_SetThumbnail(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIThumbnail
)
1395 TRACE("(%p,%p)\n", iface
, pIThumbnail
);
1396 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1399 static HRESULT WINAPI
JpegEncoder_SetPreview(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIPreview
)
1401 TRACE("(%p,%p)\n", iface
, pIPreview
);
1402 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1405 static HRESULT WINAPI
JpegEncoder_CreateNewFrame(IWICBitmapEncoder
*iface
,
1406 IWICBitmapFrameEncode
**ppIFrameEncode
, IPropertyBag2
**ppIEncoderOptions
)
1408 JpegEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1411 TRACE("(%p,%p,%p)\n", iface
, ppIFrameEncode
, ppIEncoderOptions
);
1413 EnterCriticalSection(&This
->lock
);
1415 if (This
->frame_count
!= 0)
1417 LeaveCriticalSection(&This
->lock
);
1418 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1421 if (!This
->initialized
)
1423 LeaveCriticalSection(&This
->lock
);
1424 return WINCODEC_ERR_NOTINITIALIZED
;
1427 hr
= CreatePropertyBag2(NULL
, 0, ppIEncoderOptions
);
1430 LeaveCriticalSection(&This
->lock
);
1434 This
->frame_count
= 1;
1436 LeaveCriticalSection(&This
->lock
);
1438 IWICBitmapEncoder_AddRef(iface
);
1439 *ppIFrameEncode
= &This
->IWICBitmapFrameEncode_iface
;
1444 static HRESULT WINAPI
JpegEncoder_Commit(IWICBitmapEncoder
*iface
)
1446 JpegEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1447 TRACE("(%p)\n", iface
);
1449 EnterCriticalSection(&This
->lock
);
1451 if (!This
->frame_committed
|| This
->committed
)
1453 LeaveCriticalSection(&This
->lock
);
1454 return WINCODEC_ERR_WRONGSTATE
;
1457 This
->committed
= TRUE
;
1459 LeaveCriticalSection(&This
->lock
);
1464 static HRESULT WINAPI
JpegEncoder_GetMetadataQueryWriter(IWICBitmapEncoder
*iface
,
1465 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
1467 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryWriter
);
1471 static const IWICBitmapEncoderVtbl JpegEncoder_Vtbl
= {
1472 JpegEncoder_QueryInterface
,
1474 JpegEncoder_Release
,
1475 JpegEncoder_Initialize
,
1476 JpegEncoder_GetContainerFormat
,
1477 JpegEncoder_GetEncoderInfo
,
1478 JpegEncoder_SetColorContexts
,
1479 JpegEncoder_SetPalette
,
1480 JpegEncoder_SetThumbnail
,
1481 JpegEncoder_SetPreview
,
1482 JpegEncoder_CreateNewFrame
,
1484 JpegEncoder_GetMetadataQueryWriter
1487 HRESULT
JpegEncoder_CreateInstance(REFIID iid
, void** ppv
)
1492 TRACE("(%s,%p)\n", debugstr_guid(iid
), ppv
);
1496 if (!libjpeg_handle
&& !load_libjpeg())
1498 ERR("Failed writing JPEG because unable to find %s\n",SONAME_LIBJPEG
);
1502 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(JpegEncoder
));
1503 if (!This
) return E_OUTOFMEMORY
;
1505 This
->IWICBitmapEncoder_iface
.lpVtbl
= &JpegEncoder_Vtbl
;
1506 This
->IWICBitmapFrameEncode_iface
.lpVtbl
= &JpegEncoder_FrameVtbl
;
1508 This
->initialized
= FALSE
;
1509 This
->frame_count
= 0;
1510 This
->frame_initialized
= FALSE
;
1511 This
->started_compress
= FALSE
;
1512 This
->lines_written
= 0;
1513 This
->frame_committed
= FALSE
;
1514 This
->committed
= FALSE
;
1515 This
->width
= This
->height
= 0;
1516 This
->xres
= This
->yres
= 0.0;
1517 This
->format
= NULL
;
1518 This
->stream
= NULL
;
1519 InitializeCriticalSection(&This
->lock
);
1520 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": JpegEncoder.lock");
1522 ret
= IWICBitmapEncoder_QueryInterface(&This
->IWICBitmapEncoder_iface
, iid
, ppv
);
1523 IWICBitmapEncoder_Release(&This
->IWICBitmapEncoder_iface
);
1528 #else /* !defined(SONAME_LIBJPEG) */
1530 HRESULT
JpegDecoder_CreateInstance(REFIID iid
, void** ppv
)
1532 ERR("Trying to load JPEG picture, but JPEG support is not compiled in.\n");
1536 HRESULT
JpegEncoder_CreateInstance(REFIID iid
, void** ppv
)
1538 ERR("Trying to save JPEG picture, but JPEG support is not compiled in.\n");