Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. FileImage reduceImageBitDepth(FileImage src, unsigned char bits) {
  2. FileImage result = allocateFileImage(src.width, src.height);
  3.  
  4. unsigned char maximum8 = (unsigned char) (pow(2, 8) - 1);
  5. unsigned char maximumBits = (unsigned char) (pow(2, bits) - 1);
  6. unsigned char multiplier = maximum8 / maximumBits;
  7.  
  8. if ((bits > 8) || (bits < 1)) {
  9. printf("Invalid bit depth %u", bits);
  10. exit(-1);
  11. }
  12.  
  13. for (unsigned int y = 0; y < src.height; y++) {
  14. for (unsigned int x = 0; x < src.width; x++) {
  15. result.data[y][x] = (unsigned char) (round(((float) src.data[y][x] / maximum8) * maximumBits) * multiplier);
  16. }
  17. }
  18.  
  19. // 8bit to 1bit
  20. //old gray level { src.data[0][0] = 240 }
  21. //maximum8 = 255
  22. //maximumBits = 2^1 - 1 = 1
  23. //multiplier = 255 / 1 = 255
  24. //new gray level { result.data[0][0] = (round(((float) 240 / 255) * 1) * 255) = 240 }
  25.  
  26. return result;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement