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_CreateCompress
);
66 MAKE_FUNCPTR(jpeg_CreateDecompress
);
67 MAKE_FUNCPTR(jpeg_destroy_compress
);
68 MAKE_FUNCPTR(jpeg_destroy_decompress
);
69 MAKE_FUNCPTR(jpeg_finish_compress
);
70 MAKE_FUNCPTR(jpeg_read_header
);
71 MAKE_FUNCPTR(jpeg_read_scanlines
);
72 MAKE_FUNCPTR(jpeg_resync_to_restart
);
73 MAKE_FUNCPTR(jpeg_set_defaults
);
74 MAKE_FUNCPTR(jpeg_start_compress
);
75 MAKE_FUNCPTR(jpeg_start_decompress
);
76 MAKE_FUNCPTR(jpeg_std_error
);
77 MAKE_FUNCPTR(jpeg_write_scanlines
);
80 static void *load_libjpeg(void)
82 if((libjpeg_handle
= wine_dlopen(SONAME_LIBJPEG
, RTLD_NOW
, NULL
, 0)) != NULL
) {
84 #define LOAD_FUNCPTR(f) \
85 if((p##f = wine_dlsym(libjpeg_handle, #f, NULL, 0)) == NULL) { \
86 libjpeg_handle = NULL; \
90 LOAD_FUNCPTR(jpeg_CreateCompress
);
91 LOAD_FUNCPTR(jpeg_CreateDecompress
);
92 LOAD_FUNCPTR(jpeg_destroy_compress
);
93 LOAD_FUNCPTR(jpeg_destroy_decompress
);
94 LOAD_FUNCPTR(jpeg_finish_compress
);
95 LOAD_FUNCPTR(jpeg_read_header
);
96 LOAD_FUNCPTR(jpeg_read_scanlines
);
97 LOAD_FUNCPTR(jpeg_resync_to_restart
);
98 LOAD_FUNCPTR(jpeg_set_defaults
);
99 LOAD_FUNCPTR(jpeg_start_compress
);
100 LOAD_FUNCPTR(jpeg_start_decompress
);
101 LOAD_FUNCPTR(jpeg_std_error
);
102 LOAD_FUNCPTR(jpeg_write_scanlines
);
105 return libjpeg_handle
;
108 static void error_exit_fn(j_common_ptr cinfo
)
110 char message
[JMSG_LENGTH_MAX
];
113 cinfo
->err
->format_message(cinfo
, message
);
114 ERR_(jpeg
)("%s\n", message
);
116 longjmp(*(jmp_buf*)cinfo
->client_data
, 1);
119 static void emit_message_fn(j_common_ptr cinfo
, int msg_level
)
121 char message
[JMSG_LENGTH_MAX
];
123 if (msg_level
< 0 && ERR_ON(jpeg
))
125 cinfo
->err
->format_message(cinfo
, message
);
126 ERR_(jpeg
)("%s\n", message
);
128 else if (msg_level
== 0 && WARN_ON(jpeg
))
130 cinfo
->err
->format_message(cinfo
, message
);
131 WARN_(jpeg
)("%s\n", message
);
133 else if (msg_level
> 0 && TRACE_ON(jpeg
))
135 cinfo
->err
->format_message(cinfo
, message
);
136 TRACE_(jpeg
)("%s\n", message
);
141 IWICBitmapDecoder IWICBitmapDecoder_iface
;
142 IWICBitmapFrameDecode IWICBitmapFrameDecode_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 HRESULT WINAPI
JpegDecoder_QueryInterface(IWICBitmapDecoder
*iface
, REFIID iid
,
173 JpegDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
174 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
176 if (!ppv
) return E_INVALIDARG
;
178 if (IsEqualIID(&IID_IUnknown
, iid
) ||
179 IsEqualIID(&IID_IWICBitmapDecoder
, iid
))
181 *ppv
= &This
->IWICBitmapDecoder_iface
;
186 return E_NOINTERFACE
;
189 IUnknown_AddRef((IUnknown
*)*ppv
);
193 static ULONG WINAPI
JpegDecoder_AddRef(IWICBitmapDecoder
*iface
)
195 JpegDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
196 ULONG ref
= InterlockedIncrement(&This
->ref
);
198 TRACE("(%p) refcount=%u\n", iface
, ref
);
203 static ULONG WINAPI
JpegDecoder_Release(IWICBitmapDecoder
*iface
)
205 JpegDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
206 ULONG ref
= InterlockedDecrement(&This
->ref
);
208 TRACE("(%p) refcount=%u\n", iface
, ref
);
212 This
->lock
.DebugInfo
->Spare
[0] = 0;
213 DeleteCriticalSection(&This
->lock
);
214 if (This
->cinfo_initialized
) pjpeg_destroy_decompress(&This
->cinfo
);
215 if (This
->stream
) IStream_Release(This
->stream
);
216 HeapFree(GetProcessHeap(), 0, This
->image_data
);
217 HeapFree(GetProcessHeap(), 0, This
);
223 static HRESULT WINAPI
JpegDecoder_QueryCapability(IWICBitmapDecoder
*iface
, IStream
*stream
,
228 TRACE("(%p,%p,%p)\n", iface
, stream
, capability
);
230 if (!stream
|| !capability
) return E_INVALIDARG
;
232 hr
= IWICBitmapDecoder_Initialize(iface
, stream
, WICDecodeMetadataCacheOnDemand
);
233 if (hr
!= S_OK
) return hr
;
235 *capability
= WICBitmapDecoderCapabilityCanDecodeAllImages
|
236 WICBitmapDecoderCapabilityCanDecodeSomeImages
;
237 /* FIXME: WICBitmapDecoderCapabilityCanEnumerateMetadata */
241 static void source_mgr_init_source(j_decompress_ptr cinfo
)
245 static jpeg_boolean
source_mgr_fill_input_buffer(j_decompress_ptr cinfo
)
247 JpegDecoder
*This
= decoder_from_decompress(cinfo
);
251 hr
= IStream_Read(This
->stream
, This
->source_buffer
, 1024, &bytesread
);
253 if (FAILED(hr
) || bytesread
== 0)
259 This
->source_mgr
.next_input_byte
= This
->source_buffer
;
260 This
->source_mgr
.bytes_in_buffer
= bytesread
;
265 static void source_mgr_skip_input_data(j_decompress_ptr cinfo
, long num_bytes
)
267 JpegDecoder
*This
= decoder_from_decompress(cinfo
);
270 if (num_bytes
> This
->source_mgr
.bytes_in_buffer
)
272 seek
.QuadPart
= num_bytes
- This
->source_mgr
.bytes_in_buffer
;
273 IStream_Seek(This
->stream
, seek
, STREAM_SEEK_CUR
, NULL
);
274 This
->source_mgr
.bytes_in_buffer
= 0;
276 else if (num_bytes
> 0)
278 This
->source_mgr
.next_input_byte
+= num_bytes
;
279 This
->source_mgr
.bytes_in_buffer
-= num_bytes
;
283 static void source_mgr_term_source(j_decompress_ptr cinfo
)
287 static HRESULT WINAPI
JpegDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
288 WICDecodeOptions cacheOptions
)
290 JpegDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
294 TRACE("(%p,%p,%u)\n", iface
, pIStream
, cacheOptions
);
296 EnterCriticalSection(&This
->lock
);
298 if (This
->cinfo_initialized
)
300 LeaveCriticalSection(&This
->lock
);
301 return WINCODEC_ERR_WRONGSTATE
;
304 pjpeg_std_error(&This
->jerr
);
306 This
->jerr
.error_exit
= error_exit_fn
;
307 This
->jerr
.emit_message
= emit_message_fn
;
309 This
->cinfo
.err
= &This
->jerr
;
311 This
->cinfo
.client_data
= jmpbuf
;
315 LeaveCriticalSection(&This
->lock
);
319 pjpeg_CreateDecompress(&This
->cinfo
, JPEG_LIB_VERSION
, sizeof(struct jpeg_decompress_struct
));
321 This
->cinfo_initialized
= TRUE
;
323 This
->stream
= pIStream
;
324 IStream_AddRef(pIStream
);
327 IStream_Seek(This
->stream
, seek
, STREAM_SEEK_SET
, NULL
);
329 This
->source_mgr
.bytes_in_buffer
= 0;
330 This
->source_mgr
.init_source
= source_mgr_init_source
;
331 This
->source_mgr
.fill_input_buffer
= source_mgr_fill_input_buffer
;
332 This
->source_mgr
.skip_input_data
= source_mgr_skip_input_data
;
333 This
->source_mgr
.resync_to_restart
= pjpeg_resync_to_restart
;
334 This
->source_mgr
.term_source
= source_mgr_term_source
;
336 This
->cinfo
.src
= &This
->source_mgr
;
338 ret
= pjpeg_read_header(&This
->cinfo
, TRUE
);
340 if (ret
!= JPEG_HEADER_OK
) {
341 WARN("Jpeg image in stream has bad format, read header returned %d.\n",ret
);
342 LeaveCriticalSection(&This
->lock
);
346 switch (This
->cinfo
.jpeg_color_space
)
349 This
->cinfo
.out_color_space
= JCS_GRAYSCALE
;
353 This
->cinfo
.out_color_space
= JCS_RGB
;
357 This
->cinfo
.out_color_space
= JCS_CMYK
;
360 ERR("Unknown JPEG color space %i\n", This
->cinfo
.jpeg_color_space
);
361 LeaveCriticalSection(&This
->lock
);
365 if (!pjpeg_start_decompress(&This
->cinfo
))
367 ERR("jpeg_start_decompress failed\n");
368 LeaveCriticalSection(&This
->lock
);
372 This
->initialized
= TRUE
;
374 LeaveCriticalSection(&This
->lock
);
379 static HRESULT WINAPI
JpegDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
380 GUID
*pguidContainerFormat
)
382 memcpy(pguidContainerFormat
, &GUID_ContainerFormatJpeg
, sizeof(GUID
));
386 static HRESULT WINAPI
JpegDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
387 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
390 IWICComponentInfo
*compinfo
;
392 TRACE("(%p,%p)\n", iface
, ppIDecoderInfo
);
394 hr
= CreateComponentInfo(&CLSID_WICJpegDecoder
, &compinfo
);
395 if (FAILED(hr
)) return hr
;
397 hr
= IWICComponentInfo_QueryInterface(compinfo
, &IID_IWICBitmapDecoderInfo
,
398 (void**)ppIDecoderInfo
);
400 IWICComponentInfo_Release(compinfo
);
405 static HRESULT WINAPI
JpegDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
406 IWICPalette
*pIPalette
)
408 TRACE("(%p,%p)\n", iface
, pIPalette
);
410 return WINCODEC_ERR_PALETTEUNAVAILABLE
;
413 static HRESULT WINAPI
JpegDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
414 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
416 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
420 static HRESULT WINAPI
JpegDecoder_GetPreview(IWICBitmapDecoder
*iface
,
421 IWICBitmapSource
**ppIBitmapSource
)
423 FIXME("(%p,%p): stub\n", iface
, ppIBitmapSource
);
424 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
427 static HRESULT WINAPI
JpegDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
428 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
430 FIXME("(%p,%u,%p,%p): stub\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
431 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
434 static HRESULT WINAPI
JpegDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
435 IWICBitmapSource
**ppIThumbnail
)
437 FIXME("(%p,%p): stub\n", iface
, ppIThumbnail
);
438 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
441 static HRESULT WINAPI
JpegDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
444 if (!pCount
) return E_INVALIDARG
;
450 static HRESULT WINAPI
JpegDecoder_GetFrame(IWICBitmapDecoder
*iface
,
451 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
453 JpegDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
454 TRACE("(%p,%u,%p)\n", iface
, index
, ppIBitmapFrame
);
456 if (!This
->initialized
) return WINCODEC_ERR_FRAMEMISSING
;
458 if (index
!= 0) return E_INVALIDARG
;
460 IWICBitmapDecoder_AddRef(iface
);
461 *ppIBitmapFrame
= &This
->IWICBitmapFrameDecode_iface
;
466 static const IWICBitmapDecoderVtbl JpegDecoder_Vtbl
= {
467 JpegDecoder_QueryInterface
,
470 JpegDecoder_QueryCapability
,
471 JpegDecoder_Initialize
,
472 JpegDecoder_GetContainerFormat
,
473 JpegDecoder_GetDecoderInfo
,
474 JpegDecoder_CopyPalette
,
475 JpegDecoder_GetMetadataQueryReader
,
476 JpegDecoder_GetPreview
,
477 JpegDecoder_GetColorContexts
,
478 JpegDecoder_GetThumbnail
,
479 JpegDecoder_GetFrameCount
,
483 static HRESULT WINAPI
JpegDecoder_Frame_QueryInterface(IWICBitmapFrameDecode
*iface
, REFIID iid
,
486 JpegDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
488 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
490 if (!ppv
) return E_INVALIDARG
;
492 if (IsEqualIID(&IID_IUnknown
, iid
) ||
493 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
494 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
496 *ppv
= &This
->IWICBitmapFrameDecode_iface
;
501 return E_NOINTERFACE
;
504 IUnknown_AddRef((IUnknown
*)*ppv
);
508 static ULONG WINAPI
JpegDecoder_Frame_AddRef(IWICBitmapFrameDecode
*iface
)
510 JpegDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
511 return IWICBitmapDecoder_AddRef(&This
->IWICBitmapDecoder_iface
);
514 static ULONG WINAPI
JpegDecoder_Frame_Release(IWICBitmapFrameDecode
*iface
)
516 JpegDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
517 return IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
520 static HRESULT WINAPI
JpegDecoder_Frame_GetSize(IWICBitmapFrameDecode
*iface
,
521 UINT
*puiWidth
, UINT
*puiHeight
)
523 JpegDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
524 *puiWidth
= This
->cinfo
.output_width
;
525 *puiHeight
= This
->cinfo
.output_height
;
526 TRACE("(%p)->(%u,%u)\n", iface
, *puiWidth
, *puiHeight
);
530 static HRESULT WINAPI
JpegDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
531 WICPixelFormatGUID
*pPixelFormat
)
533 JpegDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
534 TRACE("(%p,%p)\n", iface
, pPixelFormat
);
535 if (This
->cinfo
.out_color_space
== JCS_RGB
)
536 memcpy(pPixelFormat
, &GUID_WICPixelFormat24bppBGR
, sizeof(GUID
));
537 else if (This
->cinfo
.out_color_space
== JCS_CMYK
)
538 memcpy(pPixelFormat
, &GUID_WICPixelFormat32bppCMYK
, sizeof(GUID
));
539 else /* This->cinfo.out_color_space == JCS_GRAYSCALE */
540 memcpy(pPixelFormat
, &GUID_WICPixelFormat8bppGray
, sizeof(GUID
));
544 static HRESULT WINAPI
JpegDecoder_Frame_GetResolution(IWICBitmapFrameDecode
*iface
,
545 double *pDpiX
, double *pDpiY
)
547 JpegDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
549 EnterCriticalSection(&This
->lock
);
551 switch (This
->cinfo
.density_unit
)
553 case 2: /* pixels per centimeter */
554 *pDpiX
= This
->cinfo
.X_density
* 2.54;
555 *pDpiY
= This
->cinfo
.Y_density
* 2.54;
558 case 1: /* pixels per inch */
559 *pDpiX
= This
->cinfo
.X_density
;
560 *pDpiY
= This
->cinfo
.Y_density
;
563 case 0: /* unknown */
570 LeaveCriticalSection(&This
->lock
);
572 TRACE("(%p)->(%0.2f,%0.2f)\n", iface
, *pDpiX
, *pDpiY
);
577 static HRESULT WINAPI
JpegDecoder_Frame_CopyPalette(IWICBitmapFrameDecode
*iface
,
578 IWICPalette
*pIPalette
)
580 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
584 static HRESULT WINAPI
JpegDecoder_Frame_CopyPixels(IWICBitmapFrameDecode
*iface
,
585 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
587 JpegDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
594 TRACE("(%p,%p,%u,%u,%p)\n", iface
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
600 rect
.Width
= This
->cinfo
.output_width
;
601 rect
.Height
= This
->cinfo
.output_height
;
606 if (prc
->X
< 0 || prc
->Y
< 0 || prc
->X
+prc
->Width
> This
->cinfo
.output_width
||
607 prc
->Y
+prc
->Height
> This
->cinfo
.output_height
)
611 if (This
->cinfo
.out_color_space
== JCS_GRAYSCALE
) bpp
= 8;
612 else if (This
->cinfo
.out_color_space
== JCS_CMYK
) bpp
= 32;
615 stride
= bpp
* This
->cinfo
.output_width
;
616 data_size
= stride
* This
->cinfo
.output_height
;
618 max_row_needed
= prc
->Y
+ prc
->Height
;
619 if (max_row_needed
> This
->cinfo
.output_height
) return E_INVALIDARG
;
621 EnterCriticalSection(&This
->lock
);
623 if (!This
->image_data
)
625 This
->image_data
= HeapAlloc(GetProcessHeap(), 0, data_size
);
626 if (!This
->image_data
)
628 LeaveCriticalSection(&This
->lock
);
629 return E_OUTOFMEMORY
;
633 This
->cinfo
.client_data
= jmpbuf
;
637 LeaveCriticalSection(&This
->lock
);
641 while (max_row_needed
> This
->cinfo
.output_scanline
)
643 UINT first_scanline
= This
->cinfo
.output_scanline
;
645 JSAMPROW out_rows
[4];
649 max_rows
= min(This
->cinfo
.output_height
-first_scanline
, 4);
650 for (i
=0; i
<max_rows
; i
++)
651 out_rows
[i
] = This
->image_data
+ stride
* (first_scanline
+i
);
653 ret
= pjpeg_read_scanlines(&This
->cinfo
, out_rows
, max_rows
);
657 ERR("read_scanlines failed\n");
658 LeaveCriticalSection(&This
->lock
);
664 /* libjpeg gives us RGB data and we want BGR, so byteswap the data */
665 reverse_bgr8(3, This
->image_data
+ stride
* first_scanline
,
666 This
->cinfo
.output_width
, This
->cinfo
.output_scanline
- first_scanline
,
670 if (This
->cinfo
.out_color_space
== JCS_CMYK
&& This
->cinfo
.saw_Adobe_marker
)
671 /* Adobe JPEG's have inverted CMYK data. */
672 for (i
=0; i
<data_size
; i
++)
673 This
->image_data
[i
] ^= 0xff;
676 LeaveCriticalSection(&This
->lock
);
678 return copy_pixels(bpp
, This
->image_data
,
679 This
->cinfo
.output_width
, This
->cinfo
.output_height
, stride
,
680 prc
, cbStride
, cbBufferSize
, pbBuffer
);
683 static HRESULT WINAPI
JpegDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
684 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
686 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
687 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
690 static HRESULT WINAPI
JpegDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode
*iface
,
691 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
693 FIXME("(%p,%u,%p,%p): stub\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
694 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
697 static HRESULT WINAPI
JpegDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode
*iface
,
698 IWICBitmapSource
**ppIThumbnail
)
700 FIXME("(%p,%p): stub\n", iface
, ppIThumbnail
);
701 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
704 static const IWICBitmapFrameDecodeVtbl JpegDecoder_Frame_Vtbl
= {
705 JpegDecoder_Frame_QueryInterface
,
706 JpegDecoder_Frame_AddRef
,
707 JpegDecoder_Frame_Release
,
708 JpegDecoder_Frame_GetSize
,
709 JpegDecoder_Frame_GetPixelFormat
,
710 JpegDecoder_Frame_GetResolution
,
711 JpegDecoder_Frame_CopyPalette
,
712 JpegDecoder_Frame_CopyPixels
,
713 JpegDecoder_Frame_GetMetadataQueryReader
,
714 JpegDecoder_Frame_GetColorContexts
,
715 JpegDecoder_Frame_GetThumbnail
718 HRESULT
JpegDecoder_CreateInstance(REFIID iid
, void** ppv
)
723 TRACE("(%s,%p)\n", debugstr_guid(iid
), ppv
);
725 if (!libjpeg_handle
&& !load_libjpeg())
727 ERR("Failed reading JPEG because unable to find %s\n", SONAME_LIBJPEG
);
733 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(JpegDecoder
));
734 if (!This
) return E_OUTOFMEMORY
;
736 This
->IWICBitmapDecoder_iface
.lpVtbl
= &JpegDecoder_Vtbl
;
737 This
->IWICBitmapFrameDecode_iface
.lpVtbl
= &JpegDecoder_Frame_Vtbl
;
739 This
->initialized
= FALSE
;
740 This
->cinfo_initialized
= FALSE
;
742 This
->image_data
= NULL
;
743 InitializeCriticalSection(&This
->lock
);
744 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": JpegDecoder.lock");
746 ret
= IWICBitmapDecoder_QueryInterface(&This
->IWICBitmapDecoder_iface
, iid
, ppv
);
747 IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
752 typedef struct jpeg_compress_format
{
753 const WICPixelFormatGUID
*guid
;
756 J_COLOR_SPACE color_space
;
758 } jpeg_compress_format
;
760 static const jpeg_compress_format compress_formats
[] = {
761 { &GUID_WICPixelFormat24bppBGR
, 24, 3, JCS_RGB
, 1 },
762 { &GUID_WICPixelFormat32bppCMYK
, 32, 4, JCS_CMYK
},
763 { &GUID_WICPixelFormat8bppGray
, 8, 1, JCS_GRAYSCALE
},
767 typedef struct JpegEncoder
{
768 IWICBitmapEncoder IWICBitmapEncoder_iface
;
769 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface
;
771 struct jpeg_compress_struct cinfo
;
772 struct jpeg_error_mgr jerr
;
773 struct jpeg_destination_mgr dest_mgr
;
776 BOOL frame_initialized
;
777 BOOL started_compress
;
779 BOOL frame_committed
;
783 const jpeg_compress_format
*format
;
785 CRITICAL_SECTION lock
;
786 BYTE dest_buffer
[1024];
789 static inline JpegEncoder
*impl_from_IWICBitmapEncoder(IWICBitmapEncoder
*iface
)
791 return CONTAINING_RECORD(iface
, JpegEncoder
, IWICBitmapEncoder_iface
);
794 static inline JpegEncoder
*impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode
*iface
)
796 return CONTAINING_RECORD(iface
, JpegEncoder
, IWICBitmapFrameEncode_iface
);
799 static inline JpegEncoder
*encoder_from_compress(j_compress_ptr compress
)
801 return CONTAINING_RECORD(compress
, JpegEncoder
, cinfo
);
804 static void dest_mgr_init_destination(j_compress_ptr cinfo
)
806 JpegEncoder
*This
= encoder_from_compress(cinfo
);
808 This
->dest_mgr
.next_output_byte
= This
->dest_buffer
;
809 This
->dest_mgr
.free_in_buffer
= sizeof(This
->dest_buffer
);
812 static jpeg_boolean
dest_mgr_empty_output_buffer(j_compress_ptr cinfo
)
814 JpegEncoder
*This
= encoder_from_compress(cinfo
);
818 hr
= IStream_Write(This
->stream
, This
->dest_buffer
,
819 sizeof(This
->dest_buffer
), &byteswritten
);
821 if (hr
!= S_OK
|| byteswritten
== 0)
823 ERR("Failed writing data, hr=%x\n", hr
);
827 This
->dest_mgr
.next_output_byte
= This
->dest_buffer
;
828 This
->dest_mgr
.free_in_buffer
= sizeof(This
->dest_buffer
);
832 static void dest_mgr_term_destination(j_compress_ptr cinfo
)
834 JpegEncoder
*This
= encoder_from_compress(cinfo
);
838 if (This
->dest_mgr
.free_in_buffer
!= sizeof(This
->dest_buffer
))
840 hr
= IStream_Write(This
->stream
, This
->dest_buffer
,
841 sizeof(This
->dest_buffer
) - This
->dest_mgr
.free_in_buffer
, &byteswritten
);
843 if (hr
!= S_OK
|| byteswritten
== 0)
844 ERR("Failed writing data, hr=%x\n", hr
);
848 static HRESULT WINAPI
JpegEncoder_Frame_QueryInterface(IWICBitmapFrameEncode
*iface
, REFIID iid
,
851 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
852 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
854 if (!ppv
) return E_INVALIDARG
;
856 if (IsEqualIID(&IID_IUnknown
, iid
) ||
857 IsEqualIID(&IID_IWICBitmapFrameEncode
, iid
))
859 *ppv
= &This
->IWICBitmapFrameEncode_iface
;
864 return E_NOINTERFACE
;
867 IUnknown_AddRef((IUnknown
*)*ppv
);
871 static ULONG WINAPI
JpegEncoder_Frame_AddRef(IWICBitmapFrameEncode
*iface
)
873 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
874 return IWICBitmapEncoder_AddRef(&This
->IWICBitmapEncoder_iface
);
877 static ULONG WINAPI
JpegEncoder_Frame_Release(IWICBitmapFrameEncode
*iface
)
879 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
880 return IWICBitmapEncoder_Release(&This
->IWICBitmapEncoder_iface
);
883 static HRESULT WINAPI
JpegEncoder_Frame_Initialize(IWICBitmapFrameEncode
*iface
,
884 IPropertyBag2
*pIEncoderOptions
)
886 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
887 TRACE("(%p,%p)\n", iface
, pIEncoderOptions
);
889 EnterCriticalSection(&This
->lock
);
891 if (This
->frame_initialized
)
893 LeaveCriticalSection(&This
->lock
);
894 return WINCODEC_ERR_WRONGSTATE
;
897 This
->frame_initialized
= TRUE
;
899 LeaveCriticalSection(&This
->lock
);
904 static HRESULT WINAPI
JpegEncoder_Frame_SetSize(IWICBitmapFrameEncode
*iface
,
905 UINT uiWidth
, UINT uiHeight
)
907 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
908 TRACE("(%p,%u,%u)\n", iface
, uiWidth
, uiHeight
);
910 EnterCriticalSection(&This
->lock
);
912 if (!This
->frame_initialized
|| This
->started_compress
)
914 LeaveCriticalSection(&This
->lock
);
915 return WINCODEC_ERR_WRONGSTATE
;
918 This
->width
= uiWidth
;
919 This
->height
= uiHeight
;
921 LeaveCriticalSection(&This
->lock
);
926 static HRESULT WINAPI
JpegEncoder_Frame_SetResolution(IWICBitmapFrameEncode
*iface
,
927 double dpiX
, double dpiY
)
929 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
930 TRACE("(%p,%0.2f,%0.2f)\n", iface
, dpiX
, dpiY
);
932 EnterCriticalSection(&This
->lock
);
934 if (!This
->frame_initialized
|| This
->started_compress
)
936 LeaveCriticalSection(&This
->lock
);
937 return WINCODEC_ERR_WRONGSTATE
;
943 LeaveCriticalSection(&This
->lock
);
948 static HRESULT WINAPI
JpegEncoder_Frame_SetPixelFormat(IWICBitmapFrameEncode
*iface
,
949 WICPixelFormatGUID
*pPixelFormat
)
951 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
953 TRACE("(%p,%s)\n", iface
, debugstr_guid(pPixelFormat
));
955 EnterCriticalSection(&This
->lock
);
957 if (!This
->frame_initialized
|| This
->started_compress
)
959 LeaveCriticalSection(&This
->lock
);
960 return WINCODEC_ERR_WRONGSTATE
;
963 for (i
=0; compress_formats
[i
].guid
; i
++)
965 if (memcmp(compress_formats
[i
].guid
, pPixelFormat
, sizeof(GUID
)) == 0)
969 if (!compress_formats
[i
].guid
) i
= 0;
971 This
->format
= &compress_formats
[i
];
972 memcpy(pPixelFormat
, This
->format
->guid
, sizeof(GUID
));
974 LeaveCriticalSection(&This
->lock
);
979 static HRESULT WINAPI
JpegEncoder_Frame_SetColorContexts(IWICBitmapFrameEncode
*iface
,
980 UINT cCount
, IWICColorContext
**ppIColorContext
)
982 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
986 static HRESULT WINAPI
JpegEncoder_Frame_SetPalette(IWICBitmapFrameEncode
*iface
,
987 IWICPalette
*pIPalette
)
989 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
990 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
993 static HRESULT WINAPI
JpegEncoder_Frame_SetThumbnail(IWICBitmapFrameEncode
*iface
,
994 IWICBitmapSource
*pIThumbnail
)
996 FIXME("(%p,%p): stub\n", iface
, pIThumbnail
);
997 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1000 static HRESULT WINAPI
JpegEncoder_Frame_WritePixels(IWICBitmapFrameEncode
*iface
,
1001 UINT lineCount
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbPixels
)
1003 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1005 BYTE
*swapped_data
= NULL
, *current_row
;
1008 TRACE("(%p,%u,%u,%u,%p)\n", iface
, lineCount
, cbStride
, cbBufferSize
, pbPixels
);
1010 EnterCriticalSection(&This
->lock
);
1012 if (!This
->frame_initialized
|| !This
->width
|| !This
->height
|| !This
->format
)
1014 LeaveCriticalSection(&This
->lock
);
1015 return WINCODEC_ERR_WRONGSTATE
;
1018 if (lineCount
== 0 || lineCount
+ This
->lines_written
> This
->height
)
1020 LeaveCriticalSection(&This
->lock
);
1021 return E_INVALIDARG
;
1024 /* set up setjmp/longjmp error handling */
1027 LeaveCriticalSection(&This
->lock
);
1028 HeapFree(GetProcessHeap(), 0, swapped_data
);
1031 This
->cinfo
.client_data
= jmpbuf
;
1033 if (!This
->started_compress
)
1035 This
->cinfo
.image_width
= This
->width
;
1036 This
->cinfo
.image_height
= This
->height
;
1037 This
->cinfo
.input_components
= This
->format
->num_components
;
1038 This
->cinfo
.in_color_space
= This
->format
->color_space
;
1040 pjpeg_set_defaults(&This
->cinfo
);
1042 if (This
->xres
!= 0.0 && This
->yres
!= 0.0)
1044 This
->cinfo
.density_unit
= 1; /* dots per inch */
1045 This
->cinfo
.X_density
= This
->xres
;
1046 This
->cinfo
.Y_density
= This
->yres
;
1049 pjpeg_start_compress(&This
->cinfo
, TRUE
);
1051 This
->started_compress
= TRUE
;
1054 row_size
= This
->format
->bpp
/ 8 * This
->width
;
1056 if (This
->format
->swap_rgb
)
1058 swapped_data
= HeapAlloc(GetProcessHeap(), 0, row_size
);
1061 LeaveCriticalSection(&This
->lock
);
1062 return E_OUTOFMEMORY
;
1066 for (line
=0; line
< lineCount
; line
++)
1068 if (This
->format
->swap_rgb
)
1072 memcpy(swapped_data
, pbPixels
+ (cbStride
* line
), row_size
);
1074 for (x
=0; x
< This
->width
; x
++)
1078 b
= swapped_data
[x
*3];
1079 swapped_data
[x
*3] = swapped_data
[x
*3+2];
1080 swapped_data
[x
*3+2] = b
;
1083 current_row
= swapped_data
;
1086 current_row
= pbPixels
+ (cbStride
* line
);
1088 if (!pjpeg_write_scanlines(&This
->cinfo
, ¤t_row
, 1))
1090 ERR("failed writing scanlines\n");
1091 LeaveCriticalSection(&This
->lock
);
1092 HeapFree(GetProcessHeap(), 0, swapped_data
);
1096 This
->lines_written
++;
1099 LeaveCriticalSection(&This
->lock
);
1100 HeapFree(GetProcessHeap(), 0, swapped_data
);
1105 static HRESULT WINAPI
JpegEncoder_Frame_WriteSource(IWICBitmapFrameEncode
*iface
,
1106 IWICBitmapSource
*pIBitmapSource
, WICRect
*prc
)
1108 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1111 WICPixelFormatGUID guid
;
1114 TRACE("(%p,%p,%p)\n", iface
, pIBitmapSource
, prc
);
1116 if (!This
->frame_initialized
|| !This
->width
|| !This
->height
)
1117 return WINCODEC_ERR_WRONGSTATE
;
1121 hr
= IWICBitmapSource_GetPixelFormat(pIBitmapSource
, &guid
);
1122 if (FAILED(hr
)) return hr
;
1123 hr
= IWICBitmapFrameEncode_SetPixelFormat(iface
, &guid
);
1124 if (FAILED(hr
)) return hr
;
1127 hr
= IWICBitmapSource_GetPixelFormat(pIBitmapSource
, &guid
);
1128 if (FAILED(hr
)) return hr
;
1129 if (memcmp(&guid
, This
->format
->guid
, sizeof(GUID
)) != 0)
1131 /* FIXME: should use WICConvertBitmapSource to convert */
1132 ERR("format %s unsupported\n", debugstr_guid(&guid
));
1136 if (This
->xres
== 0.0 || This
->yres
== 0.0)
1139 hr
= IWICBitmapSource_GetResolution(pIBitmapSource
, &xres
, &yres
);
1140 if (FAILED(hr
)) return hr
;
1141 hr
= IWICBitmapFrameEncode_SetResolution(iface
, xres
, yres
);
1142 if (FAILED(hr
)) return hr
;
1148 hr
= IWICBitmapSource_GetSize(pIBitmapSource
, &width
, &height
);
1149 if (FAILED(hr
)) return hr
;
1157 if (prc
->Width
!= This
->width
) return E_INVALIDARG
;
1159 stride
= (This
->format
->bpp
* This
->width
+ 7)/8;
1161 pixeldata
= HeapAlloc(GetProcessHeap(), 0, stride
* prc
->Height
);
1162 if (!pixeldata
) return E_OUTOFMEMORY
;
1164 hr
= IWICBitmapSource_CopyPixels(pIBitmapSource
, prc
, stride
,
1165 stride
*prc
->Height
, pixeldata
);
1169 hr
= IWICBitmapFrameEncode_WritePixels(iface
, prc
->Height
, stride
,
1170 stride
*prc
->Height
, pixeldata
);
1173 HeapFree(GetProcessHeap(), 0, pixeldata
);
1178 static HRESULT WINAPI
JpegEncoder_Frame_Commit(IWICBitmapFrameEncode
*iface
)
1180 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1182 TRACE("(%p)\n", iface
);
1184 EnterCriticalSection(&This
->lock
);
1186 if (!This
->started_compress
|| This
->lines_written
!= This
->height
|| This
->frame_committed
)
1188 LeaveCriticalSection(&This
->lock
);
1189 return WINCODEC_ERR_WRONGSTATE
;
1192 /* set up setjmp/longjmp error handling */
1195 LeaveCriticalSection(&This
->lock
);
1198 This
->cinfo
.client_data
= jmpbuf
;
1200 pjpeg_finish_compress(&This
->cinfo
);
1202 This
->frame_committed
= TRUE
;
1204 LeaveCriticalSection(&This
->lock
);
1209 static HRESULT WINAPI
JpegEncoder_Frame_GetMetadataQueryWriter(IWICBitmapFrameEncode
*iface
,
1210 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
1212 FIXME("(%p, %p): stub\n", iface
, ppIMetadataQueryWriter
);
1216 static const IWICBitmapFrameEncodeVtbl JpegEncoder_FrameVtbl
= {
1217 JpegEncoder_Frame_QueryInterface
,
1218 JpegEncoder_Frame_AddRef
,
1219 JpegEncoder_Frame_Release
,
1220 JpegEncoder_Frame_Initialize
,
1221 JpegEncoder_Frame_SetSize
,
1222 JpegEncoder_Frame_SetResolution
,
1223 JpegEncoder_Frame_SetPixelFormat
,
1224 JpegEncoder_Frame_SetColorContexts
,
1225 JpegEncoder_Frame_SetPalette
,
1226 JpegEncoder_Frame_SetThumbnail
,
1227 JpegEncoder_Frame_WritePixels
,
1228 JpegEncoder_Frame_WriteSource
,
1229 JpegEncoder_Frame_Commit
,
1230 JpegEncoder_Frame_GetMetadataQueryWriter
1233 static HRESULT WINAPI
JpegEncoder_QueryInterface(IWICBitmapEncoder
*iface
, REFIID iid
,
1236 JpegEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1237 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
1239 if (!ppv
) return E_INVALIDARG
;
1241 if (IsEqualIID(&IID_IUnknown
, iid
) ||
1242 IsEqualIID(&IID_IWICBitmapEncoder
, iid
))
1244 *ppv
= &This
->IWICBitmapEncoder_iface
;
1249 return E_NOINTERFACE
;
1252 IUnknown_AddRef((IUnknown
*)*ppv
);
1256 static ULONG WINAPI
JpegEncoder_AddRef(IWICBitmapEncoder
*iface
)
1258 JpegEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1259 ULONG ref
= InterlockedIncrement(&This
->ref
);
1261 TRACE("(%p) refcount=%u\n", iface
, ref
);
1266 static ULONG WINAPI
JpegEncoder_Release(IWICBitmapEncoder
*iface
)
1268 JpegEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1269 ULONG ref
= InterlockedDecrement(&This
->ref
);
1271 TRACE("(%p) refcount=%u\n", iface
, ref
);
1275 This
->lock
.DebugInfo
->Spare
[0] = 0;
1276 DeleteCriticalSection(&This
->lock
);
1277 if (This
->initialized
) pjpeg_destroy_compress(&This
->cinfo
);
1278 if (This
->stream
) IStream_Release(This
->stream
);
1279 HeapFree(GetProcessHeap(), 0, This
);
1285 static HRESULT WINAPI
JpegEncoder_Initialize(IWICBitmapEncoder
*iface
,
1286 IStream
*pIStream
, WICBitmapEncoderCacheOption cacheOption
)
1288 JpegEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1291 TRACE("(%p,%p,%u)\n", iface
, pIStream
, cacheOption
);
1293 EnterCriticalSection(&This
->lock
);
1295 if (This
->initialized
)
1297 LeaveCriticalSection(&This
->lock
);
1298 return WINCODEC_ERR_WRONGSTATE
;
1301 pjpeg_std_error(&This
->jerr
);
1303 This
->jerr
.error_exit
= error_exit_fn
;
1304 This
->jerr
.emit_message
= emit_message_fn
;
1306 This
->cinfo
.err
= &This
->jerr
;
1308 This
->cinfo
.client_data
= jmpbuf
;
1312 LeaveCriticalSection(&This
->lock
);
1316 pjpeg_CreateCompress(&This
->cinfo
, JPEG_LIB_VERSION
, sizeof(struct jpeg_compress_struct
));
1318 This
->stream
= pIStream
;
1319 IStream_AddRef(pIStream
);
1321 This
->dest_mgr
.next_output_byte
= This
->dest_buffer
;
1322 This
->dest_mgr
.free_in_buffer
= sizeof(This
->dest_buffer
);
1324 This
->dest_mgr
.init_destination
= dest_mgr_init_destination
;
1325 This
->dest_mgr
.empty_output_buffer
= dest_mgr_empty_output_buffer
;
1326 This
->dest_mgr
.term_destination
= dest_mgr_term_destination
;
1328 This
->cinfo
.dest
= &This
->dest_mgr
;
1330 This
->initialized
= TRUE
;
1332 LeaveCriticalSection(&This
->lock
);
1337 static HRESULT WINAPI
JpegEncoder_GetContainerFormat(IWICBitmapEncoder
*iface
,
1338 GUID
*pguidContainerFormat
)
1340 FIXME("(%p,%s): stub\n", iface
, debugstr_guid(pguidContainerFormat
));
1344 static HRESULT WINAPI
JpegEncoder_GetEncoderInfo(IWICBitmapEncoder
*iface
,
1345 IWICBitmapEncoderInfo
**ppIEncoderInfo
)
1347 FIXME("(%p,%p): stub\n", iface
, ppIEncoderInfo
);
1351 static HRESULT WINAPI
JpegEncoder_SetColorContexts(IWICBitmapEncoder
*iface
,
1352 UINT cCount
, IWICColorContext
**ppIColorContext
)
1354 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
1358 static HRESULT WINAPI
JpegEncoder_SetPalette(IWICBitmapEncoder
*iface
, IWICPalette
*pIPalette
)
1360 TRACE("(%p,%p)\n", iface
, pIPalette
);
1361 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1364 static HRESULT WINAPI
JpegEncoder_SetThumbnail(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIThumbnail
)
1366 TRACE("(%p,%p)\n", iface
, pIThumbnail
);
1367 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1370 static HRESULT WINAPI
JpegEncoder_SetPreview(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIPreview
)
1372 TRACE("(%p,%p)\n", iface
, pIPreview
);
1373 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1376 static HRESULT WINAPI
JpegEncoder_CreateNewFrame(IWICBitmapEncoder
*iface
,
1377 IWICBitmapFrameEncode
**ppIFrameEncode
, IPropertyBag2
**ppIEncoderOptions
)
1379 JpegEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1382 TRACE("(%p,%p,%p)\n", iface
, ppIFrameEncode
, ppIEncoderOptions
);
1384 EnterCriticalSection(&This
->lock
);
1386 if (This
->frame_count
!= 0)
1388 LeaveCriticalSection(&This
->lock
);
1389 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1392 if (!This
->initialized
)
1394 LeaveCriticalSection(&This
->lock
);
1395 return WINCODEC_ERR_NOTINITIALIZED
;
1398 hr
= CreatePropertyBag2(NULL
, 0, ppIEncoderOptions
);
1401 LeaveCriticalSection(&This
->lock
);
1405 This
->frame_count
= 1;
1407 LeaveCriticalSection(&This
->lock
);
1409 IWICBitmapEncoder_AddRef(iface
);
1410 *ppIFrameEncode
= &This
->IWICBitmapFrameEncode_iface
;
1415 static HRESULT WINAPI
JpegEncoder_Commit(IWICBitmapEncoder
*iface
)
1417 JpegEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1418 TRACE("(%p)\n", iface
);
1420 EnterCriticalSection(&This
->lock
);
1422 if (!This
->frame_committed
|| This
->committed
)
1424 LeaveCriticalSection(&This
->lock
);
1425 return WINCODEC_ERR_WRONGSTATE
;
1428 This
->committed
= TRUE
;
1430 LeaveCriticalSection(&This
->lock
);
1435 static HRESULT WINAPI
JpegEncoder_GetMetadataQueryWriter(IWICBitmapEncoder
*iface
,
1436 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
1438 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryWriter
);
1442 static const IWICBitmapEncoderVtbl JpegEncoder_Vtbl
= {
1443 JpegEncoder_QueryInterface
,
1445 JpegEncoder_Release
,
1446 JpegEncoder_Initialize
,
1447 JpegEncoder_GetContainerFormat
,
1448 JpegEncoder_GetEncoderInfo
,
1449 JpegEncoder_SetColorContexts
,
1450 JpegEncoder_SetPalette
,
1451 JpegEncoder_SetThumbnail
,
1452 JpegEncoder_SetPreview
,
1453 JpegEncoder_CreateNewFrame
,
1455 JpegEncoder_GetMetadataQueryWriter
1458 HRESULT
JpegEncoder_CreateInstance(REFIID iid
, void** ppv
)
1463 TRACE("(%s,%p)\n", debugstr_guid(iid
), ppv
);
1467 if (!libjpeg_handle
&& !load_libjpeg())
1469 ERR("Failed writing JPEG because unable to find %s\n",SONAME_LIBJPEG
);
1473 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(JpegEncoder
));
1474 if (!This
) return E_OUTOFMEMORY
;
1476 This
->IWICBitmapEncoder_iface
.lpVtbl
= &JpegEncoder_Vtbl
;
1477 This
->IWICBitmapFrameEncode_iface
.lpVtbl
= &JpegEncoder_FrameVtbl
;
1479 This
->initialized
= FALSE
;
1480 This
->frame_count
= 0;
1481 This
->frame_initialized
= FALSE
;
1482 This
->started_compress
= FALSE
;
1483 This
->lines_written
= 0;
1484 This
->frame_committed
= FALSE
;
1485 This
->committed
= FALSE
;
1486 This
->width
= This
->height
= 0;
1487 This
->xres
= This
->yres
= 0.0;
1488 This
->format
= NULL
;
1489 This
->stream
= NULL
;
1490 InitializeCriticalSection(&This
->lock
);
1491 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": JpegEncoder.lock");
1493 ret
= IWICBitmapEncoder_QueryInterface(&This
->IWICBitmapEncoder_iface
, iid
, ppv
);
1494 IWICBitmapEncoder_Release(&This
->IWICBitmapEncoder_iface
);
1499 #else /* !defined(SONAME_LIBJPEG) */
1501 HRESULT
JpegDecoder_CreateInstance(REFIID iid
, void** ppv
)
1503 ERR("Trying to load JPEG picture, but JPEG support is not compiled in.\n");
1507 HRESULT
JpegEncoder_CreateInstance(REFIID iid
, void** ppv
)
1509 ERR("Trying to save JPEG picture, but JPEG support is not compiled in.\n");