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
29 #include "wincodecs_private.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs
);
35 extern BOOL WINAPI
WIC_DllMain(HINSTANCE
, DWORD
, LPVOID
) DECLSPEC_HIDDEN
;
37 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
42 case DLL_PROCESS_ATTACH
:
43 DisableThreadLibraryCalls(hinstDLL
);
47 return WIC_DllMain(hinstDLL
, fdwReason
, lpvReserved
);
50 HRESULT WINAPI
DllCanUnloadNow(void)
55 HRESULT
copy_pixels(UINT bpp
, const BYTE
*srcbuffer
,
56 UINT srcwidth
, UINT srcheight
, INT srcstride
,
57 const WICRect
*rc
, UINT dststride
, UINT dstbuffersize
, BYTE
*dstbuffer
)
60 UINT row_offset
; /* number of bits into the source rows where the data starts */
67 rect
.Width
= srcwidth
;
68 rect
.Height
= srcheight
;
73 if (rc
->X
< 0 || rc
->Y
< 0 || rc
->X
+rc
->Width
> srcwidth
|| rc
->Y
+rc
->Height
> srcheight
)
77 bytesperrow
= ((bpp
* rc
->Width
)+7)/8;
79 if (dststride
< bytesperrow
)
82 if ((dststride
* (rc
->Height
-1)) + ((rc
->Width
* bpp
) + 7)/8 > dstbuffersize
)
85 /* if the whole bitmap is copied and the buffer format matches then it's a matter of a single memcpy */
86 if (rc
->X
== 0 && rc
->Y
== 0 && rc
->Width
== srcwidth
&& rc
->Height
== srcheight
&&
87 srcstride
== dststride
&& srcstride
== bytesperrow
)
89 memcpy(dstbuffer
, srcbuffer
, srcstride
* srcheight
);
93 row_offset
= rc
->X
* bpp
;
95 if (row_offset
% 8 == 0)
97 /* everything lines up on a byte boundary */
102 src
= srcbuffer
+ (row_offset
/ 8) + srcstride
* rc
->Y
;
104 for (row
=0; row
< rc
->Height
; row
++)
106 memcpy(dst
, src
, bytesperrow
);
114 /* we have to do a weird bitwise copy. eww. */
115 FIXME("cannot reliably copy bitmap data if bpp < 8\n");
120 HRESULT
configure_write_source(IWICBitmapFrameEncode
*iface
,
121 IWICBitmapSource
*source
, const WICRect
*prc
,
122 const WICPixelFormatGUID
*format
,
123 INT width
, INT height
, double xres
, double yres
)
126 WICPixelFormatGUID src_format
, dst_format
;
128 if (width
== 0 || height
== 0)
129 return WINCODEC_ERR_WRONGSTATE
;
131 hr
= IWICBitmapSource_GetPixelFormat(source
, &src_format
);
132 if (FAILED(hr
)) return hr
;
136 dst_format
= src_format
;
138 hr
= IWICBitmapFrameEncode_SetPixelFormat(iface
, &dst_format
);
139 if (FAILED(hr
)) return hr
;
141 format
= &dst_format
;
144 if (!IsEqualGUID(&src_format
, format
))
146 /* FIXME: should use WICConvertBitmapSource to convert */
147 ERR("format %s unsupported\n", debugstr_guid(&src_format
));
151 if (xres
== 0.0 || yres
== 0.0)
153 hr
= IWICBitmapSource_GetResolution(source
, &xres
, &yres
);
154 if (FAILED(hr
)) return hr
;
155 hr
= IWICBitmapFrameEncode_SetResolution(iface
, xres
, yres
);
156 if (FAILED(hr
)) return hr
;
162 HRESULT
write_source(IWICBitmapFrameEncode
*iface
,
163 IWICBitmapSource
*source
, const WICRect
*prc
,
164 const WICPixelFormatGUID
*format
, UINT bpp
,
165 INT width
, INT height
)
174 UINT src_width
, src_height
;
175 hr
= IWICBitmapSource_GetSize(source
, &src_width
, &src_height
);
176 if (FAILED(hr
)) return hr
;
179 rc
.Width
= src_width
;
180 rc
.Height
= src_height
;
184 if (prc
->Width
!= width
|| prc
->Height
<= 0)
187 stride
= (bpp
* width
+ 7)/8;
189 pixeldata
= HeapAlloc(GetProcessHeap(), 0, stride
* prc
->Height
);
190 if (!pixeldata
) return E_OUTOFMEMORY
;
192 hr
= IWICBitmapSource_CopyPixels(source
, prc
, stride
,
193 stride
*prc
->Height
, pixeldata
);
197 hr
= IWICBitmapFrameEncode_WritePixels(iface
, prc
->Height
, stride
,
198 stride
*prc
->Height
, pixeldata
);
201 HeapFree(GetProcessHeap(), 0, pixeldata
);
206 void reverse_bgr8(UINT bytesperpixel
, LPBYTE bits
, UINT width
, UINT height
, INT stride
)
211 for (y
=0; y
<height
; y
++)
213 pixel
= bits
+ stride
* y
;
215 for (x
=0; x
<width
; x
++)
220 pixel
+= bytesperpixel
;
225 HRESULT
get_pixelformat_bpp(const GUID
*pixelformat
, UINT
*bpp
)
228 IWICComponentInfo
*info
;
229 IWICPixelFormatInfo
*formatinfo
;
231 hr
= CreateComponentInfo(pixelformat
, &info
);
234 hr
= IWICComponentInfo_QueryInterface(info
, &IID_IWICPixelFormatInfo
, (void**)&formatinfo
);
238 hr
= IWICPixelFormatInfo_GetBitsPerPixel(formatinfo
, bpp
);
240 IWICPixelFormatInfo_Release(formatinfo
);
243 IWICComponentInfo_Release(info
);