writecolmap.c 788 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. /*
  5. * This code (and the devdraw interface) will have to change
  6. * if we ever get bitmaps with ldepth > 3, because the
  7. * colormap will have to be written in chunks
  8. */
  9. void
  10. writecolmap(Display *d, RGB *m)
  11. {
  12. int i, n, fd;
  13. char buf[64], *t;
  14. ulong r, g, b;
  15. sprint(buf, "/dev/draw/%d/colormap", d->dirno);
  16. fd = open(buf, OWRITE);
  17. if(fd < 0)
  18. drawerror(d, "writecolmap: open colormap failed");
  19. t = malloc(8192);
  20. if (t == nil)
  21. drawerror(d, "writecolmap: no memory");
  22. n = 0;
  23. for(i = 0; i < 256; i++) {
  24. r = m[i].red>>24;
  25. g = m[i].green>>24;
  26. b = m[i].blue>>24;
  27. n += sprint(t+n, "%d %lud %lud %lud\n", 255-i, r, g, b);
  28. }
  29. i = write(fd, t, n);
  30. free(t);
  31. close(fd);
  32. if(i != n)
  33. drawerror(d, "writecolmap: bad write");
  34. }