简体   繁体   中英

How can I convert an image with 16 bit depth to 8 bit depth

Specifically Im working with a grayscale image matrix with 16 bit pixel values. Im trying to encode this into a PNG image but the encoder only accepts pixel values of 8 bit depth.

The greyscale image has intensity values ranging from 0 to 65,535 (2^16 - 1) assuming you are dealing with unsigned numbers. To convert it to 8 bit values that range will be 0-255 (2^8 - 1). So you want to keep the most significant 8 bits of each number (the left most 8 bits). You can either divide each number by 2^8 and floor round the result, or bit shift the numbers to the right by 8 bits. Each right bit shift by n spaces is equivalent to dividing the decimal number by 2^n and flooring the result.

int rows;  
int cols;
unsigned int* image;

unsigned char*downConvertImage(unsigned int* img, int rows, int cols) {

    unsigned char* out = new unsigned char[rows*cols];

    for (int i = 0; i < rows*cols - 1; i++)
        out[i] = (unsigned char*) image[i]/256;

    return out;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM