Add a color filter to grayscale images using objective a C ++ 6.0 to create a "retro image"

Source: Internet
Author: User
2 principle of Image Color Filter-pseudo-color Encoding

The name of a color filter added to an image in the digital image processing field is pseudo-color encoding. It is implemented by assigning a gray or black/white image to a special color palette. We know that for Grayscale Images, the RGB values are equal, that is, for any pixel in the image, the red component value = green component value = blue component value.

The general grayscale chart of level 1 is:

{// Regular grayscale Encoding

{0, 0, 0}, {1, 1, 1}, {2, 2}, {3, 3}, // 4

{4, 4, 4}, {5, 5, 5}, {6, 6, 6}, {7, 7}, // 8

{8, 8, 8}, {9, 9}, {10, 10, 10}, {11, 11, 11}, // 12

{12, 12, 12}, {13, 13}, {14, 14}, {15, 15, 15}, // 16

{16, 16, 16}, {17, 17, 17}, {18, 18, 18}, {19, 19, 19}, // 20

{20, 20, 20}, {21, 21, 21}, {22, 22, 22}, {23, 23, 23}, // 24

... ... ... ...

{228,228,228}, {229,229,229}, {230,230,230}, {231,231,231}, // 232

{232,232,232}, {233,233,233}, {234,234,234}, {235,235,235}, // 236

{236,236,236}, {237,237,237}, {238,238,238}, {239,239,239}, // 240

{240,240,240}, {241,241,241}, {242,242,242}, {243,243,243}, // 244

{244,244,244}, {245,245,245}, {246,246,246}, {247,247,247}, // 248

{248,248,248}, {249,249,249}, {250,250,250}, {251,251,251}, // 252

{252,252,252}, {253,253,253}, {254,254,254}, {255,255,255}, // 256

} // Rule: r = G = B = I, for I = 0 to 255

3 Programming

The following uses the 256-level grayscale image "angel.bmp" as an example to describe the main steps and key functions written with VC ++ 6.0. The program passes debugging in the Windows 2000 environment of the PC, and achieved good color effect.

(1) create a Main Interface

Create a single document project in the MFC Appwizard Wizard of VC. The base class of cfunnyview is cformview, And the menu attribute is modified.


Figure 1

The function that is executed when you click the "color encoding" menu on the main interface:

Void cfunnyglassview: onenhacolor ()

{

// Todo: add your command handler code here

// Pseudo-color Encoding

// Obtain the document

Cfunnyglassdoc * pdoc = getdocument ();

Int ncolor; // Save the user-selected pseudo-color-coded table Index

Lpstr lpdib; // pointer to Dib

Lpdib = (lpstr): globallock (hglobal) pdoc-> gethdib (); // lock Dib

// Determine whether it is a 8-bpp Bitmap (only processing the pseudo-color conversion of the 256-color bitmap, and so on)

Cdlgcolor dlgpara; // parameter dialog box

// Initialize the variable value

If (pdoc-> m_ncolorindex> = 0)

{

// Initially select the current pseudo color

Dlgpara. m_ncolor = pdoc-> m_ncolorindex;

}

Else

{

// The phased-out pseudo-color encoding table is selected.

Dlgpara. m_ncolor = 0;

}

// Pointer to the name Array

Dlgpara. m_lpcolorname = (lpstr) colorscalename;

// Number of pseudo-color codes

Dlgpara. m_ncolorcount = color_scale_count;

// Name String Length

Dlgpara. m_nnamelen = sizeof (colorscalename)/color_scale_count;

// Displayed dialog box, prompting you to set the translation volume

If (dlgpara. domodal ()! = Idok)

{

Return; // return

}

Ncolor = dlgpara. m_ncolor; // get user settings

Delete dlgpara; // Delete dialog box

Beginwaitcursor (); // modify the cursor shape

// Determine whether the pseudo-color encoding is changed

If (pdoc-> m_ncolorindex! = Ncolor)

{

// Call the replacecolorpal () function to change the color palette

: Replacecolorpal (lpdib, (byte *) colorstable [ncolor]);

Pdoc-> getdocpalette ()-> setpaletteentries (0,256, (lppaletteentry) colorstable [ncolor]);

// Replace the current document palette

Pdoc-> m_ncolorindex = ncolor; // update the class member variable

// Set the dirty flag pdoc-> setmodifiedflag (true );

Ondorealize (wparam) m_hwnd, 0); // implement a new palette

Pdoc-> updateallviews (null); // update the view

}

: Globalunlock (hglobal) pdoc-> gethdib (); // unlock

Endwaitcursor (); // restore the cursor

}

(2) create a dialog box

The "idc_color_list" dialog box is titled "select filter color:" and add a ListBox control and two command buttons "OK" and "cancel ".


2

Main functions processed in the idc_color_list dialog box:

I. Initialization dialog box

Bool cdlgcolor: oninitdialog ()

