Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- FileImage reduceImageBitDepth(FileImage src, unsigned char bits) {
- FileImage result = allocateFileImage(src.width, src.height);
- unsigned char maximum8 = (unsigned char) (pow(2, 8) - 1);
- unsigned char maximumBits = (unsigned char) (pow(2, bits) - 1);
- unsigned char multiplier = maximum8 / maximumBits;
- if ((bits > 8) || (bits < 1)) {
- printf("Invalid bit depth %u", bits);
- exit(-1);
- }
- for (unsigned int y = 0; y < src.height; y++) {
- for (unsigned int x = 0; x < src.width; x++) {
- result.data[y][x] = (unsigned char) (round(((float) src.data[y][x] / maximum8) * maximumBits) * multiplier);
- }
- }
- // 8bit to 1bit
- //old gray level { src.data[0][0] = 240 }
- //maximum8 = 255
- //maximumBits = 2^1 - 1 = 1
- //multiplier = 255 / 1 = 255
- //new gray level { src.data[0][0] = (round(((float) 240 / 255) * 1) * 255) = 240 }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement