Simple algorithm for converting RGB values ​​into grayscale values

xiaoxiao2021-03-06  18

Make the scene or wizard in the form of grayscale, which is often used in the normal game making, such as: When a role is used, it usually becomes gray, representing this round is no longer action. The part of the memories of the colorful butterfly in the "Xianjian" is to express the gray of the entire screen? (Remember is not clear, too long, ^ _ ^); there are many many examples of converting RGB values ​​to grayscale should be implemented in the program (at least I think so). In fact, this is very simple. The basic principle is to separate a point of the RGB value to sum, then divide the resulting value to the RGB separately, expressed as follows:

R = g = b = 0.3r 0.6 g 0.1b; // Second Edition corrected formula // The error formula in the first edition is R = g = b = (R G B) / 3;

In the actual programming application, it can be divided into three situations of 8, 16 bits, and 24th. Here, the first introduction: First, the first conversion of the simplest 24-bit point, 24-bit RGB is evenly distributed, so separate And the synthesis is simple, the code is as follows:

/ / ======= 24 bits conversion ============ int gmask = 0x00FF00; // Green mask int bmask = 0x0000FF; // blue mask

INT RGB24TOGRAY (Int Sour) {INT R, G, B, T; // Temporary variable

R = (SOUR >> 16); g = (source & gmask) >> 8; b = source & bmask; t = (r * 3 g * 6 b) / 10; // Second Edition corrected place

Return (t << 16) | (t << 8) | T;}

Second, 16-site conversion should be troublesome, because it involves 555 and 565 color code format, so we need some initial work before conversion:

/ / ======= 16 bits conversion ============ Byte rmove, gmove; // r and g move to the minimum number of steps required

// Initialization data, this function is executed in the game, only Void init () {if (IS555) // 555 mode 0rrrrrrrrrrgggggBbbbbbbB {rmove = 10; Gmove = 5;} else // 565 mode rrrrrrrrrrrrrrrrrrrrrrrrgggggBbbb {rmove = 11 Gmove = 6; / * Note: Why here GMOVE = 6, because the G value in 565 mode has 6 bits, if a 6-bit value and two 5-bit values ​​are added to 3, the result may make 5 digits Overflow, so we have to move a multi-purpose, which is divided by 2 * /}} // 16-bit point transform Word RGB16TOGRAY (Word Sour; Word R, G, B; R = SOUR >> RMOVE; G = GMASK & SOUR) >> GMOVE; B = Bmask & Sour; T = (r * 3 b * 6 g) / 10; // Second Edition corrected place Return (t << rmove) | (t << Gmove | T;

Third, 8-bit conversion and the above two have a relatively large difference, because its color is determined by the palette, we only change the conversion by changing the palette, first use lpddpal-> getNtries () to get the adjustment The swatch, then convert the required palette values, and then use LPDDPAL-> setENTRIES (), the program is as follows:

/ / =========== 8 bits (Note: This function only applies to full screen conversion) =========== Void RGB8_TO_GRAY () {INT T; // temporary variable LPPALETTEENTRY PAL = (LPPALETTEENTRY) LOCALALLOC (LPTR, SIZEOF (PALETTEENTRY) * 256);

// Get the palette LPDDPAL-> GetENTRIES (0, 0, 256, PAL);

// Convert FOR (INT i = 0; I <256; i ) {t = (PAL [i] .pered * 3 PAL [i] .pegreen * 6 PAL [i] .peblue) / 10; ///// Second Edition corrected place PAL [i] .pleen = PAL [i] .pegreen = PAL [i] .peblue = t;} // update palette LPDDPAL-> setENTRIES (0,256, pe);}

转载请注明原文地址:https://www.9cbs.com/read-53600.html

New Post(0)