Advertisement
Guest User

Untitled

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