Graphics-grayscale Transformation

Source: Internet
Author: User
Tags bmp image fread

The gray-scale transformation in Photoshop enables R, G, and B three colors to be enhanced and then mixed in any proportion. The principle is similar to the following.
The black and white transformations of black and white images are called grayscale transformations, the color transformations of color images are also called grayscale transformations, and the order transformations in Photoshop

In the pixel matrix of a color image, each pixel is represented by three RGB colors in a certain proportion to form a color. For example, black makes RGB (0, 0 ), the pure red color is RGB (0, 1 ).... When processing a photo, sometimes the ambient light source is too dark, so that the RGB value is too small, it will make the image too dark to see, if the ambient light source is too light, and make the image white, after the gray scale conversion, the RGB value can be adjusted to a proper degree to make the photo beautiful.

The conversion principle is as follows:
First, a pixel of an image is extracted. In BMP format, a pixel consists of eight Red brightness values, eight green brightness values, and eight blue brightness values, as long as the function is transformed according to a certain degree

The brightness values of these three colors can be used for gray-scale conversion.

For example, linear transformation
A linear function can be used.
F (x, y) = A' + (B '-a')/(B-a) × (f (x, y)-)
F (x, y) represents a pixel.
[A, B] indicates the gray scale range of the original image, and [A', B '] indicates the gray scale range of the new image after transformation.
Using this linear function to transform the R, G, and B components respectively can enhance the monochrome, and then mix the output.
If B '-a'> B-A, the grayscale range of the image increases, that is, the contrast increases, and the image becomes clearer.
If B '-a' is less than B-A, the grayscale range of the image is reduced, that is, the contrast is reduced.

Piecewise linear transformation
Similar to linear transformation
However, a single R, G, or B component is transformed using a piecewise function.
For example
F (x, y) = A' + (B '-a')/(B-a) × (f (x, y)-a) When F (x, y) in the range of [a, B]
F (x, y) = 0 when f (x, y) <m
F (x, y) = 255 When F (x, y)> N
In this way, a transformation segment A and B are specified. Only the gray points in A and B are enhanced. The gray level <A's programming black,> B's becomes white.

Nonlinear transformation
You can develop non-linear functions to transform the gray scale as needed.
Logarithm Transformation
F (x, y) = a + ln [F (x, y) + 1]/(B x lnc) Here A, B, and C are parameters rather than gray ranges, the logarithm transformation is used to extend the low gray area.
Exponential transformation
F (x, y) = B ^ (C * [F (x, y)]-A]), a, B, c are also parameters, exponential transformation is used to compress low gray areas.

The following uses BCB to implement a grayscale transformation for a 256-color BMP image without compression

 

// ----------------------------------------- Bcb6 program ----------------------------
# Include <VCL. h>
# Pragma hdrstop
# Include <stdio. h>
# Include "unit1.h"
# Include "file1.h"

# Pragma pack (1) // ---- force the structure to be aligned with 1 byte
Struct bitmapfileheader _
{
Short type;
Int bfsize;
Short RE1, re2;
Int offbits;
};

Struct bitmapinfo _
{
Long size;
Long width, height;
Short planes, bitcount;
Long comp, sizeimg;
Long xpels, ypels;
Long used, important;
};

// ------------- Correct the data in the BMP color table to the BCB tcolor data.
Void switchcolor (Long & C)
{
Long Blue = C & 0x000000ff;
Long green = C & 0x0000ff00;
Long red = C & 0x00ff0000;
C = (blue <16) | green | (Red> 16 );
}

// --------- Linear grayscale transformation, RR, GG, and BB are the enhancement coefficients.
Void strengthen (Long & C, double RR, double GG, Double BB)
{
Unsigned char r = (C & 0x00ff0000)> 16;
Unsigned char G = (C & 0x0000ff00)> 8;
Unsigned char B = (C & 0x000000ff );

Unsigned char NR;
Unsigned char ng;
Unsigned char NB;

If (RR * r> 255)
Nr = 255;
Else
Nr = RR * R;

If (Gg * G> 255)
Ng = 255;
Else
Ng = Gg * g;

If (BB * B> 255)
NBPs = 255;
Else
NB = BB * B;

C = (NR <16) | (NG <8) | (NB );
}
Void XXX ()
{
File * f = fopen ("F: // fx3.bmp", "rb ");
If (F = NULL)/* determines whether the file is successfully opened */
{
Showmessage ("file open error ");
Return;
}

Fseek (F, 0, 0); // move to the beginning

// ---------- Read the BMP File Header
Bitmapfileheader _ * bmp h = new bitmapfileheader _();
If (fread (char *) bmp h, sizeof (bitmapfileheader _), 1, F) = NULL)
{
Showmessage ("file read error ");
Return;
}

// ----------- Read the BMP Header
Bitmapinfo _ * BMP I = new bitmapinfo _();
If (fread (char *) bmp I, sizeof (bitmapinfo _), 1, F) = NULL)
{
Showmessage ("File Read error2 ");
Return;
}

// -------------- Read the color table
Long * c = new long [bmp h-> offbits-sizeof (bitmapfileheader _)-sizeof (bitmapinfo _)];
Fread (char *) C, bmp h-> offbits-sizeof (bitmapfileheader _)-sizeof (bitmapinfo _), 1, F );

// ---------- Display some information
Form1-> edit1-> text = inttostr (bmp h-> bfsize );
Form1-> edit2-> text = inttostr (bmp I-> width );
Form1-> edit3-> text = inttostr (bmp I-> height );
Form1-> edit4-> text = inttostr (bmp I-> comp );
Form1-> edit5-> text = inttostr (bmp I-> used );
// ------------ Display image
Unsigned char * P = new unsigned char [4];
Int I = 0, j = 0, K = 0, WC = 0;
Tcolor * TC;
If (bmp I-> width % 4 = 0) // ----------- the BMP image is 4-byte aligned.
WC = bmp I-> width/4;
Else
WC = bmp I-> width/4 + 1;

For (I = 0; I <bmp I-> height; I ++)
{
For (j = 0; j <WC; j ++)
{
Fread (p, 4, 1, F );
For (k = 0; k <4; k ++)
{
Long x = C [p [k];
// -------- Color enhancement starts here
Strengthen (x, 1.1, 1.1, 1.5); // 1.1 1.1 and 1.5 are the enhancement coefficients of each RGB component.
// --------- Correct data
Switchcolor (x); // ---------- because the tcolor of BCB and the color table of BMP are reversed.
Form1-> canvas-> pixels [200 + J * 4 + k] [300-i] = x; // ------ 200 and 300 are located in the middle of the canvas.
}
}
}
Fclose (f );
};
For non-linear transformations or other special transformations, you only need to modify the Transformation Operation in the strengthen function.
After the actual operation, the conversion effect is not good ~~~

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.