The CAPI project is now available on GitHub for image processing. This library has a simple API, is small in size, and has great compatibility. Speed improvements are currently being worked on. Tested and works on Windows and Linux. This library currently supports the following image formats:
- BMP (bitmap)
- JPG (jpeg)
- PNG (Portable Network Graphics)
- ICO (icon)
In my following examples i will be using C++ in Visual Studio on Windows.
To get started first we need some simple routines for loading and saving files. I have created the functions LoadFile and SaveFile for this purpose. The following is an example console program to convert a .ico formatted file to a .png formatted file.
The CAPI functions we will be using are:
- capi_LoadImageFromMemory
- This function detects the image format and loads the image into a simple format to work with.
- capi_Create_PNG_ImageToMemory
- This function creates a PNG formatted image from the image we loaded.
#include <Windows.h>
#include <CAPI.h>
#ifdef UNICODE
#define app_fopen _wfopen_s
#define app_printf wprintf
#else
#define app_fopen fopen_s
#define app_printf printf
#endif // UNICODE
void* LoadFile(const STRING* FilePath, U64* pFileSize)
{
FILE* Stream;
size_t BufferLength;
void* pThisFile;
void* pNewBlock;
if ((FilePath == 0) || (pFileSize == 0)) return 0;
*pFileSize = 0;
if (app_fopen(&Stream, FilePath, STR("rb")) != 0) return 0;
if (Stream == 0) return 0;
pThisFile = 0;
BufferLength = 0;
do
{
BufferLength += 0x1000;
pNewBlock = capi_realloc(pThisFile, BufferLength);
if (pNewBlock == 0)
{
if (pThisFile != 0) capi_free(pThisFile);
fclose(Stream);
return 0;
}
pThisFile = pNewBlock;
*pFileSize += fread(&((U8*)pThisFile)[*pFileSize], 1, 0x1000, Stream);
} while (!(feof(Stream)));
fclose(Stream);
return pThisFile;
}
I32 SaveFile(const STRING* FilePath, void* pFilePointer, U64 FileSize)
{
FILE* Stream;
size_t nBytesWrite;
I32 ErrorCode;
if ((FilePath == 0) || (pFilePointer == 0) || (FileSize == 0)) return CAPI_ERROR_INVALID_PARAMETER;
ErrorCode = CAPI_ERROR_NONE;
if (app_fopen(&Stream, FilePath, STR("w+b")) != 0) return CAPI_ERROR_ACCESS_DENIED;
if (Stream == 0) return CAPI_ERROR_ACCESS_DENIED;
nBytesWrite = fwrite(pFilePointer, 1, (size_t)FileSize, Stream);
if (nBytesWrite != FileSize)
{
ErrorCode = CAPI_ERROR_ACCESS_DENIED;
goto exit_func;
}
exit_func:
fclose(Stream);
return ErrorCode;
}
int main()
{
IMAGE exampleImage;
void* myLoadedFile;
I32 ErrorCode;
U64 FileSize;
PNG* myNewFile;
PNG_PARAMETERS png_params;
myLoadedFile = LoadFile(STR("C:/Users/Public/myTestImage.ico"), &FileSize);
if (myLoadedFile == 0)
{
app_printf(STR("LoadFile Failed."));
app_printf(STR("\n"));
while (true) { Sleep(100); }
}
ErrorCode = capi_LoadImageFromMemory(&exampleImage, 1, myLoadedFile, FileSize);
if (ErrorCode != CAPI_ERROR_NONE)
{
app_printf(STR("capi_LoadImageFromMemory Failed. ErrorCode: "));
app_printf(capi_ErrorCodeToString(ErrorCode));
app_printf(STR("\n"));
while (true) { Sleep(100); }
}
png_params.CompressionMethod = 0;
png_params.Level = Z_DEFAULT_COMPRESSION;
png_params.FilterMethod = 0;
png_params.InterlaceMethod = 0;
png_params.IDAT_Length = 0x20000;
ErrorCode = capi_Create_PNG_ImageToMemory(&myNewFile, &FileSize, &exampleImage, &png_params);
if (ErrorCode != CAPI_ERROR_NONE)
{
app_printf(STR("capi_Create_PNG_ImageToMemory Failed. ErrorCode: "));
app_printf(capi_ErrorCodeToString(ErrorCode));
app_printf(STR("\n"));
while (true) { Sleep(100); }
}
ErrorCode = SaveFile(STR("C:/Users/Public/myTestImage.png"), myNewFile, FileSize);
if (ErrorCode != CAPI_ERROR_NONE)
{
app_printf(STR("SaveFile Failed. ErrorCode: "));
app_printf(capi_ErrorCodeToString(ErrorCode));
app_printf(STR("\n"));
while (true) { Sleep(100); }
}
app_printf(STR("Done!"));
while (true) { Sleep(100); }
}
In this next example i will be creating a simple window that has double buffering.
We will draw our test image to the window in our Window_Paint_Handler function.
The CAPI functions we will be using are:
- capi_LoadImageFromMemory
- This function detects the image format and loads the image into a simple format to work with.
- capi_FillImage
- This function fills an image with the desired color. In our case we are filling the frame buffer.
- capi_DrawImageA
- This function draws an image onto another image. In our case we are drawing our test image to the frame buffer.
#include <Windows.h>
#include <CAPI.h>
#define WinClassName STR("ExampleProgram")
#define WinTitle STR("Example Program")
#ifdef UNICODE
#define app_fopen _wfopen_s
#define app_printf wprintf
#define ApplicationEntryPoint wWinMain
#else
#define app_fopen fopen_s
#define app_printf printf
#define ApplicationEntryPoint WinMain
#endif // UNICODE
static HWND Window_hWnd;
static HDC BufferHDC;
static BITMAPINFO* pDisplayBitmap;
static HBITMAP hBitmap;
static int ClientWidth;
static int ClientHeight;
static IMAGE FrameBuffer;
static IMAGE exampleImage;
void* LoadFile(const STRING* FilePath, U64* pFileSize)
{
FILE* Stream;
size_t BufferLength;
void* pThisFile;
void* pNewBlock;
if ((FilePath == 0) || (pFileSize == 0)) return 0;
*pFileSize = 0;
if (app_fopen(&Stream, FilePath, STR("rb")) != 0) return 0;
if (Stream == 0) return 0;
pThisFile = 0;
BufferLength = 0;
do
{
BufferLength += 0x1000;
pNewBlock = capi_realloc(pThisFile, BufferLength);
if (pNewBlock == 0)
{
if (pThisFile != 0) capi_free(pThisFile);
fclose(Stream);
return 0;
}
pThisFile = pNewBlock;
*pFileSize += fread(&((U8*)pThisFile)[*pFileSize], 1, 0x1000, Stream);
} while (!(feof(Stream)));
fclose(Stream);
return pThisFile;
}
I32 SaveFile(const STRING* FilePath, void* pFilePointer, U64 FileSize)
{
FILE* Stream;
size_t nBytesWrite;
I32 ErrorCode;
if ((FilePath == 0) || (pFilePointer == 0) || (FileSize == 0)) return CAPI_ERROR_INVALID_PARAMETER;
ErrorCode = CAPI_ERROR_NONE;
if (app_fopen(&Stream, FilePath, STR("w+b")) != 0) return CAPI_ERROR_ACCESS_DENIED;
if (Stream == 0) return CAPI_ERROR_ACCESS_DENIED;
nBytesWrite = fwrite(pFilePointer, 1, (size_t)FileSize, Stream);
if (nBytesWrite != FileSize)
{
ErrorCode = CAPI_ERROR_ACCESS_DENIED;
goto exit_func;
}
exit_func:
fclose(Stream);
return ErrorCode;
}
void FreeSysInternal()
{
if (BufferHDC != 0)
{
DeleteDC(BufferHDC);
BufferHDC = 0;
}
if (pDisplayBitmap != 0)
{
capi_free(pDisplayBitmap);
pDisplayBitmap = 0;
}
if (hBitmap != 0)
{
DeleteObject(hBitmap);
hBitmap = 0;
}
}
U32 SetupWindowFrameBuffer(HDC WindowHDC, RECT* ClientRt)
{
FreeSysInternal();
pDisplayBitmap = (BITMAPINFO*)capi_malloc(sizeof(BITMAPINFOHEADER));
if (pDisplayBitmap == 0) return 1;
pDisplayBitmap->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pDisplayBitmap->bmiHeader.biWidth = ClientRt->right;
pDisplayBitmap->bmiHeader.biHeight = -ClientRt->bottom;
pDisplayBitmap->bmiHeader.biPlanes = 1;
pDisplayBitmap->bmiHeader.biBitCount = 32;
pDisplayBitmap->bmiHeader.biCompression = 0;
pDisplayBitmap->bmiHeader.biSizeImage = 0;
pDisplayBitmap->bmiHeader.biXPelsPerMeter = 0;
pDisplayBitmap->bmiHeader.biYPelsPerMeter = 0;
pDisplayBitmap->bmiHeader.biClrUsed = 0;
pDisplayBitmap->bmiHeader.biClrImportant = 0;
BufferHDC = CreateCompatibleDC(WindowHDC);
if (BufferHDC == 0)
{
capi_free(pDisplayBitmap);
return 2;
}
hBitmap = CreateDIBSection(BufferHDC, pDisplayBitmap, 0,
(void**)&FrameBuffer.Pixels, 0, 0);
if (hBitmap == 0)
{
DeleteDC(BufferHDC);
capi_free(pDisplayBitmap);
return 3;
}
GdiFlush();
FrameBuffer.ScanLine = ClientRt->right;
FrameBuffer.Width = ClientRt->right;
FrameBuffer.Height = ClientRt->bottom;
SelectObject(BufferHDC, hBitmap);
return 0;
}
void Window_Paint_Handler(IMAGE* pDisplay)
{
// Fill the background of our window.
capi_FillImage(pDisplay, COLOR(195, 195, 195, 255));
// Now we draw our test image.
// Note: The last parameter (Alpha) must be 255 for the image to be displayed normally.
capi_DrawImageA(pDisplay, &exampleImage, 0, 0, 255);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HDC WinDC;
PAINTSTRUCT ps;
RECT WinArea;
RECT ClientArea;
int BorderWidth;
int BorderHeight;
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_ERASEBKGND:
return TRUE;
case WM_PAINT:
{
WinDC = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &ClientArea);
if ((ClientArea.right == 0) || (ClientArea.bottom == 0)) goto exit_paint_message;
if ((ClientArea.right != FrameBuffer.Width) || (ClientArea.bottom != FrameBuffer.Height))
{
if (SetupWindowFrameBuffer(WinDC, &ClientArea) != 0) goto exit_paint_message;
}
Window_Paint_Handler(&FrameBuffer);
SetDIBits(BufferHDC, hBitmap, 0,
pDisplayBitmap->bmiHeader.biHeight, FrameBuffer.Pixels, pDisplayBitmap, 0);
BitBlt(WinDC, 0, 0, ClientArea.right, ClientArea.bottom, BufferHDC, 0, 0, 0x00CC0020);
exit_paint_message:
EndPaint(hWnd, &ps);
break;
}
case WM_CREATE:
{
Window_hWnd = hWnd;
GetWindowRect(hWnd, &WinArea);
GetClientRect(hWnd, &ClientArea);
BorderWidth = WinArea.right - ClientArea.right;
BorderHeight = WinArea.bottom - ClientArea.bottom;
SetWindowPos(hWnd, NULL,
0, 0,
BorderWidth + ClientWidth, BorderHeight + ClientHeight, SWP_NOMOVE | SWP_NOZORDER);
GetWindowRect(hWnd, &WinArea);
SetWindowPos(hWnd, NULL,
(GetSystemMetrics(SM_CXSCREEN) - (WinArea.right - WinArea.left)) / 2,
(GetSystemMetrics(SM_CYSCREEN) - (WinArea.bottom - WinArea.top)) / 2,
0, 0, SWP_NOSIZE | SWP_NOZORDER);
break;
}
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
int WINAPI ApplicationEntryPoint(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ STRING* pCmdLine, _In_ int nCmdShow)
{
MSG msg;
WNDCLASSEX wcex;
void* myLoadedFile;
U64 FileSize;
I32 ErrorCode;
// Load our image to draw to the window frame buffer.
myLoadedFile = LoadFile(STR("C:/Users/Public/myTestImage.ico"), &FileSize);
if (myLoadedFile == 0)
{
MessageBox(0, STR("LoadFile Failed."), STR("Error!"), MB_OK);
return 0;
}
ErrorCode = capi_LoadImageFromMemory(&exampleImage, 1, myLoadedFile, FileSize);
if (ErrorCode != CAPI_ERROR_NONE)
{
MessageBox(0, STR("capi_LoadImageFromMemory Failed."), STR("Error!"), MB_OK);
return 0;
}
// The client area of our window will be the same size as our test image.
ClientWidth = exampleImage.Width;
ClientHeight = exampleImage.Height;
// Create the display window and enter the thread/window message loop.
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = WinClassName;
wcex.hIconSm = NULL;
if (!RegisterClassEx(&wcex))
{
MessageBox(0, STR("Failed to register the window class."), STR("Error!"), MB_OK);
return 0;
}
// Note: The 700 (Width) and 500 (Height) values are just dummy values. The Width and Height get set in the WM_CREATE message handler.
CreateWindowEx(0, WinClassName, WinTitle,
WS_VISIBLE | WS_CLIPCHILDREN | WS_BORDER | WS_MINIMIZEBOX | WS_SYSMENU | WS_SIZEBOX | WS_MAXIMIZEBOX,
0, 0, 700, 500, NULL, NULL, hInstance, NULL);
if (!Window_hWnd)
{
MessageBox(0, STR("Failed to create the window."), STR("Error!"), MB_OK);
return 0;
}
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
FreeSysInternal();
return 0;
}