Sync with trunk for console graphics palettes.
[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 }
50
51 return WIC_DllMain(hinstDLL, fdwReason, lpvReserved);
52 }
53
54 HRESULT WINAPI DllCanUnloadNow(void)
55 {
56 return S_FALSE;
57 }
58
59 HRESULT copy_pixels(UINT bpp, const BYTE *srcbuffer,
60 UINT srcwidth, UINT srcheight, INT srcstride,
61 const WICRect *rc, UINT dststride, UINT dstbuffersize, BYTE *dstbuffer)
62 {
63 UINT bytesperrow;
64 UINT row_offset; /* number of bits into the source rows where the data starts */
65 WICRect rect;
66
67 if (!rc)
68 {
69 rect.X = 0;
70 rect.Y = 0;
71 rect.Width = srcwidth;
72 rect.Height = srcheight;
73 rc = &rect;
74 }
75 else
76 {
77 if (rc->X < 0 || rc->Y < 0 || rc->X+rc->Width > srcwidth || rc->Y+rc->Height > srcheight)
78 return E_INVALIDARG;
79 }
80
81 bytesperrow = ((bpp * rc->Width)+7)/8;
82
83 if (dststride < bytesperrow)
84 return E_INVALIDARG;
85
86 if ((dststride * (rc->Height-1)) + ((rc->Width * bpp) + 7)/8 > dstbuffersize)
87 return E_INVALIDARG;
88
89 /* if the whole bitmap is copied and the buffer format matches then it's a matter of a single memcpy */
90 if (rc->X == 0 && rc->Y == 0 && rc->Width == srcwidth && rc->Height == srcheight &&
91 srcstride == dststride && srcstride == bytesperrow)
92 {
93 memcpy(dstbuffer, srcbuffer, srcstride * srcheight);
94 return S_OK;
95 }
96
97 row_offset = rc->X * bpp;
98
99 if (row_offset % 8 == 0)
100 {
101 /* everything lines up on a byte boundary */
102 INT row;
103 const BYTE *src;
104 BYTE *dst;
105
106 src = srcbuffer + (row_offset / 8) + srcstride * rc->Y;
107 dst = dstbuffer;
108 for (row=0; row < rc->Height; row++)
109 {
110 memcpy(dst, src, bytesperrow);
111 src += srcstride;
112 dst += dststride;
113 }
114 return S_OK;
115 }
116 else
117 {
118 /* we have to do a weird bitwise copy. eww. */
119 FIXME("cannot reliably copy bitmap data if bpp < 8\n");
120 return E_FAIL;
121 }
122 }
123
124 void reverse_bgr8(UINT bytesperpixel, LPBYTE bits, UINT width, UINT height, INT stride)
125 {
126 UINT x, y;
127 BYTE *pixel, temp;
128
129 for (y=0; y<height; y++)
130 {
131 pixel = bits + stride * y;
132
133 for (x=0; x<width; x++)
134 {
135 temp = pixel[2];
136 pixel[2] = pixel[0];
137 pixel[0] = temp;
138 pixel += bytesperpixel;
139 }
140 }
141 }
142
143 HRESULT get_pixelformat_bpp(const GUID *pixelformat, UINT *bpp)
144 {
145 HRESULT hr;
146 IWICComponentInfo *info;
147 IWICPixelFormatInfo *formatinfo;
148
149 hr = CreateComponentInfo(pixelformat, &info);
150 if (SUCCEEDED(hr))
151 {
152 hr = IWICComponentInfo_QueryInterface(info, &IID_IWICPixelFormatInfo, (void**)&formatinfo);
153
154 if (SUCCEEDED(hr))
155 {
156 hr = IWICPixelFormatInfo_GetBitsPerPixel(formatinfo, bpp);
157
158 IWICPixelFormatInfo_Release(formatinfo);
159 }
160
161 IWICComponentInfo_Release(info);
162 }
163
164 return hr;
165 }