Synchronize with trunk.
[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 #define WIN32_NO_STATUS
20 #define _INC_WINDOWS
21 #define COM_NO_WINDOWS_H
22
23 #define COBJMACROS
24 #include <config.h>
25
26 #include <stdarg.h>
27
28 #include <windef.h>
29 #include <winbase.h>
30 #include <objbase.h>
31 #include <wincodec.h>
32
33 #include "wincodecs_private.h"
34
35 #include <wine/debug.h>
36
37 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
38
39 extern BOOL WINAPI WIC_DllMain(HINSTANCE, DWORD, LPVOID) DECLSPEC_HIDDEN;
40
41 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
42 {
43
44 switch (fdwReason)
45 {
46 case DLL_PROCESS_ATTACH:
47 DisableThreadLibraryCalls(hinstDLL);
48 break;
49 case DLL_PROCESS_DETACH:
50 break;
51 }
52
53 return WIC_DllMain(hinstDLL, fdwReason, lpvReserved);
54 }
55
56 HRESULT WINAPI DllCanUnloadNow(void)
57 {
58 return S_FALSE;
59 }
60
61 HRESULT copy_pixels(UINT bpp, const BYTE *srcbuffer,
62 UINT srcwidth, UINT srcheight, INT srcstride,
63 const WICRect *rc, UINT dststride, UINT dstbuffersize, BYTE *dstbuffer)
64 {
65 UINT bytesperrow;
66 UINT row_offset; /* number of bits into the source rows where the data starts */
67 WICRect rect;
68
69 if (!rc)
70 {
71 rect.X = 0;
72 rect.Y = 0;
73 rect.Width = srcwidth;
74 rect.Height = srcheight;
75 rc = &rect;
76 }
77 else
78 {
79 if (rc->X < 0 || rc->Y < 0 || rc->X+rc->Width > srcwidth || rc->Y+rc->Height > srcheight)
80 return E_INVALIDARG;
81 }
82
83 bytesperrow = ((bpp * rc->Width)+7)/8;
84
85 if (dststride < bytesperrow)
86 return E_INVALIDARG;
87
88 if ((dststride * (rc->Height-1)) + ((rc->Width * bpp) + 7)/8 > dstbuffersize)
89 return E_INVALIDARG;
90
91 /* if the whole bitmap is copied and the buffer format matches then it's a matter of a single memcpy */
92 if (rc->X == 0 && rc->Y == 0 && rc->Width == srcwidth && rc->Height == srcheight &&
93 srcstride == dststride && srcstride == bytesperrow)
94 {
95 memcpy(dstbuffer, srcbuffer, srcstride * srcheight);
96 return S_OK;
97 }
98
99 row_offset = rc->X * bpp;
100
101 if (row_offset % 8 == 0)
102 {
103 /* everything lines up on a byte boundary */
104 INT row;
105 const BYTE *src;
106 BYTE *dst;
107
108 src = srcbuffer + (row_offset / 8) + srcstride * rc->Y;
109 dst = dstbuffer;
110 for (row=0; row < rc->Height; row++)
111 {
112 memcpy(dst, src, bytesperrow);
113 src += srcstride;
114 dst += dststride;
115 }
116 return S_OK;
117 }
118 else
119 {
120 /* we have to do a weird bitwise copy. eww. */
121 FIXME("cannot reliably copy bitmap data if bpp < 8\n");
122 return E_FAIL;
123 }
124 }
125
126 void reverse_bgr8(UINT bytesperpixel, LPBYTE bits, UINT width, UINT height, INT stride)
127 {
128 UINT x, y;
129 BYTE *pixel, temp;
130
131 for (y=0; y<height; y++)
132 {
133 pixel = bits + stride * y;
134
135 for (x=0; x<width; x++)
136 {
137 temp = pixel[2];
138 pixel[2] = pixel[0];
139 pixel[0] = temp;
140 pixel += bytesperpixel;
141 }
142 }
143 }
144
145 HRESULT get_pixelformat_bpp(const GUID *pixelformat, UINT *bpp)
146 {
147 HRESULT hr;
148 IWICComponentInfo *info;
149 IWICPixelFormatInfo *formatinfo;
150
151 hr = CreateComponentInfo(pixelformat, &info);
152 if (SUCCEEDED(hr))
153 {
154 hr = IWICComponentInfo_QueryInterface(info, &IID_IWICPixelFormatInfo, (void**)&formatinfo);
155
156 if (SUCCEEDED(hr))
157 {
158 hr = IWICPixelFormatInfo_GetBitsPerPixel(formatinfo, bpp);
159
160 IWICPixelFormatInfo_Release(formatinfo);
161 }
162
163 IWICComponentInfo_Release(info);
164 }
165
166 return hr;
167 }