d3dx9/tests: Add D3DXCreateTextureFromResource tests.
[wine] / dlls / d3dx9_36 / util.c
1 /*
2  * Copyright (C) 2009 Tony Wasserka
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
20 #include "wine/debug.h"
21 #include "d3dx9_36_private.h"
22
23
24 /************************************************************
25  * pixel format table providing info about number of bytes per pixel,
26  * number of bits per channel and format type.
27  *
28  * Call get_format_info to request information about a specific format.
29  */
30 static const PixelFormatDesc formats[] =
31 {
32    /* format                    bits per channel        shifts per channel   bpp   type        */
33     { D3DFMT_R8G8B8,          {  0,   8,   8,   8 },  {  0,  16,   8,   0 },   3,  FORMAT_ARGB },
34     { D3DFMT_A8R8G8B8,        {  8,   8,   8,   8 },  { 24,  16,   8,   0 },   4,  FORMAT_ARGB },
35     { D3DFMT_X8R8G8B8,        {  0,   8,   8,   8 },  {  0,  16,   8,   0 },   4,  FORMAT_ARGB },
36     { D3DFMT_A8B8G8R8,        {  8,   8,   8,   8 },  { 24,   0,   8,  16 },   4,  FORMAT_ARGB },
37     { D3DFMT_X8B8G8R8,        {  0,   8,   8,   8 },  {  0,   0,   8,  16 },   4,  FORMAT_ARGB },
38     { D3DFMT_R5G6B5,          {  0,   5,   6,   5 },  {  0,  11,   5,   0 },   2,  FORMAT_ARGB },
39     { D3DFMT_X1R5G5B5,        {  0,   5,   5,   5 },  {  0,  10,   5,   0 },   2,  FORMAT_ARGB },
40     { D3DFMT_A1R5G5B5,        {  1,   5,   5,   5 },  { 15,  10,   5,   0 },   2,  FORMAT_ARGB },
41     { D3DFMT_R3G3B2,          {  0,   3,   3,   2 },  {  0,   5,   2,   0 },   1,  FORMAT_ARGB },
42     { D3DFMT_A8R3G3B2,        {  8,   3,   3,   2 },  {  8,   5,   2,   0 },   2,  FORMAT_ARGB },
43     { D3DFMT_A4R4G4B4,        {  4,   4,   4,   4 },  { 12,   8,   4,   0 },   2,  FORMAT_ARGB },
44     { D3DFMT_X4R4G4B4,        {  0,   4,   4,   4 },  {  0,   8,   4,   0 },   2,  FORMAT_ARGB },
45     { D3DFMT_A2R10G10B10,     {  2,  10,  10,  10 },  { 30,  20,  10,   0 },   4,  FORMAT_ARGB },
46     { D3DFMT_A2B10G10R10,     {  2,  10,  10,  10 },  { 30,   0,  10,  20 },   4,  FORMAT_ARGB },
47     { D3DFMT_G16R16,          {  0,  16,  16,   0 },  {  0,   0,  16,   0 },   4,  FORMAT_ARGB },
48     { D3DFMT_A8,              {  8,   0,   0,   0 },  {  0,   0,   0,   0 },   1,  FORMAT_ARGB },
49
50     { D3DFMT_UNKNOWN,         {  0,   0,   0,   0 },  {  0,   0,   0,   0 },   0,  FORMAT_UNKNOWN }, /* marks last element */
51 };
52
53
54 /************************************************************
55  * map_view_of_file
56  *
57  * Loads a file into buffer and stores the number of read bytes in length.
58  *
59  * PARAMS
60  *   filename [I] name of the file to be loaded
61  *   buffer   [O] pointer to destination buffer
62  *   length   [O] size of the obtained data
63  *
64  * RETURNS
65  *   Success: D3D_OK
66  *   Failure:
67  *     see error codes for CreateFileW, GetFileSize, CreateFileMapping and MapViewOfFile
68  *
69  * NOTES
70  *   The caller must UnmapViewOfFile when it doesn't need the data anymore
71  *
72  */
73 HRESULT map_view_of_file(LPCWSTR filename, LPVOID *buffer, DWORD *length)
74 {
75     HANDLE hfile, hmapping = NULL;
76
77     hfile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
78     if(hfile == INVALID_HANDLE_VALUE) goto error;
79
80     *length = GetFileSize(hfile, NULL);
81     if(*length == INVALID_FILE_SIZE) goto error;
82
83     hmapping = CreateFileMappingW(hfile, NULL, PAGE_READONLY, 0, 0, NULL);
84     if(!hmapping) goto error;
85
86     *buffer = MapViewOfFile(hmapping, FILE_MAP_READ, 0, 0, 0);
87     if(*buffer == NULL) goto error;
88
89     CloseHandle(hmapping);
90     CloseHandle(hfile);
91
92     return S_OK;
93
94 error:
95     CloseHandle(hmapping);
96     CloseHandle(hfile);
97     return HRESULT_FROM_WIN32(GetLastError());
98 }
99
100 /************************************************************
101  * load_resource_into_memory
102  *
103  * Loads a resource into buffer and stores the number of
104  * read bytes in length.
105  *
106  * PARAMS
107  *   module  [I] handle to the module
108  *   resinfo [I] handle to the resource's information block
109  *   buffer  [O] pointer to destination buffer
110  *   length  [O] size of the obtained data
111  *
112  * RETURNS
113  *   Success: D3D_OK
114  *   Failure:
115  *     See error codes for SizeofResource, LoadResource and LockResource
116  *
117  * NOTES
118  *   The memory doesn't need to be freed by the caller manually
119  *
120  */
121 HRESULT load_resource_into_memory(HMODULE module, HRSRC resinfo, LPVOID *buffer, DWORD *length)
122 {
123     HGLOBAL resource;
124
125     *length = SizeofResource(module, resinfo);
126     if(*length == 0) return HRESULT_FROM_WIN32(GetLastError());
127
128     resource = LoadResource(module, resinfo);
129     if( !resource ) return HRESULT_FROM_WIN32(GetLastError());
130
131     *buffer = LockResource(resource);
132     if(*buffer == NULL) return HRESULT_FROM_WIN32(GetLastError());
133
134     return S_OK;
135 }
136
137
138 /************************************************************
139  * get_format_info
140  *
141  * Returns information about the specified format.
142  * If the format is unsupported, it's filled with the D3DFMT_UNKNOWN desc.
143  *
144  * PARAMS
145  *   format [I] format whose description is queried
146  *   desc   [O] pointer to a StaticPixelFormatDesc structure
147  *
148  */
149 const PixelFormatDesc *get_format_info(D3DFORMAT format)
150 {
151     unsigned int i = 0;
152     while(formats[i].format != format && formats[i].format != D3DFMT_UNKNOWN) i++;
153     return &formats[i];
154 }