Home > Software engineering >  How to let in the GDI setpixel efficiency?
How to let in the GDI setpixel efficiency?

Time:09-22

Made a program needs a replacement bitmap mechanism of one color to another color, use the disastrous setpixel and getpixel found efficiency, is there any way to improve efficiency?
Here is my part of the code
 for (int x=200; X & lt; 260; X++) 
{
For (int y=200; Y & lt; 294; Y++)
{
HDC=GetDC (hWnd);
COLORREF pixelColor=GetPixel (HDC, x, y);
If (pixelColor==RGB (190, 225, 245))
SetPixel (HDC, x, y, RGB (155125105));

}
}
ReleaseDC (NULL, HDC);

CodePudding user response:

Usually this kind of situation, the use of a picture, GetPixel/SetPixel into direct read and write memory, write again
Input to a diagram. Use D2D/d is much more simple, have to a PS, and the efficiency of basically the same

CodePudding user response:

GetDibBits will image data is loaded into the memory
Modify the data
SetDibBits write back to the picture

CodePudding user response:

refer to the original poster wangyangzjk response:
made a program needs a replacement bitmap mechanism of one color to another color, use the disastrous setpixel and getpixel found efficiency, is there any way to improve efficiency?
Here is my part of the code
For (int x=200; X & lt; 260; X++)
{
For (int y=200; Y & lt; 294; Y++)
{
HDC=GetDC (hWnd);
COLORREF pixelColor=GetPixel (HDC, x, y);
If (pixelColor==RGB (190, 225, 245))
SetPixel (HDC, x, y, RGB (155125105));

}
}
ReleaseDC (NULL, HDC);


GUI simulator used to do a machine when come across, you put all the Get/SetPixel logic memory DC processing, finally only draw map function called

So there will be a big boost efficiency

CodePudding user response:

Reference OpenCV source code fragments,

CodePudding user response:

 int ReadBmp (const char * szFileName, bih BITMAPINFOHEADER * and * * Buffer BYTE) 
{
The FILE * FILE;
WORD BFH [7];
Long dpixeladd;
If (NULL==(file=fopen (szFileName, "rb")))
{
return 0;
}
Printf (" % s \ n ", szFileName);
Fread (& amp; BFH, sizeof (WORD), 7, file);
If (BFH [0]!=(WORD) (((WORD), 'B') | (' M & lt; <8)))
{
The fclose (file);
return 0;
}
BITMAPINFOHEADER mbin;
Fread (& amp; Mbin, sizeof (BITMAPINFOHEADER), 1, file);
If (mbin biBitCount & lt; 24)
{
The fclose (file);
return 0;
}
Dpixeladd=mbin. BiBitCount/8;
Long LineByteWidth=mbin. BiWidth * (dpixeladd);
If ((LineByteWidth % 4)!=0)
LineByteWidth +=4 - (LineByteWidth % 4);
BYTE * mBuffer;
If ((mBuffer=(BYTE *) malloc (sizeof (BYTE) * * mbin LineByteWidth biHeight))!=NULL)
{
* bih=mbin;
Fread (mBuffer, LineByteWidth * mbin. BiHeight, 1, the file).
* Buffer=mBuffer;
The fclose (file);
return 1;
}
The fclose (file);
return 0;
}


Int GetDIBColor (int X, int Y, BITMAPINFOHEADER bih, BYTE * Buffer, BYTE * r, g BYTE * and BYTE * b)
{
Int dpixeladd;
BYTE * PTR.
If (X & lt; 0 | | X & gt;=bih. BiWidth | | Y & lt; 0 | | Y & gt;=bih. BiHeight)
{
return 0;
}
Dpixeladd=bih. BiBitCount/8;
Long LineByteWidth=bih. BiWidth * (dpixeladd);
PTR=Buffer dpixeladd + + X * (bih. BiHeight - 1 - Y) * LineByteWidth;
* b=* PTR.
* g=* (PTR + 1);
* r=* (PTR + 2);
return 1;
}

Int GetDIBColor (int X, int Y, BITMAPINFOHEADER bih, BYTE * Buffer, BYTE * RGB, int type)
{
Int dpixeladd;
BYTE * PTR.
If (X & lt; 0 | | X & gt;=bih. BiWidth | | Y & lt; 0 | | Y & gt;=bih. BiHeight)
{
return 0;
}
Dpixeladd=bih. BiBitCount/8;
Long LineByteWidth=bih. BiWidth * (dpixeladd);
PTR=Buffer dpixeladd + + X * (bih. BiHeight - 1 - Y) * LineByteWidth;
The switch (type)
{
Case 0: * RGB=* PTR;; break;
Case 1: * RGB=* (PTR + 1);; break;
Case 2: * RGB=* (PTR + 2); the; break;
Default:
break;
}
return 1;
}

Int SetDIBColor (int X, int Y, BITMAPINFOHEADER bih, BYTE * Buffer, BYTE r, BYTE g, BYTE b)
{
Int dpixeladd;
BYTE * PTR.
If (X & lt; 0 | | X & gt;=bih. BiWidth | | Y & lt; 0 | | Y & gt;=bih. BiHeight)
{
return 0;
}
Dpixeladd=bih. BiBitCount/8;
Long LineByteWidth=bih. BiWidth * (dpixeladd);
PTR=Buffer dpixeladd + + X * (bih. BiHeight - 1 - Y) * LineByteWidth;
* PTR=r;
* (PTR + 1)=g;
* (PTR + 2)=b;
return 1;
}

Int SetDIBColor_RGB (int X, int Y, BITMAPINFOHEADER bih, BYTE * Buffer, BYTE RGB, int type)
{
Int dpixeladd;
BYTE * PTR.
If (X & lt; 0 | | X & gt;=bih. BiWidth | | Y & lt; 0 | | Y & gt;=bih. BiHeight)
{
return 0;
}
Dpixeladd=bih. BiBitCount/8;
Long LineByteWidth=bih. BiWidth * (dpixeladd);
PTR=Buffer dpixeladd + + X * (bih. BiHeight - 1 - Y) * LineByteWidth;
The switch (type)
{
Case 0: * PTR=RGB; break;
Case 1: * (PTR + 1)=RGB. break;
Case 2: * (PTR + 2)=RGB; break;
Default:
break;
}
return 1;
}

//to create bitmap file
Int SaveBitmap (const char * bmpPath, BYTE * pBuffer, int lWidth, int lHeight, int nByte)
{
//generated BMP file
HANDLE hf=CreateFileA (bmpPath GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, NULL, NULL);
If (hf==INVALID_HANDLE_VALUE)
return 0;
//write file header
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related