Define several auxiliary macros as follows.
// Split and merge byte # define hibits (w) (byte) (w)> 4) & 0xf) # define lobits (W) (byte) (w) & 0xf) # define makebits (A, B) (byte) (a) & 0xf) | (byte) (B) & 0xf) <4 ))
Simple operation...
// Read and write a 16-color image demo. Add 20 bool cimgprocess: test4bppindexedbitmap (cimgprocess * PTO) to the value of each pixel) {// first check whether the image is an 8-bit grayscale image if (m_pbmih-> bibitcount! = 4) {afxmessagebox (_ T ("fuck, this is not a 4-Digit Image"); Return false ;}// test byte gray = 0x4c; // assume that this byte is two pixels of a 16-level grayscale image, byte HI = hibits (Gray), // 0 xflo = lobits (Gray ); // 0 xcbyte test = makebits (0x34, 0x56); // combine the status of the two bytes into a new byte, therefore, 0x64trace3 ("high = 0x % x, low = 0x % x, test = 0x % x \ n", hi, lo, test) should be output here ); // byte A = (Lo + 20) % 0xf; byte B = (hi + 20) % 0xf; test = makebits (A, B ); trace3 ("A = 0x % x, B = 0x % x, test add = 0x % d \ n", a, B, test ); // working.................int nheight = getheight (), nwidth = getwidthpixel (); // pixel width trace2 ("width = % d, Height = % d \ n", nwidth, nheight ); int I, j; for (I = 0; I <nheight; I ++) {for (j = 0; j <nwidth/2; j ++) // nwidth indicates the number of pixels, but only nwidth/2 bytes {byte pixel2 = This-> m_lpdata [I] [J]; // HI = hibits (pixel2) containing two pixels; // pixel decomposition Lo = lobits (pixel2); A = (Lo + 20) % 0xf; B = (hi + 20) % 0xf; test = makebits (a, B); // merge pixel PTO-> m_lpdata [I] [J] = test; // write data} return true ;}