process_image.ino 2.4 KB

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