C语言漫谈(二) 图像显示 Windows和Linux

时间:2022-02-14 17:16:28

  关于图像显示有很多库可以用,Windows下有GDI,GDI+,D3D等,Linux下有X Window和Wayland,此外还有OpenGL ,SDL等图形库以及各种GUI库。

  了解最原始的方式,对于加深理解依然是有帮助的。下面给Windows和Linux下显示位图的最简单例子:

1.Windows用GDI显示图像的例子:

 /*
* FileName: Image_Win.c
* Usage: tcc -luser32 -lgdi32 -run Image_Win.c
*/ #include <windows.h>
#include <stdlib.h>
//
typedef unsigned char byte; typedef struct {
int Width;
int Height;
byte *Data;
} Image; LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM w, LPARAM l)
{
HDC hdc;
RECT rect;
Image img;
BITMAPINFO bmi;
PAINTSTRUCT ps;
int iRowLength;
//
ZeroMemory(&bmi, sizeof(BITMAPINFO));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biPlanes = ;
bmi.bmiHeader.biBitCount = ;
bmi.bmiHeader.biCompression = BI_RGB;
//
switch (msg) {
case WM_DESTROY:
PostQuitMessage();
break;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect(hwnd,&rect);
img.Width = rect.right - rect.left;
iRowLength = ((img.Width* * + ) & ~) >> ;
img.Height = rect.bottom - rect.top;
img.Data = (byte*)malloc(iRowLength*img.Height);
for(int i=;i<iRowLength*img.Height;i++)
img.Data[i] = rand()%;
bmi.bmiHeader.biWidth = img.Width;
bmi.bmiHeader.biHeight = img.Height;
SetDIBitsToDevice(hdc, , , img.Width, img.Height,
, , , img.Height, img.Data, &bmi, DIB_RGB_COLORS);
break;
default:
return DefWindowProc(hwnd, msg, w, l);
}
return ;
} int main(int argc, char* argv[])
{
static TCHAR szAppName[] = TEXT("RandColor");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
int iCmdShow = ;
HINSTANCE hInstance = NULL;
//
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = ;
wndclass.cbWndExtra = ;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass)) {
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
return ;
}
hwnd = CreateWindow(szAppName, TEXT("Image"), WS_OVERLAPPEDWINDOW^WS_THICKFRAME,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
//The message loop.
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd); while (GetMessage(&msg, NULL, , )) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return ;
}

  C语言漫谈(二) 图像显示 Windows和Linux

2.Linux下用X Window显示图像的例子:

 /*
* FileName: Image_Linux.c
* Usage: tcc -lX11 -run Image_Linux.c
*/
#include <X11/Xlib.h>
#include <X11/Xutil.h> #include <stdlib.h> #define WIDTH 640
#define HEIGHT 480 int main(int argc, char **argv)
{
int win_b_color;
int win_w_color;
XEvent xev;
Window window;
Visual *visual;
XImage *ximage;
GC gc; char*buffer=(char*)malloc(WIDTH*HEIGHT**sizeof(char)); Display *display = XOpenDisplay(NULL);
win_b_color = BlackPixel(display, DefaultScreen(display));
win_w_color = BlackPixel(display, DefaultScreen(display));
window = XCreateSimpleWindow(display,DefaultRootWindow(display),, , WIDTH, HEIGHT, ,win_b_color, win_w_color);
visual = DefaultVisual(display, );
//XSelectInput(display, window, ExposureMask | KeyPressMask);
XMapWindow(display, window);
XFlush(display);
gc = XCreateGC(display, window, , NULL);
//XEvent event;
while () {
for (int i = ; i < WIDTH*HEIGHT*; i ++)
buffer[i] = rand()%;
ximage=XCreateImage(display, visual, ,ZPixmap, , buffer,WIDTH, HEIGHT, , );
XPutImage(display, window,gc, ximage, , , , ,WIDTH, HEIGHT);
}
return ;
}

C语言漫谈(二) 图像显示 Windows和Linux

  下一次将介绍矢量图方面的知识,主要以EMF,SVG和PostScript进行说明。