fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void
  5. convert_pixels(unsigned char *pixdata, unsigned char *convertedpix,
  6. int width, int height) {
  7. int i, j, k;
  8. unsigned int mask;
  9. unsigned char temp;
  10. for(i = 0; i < height; ++i) {
  11. for(j = 0; j < width; ++j) {
  12. k = 0;
  13. temp = *(pixdata + i * width + j);
  14. for (mask = 0x80; mask != 0; mask >>= 1) {
  15. if (mask != 0x10 && mask != 0x01) { /* code below will be
  16.   * executed 6 times */
  17. if (temp & mask) {
  18. *(convertedpix + i * width + k) = 0xFF;
  19. } else {
  20. *(convertedpix + i * width + k) = 0;
  21. }
  22. ++k;
  23. }
  24. }
  25. }
  26. }
  27. }
  28.  
  29. int main(void) {
  30. // your code goes here
  31. size_t h, w;
  32. unsigned char *pix, *conv_pix;
  33. h = 100;
  34. w = 200;
  35. pix = malloc(h * w * sizeof(unsigned char));
  36. if (pix == NULL) {
  37. perror("malloc failed for pix array");
  38. }
  39. conv_pix = calloc(h * w * 6, sizeof(unsigned char));
  40. if (conv_pix == NULL) {
  41. free(pix);
  42. perror("calloc failed for convpix array");
  43. }
  44. convert_pixels(pix, conv_pix, w, h);
  45. free(pix);
  46. free(conv_pix);
  47.  
  48. return 0;
  49. }
  50.  
Success #stdin #stdout 0s 2284KB
stdin
Standard input is empty
stdout
Standard output is empty