Multimedia Instruction (image binarization)

Source: Internet
Author: User

It is mainly used to learn multimedia commands, or I will not be so troublesome to use Win32 SDK programming.

Sure enough, we only need to learn image algorithms, or we recommend Matlab.

The pcmpgtb command is used here.

Format: pcmpgtb mm0, MM1;

Explanation: when the corresponding byte in mm0 is greater than the corresponding byte in MM1, the corresponding position of mm0 is set to 0xff; otherwise, the value is set to 0x00.

Of course, there are also pcmpgtw and so on.

For details, see here.

// This program is a simple framework that can be used to process images. // It is mainly used to learn the MMX/SSE related multimedia commands. # Include <windows. h> # include <gdiplus. h> # pragma comment (Lib, "gdiplus. lib ") using namespace gdiplus; typedef Union {argb color; struct {byte blue; byte green; byte red; byte Alpha ;};} pix; void asmgray2bw (bitmapdata * Data) {uint Height = data-> height; uint width = data-> width; pix * P = (pix *) Data-> scan0; uint n = height * width/2; // It is very strange that I have clearly defined the unsigned byte. I should use 0x80, but here I can only use 0 as the threshold value, which is equivalent to 128 of CPP. // 0x80808080 is used, the unsigned single byte is 128. It is clearly a signed-128, which is really strange. // You can only consider the MMX register as signed by default. Pix cp [] = {0 x percentile, 0x00000000}; _ ASM {push ESI; MoV ECx, N; MoV ESI, [p]; movq mm2, [Cp]; LP: movq MM1, mm2; movq mm0, [esi]; pcmpgtb MM1, mm0; movq [esi], MM1; add ESI, 8; Dec ECx; jnz LP; pop ESI; emms ;}} void cppgray2bw (bitmapdata * Data) {pix * P = (pix *) Data-> scan0; For (uint h = 0; H <data-> height; ++ h) {for (uint W = 0; W <data-> width; ++ W) {If (p-> Red> 128) {P-> Red = 0xff; P-> Blue = 0xff; P-> Green = 0xff;} else {P-> Red = 0; P-> Blue = 0; p-> Green = 0 ;}++ P ;}}void onpaint (HDC) {graphics (HDC); bitmap srcbitmap (L "lena.jpg", 0 ); bitmap * dstbitmap = srcbitmap. clone (0, 0, srcbitmap. getwidth (), srcbitmap. getheight (), srcbitmap. getpixelformat (); bitmapdata data; rect (0, 0, dstbitmap-> getwidth (), dstbitmap-> getheight (); dstbitmap-> lockbits (& rect, cursor | imagelockmodewrite, pixelformat32bppargb, & data); asmgray2bw (& data); // cppgray2bw (& data); dstbitmap-> unlockbits (& data); graphics. drawimage (& srcbitmap, 0, 0); graphics. drawimage (dstbitmap, 0); Delete dstbitmap;} lresult callback wndproc (hwnd, uint, wparam, lparam); int winapi winmain (hinstance, hinstance, pstr, int icmdshow) {hwnd; MSG; wndclass; gdiplusstartupinput; ulong_ptr gdiplustoken; // initialize GDI +. gdiplusstartup (& gdiplustoken, & gdiplusstartupinput, null); wndclass. style = cs_hredraw | cs_vredraw; wndclass. lpfnwndproc = wndproc; wndclass. cbclsextra = 0; wndclass. cbwndextra = 0; 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 = text ("dip"); registerclass (& wndclass); hwnd = createwindow (text ("dip"), // window class name text ("dip "), // window caption ws_overlappedwindow, // window style cw_usedefault, // initial X position cw_usedefault, // initial y position 600, // initial X size 400, // initial y size null, // parent window handle null, // Window menu handle hinstance, // program instance handle null); // creation parameters/* hwnd button = createwindow ("button ", "processing", ws_child, 200,300,100, 30, hwnd, null, hinstance, 0); */showwindow (hwnd, icmdshow); // showwindow (button, icmdshow ); updatewindow (hwnd); While (getmessage (& MSG, null, 0, 0) {translatemessage (& MSG); dispatchmessage (& MSG);} gdiplusshutdown (gdiplustoken ); return MSG. wparam;} lresult callback wndproc (hwnd, uint message, wparam, lparam) {HDC; paintstruct pS; Switch (Message) {// case wm_command: Case wm_paint: HDC = beginpaint (hwnd, & PS); onpaint (HDC); endpaint (hwnd, & PS); Return 0; Case wm_destroy: postquitmessage (0); Return 0; default: return defwindowproc (hwnd, message, wparam, lparam );}}

Effect:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.