process_image.ino 2.3 KB

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