Rapid image pixel operations

Source: Internet
Author: User

Rapid image pixel operations

Provides two APIs: setdibpixelcolor and getdibpixelcolor,

Quickly set and retrieve DIB image pixels to replace the setpixel and getpixel provided by the system.

 

Setpixel and getpixel are DC-based operations, which are too slow.

 

// Imagesetpixelfast. cpp: defines the entry point of the console application.
// Cheungmine
# Include "stdafx. H"
# Include <atlstr. h>
# Include <atlimage. h>

// 24-bit color and 16-bit color conversion macro
// By cheungmine
# Define rgb888torgb565 (R, G, B) (Word (r) <8) & 0xf800) | (Word (g) <3) & 0x7e0) | (Word (B)> 3 ))))

# Define rgbtorgb565 (RGB) (Word) (RGB)> 3) & (0x1f) <11) | (Word) (RGB)> 10) & (0x3f) <5) | (Word) (RGB)> 19 )) & (0x1f ))))

# Define rgb888torgb555 (R, G, B) (Word (r) <7) & 0x7c00) | (Word (g) <2) & 0x3e0) | (Word (B)> 3 ))))

# Define rgbtorgb555 (RGB) (Word) (RGB)> 3) & (0x1f) <10) | (Word) (RGB)> 11) & (0x1f) <5) | (Word) (RGB)> 19 )) & (0x1f ))))

# Define rgb555torgb (rgb555) (DWORD) (byte) (rgb555)> 7) & 0xf8) | (Word) (byte) (rgb555)> 2) & 0xf8) <8) | (DWORD) (byte) (rgb555) <3) & 0xf8 )) <16 )))

# Define rgb565torgb (rgb565) (DWORD) (byte) (rgb565) & 0xf800)> 11) <3) | (word) (byte) (rgb565) & 0x07e0)> 5) <2) <8) | (DWORD) (byte) (rgb565) & 0x001f) <3) <16 )))

Typedef struct
{
Void * lpbits;
Int width;
Int height;
Int nbpp;
Int npitch;
Int row;
Int Col;
DWORD dwcolor;
} Dibitsdesc;

Void setdibpixelcolor (dibitsdesc * pdib)
{
If (pdib-> npitch> 0 ){
// Top-down Dib
If (pdib-> nbpp = 16 ){
Int at =-pdib-> npitch * (pdib-> row-pdib-> height + 1) + pdib-> Col * 2;
* (Word *) (& (byte *) pdib-> lpbits) [at]) = (Word) pdib-> dwcolor;
}
If (pdib-> nbpp = 24 ){
Int at =-pdib-> npitch * (pdib-> row-pdib-> height + 1) + pdib-> Col * 3;
Byte * GBR = & (byte *) pdib-> lpbits) [at];
* GBR ++ = getbvalue (pdib-> dwcolor );
* GBR ++ = getgvalue (pdib-> dwcolor );
* GBR = getrvalue (pdib-> dwcolor );
}
Else if (pdib-> nbpp = 32 ){
Int at =-pdib-> npitch * (pdib-> row-pdib-> height + 1) + pdib-> Col * 4;
Byte * gbra = & (byte *) pdib-> lpbits) [at];
* Gbra ++ = 0;
* Gbra ++ = getbvalue (pdib-> dwcolor );
* Gbra ++ = getgvalue (pdib-> dwcolor );
* Gbra = getrvalue (pdib-> dwcolor );
}
}
Else {
// Bottom-up Dib
If (pdib-> nbpp = 16 ){
Int at = pdib-> npitch * pdib-> row + pdib-> Col * 2;
* (Word *) (& (byte *) pdib-> lpbits) [at]) = (Word) pdib-> dwcolor;
}
If (pdib-> nbpp = 24 ){
Int at = pdib-> npitch * pdib-> row + pdib-> Col * 3;
Byte * GBR = & (byte *) pdib-> lpbits) [at];
* GBR ++ = getbvalue (pdib-> dwcolor );
* GBR ++ = getgvalue (pdib-> dwcolor );
* GBR = getrvalue (pdib-> dwcolor );
}
Else if (pdib-> nbpp = 32 ){
Int at = pdib-> npitch * pdib-> row + pdib-> Col * 4;
Byte * AgBr = & (byte *) pdib-> lpbits) [at];
* AgBr ++ = 0;
* AgBr ++ = getbvalue (pdib-> dwcolor );
* AgBr ++ = getgvalue (pdib-> dwcolor );
* AgBr = getrvalue (pdib-> dwcolor );
}
}
}

Void getdibpixelcolor (dibitsdesc * pdib)
{
If (pdib-> npitch> 0 ){
// Top-down Dib
If (pdib-> nbpp = 16 ){
Int at =-pdib-> npitch * (pdib-> row-pdib-> height + 1) + pdib-> Col * 2;
Pdib-> dwcolor = * (word *) (& (byte *) pdib-> lpbits) [at]);
}
If (pdib-> nbpp = 24 ){
Int at =-pdib-> npitch * (pdib-> row-pdib-> height + 1) + pdib-> Col * 3;
Byte * GBR = & (byte *) pdib-> lpbits) [at];
Pdib-> dwcolor = RGB (GBR [2], GBR [1], GBR [0]);
}
Else if (pdib-> nbpp = 32 ){
Int at =-pdib-> npitch * (pdib-> row-pdib-> height + 1) + pdib-> Col * 4;
Byte * AgBr = & (byte *) pdib-> lpbits) [at];
Pdib-> dwcolor = RGB (AgBr [2], AgBr [1], AgBr [0]);
}
}
Else {
// Bottom-up Dib
If (pdib-> nbpp = 16 ){
Int at = pdib-> npitch * pdib-> row + pdib-> Col * 2;
Pdib-> dwcolor = * (word *) (& (byte *) pdib-> lpbits) [at]);
}
If (pdib-> nbpp = 24 ){
Int at = pdib-> npitch * pdib-> row + pdib-> Col * 3;
Const byte * GBR = & (byte *) pdib-> lpbits) [at];
Pdib-> dwcolor = RGB (GBR [2], GBR [1], GBR [0]);
}
Else if (pdib-> nbpp = 32 ){
Int at = pdib-> npitch * pdib-> row + pdib-> Col * 4;
Const byte * AgBr = & (byte *) pdib-> lpbits) [at];
Pdib-> dwcolor = RGB (AgBr [2], AgBr [1], AgBr [0]);
}
}
}

Int main (INT argc, char * argv [])
{
Cimage IMG;

IMG. Load ("C: // greatwall.jpg ");

Dibitsdesc Dib;

DiB. width = IMG. getwidth ();
DiB. Height = IMG. getheight ();
DiB. nbpp = IMG. getbpp ();
DiB. npitch = IMG. getpitch ();
DiB. lpbits = IMG. getbits ();

If (IMG. isdibsection ())
{
For (INT r = 0; r <dib. height; r ++ ){
For (int c = 0; C <dib. width; C ++ ){
// Too slow to use: IMG. setpixel (C, R, RGB (0,255, 0 ));
DiB. Row = R;
DiB. Col = C;
DiB. dwcolor = RGB (0,255, 0 );
Setdibpixelcolor (& DIB );
}
}
}

IMG. Save ("C: // test2.bmp ");

Return 0;
}

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.