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
19 #include "wincodecs_private.h"
21 extern BOOL WINAPI
WIC_DllMain(HINSTANCE
, DWORD
, LPVOID
) DECLSPEC_HIDDEN
;
23 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
28 case DLL_PROCESS_ATTACH
:
29 DisableThreadLibraryCalls(hinstDLL
);
33 return WIC_DllMain(hinstDLL
, fdwReason
, lpvReserved
);
36 HRESULT WINAPI
DllCanUnloadNow(void)
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
)
46 UINT row_offset
; /* number of bits into the source rows where the data starts */
53 rect
.Width
= srcwidth
;
54 rect
.Height
= srcheight
;
59 if (rc
->X
< 0 || rc
->Y
< 0 || rc
->X
+rc
->Width
> srcwidth
|| rc
->Y
+rc
->Height
> srcheight
)
63 bytesperrow
= ((bpp
* rc
->Width
)+7)/8;
65 if (dststride
< bytesperrow
)
68 if ((dststride
* (rc
->Height
-1)) + ((rc
->Width
* bpp
) + 7)/8 > dstbuffersize
)
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
)
75 memcpy(dstbuffer
, srcbuffer
, srcstride
* srcheight
);
79 row_offset
= rc
->X
* bpp
;
81 if (row_offset
% 8 == 0)
83 /* everything lines up on a byte boundary */
88 src
= srcbuffer
+ (row_offset
/ 8) + srcstride
* rc
->Y
;
90 for (row
=0; row
< rc
->Height
; row
++)
92 memcpy(dst
, src
, bytesperrow
);
100 /* we have to do a weird bitwise copy. eww. */
101 FIXME("cannot reliably copy bitmap data if bpp < 8\n");
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
)
113 if (width
== 0 || height
== 0)
114 return WINCODEC_ERR_WRONGSTATE
;
118 WICPixelFormatGUID src_format
;
120 hr
= IWICBitmapSource_GetPixelFormat(source
, &src_format
);
121 if (FAILED(hr
)) return hr
;
123 hr
= IWICBitmapFrameEncode_SetPixelFormat(iface
, &src_format
);
124 if (FAILED(hr
)) return hr
;
127 if (xres
== 0.0 || yres
== 0.0)
129 hr
= IWICBitmapSource_GetResolution(source
, &xres
, &yres
);
130 if (FAILED(hr
)) return hr
;
131 hr
= IWICBitmapFrameEncode_SetResolution(iface
, xres
, yres
);
132 if (FAILED(hr
)) return hr
;
138 HRESULT
write_source(IWICBitmapFrameEncode
*iface
,
139 IWICBitmapSource
*source
, const WICRect
*prc
,
140 const WICPixelFormatGUID
*format
, UINT bpp
,
141 INT width
, INT height
)
143 IWICBitmapSource
*converted_source
;
151 UINT src_width
, src_height
;
152 hr
= IWICBitmapSource_GetSize(source
, &src_width
, &src_height
);
153 if (FAILED(hr
)) return hr
;
156 rc
.Width
= src_width
;
157 rc
.Height
= src_height
;
161 if (prc
->Width
!= width
|| prc
->Height
<= 0)
164 hr
= WICConvertBitmapSource(format
, source
, &converted_source
);
167 ERR("Failed to convert source, target format %s, %#x\n", debugstr_guid(format
), hr
);
171 stride
= (bpp
* width
+ 7)/8;
173 pixeldata
= HeapAlloc(GetProcessHeap(), 0, stride
* prc
->Height
);
176 IWICBitmapSource_Release(converted_source
);
177 return E_OUTOFMEMORY
;
180 hr
= IWICBitmapSource_CopyPixels(converted_source
, prc
, stride
,
181 stride
*prc
->Height
, pixeldata
);
185 hr
= IWICBitmapFrameEncode_WritePixels(iface
, prc
->Height
, stride
,
186 stride
*prc
->Height
, pixeldata
);
189 HeapFree(GetProcessHeap(), 0, pixeldata
);
190 IWICBitmapSource_Release(converted_source
);
195 void reverse_bgr8(UINT bytesperpixel
, LPBYTE bits
, UINT width
, UINT height
, INT stride
)
200 for (y
=0; y
<height
; y
++)
202 pixel
= bits
+ stride
* y
;
204 for (x
=0; x
<width
; x
++)
209 pixel
+= bytesperpixel
;
214 HRESULT
get_pixelformat_bpp(const GUID
*pixelformat
, UINT
*bpp
)
217 IWICComponentInfo
*info
;
218 IWICPixelFormatInfo
*formatinfo
;
220 hr
= CreateComponentInfo(pixelformat
, &info
);
223 hr
= IWICComponentInfo_QueryInterface(info
, &IID_IWICPixelFormatInfo
, (void**)&formatinfo
);
227 hr
= IWICPixelFormatInfo_GetBitsPerPixel(formatinfo
, bpp
);
229 IWICPixelFormatInfo_Release(formatinfo
);
232 IWICComponentInfo_Release(info
);