makefiles: Explicitly create destination dirs when installing symlinks.
[wine/zf.git] / dlls / windowscodecs / main.c
blob1b5057b1b95978714ea4a9c144c0ee78f46a4564
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
20 #define COBJMACROS
21 #include "config.h"
23 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winternl.h"
28 #include "objbase.h"
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)
45 switch (fdwReason)
47 case DLL_PROCESS_ATTACH:
48 DisableThreadLibraryCalls(hinstDLL);
49 windowscodecs_module = hinstDLL;
50 break;
51 case DLL_PROCESS_DETACH:
52 ReleaseComponentInfos();
53 break;
56 return WIC_DllMain(hinstDLL, fdwReason, lpvReserved);
59 HRESULT WINAPI DllCanUnloadNow(void)
61 return S_FALSE;
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;
70 HRESULT hr = S_OK;
72 if (width == 0 && height == 0)
74 if (prc)
76 if (prc->Width <= 0 || prc->Height <= 0) return E_INVALIDARG;
77 width = prc->Width;
78 height = prc->Height;
80 else
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;
85 width = src_width;
86 height = src_height;
88 hr = IWICBitmapFrameEncode_SetSize(iface, (UINT)width, (UINT)height);
89 if (FAILED(hr)) return hr;
91 if (width == 0 || height == 0) return E_INVALIDARG;
93 if (!format)
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;
112 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;
121 HRESULT hr=S_OK;
122 WICRect rc;
123 UINT stride;
124 BYTE* pixeldata;
126 if (!prc)
128 UINT src_width, src_height;
129 hr = IWICBitmapSource_GetSize(source, &src_width, &src_height);
130 if (FAILED(hr)) return hr;
131 rc.X = 0;
132 rc.Y = 0;
133 rc.Width = src_width;
134 rc.Height = src_height;
135 prc = &rc;
138 if (prc->Width != width || prc->Height <= 0)
139 return E_INVALIDARG;
141 hr = WICConvertBitmapSource(format, source, &converted_source);
142 if (FAILED(hr))
144 ERR("Failed to convert source, target format %s, %#x\n", debugstr_guid(format), hr);
145 return E_NOTIMPL;
148 if (need_palette)
150 IWICPalette *palette;
152 hr = PaletteImpl_Create(&palette);
153 if (SUCCEEDED(hr))
155 hr = IWICBitmapSource_CopyPalette(converted_source, palette);
157 if (SUCCEEDED(hr))
158 hr = IWICBitmapFrameEncode_SetPalette(iface, palette);
160 IWICPalette_Release(palette);
163 if (FAILED(hr))
165 IWICBitmapSource_Release(converted_source);
166 return hr;
170 stride = (bpp * width + 7)/8;
172 pixeldata = HeapAlloc(GetProcessHeap(), 0, stride * prc->Height);
173 if (!pixeldata)
175 IWICBitmapSource_Release(converted_source);
176 return E_OUTOFMEMORY;
179 hr = IWICBitmapSource_CopyPixels(converted_source, prc, stride,
180 stride*prc->Height, pixeldata);
182 if (SUCCEEDED(hr))
184 hr = IWICBitmapFrameEncode_WritePixels(iface, prc->Height, stride,
185 stride*prc->Height, pixeldata);
188 HeapFree(GetProcessHeap(), 0, pixeldata);
189 IWICBitmapSource_Release(converted_source);
191 return hr;
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)
201 HRESULT hr;
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);
207 if (new_position)
208 *new_position = pos_large.QuadPart;
210 return hr;
213 void reverse_bgr8(UINT bytesperpixel, LPBYTE bits, UINT width, UINT height, INT stride)
215 UINT x, y;
216 BYTE *pixel, temp;
218 for (y=0; y<height; y++)
220 pixel = bits + stride * y;
222 for (x=0; x<width; x++)
224 temp = pixel[2];
225 pixel[2] = pixel[0];
226 pixel[0] = temp;
227 pixel += bytesperpixel;
232 HRESULT get_pixelformat_bpp(const GUID *pixelformat, UINT *bpp)
234 HRESULT hr;
235 IWICComponentInfo *info;
236 IWICPixelFormatInfo *formatinfo;
238 hr = CreateComponentInfo(pixelformat, &info);
239 if (SUCCEEDED(hr))
241 hr = IWICComponentInfo_QueryInterface(info, &IID_IWICPixelFormatInfo, (void**)&formatinfo);
243 if (SUCCEEDED(hr))
245 hr = IWICPixelFormatInfo_GetBitsPerPixel(formatinfo, bpp);
247 IWICPixelFormatInfo_Release(formatinfo);
250 IWICComponentInfo_Release(info);
253 return hr;