Adjust the brightness/contrast of Photoshop images

Source: Internet
Author: User

The following describes how to adjust the brightness and contrast of a Photoshop image:

1. Contrast algorithm formula.

Photoshop processes contrast increments based on the positive and negative values of the given values.

If newrgb is used to represent the new R, G, and B Components of image pixels, RGB is used to represent the R, G, and B Components of image pixels, threshold is the given threshold value, and contrast is the contrast increment, when contrast is greater than 0:

1) newrgb = RGB + (RGB-threshold) * (1/(1-contrast/255)-1)

Where, when contrast is equal to 255 (RGB-threshold) * (1/(1-contrast/255)-1) is infinite (± ), since the maximum and minimum values of RGB are 255 and 0, you can only determine newrgb by threshold, that is, newrgb = RGB> = threshold? 255: 0, which is actually set the image threshold. The image consists of up to eight colors: red, yellow, green, green, blue, purple, black, and white, there are only a maximum of eight lines in the grayscale image.

When contrast is less than 0:

2) newrgb = RGB + (RGB-threshold) * contrast/255

When contrast is equal to-255, each component of the image RGB is equal to the threshold value, and the image is completely gray. There is only one line on the grayscale image, that is, the threshold gray level.

Ii. Adjust the image brightness. This article uses the most commonly used nonlinear brightness adjustment (this is also the brightness adjustment method for versions earlier than phoposhop CS3 and later versions also retain the options for this brightness adjustment method ).

3. image brightness/contrast comprehensive adjustment algorithm. This is very simple. When the brightness and contrast are adjusted at the same time, if the contrast is greater than 0, the brightness is adjusted, and then the contrast is adjusted. If the contrast is smaller than 0, the contrast is adjusted first, then adjust the brightness.

The following code uses bcb2007 and GDI + bitmap data to adjust the brightness and contrast of Photoshop images, including the example code:

 

//---------------------------------------------------------------------------

// Define the argb pixel structure
Typedef Union
{
Argb color;
Struct
{
Byte blue;
Byte green;
Byte red;
Byte Alpha;
};
} Argbquad, * pargbquad;
//---------------------------------------------------------------------------

 

//---------------------------------------------------------------------------

Forceinline
Int checkvalue (INT value)
{
Return value <= 0? 0: Value >=255? 255: value;
}
//---------------------------------------------------------------------------

Void brightandcontrast (bitmapdata * data, int bright, int contrast, byte threshold)
{
Float CV = contrast <=- 255? -1.0f: contrast/255.0f;
If (contrast> 0 & contrast <255)
CV = 1.0f/(1.0f-cv)-1.0f;

Byte values [256];
For (INT I = 0; I <256; I ++)
{
Int v = contrast> 0? Checkvalue (I + bright): I;
If (contrast> = 255)
V = V> = threshold? 255: 0;
Else
V = checkvalue (V + (INT) (V-threshold) * CV + 0.5f ));
Values [I] = contrast <0? Checkvalue (V + bright): V;
}

Pargbquad P = (pargbquad) Data-> scan0;
Int offset = data-> stride-data-> width * sizeof (argbquad );

For (uint y = 0; y <data-> height; y ++, (byte *) P + = offset)
{
For (uint x = 0; x <data-> width; X ++, P ++)
{
P-> Blue = values [p-> Blue];
P-> Green = values [p-> Green];
P-> Red = values [p-> Red];
}
}
}
//---------------------------------------------------------------------------

// Lock the Scan Line of the GDI + bitmap to data
Forceinline
Void lockbitmap (gdiplus: bitmap * BMP, bitmapdata * Data)
{
Gdiplus: rect R (0, 0, BMP-> getwidth (), BMP-> getheight ());
BMP-> lockbits (& R, imagelockmoderead | imagelockmodewrite,
Pixelformat32bppargb, data );
}
//---------------------------------------------------------------------------

// GDI + bitmap Scan Line unlock
Forceinline
Void unlockbitmap (gdiplus: bitmap * BMP, bitmapdata * Data)
{
BMP-> unlockbits (data );
}
//---------------------------------------------------------------------------

Void _ fastcall tform1: button3click (tobject * sender)
{
Gdiplus: bitmap * BMP = new gdiplus: Bitmap (l "d: \ source.jpg ");
Gdiplus: Graphics * g = new gdiplus: Graphics (canvas-> handle );
G-> drawimage (BMP, 0, 0 );

Bitmapdata data;
Lockbitmap (BMP, & data );
Brightandcontrast (& Data, 0,100,121 );
Unlockbitmap (BMP, & data );
G-> drawimage (BMP, Data. Width, 0 );

Delete g;
Delete BMP;
}
//---------------------------------------------------------------------------

In the brightness/contrast adjustment function brightandcontrast, A 256-element search table is created based on the principles described above, then, the adjusted data is obtained for the image data pixel by R, G, and B component values in the search table, so the processing speed is quite fast.

The level is limited and errors are inevitable. You are welcome to correct and provide guidance. Email Address: maozefa@hotmail.com

 

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.