[PATCH] d3drm/tests: Add tests for IDirect3DRM*::LoadTexture (try 2).

Aaryaman Vasishta jem456.vasishta at gmail.com
Fri Dec 18 07:15:28 CST 2015


Added bitmap generation function to generate bitmaps at runtime.

Signed-off-by: Aaryaman Vasishta <jem456.vasishta at gmail.com>
---
 dlls/d3drm/tests/Makefile.in |   2 +-
 dlls/d3drm/tests/d3drm.c     | 381 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 382 insertions(+), 1 deletion(-)

diff --git a/dlls/d3drm/tests/Makefile.in b/dlls/d3drm/tests/Makefile.in
index 4b39989..76613e2 100644
--- a/dlls/d3drm/tests/Makefile.in
+++ b/dlls/d3drm/tests/Makefile.in
@@ -1,5 +1,5 @@
 TESTDLL   = d3drm.dll
-IMPORTS   = dxguid uuid d3drm ddraw user32
+IMPORTS   = dxguid uuid d3drm ddraw user32 gdi32
 
 C_SRCS = \
 	d3drm.c \
diff --git a/dlls/d3drm/tests/d3drm.c b/dlls/d3drm/tests/d3drm.c
index 06c2c54..30126af 100644
--- a/dlls/d3drm/tests/d3drm.c
+++ b/dlls/d3drm/tests/d3drm.c
@@ -2,6 +2,7 @@
  * Copyright 2010, 2012 Christian Costa
  * Copyright 2012 André Hentschel
  * Copyright 2011-2014 Henri Verbeet for CodeWeavers
+ * Copyright 2014-2015 Aaryaman Vasishta
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -3693,6 +3694,383 @@ static void test_create_device_from_d3d3(void)
     DestroyWindow(window);
 }
 
