windowscodecs: Implement JpegEncoder_Frame_SetSize.
[wine/multimedia.git] / dlls / windowscodecs / jpegformat.c
blob014904321035b47695cc3892ee122acbdda158f2
1 /*
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
19 #include "config.h"
20 #include "wine/port.h"
22 #ifdef HAVE_UNISTD_H
23 # include <unistd.h>
24 #endif
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <setjmp.h>
30 #ifdef SONAME_LIBJPEG
31 /* This is a hack, so jpeglib.h does not redefine INT32 and the like*/
32 #define XMD_H
33 #define UINT8 JPEG_UINT8
34 #define UINT16 JPEG_UINT16
35 #define boolean jpeg_boolean
36 #undef HAVE_STDLIB_H
37 # include <jpeglib.h>
38 #undef HAVE_STDLIB_H
39 #define HAVE_STDLIB_H 1
40 #undef UINT8
41 #undef UINT16
42 #undef boolean
43 #endif
45 #define COBJMACROS
47 #include "windef.h"
48 #include "winbase.h"
49 #include "objbase.h"
50 #include "wincodec.h"
52 #include "wincodecs_private.h"
54 #include "wine/debug.h"
55 #include "wine/library.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
59 #ifdef SONAME_LIBJPEG
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);
74 #undef MAKE_FUNCPTR
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; \
83 return 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);
95 #undef LOAD_FUNCPTR
97 return libjpeg_handle;
100 static void error_exit_fn(j_common_ptr cinfo)
102 char message[JMSG_LENGTH_MAX];
103 if (ERR_ON(jpeg))
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);
132 typedef struct {
133 IWICBitmapDecoder IWICBitmapDecoder_iface;
134 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
135 LONG ref;
136 BOOL initialized;
137 BOOL cinfo_initialized;
138 IStream *stream;
139 struct jpeg_decompress_struct cinfo;
140 struct jpeg_error_mgr jerr;
141 struct jpeg_source_mgr source_mgr;
142 BYTE source_buffer[1024];
143 BYTE *image_data;
144 CRITICAL_SECTION lock;
145 } JpegDecoder;
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,
163 void **ppv)
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))
172 *ppv = This;
174 else
176 *ppv = NULL;
177 return E_NOINTERFACE;
180 IUnknown_AddRef((IUnknown*)*ppv);
181 return S_OK;
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);
191 return 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);
201 if (ref == 0)
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);
211 return ref;
214 static HRESULT WINAPI JpegDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
215 DWORD *pdwCapability)
217 FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
218 return E_NOTIMPL;
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);
228 HRESULT hr;
229 ULONG bytesread;
231 hr = IStream_Read(This->stream, This->source_buffer, 1024, &bytesread);
233 if (hr != S_OK || bytesread == 0)
235 return FALSE;
237 else
239 This->source_mgr.next_input_byte = This->source_buffer;
240 This->source_mgr.bytes_in_buffer = bytesread;
241 return TRUE;
245 static void source_mgr_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
247 JpegDecoder *This = decoder_from_decompress(cinfo);
248 LARGE_INTEGER seek;
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);
271 int ret;
272 LARGE_INTEGER seek;
273 jmp_buf jmpbuf;
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;
293 if (setjmp(jmpbuf))
295 LeaveCriticalSection(&This->lock);
296 return E_FAIL;
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);
306 seek.QuadPart = 0;
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);
323 return E_FAIL;
326 switch (This->cinfo.jpeg_color_space)
328 case JCS_GRAYSCALE:
329 This->cinfo.out_color_space = JCS_GRAYSCALE;
330 break;
331 case JCS_RGB:
332 case JCS_YCbCr:
333 This->cinfo.out_color_space = JCS_RGB;
334 break;
335 case JCS_CMYK:
336 case JCS_YCCK:
337 This->cinfo.out_color_space = JCS_CMYK;
338 break;
339 default:
340 ERR("Unknown JPEG color space %i\n", This->cinfo.jpeg_color_space);
341 LeaveCriticalSection(&This->lock);
342 return E_FAIL;
345 if (!pjpeg_start_decompress(&This->cinfo))
347 ERR("jpeg_start_decompress failed\n");
348 LeaveCriticalSection(&This->lock);
349 return E_FAIL;
352 This->initialized = TRUE;
354 LeaveCriticalSection(&This->lock);
356 return S_OK;
359 static HRESULT WINAPI JpegDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
360 GUID *pguidContainerFormat)
362 memcpy(pguidContainerFormat, &GUID_ContainerFormatJpeg, sizeof(GUID));
363 return S_OK;
366 static HRESULT WINAPI JpegDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
367 IWICBitmapDecoderInfo **ppIDecoderInfo)
369 HRESULT hr;
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);
382 return hr;
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);
397 return E_NOTIMPL;
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,
422 UINT *pCount)
424 *pCount = 1;
425 return S_OK;
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;
441 return S_OK;
444 static const IWICBitmapDecoderVtbl JpegDecoder_Vtbl = {
445 JpegDecoder_QueryInterface,
446 JpegDecoder_AddRef,
447 JpegDecoder_Release,
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,
458 JpegDecoder_GetFrame
461 static HRESULT WINAPI JpegDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
462 void **ppv)
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))
472 *ppv = iface;
474 else
476 *ppv = NULL;
477 return E_NOINTERFACE;
480 IUnknown_AddRef((IUnknown*)*ppv);
481 return S_OK;
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);
503 return S_OK;
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));
517 return S_OK;
520 static HRESULT WINAPI JpegDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
521 double *pDpiX, double *pDpiY)
523 FIXME("(%p,%p,%p): stub\n", iface, pDpiX, pDpiY);
524 return E_NOTIMPL;
527 static HRESULT WINAPI JpegDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
528 IWICPalette *pIPalette)
530 FIXME("(%p,%p): stub\n", iface, pIPalette);
531 return E_NOTIMPL;
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);
538 UINT bpp;
539 UINT stride;
540 UINT data_size;
541 UINT max_row_needed;
542 jmp_buf jmpbuf;
543 WICRect rect;
544 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
546 if (!prc)
548 rect.X = 0;
549 rect.Y = 0;
550 rect.Width = This->cinfo.output_width;
551 rect.Height = This->cinfo.output_height;
552 prc = &rect;
554 else
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)
558 return E_INVALIDARG;
561 if (This->cinfo.out_color_space == JCS_GRAYSCALE) bpp = 8;
562 else if (This->cinfo.out_color_space == JCS_CMYK) bpp = 32;
563 else bpp = 24;
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;
585 if (setjmp(jmpbuf))
587 LeaveCriticalSection(&This->lock);
588 return E_FAIL;
591 while (max_row_needed > This->cinfo.output_scanline)
593 UINT first_scanline = This->cinfo.output_scanline;
594 UINT max_rows;
595 JSAMPROW out_rows[4];
596 UINT i;
597 JDIMENSION ret;
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);
605 if (ret == 0)
607 ERR("read_scanlines failed\n");
608 LeaveCriticalSection(&This->lock);
609 return E_FAIL;
612 if (bpp == 24)
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,
617 stride);
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)
670 JpegDecoder *This;
671 HRESULT ret;
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);
678 return E_FAIL;
681 *ppv = NULL;
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;
690 This->ref = 1;
691 This->initialized = FALSE;
692 This->cinfo_initialized = FALSE;
693 This->stream = NULL;
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);
701 return ret;
704 typedef struct JpegEncoder {
705 IWICBitmapEncoder IWICBitmapEncoder_iface;
706 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface;
707 LONG ref;
708 struct jpeg_compress_struct cinfo;
709 struct jpeg_error_mgr jerr;
710 struct jpeg_destination_mgr dest_mgr;
711 int initialized;
712 int frame_count;
713 int frame_initialized;
714 int started_compress;
715 UINT width, height;
716 IStream *stream;
717 CRITICAL_SECTION lock;
718 BYTE dest_buffer[1024];
719 } JpegEncoder;
721 static inline JpegEncoder *impl_from_IWICBitmapEncoder(IWICBitmapEncoder *iface)
723 return CONTAINING_RECORD(iface, JpegEncoder, IWICBitmapEncoder_iface);
726 static inline JpegEncoder *impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode *iface)
728 return CONTAINING_RECORD(iface, JpegEncoder, IWICBitmapFrameEncode_iface);
731 static inline JpegEncoder *encoder_from_compress(j_compress_ptr compress)
733 return CONTAINING_RECORD(compress, JpegEncoder, cinfo);
736 static void dest_mgr_init_destination(j_compress_ptr cinfo)
738 JpegEncoder *This = encoder_from_compress(cinfo);
740 This->dest_mgr.next_output_byte = This->dest_buffer;
741 This->dest_mgr.free_in_buffer = sizeof(This->dest_buffer);
744 static jpeg_boolean dest_mgr_empty_output_buffer(j_compress_ptr cinfo)
746 JpegEncoder *This = encoder_from_compress(cinfo);
747 HRESULT hr;
748 ULONG byteswritten;
750 hr = IStream_Write(This->stream, This->dest_buffer,
751 sizeof(This->dest_buffer), &byteswritten);
753 if (hr != S_OK || byteswritten == 0)
755 ERR("Failed writing data, hr=%x\n", hr);
756 return FALSE;
759 This->dest_mgr.next_output_byte = This->dest_buffer;
760 This->dest_mgr.free_in_buffer = sizeof(This->dest_buffer);
761 return TRUE;
764 static void dest_mgr_term_destination(j_compress_ptr cinfo)
766 JpegEncoder *This = encoder_from_compress(cinfo);
767 ULONG byteswritten;
768 HRESULT hr;
770 if (This->dest_mgr.free_in_buffer != sizeof(This->dest_buffer))
772 hr = IStream_Write(This->stream, This->dest_buffer,
773 sizeof(This->dest_buffer) - This->dest_mgr.free_in_buffer, &byteswritten);
775 if (hr != S_OK || byteswritten == 0)
776 ERR("Failed writing data, hr=%x\n", hr);
780 static HRESULT WINAPI JpegEncoder_Frame_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid,
781 void **ppv)
783 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
784 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
786 if (!ppv) return E_INVALIDARG;
788 if (IsEqualIID(&IID_IUnknown, iid) ||
789 IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
791 *ppv = &This->IWICBitmapFrameEncode_iface;
793 else
795 *ppv = NULL;
796 return E_NOINTERFACE;
799 IUnknown_AddRef((IUnknown*)*ppv);
800 return S_OK;
803 static ULONG WINAPI JpegEncoder_Frame_AddRef(IWICBitmapFrameEncode *iface)
805 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
806 return IWICBitmapEncoder_AddRef(&This->IWICBitmapEncoder_iface);
809 static ULONG WINAPI JpegEncoder_Frame_Release(IWICBitmapFrameEncode *iface)
811 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
812 return IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
815 static HRESULT WINAPI JpegEncoder_Frame_Initialize(IWICBitmapFrameEncode *iface,
816 IPropertyBag2 *pIEncoderOptions)
818 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
819 TRACE("(%p,%p)\n", iface, pIEncoderOptions);
821 EnterCriticalSection(&This->lock);
823 if (This->frame_initialized)
825 LeaveCriticalSection(&This->lock);
826 return WINCODEC_ERR_WRONGSTATE;
829 This->frame_initialized = TRUE;
831 LeaveCriticalSection(&This->lock);
833 return S_OK;
836 static HRESULT WINAPI JpegEncoder_Frame_SetSize(IWICBitmapFrameEncode *iface,
837 UINT uiWidth, UINT uiHeight)
839 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
840 TRACE("(%p,%u,%u)\n", iface, uiWidth, uiHeight);
842 EnterCriticalSection(&This->lock);
844 if (!This->frame_initialized || This->started_compress)
846 LeaveCriticalSection(&This->lock);
847 return WINCODEC_ERR_WRONGSTATE;
850 This->width = uiWidth;
851 This->height = uiHeight;
853 LeaveCriticalSection(&This->lock);
855 return S_OK;
858 static HRESULT WINAPI JpegEncoder_Frame_SetResolution(IWICBitmapFrameEncode *iface,
859 double dpiX, double dpiY)
861 FIXME("(%p,%0.2f,%0.2f): stub\n", iface, dpiX, dpiY);
862 return E_NOTIMPL;
865 static HRESULT WINAPI JpegEncoder_Frame_SetPixelFormat(IWICBitmapFrameEncode *iface,
866 WICPixelFormatGUID *pPixelFormat)
868 FIXME("(%p,%s): stub\n", iface, debugstr_guid(pPixelFormat));
869 return E_NOTIMPL;
872 static HRESULT WINAPI JpegEncoder_Frame_SetColorContexts(IWICBitmapFrameEncode *iface,
873 UINT cCount, IWICColorContext **ppIColorContext)
875 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
876 return E_NOTIMPL;
879 static HRESULT WINAPI JpegEncoder_Frame_SetPalette(IWICBitmapFrameEncode *iface,
880 IWICPalette *pIPalette)
882 FIXME("(%p,%p): stub\n", iface, pIPalette);
883 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
886 static HRESULT WINAPI JpegEncoder_Frame_SetThumbnail(IWICBitmapFrameEncode *iface,
887 IWICBitmapSource *pIThumbnail)
889 FIXME("(%p,%p): stub\n", iface, pIThumbnail);
890 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
893 static HRESULT WINAPI JpegEncoder_Frame_WritePixels(IWICBitmapFrameEncode *iface,
894 UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
896 FIXME("(%p,%u,%u,%u,%p): stub\n", iface, lineCount, cbStride, cbBufferSize, pbPixels);
897 return E_NOTIMPL;
900 static HRESULT WINAPI JpegEncoder_Frame_WriteSource(IWICBitmapFrameEncode *iface,
901 IWICBitmapSource *pIBitmapSource, WICRect *prc)
903 FIXME("(%p,%p,%p): stub\n", iface, pIBitmapSource, prc);
904 return E_NOTIMPL;
907 static HRESULT WINAPI JpegEncoder_Frame_Commit(IWICBitmapFrameEncode *iface)
909 FIXME("(%p): stub\n", iface);
910 return E_NOTIMPL;
913 static HRESULT WINAPI JpegEncoder_Frame_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
914 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
916 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
917 return E_NOTIMPL;
920 static const IWICBitmapFrameEncodeVtbl JpegEncoder_FrameVtbl = {
921 JpegEncoder_Frame_QueryInterface,
922 JpegEncoder_Frame_AddRef,
923 JpegEncoder_Frame_Release,
924 JpegEncoder_Frame_Initialize,
925 JpegEncoder_Frame_SetSize,
926 JpegEncoder_Frame_SetResolution,
927 JpegEncoder_Frame_SetPixelFormat,
928 JpegEncoder_Frame_SetColorContexts,
929 JpegEncoder_Frame_SetPalette,
930 JpegEncoder_Frame_SetThumbnail,
931 JpegEncoder_Frame_WritePixels,
932 JpegEncoder_Frame_WriteSource,
933 JpegEncoder_Frame_Commit,
934 JpegEncoder_Frame_GetMetadataQueryWriter
937 static HRESULT WINAPI JpegEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
938 void **ppv)
940 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
941 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
943 if (!ppv) return E_INVALIDARG;
945 if (IsEqualIID(&IID_IUnknown, iid) ||
946 IsEqualIID(&IID_IWICBitmapEncoder, iid))
948 *ppv = This;
950 else
952 *ppv = NULL;
953 return E_NOINTERFACE;
956 IUnknown_AddRef((IUnknown*)*ppv);
957 return S_OK;
960 static ULONG WINAPI JpegEncoder_AddRef(IWICBitmapEncoder *iface)
962 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
963 ULONG ref = InterlockedIncrement(&This->ref);
965 TRACE("(%p) refcount=%u\n", iface, ref);
967 return ref;
970 static ULONG WINAPI JpegEncoder_Release(IWICBitmapEncoder *iface)
972 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
973 ULONG ref = InterlockedDecrement(&This->ref);
975 TRACE("(%p) refcount=%u\n", iface, ref);
977 if (ref == 0)
979 This->lock.DebugInfo->Spare[0] = 0;
980 DeleteCriticalSection(&This->lock);
981 if (This->initialized) pjpeg_destroy_compress(&This->cinfo);
982 if (This->stream) IStream_Release(This->stream);
983 HeapFree(GetProcessHeap(), 0, This);
986 return ref;
989 static HRESULT WINAPI JpegEncoder_Initialize(IWICBitmapEncoder *iface,
990 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
992 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
993 jmp_buf jmpbuf;
995 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
997 EnterCriticalSection(&This->lock);
999 if (This->initialized)
1001 LeaveCriticalSection(&This->lock);
1002 return WINCODEC_ERR_WRONGSTATE;
1005 pjpeg_std_error(&This->jerr);
1007 This->jerr.error_exit = error_exit_fn;
1008 This->jerr.emit_message = emit_message_fn;
1010 This->cinfo.err = &This->jerr;
1012 This->cinfo.client_data = &jmpbuf;
1014 if (setjmp(jmpbuf))
1016 LeaveCriticalSection(&This->lock);
1017 return E_FAIL;
1020 pjpeg_CreateCompress(&This->cinfo, JPEG_LIB_VERSION, sizeof(struct jpeg_compress_struct));
1022 This->stream = pIStream;
1023 IStream_AddRef(pIStream);
1025 This->dest_mgr.next_output_byte = This->dest_buffer;
1026 This->dest_mgr.free_in_buffer = sizeof(This->dest_buffer);
1028 This->dest_mgr.init_destination = dest_mgr_init_destination;
1029 This->dest_mgr.empty_output_buffer = dest_mgr_empty_output_buffer;
1030 This->dest_mgr.term_destination = dest_mgr_term_destination;
1032 This->cinfo.dest = &This->dest_mgr;
1034 This->initialized = TRUE;
1036 LeaveCriticalSection(&This->lock);
1038 return S_OK;
1041 static HRESULT WINAPI JpegEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
1042 GUID *pguidContainerFormat)
1044 FIXME("(%p,%s): stub\n", iface, debugstr_guid(pguidContainerFormat));
1045 return E_NOTIMPL;
1048 static HRESULT WINAPI JpegEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
1049 IWICBitmapEncoderInfo **ppIEncoderInfo)
1051 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
1052 return E_NOTIMPL;
1055 static HRESULT WINAPI JpegEncoder_SetColorContexts(IWICBitmapEncoder *iface,
1056 UINT cCount, IWICColorContext **ppIColorContext)
1058 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1059 return E_NOTIMPL;
1062 static HRESULT WINAPI JpegEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
1064 TRACE("(%p,%p)\n", iface, pIPalette);
1065 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1068 static HRESULT WINAPI JpegEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
1070 TRACE("(%p,%p)\n", iface, pIThumbnail);
1071 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1074 static HRESULT WINAPI JpegEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
1076 TRACE("(%p,%p)\n", iface, pIPreview);
1077 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1080 static HRESULT WINAPI JpegEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
1081 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
1083 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
1084 HRESULT hr;
1086 TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
1088 EnterCriticalSection(&This->lock);
1090 if (This->frame_count != 0)
1092 LeaveCriticalSection(&This->lock);
1093 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1096 if (!This->initialized)
1098 LeaveCriticalSection(&This->lock);
1099 return WINCODEC_ERR_NOTINITIALIZED;
1102 hr = CreatePropertyBag2(ppIEncoderOptions);
1103 if (FAILED(hr))
1105 LeaveCriticalSection(&This->lock);
1106 return hr;
1109 This->frame_count = 1;
1111 LeaveCriticalSection(&This->lock);
1113 IWICBitmapEncoder_AddRef(iface);
1114 *ppIFrameEncode = &This->IWICBitmapFrameEncode_iface;
1116 return S_OK;
1119 static HRESULT WINAPI JpegEncoder_Commit(IWICBitmapEncoder *iface)
1121 FIXME("(%p): stub\n", iface);
1122 return E_NOTIMPL;
1125 static HRESULT WINAPI JpegEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
1126 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1128 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
1129 return E_NOTIMPL;
1132 static const IWICBitmapEncoderVtbl JpegEncoder_Vtbl = {
1133 JpegEncoder_QueryInterface,
1134 JpegEncoder_AddRef,
1135 JpegEncoder_Release,
1136 JpegEncoder_Initialize,
1137 JpegEncoder_GetContainerFormat,
1138 JpegEncoder_GetEncoderInfo,
1139 JpegEncoder_SetColorContexts,
1140 JpegEncoder_SetPalette,
1141 JpegEncoder_SetThumbnail,
1142 JpegEncoder_SetPreview,
1143 JpegEncoder_CreateNewFrame,
1144 JpegEncoder_Commit,
1145 JpegEncoder_GetMetadataQueryWriter
1148 HRESULT JpegEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1150 JpegEncoder *This;
1151 HRESULT ret;
1153 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1155 *ppv = NULL;
1157 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1159 if (!libjpeg_handle && !load_libjpeg())
1161 ERR("Failed writing JPEG because unable to find %s\n",SONAME_LIBJPEG);
1162 return E_FAIL;
1165 This = HeapAlloc(GetProcessHeap(), 0, sizeof(JpegEncoder));
1166 if (!This) return E_OUTOFMEMORY;
1168 This->IWICBitmapEncoder_iface.lpVtbl = &JpegEncoder_Vtbl;
1169 This->IWICBitmapFrameEncode_iface.lpVtbl = &JpegEncoder_FrameVtbl;
1170 This->ref = 1;
1171 This->initialized = 0;
1172 This->frame_count = 0;
1173 This->frame_initialized = 0;
1174 This->started_compress = 0;
1175 This->width = This->height = 0;
1176 This->stream = NULL;
1177 InitializeCriticalSection(&This->lock);
1178 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JpegEncoder.lock");
1180 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
1181 IUnknown_Release((IUnknown*)This);
1183 return ret;
1186 #else /* !defined(SONAME_LIBJPEG) */
1188 HRESULT JpegDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1190 ERR("Trying to load JPEG picture, but JPEG support is not compiled in.\n");
1191 return E_FAIL;
1194 HRESULT JpegEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1196 ERR("Trying to save JPEG picture, but JPEG support is not compiled in.\n");
1197 return E_FAIL;
1200 #endif