[WINDOWSCODECS]
[reactos.git] / reactos / dll / win32 / windowscodecs / jpegformat.c
1 /*
2 * Copyright 2009 Vincent Povirk for CodeWeavers
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include "wincodecs_private.h"
20
21 #ifdef HAVE_UNISTD_H
22 # include <unistd.h>
23 #endif
24
25 #include <stdio.h>
26 #include <setjmp.h>
27
28 #ifdef SONAME_LIBJPEG
29 /* This is a hack, so jpeglib.h does not redefine INT32 and the like*/
30 #define XMD_H
31 #define UINT8 JPEG_UINT8
32 #define UINT16 JPEG_UINT16
33 #define boolean jpeg_boolean
34 #undef HAVE_STDLIB_H
35 # include <jpeglib.h>
36 #undef HAVE_STDLIB_H
37 #define HAVE_STDLIB_H 1
38 #undef UINT8
39 #undef UINT16
40 #undef boolean
41 #endif
42
43 #include <wine/library.h>
44
45 #ifdef SONAME_LIBJPEG
46 WINE_DECLARE_DEBUG_CHANNEL(jpeg);
47
48 static void *libjpeg_handle;
49
50 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
51 MAKE_FUNCPTR(jpeg_CreateCompress);
52 MAKE_FUNCPTR(jpeg_CreateDecompress);
53 MAKE_FUNCPTR(jpeg_destroy_compress);
54 MAKE_FUNCPTR(jpeg_destroy_decompress);
55 MAKE_FUNCPTR(jpeg_finish_compress);
56 MAKE_FUNCPTR(jpeg_read_header);
57 MAKE_FUNCPTR(jpeg_read_scanlines);
58 MAKE_FUNCPTR(jpeg_resync_to_restart);
59 MAKE_FUNCPTR(jpeg_set_defaults);
60 MAKE_FUNCPTR(jpeg_start_compress);
61 MAKE_FUNCPTR(jpeg_start_decompress);
62 MAKE_FUNCPTR(jpeg_std_error);
63 MAKE_FUNCPTR(jpeg_write_scanlines);
64 #undef MAKE_FUNCPTR
65
66 static void *load_libjpeg(void)
67 {
68 if((libjpeg_handle = wine_dlopen(SONAME_LIBJPEG, RTLD_NOW, NULL, 0)) != NULL) {
69
70 #define LOAD_FUNCPTR(f) \
71 if((p##f = wine_dlsym(libjpeg_handle, #f, NULL, 0)) == NULL) { \
72 libjpeg_handle = NULL; \
73 return NULL; \
74 }
75
76 LOAD_FUNCPTR(jpeg_CreateCompress);
77 LOAD_FUNCPTR(jpeg_CreateDecompress);
78 LOAD_FUNCPTR(jpeg_destroy_compress);
79 LOAD_FUNCPTR(jpeg_destroy_decompress);
80 LOAD_FUNCPTR(jpeg_finish_compress);
81 LOAD_FUNCPTR(jpeg_read_header);
82 LOAD_FUNCPTR(jpeg_read_scanlines);
83 LOAD_FUNCPTR(jpeg_resync_to_restart);
84 LOAD_FUNCPTR(jpeg_set_defaults);
85 LOAD_FUNCPTR(jpeg_start_compress);
86 LOAD_FUNCPTR(jpeg_start_decompress);
87 LOAD_FUNCPTR(jpeg_std_error);
88 LOAD_FUNCPTR(jpeg_write_scanlines);
89 #undef LOAD_FUNCPTR
90 }
91 return libjpeg_handle;
92 }
93
94 static void error_exit_fn(j_common_ptr cinfo)
95 {
96 char message[JMSG_LENGTH_MAX];
97 if (ERR_ON(jpeg))
98 {
99 cinfo->err->format_message(cinfo, message);
100 ERR_(jpeg)("%s\n", message);
101 }
102 longjmp(*(jmp_buf*)cinfo->client_data, 1);
103 }
104
105 static void emit_message_fn(j_common_ptr cinfo, int msg_level)
106 {
107 char message[JMSG_LENGTH_MAX];
108
109 if (msg_level < 0 && ERR_ON(jpeg))
110 {
111 cinfo->err->format_message(cinfo, message);
112 ERR_(jpeg)("%s\n", message);
113 }
114 else if (msg_level == 0 && WARN_ON(jpeg))
115 {
116 cinfo->err->format_message(cinfo, message);
117 WARN_(jpeg)("%s\n", message);
118 }
119 else if (msg_level > 0 && TRACE_ON(jpeg))
120 {
121 cinfo->err->format_message(cinfo, message);
122 TRACE_(jpeg)("%s\n", message);
123 }
124 }
125
126 typedef struct {
127 IWICBitmapDecoder IWICBitmapDecoder_iface;
128 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
129 LONG ref;
130 BOOL initialized;
131 BOOL cinfo_initialized;
132 IStream *stream;
133 struct jpeg_decompress_struct cinfo;
134 struct jpeg_error_mgr jerr;
135 struct jpeg_source_mgr source_mgr;
136 BYTE source_buffer[1024];
137 BYTE *image_data;
138 CRITICAL_SECTION lock;
139 } JpegDecoder;
140
141 static inline JpegDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
142 {
143 return CONTAINING_RECORD(iface, JpegDecoder, IWICBitmapDecoder_iface);
144 }
145
146 static inline JpegDecoder *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
147 {
148 return CONTAINING_RECORD(iface, JpegDecoder, IWICBitmapFrameDecode_iface);
149 }
150
151 static inline JpegDecoder *decoder_from_decompress(j_decompress_ptr decompress)
152 {
153 return CONTAINING_RECORD(decompress, JpegDecoder, cinfo);
154 }
155
156 static HRESULT WINAPI JpegDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
157 void **ppv)
158 {
159 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
160 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
161
162 if (!ppv) return E_INVALIDARG;
163
164 if (IsEqualIID(&IID_IUnknown, iid) ||
165 IsEqualIID(&IID_IWICBitmapDecoder, iid))
166 {
167 *ppv = &This->IWICBitmapDecoder_iface;
168 }
169 else
170 {
171 *ppv = NULL;
172 return E_NOINTERFACE;
173 }
174
175 IUnknown_AddRef((IUnknown*)*ppv);
176 return S_OK;
177 }
178
179 static ULONG WINAPI JpegDecoder_AddRef(IWICBitmapDecoder *iface)
180 {
181 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
182 ULONG ref = InterlockedIncrement(&This->ref);
183
184 TRACE("(%p) refcount=%u\n", iface, ref);
185
186 return ref;
187 }
188
189 static ULONG WINAPI JpegDecoder_Release(IWICBitmapDecoder *iface)
190 {
191 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
192 ULONG ref = InterlockedDecrement(&This->ref);
193
194 TRACE("(%p) refcount=%u\n", iface, ref);
195
196 if (ref == 0)
197 {
198 This->lock.DebugInfo->Spare[0] = 0;
199 DeleteCriticalSection(&This->lock);
200 if (This->cinfo_initialized) pjpeg_destroy_decompress(&This->cinfo);
201 if (This->stream) IStream_Release(This->stream);
202 HeapFree(GetProcessHeap(), 0, This->image_data);
203 HeapFree(GetProcessHeap(), 0, This);
204 }
205
206 return ref;
207 }
208
209 static HRESULT WINAPI JpegDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *stream,
210 DWORD *capability)
211 {
212 HRESULT hr;
213
214 TRACE("(%p,%p,%p)\n", iface, stream, capability);
215
216 if (!stream || !capability) return E_INVALIDARG;
217
218 hr = IWICBitmapDecoder_Initialize(iface, stream, WICDecodeMetadataCacheOnDemand);
219 if (hr != S_OK) return hr;
220
221 *capability = WICBitmapDecoderCapabilityCanDecodeAllImages |
222 WICBitmapDecoderCapabilityCanDecodeSomeImages;
223 /* FIXME: WICBitmapDecoderCapabilityCanEnumerateMetadata */
224 return S_OK;
225 }
226
227 static void source_mgr_init_source(j_decompress_ptr cinfo)
228 {
229 }
230
231 static jpeg_boolean source_mgr_fill_input_buffer(j_decompress_ptr cinfo)
232 {
233 JpegDecoder *This = decoder_from_decompress(cinfo);
234 HRESULT hr;
235 ULONG bytesread;
236
237 hr = IStream_Read(This->stream, This->source_buffer, 1024, &bytesread);
238
239 if (hr != S_OK || bytesread == 0)
240 {
241 return FALSE;
242 }
243 else
244 {
245 This->source_mgr.next_input_byte = This->source_buffer;
246 This->source_mgr.bytes_in_buffer = bytesread;
247 return TRUE;
248 }
249 }
250
251 static void source_mgr_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
252 {
253 JpegDecoder *This = decoder_from_decompress(cinfo);
254 LARGE_INTEGER seek;
255
256 if (num_bytes > This->source_mgr.bytes_in_buffer)
257 {
258 seek.QuadPart = num_bytes - This->source_mgr.bytes_in_buffer;
259 IStream_Seek(This->stream, seek, STREAM_SEEK_CUR, NULL);
260 This->source_mgr.bytes_in_buffer = 0;
261 }
262 else if (num_bytes > 0)
263 {
264 This->source_mgr.next_input_byte += num_bytes;
265 This->source_mgr.bytes_in_buffer -= num_bytes;
266 }
267 }
268
269 static void source_mgr_term_source(j_decompress_ptr cinfo)
270 {
271 }
272
273 static HRESULT WINAPI JpegDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
274 WICDecodeOptions cacheOptions)
275 {
276 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
277 int ret;
278 LARGE_INTEGER seek;
279 jmp_buf jmpbuf;
280 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOptions);
281
282 EnterCriticalSection(&This->lock);
283
284 if (This->cinfo_initialized)
285 {
286 LeaveCriticalSection(&This->lock);
287 return WINCODEC_ERR_WRONGSTATE;
288 }
289
290 pjpeg_std_error(&This->jerr);
291
292 This->jerr.error_exit = error_exit_fn;
293 This->jerr.emit_message = emit_message_fn;
294
295 This->cinfo.err = &This->jerr;
296
297 This->cinfo.client_data = jmpbuf;
298
299 if (setjmp(jmpbuf))
300 {
301 LeaveCriticalSection(&This->lock);
302 return E_FAIL;
303 }
304
305 pjpeg_CreateDecompress(&This->cinfo, JPEG_LIB_VERSION, sizeof(struct jpeg_decompress_struct));
306
307 This->cinfo_initialized = TRUE;
308
309 This->stream = pIStream;
310 IStream_AddRef(pIStream);
311
312 seek.QuadPart = 0;
313 IStream_Seek(This->stream, seek, STREAM_SEEK_SET, NULL);
314
315 This->source_mgr.bytes_in_buffer = 0;
316 This->source_mgr.init_source = source_mgr_init_source;
317 This->source_mgr.fill_input_buffer = source_mgr_fill_input_buffer;
318 This->source_mgr.skip_input_data = source_mgr_skip_input_data;
319 This->source_mgr.resync_to_restart = pjpeg_resync_to_restart;
320 This->source_mgr.term_source = source_mgr_term_source;
321
322 This->cinfo.src = &This->source_mgr;
323
324 ret = pjpeg_read_header(&This->cinfo, TRUE);
325
326 if (ret != JPEG_HEADER_OK) {
327 WARN("Jpeg image in stream has bad format, read header returned %d.\n",ret);
328 LeaveCriticalSection(&This->lock);
329 return E_FAIL;
330 }
331
332 switch (This->cinfo.jpeg_color_space)
333 {
334 case JCS_GRAYSCALE:
335 This->cinfo.out_color_space = JCS_GRAYSCALE;
336 break;
337 case JCS_RGB:
338 case JCS_YCbCr:
339 This->cinfo.out_color_space = JCS_RGB;
340 break;
341 case JCS_CMYK:
342 case JCS_YCCK:
343 This->cinfo.out_color_space = JCS_CMYK;
344 break;
345 default:
346 ERR("Unknown JPEG color space %i\n", This->cinfo.jpeg_color_space);
347 LeaveCriticalSection(&This->lock);
348 return E_FAIL;
349 }
350
351 if (!pjpeg_start_decompress(&This->cinfo))
352 {
353 ERR("jpeg_start_decompress failed\n");
354 LeaveCriticalSection(&This->lock);
355 return E_FAIL;
356 }
357
358 This->initialized = TRUE;
359
360 LeaveCriticalSection(&This->lock);
361
362 return S_OK;
363 }
364
365 static HRESULT WINAPI JpegDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
366 GUID *pguidContainerFormat)
367 {
368 memcpy(pguidContainerFormat, &GUID_ContainerFormatJpeg, sizeof(GUID));
369 return S_OK;
370 }
371
372 static HRESULT WINAPI JpegDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
373 IWICBitmapDecoderInfo **ppIDecoderInfo)
374 {
375 HRESULT hr;
376 IWICComponentInfo *compinfo;
377
378 TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
379
380 hr = CreateComponentInfo(&CLSID_WICJpegDecoder, &compinfo);
381 if (FAILED(hr)) return hr;
382
383 hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
384 (void**)ppIDecoderInfo);
385
386 IWICComponentInfo_Release(compinfo);
387
388 return hr;
389 }
390
391 static HRESULT WINAPI JpegDecoder_CopyPalette(IWICBitmapDecoder *iface,
392 IWICPalette *pIPalette)
393 {
394 TRACE("(%p,%p)\n", iface, pIPalette);
395
396 return WINCODEC_ERR_PALETTEUNAVAILABLE;
397 }
398
399 static HRESULT WINAPI JpegDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
400 IWICMetadataQueryReader **ppIMetadataQueryReader)
401 {
402 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
403 return E_NOTIMPL;
404 }
405
406 static HRESULT WINAPI JpegDecoder_GetPreview(IWICBitmapDecoder *iface,
407 IWICBitmapSource **ppIBitmapSource)
408 {
409 FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
410 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
411 }
412
413 static HRESULT WINAPI JpegDecoder_GetColorContexts(IWICBitmapDecoder *iface,
414 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
415 {
416 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
417 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
418 }
419
420 static HRESULT WINAPI JpegDecoder_GetThumbnail(IWICBitmapDecoder *iface,
421 IWICBitmapSource **ppIThumbnail)
422 {
423 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
424 return WINCODEC_ERR_CODECNOTHUMBNAIL;
425 }
426
427 static HRESULT WINAPI JpegDecoder_GetFrameCount(IWICBitmapDecoder *iface,
428 UINT *pCount)
429 {
430 if (!pCount) return E_INVALIDARG;
431
432 *pCount = 1;
433 return S_OK;
434 }
435
436 static HRESULT WINAPI JpegDecoder_GetFrame(IWICBitmapDecoder *iface,
437 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
438 {
439 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
440 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
441
442 if (!This->initialized) return WINCODEC_ERR_FRAMEMISSING;
443
444 if (index != 0) return E_INVALIDARG;
445
446 IWICBitmapDecoder_AddRef(iface);
447 *ppIBitmapFrame = &This->IWICBitmapFrameDecode_iface;
448
449 return S_OK;
450 }
451
452 static const IWICBitmapDecoderVtbl JpegDecoder_Vtbl = {
453 JpegDecoder_QueryInterface,
454 JpegDecoder_AddRef,
455 JpegDecoder_Release,
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,
466 JpegDecoder_GetFrame
467 };
468
469 static HRESULT WINAPI JpegDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
470 void **ppv)
471 {
472 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
473
474 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
475
476 if (!ppv) return E_INVALIDARG;
477
478 if (IsEqualIID(&IID_IUnknown, iid) ||
479 IsEqualIID(&IID_IWICBitmapSource, iid) ||
480 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
481 {
482 *ppv = &This->IWICBitmapFrameDecode_iface;
483 }
484 else
485 {
486 *ppv = NULL;
487 return E_NOINTERFACE;
488 }
489
490 IUnknown_AddRef((IUnknown*)*ppv);
491 return S_OK;
492 }
493
494 static ULONG WINAPI JpegDecoder_Frame_AddRef(IWICBitmapFrameDecode *iface)
495 {
496 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
497 return IWICBitmapDecoder_AddRef(&This->IWICBitmapDecoder_iface);
498 }
499
500 static ULONG WINAPI JpegDecoder_Frame_Release(IWICBitmapFrameDecode *iface)
501 {
502 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
503 return IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
504 }
505
506 static HRESULT WINAPI JpegDecoder_Frame_GetSize(IWICBitmapFrameDecode *iface,
507 UINT *puiWidth, UINT *puiHeight)
508 {
509 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
510 *puiWidth = This->cinfo.output_width;
511 *puiHeight = This->cinfo.output_height;
512 TRACE("(%p)->(%u,%u)\n", iface, *puiWidth, *puiHeight);
513 return S_OK;
514 }
515
516 static HRESULT WINAPI JpegDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
517 WICPixelFormatGUID *pPixelFormat)
518 {
519 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
520 TRACE("(%p,%p)\n", iface, pPixelFormat);
521 if (This->cinfo.out_color_space == JCS_RGB)
522 memcpy(pPixelFormat, &GUID_WICPixelFormat24bppBGR, sizeof(GUID));
523 else if (This->cinfo.out_color_space == JCS_CMYK)
524 memcpy(pPixelFormat, &GUID_WICPixelFormat32bppCMYK, sizeof(GUID));
525 else /* This->cinfo.out_color_space == JCS_GRAYSCALE */
526 memcpy(pPixelFormat, &GUID_WICPixelFormat8bppGray, sizeof(GUID));
527 return S_OK;
528 }
529
530 static HRESULT WINAPI JpegDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
531 double *pDpiX, double *pDpiY)
532 {
533 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
534
535 EnterCriticalSection(&This->lock);
536
537 switch (This->cinfo.density_unit)
538 {
539 case 2: /* pixels per centimeter */
540 *pDpiX = This->cinfo.X_density * 2.54;
541 *pDpiY = This->cinfo.Y_density * 2.54;
542 break;
543
544 case 1: /* pixels per inch */
545 *pDpiX = This->cinfo.X_density;
546 *pDpiY = This->cinfo.Y_density;
547 break;
548
549 case 0: /* unknown */
550 default:
551 *pDpiX = 96.0;
552 *pDpiY = 96.0;
553 break;
554 }
555
556 LeaveCriticalSection(&This->lock);
557
558 TRACE("(%p)->(%0.2f,%0.2f)\n", iface, *pDpiX, *pDpiY);
559
560 return S_OK;
561 }
562
563 static HRESULT WINAPI JpegDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
564 IWICPalette *pIPalette)
565 {
566 FIXME("(%p,%p): stub\n", iface, pIPalette);
567 return E_NOTIMPL;
568 }
569
570 static HRESULT WINAPI JpegDecoder_Frame_CopyPixels(IWICBitmapFrameDecode *iface,
571 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
572 {
573 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
574 UINT bpp;
575 UINT stride;
576 UINT data_size;
577 UINT max_row_needed;
578 jmp_buf jmpbuf;
579 WICRect rect;
580 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
581
582 if (!prc)
583 {
584 rect.X = 0;
585 rect.Y = 0;
586 rect.Width = This->cinfo.output_width;
587 rect.Height = This->cinfo.output_height;
588 prc = &rect;
589 }
590 else
591 {
592 if (prc->X < 0 || prc->Y < 0 || prc->X+prc->Width > This->cinfo.output_width ||
593 prc->Y+prc->Height > This->cinfo.output_height)
594 return E_INVALIDARG;
595 }
596
597 if (This->cinfo.out_color_space == JCS_GRAYSCALE) bpp = 8;
598 else if (This->cinfo.out_color_space == JCS_CMYK) bpp = 32;
599 else bpp = 24;
600
601 stride = bpp * This->cinfo.output_width;
602 data_size = stride * This->cinfo.output_height;
603
604 max_row_needed = prc->Y + prc->Height;
605 if (max_row_needed > This->cinfo.output_height) return E_INVALIDARG;
606
607 EnterCriticalSection(&This->lock);
608
609 if (!This->image_data)
610 {
611 This->image_data = HeapAlloc(GetProcessHeap(), 0, data_size);
612 if (!This->image_data)
613 {
614 LeaveCriticalSection(&This->lock);
615 return E_OUTOFMEMORY;
616 }
617 }
618
619 This->cinfo.client_data = jmpbuf;
620
621 if (setjmp(jmpbuf))
622 {
623 LeaveCriticalSection(&This->lock);
624 return E_FAIL;
625 }
626
627 while (max_row_needed > This->cinfo.output_scanline)
628 {
629 UINT first_scanline = This->cinfo.output_scanline;
630 UINT max_rows;
631 JSAMPROW out_rows[4];
632 UINT i;
633 JDIMENSION ret;
634
635 max_rows = min(This->cinfo.output_height-first_scanline, 4);
636 for (i=0; i<max_rows; i++)
637 out_rows[i] = This->image_data + stride * (first_scanline+i);
638
639 ret = pjpeg_read_scanlines(&This->cinfo, out_rows, max_rows);
640
641 if (ret == 0)
642 {
643 ERR("read_scanlines failed\n");
644 LeaveCriticalSection(&This->lock);
645 return E_FAIL;
646 }
647
648 if (bpp == 24)
649 {
650 /* libjpeg gives us RGB data and we want BGR, so byteswap the data */
651 reverse_bgr8(3, This->image_data + stride * first_scanline,
652 This->cinfo.output_width, This->cinfo.output_scanline - first_scanline,
653 stride);
654 }
655
656 if (This->cinfo.out_color_space == JCS_CMYK && This->cinfo.saw_Adobe_marker)
657 /* Adobe JPEG's have inverted CMYK data. */
658 for (i=0; i<data_size; i++)
659 This->image_data[i] ^= 0xff;
660 }
661
662 LeaveCriticalSection(&This->lock);
663
664 return copy_pixels(bpp, This->image_data,
665 This->cinfo.output_width, This->cinfo.output_height, stride,
666 prc, cbStride, cbBufferSize, pbBuffer);
667 }
668
669 static HRESULT WINAPI JpegDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
670 IWICMetadataQueryReader **ppIMetadataQueryReader)
671 {
672 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
673 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
674 }
675
676 static HRESULT WINAPI JpegDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode *iface,
677 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
678 {
679 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
680 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
681 }
682
683 static HRESULT WINAPI JpegDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode *iface,
684 IWICBitmapSource **ppIThumbnail)
685 {
686 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
687 return WINCODEC_ERR_CODECNOTHUMBNAIL;
688 }
689
690 static const IWICBitmapFrameDecodeVtbl JpegDecoder_Frame_Vtbl = {
691 JpegDecoder_Frame_QueryInterface,
692 JpegDecoder_Frame_AddRef,
693 JpegDecoder_Frame_Release,
694 JpegDecoder_Frame_GetSize,
695 JpegDecoder_Frame_GetPixelFormat,
696 JpegDecoder_Frame_GetResolution,
697 JpegDecoder_Frame_CopyPalette,
698 JpegDecoder_Frame_CopyPixels,
699 JpegDecoder_Frame_GetMetadataQueryReader,
700 JpegDecoder_Frame_GetColorContexts,
701 JpegDecoder_Frame_GetThumbnail
702 };
703
704 HRESULT JpegDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
705 {
706 JpegDecoder *This;
707 HRESULT ret;
708
709 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
710
711 if (!libjpeg_handle && !load_libjpeg())
712 {
713 ERR("Failed reading JPEG because unable to find %s\n", SONAME_LIBJPEG);
714 return E_FAIL;
715 }
716
717 *ppv = NULL;
718
719 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
720
721 This = HeapAlloc(GetProcessHeap(), 0, sizeof(JpegDecoder));
722 if (!This) return E_OUTOFMEMORY;
723
724 This->IWICBitmapDecoder_iface.lpVtbl = &JpegDecoder_Vtbl;
725 This->IWICBitmapFrameDecode_iface.lpVtbl = &JpegDecoder_Frame_Vtbl;
726 This->ref = 1;
727 This->initialized = FALSE;
728 This->cinfo_initialized = FALSE;
729 This->stream = NULL;
730 This->image_data = NULL;
731 InitializeCriticalSection(&This->lock);
732 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JpegDecoder.lock");
733
734 ret = IWICBitmapDecoder_QueryInterface(&This->IWICBitmapDecoder_iface, iid, ppv);
735 IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
736
737 return ret;
738 }
739
740 typedef struct jpeg_compress_format {
741 const WICPixelFormatGUID *guid;
742 int bpp;
743 int num_components;
744 J_COLOR_SPACE color_space;
745 int swap_rgb;
746 } jpeg_compress_format;
747
748 static const jpeg_compress_format compress_formats[] = {
749 { &GUID_WICPixelFormat24bppBGR, 24, 3, JCS_RGB, 1 },
750 { &GUID_WICPixelFormat32bppCMYK, 32, 4, JCS_CMYK },
751 { &GUID_WICPixelFormat8bppGray, 8, 1, JCS_GRAYSCALE },
752 { 0 }
753 };
754
755 typedef struct JpegEncoder {
756 IWICBitmapEncoder IWICBitmapEncoder_iface;
757 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface;
758 LONG ref;
759 struct jpeg_compress_struct cinfo;
760 struct jpeg_error_mgr jerr;
761 struct jpeg_destination_mgr dest_mgr;
762 int initialized;
763 int frame_count;
764 int frame_initialized;
765 int started_compress;
766 int lines_written;
767 int frame_committed;
768 int committed;
769 UINT width, height;
770 double xres, yres;
771 const jpeg_compress_format *format;
772 IStream *stream;
773 CRITICAL_SECTION lock;
774 BYTE dest_buffer[1024];
775 } JpegEncoder;
776
777 static inline JpegEncoder *impl_from_IWICBitmapEncoder(IWICBitmapEncoder *iface)
778 {
779 return CONTAINING_RECORD(iface, JpegEncoder, IWICBitmapEncoder_iface);
780 }
781
782 static inline JpegEncoder *impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode *iface)
783 {
784 return CONTAINING_RECORD(iface, JpegEncoder, IWICBitmapFrameEncode_iface);
785 }
786
787 static inline JpegEncoder *encoder_from_compress(j_compress_ptr compress)
788 {
789 return CONTAINING_RECORD(compress, JpegEncoder, cinfo);
790 }
791
792 static void dest_mgr_init_destination(j_compress_ptr cinfo)
793 {
794 JpegEncoder *This = encoder_from_compress(cinfo);
795
796 This->dest_mgr.next_output_byte = This->dest_buffer;
797 This->dest_mgr.free_in_buffer = sizeof(This->dest_buffer);
798 }
799
800 static jpeg_boolean dest_mgr_empty_output_buffer(j_compress_ptr cinfo)
801 {
802 JpegEncoder *This = encoder_from_compress(cinfo);
803 HRESULT hr;
804 ULONG byteswritten;
805
806 hr = IStream_Write(This->stream, This->dest_buffer,
807 sizeof(This->dest_buffer), &byteswritten);
808
809 if (hr != S_OK || byteswritten == 0)
810 {
811 ERR("Failed writing data, hr=%x\n", hr);
812 return FALSE;
813 }
814
815 This->dest_mgr.next_output_byte = This->dest_buffer;
816 This->dest_mgr.free_in_buffer = sizeof(This->dest_buffer);
817 return TRUE;
818 }
819
820 static void dest_mgr_term_destination(j_compress_ptr cinfo)
821 {
822 JpegEncoder *This = encoder_from_compress(cinfo);
823 ULONG byteswritten;
824 HRESULT hr;
825
826 if (This->dest_mgr.free_in_buffer != sizeof(This->dest_buffer))
827 {
828 hr = IStream_Write(This->stream, This->dest_buffer,
829 sizeof(This->dest_buffer) - This->dest_mgr.free_in_buffer, &byteswritten);
830
831 if (hr != S_OK || byteswritten == 0)
832 ERR("Failed writing data, hr=%x\n", hr);
833 }
834 }
835
836 static HRESULT WINAPI JpegEncoder_Frame_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid,
837 void **ppv)
838 {
839 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
840 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
841
842 if (!ppv) return E_INVALIDARG;
843
844 if (IsEqualIID(&IID_IUnknown, iid) ||
845 IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
846 {
847 *ppv = &This->IWICBitmapFrameEncode_iface;
848 }
849 else
850 {
851 *ppv = NULL;
852 return E_NOINTERFACE;
853 }
854
855 IUnknown_AddRef((IUnknown*)*ppv);
856 return S_OK;
857 }
858
859 static ULONG WINAPI JpegEncoder_Frame_AddRef(IWICBitmapFrameEncode *iface)
860 {
861 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
862 return IWICBitmapEncoder_AddRef(&This->IWICBitmapEncoder_iface);
863 }
864
865 static ULONG WINAPI JpegEncoder_Frame_Release(IWICBitmapFrameEncode *iface)
866 {
867 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
868 return IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
869 }
870
871 static HRESULT WINAPI JpegEncoder_Frame_Initialize(IWICBitmapFrameEncode *iface,
872 IPropertyBag2 *pIEncoderOptions)
873 {
874 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
875 TRACE("(%p,%p)\n", iface, pIEncoderOptions);
876
877 EnterCriticalSection(&This->lock);
878
879 if (This->frame_initialized)
880 {
881 LeaveCriticalSection(&This->lock);
882 return WINCODEC_ERR_WRONGSTATE;
883 }
884
885 This->frame_initialized = TRUE;
886
887 LeaveCriticalSection(&This->lock);
888
889 return S_OK;
890 }
891
892 static HRESULT WINAPI JpegEncoder_Frame_SetSize(IWICBitmapFrameEncode *iface,
893 UINT uiWidth, UINT uiHeight)
894 {
895 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
896 TRACE("(%p,%u,%u)\n", iface, uiWidth, uiHeight);
897
898 EnterCriticalSection(&This->lock);
899
900 if (!This->frame_initialized || This->started_compress)
901 {
902 LeaveCriticalSection(&This->lock);
903 return WINCODEC_ERR_WRONGSTATE;
904 }
905
906 This->width = uiWidth;
907 This->height = uiHeight;
908
909 LeaveCriticalSection(&This->lock);
910
911 return S_OK;
912 }
913
914 static HRESULT WINAPI JpegEncoder_Frame_SetResolution(IWICBitmapFrameEncode *iface,
915 double dpiX, double dpiY)
916 {
917 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
918 TRACE("(%p,%0.2f,%0.2f)\n", iface, dpiX, dpiY);
919
920 EnterCriticalSection(&This->lock);
921
922 if (!This->frame_initialized || This->started_compress)
923 {
924 LeaveCriticalSection(&This->lock);
925 return WINCODEC_ERR_WRONGSTATE;
926 }
927
928 This->xres = dpiX;
929 This->yres = dpiY;
930
931 LeaveCriticalSection(&This->lock);
932
933 return S_OK;
934 }
935
936 static HRESULT WINAPI JpegEncoder_Frame_SetPixelFormat(IWICBitmapFrameEncode *iface,
937 WICPixelFormatGUID *pPixelFormat)
938 {
939 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
940 int i;
941 TRACE("(%p,%s)\n", iface, debugstr_guid(pPixelFormat));
942
943 EnterCriticalSection(&This->lock);
944
945 if (!This->frame_initialized || This->started_compress)
946 {
947 LeaveCriticalSection(&This->lock);
948 return WINCODEC_ERR_WRONGSTATE;
949 }
950
951 for (i=0; compress_formats[i].guid; i++)
952 {
953 if (memcmp(compress_formats[i].guid, pPixelFormat, sizeof(GUID)) == 0)
954 break;
955 }
956
957 if (!compress_formats[i].guid) i = 0;
958
959 This->format = &compress_formats[i];
960 memcpy(pPixelFormat, This->format->guid, sizeof(GUID));
961
962 LeaveCriticalSection(&This->lock);
963
964 return S_OK;
965 }
966
967 static HRESULT WINAPI JpegEncoder_Frame_SetColorContexts(IWICBitmapFrameEncode *iface,
968 UINT cCount, IWICColorContext **ppIColorContext)
969 {
970 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
971 return E_NOTIMPL;
972 }
973
974 static HRESULT WINAPI JpegEncoder_Frame_SetPalette(IWICBitmapFrameEncode *iface,
975 IWICPalette *pIPalette)
976 {
977 FIXME("(%p,%p): stub\n", iface, pIPalette);
978 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
979 }
980
981 static HRESULT WINAPI JpegEncoder_Frame_SetThumbnail(IWICBitmapFrameEncode *iface,
982 IWICBitmapSource *pIThumbnail)
983 {
984 FIXME("(%p,%p): stub\n", iface, pIThumbnail);
985 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
986 }
987
988 static HRESULT WINAPI JpegEncoder_Frame_WritePixels(IWICBitmapFrameEncode *iface,
989 UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
990 {
991 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
992 jmp_buf jmpbuf;
993 BYTE *swapped_data = NULL, *current_row;
994 UINT line;
995 int row_size;
996 TRACE("(%p,%u,%u,%u,%p)\n", iface, lineCount, cbStride, cbBufferSize, pbPixels);
997
998 EnterCriticalSection(&This->lock);
999
1000 if (!This->frame_initialized || !This->width || !This->height || !This->format)
1001 {
1002 LeaveCriticalSection(&This->lock);
1003 return WINCODEC_ERR_WRONGSTATE;
1004 }
1005
1006 if (lineCount == 0 || lineCount + This->lines_written > This->height)
1007 {
1008 LeaveCriticalSection(&This->lock);
1009 return E_INVALIDARG;
1010 }
1011
1012 /* set up setjmp/longjmp error handling */
1013 if (setjmp(jmpbuf))
1014 {
1015 LeaveCriticalSection(&This->lock);
1016 HeapFree(GetProcessHeap(), 0, swapped_data);
1017 return E_FAIL;
1018 }
1019 This->cinfo.client_data = jmpbuf;
1020
1021 if (!This->started_compress)
1022 {
1023 This->cinfo.image_width = This->width;
1024 This->cinfo.image_height = This->height;
1025 This->cinfo.input_components = This->format->num_components;
1026 This->cinfo.in_color_space = This->format->color_space;
1027
1028 pjpeg_set_defaults(&This->cinfo);
1029
1030 if (This->xres != 0.0 && This->yres != 0.0)
1031 {
1032 This->cinfo.density_unit = 1; /* dots per inch */
1033 This->cinfo.X_density = This->xres;
1034 This->cinfo.Y_density = This->yres;
1035 }
1036
1037 pjpeg_start_compress(&This->cinfo, TRUE);
1038
1039 This->started_compress = 1;
1040 }
1041
1042 row_size = This->format->bpp / 8 * This->width;
1043
1044 if (This->format->swap_rgb)
1045 {
1046 swapped_data = HeapAlloc(GetProcessHeap(), 0, row_size);
1047 if (!swapped_data)
1048 {
1049 LeaveCriticalSection(&This->lock);
1050 return E_OUTOFMEMORY;
1051 }
1052 }
1053
1054 for (line=0; line < lineCount; line++)
1055 {
1056 if (This->format->swap_rgb)
1057 {
1058 UINT x;
1059
1060 memcpy(swapped_data, pbPixels + (cbStride * line), row_size);
1061
1062 for (x=0; x < This->width; x++)
1063 {
1064 BYTE b;
1065
1066 b = swapped_data[x*3];
1067 swapped_data[x*3] = swapped_data[x*3+2];
1068 swapped_data[x*3+2] = b;
1069 }
1070
1071 current_row = swapped_data;
1072 }
1073 else
1074 current_row = pbPixels + (cbStride * line);
1075
1076 if (!pjpeg_write_scanlines(&This->cinfo, &current_row, 1))
1077 {
1078 ERR("failed writing scanlines\n");
1079 LeaveCriticalSection(&This->lock);
1080 HeapFree(GetProcessHeap(), 0, swapped_data);
1081 return E_FAIL;
1082 }
1083
1084 This->lines_written++;
1085 }
1086
1087 LeaveCriticalSection(&This->lock);
1088 HeapFree(GetProcessHeap(), 0, swapped_data);
1089
1090 return S_OK;
1091 }
1092
1093 static HRESULT WINAPI JpegEncoder_Frame_WriteSource(IWICBitmapFrameEncode *iface,
1094 IWICBitmapSource *pIBitmapSource, WICRect *prc)
1095 {
1096 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1097 HRESULT hr;
1098 WICRect rc;
1099 WICPixelFormatGUID guid;
1100 UINT stride;
1101 BYTE *pixeldata;
1102 TRACE("(%p,%p,%p)\n", iface, pIBitmapSource, prc);
1103
1104 if (!This->frame_initialized || !This->width || !This->height)
1105 return WINCODEC_ERR_WRONGSTATE;
1106
1107 if (!This->format)
1108 {
1109 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1110 if (FAILED(hr)) return hr;
1111 hr = IWICBitmapFrameEncode_SetPixelFormat(iface, &guid);
1112 if (FAILED(hr)) return hr;
1113 }
1114
1115 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1116 if (FAILED(hr)) return hr;
1117 if (memcmp(&guid, This->format->guid, sizeof(GUID)) != 0)
1118 {
1119 /* FIXME: should use WICConvertBitmapSource to convert */
1120 ERR("format %s unsupported\n", debugstr_guid(&guid));
1121 return E_FAIL;
1122 }
1123
1124 if (This->xres == 0.0 || This->yres == 0.0)
1125 {
1126 double xres, yres;
1127 hr = IWICBitmapSource_GetResolution(pIBitmapSource, &xres, &yres);
1128 if (FAILED(hr)) return hr;
1129 hr = IWICBitmapFrameEncode_SetResolution(iface, xres, yres);
1130 if (FAILED(hr)) return hr;
1131 }
1132
1133 if (!prc)
1134 {
1135 UINT width, height;
1136 hr = IWICBitmapSource_GetSize(pIBitmapSource, &width, &height);
1137 if (FAILED(hr)) return hr;
1138 rc.X = 0;
1139 rc.Y = 0;
1140 rc.Width = width;
1141 rc.Height = height;
1142 prc = &rc;
1143 }
1144
1145 if (prc->Width != This->width) return E_INVALIDARG;
1146
1147 stride = (This->format->bpp * This->width + 7)/8;
1148
1149 pixeldata = HeapAlloc(GetProcessHeap(), 0, stride * prc->Height);
1150 if (!pixeldata) return E_OUTOFMEMORY;
1151
1152 hr = IWICBitmapSource_CopyPixels(pIBitmapSource, prc, stride,
1153 stride*prc->Height, pixeldata);
1154
1155 if (SUCCEEDED(hr))
1156 {
1157 hr = IWICBitmapFrameEncode_WritePixels(iface, prc->Height, stride,
1158 stride*prc->Height, pixeldata);
1159 }
1160
1161 HeapFree(GetProcessHeap(), 0, pixeldata);
1162
1163 return hr;
1164 }
1165
1166 static HRESULT WINAPI JpegEncoder_Frame_Commit(IWICBitmapFrameEncode *iface)
1167 {
1168 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1169 jmp_buf jmpbuf;
1170 TRACE("(%p)\n", iface);
1171
1172 EnterCriticalSection(&This->lock);
1173
1174 if (!This->started_compress || This->lines_written != This->height || This->frame_committed)
1175 {
1176 LeaveCriticalSection(&This->lock);
1177 return WINCODEC_ERR_WRONGSTATE;
1178 }
1179
1180 /* set up setjmp/longjmp error handling */
1181 if (setjmp(jmpbuf))
1182 {
1183 LeaveCriticalSection(&This->lock);
1184 return E_FAIL;
1185 }
1186 This->cinfo.client_data = jmpbuf;
1187
1188 pjpeg_finish_compress(&This->cinfo);
1189
1190 This->frame_committed = TRUE;
1191
1192 LeaveCriticalSection(&This->lock);
1193
1194 return S_OK;
1195 }
1196
1197 static HRESULT WINAPI JpegEncoder_Frame_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
1198 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1199 {
1200 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
1201 return E_NOTIMPL;
1202 }
1203
1204 static const IWICBitmapFrameEncodeVtbl JpegEncoder_FrameVtbl = {
1205 JpegEncoder_Frame_QueryInterface,
1206 JpegEncoder_Frame_AddRef,
1207 JpegEncoder_Frame_Release,
1208 JpegEncoder_Frame_Initialize,
1209 JpegEncoder_Frame_SetSize,
1210 JpegEncoder_Frame_SetResolution,
1211 JpegEncoder_Frame_SetPixelFormat,
1212 JpegEncoder_Frame_SetColorContexts,
1213 JpegEncoder_Frame_SetPalette,
1214 JpegEncoder_Frame_SetThumbnail,
1215 JpegEncoder_Frame_WritePixels,
1216 JpegEncoder_Frame_WriteSource,
1217 JpegEncoder_Frame_Commit,
1218 JpegEncoder_Frame_GetMetadataQueryWriter
1219 };
1220
1221 static HRESULT WINAPI JpegEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
1222 void **ppv)
1223 {
1224 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
1225 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1226
1227 if (!ppv) return E_INVALIDARG;
1228
1229 if (IsEqualIID(&IID_IUnknown, iid) ||
1230 IsEqualIID(&IID_IWICBitmapEncoder, iid))
1231 {
1232 *ppv = &This->IWICBitmapEncoder_iface;
1233 }
1234 else
1235 {
1236 *ppv = NULL;
1237 return E_NOINTERFACE;
1238 }
1239
1240 IUnknown_AddRef((IUnknown*)*ppv);
1241 return S_OK;
1242 }
1243
1244 static ULONG WINAPI JpegEncoder_AddRef(IWICBitmapEncoder *iface)
1245 {
1246 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
1247 ULONG ref = InterlockedIncrement(&This->ref);
1248
1249 TRACE("(%p) refcount=%u\n", iface, ref);
1250
1251 return ref;
1252 }
1253
1254 static ULONG WINAPI JpegEncoder_Release(IWICBitmapEncoder *iface)
1255 {
1256 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
1257 ULONG ref = InterlockedDecrement(&This->ref);
1258
1259 TRACE("(%p) refcount=%u\n", iface, ref);
1260
1261 if (ref == 0)
1262 {
1263 This->lock.DebugInfo->Spare[0] = 0;
1264 DeleteCriticalSection(&This->lock);
1265 if (This->initialized) pjpeg_destroy_compress(&This->cinfo);
1266 if (This->stream) IStream_Release(This->stream);
1267 HeapFree(GetProcessHeap(), 0, This);
1268 }
1269
1270 return ref;
1271 }
1272
1273 static HRESULT WINAPI JpegEncoder_Initialize(IWICBitmapEncoder *iface,
1274 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
1275 {
1276 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
1277 jmp_buf jmpbuf;
1278
1279 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
1280
1281 EnterCriticalSection(&This->lock);
1282
1283 if (This->initialized)
1284 {
1285 LeaveCriticalSection(&This->lock);
1286 return WINCODEC_ERR_WRONGSTATE;
1287 }
1288
1289 pjpeg_std_error(&This->jerr);
1290
1291 This->jerr.error_exit = error_exit_fn;
1292 This->jerr.emit_message = emit_message_fn;
1293
1294 This->cinfo.err = &This->jerr;
1295
1296 This->cinfo.client_data = jmpbuf;
1297
1298 if (setjmp(jmpbuf))
1299 {
1300 LeaveCriticalSection(&This->lock);
1301 return E_FAIL;
1302 }
1303
1304 pjpeg_CreateCompress(&This->cinfo, JPEG_LIB_VERSION, sizeof(struct jpeg_compress_struct));
1305
1306 This->stream = pIStream;
1307 IStream_AddRef(pIStream);
1308
1309 This->dest_mgr.next_output_byte = This->dest_buffer;
1310 This->dest_mgr.free_in_buffer = sizeof(This->dest_buffer);
1311
1312 This->dest_mgr.init_destination = dest_mgr_init_destination;
1313 This->dest_mgr.empty_output_buffer = dest_mgr_empty_output_buffer;
1314 This->dest_mgr.term_destination = dest_mgr_term_destination;
1315
1316 This->cinfo.dest = &This->dest_mgr;
1317
1318 This->initialized = TRUE;
1319
1320 LeaveCriticalSection(&This->lock);
1321
1322 return S_OK;
1323 }
1324
1325 static HRESULT WINAPI JpegEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
1326 GUID *pguidContainerFormat)
1327 {
1328 FIXME("(%p,%s): stub\n", iface, debugstr_guid(pguidContainerFormat));
1329 return E_NOTIMPL;
1330 }
1331
1332 static HRESULT WINAPI JpegEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
1333 IWICBitmapEncoderInfo **ppIEncoderInfo)
1334 {
1335 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
1336 return E_NOTIMPL;
1337 }
1338
1339 static HRESULT WINAPI JpegEncoder_SetColorContexts(IWICBitmapEncoder *iface,
1340 UINT cCount, IWICColorContext **ppIColorContext)
1341 {
1342 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1343 return E_NOTIMPL;
1344 }
1345
1346 static HRESULT WINAPI JpegEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
1347 {
1348 TRACE("(%p,%p)\n", iface, pIPalette);
1349 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1350 }
1351
1352 static HRESULT WINAPI JpegEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
1353 {
1354 TRACE("(%p,%p)\n", iface, pIThumbnail);
1355 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1356 }
1357
1358 static HRESULT WINAPI JpegEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
1359 {
1360 TRACE("(%p,%p)\n", iface, pIPreview);
1361 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1362 }
1363
1364 static HRESULT WINAPI JpegEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
1365 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
1366 {
1367 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
1368 HRESULT hr;
1369
1370 TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
1371
1372 EnterCriticalSection(&This->lock);
1373
1374 if (This->frame_count != 0)
1375 {
1376 LeaveCriticalSection(&This->lock);
1377 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1378 }
1379
1380 if (!This->initialized)
1381 {
1382 LeaveCriticalSection(&This->lock);
1383 return WINCODEC_ERR_NOTINITIALIZED;
1384 }
1385
1386 hr = CreatePropertyBag2(NULL, 0, ppIEncoderOptions);
1387 if (FAILED(hr))
1388 {
1389 LeaveCriticalSection(&This->lock);
1390 return hr;
1391 }
1392
1393 This->frame_count = 1;
1394
1395 LeaveCriticalSection(&This->lock);
1396
1397 IWICBitmapEncoder_AddRef(iface);
1398 *ppIFrameEncode = &This->IWICBitmapFrameEncode_iface;
1399
1400 return S_OK;
1401 }
1402
1403 static HRESULT WINAPI JpegEncoder_Commit(IWICBitmapEncoder *iface)
1404 {
1405 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
1406 TRACE("(%p)\n", iface);
1407
1408 EnterCriticalSection(&This->lock);
1409
1410 if (!This->frame_committed || This->committed)
1411 {
1412 LeaveCriticalSection(&This->lock);
1413 return WINCODEC_ERR_WRONGSTATE;
1414 }
1415
1416 This->committed = TRUE;
1417
1418 LeaveCriticalSection(&This->lock);
1419
1420 return S_OK;
1421 }
1422
1423 static HRESULT WINAPI JpegEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
1424 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1425 {
1426 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
1427 return E_NOTIMPL;
1428 }
1429
1430 static const IWICBitmapEncoderVtbl JpegEncoder_Vtbl = {
1431 JpegEncoder_QueryInterface,
1432 JpegEncoder_AddRef,
1433 JpegEncoder_Release,
1434 JpegEncoder_Initialize,
1435 JpegEncoder_GetContainerFormat,
1436 JpegEncoder_GetEncoderInfo,
1437 JpegEncoder_SetColorContexts,
1438 JpegEncoder_SetPalette,
1439 JpegEncoder_SetThumbnail,
1440 JpegEncoder_SetPreview,
1441 JpegEncoder_CreateNewFrame,
1442 JpegEncoder_Commit,
1443 JpegEncoder_GetMetadataQueryWriter
1444 };
1445
1446 HRESULT JpegEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1447 {
1448 JpegEncoder *This;
1449 HRESULT ret;
1450
1451 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1452
1453 *ppv = NULL;
1454
1455 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1456
1457 if (!libjpeg_handle && !load_libjpeg())
1458 {
1459 ERR("Failed writing JPEG because unable to find %s\n",SONAME_LIBJPEG);
1460 return E_FAIL;
1461 }
1462
1463 This = HeapAlloc(GetProcessHeap(), 0, sizeof(JpegEncoder));
1464 if (!This) return E_OUTOFMEMORY;
1465
1466 This->IWICBitmapEncoder_iface.lpVtbl = &JpegEncoder_Vtbl;
1467 This->IWICBitmapFrameEncode_iface.lpVtbl = &JpegEncoder_FrameVtbl;
1468 This->ref = 1;
1469 This->initialized = 0;
1470 This->frame_count = 0;
1471 This->frame_initialized = 0;
1472 This->started_compress = 0;
1473 This->lines_written = 0;
1474 This->frame_committed = 0;
1475 This->committed = 0;
1476 This->width = This->height = 0;
1477 This->xres = This->yres = 0.0;
1478 This->format = NULL;
1479 This->stream = NULL;
1480 InitializeCriticalSection(&This->lock);
1481 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JpegEncoder.lock");
1482
1483 ret = IWICBitmapEncoder_QueryInterface(&This->IWICBitmapEncoder_iface, iid, ppv);
1484 IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
1485
1486 return ret;
1487 }
1488
1489 #else /* !defined(SONAME_LIBJPEG) */
1490
1491 HRESULT JpegDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1492 {
1493 ERR("Trying to load JPEG picture, but JPEG support is not compiled in.\n");
1494 return E_FAIL;
1495 }
1496
1497 HRESULT JpegEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1498 {
1499 ERR("Trying to save JPEG picture, but JPEG support is not compiled in.\n");
1500 return E_FAIL;
1501 }
1502
1503 #endif