// RGB_to_grayscale.cpp : This file contains the 'main' function. Program execution begins and ends there.

/*
Program to include an h file containing {r,g,b} values and write a new .h file
containing only grayscale values {gray} where gray is the average of r, g, and b.

Rewritten for Visual Studio 2022 C++, to avoid the hassle of using Arduino to write to SD card.

*/

#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

#define imageRows 16  // the size of the image that is going to be displayed.  Height normally 16.
#define imageCols 167  // Change this number depending on the width of your scrolling image.
#define moonRows 48
#define moonCols 48
#define batUpRows 16
#define batUpCols 19
#define batDownRows 16
#define batDownCols 19
#define witchRows 16
#define witchCols 19

int main()
{

const uint8_t hhw[imageRows][imageCols][3] =
#include "Happy_Halloween_white_167x16.h"   // Happy Halloween written in white with black background
;
const uint8_t bu[batUpRows][batUpCols][3] =
#include "bat_up_19x16.h"   // black bat with wings up, on white background
;
const uint8_t bd[batDownRows][batDownCols][3] =
#include "bat_down_19x16.h"   // black bat with wings down, on white background
;
const uint8_t witch[witchRows][witchCols][3] =
#include "flying_witch_19x16.h"   // black witch silhouette on white background
;
const uint8_t moon[moonRows][moonCols][3] =
#include "full_moon_darker_48x48.h"   // full moon image on black background.
;

int col;
int row;
int avg;


 
    // open the file. note that only one file can be open at a time,
    // so you have to close this one before opening another.

      std::ofstream myFile("HHg.h");

      myFile << "{";
      for (row = 0; row < imageRows; row++) {
          myFile << "{";
            for (col = 0; col < imageCols; col++) {
                avg = (int(hhw[row][col][0]) + int(hhw[row][col][1]) + int(hhw[row][col][2])) / 3;
                myFile << avg;
                if (col < imageCols - 1) {
                    myFile << ",";
                }
            }
            if (row < imageRows - 1) {
                myFile << "},\n";
            }
            else {
                myFile << "}}" << std::endl;
            }
        }

        // close the file:
        myFile.close();

        myFile.open("moong.h");

        myFile << "{";
        for (row = 0; row < moonRows; row++) {
            myFile << "{";
            for (col = 0; col < moonCols; col++) {
                avg = (int(moon[row][col][0]) + int(moon[row][col][1]) + int(moon[row][col][2])) / 3;
                myFile << avg;
                if (col < moonCols - 1) {
                    myFile << ",";
                }
            }
            if (row < moonRows - 1) {
                myFile << "},\n";
            }
            else {
                myFile << "}}" << std::endl;
            }
        }

        // close the file:
        myFile.close();
 
        myFile.open("batupg.h");

        myFile << "{";
        for (row = 0; row < batUpRows; row++) {
            myFile << "{";
            for (col = 0; col < batUpCols; col++) {
                avg = (int(bu[row][col][0]) + int(bu[row][col][1]) + int(bu[row][col][2])) / 3;
                myFile << avg;
                if (col < batUpCols - 1) {
                    myFile << ",";
                }
            }
            if (row < batUpRows - 1) {
                myFile << "},\n";
            }
            else {
                myFile << "}}\n";
            }
        }

        // close the file:
        myFile.close();
        myFile.open("batdowng.h");

        myFile << "{";
        for (row = 0; row < batDownRows; row++) {
            myFile << "{";
            for (col = 0; col < batDownCols; col++) {
                avg = (int(bd[row][col][0]) + int(bd[row][col][1]) + int(bd[row][col][2])) / 3;
                myFile << avg;
                if (col < batDownCols - 1) {
                    myFile << ",";
                }
            }
            if (row < batDownRows - 1) {
                myFile << "},\n";
            }
            else {
                myFile << "}}\n";
            }
        }

        // close the file:
        myFile.close();

        myFile.open("witchg.h");

        myFile << "{";
        for (row = 0; row < witchRows; row++) {
            myFile << "{";
            for (col = 0; col < witchCols; col++) {
                avg = (int(witch[row][col][0]) + int(witch[row][col][1]) + int(witch[row][col][2])) / 3;
                myFile << avg;
                if (col < witchCols - 1) {
                    myFile << ",";
                }
            }
            if (row < witchRows - 1) {
                myFile << "},\n";
            }
            else {
                myFile << "}}\n";
            }
        }

        // close the file:
        myFile.close();

}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
