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_read_header);
70 MAKE_FUNCPTR(jpeg_read_scanlines);
71 MAKE_FUNCPTR(jpeg_resync_to_restart);
72 MAKE_FUNCPTR(jpeg_start_decompress);
73 MAKE_FUNCPTR(jpeg_std_error);
76 static void *load_libjpeg(void)
78 if((libjpeg_handle = wine_dlopen(SONAME_LIBJPEG, RTLD_NOW, NULL, 0)) != NULL) {
80 #define LOAD_FUNCPTR(f) \
81 if((p##f = wine_dlsym(libjpeg_handle, #f, NULL, 0)) == NULL) { \
82 libjpeg_handle = NULL; \
86 LOAD_FUNCPTR(jpeg_CreateCompress);
87 LOAD_FUNCPTR(jpeg_CreateDecompress);
88 LOAD_FUNCPTR(jpeg_destroy_compress);
89 LOAD_FUNCPTR(jpeg_destroy_decompress);
90 LOAD_FUNCPTR(jpeg_read_header);
91 LOAD_FUNCPTR(jpeg_read_scanlines);
92 LOAD_FUNCPTR(jpeg_resync_to_restart);
93 LOAD_FUNCPTR(jpeg_start_decompress);
94 LOAD_FUNCPTR(jpeg_std_error);
97 return libjpeg_handle;
100 static void error_exit_fn(j_common_ptr cinfo)
102 char message[JMSG_LENGTH_MAX];
105 cinfo->err->format_message(cinfo, message);
106 ERR_(jpeg)("%s\n", message);
108 longjmp(*(jmp_buf*)cinfo->client_data, 1);
111 static void emit_message_fn(j_common_ptr cinfo, int msg_level)
113 char message[JMSG_LENGTH_MAX];
115 if (msg_level < 0 && ERR_ON(jpeg))
117 cinfo->err->format_message(cinfo, message);
118 ERR_(jpeg)("%s\n", message);
120 else if (msg_level == 0 && WARN_ON(jpeg))
122 cinfo->err->format_message(cinfo, message);
123 WARN_(jpeg)("%s\n", message);
125 else if (msg_level > 0 && TRACE_ON(jpeg))
127 cinfo->err->format_message(cinfo, message);
128 TRACE_(jpeg)("%s\n", message);
133 IWICBitmapDecoder IWICBitmapDecoder_iface;
134 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
137 BOOL cinfo_initialized;
139 struct jpeg_decompress_struct cinfo;
140 struct jpeg_error_mgr jerr;
141 struct jpeg_source_mgr source_mgr;
142 BYTE source_buffer[1024];
144 CRITICAL_SECTION lock;
147 static inline JpegDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
149 return CONTAINING_RECORD(iface, JpegDecoder, IWICBitmapDecoder_iface);
152 static inline JpegDecoder *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
154 return CONTAINING_RECORD(iface, JpegDecoder, IWICBitmapFrameDecode_iface);
157 static inline JpegDecoder *decoder_from_decompress(j_decompress_ptr decompress)
159 return CONTAINING_RECORD(decompress, JpegDecoder, cinfo);
162 static HRESULT WINAPI JpegDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
165 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
166 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
168 if (!ppv) return E_INVALIDARG;
170 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
177 return E_NOINTERFACE;
180 IUnknown_AddRef((IUnknown*)*ppv);
184 static ULONG WINAPI JpegDecoder_AddRef(IWICBitmapDecoder *iface)
186 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
187 ULONG ref = InterlockedIncrement(&This->ref);
189 TRACE("(%p) refcount=%u\n", iface, ref);
194 static ULONG WINAPI JpegDecoder_Release(IWICBitmapDecoder *iface)
196 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
197 ULONG ref = InterlockedDecrement(&This->ref);
199 TRACE("(%p) refcount=%u\n", iface, ref);
203 This->lock.DebugInfo->Spare[0] = 0;
204 DeleteCriticalSection(&This->lock);
205 if (This->cinfo_initialized) pjpeg_destroy_decompress(&This->cinfo);
206 if (This->stream) IStream_Release(This->stream);
207 HeapFree(GetProcessHeap(), 0, This->image_data);
208 HeapFree(GetProcessHeap(), 0, This);
214 static HRESULT WINAPI JpegDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
215 DWORD *pdwCapability)
217 FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
221 static void source_mgr_init_source(j_decompress_ptr cinfo)
225 static jpeg_boolean source_mgr_fill_input_buffer(j_decompress_ptr cinfo)
227 JpegDecoder *This = decoder_from_decompress(cinfo);
231 hr = IStream_Read(This->stream, This->source_buffer, 1024, &bytesread);
233 if (hr != S_OK || bytesread == 0)
239 This->source_mgr.next_input_byte = This->source_buffer;
240 This->source_mgr.bytes_in_buffer = bytesread;
245 static void source_mgr_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
247 JpegDecoder *This = decoder_from_decompress(cinfo);
250 if (num_bytes > This->source_mgr.bytes_in_buffer)
252 seek.QuadPart = num_bytes - This->source_mgr.bytes_in_buffer;
253 IStream_Seek(This->stream, seek, STREAM_SEEK_CUR, NULL);
254 This->source_mgr.bytes_in_buffer = 0;
256 else if (num_bytes > 0)
258 This->source_mgr.next_input_byte += num_bytes;
259 This->source_mgr.bytes_in_buffer -= num_bytes;
263 static void source_mgr_term_source(j_decompress_ptr cinfo)
267 static HRESULT WINAPI JpegDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
268 WICDecodeOptions cacheOptions)
270 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
274 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOptions);
276 EnterCriticalSection(&This->lock);
278 if (This->cinfo_initialized)
280 LeaveCriticalSection(&This->lock);
281 return WINCODEC_ERR_WRONGSTATE;
284 pjpeg_std_error(&This->jerr);
286 This->jerr.error_exit = error_exit_fn;
287 This->jerr.emit_message = emit_message_fn;
289 This->cinfo.err = &This->jerr;
291 This->cinfo.client_data = jmpbuf;
295 LeaveCriticalSection(&This->lock);
299 pjpeg_CreateDecompress(&This->cinfo, JPEG_LIB_VERSION, sizeof(struct jpeg_decompress_struct));
301 This->cinfo_initialized = TRUE;
303 This->stream = pIStream;
304 IStream_AddRef(pIStream);
307 IStream_Seek(This->stream, seek, STREAM_SEEK_SET, NULL);
309 This->source_mgr.bytes_in_buffer = 0;
310 This->source_mgr.init_source = source_mgr_init_source;
311 This->source_mgr.fill_input_buffer = source_mgr_fill_input_buffer;
312 This->source_mgr.skip_input_data = source_mgr_skip_input_data;
313 This->source_mgr.resync_to_restart = pjpeg_resync_to_restart;
314 This->source_mgr.term_source = source_mgr_term_source;
316 This->cinfo.src = &This->source_mgr;
318 ret = pjpeg_read_header(&This->cinfo, TRUE);
320 if (ret != JPEG_HEADER_OK) {
321 WARN("Jpeg image in stream has bad format, read header returned %d.\n",ret);
322 LeaveCriticalSection(&This->lock);
326 switch (This->cinfo.jpeg_color_space)
329 This->cinfo.out_color_space = JCS_GRAYSCALE;
333 This->cinfo.out_color_space = JCS_RGB;
337 This->cinfo.out_color_space = JCS_CMYK;
340 ERR("Unknown JPEG color space %i\n", This->cinfo.jpeg_color_space);
341 LeaveCriticalSection(&This->lock);
345 if (!pjpeg_start_decompress(&This->cinfo))
347 ERR("jpeg_start_decompress failed\n");
348 LeaveCriticalSection(&This->lock);
352 This->initialized = TRUE;
354 LeaveCriticalSection(&This->lock);
359 static HRESULT WINAPI JpegDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
360 GUID *pguidContainerFormat)
362 memcpy(pguidContainerFormat, &GUID_ContainerFormatJpeg, sizeof(GUID));
366 static HRESULT WINAPI JpegDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
367 IWICBitmapDecoderInfo **ppIDecoderInfo)
370 IWICComponentInfo *compinfo;
372 TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
374 hr = CreateComponentInfo(&CLSID_WICJpegDecoder, &compinfo);
375 if (FAILED(hr)) return hr;
377 hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
378 (void**)ppIDecoderInfo);
380 IWICComponentInfo_Release(compinfo);
385 static HRESULT WINAPI JpegDecoder_CopyPalette(IWICBitmapDecoder *iface,
386 IWICPalette *pIPalette)
388 TRACE("(%p,%p)\n", iface, pIPalette);
390 return WINCODEC_ERR_PALETTEUNAVAILABLE;
393 static HRESULT WINAPI JpegDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
394 IWICMetadataQueryReader **ppIMetadataQueryReader)
396 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
400 static HRESULT WINAPI JpegDecoder_GetPreview(IWICBitmapDecoder *iface,
401 IWICBitmapSource **ppIBitmapSource)
403 FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
404 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
407 static HRESULT WINAPI JpegDecoder_GetColorContexts(IWICBitmapDecoder *iface,
408 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
410 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
411 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
414 static HRESULT WINAPI JpegDecoder_GetThumbnail(IWICBitmapDecoder *iface,
415 IWICBitmapSource **ppIThumbnail)
417 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
418 return WINCODEC_ERR_CODECNOTHUMBNAIL;
421 static HRESULT WINAPI JpegDecoder_GetFrameCount(IWICBitmapDecoder *iface,
428 static HRESULT WINAPI JpegDecoder_GetFrame(IWICBitmapDecoder *iface,
429 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
431 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
432 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
434 if (!This->initialized) return WINCODEC_ERR_NOTINITIALIZED;
436 if (index != 0) return E_INVALIDARG;
438 IWICBitmapDecoder_AddRef(iface);
439 *ppIBitmapFrame = &This->IWICBitmapFrameDecode_iface;
444 static const IWICBitmapDecoderVtbl JpegDecoder_Vtbl = {
445 JpegDecoder_QueryInterface,
448 JpegDecoder_QueryCapability,
449 JpegDecoder_Initialize,
450 JpegDecoder_GetContainerFormat,
451 JpegDecoder_GetDecoderInfo,
452 JpegDecoder_CopyPalette,
453 JpegDecoder_GetMetadataQueryReader,
454 JpegDecoder_GetPreview,
455 JpegDecoder_GetColorContexts,
456 JpegDecoder_GetThumbnail,
457 JpegDecoder_GetFrameCount,
461 static HRESULT WINAPI JpegDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
464 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
466 if (!ppv) return E_INVALIDARG;
468 if (IsEqualIID(&IID_IUnknown, iid) ||
469 IsEqualIID(&IID_IWICBitmapSource, iid) ||
470 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
477 return E_NOINTERFACE;
480 IUnknown_AddRef((IUnknown*)*ppv);
484 static ULONG WINAPI JpegDecoder_Frame_AddRef(IWICBitmapFrameDecode *iface)
486 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
487 return IUnknown_AddRef((IUnknown*)This);
490 static ULONG WINAPI JpegDecoder_Frame_Release(IWICBitmapFrameDecode *iface)
492 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
493 return IUnknown_Release((IUnknown*)This);
496 static HRESULT WINAPI JpegDecoder_Frame_GetSize(IWICBitmapFrameDecode *iface,
497 UINT *puiWidth, UINT *puiHeight)
499 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
500 *puiWidth = This->cinfo.output_width;
501 *puiHeight = This->cinfo.output_height;
502 TRACE("(%p)->(%u,%u)\n", iface, *puiWidth, *puiHeight);
506 static HRESULT WINAPI JpegDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
507 WICPixelFormatGUID *pPixelFormat)
509 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
510 TRACE("(%p,%p)\n", iface, pPixelFormat);
511 if (This->cinfo.out_color_space == JCS_RGB)
512 memcpy(pPixelFormat, &GUID_WICPixelFormat24bppBGR, sizeof(GUID));
513 else if (This->cinfo.out_color_space == JCS_CMYK)
514 memcpy(pPixelFormat, &GUID_WICPixelFormat32bppCMYK, sizeof(GUID));
515 else /* This->cinfo.out_color_space == JCS_GRAYSCALE */
516 memcpy(pPixelFormat, &GUID_WICPixelFormat8bppGray, sizeof(GUID));
520 static HRESULT WINAPI JpegDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
521 double *pDpiX, double *pDpiY)
523 FIXME("(%p,%p,%p): stub\n", iface, pDpiX, pDpiY);
527 static HRESULT WINAPI JpegDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
528 IWICPalette *pIPalette)
530 FIXME("(%p,%p): stub\n", iface, pIPalette);
534 static HRESULT WINAPI JpegDecoder_Frame_CopyPixels(IWICBitmapFrameDecode *iface,
535 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
537 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
544 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
550 rect.Width = This->cinfo.output_width;
551 rect.Height = This->cinfo.output_height;
556 if (prc->X < 0 || prc->Y < 0 || prc->X+prc->Width > This->cinfo.output_width ||
557 prc->Y+prc->Height > This->cinfo.output_height)
561 if (This->cinfo.out_color_space == JCS_GRAYSCALE) bpp = 8;
562 else if (This->cinfo.out_color_space == JCS_CMYK) bpp = 32;
565 stride = bpp * This->cinfo.output_width;
566 data_size = stride * This->cinfo.output_height;
568 max_row_needed = prc->Y + prc->Height;
569 if (max_row_needed > This->cinfo.output_height) return E_INVALIDARG;
571 EnterCriticalSection(&This->lock);
573 if (!This->image_data)
575 This->image_data = HeapAlloc(GetProcessHeap(), 0, data_size);
576 if (!This->image_data)
578 LeaveCriticalSection(&This->lock);
579 return E_OUTOFMEMORY;
583 This->cinfo.client_data = jmpbuf;
587 LeaveCriticalSection(&This->lock);
591 while (max_row_needed > This->cinfo.output_scanline)
593 UINT first_scanline = This->cinfo.output_scanline;
595 JSAMPROW out_rows[4];
599 max_rows = min(This->cinfo.output_height-first_scanline, 4);
600 for (i=0; i<max_rows; i++)
601 out_rows[i] = This->image_data + stride * (first_scanline+i);
603 ret = pjpeg_read_scanlines(&This->cinfo, out_rows, max_rows);
607 ERR("read_scanlines failed\n");
608 LeaveCriticalSection(&This->lock);
614 /* libjpeg gives us RGB data and we want BGR, so byteswap the data */
615 reverse_bgr8(3, This->image_data + stride * first_scanline,
616 This->cinfo.output_width, This->cinfo.output_scanline - first_scanline,
620 if (This->cinfo.out_color_space == JCS_CMYK && This->cinfo.saw_Adobe_marker)
621 /* Adobe JPEG's have inverted CMYK data. */
622 for (i=0; i<data_size; i++)
623 This->image_data[i] ^= 0xff;
626 LeaveCriticalSection(&This->lock);
628 return copy_pixels(bpp, This->image_data,
629 This->cinfo.output_width, This->cinfo.output_height, stride,
630 prc, cbStride, cbBufferSize, pbBuffer);
633 static HRESULT WINAPI JpegDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
634 IWICMetadataQueryReader **ppIMetadataQueryReader)
636 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
637 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
640 static HRESULT WINAPI JpegDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode *iface,
641 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
643 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
644 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
647 static HRESULT WINAPI JpegDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode *iface,
648 IWICBitmapSource **ppIThumbnail)
650 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
651 return WINCODEC_ERR_CODECNOTHUMBNAIL;
654 static const IWICBitmapFrameDecodeVtbl JpegDecoder_Frame_Vtbl = {
655 JpegDecoder_Frame_QueryInterface,
656 JpegDecoder_Frame_AddRef,
657 JpegDecoder_Frame_Release,
658 JpegDecoder_Frame_GetSize,
659 JpegDecoder_Frame_GetPixelFormat,
660 JpegDecoder_Frame_GetResolution,
661 JpegDecoder_Frame_CopyPalette,
662 JpegDecoder_Frame_CopyPixels,
663 JpegDecoder_Frame_GetMetadataQueryReader,
664 JpegDecoder_Frame_GetColorContexts,
665 JpegDecoder_Frame_GetThumbnail
668 HRESULT JpegDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
673 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
675 if (!libjpeg_handle && !load_libjpeg())
677 ERR("Failed reading JPEG because unable to find %s\n", SONAME_LIBJPEG);
683 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
685 This = HeapAlloc(GetProcessHeap(), 0, sizeof(JpegDecoder));
686 if (!This) return E_OUTOFMEMORY;
688 This->IWICBitmapDecoder_iface.lpVtbl = &JpegDecoder_Vtbl;
689 This->IWICBitmapFrameDecode_iface.lpVtbl = &JpegDecoder_Frame_Vtbl;
691 This->initialized = FALSE;
692 This->cinfo_initialized = FALSE;
694 This->image_data = NULL;
695 InitializeCriticalSection(&This->lock);
696 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JpegDecoder.lock");
698 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
699 IUnknown_Release((IUnknown*)This);
704 typedef struct JpegEncoder {
705 IWICBitmapEncoder IWICBitmapEncoder_iface;
707 struct jpeg_compress_struct cinfo;
708 struct jpeg_error_mgr jerr;
709 struct jpeg_destination_mgr dest_mgr;
712 CRITICAL_SECTION lock;
713 BYTE dest_buffer[1024];
716 static inline JpegEncoder *impl_from_IWICBitmapEncoder(IWICBitmapEncoder *iface)
718 return CONTAINING_RECORD(iface, JpegEncoder, IWICBitmapEncoder_iface);
721 static inline JpegEncoder *encoder_from_compress(j_compress_ptr compress)
723 return CONTAINING_RECORD(compress, JpegEncoder, cinfo);
726 static void dest_mgr_init_destination(j_compress_ptr cinfo)
728 JpegEncoder *This = encoder_from_compress(cinfo);
730 This->dest_mgr.next_output_byte = This->dest_buffer;
731 This->dest_mgr.free_in_buffer = sizeof(This->dest_buffer);
734 static jpeg_boolean dest_mgr_empty_output_buffer(j_compress_ptr cinfo)
736 JpegEncoder *This = encoder_from_compress(cinfo);
740 hr = IStream_Write(This->stream, This->dest_buffer,
741 sizeof(This->dest_buffer), &byteswritten);
743 if (hr != S_OK || byteswritten == 0)
745 ERR("Failed writing data, hr=%x\n", hr);
749 This->dest_mgr.next_output_byte = This->dest_buffer;
750 This->dest_mgr.free_in_buffer = sizeof(This->dest_buffer);
754 static void dest_mgr_term_destination(j_compress_ptr cinfo)
756 JpegEncoder *This = encoder_from_compress(cinfo);
760 if (This->dest_mgr.free_in_buffer != sizeof(This->dest_buffer))
762 hr = IStream_Write(This->stream, This->dest_buffer,
763 sizeof(This->dest_buffer) - This->dest_mgr.free_in_buffer, &byteswritten);
765 if (hr != S_OK || byteswritten == 0)
766 ERR("Failed writing data, hr=%x\n", hr);
770 static HRESULT WINAPI JpegEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
773 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
774 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
776 if (!ppv) return E_INVALIDARG;
778 if (IsEqualIID(&IID_IUnknown, iid) ||
779 IsEqualIID(&IID_IWICBitmapEncoder, iid))
786 return E_NOINTERFACE;
789 IUnknown_AddRef((IUnknown*)*ppv);
793 static ULONG WINAPI JpegEncoder_AddRef(IWICBitmapEncoder *iface)
795 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
796 ULONG ref = InterlockedIncrement(&This->ref);
798 TRACE("(%p) refcount=%u\n", iface, ref);
803 static ULONG WINAPI JpegEncoder_Release(IWICBitmapEncoder *iface)
805 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
806 ULONG ref = InterlockedDecrement(&This->ref);
808 TRACE("(%p) refcount=%u\n", iface, ref);
812 This->lock.DebugInfo->Spare[0] = 0;
813 DeleteCriticalSection(&This->lock);
814 if (This->initialized) pjpeg_destroy_compress(&This->cinfo);
815 if (This->stream) IStream_Release(This->stream);
816 HeapFree(GetProcessHeap(), 0, This);
822 static HRESULT WINAPI JpegEncoder_Initialize(IWICBitmapEncoder *iface,
823 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
825 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
828 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
830 EnterCriticalSection(&This->lock);
832 if (This->initialized)
834 LeaveCriticalSection(&This->lock);
835 return WINCODEC_ERR_WRONGSTATE;
838 pjpeg_std_error(&This->jerr);
840 This->jerr.error_exit = error_exit_fn;
841 This->jerr.emit_message = emit_message_fn;
843 This->cinfo.err = &This->jerr;
845 This->cinfo.client_data = &jmpbuf;
849 LeaveCriticalSection(&This->lock);
853 pjpeg_CreateCompress(&This->cinfo, JPEG_LIB_VERSION, sizeof(struct jpeg_compress_struct));
855 This->stream = pIStream;
856 IStream_AddRef(pIStream);
858 This->dest_mgr.next_output_byte = This->dest_buffer;
859 This->dest_mgr.free_in_buffer = sizeof(This->dest_buffer);
861 This->dest_mgr.init_destination = dest_mgr_init_destination;
862 This->dest_mgr.empty_output_buffer = dest_mgr_empty_output_buffer;
863 This->dest_mgr.term_destination = dest_mgr_term_destination;
865 This->cinfo.dest = &This->dest_mgr;
867 This->initialized = TRUE;
869 LeaveCriticalSection(&This->lock);
874 static HRESULT WINAPI JpegEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
875 GUID *pguidContainerFormat)
877 FIXME("(%p,%s): stub\n", iface, debugstr_guid(pguidContainerFormat));
881 static HRESULT WINAPI JpegEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
882 IWICBitmapEncoderInfo **ppIEncoderInfo)
884 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
888 static HRESULT WINAPI JpegEncoder_SetColorContexts(IWICBitmapEncoder *iface,
889 UINT cCount, IWICColorContext **ppIColorContext)
891 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
895 static HRESULT WINAPI JpegEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
897 TRACE("(%p,%p)\n", iface, pIPalette);
898 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
901 static HRESULT WINAPI JpegEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
903 TRACE("(%p,%p)\n", iface, pIThumbnail);
904 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
907 static HRESULT WINAPI JpegEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
909 TRACE("(%p,%p)\n", iface, pIPreview);
910 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
913 static HRESULT WINAPI JpegEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
914 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
916 FIXME("(%p,%p,%p): stub\n", iface, ppIFrameEncode, ppIEncoderOptions);
920 static HRESULT WINAPI JpegEncoder_Commit(IWICBitmapEncoder *iface)
922 FIXME("(%p): stub\n", iface);
926 static HRESULT WINAPI JpegEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
927 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
929 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
933 static const IWICBitmapEncoderVtbl JpegEncoder_Vtbl = {
934 JpegEncoder_QueryInterface,
937 JpegEncoder_Initialize,
938 JpegEncoder_GetContainerFormat,
939 JpegEncoder_GetEncoderInfo,
940 JpegEncoder_SetColorContexts,
941 JpegEncoder_SetPalette,
942 JpegEncoder_SetThumbnail,
943 JpegEncoder_SetPreview,
944 JpegEncoder_CreateNewFrame,
946 JpegEncoder_GetMetadataQueryWriter
949 HRESULT JpegEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
954 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
958 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
960 if (!libjpeg_handle && !load_libjpeg())
962 ERR("Failed writing JPEG because unable to find %s\n",SONAME_LIBJPEG);
966 This = HeapAlloc(GetProcessHeap(), 0, sizeof(JpegEncoder));
967 if (!This) return E_OUTOFMEMORY;
969 This->IWICBitmapEncoder_iface.lpVtbl = &JpegEncoder_Vtbl;
971 This->initialized = 0;
973 InitializeCriticalSection(&This->lock);
974 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JpegEncoder.lock");
976 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
977 IUnknown_Release((IUnknown*)This);
982 #else /* !defined(SONAME_LIBJPEG) */
984 HRESULT JpegDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
986 ERR("Trying to load JPEG picture, but JPEG support is not compiled in.\n");
990 HRESULT JpegEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
992 ERR("Trying to save JPEG picture, but JPEG support is not compiled in.\n");