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
30 #include "wincodecs_private.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs
);
36 #include "wincodecs_common.h"
38 extern BOOL WINAPI
WIC_DllMain(HINSTANCE
, DWORD
, LPVOID
) DECLSPEC_HIDDEN
;
40 HMODULE windowscodecs_module
= 0;
42 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
47 case DLL_PROCESS_ATTACH
:
48 DisableThreadLibraryCalls(hinstDLL
);
49 windowscodecs_module
= hinstDLL
;
51 case DLL_PROCESS_DETACH
:
52 ReleaseComponentInfos();
56 return WIC_DllMain(hinstDLL
, fdwReason
, lpvReserved
);
59 HRESULT WINAPI
DllCanUnloadNow(void)
64 HRESULT
configure_write_source(IWICBitmapFrameEncode
*iface
,
65 IWICBitmapSource
*source
, const WICRect
*prc
,
66 const WICPixelFormatGUID
*format
,
67 INT width
, INT height
, double xres
, double yres
)
69 UINT src_width
, src_height
;
72 if (width
== 0 && height
== 0)
76 if (prc
->Width
<= 0 || prc
->Height
<= 0) return E_INVALIDARG
;
82 hr
= IWICBitmapSource_GetSize(source
, &src_width
, &src_height
);
83 if (FAILED(hr
)) return hr
;
84 if (src_width
== 0 || src_height
== 0) return E_INVALIDARG
;
88 hr
= IWICBitmapFrameEncode_SetSize(iface
, (UINT
)width
, (UINT
)height
);
89 if (FAILED(hr
)) return hr
;
91 if (width
== 0 || height
== 0) return E_INVALIDARG
;
95 WICPixelFormatGUID src_format
;
97 hr
= IWICBitmapSource_GetPixelFormat(source
, &src_format
);
98 if (FAILED(hr
)) return hr
;
100 hr
= IWICBitmapFrameEncode_SetPixelFormat(iface
, &src_format
);
101 if (FAILED(hr
)) return hr
;
104 if (xres
== 0.0 || yres
== 0.0)
106 hr
= IWICBitmapSource_GetResolution(source
, &xres
, &yres
);
107 if (FAILED(hr
)) return hr
;
108 hr
= IWICBitmapFrameEncode_SetResolution(iface
, xres
, yres
);
109 if (FAILED(hr
)) return hr
;
115 HRESULT
write_source(IWICBitmapFrameEncode
*iface
,
116 IWICBitmapSource
*source
, const WICRect
*prc
,
117 const WICPixelFormatGUID
*format
, UINT bpp
, BOOL need_palette
,
118 INT width
, INT height
)
120 IWICBitmapSource
*converted_source
;
128 UINT src_width
, src_height
;
129 hr
= IWICBitmapSource_GetSize(source
, &src_width
, &src_height
);
130 if (FAILED(hr
)) return hr
;
133 rc
.Width
= src_width
;
134 rc
.Height
= src_height
;
138 if (prc
->Width
!= width
|| prc
->Height
<= 0)
141 hr
= WICConvertBitmapSource(format
, source
, &converted_source
);
144 ERR("Failed to convert source, target format %s, %#x\n", debugstr_guid(format
), hr
);
150 IWICPalette
*palette
;
152 hr
= PaletteImpl_Create(&palette
);
155 hr
= IWICBitmapSource_CopyPalette(converted_source
, palette
);
158 hr
= IWICBitmapFrameEncode_SetPalette(iface
, palette
);
160 IWICPalette_Release(palette
);
165 IWICBitmapSource_Release(converted_source
);
170 stride
= (bpp
* width
+ 7)/8;
172 pixeldata
= HeapAlloc(GetProcessHeap(), 0, stride
* prc
->Height
);
175 IWICBitmapSource_Release(converted_source
);
176 return E_OUTOFMEMORY
;
179 hr
= IWICBitmapSource_CopyPixels(converted_source
, prc
, stride
,
180 stride
*prc
->Height
, pixeldata
);
184 hr
= IWICBitmapFrameEncode_WritePixels(iface
, prc
->Height
, stride
,
185 stride
*prc
->Height
, pixeldata
);
188 HeapFree(GetProcessHeap(), 0, pixeldata
);
189 IWICBitmapSource_Release(converted_source
);
194 HRESULT CDECL
stream_read(IStream
*stream
, void *buffer
, ULONG read
, ULONG
*bytes_read
)
196 return IStream_Read(stream
, buffer
, read
, bytes_read
);
199 HRESULT CDECL
stream_seek(IStream
*stream
, LONGLONG ofs
, DWORD origin
, ULONGLONG
*new_position
)
202 LARGE_INTEGER ofs_large
;
203 ULARGE_INTEGER pos_large
;
205 ofs_large
.QuadPart
= ofs
;
206 hr
= IStream_Seek(stream
, ofs_large
, origin
, &pos_large
);
208 *new_position
= pos_large
.QuadPart
;
213 void reverse_bgr8(UINT bytesperpixel
, LPBYTE bits
, UINT width
, UINT height
, INT stride
)
218 for (y
=0; y
<height
; y
++)
220 pixel
= bits
+ stride
* y
;
222 for (x
=0; x
<width
; x
++)
227 pixel
+= bytesperpixel
;
232 HRESULT
get_pixelformat_bpp(const GUID
*pixelformat
, UINT
*bpp
)
235 IWICComponentInfo
*info
;
236 IWICPixelFormatInfo
*formatinfo
;
238 hr
= CreateComponentInfo(pixelformat
, &info
);
241 hr
= IWICComponentInfo_QueryInterface(info
, &IID_IWICPixelFormatInfo
, (void**)&formatinfo
);
245 hr
= IWICPixelFormatInfo_GetBitsPerPixel(formatinfo
, bpp
);
247 IWICPixelFormatInfo_Release(formatinfo
);
250 IWICComponentInfo_Release(info
);