From de66b62e0eb11b2429c86d04d53bc117bf24a945 Mon Sep 17 00:00:00 2001 From: x1nixmzeng Date: Sat, 7 Jan 2017 20:16:12 +0000 Subject: [PATCH] Hooked up stbi to decode images for the app GUI --- src/Cxbx/WndAbout.cpp | 25 ++++++++++++++++++++----- src/Cxbx/WndMain.cpp | 32 ++++++++++++++++++++++---------- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/Cxbx/WndAbout.cpp b/src/Cxbx/WndAbout.cpp index 9d25085cc..330d090f4 100644 --- a/src/Cxbx/WndAbout.cpp +++ b/src/Cxbx/WndAbout.cpp @@ -36,6 +36,8 @@ #include "WndAbout.h" #include "ResCxbx.h" +#include "stb_image.h" + #include WndAbout::WndAbout(HINSTANCE x_hInstance, HWND x_parent) : Wnd(x_hInstance) @@ -101,13 +103,25 @@ LRESULT CALLBACK WndAbout::WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM l { HRSRC hSrc = FindResource(NULL, MAKEINTRESOURCE(IDR_JPEG_ABOUT), "JPEG"); HGLOBAL hRes = LoadResource(NULL, hSrc); - uint08 *jpgData = (uint08*)LockResource(hRes); + uint08 *jpgData = (uint08*)LockResource(hRes); uint32 jpgFileSize = SizeofResource(NULL, hSrc); uint32 bmpFileSize = 0; - uint32 bmpWidth, bmpHeight; + uint32 bmpWidth = 0; + uint32 bmpHeight = 0; - uint08 *bmpBuff = nullptr; // jpeg2bmp(jpgData, jpgFileSize, &bmpFileSize, &bmpWidth, &bmpHeight); + uint08 *bmpBuff = reinterpret_cast + ( + stbi_load_from_memory + ( + reinterpret_cast(jpgData), + static_cast(jpgFileSize), + reinterpret_cast(&bmpWidth), + reinterpret_cast(&bmpHeight), + nullptr, + STBI_rgb + ) + ); // create bitmap { @@ -115,7 +129,7 @@ LRESULT CALLBACK WndAbout::WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM l BmpInfo.bmiHeader.biSize = sizeof(BITMAPINFO) - sizeof(RGBQUAD); BmpInfo.bmiHeader.biWidth = bmpWidth; - BmpInfo.bmiHeader.biHeight = 0 - (int)bmpHeight; + BmpInfo.bmiHeader.biHeight = 0 - (long)bmpHeight; BmpInfo.bmiHeader.biPlanes = 1; BmpInfo.bmiHeader.biBitCount = 24; BmpInfo.bmiHeader.biCompression = BI_RGB; @@ -128,8 +142,9 @@ LRESULT CALLBACK WndAbout::WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM l SetDIBits(hDC, m_BackBmp, 0, bmpHeight, bmpBuff, &BmpInfo, DIB_RGB_COLORS); } - free(bmpBuff); + stbi_image_free(bmpBuff); + FreeResource(hRes); UnlockResource(hRes); } diff --git a/src/Cxbx/WndMain.cpp b/src/Cxbx/WndMain.cpp index 0c264f803..1811fd5f9 100644 --- a/src/Cxbx/WndMain.cpp +++ b/src/Cxbx/WndMain.cpp @@ -42,11 +42,10 @@ #include -/** - * Silly little hack to fix link error with libjpeg on MSVC 2015 - */ -FILE _iob[] = { *stdin, *stdout, *stderr }; -extern "C" FILE * __cdecl __iob_func(void) { return _iob; } +#define STBI_ONLY_JPEG +#define STBI_NO_LINEAR +#define STB_IMAGE_IMPLEMENTATION +#include "stb_image.h" WndMain::WndMain(HINSTANCE x_hInstance) : Wnd(x_hInstance), @@ -226,13 +225,25 @@ LRESULT CALLBACK WndMain::WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lP { HRSRC hSrc = FindResource(NULL, MAKEINTRESOURCE(IDR_JPEG_SPLASH), "JPEG"); HGLOBAL hRes = LoadResource(NULL, hSrc); - uint08 *jpgData = (uint08*)LockResource(hRes); + uint08 *jpgData = (uint08*)LockResource(hRes); uint32 jpgFileSize = SizeofResource(NULL, hSrc); uint32 bmpFileSize = 0; - uint32 bmpWidth, bmpHeight; + uint32 bmpWidth = 0; + uint32 bmpHeight = 0; - uint08 *bmpBuff = nullptr;// jpeg2bmp(jpgData, jpgFileSize, &bmpFileSize, &bmpWidth, &bmpHeight); + uint08 *bmpBuff = reinterpret_cast + ( + stbi_load_from_memory + ( + reinterpret_cast(jpgData), + static_cast(jpgFileSize), + reinterpret_cast(&bmpWidth), + reinterpret_cast(&bmpHeight), + nullptr, + STBI_rgb + ) + ); // create bitmap { @@ -240,7 +251,7 @@ LRESULT CALLBACK WndMain::WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lP BmpInfo.bmiHeader.biSize = sizeof(BITMAPINFO) - sizeof(RGBQUAD); BmpInfo.bmiHeader.biWidth = bmpWidth; - BmpInfo.bmiHeader.biHeight = 0 - (int)bmpHeight; + BmpInfo.bmiHeader.biHeight = 0 - (long)bmpHeight; BmpInfo.bmiHeader.biPlanes = 1; BmpInfo.bmiHeader.biBitCount = 24; BmpInfo.bmiHeader.biCompression = BI_RGB; @@ -253,8 +264,9 @@ LRESULT CALLBACK WndMain::WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lP SetDIBits(hDC, m_BackBmp, 0, bmpHeight, bmpBuff, &BmpInfo, DIB_RGB_COLORS); } - free(bmpBuff); + stbi_image_free(bmpBuff); + FreeResource(hRes); UnlockResource(hRes); }