{

Cdialog: oninitdialog ();

Int I;

// Add pseudo-color Encoding

For (I = 0; I <m_ncolorcount; I ++)

{

M_lstcolor.addstring (m_lpcolorname + I * m_nnamelen );

}

// Select the initial encoding table

M_lstcolor.setcursel (m_ncolor );

Return true; // return true unless you set the focus to a control

// Exception: OCX property pages shold return false

}

Ii. When the user determines the selected code table, execute the following functions:

Void cdlgcolor: onok ()

{

// Todo: add extra validation here

// Click OK.

M_ncolor = m_lstcolor.getcursel ();

Cdialog: onok ();

}

(3) pseudo-color code table File

// Number of encoded tables

# Define color_scale_count 6

// Encode the table name

Const char colorscalename [color_scale_count] [64] =

{

"1 conventional grayscale encoding ",

"2 inverse grayscale encoding ",

"3 red saturation encoding ",

"4 Green saturation encoding ",

"5 blue saturation encoding ",

"6 Yellow saturation encoding ",

"7 cyan saturation encoding ",

"8 purple saturation encoding ",

};

// Encode the RGB array of the table

Const byte colorstable [color_scale_count] [1, 256] [4] =

{

{// Red saturation code

{0, 0, 0}, {1, 0, 0}, {2, 0, 0}, {3, 0, 0}, // 4

{4, 0, 0}, {5, 0, 0}, {6, 0, 0}, {7, 0, 0}, // 8

{8, 0, 0}, {9, 0, 0}, {10, 0, 0}, {11, 0, 0}, // 12

{12, 0, 0}, {13, 0, 0}, {14, 0, 0}, {15, 0, 0}, // 16

{16, 0, 0}, {17, 0, 0}, {18, 0, 0}, {19, 0, 0}, // 20

... ... ... ...

{224, 0, 0}, {225, 0, 0}, {226, 0, 0}, {227, 0, 0}, // 228

{228, 0, 0}, {229, 0, 0}, {230, 0, 0}, {231, 0, 0}, // 232

{232, 0, 0}, {233, 0, 0}, {234, 0, 0}, {235, 0, 0}, // 236

{236, 0, 0}, {237, 0, 0}, {238, 0, 0}, {239, 0, 0}, // 240

{240, 0, 0}, {241, 0, 0}, {242, 0, 0}, {243, 0, 0}, // 244

{244, 0, 0}, {245, 0, 0}, {246, 0, 0}, {247, 0, 0}, // 248

{248, 0, 0}, {249, 0, 0}, {250, 0, 0}, {251, 0, 0}, // 252

{252, 0, 0}, {253, 0, 0}, {254, 0, 0}, {255, 0, 0}, // 256

}, // Rule: r = I, G = B = 0; I = 0 to 255

{// Green saturation Encoding

{0, 0, 0}, {0, 1, 0}, {0, 2, 0}, {0, 3, 0}, // 4

{0, 4, 0}, {0, 5, 0}, {0, 6, 0}, {0, 7, 0}, // 8

{0, 8, 0}, {0, 9, 0}, {0, 10, 0}, {0, 11, 0}, // 12

{0, 12, 0}, {0, 13, 0}, {0, 14, 0}, {0, 15, 0}, // 16

{0, 16, 0}, {0, 17, 0}, {0, 18, 0}, {0, 19, 0}, // 20

{0, 20, 0}, {0, 21, 0}, {0, 22, 0}, {0, 23, 0}, // 24

... ... ... ...

{0,224, 0}, {0,225, 0}, {0,226, 0}, {0,227, 0}, // 228

{0,228, 0}, {0,229, 0}, {0,230, 0}, {0,231, 0}, // 232

{0,232, 0}, {0,233, 0}, {0,234, 0}, {0,235, 0}, // 236

{0,236, 0}, {0,237, 0}, {0,238, 0}, {0,239, 0}, // 240

{0,240, 0}, {0,241, 0}, {0,242, 0}, {0,243, 0}, // 244

{0,244, 0}, {0,245, 0}, {0,246, 0}, {0,247, 0}, // 248

{0,248, 0}, {0,249, 0}, {0,250, 0}, {0,251, 0}, // 252

{0,252, 0}, {0,253, 0}, {0,254, 0}, {0,255, 0}, // 256

}, // Law: g = I, r = B = 0; for I = 0 to 255

{// Blue saturation Encoding

{0, 0, 0}, {0, 0, 1}, {0, 0, 2}, {0, 0, 3}, // 4

{0, 0, 4}, {0, 0, 5}, {0, 0, 6}, {0, 0, 7}, // 8

{0, 0, 8}, {0, 0, 9}, {0, 0, 10}, {0, 0, 11}, // 12

{0, 0, 12}, {0, 0, 13}, {0, 0, 14}, {0, 0, 15}, // 16

{0, 0, 16}, {0, 0, 17}, {0, 0, 18}, {0, 0, 19}, // 20

{0, 0, 20}, {0, 0, 21}, {0, 0, 22}, {0, 0, 23}, // 24

... ... ... ...

{0, 0,244}, {0, 0,245}, {0, 0,246}, {0, 0,247}, // 248

{0, 0,248}, {0, 0,249}, {0, 0,250}, {0, 0,251}, // 252

{0, 0,252}, {0, 0,253}, {0, 0,254}, {0, 0,255}, // 256

}, // Rule: B = I, r = G = 0; for I = 0 to 255

{// Yellow saturation code

{0, 0, 0}, {1, 1, 0}, {2, 2, 0}, {3, 3, 0}, // 4

{4, 4, 0}, {5, 5, 0}, {6, 6, 0}, {7, 7, 0}, // 8

{8, 8, 0}, {9, 9, 0}, {10, 10, 0}, {11, 11, 0}, // 12

{12, 12, 0}, {13, 13, 0}, {14, 14, 0}, {15, 15, 0}, // 16

{16, 16, 0}, {17, 17, 0}, {18, 18, 0}, {19, 19, 0}, // 20

{20, 20, 0}, {21, 21, 0}, {22, 22, 0}, {23, 23, 0}, // 24

... ... ... ...

{228,228, 0}, {229,229, 0}, {230,230, 0}, {231,231, 0}, // 232

{232,232, 0}, {233,233, 0}, {234,234, 0}, {235,235, 0}, // 236

{236,236, 0}, {237,237, 0}, {238,238, 0}, {239,239, 0}, // 240

{240,240, 0}, {241,241, 0}, {242,242, 0}, {243,243, 0}, // 244

{244,244, 0}, {245,245, 0}, {246,246, 0}, {247,247, 0}, // 248

{248,248, 0}, {249,249, 0}, {250,250, 0}, {251,251, 0}, // 252

{252,252, 0}, {253,253, 0}, {254,254, 0}, {255,255, 0}, // 256

}, // Rule: r = G = I, B = 0; for I = 0 to 255

{// Cyan saturation Encoding

{0, 0, 0}, {0, 1, 1}, {0, 2, 2}, {0, 3, 3}, // 4

{0, 4, 4}, {0, 5, 5}, {0, 6, 6}, {0, 7}, // 8

{0, 8, 8}, {0, 9}, {0, 10}, {0, 11}, // 12

{0, 12, 12}, {0, 13, 13}, {0, 14, 14}, {0, 15, 15}, // 16

{0, 16, 16}, {0, 17, 17}, {0, 18, 18}, {0, 19, 19}, // 20

{0, 20, 20}, {0, 21, 21}, {0, 22, 22}, {0, 23, 23}, // 24

{0, 24, 24}, {0, 25, 25}, {0, 26, 26}, {0, 27, 27}, // 28

... ... ... ...

{0,232,232}, {0,233,233}, {0,234,234}, {0,235,235}, // 236

{0,236,236}, {0,237,237}, {0,238,238}, {0,239,239}, // 240

{0,240,240}, {0,241,241}, {0,242,242}, {0,243,243}, // 244

{0,244,244}, {0,245,245}, {0,246,246}, {0,247,247}, // 248

{0,248,248}, {0,249,249}, {0,250,250}, {0,251,251}, // 252

{0,252,252}, {0,253,253}, {0,254,254}, {0,255,255}, // 256

}, // Rule: B = G = I, r = 0; for I = 0 to 255

{// Purple saturation Encoding

{0, 0, 0}, {1, 0, 1}, {2, 0, 2}, {3, 0, 3}, // 4

{4, 0, 4}, {5, 0, 5}, {6, 0, 6}, {7, 0, 7}, // 8

{8, 0, 8}, {9, 0, 9}, {10, 0, 10}, {11, 0, 11}, // 12

{12, 0, 12}, {13, 0, 13}, {14, 0, 14}, {15, 0, 15}, // 16

{16, 0, 16}, {17, 0, 17}, {18, 0, 18}, {19, 0, 19}, // 20

{20, 0, 20}, {21, 0, 21}, {22, 0, 22}, {23, 0, 23}, // 24

... ... ... ...

{228, 0,228}, {229, 0,229}, {230, 0,230}, {231, 0,231}, // 232

{232, 0,232}, {233, 0,233}, {234, 0,234}, {235, 0,235}, // 236

{236, 0,236}, {237, 0,237}, {238, 0,238}, {239, 0,239}, // 240

{240, 0,240}, {241, 0,241}, {242, 0,242}, {243, 0,243}, // 244

{244, 0,244}, {245, 0,245}, {246, 0,246}, {247, 0,247}, // 248

{248, 0,248}, {249, 0,249}, {250, 0,250}, {251, 0,251}, // 252

{252, 0,252}, {253, 0,253}, {254, 0,254}, {255, 0,255}, // 256

} // Rule: r = B = I, G = 0; for I = 0 to 255

// End the encoding table

};

Related Article

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.