+static char *create_bitmap(const char *filename, const int w, const int h, const int type)
+{
+    static char path[MAX_PATH];
+    HANDLE file;
+    HBITMAP hbitmap;
+    BITMAPFILEHEADER file_header;
+    BITMAPINFOHEADER info_header;
+    BITMAPINFO *info = NULL;
+    /* Type 1 bitmaps are 8 bpp whereas type 2 bitmaps are 24bpp. */
+    const int bpp = type == 1 ? 8 : 24;
+    unsigned char *buffer = NULL;
+    DWORD written, size;
+    int i, j;
+
+    /* Calculate size of the BITMAPINFO header */
+    /* Space for 256 color palette would need to be allocated if the bitmap is 8bpp */
+    size = sizeof(BITMAPINFOHEADER);
+    if (type == 1)
+        size += sizeof(RGBQUAD)* 256;
+
+    ZeroMemory(&file_header, sizeof(BITMAPFILEHEADER));
+    file_header.bfType = 0x4d42;
+    file_header.bfOffBits = sizeof(BITMAPFILEHEADER)+size;
+    file_header.bfSize = ((((w * bpp + 31) & ~31) >> 3) * h) + file_header.bfOffBits;
+    ZeroMemory(&info_header, sizeof(BITMAPINFOHEADER));
+    info_header.biSize = sizeof(BITMAPINFOHEADER);
+    info_header.biBitCount = bpp;
+    info_header.biPlanes = 1;
+    info_header.biWidth = w;
+    info_header.biHeight = h;
+    info_header.biCompression = BI_RGB;
+
+    if (type == 1)
+    {
+        info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
+        /* Create a 256 color palette for 8bpp bitmap */
+        for (i = 0; i < 256; i++)
+        {
+            info->bmiColors[i].rgbBlue = i;
+            info->bmiColors[i].rgbGreen = i;
+            info->bmiColors[i].rgbRed = i;
+        }
+    }
+    else
+        info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFO));
+    info->bmiHeader = info_header;
+
+    /* Create the bitmap file */
+    GetTempPathA(sizeof(path) / sizeof(char), path);
+    lstrcatA(path, filename);
+    file = CreateFileA(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
+    ok(file != INVALID_HANDLE_VALUE, "file creation failed, at %s, error %d\n", path,
+            GetLastError());
+
+    /* Write the file header, followed by the BITMAPINFO header */
+    WriteFile(file, (void *)&file_header, sizeof(BITMAPFILEHEADER), &written, NULL);
+    ok(written == sizeof(BITMAPFILEHEADER), "Couldn't write bitmap file header.\n");
+    WriteFile(file, (void *)info, size, &written, NULL);
+    ok(written == size, "Couldn't write bitmap info header.\n");
+
+    hbitmap = CreateDIBSection(NULL, info, DIB_RGB_COLORS, (void **)&buffer, 0, 0);
+    ok(hbitmap != (HBITMAP)ERROR_INVALID_PARAMETER, "Could not create bitmap.\n");
+
+    size = w * h * (bpp / 8);
+    /* Saving pixels by directly accessing bitmap buffer is faster than using SetPixel */
+    for (i = 0, j = 0; i < size;)
+    {
+        if (type == 1)
+        {
+            buffer[i++] = j;
+            buffer[i++] = j;
+            buffer[i++] = j++;
+            j %= 256;
+        }
+        else
+        {
+            buffer[i] = (i + rand()) % 255;
+            i++;
+        }
+    }
+
+    /* Finally, write the bitmap pixels */
+    size = file_header.bfSize;
+    WriteFile(file, (void *)buffer, size, &written, NULL);
+    ok(written == size, "Couldn't write bitmap pixels.\n");
+
+    CloseHandle(file);
+    DeleteObject(hbitmap);
+
+    return path;
+}
+
+static BOOL test_bitmap_data(unsigned char *buffer1, unsigned char *buffer2, D3DRMIMAGE *img, const int depth, const int version)
+{
+    const int w = img->width;
+    const int h = img->height;
+    int i, j, k = 0, offset;
+    BOOL val1, val2, val3;
+
+    if (img->palette)
+    {
+        if (depth == 8)
+        {
+            for (i = 0; i < h; i++)
+            {
+                offset = version == 1 ? (img->bytes_per_line * i) : (img->bytes_per_line * (h - i - 1));
+                for (j = 0; j < img->bytes_per_line; j++)
+                {
+                    if (buffer1[k++] != buffer2[offset++])
+                        return FALSE;
+                }
+            }
+        }
+        else
+        {
+            /* d3drm aligns the 24bpp texture to 4 bytes in the buffer, with one bype padding from 24bpp texture. */
+            /* The image is palettized if the total number of colors used is <= 256. */
+            for (i = 0; i < h; i++)
+            {
+                offset = version == 1 ? (w * i * 3) : (w * (h - i - 1) * 3);
+                for (j = 0; j < w; j++)
+                {
+                    val1 = img->palette[buffer1[k]].blue == buffer2[offset];
+                    val2 = img->palette[buffer1[k]].green == buffer2[offset + 1];
+                    val3 = img->palette[buffer1[k]].red == buffer2[offset + 2];
+                    if (!(val1 && val2 && val3))
+                        return FALSE;
+                    k++;
+                    /* Take into account padded bytes after every row for odd widths */
+                    if ((w % 2 == 1) && (j == w - 1))
+                        k += img->bytes_per_line - w;
+                    offset += 3;
+                }
+            }
+        }
+    }
+    else
+    {
+        for (i = 0; i < h; i++)
+        {
+            offset = version == 1 ? (w * i * 3) : (w * (h - i - 1) * 3);
+            for (j = 0; j < w; j++)
+            {
+                val1 = buffer1[k] == buffer2[offset];
+                val2 = buffer1[k + 1] == buffer2[offset + 1];
+                val3 = buffer1[k + 2] == buffer2[offset + 2];
+                if (!(val1 && val2 && val3))
+                    return FALSE;
+                k += 4;
+                offset += 3;
+            }
+        }
+    }
+
+    return TRUE;
+}
+
+static void test_load_texture1(void)
+{
+    HRESULT hr;
+    IDirect3DRM *d3drm1 = NULL;
+    IDirect3DRMTexture *texture1 = NULL;
+    D3DRMIMAGE *d3drm_img = NULL;
+    HWND window;
+    BITMAPFILEHEADER *bmp_header = NULL;
+    BITMAPINFO *bmp_info = NULL;
+    HANDLE hfile, hmapping = NULL;
+    BOOL check;
+    DWORD size;
+    char *file = NULL;
+    unsigned char *buffer = NULL, *buffer1 = NULL, *buffer2 = NULL;
+
+    window = CreateWindowA("static", "d3drm_test", WS_OVERLAPPEDWINDOW, 0, 0, 300, 200, 0, 0, 0, 0);
+    file = create_bitmap("8bpp.bmp", 100, 100, 1);
+
+    /* Test if image information matches that from the header by loading the bitmap seperately. */
+    hfile = CreateFileA(file, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
+    ok(file != INVALID_HANDLE_VALUE, "Failed to open file at %s, error %d\n", file,
+            GetLastError());
+    if (hfile == INVALID_HANDLE_VALUE)
+        goto cleanup;
+
+    size = GetFileSize(hfile, NULL);
+    ok(size != INVALID_FILE_SIZE, "Failed to get file size, error %d\n", GetLastError());
+    if (size == INVALID_FILE_SIZE)
+        goto cleanup;
+
+    hmapping = CreateFileMappingA(hfile, NULL, PAGE_READONLY, 0, 0, NULL);
+    ok(hmapping != NULL, "Cannot create file mapping (error = %x).\n", GetLastError());
+    if (hmapping == NULL)
+        goto cleanup;
+
+    buffer = MapViewOfFile(hmapping, FILE_MAP_READ, 0, 0, 0);
+    ok(buffer != NULL, "Cannot create map view of file (error = %x).\n", GetLastError());
+    if (buffer == NULL)
+        goto cleanup;
+
+    bmp_header = (BITMAPFILEHEADER *)buffer;
+    bmp_info = (BITMAPINFO *)((unsigned char *)buffer + sizeof(BITMAPFILEHEADER));
+    buffer1 = ((unsigned char *)buffer + bmp_header->bfOffBits);
+
+    hr = Direct3DRMCreate(&d3drm1);
+    ok(hr == D3DRM_OK, "Cannot get IDirect3DRM interface (hr = %x)\n", hr);
+
+    hr = IDirect3DRM_LoadTexture(d3drm1, file, &texture1);
+    ok(SUCCEEDED(hr), "Failed to load texture (hr = %x).\n", hr);
+    d3drm_img = IDirect3DRMTexture_GetImage(texture1);
+    todo_wine ok(d3drm_img != NULL, "Could not get a D3DRMIMAGE struct from texture.\n");
+    if (d3drm_img == NULL)
+        goto cleanup;
+    ok(bmp_info->bmiHeader.biWidth == d3drm_img->width, "Expected texture width == %d, got %d.\n", bmp_info->bmiHeader.biWidth, d3drm_img->width);
+    ok(bmp_info->bmiHeader.biHeight == d3drm_img->height, "Expected texture height == %d, got %d.\n", bmp_info->bmiHeader.biHeight, d3drm_img->height);
+    buffer2 = (unsigned char *)d3drm_img->buffer1;
+    check = test_bitmap_data(buffer2, buffer1, d3drm_img, bmp_info->bmiHeader.biBitCount, 1);
+    ok(check, "Buffer contents do not match that of bitmap.\n");
+
+cleanup:
+    UnmapViewOfFile(buffer);
+    CloseHandle(hmapping);
+    CloseHandle(hfile);
+    IDirect3DRMTexture_Release(texture1);
+    IDirect3DRM_Release(d3drm1);
+    check = DeleteFileA(file);
+    ok(check, "Cannot delete image stored in %s (error = %x).\n", file, GetLastError());
+    DestroyWindow(window);
+}
+
+static void test_load_texture2(void)
+{
+    HRESULT hr;
+    IDirect3DRM *d3drm1 = NULL;
+    IDirect3DRM2 *d3drm2 = NULL;
+    IDirect3DRMTexture2 *texture2 = NULL;
+    D3DRMIMAGE *d3drm_img = NULL;
+    HWND window;
+    BITMAPFILEHEADER *bmp_header = NULL;
+    BITMAPINFO *bmp_info = NULL;
+    HANDLE hfile, hmapping = NULL;
+    BOOL check;
+    DWORD size;
+    char *file;
+    unsigned char *buffer = NULL, *buffer1 = NULL, *buffer2 = NULL;
+
+    window = CreateWindowA("static", "d3drm_test", WS_OVERLAPPEDWINDOW, 0, 0, 300, 200, 0, 0, 0, 0);
+    file = create_bitmap("palette.bmp", 3, 39, 2);
+
+    /* Test if image information matches that from the header by loading the bitmap seperately. */
+    hfile = CreateFileA(file, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
+    ok(file != INVALID_HANDLE_VALUE, "Failed to open file at %s, error %d\n", file,
+            GetLastError());
+    if (hfile == INVALID_HANDLE_VALUE)
+        goto cleanup;
+
+    size = GetFileSize(hfile, NULL);
+    ok(size != INVALID_FILE_SIZE, "Failed to get file size, error %d\n", GetLastError());
+    if (size == INVALID_FILE_SIZE)
+        goto cleanup;
+
+    hmapping = CreateFileMappingA(hfile, NULL, PAGE_READONLY, 0, 0, NULL);
+    ok(hmapping != NULL, "Cannot create file mapping (error = %x).\n", GetLastError());
+    if (hmapping == NULL)
+        goto cleanup;
+
+    buffer = MapViewOfFile(hmapping, FILE_MAP_READ, 0, 0, 0);
+    ok(buffer != NULL, "Cannot create map view of file (error = %x).\n", GetLastError());
+    if (buffer == NULL)
+        goto cleanup;
+
+
+    bmp_header = (BITMAPFILEHEADER *)buffer;
+    bmp_info = (BITMAPINFO *)((unsigned char *)buffer + sizeof(BITMAPFILEHEADER));
+    buffer1 = ((unsigned char *)buffer + bmp_header->bfOffBits);
+
+    hr = Direct3DRMCreate(&d3drm1);
+    ok(hr == D3DRM_OK, "Cannot get IDirect3DRM interface (hr = %x)\n", hr);
+
+    hr = IDirect3DRM_QueryInterface(d3drm1, &IID_IDirect3DRM2, (void **)&d3drm2);
+    ok(SUCCEEDED(hr), "Cannot get IDirect3DRM2 interface (hr = %x)\n", hr);
+    hr = IDirect3DRM2_LoadTexture(d3drm2, file, &texture2);
+    ok(SUCCEEDED(hr), "Failed to load texture (hr = %x).\n", hr);
+    d3drm_img = IDirect3DRMTexture2_GetImage(texture2);
+    todo_wine ok(d3drm_img != NULL, "Could not get a D3DRMIMAGE struct from texture.\n");
+    if (d3drm_img == NULL)
+        goto cleanup;
+    ok(bmp_info->bmiHeader.biWidth == d3drm_img->width, "Expected texture width == %d, got %d.\n", bmp_info->bmiHeader.biWidth, d3drm_img->width);
+    ok(bmp_info->bmiHeader.biHeight == d3drm_img->height, "Expected texture height == %d, got %d.\n", bmp_info->bmiHeader.biHeight, d3drm_img->height);
+    buffer2 = (unsigned char *)d3drm_img->buffer1;
+    check = test_bitmap_data(buffer2, buffer1, d3drm_img, bmp_info->bmiHeader.biBitCount, 2);
+    ok(check, "Buffer contents do not match that of bitmap.\n");
+
+cleanup:
+    UnmapViewOfFile(buffer);
+    CloseHandle(hmapping);
+    CloseHandle(hfile);
+    IDirect3DRMTexture2_Release(texture2);
+    IDirect3DRM2_Release(d3drm2);
+    IDirect3DRM_Release(d3drm1);
+    check = DeleteFileA(file);
+    ok(check, "Cannot delete image stored in %s (error = %x).\n", file, GetLastError());
+    DestroyWindow(window);
+}
+
+static void test_load_texture3(void)
+{
+    HRESULT hr;
+    IDirect3DRM *d3drm1 = NULL;
+    IDirect3DRM3 *d3drm3 = NULL;
+    IDirect3DRMTexture3 *texture3 = NULL;
+    D3DRMIMAGE *d3drm_img = NULL;
+    HWND window;
+    BITMAPFILEHEADER *bmp_header = NULL;
+    BITMAPINFO *bmp_info = NULL;
+    HANDLE hfile, hmapping = NULL;
+    BOOL check;
+    DWORD size;
+    char *file = NULL;
+    unsigned char *buffer = NULL, *buffer1 = NULL, *buffer2 = NULL;
+
+    window = CreateWindowA("static", "d3drm_test", WS_OVERLAPPEDWINDOW, 0, 0, 300, 200, 0, 0, 0, 0);
+    file = create_bitmap("24bpp.bmp", 100, 100, 2);
+
+    /* Test if image information matches that from the header by loading the bitmap seperately. */
+    hfile = CreateFileA(file, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
+    ok(file != INVALID_HANDLE_VALUE, "Failed to open file at %s, error %d\n", file,
+            GetLastError());
+    if (hfile == INVALID_HANDLE_VALUE)
+        goto cleanup;
+
+    size = GetFileSize(hfile, NULL);
+    ok(size != INVALID_FILE_SIZE, "Failed to get file size, error %d\n", GetLastError());
+    if (size == INVALID_FILE_SIZE)
+        goto cleanup;
+
+    hmapping = CreateFileMappingA(hfile, NULL, PAGE_READONLY, 0, 0, NULL);
+    ok(hmapping != NULL, "Cannot create file mapping (error = %x).\n", GetLastError());
+    if (hmapping == NULL)
+        goto cleanup;
+
+    buffer = MapViewOfFile(hmapping, FILE_MAP_READ, 0, 0, 0);
+    ok(buffer != NULL, "Cannot create map view of file (error = %x).\n", GetLastError());
+    if (buffer == NULL)
+        goto cleanup;
+
+
+    bmp_header = (BITMAPFILEHEADER *)buffer;
+    bmp_info = (BITMAPINFO *)((unsigned char *)buffer + sizeof(BITMAPFILEHEADER));
+    buffer1 = ((unsigned char *)buffer + bmp_header->bfOffBits);
+
+    hr = Direct3DRMCreate(&d3drm1);
+    ok(hr == D3DRM_OK, "Cannot get IDirect3DRM interface (hr = %x)\n", hr);
+
+    hr = IDirect3DRM_QueryInterface(d3drm1, &IID_IDirect3DRM3, (void **)&d3drm3);
+    ok(SUCCEEDED(hr), "Cannot get IDirect3DRM3 interface (hr = %x)\n", hr);
+    hr = IDirect3DRM3_LoadTexture(d3drm3, file, &texture3);
+    ok(SUCCEEDED(hr), "Failed to load texture (hr = %x).\n", hr);
+    d3drm_img = IDirect3DRMTexture3_GetImage(texture3);
+    todo_wine ok(d3drm_img != NULL, "Could not get a D3DRMIMAGE struct from texture.\n");
+    if (d3drm_img == NULL)
+        goto cleanup;
+    ok(bmp_info->bmiHeader.biWidth == d3drm_img->width, "Expected texture width == %d, got %d.\n", bmp_info->bmiHeader.biWidth, d3drm_img->width);
+    ok(bmp_info->bmiHeader.biHeight == d3drm_img->height, "Expected texture height == %d, got %d.\n", bmp_info->bmiHeader.biHeight, d3drm_img->height);
+    buffer2 = (unsigned char *)d3drm_img->buffer1;
+    check = test_bitmap_data(buffer2, buffer1, d3drm_img, bmp_info->bmiHeader.biBitCount, 3);
+    ok(check, "Buffer contents do not match that of bitmap.\n");
+
+cleanup:
+    UnmapViewOfFile(buffer);
+    CloseHandle(hmapping);
+    CloseHandle(hfile);
+    IDirect3DRMTexture3_Release(texture3);
+    IDirect3DRM3_Release(d3drm3);
+    IDirect3DRM_Release(d3drm1);
+    check = DeleteFileA(file);
+    ok(check, "Cannot delete image stored in %s (error = %x).\n", file, GetLastError());
+    DestroyWindow(window);
+}
+
 START_TEST(d3drm)
 {
     test_MeshBuilder();
@@ -3720,4 +4098,7 @@ START_TEST(d3drm)
     test_create_device_from_d3d1();
     test_create_device_from_d3d2();
     test_create_device_from_d3d3();
+    test_load_texture1();
+    test_load_texture2();
+    test_load_texture3();
 }
-- 
2.3.2 (Apple Git-55)




More information about the wine-patches mailing list