process_image.ino 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "process_image.h"
  2. void process_image(camera_fb_t *frame_buffer, CameraModel *model)
  3. {
  4. // If dithering is not disabled, perform dithering on the image. Dithering is the
  5. // process of approximating the look of a high-resolution grayscale image in a
  6. // lower resolution by binary values (black & white), thereby representing
  7. // different shades of gray.
  8. if (!model->isDitheringDisabled)
  9. {
  10. dither_image(frame_buffer, model); // Invokes the dithering process on the frame buffer.
  11. }
  12. uint8_t flipper_y = 0;
  13. // Iterating over specific rows of the frame buffer.
  14. for (uint8_t y = 28; y < 92; ++y)
  15. {
  16. Serial.print("Y:"); // Print "Y:" for every new row.
  17. Serial.write(flipper_y); // Send the row identifier as a byte.
  18. // Calculate the actual y index in the frame buffer 1D array by multiplying the
  19. // y value with the width of the frame buffer. This gives the starting index of
  20. // the row in the 1D array.
  21. size_t true_y = y * frame_buffer->width;
  22. // Iterating over specific columns of each row in the frame buffer.
  23. for (uint8_t x = 16; x < 144; x += 8)
  24. { // step by 8 as we're packing 8 pixels per byte.
  25. uint8_t packed_pixels = 0;
  26. // Packing 8 pixel values into one byte.
  27. for (uint8_t bit = 0; bit < 8; ++bit)
  28. {
  29. // Check the invert flag and pack the pixels accordingly.
  30. if (model->isInverted)
  31. {
  32. // If invert is true, consider pixel as 1 if it's more than 127.
  33. if (frame_buffer->buf[true_y + x + bit] > 127)
  34. {
  35. packed_pixels |= (1 << (7 - bit));
  36. }
  37. }
  38. else
  39. {
  40. // If invert is false, consider pixel as 1 if it's less than 127.
  41. if (frame_buffer->buf[true_y + x + bit] < 127)
  42. {
  43. packed_pixels |= (1 << (7 - bit));
  44. }
  45. }
  46. }
  47. Serial.write(packed_pixels); // Sending packed pixel byte.
  48. }
  49. ++flipper_y; // Move to the next row.
  50. Serial.flush(); // Ensure all data in the Serial buffer is sent before moving to the next iteration.
  51. }
  52. }