process_image.ino 2.4 KB

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