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) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
185 return E_NOINTERFACE;
188 IUnknown_AddRef((IUnknown*)*ppv);
192 static ULONG WINAPI JpegDecoder_AddRef(IWICBitmapDecoder *iface)
194 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
195 ULONG ref = InterlockedIncrement(&This->ref);
197 TRACE("(%p) refcount=%u\n", iface, ref);
202 static ULONG WINAPI JpegDecoder_Release(IWICBitmapDecoder *iface)
204 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
205 ULONG ref = InterlockedDecrement(&This->ref);
207 TRACE("(%p) refcount=%u\n", iface, ref);
211 This->lock.DebugInfo->Spare[0] = 0;
212 DeleteCriticalSection(&This->lock);
213 if (This->cinfo_initialized) pjpeg_destroy_decompress(&This->cinfo);
214 if (This->stream) IStream_Release(This->stream);
215 HeapFree(GetProcessHeap(), 0, This->image_data);
216 HeapFree(GetProcessHeap(), 0, This);
222 static HRESULT WINAPI JpegDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
223 DWORD *pdwCapability)
225 FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
229 static void source_mgr_init_source(j_decompress_ptr cinfo)
233 static jpeg_boolean source_mgr_fill_input_buffer(j_decompress_ptr cinfo)
235 JpegDecoder *This = decoder_from_decompress(cinfo);
239 hr = IStream_Read(This->stream, This->source_buffer, 1024, &bytesread);
241 if (hr != S_OK || bytesread == 0)
247 This->source_mgr.next_input_byte = This->source_buffer;
248 This->source_mgr.bytes_in_buffer = bytesread;
253 static void source_mgr_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
255 JpegDecoder *This = decoder_from_decompress(cinfo);
258 if (num_bytes > This->source_mgr.bytes_in_buffer)
260 seek.QuadPart = num_bytes - This->source_mgr.bytes_in_buffer;
261 IStream_Seek(This->stream, seek, STREAM_SEEK_CUR, NULL);
262 This->source_mgr.bytes_in_buffer = 0;
264 else if (num_bytes > 0)
266 This->source_mgr.next_input_byte += num_bytes;
267 This->source_mgr.bytes_in_buffer -= num_bytes;
271 static void source_mgr_term_source(j_decompress_ptr cinfo)
275 static HRESULT WINAPI JpegDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
276 WICDecodeOptions cacheOptions)
278 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
282 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOptions);
284 EnterCriticalSection(&This->lock);
286 if (This->cinfo_initialized)
288 LeaveCriticalSection(&This->lock);
289 return WINCODEC_ERR_WRONGSTATE;
292 pjpeg_std_error(&This->jerr);
294 This->jerr.error_exit = error_exit_fn;
295 This->jerr.emit_message = emit_message_fn;
297 This->cinfo.err = &This->jerr;
299 This->cinfo.client_data = jmpbuf;
303 LeaveCriticalSection(&This->lock);
307 pjpeg_CreateDecompress(&This->cinfo, JPEG_LIB_VERSION, sizeof(struct jpeg_decompress_struct));
309 This->cinfo_initialized = TRUE;
311 This->stream = pIStream;
312 IStream_AddRef(pIStream);
315 IStream_Seek(This->stream, seek, STREAM_SEEK_SET, NULL);
317 This->source_mgr.bytes_in_buffer = 0;
318 This->source_mgr.init_source = source_mgr_init_source;
319 This->source_mgr.fill_input_buffer = source_mgr_fill_input_buffer;
320 This->source_mgr.skip_input_data = source_mgr_skip_input_data;
321 This->source_mgr.resync_to_restart = pjpeg_resync_to_restart;
322 This->source_mgr.term_source = source_mgr_term_source;
324 This->cinfo.src = &This->source_mgr;
326 ret = pjpeg_read_header(&This->cinfo, TRUE);
328 if (ret != JPEG_HEADER_OK) {
329 WARN("Jpeg image in stream has bad format, read header returned %d.\n",ret);
330 LeaveCriticalSection(&This->lock);
334 switch (This->cinfo.jpeg_color_space)
337 This->cinfo.out_color_space = JCS_GRAYSCALE;
341 This->cinfo.out_color_space = JCS_RGB;
345 This->cinfo.out_color_space = JCS_CMYK;
348 ERR("Unknown JPEG color space %i\n", This->cinfo.jpeg_color_space);
349 LeaveCriticalSection(&This->lock);
353 if (!pjpeg_start_decompress(&This->cinfo))
355 ERR("jpeg_start_decompress failed\n");
356 LeaveCriticalSection(&This->lock);
360 This->initialized = TRUE;
362 LeaveCriticalSection(&This->lock);
367 static HRESULT WINAPI JpegDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
368 GUID *pguidContainerFormat)
370 memcpy(pguidContainerFormat, &GUID_ContainerFormatJpeg, sizeof(GUID));
374 static HRESULT WINAPI JpegDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
375 IWICBitmapDecoderInfo **ppIDecoderInfo)
378 IWICComponentInfo *compinfo;
380 TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
382 hr = CreateComponentInfo(&CLSID_WICJpegDecoder, &compinfo);
383 if (FAILED(hr)) return hr;
385 hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
386 (void**)ppIDecoderInfo);
388 IWICComponentInfo_Release(compinfo);
393 static HRESULT WINAPI JpegDecoder_CopyPalette(IWICBitmapDecoder *iface,
394 IWICPalette *pIPalette)
396 TRACE("(%p,%p)\n", iface, pIPalette);
398 return WINCODEC_ERR_PALETTEUNAVAILABLE;
401 static HRESULT WINAPI JpegDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
402 IWICMetadataQueryReader **ppIMetadataQueryReader)
404 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
408 static HRESULT WINAPI JpegDecoder_GetPreview(IWICBitmapDecoder *iface,
409 IWICBitmapSource **ppIBitmapSource)
411 FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
412 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
415 static HRESULT WINAPI JpegDecoder_GetColorContexts(IWICBitmapDecoder *iface,
416 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
418 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
419 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
422 static HRESULT WINAPI JpegDecoder_GetThumbnail(IWICBitmapDecoder *iface,
423 IWICBitmapSource **ppIThumbnail)
425 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
426 return WINCODEC_ERR_CODECNOTHUMBNAIL;
429 static HRESULT WINAPI JpegDecoder_GetFrameCount(IWICBitmapDecoder *iface,
436 static HRESULT WINAPI JpegDecoder_GetFrame(IWICBitmapDecoder *iface,
437 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
439 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
440 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
442 if (!This->initialized) return WINCODEC_ERR_NOTINITIALIZED;
444 if (index != 0) return E_INVALIDARG;
446 IWICBitmapDecoder_AddRef(iface);
447 *ppIBitmapFrame = &This->IWICBitmapFrameDecode_iface;
452 static const IWICBitmapDecoderVtbl JpegDecoder_Vtbl = {
453 JpegDecoder_QueryInterface,
456 JpegDecoder_QueryCapability,
457 JpegDecoder_Initialize,
458 JpegDecoder_GetContainerFormat,
459 JpegDecoder_GetDecoderInfo,
460 JpegDecoder_CopyPalette,
461 JpegDecoder_GetMetadataQueryReader,
462 JpegDecoder_GetPreview,
463 JpegDecoder_GetColorContexts,
464 JpegDecoder_GetThumbnail,
465 JpegDecoder_GetFrameCount,
469 static HRESULT WINAPI JpegDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
472 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
474 if (!ppv) return E_INVALIDARG;
476 if (IsEqualIID(&IID_IUnknown, iid) ||
477 IsEqualIID(&IID_IWICBitmapSource, iid) ||
478 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
485 return E_NOINTERFACE;
488 IUnknown_AddRef((IUnknown*)*ppv);
492 static ULONG WINAPI JpegDecoder_Frame_AddRef(IWICBitmapFrameDecode *iface)
494 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
495 return IUnknown_AddRef((IUnknown*)This);
498 static ULONG WINAPI JpegDecoder_Frame_Release(IWICBitmapFrameDecode *iface)
500 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
501 return IUnknown_Release((IUnknown*)This);
504 static HRESULT WINAPI JpegDecoder_Frame_GetSize(IWICBitmapFrameDecode *iface,
505 UINT *puiWidth, UINT *puiHeight)
507 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
508 *puiWidth = This->cinfo.output_width;
509 *puiHeight = This->cinfo.output_height;
510 TRACE("(%p)->(%u,%u)\n", iface, *puiWidth, *puiHeight);
514 static HRESULT WINAPI JpegDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
515 WICPixelFormatGUID *pPixelFormat)
517 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
518 TRACE("(%p,%p)\n", iface, pPixelFormat);
519 if (This->cinfo.out_color_space == JCS_RGB)
520 memcpy(pPixelFormat, &GUID_WICPixelFormat24bppBGR, sizeof(GUID));
521 else if (This->cinfo.out_color_space == JCS_CMYK)
522 memcpy(pPixelFormat, &GUID_WICPixelFormat32bppCMYK, sizeof(GUID));
523 else /* This->cinfo.out_color_space == JCS_GRAYSCALE */
524 memcpy(pPixelFormat, &GUID_WICPixelFormat8bppGray, sizeof(GUID));
528 static HRESULT WINAPI JpegDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
529 double *pDpiX, double *pDpiY)
531 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
533 EnterCriticalSection(&This->lock);
535 if (This->cinfo.density_unit == 2) /* pixels per centimeter */
537 *pDpiX = This->cinfo.X_density * 2.54;
538 *pDpiY = This->cinfo.Y_density * 2.54;
542 /* 1 = pixels per inch, 0 = unknown */
543 *pDpiX = This->cinfo.X_density;
544 *pDpiY = This->cinfo.Y_density;
547 LeaveCriticalSection(&This->lock);
549 TRACE("(%p)->(%0.2f,%0.2f)\n", iface, *pDpiX, *pDpiY);
554 static HRESULT WINAPI JpegDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
555 IWICPalette *pIPalette)
557 FIXME("(%p,%p): stub\n", iface, pIPalette);
561 static HRESULT WINAPI JpegDecoder_Frame_CopyPixels(IWICBitmapFrameDecode *iface,
562 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
564 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
571 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
577 rect.Width = This->cinfo.output_width;
578 rect.Height = This->cinfo.output_height;
583 if (prc->X < 0 || prc->Y < 0 || prc->X+prc->Width > This->cinfo.output_width ||
584 prc->Y+prc->Height > This->cinfo.output_height)
588 if (This->cinfo.out_color_space == JCS_GRAYSCALE) bpp = 8;
589 else if (This->cinfo.out_color_space == JCS_CMYK) bpp = 32;
592 stride = bpp * This->cinfo.output_width;
593 data_size = stride * This->cinfo.output_height;
595 max_row_needed = prc->Y + prc->Height;
596 if (max_row_needed > This->cinfo.output_height) return E_INVALIDARG;
598 EnterCriticalSection(&This->lock);
600 if (!This->image_data)
602 This->image_data = HeapAlloc(GetProcessHeap(), 0, data_size);
603 if (!This->image_data)
605 LeaveCriticalSection(&This->lock);
606 return E_OUTOFMEMORY;
610 This->cinfo.client_data = jmpbuf;
614 LeaveCriticalSection(&This->lock);
618 while (max_row_needed > This->cinfo.output_scanline)
620 UINT first_scanline = This->cinfo.output_scanline;
622 JSAMPROW out_rows[4];
626 max_rows = min(This->cinfo.output_height-first_scanline, 4);
627 for (i=0; i<max_rows; i++)
628 out_rows[i] = This->image_data + stride * (first_scanline+i);
630 ret = pjpeg_read_scanlines(&This->cinfo, out_rows, max_rows);
634 ERR("read_scanlines failed\n");
635 LeaveCriticalSection(&This->lock);
641 /* libjpeg gives us RGB data and we want BGR, so byteswap the data */
642 reverse_bgr8(3, This->image_data + stride * first_scanline,
643 This->cinfo.output_width, This->cinfo.output_scanline - first_scanline,
647 if (This->cinfo.out_color_space == JCS_CMYK && This->cinfo.saw_Adobe_marker)
648 /* Adobe JPEG's have inverted CMYK data. */
649 for (i=0; i<data_size; i++)
650 This->image_data[i] ^= 0xff;
653 LeaveCriticalSection(&This->lock);
655 return copy_pixels(bpp, This->image_data,
656 This->cinfo.output_width, This->cinfo.output_height, stride,
657 prc, cbStride, cbBufferSize, pbBuffer);
660 static HRESULT WINAPI JpegDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
661 IWICMetadataQueryReader **ppIMetadataQueryReader)
663 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
664 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
667 static HRESULT WINAPI JpegDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode *iface,
668 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
670 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
671 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
674 static HRESULT WINAPI JpegDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode *iface,
675 IWICBitmapSource **ppIThumbnail)
677 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
678 return WINCODEC_ERR_CODECNOTHUMBNAIL;
681 static const IWICBitmapFrameDecodeVtbl JpegDecoder_Frame_Vtbl = {
682 JpegDecoder_Frame_QueryInterface,
683 JpegDecoder_Frame_AddRef,
684 JpegDecoder_Frame_Release,
685 JpegDecoder_Frame_GetSize,
686 JpegDecoder_Frame_GetPixelFormat,
687 JpegDecoder_Frame_GetResolution,
688 JpegDecoder_Frame_CopyPalette,
689 JpegDecoder_Frame_CopyPixels,
690 JpegDecoder_Frame_GetMetadataQueryReader,
691 JpegDecoder_Frame_GetColorContexts,
692 JpegDecoder_Frame_GetThumbnail
695 HRESULT JpegDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
700 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
702 if (!libjpeg_handle && !load_libjpeg())
704 ERR("Failed reading JPEG because unable to find %s\n", SONAME_LIBJPEG);
710 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
712 This = HeapAlloc(GetProcessHeap(), 0, sizeof(JpegDecoder));
713 if (!This) return E_OUTOFMEMORY;
715 This->IWICBitmapDecoder_iface.lpVtbl = &JpegDecoder_Vtbl;
716 This->IWICBitmapFrameDecode_iface.lpVtbl = &JpegDecoder_Frame_Vtbl;
718 This->initialized = FALSE;
719 This->cinfo_initialized = FALSE;
721 This->image_data = NULL;
722 InitializeCriticalSection(&This->lock);
723 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JpegDecoder.lock");
725 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
726 IUnknown_Release((IUnknown*)This);
731 typedef struct jpeg_compress_format {
732 const WICPixelFormatGUID *guid;
735 J_COLOR_SPACE color_space;
737 } jpeg_compress_format;
739 static const jpeg_compress_format compress_formats[] = {
740 { &GUID_WICPixelFormat24bppBGR, 24, 3, JCS_RGB, 1 },
741 { &GUID_WICPixelFormat32bppCMYK, 32, 4, JCS_CMYK },
742 { &GUID_WICPixelFormat8bppGray, 8, 1, JCS_GRAYSCALE },
746 typedef struct JpegEncoder {
747 IWICBitmapEncoder IWICBitmapEncoder_iface;
748 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface;
750 struct jpeg_compress_struct cinfo;
751 struct jpeg_error_mgr jerr;
752 struct jpeg_destination_mgr dest_mgr;
755 int frame_initialized;
756 int started_compress;
762 const jpeg_compress_format *format;
764 CRITICAL_SECTION lock;
765 BYTE dest_buffer[1024];
768 static inline JpegEncoder *impl_from_IWICBitmapEncoder(IWICBitmapEncoder *iface)
770 return CONTAINING_RECORD(iface, JpegEncoder, IWICBitmapEncoder_iface);
773 static inline JpegEncoder *impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode *iface)
775 return CONTAINING_RECORD(iface, JpegEncoder, IWICBitmapFrameEncode_iface);
778 static inline JpegEncoder *encoder_from_compress(j_compress_ptr compress)
780 return CONTAINING_RECORD(compress, JpegEncoder, cinfo);
783 static void dest_mgr_init_destination(j_compress_ptr cinfo)
785 JpegEncoder *This = encoder_from_compress(cinfo);
787 This->dest_mgr.next_output_byte = This->dest_buffer;
788 This->dest_mgr.free_in_buffer = sizeof(This->dest_buffer);
791 static jpeg_boolean dest_mgr_empty_output_buffer(j_compress_ptr cinfo)
793 JpegEncoder *This = encoder_from_compress(cinfo);
797 hr = IStream_Write(This->stream, This->dest_buffer,
798 sizeof(This->dest_buffer), &byteswritten);
800 if (hr != S_OK || byteswritten == 0)
802 ERR("Failed writing data, hr=%x\n", hr);
806 This->dest_mgr.next_output_byte = This->dest_buffer;
807 This->dest_mgr.free_in_buffer = sizeof(This->dest_buffer);
811 static void dest_mgr_term_destination(j_compress_ptr cinfo)
813 JpegEncoder *This = encoder_from_compress(cinfo);
817 if (This->dest_mgr.free_in_buffer != sizeof(This->dest_buffer))
819 hr = IStream_Write(This->stream, This->dest_buffer,
820 sizeof(This->dest_buffer) - This->dest_mgr.free_in_buffer, &byteswritten);
822 if (hr != S_OK || byteswritten == 0)
823 ERR("Failed writing data, hr=%x\n", hr);
827 static HRESULT WINAPI JpegEncoder_Frame_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid,
830 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
831 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
833 if (!ppv) return E_INVALIDARG;
835 if (IsEqualIID(&IID_IUnknown, iid) ||
836 IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
838 *ppv = &This->IWICBitmapFrameEncode_iface;
843 return E_NOINTERFACE;
846 IUnknown_AddRef((IUnknown*)*ppv);
850 static ULONG WINAPI JpegEncoder_Frame_AddRef(IWICBitmapFrameEncode *iface)
852 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
853 return IWICBitmapEncoder_AddRef(&This->IWICBitmapEncoder_iface);
856 static ULONG WINAPI JpegEncoder_Frame_Release(IWICBitmapFrameEncode *iface)
858 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
859 return IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
862 static HRESULT WINAPI JpegEncoder_Frame_Initialize(IWICBitmapFrameEncode *iface,
863 IPropertyBag2 *pIEncoderOptions)
865 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
866 TRACE("(%p,%p)\n", iface, pIEncoderOptions);
868 EnterCriticalSection(&This->lock);
870 if (This->frame_initialized)
872 LeaveCriticalSection(&This->lock);
873 return WINCODEC_ERR_WRONGSTATE;
876 This->frame_initialized = TRUE;
878 LeaveCriticalSection(&This->lock);
883 static HRESULT WINAPI JpegEncoder_Frame_SetSize(IWICBitmapFrameEncode *iface,
884 UINT uiWidth, UINT uiHeight)
886 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
887 TRACE("(%p,%u,%u)\n", iface, uiWidth, uiHeight);
889 EnterCriticalSection(&This->lock);
891 if (!This->frame_initialized || This->started_compress)
893 LeaveCriticalSection(&This->lock);
894 return WINCODEC_ERR_WRONGSTATE;
897 This->width = uiWidth;
898 This->height = uiHeight;
900 LeaveCriticalSection(&This->lock);
905 static HRESULT WINAPI JpegEncoder_Frame_SetResolution(IWICBitmapFrameEncode *iface,
906 double dpiX, double dpiY)
908 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
909 TRACE("(%p,%0.2f,%0.2f)\n", iface, dpiX, dpiY);
911 EnterCriticalSection(&This->lock);
913 if (!This->frame_initialized || This->started_compress)
915 LeaveCriticalSection(&This->lock);
916 return WINCODEC_ERR_WRONGSTATE;
922 LeaveCriticalSection(&This->lock);
927 static HRESULT WINAPI JpegEncoder_Frame_SetPixelFormat(IWICBitmapFrameEncode *iface,
928 WICPixelFormatGUID *pPixelFormat)
930 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
932 TRACE("(%p,%s)\n", iface, debugstr_guid(pPixelFormat));
934 EnterCriticalSection(&This->lock);
936 if (!This->frame_initialized || This->started_compress)
938 LeaveCriticalSection(&This->lock);
939 return WINCODEC_ERR_WRONGSTATE;
942 for (i=0; compress_formats[i].guid; i++)
944 if (memcmp(compress_formats[i].guid, pPixelFormat, sizeof(GUID)) == 0)
948 if (!compress_formats[i].guid) i = 0;
950 This->format = &compress_formats[i];
951 memcpy(pPixelFormat, This->format->guid, sizeof(GUID));
953 LeaveCriticalSection(&This->lock);
958 static HRESULT WINAPI JpegEncoder_Frame_SetColorContexts(IWICBitmapFrameEncode *iface,
959 UINT cCount, IWICColorContext **ppIColorContext)
961 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
965 static HRESULT WINAPI JpegEncoder_Frame_SetPalette(IWICBitmapFrameEncode *iface,
966 IWICPalette *pIPalette)
968 FIXME("(%p,%p): stub\n", iface, pIPalette);
969 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
972 static HRESULT WINAPI JpegEncoder_Frame_SetThumbnail(IWICBitmapFrameEncode *iface,
973 IWICBitmapSource *pIThumbnail)
975 FIXME("(%p,%p): stub\n", iface, pIThumbnail);
976 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
979 static HRESULT WINAPI JpegEncoder_Frame_WritePixels(IWICBitmapFrameEncode *iface,
980 UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
982 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
984 BYTE *swapped_data = NULL, *current_row;
986 TRACE("(%p,%u,%u,%u,%p)\n", iface, lineCount, cbStride, cbBufferSize, pbPixels);
988 EnterCriticalSection(&This->lock);
990 if (!This->frame_initialized || !This->width || !This->height || !This->format)
992 LeaveCriticalSection(&This->lock);
993 return WINCODEC_ERR_WRONGSTATE;
996 if (lineCount == 0 || lineCount + This->lines_written > This->height)
998 LeaveCriticalSection(&This->lock);
1002 /* set up setjmp/longjmp error handling */
1005 LeaveCriticalSection(&This->lock);
1006 HeapFree(GetProcessHeap(), 0, swapped_data);
1009 This->cinfo.client_data = &jmpbuf;
1011 if (!This->started_compress)
1013 This->cinfo.image_width = This->width;
1014 This->cinfo.image_height = This->height;
1015 This->cinfo.input_components = This->format->num_components;
1016 This->cinfo.in_color_space = This->format->color_space;
1018 pjpeg_set_defaults(&This->cinfo);
1020 if (This->xres != 0.0 && This->yres != 0.0)
1022 This->cinfo.density_unit = 1; /* dots per inch */
1023 This->cinfo.X_density = This->xres;
1024 This->cinfo.Y_density = This->yres;
1027 pjpeg_start_compress(&This->cinfo, TRUE);
1029 This->started_compress = 1;
1032 row_size = This->format->bpp / 8 * This->width;
1034 if (This->format->swap_rgb)
1036 swapped_data = HeapAlloc(GetProcessHeap(), 0, row_size);
1039 LeaveCriticalSection(&This->lock);
1040 return E_OUTOFMEMORY;
1044 for (line=0; line < lineCount; line++)
1046 if (This->format->swap_rgb)
1050 memcpy(swapped_data, pbPixels + (cbStride * line), row_size);
1052 for (x=0; x < This->width; x++)
1056 b = swapped_data[x*3];
1057 swapped_data[x*3] = swapped_data[x*3+2];
1058 swapped_data[x*3+2] = b;
1061 current_row = swapped_data;
1064 current_row = pbPixels + (cbStride * line);
1066 if (!pjpeg_write_scanlines(&This->cinfo, ¤t_row, 1))
1068 ERR("failed writing scanlines\n");
1069 LeaveCriticalSection(&This->lock);
1070 HeapFree(GetProcessHeap(), 0, swapped_data);
1074 This->lines_written++;
1077 LeaveCriticalSection(&This->lock);
1078 HeapFree(GetProcessHeap(), 0, swapped_data);
1083 static HRESULT WINAPI JpegEncoder_Frame_WriteSource(IWICBitmapFrameEncode *iface,
1084 IWICBitmapSource *pIBitmapSource, WICRect *prc)
1086 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1089 WICPixelFormatGUID guid;
1092 TRACE("(%p,%p,%p)\n", iface, pIBitmapSource, prc);
1094 if (!This->frame_initialized || !This->width || !This->height)
1095 return WINCODEC_ERR_WRONGSTATE;
1099 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1100 if (FAILED(hr)) return hr;
1101 hr = IWICBitmapFrameEncode_SetPixelFormat(iface, &guid);
1102 if (FAILED(hr)) return hr;
1105 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1106 if (FAILED(hr)) return hr;
1107 if (memcmp(&guid, This->format->guid, sizeof(GUID)) != 0)
1109 /* FIXME: should use WICConvertBitmapSource to convert */
1110 ERR("format %s unsupported\n", debugstr_guid(&guid));
1114 if (This->xres == 0.0 || This->yres == 0.0)
1117 hr = IWICBitmapSource_GetResolution(pIBitmapSource, &xres, &yres);
1118 if (FAILED(hr)) return hr;
1119 hr = IWICBitmapFrameEncode_SetResolution(iface, xres, yres);
1120 if (FAILED(hr)) return hr;
1126 hr = IWICBitmapSource_GetSize(pIBitmapSource, &width, &height);
1127 if (FAILED(hr)) return hr;
1135 if (prc->Width != This->width) return E_INVALIDARG;
1137 stride = (This->format->bpp * This->width + 7)/8;
1139 pixeldata = HeapAlloc(GetProcessHeap(), 0, stride * prc->Height);
1140 if (!pixeldata) return E_OUTOFMEMORY;
1142 hr = IWICBitmapSource_CopyPixels(pIBitmapSource, prc, stride,
1143 stride*prc->Height, pixeldata);
1147 hr = IWICBitmapFrameEncode_WritePixels(iface, prc->Height, stride,
1148 stride*prc->Height, pixeldata);
1151 HeapFree(GetProcessHeap(), 0, pixeldata);
1156 static HRESULT WINAPI JpegEncoder_Frame_Commit(IWICBitmapFrameEncode *iface)
1158 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1160 TRACE("(%p)\n", iface);
1162 EnterCriticalSection(&This->lock);
1164 if (!This->started_compress || This->lines_written != This->height || This->frame_committed)
1166 LeaveCriticalSection(&This->lock);
1167 return WINCODEC_ERR_WRONGSTATE;
1170 /* set up setjmp/longjmp error handling */
1173 LeaveCriticalSection(&This->lock);
1176 This->cinfo.client_data = &jmpbuf;
1178 pjpeg_finish_compress(&This->cinfo);
1180 This->frame_committed = TRUE;
1182 LeaveCriticalSection(&This->lock);
1187 static HRESULT WINAPI JpegEncoder_Frame_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
1188 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1190 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
1194 static const IWICBitmapFrameEncodeVtbl JpegEncoder_FrameVtbl = {
1195 JpegEncoder_Frame_QueryInterface,
1196 JpegEncoder_Frame_AddRef,
1197 JpegEncoder_Frame_Release,
1198 JpegEncoder_Frame_Initialize,
1199 JpegEncoder_Frame_SetSize,
1200 JpegEncoder_Frame_SetResolution,
1201 JpegEncoder_Frame_SetPixelFormat,
1202 JpegEncoder_Frame_SetColorContexts,
1203 JpegEncoder_Frame_SetPalette,
1204 JpegEncoder_Frame_SetThumbnail,
1205 JpegEncoder_Frame_WritePixels,
1206 JpegEncoder_Frame_WriteSource,
1207 JpegEncoder_Frame_Commit,
1208 JpegEncoder_Frame_GetMetadataQueryWriter
1211 static HRESULT WINAPI JpegEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
1214 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
1215 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1217 if (!ppv) return E_INVALIDARG;
1219 if (IsEqualIID(&IID_IUnknown, iid) ||
1220 IsEqualIID(&IID_IWICBitmapEncoder, iid))
1227 return E_NOINTERFACE;
1230 IUnknown_AddRef((IUnknown*)*ppv);
1234 static ULONG WINAPI JpegEncoder_AddRef(IWICBitmapEncoder *iface)
1236 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
1237 ULONG ref = InterlockedIncrement(&This->ref);
1239 TRACE("(%p) refcount=%u\n", iface, ref);
1244 static ULONG WINAPI JpegEncoder_Release(IWICBitmapEncoder *iface)
1246 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
1247 ULONG ref = InterlockedDecrement(&This->ref);
1249 TRACE("(%p) refcount=%u\n", iface, ref);
1253 This->lock.DebugInfo->Spare[0] = 0;
1254 DeleteCriticalSection(&This->lock);
1255 if (This->initialized) pjpeg_destroy_compress(&This->cinfo);
1256 if (This->stream) IStream_Release(This->stream);
1257 HeapFree(GetProcessHeap(), 0, This);
1263 static HRESULT WINAPI JpegEncoder_Initialize(IWICBitmapEncoder *iface,
1264 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
1266 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
1269 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
1271 EnterCriticalSection(&This->lock);
1273 if (This->initialized)
1275 LeaveCriticalSection(&This->lock);
1276 return WINCODEC_ERR_WRONGSTATE;
1279 pjpeg_std_error(&This->jerr);
1281 This->jerr.error_exit = error_exit_fn;
1282 This->jerr.emit_message = emit_message_fn;
1284 This->cinfo.err = &This->jerr;
1286 This->cinfo.client_data = &jmpbuf;
1290 LeaveCriticalSection(&This->lock);
1294 pjpeg_CreateCompress(&This->cinfo, JPEG_LIB_VERSION, sizeof(struct jpeg_compress_struct));
1296 This->stream = pIStream;
1297 IStream_AddRef(pIStream);
1299 This->dest_mgr.next_output_byte = This->dest_buffer;
1300 This->dest_mgr.free_in_buffer = sizeof(This->dest_buffer);
1302 This->dest_mgr.init_destination = dest_mgr_init_destination;
1303 This->dest_mgr.empty_output_buffer = dest_mgr_empty_output_buffer;
1304 This->dest_mgr.term_destination = dest_mgr_term_destination;
1306 This->cinfo.dest = &This->dest_mgr;
1308 This->initialized = TRUE;
1310 LeaveCriticalSection(&This->lock);
1315 static HRESULT WINAPI JpegEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
1316 GUID *pguidContainerFormat)
1318 FIXME("(%p,%s): stub\n", iface, debugstr_guid(pguidContainerFormat));
1322 static HRESULT WINAPI JpegEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
1323 IWICBitmapEncoderInfo **ppIEncoderInfo)
1325 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
1329 static HRESULT WINAPI JpegEncoder_SetColorContexts(IWICBitmapEncoder *iface,
1330 UINT cCount, IWICColorContext **ppIColorContext)
1332 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1336 static HRESULT WINAPI JpegEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
1338 TRACE("(%p,%p)\n", iface, pIPalette);
1339 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1342 static HRESULT WINAPI JpegEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
1344 TRACE("(%p,%p)\n", iface, pIThumbnail);
1345 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1348 static HRESULT WINAPI JpegEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
1350 TRACE("(%p,%p)\n", iface, pIPreview);
1351 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1354 static HRESULT WINAPI JpegEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
1355 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
1357 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
1360 TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
1362 EnterCriticalSection(&This->lock);
1364 if (This->frame_count != 0)
1366 LeaveCriticalSection(&This->lock);
1367 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1370 if (!This->initialized)
1372 LeaveCriticalSection(&This->lock);
1373 return WINCODEC_ERR_NOTINITIALIZED;
1376 hr = CreatePropertyBag2(ppIEncoderOptions);
1379 LeaveCriticalSection(&This->lock);
1383 This->frame_count = 1;
1385 LeaveCriticalSection(&This->lock);
1387 IWICBitmapEncoder_AddRef(iface);
1388 *ppIFrameEncode = &This->IWICBitmapFrameEncode_iface;
1393 static HRESULT WINAPI JpegEncoder_Commit(IWICBitmapEncoder *iface)
1395 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
1396 TRACE("(%p)\n", iface);
1398 EnterCriticalSection(&This->lock);
1400 if (!This->frame_committed || This->committed)
1402 LeaveCriticalSection(&This->lock);
1403 return WINCODEC_ERR_WRONGSTATE;
1406 This->committed = TRUE;
1408 LeaveCriticalSection(&This->lock);
1413 static HRESULT WINAPI JpegEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
1414 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1416 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
1420 static const IWICBitmapEncoderVtbl JpegEncoder_Vtbl = {
1421 JpegEncoder_QueryInterface,
1423 JpegEncoder_Release,
1424 JpegEncoder_Initialize,
1425 JpegEncoder_GetContainerFormat,
1426 JpegEncoder_GetEncoderInfo,
1427 JpegEncoder_SetColorContexts,
1428 JpegEncoder_SetPalette,
1429 JpegEncoder_SetThumbnail,
1430 JpegEncoder_SetPreview,
1431 JpegEncoder_CreateNewFrame,
1433 JpegEncoder_GetMetadataQueryWriter
1436 HRESULT JpegEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1441 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1445 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1447 if (!libjpeg_handle && !load_libjpeg())
1449 ERR("Failed writing JPEG because unable to find %s\n",SONAME_LIBJPEG);
1453 This = HeapAlloc(GetProcessHeap(), 0, sizeof(JpegEncoder));
1454 if (!This) return E_OUTOFMEMORY;
1456 This->IWICBitmapEncoder_iface.lpVtbl = &JpegEncoder_Vtbl;
1457 This->IWICBitmapFrameEncode_iface.lpVtbl = &JpegEncoder_FrameVtbl;
1459 This->initialized = 0;
1460 This->frame_count = 0;
1461 This->frame_initialized = 0;
1462 This->started_compress = 0;
1463 This->lines_written = 0;
1464 This->frame_committed = 0;
1465 This->committed = 0;
1466 This->width = This->height = 0;
1467 This->xres = This->yres = 0.0;
1468 This->format = NULL;
1469 This->stream = NULL;
1470 InitializeCriticalSection(&This->lock);
1471 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JpegEncoder.lock");
1473 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
1474 IUnknown_Release((IUnknown*)This);
1479 #else /* !defined(SONAME_LIBJPEG) */
1481 HRESULT JpegDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1483 ERR("Trying to load JPEG picture, but JPEG support is not compiled in.\n");
1487 HRESULT JpegEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1489 ERR("Trying to save JPEG picture, but JPEG support is not compiled in.\n");