* Sync up to trunk head (r65074).
[reactos.git] / dll / win32 / windowscodecs / main.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 extern BOOL WINAPI WIC_DllMain(HINSTANCE, DWORD, LPVOID) DECLSPEC_HIDDEN;
22
23 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
24 {
25
26 switch (fdwReason)
27 {
28 case DLL_PROCESS_ATTACH:
29 DisableThreadLibraryCalls(hinstDLL);
30 break;
31 }
32
33 return WIC_DllMain(hinstDLL, fdwReason, lpvReserved);
34 }
35
36 HRESULT WINAPI DllCanUnloadNow(void)
37 {
38 return S_FALSE;
39 }
40
41 HRESULT copy_pixels(UINT bpp, const BYTE *srcbuffer,
42 UINT srcwidth, UINT srcheight, INT srcstride,
43 const WICRect *rc, UINT dststride, UINT dstbuffersize, BYTE *dstbuffer)
44 {
45 UINT bytesperrow;
46 UINT row_offset; /* number of bits into the source rows where the data starts */
47 WICRect rect;
48
49 if (!rc)
50 {
51 rect.X = 0;
52 rect.Y = 0;
53 rect.Width = srcwidth;
54 rect.Height = srcheight;
55 rc = ▭
56 }
57 else
58 {
59 if (rc->X < 0 || rc->Y < 0 || rc->X+rc->Width > srcwidth || rc->Y+rc->Height > srcheight)
60 return E_INVALIDARG;
61 }
62
63 bytesperrow = ((bpp * rc->Width)+7)/8;
64
65 if (dststride < bytesperrow)
66 return E_INVALIDARG;
67
68 if ((dststride * (rc->Height-1)) + ((rc->Width * bpp) + 7)/8 > dstbuffersize)
69 return E_INVALIDARG;
70
71 /* if the whole bitmap is copied and the buffer format matches then it's a matter of a single memcpy */
72 if (rc->X == 0 && rc->Y == 0 && rc->Width == srcwidth && rc->Height == srcheight &&
73 srcstride == dststride && srcstride == bytesperrow)
74 {
75 memcpy(dstbuffer, srcbuffer, srcstride * srcheight);
76 return S_OK;
77 }
78
79 row_offset = rc->X * bpp;
80
81 if (row_offset % 8 == 0)
82 {
83 /* everything lines up on a byte boundary */
84 INT row;
85 const BYTE *src;
86 BYTE *dst;
87
88 src = srcbuffer + (row_offset / 8) + srcstride * rc->Y;
89 dst = dstbuffer;
90 for (row=0; row < rc->Height; row++)
91 {
92 memcpy(dst, src, bytesperrow);
93 src += srcstride;
94 dst += dststride;
95 }
96 return S_OK;
97 }
98 else
99 {
100 /* we have to do a weird bitwise copy. eww. */
101 FIXME("cannot reliably copy bitmap data if bpp < 8\n");
102 return E_FAIL;
103 }
104 }
105
106 HRESULT configure_write_source(IWICBitmapFrameEncode *iface,
107 IWICBitmapSource *source, const WICRect *prc,
108 const WICPixelFormatGUID *format,
109 INT width, INT height, double xres, double yres)
110 {
111 HRESULT hr=S_OK;
112 WICPixelFormatGUID src_format, dst_format;
113
114 if (width == 0 || height == 0)
115 return WINCODEC_ERR_WRONGSTATE;
116
117 hr = IWICBitmapSource_GetPixelFormat(source, &src_format);
118 if (FAILED(hr)) return hr;
119
120 if (!format)
121 {
122 dst_format = src_format;
123
124 hr = IWICBitmapFrameEncode_SetPixelFormat(iface, &dst_format);
125 if (FAILED(hr)) return hr;
126
127 format = &dst_format;
128 }
129
130 if (!IsEqualGUID(&src_format, format))
131 {
132 /* FIXME: should use WICConvertBitmapSource to convert */
133 ERR("format %s unsupported\n", debugstr_guid(&src_format));
134 return E_FAIL;
135 }
136
137 if (xres == 0.0 || yres == 0.0)
138 {
139 hr = IWICBitmapSource_GetResolution(source, &xres, &yres);
140 if (FAILED(hr)) return hr;
141 hr = IWICBitmapFrameEncode_SetResolution(iface, xres, yres);
142 if (FAILED(hr)) return hr;
143 }
144
145 return hr;
146 }
147
148 HRESULT write_source(IWICBitmapFrameEncode *iface,
149 IWICBitmapSource *source, const WICRect *prc,
150 const WICPixelFormatGUID *format, UINT bpp,
151 INT width, INT height)
152 {
153 HRESULT hr=S_OK;
154 WICRect rc;
155 UINT stride;
156 BYTE* pixeldata;
157
158 if (!prc)
159 {
160 UINT src_width, src_height;
161 hr = IWICBitmapSource_GetSize(source, &src_width, &src_height);
162 if (FAILED(hr)) return hr;
163 rc.X = 0;
164 rc.Y = 0;
165 rc.Width = src_width;
166 rc.Height = src_height;
167 prc = &rc;
168 }
169
170 if (prc->Width != width || prc->Height <= 0)
171 return E_INVALIDARG;
172
173 stride = (bpp * width + 7)/8;
174
175 pixeldata = HeapAlloc(GetProcessHeap(), 0, stride * prc->Height);
176 if (!pixeldata) return E_OUTOFMEMORY;
177
178 hr = IWICBitmapSource_CopyPixels(source, prc, stride,
179 stride*prc->Height, pixeldata);
180
181 if (SUCCEEDED(hr))
182 {
183 hr = IWICBitmapFrameEncode_WritePixels(iface, prc->Height, stride,
184 stride*prc->Height, pixeldata);
185 }
186
187 HeapFree(GetProcessHeap(), 0, pixeldata);
188
189 return hr;
190 }
191
192 void reverse_bgr8(UINT bytesperpixel, LPBYTE bits, UINT width, UINT height, INT stride)
193 {
194 UINT x, y;
195 BYTE *pixel, temp;
196
197 for (y=0; y<height; y++)
198 {
199 pixel = bits + stride * y;
200
201 for (x=0; x<width; x++)
202 {
203 temp = pixel[2];
204 pixel[2] = pixel[0];
205 pixel[0] = temp;
206 pixel += bytesperpixel;
207 }
208 }
209 }
210
211 HRESULT get_pixelformat_bpp(const GUID *pixelformat, UINT *bpp)
212 {
213 HRESULT hr;
214 IWICComponentInfo *info;
215 IWICPixelFormatInfo *formatinfo;
216
217 hr = CreateComponentInfo(pixelformat, &info);
218 if (SUCCEEDED(hr))
219 {
220 hr = IWICComponentInfo_QueryInterface(info, &IID_IWICPixelFormatInfo, (void**)&formatinfo);
221
222 if (SUCCEEDED(hr))
223 {
224 hr = IWICPixelFormatInfo_GetBitsPerPixel(formatinfo, bpp);
225
226 IWICPixelFormatInfo_Release(formatinfo);
227 }
228
229 IWICComponentInfo_Release(info);
230 }
231
232 return hr;
233 }