// stufpic.h
//
// Written by John A deLaubenfels, https://www.adaptune.com/steg.htm .
// Permission granted to use freely in any non-fraudulent way.
//
//
//
//
//
//



#pragma once

#include "globs.h"
#include "jgrlil.h"
#include "jarq.h"



// qxbitsPair

struct bitsPair {
   int nBits;                  // number of bits we can embed; might increase if hurdle isn't too high.
   int highRemain;              // any hurdle this high or higher won't bump nBits.

   bitsPair(int nb=0, int bh=0) : nBits(nb), highRemain(bh) {}
};  // struct bitsPair


// qx bitsLib

struct bitsLib {
   int                                numer;
   int                                denom;
   int                                maxBits;        // if > 0, never embed more bits than this
   int                                color;          // -1: everyone; 0: blue; 1: green; 2: red.
   int                                mostestBits;    // most bits that numer/denom can embed.
   double                             factCol;        // default 1.0; if less, don't stuff this color as hard.
   std::vector<std::vector<bitsPair>> vcVcLib;        // outer: iVal; inner: iOffset.

   bitsLib(int num=0, int den=0, int mb=0, int col=-1) : numer(num), denom(den), maxBits(mb), color(col), mostestBits(0), factCol(1.0) {
      this->clrPairs();
   } // bitsLib()...

   void clrPairs() {
      vcVcLib.clear();
      vcVcLib.resize(256);                                      // 256 iVals
      for(int iVal=0; iVal<256; iVal++) {
         std::vector<bitsPair> &vcPairs = vcVcLib[iVal];
         vcPairs.resize(256);                                   // 256 iOffsets
      } // for(int iVal=0; iVal<256; iVal++) 
   } // void clrPairs()

   inline int calcFast(int iVal, int iScatOff, int nHurdle) const {
      bitsPair bp = vcVcLib[iVal][iScatOff];

      int iRet = bp.nBits;
      if(bp.highRemain > nHurdle)
         iRet++;

      return iRet;
   } // inline int calcFast(int iVal, int iScatOff, int nHurdle) const

   int calcScattel(int iVal, int iScatOff, int nHurdle) const {
      if((iVal     < 0) || (iVal     >= 256)) return -1;
      if((iScatOff < 0) || (iScatOff >= 256)) return -1;

      bitsPair bp = vcVcLib[iVal][iScatOff];

      int iRet = bp.nBits;
      if(bp.highRemain > nHurdle)
         iRet++;

      return iRet;
   } // int calcScattel(int iVal, int iScatOff, int nHurdl) const 
}; // struct bitsLib


bool evalColor(const double *pFacts3);

std::vector<bitsLib> *makeVcbl(const double *pFacts3, std::vector<bitsLib> &vcBL, double dRat, int maxBits);
int updateVcbl(std::vector<bitsLib> &vcBL, double dRat);

double calcSafety(const jgrLil &jgGuy, double bytesEmbed, double bytesBack, double nReserve, double factSafety, int maxBits, bool bScatter, const std::vector<onePair> &vcBackRatios, twoPairs &twoGuys, bitsLib *pBitsLib, std::vector<bitsLib> *pvcBL);

int updateShrink(const jgrLil &jgGuy, int bytesEmbed, int bytesBack, bool bScatter, int &fileBytesOut, int &widPix, int &hgtPix, double &safety, int maxBits, const std::vector<onePair> &vcBackRatios, twoPairs &twoGuys, MT64 &anMT, const double *pColFacts);

int doEmbed(const jgrLil &jgSrc, jgrLil &jgDest, std::vector<unsigned char> &vcEmbed, const std::vector<unsigned long long> &passwordSeedVector, int widPix, int hgtPix, int maxBits, twoPairs &twoGuys, MT64 &anMTin, bool bScatter, bool bShuffle, bool bConserv, bool bTrim, onePair *pOneOverride, const double *pColFacts);

int doExtract(jgrLil &jgGuy, const std::vector<unsigned long long> &passwordSeedVector, const std::vector<onePair> &vcBackRatios, JarqAnch &jrqOut);



double bitsCanTakeScattered(const jgrLil &jgGuy, int    nNumer, int nDenom, int maxBits, double *pBitsPerIval=NULL, double *pErrsPerIval=NULL, bitsLib *pBitsLib=NULL, double *pColFacts=NULL);

inline double bytesCanTakeScattered(const jgrLil &jgGuy, int    nNumer, int nDenom, int maxBits, double *pBitsPerIval=NULL, double *pErrsPerIval=NULL, bitsLib *pBitsLib=NULL, double *pColFacts=NULL) {return bitsCanTakeScattered(jgGuy, nNumer, nDenom, maxBits, pBitsPerIval, pErrsPerIval, pBitsLib, pColFacts) / (double)BITS_PER_BYTE;}


#ifdef DONT_NEED
double bitsCanTakeScattered(const jgrLil &jgGuy, double dNumer, int nDenom, int maxBits);

inline double bytesCanTakeScattered(const jgrLil &jgGuy, double dNumer, int nDenom, int maxBits) {return bitsCanTakeScattered(jgGuy, dNumer, nDenom, maxBits) / (double)BITS_PER_BYTE;}
#endif // DONT_NEED


double bitsCanTakeScattered(const jgrLil &jgGuy, std::vector<bitsLib> &vcBL);   // new overload 12-23-25, each color.

inline double bytesCanTakeScattered(const jgrLil &jgGuy, std::vector<bitsLib> &vcBL) {return bitsCanTakeScattered(jgGuy, vcBL) / (double)BITS_PER_BYTE;}



int calcScatter(int iVal, int iScatoff, int nNumer, int nDenom, int nHurdl, int maxBits, bool bDbg, int *pRemainOut=NULL, double *pValShiftAvg=NULL);

std::string makeSillyWord(MT64 &anMT, int nSyllables);

static void initByTwo(MT64 &anMT, const std::vector<unsigned long long> &passwordSeedVector, const char *secondPassword);

static void initByThree(MT64 &anMT, const std::vector<unsigned long long> &passwordSeedVector, const char *secondPassword, const char *thirdPassword);

int applyBytesg3(jgrLil &ajg, const UCHAR *cBytes, int nBytes, int nNumer, int nDenom, int maxBits, MT64 &anMT, bool bBackward, bool bFillEntireFile, bool bScatter, const bitsLib *pBitsLib=NULL, bool bShuffle=false, bool bConserv=false, bool bTrim=false, const std::vector<bitsLib> *pvcBL=NULL);

int extractBytesg3(const jgrLil &ajg, UCHAR *&cBytes, int nValidIn, int nGuardNumer, int nGuardDenom, int maxBits, MT64 &anMT, bool bBackward, bool bScatter, const bitsLib *pBitsLib, bool bShuffle, bool bConserv, bool bTrim, const std::vector<bitsLib> *pvcBL, bool bBug);

int bitsCanTakeg(const jgrLil &ajg, int nGuard);
inline int bytesCanTakeg(const jgrLil &ajg, int nGuard) {return (bitsCanTakeg(ajg, nGuard) / BITS_PER_BYTE);}

int bitsCanTakeg3(const jgrLil &ajg, int nNumer, int nDenom, int maxBits);
inline int bytesCanTakeg3(const jgrLil &ajg, int nNumer, int nDenom, int maxBits) {return (bitsCanTakeg3(ajg, nNumer, nDenom, maxBits) / BITS_PER_BYTE);}

int bytesCanTakegPegged(const jgrLil &ajg, int nGuard);


