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 static const WCHAR wszImageQuality
[] = {'I','m','a','g','e','Q','u','a','l','i','t','y',0};
64 static const WCHAR wszBitmapTransform
[] = {'B','i','t','m','a','p','T','r','a','n','s','f','o','r','m',0};
65 static const WCHAR wszLuminance
[] = {'L','u','m','i','n','a','n','c','e',0};
66 static const WCHAR wszChrominance
[] = {'C','h','r','o','m','i','n','a','n','c','e',0};
67 static const WCHAR wszJpegYCrCbSubsampling
[] = {'J','p','e','g','Y','C','r','C','b','S','u','b','s','a','m','p','l','i','n','g',0};
68 static const WCHAR wszSuppressApp0
[] = {'S','u','p','p','r','e','s','s','A','p','p','0',0};
70 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
71 MAKE_FUNCPTR(jpeg_CreateCompress
);
72 MAKE_FUNCPTR(jpeg_CreateDecompress
);
73 MAKE_FUNCPTR(jpeg_destroy_compress
);
74 MAKE_FUNCPTR(jpeg_destroy_decompress
);
75 MAKE_FUNCPTR(jpeg_finish_compress
);
76 MAKE_FUNCPTR(jpeg_read_header
);
77 MAKE_FUNCPTR(jpeg_read_scanlines
);
78 MAKE_FUNCPTR(jpeg_resync_to_restart
);
79 MAKE_FUNCPTR(jpeg_set_defaults
);
80 MAKE_FUNCPTR(jpeg_start_compress
);
81 MAKE_FUNCPTR(jpeg_start_decompress
);
82 MAKE_FUNCPTR(jpeg_std_error
);
83 MAKE_FUNCPTR(jpeg_write_scanlines
);
86 static void *load_libjpeg(void)
88 if((libjpeg_handle
= wine_dlopen(SONAME_LIBJPEG
, RTLD_NOW
, NULL
, 0)) != NULL
) {
90 #define LOAD_FUNCPTR(f) \
91 if((p##f = wine_dlsym(libjpeg_handle, #f, NULL, 0)) == NULL) { \
92 libjpeg_handle = NULL; \
96 LOAD_FUNCPTR(jpeg_CreateCompress
);
97 LOAD_FUNCPTR(jpeg_CreateDecompress
);
98 LOAD_FUNCPTR(jpeg_destroy_compress
);
99 LOAD_FUNCPTR(jpeg_destroy_decompress
);
100 LOAD_FUNCPTR(jpeg_finish_compress
);
101 LOAD_FUNCPTR(jpeg_read_header
);
102 LOAD_FUNCPTR(jpeg_read_scanlines
);
103 LOAD_FUNCPTR(jpeg_resync_to_restart
);
104 LOAD_FUNCPTR(jpeg_set_defaults
);
105 LOAD_FUNCPTR(jpeg_start_compress
);
106 LOAD_FUNCPTR(jpeg_start_decompress
);
107 LOAD_FUNCPTR(jpeg_std_error
);
108 LOAD_FUNCPTR(jpeg_write_scanlines
);
111 return libjpeg_handle
;
114 static void error_exit_fn(j_common_ptr cinfo
)
116 char message
[JMSG_LENGTH_MAX
];
119 cinfo
->err
->format_message(cinfo
, message
);
120 ERR_(jpeg
)("%s\n", message
);
122 longjmp(*(jmp_buf*)cinfo
->client_data
, 1);
125 static void emit_message_fn(j_common_ptr cinfo
, int msg_level
)
127 char message
[JMSG_LENGTH_MAX
];
129 if (msg_level
< 0 && ERR_ON(jpeg
))
131 cinfo
->err
->format_message(cinfo
, message
);
132 ERR_(jpeg
)("%s\n", message
);
134 else if (msg_level
== 0 && WARN_ON(jpeg
))
136 cinfo
->err
->format_message(cinfo
, message
);
137 WARN_(jpeg
)("%s\n", message
);
139 else if (msg_level
> 0 && TRACE_ON(jpeg
))
141 cinfo
->err
->format_message(cinfo
, message
);
142 TRACE_(jpeg
)("%s\n", message
);
147 IWICBitmapDecoder IWICBitmapDecoder_iface
;
148 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface
;
149 IWICMetadataBlockReader IWICMetadataBlockReader_iface
;
152 BOOL cinfo_initialized
;
154 struct jpeg_decompress_struct cinfo
;
155 struct jpeg_error_mgr jerr
;
156 struct jpeg_source_mgr source_mgr
;
157 BYTE source_buffer
[1024];
159 CRITICAL_SECTION lock
;
162 static inline JpegDecoder
*impl_from_IWICBitmapDecoder(IWICBitmapDecoder
*iface
)
164 return CONTAINING_RECORD(iface
, JpegDecoder
, IWICBitmapDecoder_iface
);
167 static inline JpegDecoder
*impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode
*iface
)
169 return CONTAINING_RECORD(iface
, JpegDecoder
, IWICBitmapFrameDecode_iface
);
172 static inline JpegDecoder
*decoder_from_decompress(j_decompress_ptr decompress
)
174 return CONTAINING_RECORD(decompress
, JpegDecoder
, cinfo
);
177 static inline JpegDecoder
*impl_from_IWICMetadataBlockReader(IWICMetadataBlockReader
*iface
)
179 return CONTAINING_RECORD(iface
, JpegDecoder
, IWICMetadataBlockReader_iface
);
182 static HRESULT WINAPI
JpegDecoder_QueryInterface(IWICBitmapDecoder
*iface
, REFIID iid
,
185 JpegDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
186 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
188 if (!ppv
) return E_INVALIDARG
;
190 if (IsEqualIID(&IID_IUnknown
, iid
) ||
191 IsEqualIID(&IID_IWICBitmapDecoder
, iid
))
193 *ppv
= &This
->IWICBitmapDecoder_iface
;
198 return E_NOINTERFACE
;
201 IUnknown_AddRef((IUnknown
*)*ppv
);
205 static ULONG WINAPI
JpegDecoder_AddRef(IWICBitmapDecoder
*iface
)
207 JpegDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
208 ULONG ref
= InterlockedIncrement(&This
->ref
);
210 TRACE("(%p) refcount=%u\n", iface
, ref
);
215 static ULONG WINAPI
JpegDecoder_Release(IWICBitmapDecoder
*iface
)
217 JpegDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
218 ULONG ref
= InterlockedDecrement(&This
->ref
);
220 TRACE("(%p) refcount=%u\n", iface
, ref
);
224 This
->lock
.DebugInfo
->Spare
[0] = 0;
225 DeleteCriticalSection(&This
->lock
);
226 if (This
->cinfo_initialized
) pjpeg_destroy_decompress(&This
->cinfo
);
227 if (This
->stream
) IStream_Release(This
->stream
);
228 HeapFree(GetProcessHeap(), 0, This
->image_data
);
229 HeapFree(GetProcessHeap(), 0, This
);
235 static HRESULT WINAPI
JpegDecoder_QueryCapability(IWICBitmapDecoder
*iface
, IStream
*stream
,
240 TRACE("(%p,%p,%p)\n", iface
, stream
, capability
);
242 if (!stream
|| !capability
) return E_INVALIDARG
;
244 hr
= IWICBitmapDecoder_Initialize(iface
, stream
, WICDecodeMetadataCacheOnDemand
);
245 if (hr
!= S_OK
) return hr
;
247 *capability
= WICBitmapDecoderCapabilityCanDecodeAllImages
|
248 WICBitmapDecoderCapabilityCanDecodeSomeImages
;
249 /* FIXME: WICBitmapDecoderCapabilityCanEnumerateMetadata */
253 static void source_mgr_init_source(j_decompress_ptr cinfo
)
257 static jpeg_boolean
source_mgr_fill_input_buffer(j_decompress_ptr cinfo
)
259 JpegDecoder
*This
= decoder_from_decompress(cinfo
);
263 hr
= IStream_Read(This
->stream
, This
->source_buffer
, 1024, &bytesread
);
265 if (FAILED(hr
) || bytesread
== 0)
271 This
->source_mgr
.next_input_byte
= This
->source_buffer
;
272 This
->source_mgr
.bytes_in_buffer
= bytesread
;
277 static void source_mgr_skip_input_data(j_decompress_ptr cinfo
, long num_bytes
)
279 JpegDecoder
*This
= decoder_from_decompress(cinfo
);
282 if (num_bytes
> This
->source_mgr
.bytes_in_buffer
)
284 seek
.QuadPart
= num_bytes
- This
->source_mgr
.bytes_in_buffer
;
285 IStream_Seek(This
->stream
, seek
, STREAM_SEEK_CUR
, NULL
);
286 This
->source_mgr
.bytes_in_buffer
= 0;
288 else if (num_bytes
> 0)
290 This
->source_mgr
.next_input_byte
+= num_bytes
;
291 This
->source_mgr
.bytes_in_buffer
-= num_bytes
;
295 static void source_mgr_term_source(j_decompress_ptr cinfo
)
299 static HRESULT WINAPI
JpegDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
300 WICDecodeOptions cacheOptions
)
302 JpegDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
306 TRACE("(%p,%p,%u)\n", iface
, pIStream
, cacheOptions
);
308 EnterCriticalSection(&This
->lock
);
310 if (This
->cinfo_initialized
)
312 LeaveCriticalSection(&This
->lock
);
313 return WINCODEC_ERR_WRONGSTATE
;
316 pjpeg_std_error(&This
->jerr
);
318 This
->jerr
.error_exit
= error_exit_fn
;
319 This
->jerr
.emit_message
= emit_message_fn
;
321 This
->cinfo
.err
= &This
->jerr
;
323 This
->cinfo
.client_data
= jmpbuf
;
327 LeaveCriticalSection(&This
->lock
);
331 pjpeg_CreateDecompress(&This
->cinfo
, JPEG_LIB_VERSION
, sizeof(struct jpeg_decompress_struct
));
333 This
->cinfo_initialized
= TRUE
;
335 This
->stream
= pIStream
;
336 IStream_AddRef(pIStream
);
339 IStream_Seek(This
->stream
, seek
, STREAM_SEEK_SET
, NULL
);
341 This
->source_mgr
.bytes_in_buffer
= 0;
342 This
->source_mgr
.init_source
= source_mgr_init_source
;
343 This
->source_mgr
.fill_input_buffer
= source_mgr_fill_input_buffer
;
344 This
->source_mgr
.skip_input_data
= source_mgr_skip_input_data
;
345 This
->source_mgr
.resync_to_restart
= pjpeg_resync_to_restart
;
346 This
->source_mgr
.term_source
= source_mgr_term_source
;
348 This
->cinfo
.src
= &This
->source_mgr
;
350 ret
= pjpeg_read_header(&This
->cinfo
, TRUE
);
352 if (ret
!= JPEG_HEADER_OK
) {
353 WARN("Jpeg image in stream has bad format, read header returned %d.\n",ret
);
354 LeaveCriticalSection(&This
->lock
);
358 switch (This
->cinfo
.jpeg_color_space
)
361 This
->cinfo
.out_color_space
= JCS_GRAYSCALE
;
365 This
->cinfo
.out_color_space
= JCS_RGB
;
369 This
->cinfo
.out_color_space
= JCS_CMYK
;
372 ERR("Unknown JPEG color space %i\n", This
->cinfo
.jpeg_color_space
);
373 LeaveCriticalSection(&This
->lock
);
377 if (!pjpeg_start_decompress(&This
->cinfo
))
379 ERR("jpeg_start_decompress failed\n");
380 LeaveCriticalSection(&This
->lock
);
384 This
->initialized
= TRUE
;
386 LeaveCriticalSection(&This
->lock
);
391 static HRESULT WINAPI
JpegDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
392 GUID
*pguidContainerFormat
)
394 memcpy(pguidContainerFormat
, &GUID_ContainerFormatJpeg
, sizeof(GUID
));
398 static HRESULT WINAPI
JpegDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
399 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
402 IWICComponentInfo
*compinfo
;
404 TRACE("(%p,%p)\n", iface
, ppIDecoderInfo
);
406 hr
= CreateComponentInfo(&CLSID_WICJpegDecoder
, &compinfo
);
407 if (FAILED(hr
)) return hr
;
409 hr
= IWICComponentInfo_QueryInterface(compinfo
, &IID_IWICBitmapDecoderInfo
,
410 (void**)ppIDecoderInfo
);
412 IWICComponentInfo_Release(compinfo
);
417 static HRESULT WINAPI
JpegDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
418 IWICPalette
*pIPalette
)
420 TRACE("(%p,%p)\n", iface
, pIPalette
);
422 return WINCODEC_ERR_PALETTEUNAVAILABLE
;
425 static HRESULT WINAPI
JpegDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
426 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
428 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
432 static HRESULT WINAPI
JpegDecoder_GetPreview(IWICBitmapDecoder
*iface
,
433 IWICBitmapSource
**ppIBitmapSource
)
435 FIXME("(%p,%p): stub\n", iface
, ppIBitmapSource
);
436 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
439 static HRESULT WINAPI
JpegDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
440 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
442 FIXME("(%p,%u,%p,%p): stub\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
443 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
446 static HRESULT WINAPI
JpegDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
447 IWICBitmapSource
**ppIThumbnail
)
449 FIXME("(%p,%p): stub\n", iface
, ppIThumbnail
);
450 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
453 static HRESULT WINAPI
JpegDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
456 if (!pCount
) return E_INVALIDARG
;
462 static HRESULT WINAPI
JpegDecoder_GetFrame(IWICBitmapDecoder
*iface
,
463 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
465 JpegDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
466 TRACE("(%p,%u,%p)\n", iface
, index
, ppIBitmapFrame
);
468 if (!This
->initialized
) return WINCODEC_ERR_FRAMEMISSING
;
470 if (index
!= 0) return E_INVALIDARG
;
472 IWICBitmapDecoder_AddRef(iface
);
473 *ppIBitmapFrame
= &This
->IWICBitmapFrameDecode_iface
;
478 static const IWICBitmapDecoderVtbl JpegDecoder_Vtbl
= {
479 JpegDecoder_QueryInterface
,
482 JpegDecoder_QueryCapability
,
483 JpegDecoder_Initialize
,
484 JpegDecoder_GetContainerFormat
,
485 JpegDecoder_GetDecoderInfo
,
486 JpegDecoder_CopyPalette
,
487 JpegDecoder_GetMetadataQueryReader
,
488 JpegDecoder_GetPreview
,
489 JpegDecoder_GetColorContexts
,
490 JpegDecoder_GetThumbnail
,
491 JpegDecoder_GetFrameCount
,
495 static HRESULT WINAPI
JpegDecoder_Frame_QueryInterface(IWICBitmapFrameDecode
*iface
, REFIID iid
,
498 JpegDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
500 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
502 if (!ppv
) return E_INVALIDARG
;
504 if (IsEqualIID(&IID_IUnknown
, iid
) ||
505 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
506 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
508 *ppv
= &This
->IWICBitmapFrameDecode_iface
;
513 return E_NOINTERFACE
;
516 IUnknown_AddRef((IUnknown
*)*ppv
);
520 static ULONG WINAPI
JpegDecoder_Frame_AddRef(IWICBitmapFrameDecode
*iface
)
522 JpegDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
523 return IWICBitmapDecoder_AddRef(&This
->IWICBitmapDecoder_iface
);
526 static ULONG WINAPI
JpegDecoder_Frame_Release(IWICBitmapFrameDecode
*iface
)
528 JpegDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
529 return IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
532 static HRESULT WINAPI
JpegDecoder_Frame_GetSize(IWICBitmapFrameDecode
*iface
,
533 UINT
*puiWidth
, UINT
*puiHeight
)
535 JpegDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
536 *puiWidth
= This
->cinfo
.output_width
;
537 *puiHeight
= This
->cinfo
.output_height
;
538 TRACE("(%p)->(%u,%u)\n", iface
, *puiWidth
, *puiHeight
);
542 static HRESULT WINAPI
JpegDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
543 WICPixelFormatGUID
*pPixelFormat
)
545 JpegDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
546 TRACE("(%p,%p)\n", iface
, pPixelFormat
);
547 if (This
->cinfo
.out_color_space
== JCS_RGB
)
548 memcpy(pPixelFormat
, &GUID_WICPixelFormat24bppBGR
, sizeof(GUID
));
549 else if (This
->cinfo
.out_color_space
== JCS_CMYK
)
550 memcpy(pPixelFormat
, &GUID_WICPixelFormat32bppCMYK
, sizeof(GUID
));
551 else /* This->cinfo.out_color_space == JCS_GRAYSCALE */
552 memcpy(pPixelFormat
, &GUID_WICPixelFormat8bppGray
, sizeof(GUID
));
556 static HRESULT WINAPI
JpegDecoder_Frame_GetResolution(IWICBitmapFrameDecode
*iface
,
557 double *pDpiX
, double *pDpiY
)
559 JpegDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
561 EnterCriticalSection(&This
->lock
);
563 switch (This
->cinfo
.density_unit
)
565 case 2: /* pixels per centimeter */
566 *pDpiX
= This
->cinfo
.X_density
* 2.54;
567 *pDpiY
= This
->cinfo
.Y_density
* 2.54;
570 case 1: /* pixels per inch */
571 *pDpiX
= This
->cinfo
.X_density
;
572 *pDpiY
= This
->cinfo
.Y_density
;
575 case 0: /* unknown */
582 LeaveCriticalSection(&This
->lock
);
584 TRACE("(%p)->(%0.2f,%0.2f)\n", iface
, *pDpiX
, *pDpiY
);
589 static HRESULT WINAPI
JpegDecoder_Frame_CopyPalette(IWICBitmapFrameDecode
*iface
,
590 IWICPalette
*pIPalette
)
592 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
596 static HRESULT WINAPI
JpegDecoder_Frame_CopyPixels(IWICBitmapFrameDecode
*iface
,
597 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
599 JpegDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
606 TRACE("(%p,%p,%u,%u,%p)\n", iface
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
612 rect
.Width
= This
->cinfo
.output_width
;
613 rect
.Height
= This
->cinfo
.output_height
;
618 if (prc
->X
< 0 || prc
->Y
< 0 || prc
->X
+prc
->Width
> This
->cinfo
.output_width
||
619 prc
->Y
+prc
->Height
> This
->cinfo
.output_height
)
623 if (This
->cinfo
.out_color_space
== JCS_GRAYSCALE
) bpp
= 8;
624 else if (This
->cinfo
.out_color_space
== JCS_CMYK
) bpp
= 32;
627 stride
= (bpp
* This
->cinfo
.output_width
+ 7) / 8;
628 data_size
= stride
* This
->cinfo
.output_height
;
630 max_row_needed
= prc
->Y
+ prc
->Height
;
631 if (max_row_needed
> This
->cinfo
.output_height
) return E_INVALIDARG
;
633 EnterCriticalSection(&This
->lock
);
635 if (!This
->image_data
)
637 This
->image_data
= HeapAlloc(GetProcessHeap(), 0, data_size
);
638 if (!This
->image_data
)
640 LeaveCriticalSection(&This
->lock
);
641 return E_OUTOFMEMORY
;
645 This
->cinfo
.client_data
= jmpbuf
;
649 LeaveCriticalSection(&This
->lock
);
653 while (max_row_needed
> This
->cinfo
.output_scanline
)
655 UINT first_scanline
= This
->cinfo
.output_scanline
;
657 JSAMPROW out_rows
[4];
661 max_rows
= min(This
->cinfo
.output_height
-first_scanline
, 4);
662 for (i
=0; i
<max_rows
; i
++)
663 out_rows
[i
] = This
->image_data
+ stride
* (first_scanline
+i
);
665 ret
= pjpeg_read_scanlines(&This
->cinfo
, out_rows
, max_rows
);
669 ERR("read_scanlines failed\n");
670 LeaveCriticalSection(&This
->lock
);
676 /* libjpeg gives us RGB data and we want BGR, so byteswap the data */
677 reverse_bgr8(3, This
->image_data
+ stride
* first_scanline
,
678 This
->cinfo
.output_width
, This
->cinfo
.output_scanline
- first_scanline
,
682 if (This
->cinfo
.out_color_space
== JCS_CMYK
&& This
->cinfo
.saw_Adobe_marker
)
684 DWORD
*pDwordData
= (DWORD
*) (This
->image_data
+ stride
* first_scanline
);
685 DWORD
*pDwordDataEnd
= (DWORD
*) (This
->image_data
+ This
->cinfo
.output_scanline
* stride
);
687 /* Adobe JPEG's have inverted CMYK data. */
688 while(pDwordData
< pDwordDataEnd
)
689 *pDwordData
++ ^= 0xffffffff;
694 LeaveCriticalSection(&This
->lock
);
696 return copy_pixels(bpp
, This
->image_data
,
697 This
->cinfo
.output_width
, This
->cinfo
.output_height
, stride
,
698 prc
, cbStride
, cbBufferSize
, pbBuffer
);
701 static HRESULT WINAPI
JpegDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
702 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
704 JpegDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
706 TRACE("(%p,%p)\n", iface
, ppIMetadataQueryReader
);
708 if (!ppIMetadataQueryReader
)
711 return MetadataQueryReader_CreateInstance(&This
->IWICMetadataBlockReader_iface
, NULL
, ppIMetadataQueryReader
);
714 static HRESULT WINAPI
JpegDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode
*iface
,
715 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
717 FIXME("(%p,%u,%p,%p): stub\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
718 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
721 static HRESULT WINAPI
JpegDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode
*iface
,
722 IWICBitmapSource
**ppIThumbnail
)
724 FIXME("(%p,%p): stub\n", iface
, ppIThumbnail
);
725 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
728 static const IWICBitmapFrameDecodeVtbl JpegDecoder_Frame_Vtbl
= {
729 JpegDecoder_Frame_QueryInterface
,
730 JpegDecoder_Frame_AddRef
,
731 JpegDecoder_Frame_Release
,
732 JpegDecoder_Frame_GetSize
,
733 JpegDecoder_Frame_GetPixelFormat
,
734 JpegDecoder_Frame_GetResolution
,
735 JpegDecoder_Frame_CopyPalette
,
736 JpegDecoder_Frame_CopyPixels
,
737 JpegDecoder_Frame_GetMetadataQueryReader
,
738 JpegDecoder_Frame_GetColorContexts
,
739 JpegDecoder_Frame_GetThumbnail
742 static HRESULT WINAPI
JpegDecoder_Block_QueryInterface(IWICMetadataBlockReader
*iface
, REFIID iid
,
745 JpegDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
746 return IWICBitmapFrameDecode_QueryInterface(&This
->IWICBitmapFrameDecode_iface
, iid
, ppv
);
749 static ULONG WINAPI
JpegDecoder_Block_AddRef(IWICMetadataBlockReader
*iface
)
751 JpegDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
752 return IWICBitmapDecoder_AddRef(&This
->IWICBitmapDecoder_iface
);
755 static ULONG WINAPI
JpegDecoder_Block_Release(IWICMetadataBlockReader
*iface
)
757 JpegDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
758 return IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
761 static HRESULT WINAPI
JpegDecoder_Block_GetContainerFormat(IWICMetadataBlockReader
*iface
,
762 GUID
*pguidContainerFormat
)
764 TRACE("%p,%p\n", iface
, pguidContainerFormat
);
766 if (!pguidContainerFormat
) return E_INVALIDARG
;
768 memcpy(pguidContainerFormat
, &GUID_ContainerFormatJpeg
, sizeof(GUID
));
773 static HRESULT WINAPI
JpegDecoder_Block_GetCount(IWICMetadataBlockReader
*iface
,
776 FIXME("%p,%p\n", iface
, pcCount
);
778 if (!pcCount
) return E_INVALIDARG
;
785 static HRESULT WINAPI
JpegDecoder_Block_GetReaderByIndex(IWICMetadataBlockReader
*iface
,
786 UINT nIndex
, IWICMetadataReader
**ppIMetadataReader
)
788 FIXME("%p,%d,%p\n", iface
, nIndex
, ppIMetadataReader
);
792 static HRESULT WINAPI
JpegDecoder_Block_GetEnumerator(IWICMetadataBlockReader
*iface
,
793 IEnumUnknown
**ppIEnumMetadata
)
795 FIXME("%p,%p\n", iface
, ppIEnumMetadata
);
799 static const IWICMetadataBlockReaderVtbl JpegDecoder_Block_Vtbl
= {
800 JpegDecoder_Block_QueryInterface
,
801 JpegDecoder_Block_AddRef
,
802 JpegDecoder_Block_Release
,
803 JpegDecoder_Block_GetContainerFormat
,
804 JpegDecoder_Block_GetCount
,
805 JpegDecoder_Block_GetReaderByIndex
,
806 JpegDecoder_Block_GetEnumerator
,
809 HRESULT
JpegDecoder_CreateInstance(REFIID iid
, void** ppv
)
814 TRACE("(%s,%p)\n", debugstr_guid(iid
), ppv
);
816 if (!libjpeg_handle
&& !load_libjpeg())
818 ERR("Failed reading JPEG because unable to find %s\n", SONAME_LIBJPEG
);
824 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(JpegDecoder
));
825 if (!This
) return E_OUTOFMEMORY
;
827 This
->IWICBitmapDecoder_iface
.lpVtbl
= &JpegDecoder_Vtbl
;
828 This
->IWICBitmapFrameDecode_iface
.lpVtbl
= &JpegDecoder_Frame_Vtbl
;
829 This
->IWICMetadataBlockReader_iface
.lpVtbl
= &JpegDecoder_Block_Vtbl
;
831 This
->initialized
= FALSE
;
832 This
->cinfo_initialized
= FALSE
;
834 This
->image_data
= NULL
;
835 InitializeCriticalSection(&This
->lock
);
836 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": JpegDecoder.lock");
838 ret
= IWICBitmapDecoder_QueryInterface(&This
->IWICBitmapDecoder_iface
, iid
, ppv
);
839 IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
844 typedef struct jpeg_compress_format
{
845 const WICPixelFormatGUID
*guid
;
848 J_COLOR_SPACE color_space
;
850 } jpeg_compress_format
;
852 static const jpeg_compress_format compress_formats
[] = {
853 { &GUID_WICPixelFormat24bppBGR
, 24, 3, JCS_RGB
, 1 },
854 { &GUID_WICPixelFormat32bppCMYK
, 32, 4, JCS_CMYK
},
855 { &GUID_WICPixelFormat8bppGray
, 8, 1, JCS_GRAYSCALE
},
859 typedef struct JpegEncoder
{
860 IWICBitmapEncoder IWICBitmapEncoder_iface
;
861 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface
;
863 struct jpeg_compress_struct cinfo
;
864 struct jpeg_error_mgr jerr
;
865 struct jpeg_destination_mgr dest_mgr
;
868 BOOL frame_initialized
;
869 BOOL started_compress
;
871 BOOL frame_committed
;
875 const jpeg_compress_format
*format
;
877 WICColor palette
[256];
879 CRITICAL_SECTION lock
;
880 BYTE dest_buffer
[1024];
883 static inline JpegEncoder
*impl_from_IWICBitmapEncoder(IWICBitmapEncoder
*iface
)
885 return CONTAINING_RECORD(iface
, JpegEncoder
, IWICBitmapEncoder_iface
);
888 static inline JpegEncoder
*impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode
*iface
)
890 return CONTAINING_RECORD(iface
, JpegEncoder
, IWICBitmapFrameEncode_iface
);
893 static inline JpegEncoder
*encoder_from_compress(j_compress_ptr compress
)
895 return CONTAINING_RECORD(compress
, JpegEncoder
, cinfo
);
898 static void dest_mgr_init_destination(j_compress_ptr cinfo
)
900 JpegEncoder
*This
= encoder_from_compress(cinfo
);
902 This
->dest_mgr
.next_output_byte
= This
->dest_buffer
;
903 This
->dest_mgr
.free_in_buffer
= sizeof(This
->dest_buffer
);
906 static jpeg_boolean
dest_mgr_empty_output_buffer(j_compress_ptr cinfo
)
908 JpegEncoder
*This
= encoder_from_compress(cinfo
);
912 hr
= IStream_Write(This
->stream
, This
->dest_buffer
,
913 sizeof(This
->dest_buffer
), &byteswritten
);
915 if (hr
!= S_OK
|| byteswritten
== 0)
917 ERR("Failed writing data, hr=%x\n", hr
);
921 This
->dest_mgr
.next_output_byte
= This
->dest_buffer
;
922 This
->dest_mgr
.free_in_buffer
= sizeof(This
->dest_buffer
);
926 static void dest_mgr_term_destination(j_compress_ptr cinfo
)
928 JpegEncoder
*This
= encoder_from_compress(cinfo
);
932 if (This
->dest_mgr
.free_in_buffer
!= sizeof(This
->dest_buffer
))
934 hr
= IStream_Write(This
->stream
, This
->dest_buffer
,
935 sizeof(This
->dest_buffer
) - This
->dest_mgr
.free_in_buffer
, &byteswritten
);
937 if (hr
!= S_OK
|| byteswritten
== 0)
938 ERR("Failed writing data, hr=%x\n", hr
);
942 static HRESULT WINAPI
JpegEncoder_Frame_QueryInterface(IWICBitmapFrameEncode
*iface
, REFIID iid
,
945 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
946 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
948 if (!ppv
) return E_INVALIDARG
;
950 if (IsEqualIID(&IID_IUnknown
, iid
) ||
951 IsEqualIID(&IID_IWICBitmapFrameEncode
, iid
))
953 *ppv
= &This
->IWICBitmapFrameEncode_iface
;
958 return E_NOINTERFACE
;
961 IUnknown_AddRef((IUnknown
*)*ppv
);
965 static ULONG WINAPI
JpegEncoder_Frame_AddRef(IWICBitmapFrameEncode
*iface
)
967 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
968 return IWICBitmapEncoder_AddRef(&This
->IWICBitmapEncoder_iface
);
971 static ULONG WINAPI
JpegEncoder_Frame_Release(IWICBitmapFrameEncode
*iface
)
973 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
974 return IWICBitmapEncoder_Release(&This
->IWICBitmapEncoder_iface
);
977 static HRESULT WINAPI
JpegEncoder_Frame_Initialize(IWICBitmapFrameEncode
*iface
,
978 IPropertyBag2
*pIEncoderOptions
)
980 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
981 TRACE("(%p,%p)\n", iface
, pIEncoderOptions
);
983 EnterCriticalSection(&This
->lock
);
985 if (This
->frame_initialized
)
987 LeaveCriticalSection(&This
->lock
);
988 return WINCODEC_ERR_WRONGSTATE
;
991 This
->frame_initialized
= TRUE
;
993 LeaveCriticalSection(&This
->lock
);
998 static HRESULT WINAPI
JpegEncoder_Frame_SetSize(IWICBitmapFrameEncode
*iface
,
999 UINT uiWidth
, UINT uiHeight
)
1001 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1002 TRACE("(%p,%u,%u)\n", iface
, uiWidth
, uiHeight
);
1004 EnterCriticalSection(&This
->lock
);
1006 if (!This
->frame_initialized
|| This
->started_compress
)
1008 LeaveCriticalSection(&This
->lock
);
1009 return WINCODEC_ERR_WRONGSTATE
;
1012 This
->width
= uiWidth
;
1013 This
->height
= uiHeight
;
1015 LeaveCriticalSection(&This
->lock
);
1020 static HRESULT WINAPI
JpegEncoder_Frame_SetResolution(IWICBitmapFrameEncode
*iface
,
1021 double dpiX
, double dpiY
)
1023 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1024 TRACE("(%p,%0.2f,%0.2f)\n", iface
, dpiX
, dpiY
);
1026 EnterCriticalSection(&This
->lock
);
1028 if (!This
->frame_initialized
|| This
->started_compress
)
1030 LeaveCriticalSection(&This
->lock
);
1031 return WINCODEC_ERR_WRONGSTATE
;
1037 LeaveCriticalSection(&This
->lock
);
1042 static HRESULT WINAPI
JpegEncoder_Frame_SetPixelFormat(IWICBitmapFrameEncode
*iface
,
1043 WICPixelFormatGUID
*pPixelFormat
)
1045 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1047 TRACE("(%p,%s)\n", iface
, debugstr_guid(pPixelFormat
));
1049 EnterCriticalSection(&This
->lock
);
1051 if (!This
->frame_initialized
|| This
->started_compress
)
1053 LeaveCriticalSection(&This
->lock
);
1054 return WINCODEC_ERR_WRONGSTATE
;
1057 for (i
=0; compress_formats
[i
].guid
; i
++)
1059 if (memcmp(compress_formats
[i
].guid
, pPixelFormat
, sizeof(GUID
)) == 0)
1063 if (!compress_formats
[i
].guid
) i
= 0;
1065 This
->format
= &compress_formats
[i
];
1066 memcpy(pPixelFormat
, This
->format
->guid
, sizeof(GUID
));
1068 LeaveCriticalSection(&This
->lock
);
1073 static HRESULT WINAPI
JpegEncoder_Frame_SetColorContexts(IWICBitmapFrameEncode
*iface
,
1074 UINT cCount
, IWICColorContext
**ppIColorContext
)
1076 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
1080 static HRESULT WINAPI
JpegEncoder_Frame_SetPalette(IWICBitmapFrameEncode
*iface
,
1081 IWICPalette
*palette
)
1083 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1086 TRACE("(%p,%p)\n", iface
, palette
);
1088 if (!palette
) return E_INVALIDARG
;
1090 EnterCriticalSection(&This
->lock
);
1092 if (This
->frame_initialized
)
1093 hr
= IWICPalette_GetColors(palette
, 256, This
->palette
, &This
->colors
);
1095 hr
= WINCODEC_ERR_NOTINITIALIZED
;
1097 LeaveCriticalSection(&This
->lock
);
1101 static HRESULT WINAPI
JpegEncoder_Frame_SetThumbnail(IWICBitmapFrameEncode
*iface
,
1102 IWICBitmapSource
*pIThumbnail
)
1104 FIXME("(%p,%p): stub\n", iface
, pIThumbnail
);
1105 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1108 static HRESULT WINAPI
JpegEncoder_Frame_WritePixels(IWICBitmapFrameEncode
*iface
,
1109 UINT lineCount
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbPixels
)
1111 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1113 BYTE
*swapped_data
= NULL
, *current_row
;
1116 TRACE("(%p,%u,%u,%u,%p)\n", iface
, lineCount
, cbStride
, cbBufferSize
, pbPixels
);
1118 EnterCriticalSection(&This
->lock
);
1120 if (!This
->frame_initialized
|| !This
->width
|| !This
->height
|| !This
->format
)
1122 LeaveCriticalSection(&This
->lock
);
1123 return WINCODEC_ERR_WRONGSTATE
;
1126 if (lineCount
== 0 || lineCount
+ This
->lines_written
> This
->height
)
1128 LeaveCriticalSection(&This
->lock
);
1129 return E_INVALIDARG
;
1132 /* set up setjmp/longjmp error handling */
1135 LeaveCriticalSection(&This
->lock
);
1136 HeapFree(GetProcessHeap(), 0, swapped_data
);
1139 This
->cinfo
.client_data
= jmpbuf
;
1141 if (!This
->started_compress
)
1143 This
->cinfo
.image_width
= This
->width
;
1144 This
->cinfo
.image_height
= This
->height
;
1145 This
->cinfo
.input_components
= This
->format
->num_components
;
1146 This
->cinfo
.in_color_space
= This
->format
->color_space
;
1148 pjpeg_set_defaults(&This
->cinfo
);
1150 if (This
->xres
!= 0.0 && This
->yres
!= 0.0)
1152 This
->cinfo
.density_unit
= 1; /* dots per inch */
1153 This
->cinfo
.X_density
= This
->xres
;
1154 This
->cinfo
.Y_density
= This
->yres
;
1157 pjpeg_start_compress(&This
->cinfo
, TRUE
);
1159 This
->started_compress
= TRUE
;
1162 row_size
= This
->format
->bpp
/ 8 * This
->width
;
1164 if (This
->format
->swap_rgb
)
1166 swapped_data
= HeapAlloc(GetProcessHeap(), 0, row_size
);
1169 LeaveCriticalSection(&This
->lock
);
1170 return E_OUTOFMEMORY
;
1174 for (line
=0; line
< lineCount
; line
++)
1176 if (This
->format
->swap_rgb
)
1180 memcpy(swapped_data
, pbPixels
+ (cbStride
* line
), row_size
);
1182 for (x
=0; x
< This
->width
; x
++)
1186 b
= swapped_data
[x
*3];
1187 swapped_data
[x
*3] = swapped_data
[x
*3+2];
1188 swapped_data
[x
*3+2] = b
;
1191 current_row
= swapped_data
;
1194 current_row
= pbPixels
+ (cbStride
* line
);
1196 if (!pjpeg_write_scanlines(&This
->cinfo
, ¤t_row
, 1))
1198 ERR("failed writing scanlines\n");
1199 LeaveCriticalSection(&This
->lock
);
1200 HeapFree(GetProcessHeap(), 0, swapped_data
);
1204 This
->lines_written
++;
1207 LeaveCriticalSection(&This
->lock
);
1208 HeapFree(GetProcessHeap(), 0, swapped_data
);
1213 static HRESULT WINAPI
JpegEncoder_Frame_WriteSource(IWICBitmapFrameEncode
*iface
,
1214 IWICBitmapSource
*pIBitmapSource
, WICRect
*prc
)
1216 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1218 TRACE("(%p,%p,%p)\n", iface
, pIBitmapSource
, prc
);
1220 if (!This
->frame_initialized
)
1221 return WINCODEC_ERR_WRONGSTATE
;
1223 hr
= configure_write_source(iface
, pIBitmapSource
, prc
,
1224 This
->format
? This
->format
->guid
: NULL
, This
->width
, This
->height
,
1225 This
->xres
, This
->yres
);
1229 hr
= write_source(iface
, pIBitmapSource
, prc
,
1230 This
->format
->guid
, This
->format
->bpp
, This
->width
, This
->height
);
1236 static HRESULT WINAPI
JpegEncoder_Frame_Commit(IWICBitmapFrameEncode
*iface
)
1238 JpegEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1240 TRACE("(%p)\n", iface
);
1242 EnterCriticalSection(&This
->lock
);
1244 if (!This
->started_compress
|| This
->lines_written
!= This
->height
|| This
->frame_committed
)
1246 LeaveCriticalSection(&This
->lock
);
1247 return WINCODEC_ERR_WRONGSTATE
;
1250 /* set up setjmp/longjmp error handling */
1253 LeaveCriticalSection(&This
->lock
);
1256 This
->cinfo
.client_data
= jmpbuf
;
1258 pjpeg_finish_compress(&This
->cinfo
);
1260 This
->frame_committed
= TRUE
;
1262 LeaveCriticalSection(&This
->lock
);
1267 static HRESULT WINAPI
JpegEncoder_Frame_GetMetadataQueryWriter(IWICBitmapFrameEncode
*iface
,
1268 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
1270 FIXME("(%p, %p): stub\n", iface
, ppIMetadataQueryWriter
);
1274 static const IWICBitmapFrameEncodeVtbl JpegEncoder_FrameVtbl
= {
1275 JpegEncoder_Frame_QueryInterface
,
1276 JpegEncoder_Frame_AddRef
,
1277 JpegEncoder_Frame_Release
,
1278 JpegEncoder_Frame_Initialize
,
1279 JpegEncoder_Frame_SetSize
,
1280 JpegEncoder_Frame_SetResolution
,
1281 JpegEncoder_Frame_SetPixelFormat
,
1282 JpegEncoder_Frame_SetColorContexts
,
1283 JpegEncoder_Frame_SetPalette
,
1284 JpegEncoder_Frame_SetThumbnail
,
1285 JpegEncoder_Frame_WritePixels
,
1286 JpegEncoder_Frame_WriteSource
,
1287 JpegEncoder_Frame_Commit
,
1288 JpegEncoder_Frame_GetMetadataQueryWriter
1291 static HRESULT WINAPI
JpegEncoder_QueryInterface(IWICBitmapEncoder
*iface
, REFIID iid
,
1294 JpegEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1295 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
1297 if (!ppv
) return E_INVALIDARG
;
1299 if (IsEqualIID(&IID_IUnknown
, iid
) ||
1300 IsEqualIID(&IID_IWICBitmapEncoder
, iid
))
1302 *ppv
= &This
->IWICBitmapEncoder_iface
;
1307 return E_NOINTERFACE
;
1310 IUnknown_AddRef((IUnknown
*)*ppv
);
1314 static ULONG WINAPI
JpegEncoder_AddRef(IWICBitmapEncoder
*iface
)
1316 JpegEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1317 ULONG ref
= InterlockedIncrement(&This
->ref
);
1319 TRACE("(%p) refcount=%u\n", iface
, ref
);
1324 static ULONG WINAPI
JpegEncoder_Release(IWICBitmapEncoder
*iface
)
1326 JpegEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1327 ULONG ref
= InterlockedDecrement(&This
->ref
);
1329 TRACE("(%p) refcount=%u\n", iface
, ref
);
1333 This
->lock
.DebugInfo
->Spare
[0] = 0;
1334 DeleteCriticalSection(&This
->lock
);
1335 if (This
->initialized
) pjpeg_destroy_compress(&This
->cinfo
);
1336 if (This
->stream
) IStream_Release(This
->stream
);
1337 HeapFree(GetProcessHeap(), 0, This
);
1343 static HRESULT WINAPI
JpegEncoder_Initialize(IWICBitmapEncoder
*iface
,
1344 IStream
*pIStream
, WICBitmapEncoderCacheOption cacheOption
)
1346 JpegEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1349 TRACE("(%p,%p,%u)\n", iface
, pIStream
, cacheOption
);
1351 EnterCriticalSection(&This
->lock
);
1353 if (This
->initialized
)
1355 LeaveCriticalSection(&This
->lock
);
1356 return WINCODEC_ERR_WRONGSTATE
;
1359 pjpeg_std_error(&This
->jerr
);
1361 This
->jerr
.error_exit
= error_exit_fn
;
1362 This
->jerr
.emit_message
= emit_message_fn
;
1364 This
->cinfo
.err
= &This
->jerr
;
1366 This
->cinfo
.client_data
= jmpbuf
;
1370 LeaveCriticalSection(&This
->lock
);
1374 pjpeg_CreateCompress(&This
->cinfo
, JPEG_LIB_VERSION
, sizeof(struct jpeg_compress_struct
));
1376 This
->stream
= pIStream
;
1377 IStream_AddRef(pIStream
);
1379 This
->dest_mgr
.next_output_byte
= This
->dest_buffer
;
1380 This
->dest_mgr
.free_in_buffer
= sizeof(This
->dest_buffer
);
1382 This
->dest_mgr
.init_destination
= dest_mgr_init_destination
;
1383 This
->dest_mgr
.empty_output_buffer
= dest_mgr_empty_output_buffer
;
1384 This
->dest_mgr
.term_destination
= dest_mgr_term_destination
;
1386 This
->cinfo
.dest
= &This
->dest_mgr
;
1388 This
->initialized
= TRUE
;
1390 LeaveCriticalSection(&This
->lock
);
1395 static HRESULT WINAPI
JpegEncoder_GetContainerFormat(IWICBitmapEncoder
*iface
,
1396 GUID
*pguidContainerFormat
)
1398 FIXME("(%p,%s): stub\n", iface
, debugstr_guid(pguidContainerFormat
));
1402 static HRESULT WINAPI
JpegEncoder_GetEncoderInfo(IWICBitmapEncoder
*iface
,
1403 IWICBitmapEncoderInfo
**ppIEncoderInfo
)
1405 FIXME("(%p,%p): stub\n", iface
, ppIEncoderInfo
);
1409 static HRESULT WINAPI
JpegEncoder_SetColorContexts(IWICBitmapEncoder
*iface
,
1410 UINT cCount
, IWICColorContext
**ppIColorContext
)
1412 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
1416 static HRESULT WINAPI
JpegEncoder_SetPalette(IWICBitmapEncoder
*iface
, IWICPalette
*pIPalette
)
1418 JpegEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1421 TRACE("(%p,%p)\n", iface
, pIPalette
);
1423 EnterCriticalSection(&This
->lock
);
1425 hr
= This
->initialized
? WINCODEC_ERR_UNSUPPORTEDOPERATION
: WINCODEC_ERR_NOTINITIALIZED
;
1427 LeaveCriticalSection(&This
->lock
);
1432 static HRESULT WINAPI
JpegEncoder_SetThumbnail(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIThumbnail
)
1434 TRACE("(%p,%p)\n", iface
, pIThumbnail
);
1435 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1438 static HRESULT WINAPI
JpegEncoder_SetPreview(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIPreview
)
1440 TRACE("(%p,%p)\n", iface
, pIPreview
);
1441 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1444 static HRESULT WINAPI
JpegEncoder_CreateNewFrame(IWICBitmapEncoder
*iface
,
1445 IWICBitmapFrameEncode
**ppIFrameEncode
, IPropertyBag2
**ppIEncoderOptions
)
1447 JpegEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1449 static const PROPBAG2 opts
[6] =
1451 { PROPBAG2_TYPE_DATA
, VT_R4
, 0, 0, (LPOLESTR
)wszImageQuality
},
1452 { PROPBAG2_TYPE_DATA
, VT_UI1
, 0, 0, (LPOLESTR
)wszBitmapTransform
},
1453 { PROPBAG2_TYPE_DATA
, VT_I4
| VT_ARRAY
, 0, 0, (LPOLESTR
)wszLuminance
},
1454 { PROPBAG2_TYPE_DATA
, VT_I4
| VT_ARRAY
, 0, 0, (LPOLESTR
)wszChrominance
},
1455 { PROPBAG2_TYPE_DATA
, VT_UI1
, 0, 0, (LPOLESTR
)wszJpegYCrCbSubsampling
},
1456 { PROPBAG2_TYPE_DATA
, VT_BOOL
, 0, 0, (LPOLESTR
)wszSuppressApp0
},
1459 TRACE("(%p,%p,%p)\n", iface
, ppIFrameEncode
, ppIEncoderOptions
);
1461 EnterCriticalSection(&This
->lock
);
1463 if (This
->frame_count
!= 0)
1465 LeaveCriticalSection(&This
->lock
);
1466 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1469 if (!This
->initialized
)
1471 LeaveCriticalSection(&This
->lock
);
1472 return WINCODEC_ERR_NOTINITIALIZED
;
1475 if (ppIEncoderOptions
)
1477 hr
= CreatePropertyBag2(opts
, sizeof(opts
)/sizeof(opts
[0]), ppIEncoderOptions
);
1480 LeaveCriticalSection(&This
->lock
);
1485 This
->frame_count
= 1;
1487 LeaveCriticalSection(&This
->lock
);
1489 IWICBitmapEncoder_AddRef(iface
);
1490 *ppIFrameEncode
= &This
->IWICBitmapFrameEncode_iface
;
1495 static HRESULT WINAPI
JpegEncoder_Commit(IWICBitmapEncoder
*iface
)
1497 JpegEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1498 TRACE("(%p)\n", iface
);
1500 EnterCriticalSection(&This
->lock
);
1502 if (!This
->frame_committed
|| This
->committed
)
1504 LeaveCriticalSection(&This
->lock
);
1505 return WINCODEC_ERR_WRONGSTATE
;
1508 This
->committed
= TRUE
;
1510 LeaveCriticalSection(&This
->lock
);
1515 static HRESULT WINAPI
JpegEncoder_GetMetadataQueryWriter(IWICBitmapEncoder
*iface
,
1516 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
1518 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryWriter
);
1522 static const IWICBitmapEncoderVtbl JpegEncoder_Vtbl
= {
1523 JpegEncoder_QueryInterface
,
1525 JpegEncoder_Release
,
1526 JpegEncoder_Initialize
,
1527 JpegEncoder_GetContainerFormat
,
1528 JpegEncoder_GetEncoderInfo
,
1529 JpegEncoder_SetColorContexts
,
1530 JpegEncoder_SetPalette
,
1531 JpegEncoder_SetThumbnail
,
1532 JpegEncoder_SetPreview
,
1533 JpegEncoder_CreateNewFrame
,
1535 JpegEncoder_GetMetadataQueryWriter
1538 HRESULT
JpegEncoder_CreateInstance(REFIID iid
, void** ppv
)
1543 TRACE("(%s,%p)\n", debugstr_guid(iid
), ppv
);
1547 if (!libjpeg_handle
&& !load_libjpeg())
1549 ERR("Failed writing JPEG because unable to find %s\n",SONAME_LIBJPEG
);
1553 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(JpegEncoder
));
1554 if (!This
) return E_OUTOFMEMORY
;
1556 This
->IWICBitmapEncoder_iface
.lpVtbl
= &JpegEncoder_Vtbl
;
1557 This
->IWICBitmapFrameEncode_iface
.lpVtbl
= &JpegEncoder_FrameVtbl
;
1559 This
->initialized
= FALSE
;
1560 This
->frame_count
= 0;
1561 This
->frame_initialized
= FALSE
;
1562 This
->started_compress
= FALSE
;
1563 This
->lines_written
= 0;
1564 This
->frame_committed
= FALSE
;
1565 This
->committed
= FALSE
;
1566 This
->width
= This
->height
= 0;
1567 This
->xres
= This
->yres
= 0.0;
1568 This
->format
= NULL
;
1569 This
->stream
= NULL
;
1571 InitializeCriticalSection(&This
->lock
);
1572 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": JpegEncoder.lock");
1574 ret
= IWICBitmapEncoder_QueryInterface(&This
->IWICBitmapEncoder_iface
, iid
, ppv
);
1575 IWICBitmapEncoder_Release(&This
->IWICBitmapEncoder_iface
);
1580 #else /* !defined(SONAME_LIBJPEG) */
1582 HRESULT
JpegDecoder_CreateInstance(REFIID iid
, void** ppv
)
1584 ERR("Trying to load JPEG picture, but JPEG support is not compiled in.\n");
1588 HRESULT
JpegEncoder_CreateInstance(REFIID iid
, void** ppv
)
1590 ERR("Trying to save JPEG picture, but JPEG support is not compiled in.\n");